// // $Source: /cvsroot/gambit/gambit/sources/libgambit/dvector.imp,v $ // $Date: 2006/01/07 06:37:06 $ // $Revision: 1.2 $ // // DESCRIPTION: // Implementation of doubly-partitioned 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 "dvector.h" namespace Gambit { //-------------------------------------------------------------------------- // DVector: Private and protected member functions //-------------------------------------------------------------------------- template int DVector::sum(int part, const PVector &v) const { int s = 0; Array len(v.Lengths()); for (int j = 1; j <= len[part]; j++) s += v(part, j); return s; } template void DVector::setindex(void) { int index = 1; for (int i = 1; i <= dvlen.Length(); i++) { dvptr[i] = this->svptr + index - 1; dvidx[i] = index; index += dvlen[i]; } } template bool DVector::Check(const DVector &v) const { for (int i = 1; i <= dvlen.Length(); i++) if (dvlen[i] != v.dvlen[i]) return false; return true; } //-------------------------------------------------------------------------- // DVector: Constructors, destructor, and constructive operators //-------------------------------------------------------------------------- template DVector::DVector(void) : dvptr(0) { } template DVector::DVector(const PVector &sig) : PVector((Array)sig), dvlen(sig.Lengths().Length()), dvidx(sig.Lengths().Length()) { dvptr = new T **[dvlen.Length()]; dvptr -= 1; for (int i = 1; i <= dvlen.Length(); i++) dvlen[i] = sig.Lengths()[i]; setindex(); } template DVector::DVector(const Vector &val, const PVector &sig) : PVector(val, sig), dvlen(sig.Lengths().Length()), dvidx(sig.Lengths().Length()) { dvptr = new T **[dvlen.Length()]; dvptr -= 1; for (int i = 1; i <= dvlen.Length(); i++) dvlen[i] = sig.Lengths()[i]; setindex(); } template DVector::DVector(const DVector &v) : PVector(v), dvlen(v.dvlen), dvidx(v.dvidx) { dvptr = new T **[dvlen.Length()]; dvptr -= 1; setindex(); } template DVector::~DVector() { if (dvptr) delete [] (dvptr + 1); } template DVector &DVector::operator=(const DVector &v) { if (!Check(v)) { throw DimensionException(); } PVector::operator=(v); return *this; } template DVector &DVector::operator=(const PVector &v) { PVector::operator=(v); return *this; } template DVector &DVector::operator=(const Vector &v) { PVector::operator=(v); return *this; } template DVector &DVector::operator=(T c) { PVector::operator=(c); return *this; } //-------------------------------------------------------------------------- // DVector: Operator definitions //-------------------------------------------------------------------------- template T &DVector::operator()(int a, int b, int c) { if (dvlen.First() > a || a > dvlen.Last()) { throw IndexException(); } if (1 > b || b > dvlen[a]) { throw IndexException(); } if (1 > c || c > this->svlen[dvidx[a] + b - 1]) { throw IndexException(); } return dvptr[a][b][c]; } template const T &DVector::operator()(int a, int b, int c) const { if (dvlen.First() > a || a > dvlen.Last()) { throw IndexException(); } if (1 > b || b > dvlen[a]) { throw IndexException(); } if (1 > c || c > this->svlen[dvidx[a] + b - 1]) { throw IndexException(); } return dvptr[a][b][c]; } template DVector DVector::operator+(const DVector &v) const { if (!Check(v)) { throw DimensionException(); } DVector tmp(*this); tmp.PVector::operator+=(v); return tmp; } template DVector &DVector::operator+=(const DVector &v) { if (!Check(v)) { throw DimensionException(); } PVector::operator+=(v); return *this; } template DVector DVector::operator-(void) const { DVector tmp(*this); for (int i = this->First(); i <= this->Last(); i++) tmp[i] = -tmp[i]; return tmp; } template DVector DVector::operator-(const DVector &v) const { if (!Check(v)) { throw DimensionException(); } DVector tmp(*this); tmp.PVector::operator-=(v); return tmp; } template DVector &DVector::operator-=(const DVector &v) { if (!Check(v)) { throw DimensionException(); } PVector::operator-=(v); return *this; } template T DVector::operator*(const DVector &v) const { if (!Check(v)) { throw DimensionException(); } return (*this).PVector::operator*(v); } template DVector &DVector::operator*=(const T &c) { PVector::operator*=(c); return *this; } template DVector DVector::operator/(const T &c) const { DVector tmp(*this); tmp = tmp.PVector::operator/(c); return tmp; } template bool DVector::operator==(const DVector &v) const { if (!Check(v)) { throw DimensionException(); } return PVector::operator==(v); } template bool DVector::operator!=(const DVector &v) const { return !(*this == v); } //------------------------------------------------------------------------- // DVector: General data access //------------------------------------------------------------------------- template void DVector::CopySubRow(int row, int col, const DVector &v) { if (!Check(v)) { throw DimensionException(); } if (dvlen.First() > row || row > dvlen.Last()) { throw IndexException(); } if (1 > col || col > dvlen[row]) { throw IndexException(); } for (int i = 1; i <= this->svlen[dvidx[row]+col-1]; i++) dvptr[row][col][i] = v.dvptr[row][col][i]; } template const Array &DVector::DPLengths(void) const { return dvlen; } } // end namespace Gambit