// © Copyright 1996, Joseph Bergin. All rights reserved.

#ifndef __Matrix__
#define __Matrix__

#include "Range.h"
#include <iostream.h>

template <int rows, int cols, class E>
class Matrix
{	public:
		Matrix ()
		:	_storage()
		{
		}
		
		Matrix (E& e)
		:	_storage(e)
		{
		}
		
		E& operator()(int i, int j)const
		{	return _storage(i,j);
		}
		
		Matrix<rows,cols,E> operator+(const Matrix<rows,cols,E> &m)const
		{	Matrix<rows,cols,E> result;
			for(int i = 1; i<=rows; i++)
				for(int j = 1; j<=cols; j++)
					result(i,j) = _storage(i,j) + m(i,j);
			return result;
		}

		Matrix<rows,cols,E> operator-(const Matrix<rows,cols,E> &m)const
		{	Matrix<rows,cols,E> result;
			int i,j;
			for(i = 1; i<=rows; i++)
				for(j = 1; j<=cols; j++)
					result(i,j) = _storage(i,j) - m(i,j);
			return result;
		}
/*		
		template <int n>  // should work in new C++
		Matrix<rows,n,E> operator*(const Matrix<cols,n,E> &m)const
		{	Matrix<rows,n,E> result;
			int i,j,k;
			for(i = 1; i<= rows; i++)
				for(j = 1; j <= n; j++)
				{	result(i,j) = 0;
					for(k = 1; k <= cols; k++)
						result(i,j) := _storage(i,k) * m(k,j);
				}
			return result;
		}
*/
		Matrix<rows, cols, E> operator*(const E& e)
		{	Matrix<rows, cols, E> result;
			for(int i = 1; i<=rows; i++)
				for(int j = 1; j<=cols; j++)
					result(i,j) = e * _storage(i,j);
			return result;
		}
		
	private:
	FreeArray2<1, rows, 1, cols, E> _storage;
};


#endif
