/*This file contains the class definitions for the objects of this Matrix library.
At the end of this file, the files Vector.C and Matrix.C are included.  These files
are not meant to be compiled, but they have the function definitions for the matrix
and vector classes.*/

#ifndef Matrix_H
#define Matrix_H

#include <iostream.h>
#include <iomanip.h>
#include <math.h>
#include "Error.h"  //this will handle the error that may occur when running

/* These are the classes that are declared in this file.  Only Arraymd has its
function declarations in this file.*/

template <class T> class Arraymd;
template <class T> class Matrix;
template <class T> class Vector;

/*These declarations are for global functions.  These are done as global 
because I can't overload built in data types.  As the user, you will never 
notice these (except for gauss) but they allow you to create things like 
1.2*a-b+3.5+c without worrying about the ordering of the variables.*/

template <class T>  Matrix<T>  operator + (const T, const Matrix<T>&);
template <class T>  Matrix<T>  operator - (const T, const Matrix<T>&);
template <class T>  Matrix<T>  operator * (const T, Matrix<T>&);
template <class T>  Matrix<T>  operator / (const T, const Matrix<T>&);
template <class T>  Matrix<T>  gauss(const Matrix<T>& a);
template <class T>  Matrix<T>  inverse (const Matrix<T>& m);
template <class T>  Vector<T>  operator + (const T, const Vector<T>&);
template <class T>  Vector<T>  operator - (const T, const Vector<T>&);
template <class T>  Vector<T>  operator * (const T, Vector<T>&);
template <class T>  Vector<T>  operator * (const Matrix<T>&, Vector<T>&);

/*The main classes listed below are Matrix and Vector.  The class Arraymd is the
base class for these two. It is included so that arrays of dimension greater than 2
can be easily inserted into the existing program structure.  The Arraymd class is
virtual and therefore there can be no objects of it.  If you need an array with
more than two dimensions, you will need to modify this code to handle it.*/

template <class T>
class Arraymd
{//abstract class for an array of arbitrary dimension

public:
/* These basic elements are needed by any array. */
    virtual int numEls()=0; //number of array elements
    virtual int dim() = 0;  //array dimension
    virtual Arraymd<T>& transpose()=0;
    virtual Arraymd<T>& operator = (const T)=0;  //set all elements to a number
    virtual void display(ostream& out)=0;
    virtual ~Arraymd() {} //destructor
};

/*The Matrix class is the main focus of this program.  All arithmetic operators
have been overloaded so that matrix objects can be used in expressions just like
built in data types might be.  In addition, you can acces any element of the
matrix, copy matrices, transpose them, and find their determinants.  The friend
fuction gauss finds the upper triangular equivalent to the input matrix.  If a
solution vector is included, it would then be straightforward to implement
backsubstitution to solve a system of linear equations.  However. The code is
designed for you to divide the solution vector by a matrix to solve such systems.*/


template <class T>
class Matrix:public Arraymd<T>
{

public:
	Matrix();	//Creates zero element array
	Matrix (int n, int m);	  //Creates an ixj array
	Matrix (const Matrix<T>&); //Copy constructor for Matrix
	virtual ~Matrix();	//Destructor for Matrix
	const T& operator () (int i, int j) const;  //access matrix elements
	T& operator () (int i, int j);
	int numRows() const;	//gets number of rows
	int numCols() const;	//gets number of columns
	virtual int numEls();	//gets the total number of elements
	virtual int dim();      //returns the dimension of the array
	Matrix<T>& operator = (const Matrix<T>&);	//copies array
	virtual Matrix<T>& operator = (const T);	//sets all elements to number 	
    void setSize(int n, int m);	//change the array dimensions
    virtual void display(ostream& out);//used to overload << operator

//these functions will allow the user to manipulate row/cols of matrix
    Matrix<T>& addrow(int i, Vector<T>& v);//add v to row i
    Matrix<T>& addcol(int i, Vector<T>& v);//add v to ith col
    Matrix<T>& subrow(int i, Vector<T>& v);//subtract v from ith row
    Matrix<T>& subcol(int i, Vector<T>& v);//subtract c from ith colum
    Matrix<T>& setrow (int i, const T& val); //set ith row = val
    Matrix<T>& setcol (int i, const T& val); //set ith col = val
    Matrix<T>& scalerow (int i, const T& val); //multiply ith row by val
    Matrix<T>& scalecol (int i, const T& val); //multiply ith col by val
    Matrix<T>& swaprow (int i, int j); //switches the ith and jth rows
    Matrix<T>& swapcol (int i, int j); //switched the ith and jth col

//function declarations for matrix addition
	Matrix<T> operator + (const Matrix<T>& b); //add matrices
	Matrix<T> operator + (const T val); //add a number to a matrix
	Matrix<T>& operator += (const Matrix<T> &b);
	Matrix<T>& operator += (const T val);

//function declarations for matrix subtraction
	Matrix<T> operator - (const Matrix<T>& b);//subtract matrices
	Matrix<T> operator - (const T val);//sebtract a number from a matrix
	Matrix<T>& operator -= (const Matrix<T>& b);
	Matrix<T>& operator -= (const T val);

//function decalrations for matrix multiplication
	Matrix<T> operator * (const Matrix<T>& b);//multiply matrices
	Matrix<T> operator * (const T val);//multiply a number and a matrix
	Matrix<T>& operator *= (const Matrix<T>& b);
	Matrix<T>& operator *= (const T val);
	
//function declarations for matrix division
    Matrix<T> operator / (const Matrix<T>& b); //divide two matrices
    Matrix<T> operator / (const T val); //divide matrix by a number
    Matrix<T>& operator /= (const Matrix<T>& b);
    Matrix<T>& operator /= (const T val);

//useful matrix operations 
	virtual Matrix<T>& transpose();  //finds the matrix transpose
	T determinant(); //computes the determinant of a square matrix

private:
	int num_rows;	//number of rows in the array
	int num_cols;	//number of colums in the array
	int dimension;  //dimension of the array
	T* ptr_to_data;	 //where the matrix elements are stored
	void copy(const Matrix<T>& a);	//copy the elements of matrix
};


/*It is only natural to define vectors if you have defined matrices.  The structure
of the two classes is almost identical.  Even though so many of the functions are
the same, I have not been able to find a way to implement them in the base class
because of the differing number of dimensions.  If you have any suggestions on how
you might do this I would appreciate the help.  In this class, I have defined
a vector times a matrix.  However, division of two vectors is not an actual
operation (at least not as far as I know) so that function that from the matrix
class is not present here.*/


template <class T>
class Vector : public Arraymd<T>
{

public:
    Vector();  //default constructor
    Vector(int n); //created an n element vector
    Vector(const Vector<T>&);  //defines the copy constructor for vector
    virtual ~Vector();  //destructor for the vector class
    const T& operator() (int i) const; //access the element v(i)
    T& operator() (int i);
    virtual int numEls(); //get the number of elements in the vector
    virtual int dim();    //returns the dimension of the array
    Vector<T>& operator = (const Vector<T>&); //sets two vectors equal
    virtual Vector<T>& operator = (const T val); //set all elemts to a number
    virtual Vector<T>& transpose(); //should not be used
    void setSize (int n); //resize your original vector
    virtual void display (ostream& out);

//function declarations for vector addition
    Vector<T> operator + (const Vector<T>& v); //add two vectors
    Vector<T> operator + (const T val); //add vector to a number
    Vector<T>& operator += (const Vector<T>& v);
    Vector<T>& operator += (const T val);

//function declarations for vector subtraction
    Vector<T> operator - (const Vector<T>& v); //subtract two vectors
    Vector<T> operator - (const T val); //subtract number from a vector
    Vector<T>& operator -= (const Vector<T>& v);
    Vector<T>& operator -= (const T val);

//function declarations for vector multiplication
    T operator * (const Vector<T>& v); //compute the dot product
    Vector<T> operator * (const T val); //scale the vector elements
    Vector<T> operator * (const Matrix<T>& b); //matrix vector multiplication
    Vector<T>& operator *= (const T val);
    Vector<T>& operator *= (const Matrix<T>& b);
    
//vector scaling functions with division with a number
    Vector<T> operator / (const T val); //divide vector elements by val
    Vector<T>& operator /= (const T val);

protected:
    int num_elts;    //number elements in the vector
    int dimension;   //array dimension
    T* ptr_to_data;  //where vector elemeents are stored
    void copy (const Vector<T>& v); //copy one vector into another

};


//The intermediate functions for transpose, dimension, and numEls.

template <class T>
int NumElts(Arraymd<T>& m)
{//finds out the number of array elements
    m.numEls();
}

template <class T>
int Dimension(Arraymd<T>& m)
{//returns the dimension of the array
    m.dim();
}

template<class T>
ostream& operator << (ostream& o, Arraymd<T>& m)
{
    m.display(o);
    return o;
}


#include "Global.C" //where global files are defined
#include "Matrix.C" //where matrix files are defined
#include "Vector.C" //where vector files are defined

#endif






	

    


     



