bool VectorTemplate<T>::isEqual(const MyT& a,T eps) const { CHECKEMPTY(); Assert(size()==a.size()); ItT v=begin(); ItT va=a.begin(); for(int i=0;i<n;i++,v++,va++) if(!FuzzyEquals(*v,*va,eps)) return false; return true; }
void VectorTemplate<T>::swapCopy(MyT& a) { CHECKSIZE(a.n); T tmp; ItT v=begin(); ItT va=a.begin(); for(int i=0;i<n;i++,v++,va++) { tmp = *v; *v = *va; *va = tmp; } }
T VectorTemplate<T>::distanceSquared(const MyT& a) const { CHECKSIZE(a.n); ItT v=begin(); ItT va=a.begin(); T sum=0; for(int i=0;i<n;i++,v++,va++) { T d=*v-*va; sum += Math::dot(d,d); } return sum; }
void MatrixTemplate<T>::swapCopy(MyT& a) { CHECKDIMS(a.m,a.n); T tmp; ItT v=begin(); ItT va=a.begin(); for(int i=0;i<m;i++,v.nextRow(),va.nextRow()) { for(int j=0;j<n;j++,v.nextCol(),va.nextCol()) { tmp=*v; *v=*va; *va=tmp; } } }
void DiagonalMatrixTemplate<T>::setPseudoInverse(const MyT& a) { if(this->empty()) resize(a.n); else if(this->size() != a.size()) { RaiseErrorFmt(WHERE_AM_I,MatrixError_IncompatibleDimensions,this->n,this->n,a.n,a.n); } ItT v=this->begin(); ItT va=a.begin(); for(int i=0; i<this->n; i++,v++,va++) *v = PseudoInv(*va); }
bool MatrixTemplate<T>::isEqual(const MyT& a,T eps) const { CHECKEMPTY(); CHECKDIMS(a.m,a.n); ItT v=begin(); ItT va=a.begin(); for(int i=0;i<m;i++,v.nextRow(),va.nextRow()) for(int j=0;j<n;j++,v.nextCol(),va.nextCol()) if(!FuzzyEquals(*v,*va,eps)) return false; return true; }
void SparseVectorTemplate<T>::copySubVector(int i,const MyT& x) { Assert(i >= 0); Assert(i+x.n <= this->n); //erase entries between i and i+x.n typename MyT::iterator first,last; first = this->entries.lower_bound(i); last = this->entries.upper_bound(i+x.n); if(first != this->entries.end() && last != this->entries.end()) this->entries.erase(first,last); //insert new entries typename MyT::const_iterator j; for(j=x.begin();j!=x.end();j++) { this->insert(i+j->first,j->second); } }
T SparseVectorTemplate<T>::dot(const MyT& b) const { Assert(this->n==b.n); const_iterator k=this->begin(),bk=b.begin(); int i,bi; T sum=Zero; while(k!=this->end() || bk!=b.end()) { i = (k!=this->end()? k->first : this->n); bi = (bk!=b.end()? bk->first : this->n); if(i < bi) k++; else if (bi < i) bk++; else { sum+=k->second*bk->second; k++; bk++; } } return sum; }
bool VectorTemplate<T>::operator == (const MyT& v) const { if(this == &v) return true; if(size() != v.n) return false; return std::equal(begin(),end(),v.begin()); }