📄 gvector.imp
字号:
//// $Source: /home/gambit/CVS/gambit/sources/math/gvector.imp,v $// $Date: 2002/08/26 05:50:04 $// $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 "base/base.h"#include "gvector.h"template <class T> gText gVector<T>::BadDim::Description(void) const{ return "Mismatched vector lengths";}//------------------------------------------------------------------------// gVector<T>: Constructors, destructor, constructive operators//------------------------------------------------------------------------template <class T> gVector<T>::gVector(unsigned int len) : gArray<T>(len) { }template <class T> gVector<T>::gVector(int low, int high) : gArray<T>(low, high){ }template <class T> gVector<T>::gVector(const gVector<T> &V) : gArray<T>(V) { }template <class T> gVector<T>::~gVector() { }template <class T> gVector<T>& gVector<T>::operator=(const gVector<T>& V){ if (!Check(V)) throw BadDim(); gArray<T>::operator=(V); return *this;}//------------------------------------------------------------------------// inline arithmetic operators//------------------------------------------------------------------------template <class T> bool gVector<T>::operator!=(const gVector<T> &V) const{ return !(*this == V); }template <class T> bool gVector<T>::operator!=(T c) const{ return !(*this == c); }//------------------------------------------------------------------------// inline internal functions//------------------------------------------------------------------------template <class T> bool gVector<T>::Check(const gVector<T> &v) const{ return( v.mindex == mindex && v.maxdex == maxdex ); }//------------------------------------------------------------------------// gVector: arithmetic operators//------------------------------------------------------------------------template<class T> gVector<T>& gVector<T>::operator=(T c){ for(int i=mindex; i<=maxdex; i++) (*this)[i]= c; return (*this);}// arithmetic operatorstemplate <class T> gVector<T>gVector<T>::operator+(const gVector<T>& V) const{ if (!Check(V)) throw BadDim(); gVector<T> tmp(mindex,maxdex); for(int i=mindex; i<=maxdex; i++) tmp[i]= (*this)[i] + V[i]; return tmp;}template <class T> gVector<T>gVector<T>::operator-(const gVector<T>& V) const{ if (!Check(V)) throw BadDim(); gVector<T> tmp(mindex,maxdex); for(int i=mindex; i<=maxdex; i++) tmp[i]= (*this)[i] - V[i]; return tmp;}template <class T> gVector<T>&gVector<T>::operator+=(const gVector<T>& V){ if (!Check(V)) throw BadDim(); for(int i=mindex; i<=maxdex; i++) (*this)[i] += V[i]; return (*this);}template <class T> gVector<T>&gVector<T>::operator-=(const gVector<T>& V){ if (!Check(V)) throw BadDim(); for(int i=mindex; i<=maxdex; i++) (*this)[i] -= V[i]; return (*this);}template <class T> gVector<T> gVector<T>::operator-(void){ gVector<T> tmp(mindex,maxdex); for(int i=mindex; i<=maxdex; i++) tmp[i]= -(*this)[i]; return tmp;}template <class T> gVector<T> gVector<T>::operator*(T c) const{ gVector<T> tmp(mindex,maxdex); for(int i=mindex; i<=maxdex; i++) tmp[i]= (*this)[i]*c; return tmp;}template <class T> gVector<T> &gVector<T>::operator*=(T c){ for(int i=mindex; i<=maxdex; i++) (*this)[i] *= c; return (*this);}template <class T> T gVector<T>::operator*(const gVector<T>& V) const{ if (!Check(V)) throw BadDim(); T sum= (T)0; for(int i=mindex; i<=maxdex; i++) sum += (*this)[i] * V[i]; return sum;}template <class T> gVector<T> gVector<T>::operator/(T c) const{ gVector<T> tmp(mindex,maxdex); for(int i=mindex; i<=maxdex; i++) tmp[i]= (*this)[i]/c; return tmp;}template <class T> bool gVector<T>::operator==(const gVector<T>& V) const{ if (!Check(V)) throw BadDim(); for(int i=mindex; i<=maxdex; i++) if( (*this)[i] != V[i] ) return false; return true;}template <class T> bool gVector<T>::operator==(T c) const{ for(int i=mindex; i<=maxdex; i++) if( (*this)[i] != c ) return false; return true;}template <class T> T gVector<T>::NormSquared(void) const{ T answer = (T)0; for (int i = 1; i <= Length(); i++) answer += (*this)[i] * (*this)[i]; return answer;}template <class T> gOutput &operator<<(gOutput &f, const gVector<T> &V){ V.Dump(f); return f; }template <class T> gVector<gDouble> TogDouble(const gVector<T>& V){ gVector<gDouble> answer(V.Length()); for (int i = 1; i <= V.Length(); i++) answer[i] = (gDouble)V[i]; return answer;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -