// // $Source: /cvsroot/gambit/gambit/sources/libgambit/vector.imp,v $ // $Date: 2006/01/07 06:37:06 $ // $Revision: 1.3 $ // // DESCRIPTION: // Implementation of vector class // // This file is part of Gambit // Copyright (c) 2002, The Gambit Project // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // #include "vector.h" namespace Gambit { //------------------------------------------------------------------------ // Vector: Constructors, destructor, constructive operators //------------------------------------------------------------------------ template Vector::Vector(unsigned int len) : Array(len) { } template Vector::Vector(int low, int high) : Array(low, high) { } template Vector::Vector(const Vector &V) : Array(V) { } template Vector::~Vector() { } template Vector& Vector::operator=(const Vector& V) { if (!Check(V)) throw DimensionException(); Array::operator=(V); return *this; } //------------------------------------------------------------------------ // inline arithmetic operators //------------------------------------------------------------------------ template bool Vector::operator!=(const Vector &V) const { return !(*this == V); } template bool Vector::operator!=(T c) const { return !(*this == c); } //------------------------------------------------------------------------ // inline internal functions //------------------------------------------------------------------------ template bool Vector::Check(const Vector &v) const { return (v.mindex == this->mindex && v.maxdex == this->maxdex ); } //------------------------------------------------------------------------ // Vector: arithmetic operators //------------------------------------------------------------------------ template Vector& Vector::operator=(T c) { for(int i=this->mindex; i<=this->maxdex; i++) (*this)[i]= c; return (*this); } // arithmetic operators template Vector Vector::operator+(const Vector& V) const { if (!Check(V)) throw DimensionException(); Vector tmp(this->mindex,this->maxdex); for(int i=this->mindex; i<=this->maxdex; i++) tmp[i]= (*this)[i] + V[i]; return tmp; } template Vector Vector::operator-(const Vector& V) const { if (!Check(V)) throw DimensionException(); Vector tmp(this->mindex,this->maxdex); for(int i=this->mindex; i<=this->maxdex; i++) tmp[i]= (*this)[i] - V[i]; return tmp; } template Vector& Vector::operator+=(const Vector& V) { if (!Check(V)) throw DimensionException(); for(int i=this->mindex; i<=this->maxdex; i++) (*this)[i] += V[i]; return (*this); } template Vector& Vector::operator-=(const Vector& V) { if (!Check(V)) throw DimensionException(); for(int i=this->mindex; i<=this->maxdex; i++) (*this)[i] -= V[i]; return (*this); } template Vector Vector::operator-(void) { Vector tmp(this->mindex,this->maxdex); for(int i=this->mindex; i<=this->maxdex; i++) tmp[i]= -(*this)[i]; return tmp; } template Vector Vector::operator*(T c) const { Vector tmp(this->mindex,this->maxdex); for(int i=this->mindex; i<=this->maxdex; i++) tmp[i]= (*this)[i]*c; return tmp; } template Vector &Vector::operator*=(T c) { for(int i=this->mindex; i<=this->maxdex; i++) (*this)[i] *= c; return (*this); } template T Vector::operator*(const Vector& V) const { if (!Check(V)) throw DimensionException(); T sum= (T)0; for(int i=this->mindex; i<=this->maxdex; i++) sum += (*this)[i] * V[i]; return sum; } template Vector Vector::operator/(T c) const { Vector tmp(this->mindex,this->maxdex); for(int i=this->mindex; i<=this->maxdex; i++) tmp[i]= (*this)[i]/c; return tmp; } template bool Vector::operator==(const Vector& V) const { if (!Check(V)) throw DimensionException(); for(int i=this->mindex; i<=this->maxdex; i++) if( (*this)[i] != V[i] ) return false; return true; } template bool Vector::operator==(T c) const { for(int i=this->mindex; i<=this->maxdex; i++) if( (*this)[i] != c ) return false; return true; } template T Vector::NormSquared(void) const { T answer = (T)0; for (int i = 1; i <= this->Length(); i++) answer += (*this)[i] * (*this)[i]; return answer; } } // end namespace Gambit