/*This file contains the function definitions for that global functions.  These
functions are the ones which were defined before the Matrix class structure.
As in the cas of Matrix.C and Vector.C, this file is not meant to be
compiled.  It has been created to make it easier to find the functions that
have been declared in Matrix.h.  This file is included at the end of Matrix.h
and must be downloaded for the test programs to compile and run properly.*/

//defines a number + a matrix (matrix + number is in Matrix.C)
template <class T>
inline Matrix<T> operator + (const T val, const Matrix<T>& b)
{
	Matrix<T> tmp(b.numRows(), b.numCols());
	for (int i=0; i<b.numRows(); i++){
	    for (int j = 0; j<b.numCols(); j++)
		    tmp(i,j)=b(i,j)+val;
    }
	return tmp;
}

//defines a number - a matrix (matrix - number is in Matrix.C)
template <class T>
inline Matrix<T> operator - (const T val, const Matrix<T>& b)
{
	Matrix<T> tmp(b.numRows(), b.numCols());
	for (int i=0; i<b.numRows(); i++){
	    for (int j = 0; j<b.numCols(); j++)
		    tmp(i,j)=val-b(i,j);
	}
	return tmp;
}

//defines a number * a matrix (matrix * number is in Matrix.C)
template <class T>
inline Matrix<T> operator * (const T val, Matrix<T>& b)
{
	Matrix<T> tmp(b.numRows(), b.numCols());
	for (int i=0; i<b.numRows(); i++){
	    for (int j=0; j<b.numCols(); j++)
		    tmp(i,j)=b(i,j)*val;
	}
	return tmp;
}

//defines a number / a matrix (matrix / number is in Matrix.C)
template <class T>
inline Matrix<T> operator / (const T val, const Matrix<T>& b)
{
    Matrix<T> tmp = inverse(b);
    tmp *= val;
    return tmp;
}

//will create upper triangular matrix to find determinant
template <class T>
Matrix<T> gauss (const Matrix<T>& a)
{	
	Matrix<T> b=a;
	if (b.numCols()==b.numRows()){
		T t;
		for (int i=0; i<b.numRows();i++)
		{
			int max = i;
			for (int j=i+1; j<(b.numRows()-1);j++)
			{
				if (fabs (b(j,i))>fabs(b(max,i)))
					max = j;
			}
			for (int k=i; k<b.numRows(); k++)
			{
				t=b(i,k);
				b(i,k) = b(max,k);
				b(max,k)=t;
			}
			for (int j=i+1; j<b.numRows(); j++)
			{
				for (int k=(b.numRows()-1); k>=i; k--)
					b(j,k)-=b(i,k)*b(j,i)/b(i,i);
			}
		}
		return b;
	}
	else
		throw InvalidSizes("numRows()", "numCols()", __FILE__, __LINE__);
}

/*inverse will find the inverse of a matrix using a method similar to Gauss -
Jordan elimination.  It should be noted that matrix inversion is an n cubed
process and should not be attempted for extremely large matrices.*/

template <class T>
Matrix<T> inverse(const Matrix<T>& m)
{
    if (m.numRows()==m.numCols())
    {
        int bigcol;
        T bigval, bv;
        Matrix <T> big(m.numRows(), 2*m.numCols());
        Vector<T> tmp(big.numCols());
        for (int i=0; i<m.numRows(); i++)
            for (int j=0; j < m.numCols(); j++)
            {
                big(i,j)=m(i,j);
                big(i,j+m.numCols())=(i==j?1.0:0.0);
            }
        for (int i=0; i<big.numRows(); i++)
        {
            bigcol = i;
            bigval = fabs(m(i,m.numCols()-1));
            for (int j=i+1; j<m.numRows(); j++)
                if ((bv=fabs(big(j,i)))>bigval)
                    bigval=bv,bigcol=j;
            if (i != bigcol)
                big.swaprow(i,bigcol);
            big.scalerow(i,(1.0/big(i,i)));
            for (int j=i+1;j<big.numRows();j++)
            {
                for (int k=0; k<tmp.numEls(); k++){
                    tmp(k) = big(i,k)*(-1.0*big(j,i));
                }
                big.addrow(j, tmp);
            }
        }
        for (int i=(big.numRows()-1);i>=0; i--)
        {
            for (int j=i-1; j>=0; j--)
            {
                for (int k=0; k<big.numCols(); k++)
                    tmp(k) = big(i,k)*(-1.0*big(j,i));
                big.addrow(j,tmp);
            }
        }
        Matrix<T> res(m.numRows(), m.numCols());
        for (int i=0; i<m.numRows(); i++)
            for (int j=0; j<m.numCols(); j++)
                res(i,j)=big(i,j+m.numCols());
        return res;
    }
    else
    {
        throw InvalidSizes("numRows()", "numCols()", __FILE__, __LINE__);
    }
}

//defines a number + vector (vector + number is defined in Vector.C)
template<class T>
inline Vector<T> operator + (const T val,  Vector<T>& v)
{
    Vector<T> tmp(v.numEls());
    for (int i=0; i<v.numEls(); i++)
         tmp(i) = v(i)+val;
    return tmp;
}

//defines a number - vector (vector - number is defined in Vector.C)
template<class T>
inline Vector<T> operator - (const T val, Vector<T>& v)
{
    Vector<T> tmp(v.numEls());
    for (int i=0; i<v.numEls(); i++)
            tmp(i)=val-v(i);
    return tmp;
}

//defines a number * vector (vector * number is defined in Vector.C)
template <class T>
inline Vector<T> operator * (const T val, Vector<T>& v)
{
    Vector<T> tmp(v.numEls());
    for (int i=0; i<v.numEls(); i++)
        tmp(i)=v(i)*val;
    return tmp;
}

//defines a matrix * vector (vector * matrix is defined in Vector.C)
//In this case the vector is assumed to be a column vector.
template <class T>
inline Vector<T> operator * (const Matrix<T>& b, Vector<T>& v)
{
    Vector<T> tmp(v.numEls());
    tmp = 0;
    T sum;
    if (v.numEls() == b.numCols())
    {
            for (int i=0; i<v.numEls(); i++)
            {   sum = 0;
                for (int j=0; j<b.numCols(); j++)
                    sum+=v(j)*b(i,j);
                tmp(i)=sum;
            }
    return tmp;
    }
    else
    {//length of vector must match number of matrix rows
        throw InvalidSizes("num_elts", "b.numCols()", __FILE__, __LINE__);
    }
}






