/* This is the file Vector.C.  It contains the functions definitions for the
Vector<T> classe.  This file is not to be compliled it is included at the end 
of the Matrix.h file.  The files for the vector class are ver similar to those 
in the matrix class. */

/*These are the basic functions that were defined in class for 1D arrays.
There is minimal commenting here for that reason.*/

//the default constructor for vectors
template <class T>
Vector<T>::Vector<T>()
{
    num_elts = 0;
    dimension = 1;
    ptr_to_data=0;
}

//will create a vector with n elements
template <class T>
Vector<T>::Vector(int n)
{
    num_elts=n;
    dimension = 1;
    ptr_to_data = new T [n];  //creates the new vector
}

//implements the copy constructor for the Vector<T> class and its daughters
template <class T>
Vector<T>::Vector(const Vector<T>& v)
{
    num_elts=v.num_elts;
    ptr_to_data = new T [num_elts];
    copy (v);  //calls the private vector copy function
}

//the vector destructor
template <class T>
Vector<T>::~Vector()
{
    delete [] ptr_to_data;
}

/*I have overloaded the () operator as opposed to [] which is what was used for
the class in lecture.  This helps to create a greazter continuity betwee the
vevector and matrix classes.  Further, if I figure out how to do it, this
operator will be added to the base class so that exists in any array class that
might be added to this structure.  As ih the matrix cases, there are const and
non-const versions of the operaotr.*/

//gets the vector element but can't change it
template <class T>
inline const T& Vector<T>::operator() (int i) const
{
    if (i>=0 && i<num_elts) //makes sure the element actually exists
        return ptr_to_data[i];
    else
    {//throws if asking for an element that does not exist
        throw InvalidRange(i,__FILE__,__LINE__);
    }
}

//same as above but can change the value of elements
template <class T>
inline T& Vector<T>::operator() (int i)
{
    if (i>=0 && i<num_elts) //makes sure that the element is actually there
        return ptr_to_data[i];
    else
    {
        throw InvalidRange (i, __FILE__, __LINE__);
    }
}

//virtual function - returns the number of elements in the vector
template <class T>
int Vector<T>::numEls()
{
    return num_elts;
}

//virtual function - returns the dimension of the array
template <class T>
int Vector<T>::dim()
{
    return dimension;
}

//allows you to set two vectors equal to each other
template <class T>
Vector<T>& Vector<T>::operator = (const Vector<T>& v)
{
    if ( ptr_to_data != v.ptr_to_data )//check if already equal
    {
        setSize (v.num_elts);
        copy (v);
    }
    return *this;
}

//set each element equal to a number
template <class T>
Vector<T>& Vector<T>::operator = (const T val)
{
    for (int i=0; i<num_elts; i++)
        ptr_to_data[i]=val;
    return *this;
}

template <class T>
Vector<T>& Vector<T>::transpose()
{
    return *this;
}

/*As in the case of matrices, the setSize function allows you to change the
number of elements in a previously defined vector.  Again, please note, that
the information that was stored in the original vector will be lost.  If you
are going to need that data, you will have to make a copy of the vector before
you change its size.*/

template <class T>
void Vector<T>::setSize(int n)
{
    if ( n!=num_elts )
    {
        delete [] ptr_to_data;
        num_elts = n;
        ptr_to_data = new T [n];
    }
}


//this allows you to overload << operator polymorphically
template <class T>
void Vector<T>::display(ostream& out)
{
     for(int i=0; i<num_elts; i++)
        out<<setw(12)<<setprecision(6)<<ptr_to_data[i]<<endl;
}

/*As in the matrix case, the copy function is used by the = operator (with the
use of the copy constructor) to copy the data from one vector into another.*/

template <class T>
void Vector<T>::copy (const Vector<T>& v)
{
    for (int i=0; i<num_elts; i++)
        ptr_to_data[i]=v.ptr_to_data[i];
}

/*As with the matrix class, the overloaded operators for arithmetic operations
are ging to be defined.*/

//this will add two vectors together
template <class T>
inline Vector<T> Vector<T>::operator + (const Vector<T>& v)
{
    Vector<T> tmp(num_elts);
    if ( num_elts = v.num_elts )
    {
        for (int i=0; i<num_elts; i++)
            tmp.ptr_to_data[i]=ptr_to_data[i]+v.ptr_to_data[i];
        return tmp;
    }
    else
    {//throw if the vectors aren't the same size
        throw InvalidSizes("num_elts", "v.num_elts", __FILE__,__LINE__);
    }
}

//this will add a number to each element of the vector
template <class T>
inline Vector<T> Vector<T>::operator + (const T val)
{
    Vector<T> tmp(num_elts);
    for (int i=0; i<numEls(); i++)
            tmp.ptr_to_data[i] = ptr_to_data[i]+val;
        return tmp;
}

//+= with two vectors
template <class T>
inline Vector<T>& Vector<T>::operator += (const Vector<T>& v)
{
    if ( num_elts = v.num_elts )
    {
        for (int i=0; i<num_elts; i++)
            ptr_to_data[i]=ptr_to_data[i]+v.ptr_to_data[i];
        return *this;
    }
    else
    {//throw if the vectors aren't the same size
        throw InvalidSizes("num_elts", "v.num_elts", __FILE__,__LINE__);
    }
}

//+= with a number and a vector
template <class T>
inline Vector<T>& Vector<T>::operator += (const T val)
{
    for (int i=0; i<numEls(); i++)
       ptr_to_data[i] = ptr_to_data[i]+val;
    return *this;
}

/*The next operators to be overloaded ar the subtraction operators.  As in the 
case of addition, there are five functions in the section.  This allows the 
subtraction of two vectors, number-vector, vector - number, and -= with vector 
and number.  These functions are almost identical to those in the Matrix 
class.*/

//will subtract one vector from another
template <class T>
inline Vector<T> Vector<T>::operator - (const Vector<T>& v)
{
    Vector<T> tmp(num_elts);
    if ( num_elts = v.num_elts )
    {
        for (int i=0; i<num_elts; i++)
            tmp.ptr_to_data[i]=ptr_to_data[i]-v.ptr_to_data[i];
        return tmp;
    }
    else
    {//throws if the two vectors aren't the same size
        throw InvalidSizes("num_elts", "v.num_elts", __FILE__,__LINE__);
    }
}

//will subtract a number from a vector
template <class T>
inline Vector<T> Vector<T>::operator - (const T val)
{
    Vector<T> tmp(num_elts);
    for (int i=0; i<num_elts; i++)
            tmp.ptr_to_data[i]=ptr_to_data[i]-val;
    return tmp;
}

//-= with two vectors
template <class T>
inline Vector<T>& Vector<T>::operator -= (const Vector<T>& v)
{
    if ( num_elts = v.num_elts )
    {
        for (int i=0; i<num_elts; i++)
            ptr_to_data[i]=ptr_to_data[i]-v.ptr_to_data[i];
        return *this;
    }
    else
    {//throws if the two vectors aren't the same size
        throw InvalidSizes("num_elts", "v.num_elts", __FILE__,__LINE__);
    }
}

//-= with a vector and a number
template <class T>
inline Vector<T>& Vector<T>::operator -= (const T val)
{
    for (int i=0; i<num_elts; i++)
        ptr_to_data[i]=ptr_to_data[i]-val;
    return *this;
}

/*Now we come to the multiplication operations for vectors.  There are more 
functions here than there are in the Matrix class.  There is the vector dot 
product, vector * number, number * vector, matrix * vector, vector*matrix, and 
*= with a vector and a number and with a vector and a matrix.  There is no *= 
with a vector because the return type of this operation is a number. */

//will compute the vector dot product
template <class T>
inline T Vector<T>::operator * (const Vector<T>& v)
{
    T dot =0;
    if ( num_elts==v.num_elts)
    {
        for (int i=0; i<num_elts; i++)
            dot += (ptr_to_data[i]*v.ptr_to_data[i]);
        return dot;
    }
    else
    {//throws if the two vectors aren't the same size
        throw InvalidSizes("num_elts", "v.num_elts", __FILE__,__LINE__);
    }
}

//numtiplies a number to each element of the vector
template <class T>
inline Vector<T> Vector<T>::operator * (const T val)
{
    Vector<T> tmp(num_elts);
    for (int i=0; i<num_elts; i++)
        tmp.ptr_to_data[i]=ptr_to_data[i]*val;
    return tmp;
}

//multiplies a vector and a matrix to yield a vector
template <class T>
inline Vector<T> Vector<T>::operator * (const Matrix<T>& b)
{
    Vector<T> tmp(num_elts);
    tmp = 0;
    T sum = 0;
    if (num_elts == b.numRows())
    {
            for (int i=0; i<num_elts; i++)
            {   sum = 0;
                for (int j=0; j<b.numRows(); j++)
                    sum+=ptr_to_data[j]*b(j,i);
                tmp(i)=sum;
            }
    return tmp;
    }
    else
    {//length of vector must match number of matrix rows
        throw InvalidSizes("num_elts", "b.numRoWs()", __FILE__, __LINE__);
    }
}

//*= with a number
template <class T>
inline Vector<T>& Vector<T>::operator *= (const T val)
{
    for (int i=0; i<num_elts; i++)
        ptr_to_data[i]=ptr_to_data[i]*val;
    return *this; 
}

//*= with a matrix
template <class T>
inline Vector<T>& Vector<T>::operator *= (const Matrix<T>& b)
{
    Vector<T> tmp(num_elts);
    tmp = 0;
    T sum;
    if (num_elts == b.numRows())
    {
            for (int i=0; i<num_elts; i++)
            {   sum = 0;
                for (int j=0; j<b.numRows(); j++)
                    sum+=ptr_to_data[j]*b(j,i);
                tmp(i)=sum;
            }
    *this = tmp;
    return *this;
    }
    else
    {//length of vector must match number of matrix rows
        throw InvalidSizes("num_elts", "b.numRoWs()", __FILE__, __LINE__);
    }
}

//functions to divide the vector elements by a number

//divides each element of the vector by val
template <class T>
inline Vector<T> Vector<T>::operator / (const T val)
{
    Vector<T> tmp(num_elts);
    for (int i=0; i<num_elts; i++)
        tmp.ptr_to_data[i] = ptr_to_data[i]/val;
    return tmp;
}

//divides each element of the vector by val
template <class T>
inline Vector<T>& Vector<T>::operator /= (const T val)
{
    Vector<T> tmp(num_elts);
    for (int i=0; i<num_elts; i++)
        tmp.ptr_to_data[i] = ptr_to_data[i]/val;
    *this = tmp;
    return *this;
}


