Пример #1
0
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;
}
Пример #2
0
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);
}
Пример #3
0
T SparseVectorCompressed<T>::distanceSquared(const MyT& rhs) const
{
  Assert(isValid() && rhs.isValid());
  Assert(n == rhs.n);
  int ak=0,bk=0;
  int ai,bi;
  T res = 0;
  while(ak<num_entries || bk<rhs.num_entries) {
    ai = (ak<num_entries? indices[ak] : n);
    bi = (bk<rhs.num_entries? rhs.indices[bk] : n);
    if(ai < bi) {
	  res += Sqr(vals[ak]);
      ak++;
    }
    else if(bi < ai) {
      res += Sqr(rhs.vals[bk]);
      bk++;
    }
    else {
	  res += Sqr(vals[ak]-rhs.vals[bk]);
      ak++; bk++;
    }
  }
  return res;
}
Пример #4
0
void VectorTemplate<T>::copySubVector(int i,const MyT& a)
{
  Assert(this != &a);
  Assert(isValidIndex(i));
  Assert(isValidIndex(i+a.n-1));
	gen_array_equal(getStart()+i*stride,stride, a.getStart(),a.stride, a.n);
}
Пример #5
0
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);
  }
}
Пример #6
0
const VectorTemplate<T>& VectorTemplate<T>::operator = (const MyT& a)
{
  if(this == &a) return *this;
  if(n!=a.n) resize(a.n);
  gen_array_equal(getStart(),stride, a.getStart(),a.stride, n);
  return *this;
}
Пример #7
0
void VectorTemplate<T>::getSubVectorCopy(int i,MyT& a) const
{
  Assert(&a != this);
  Assert(isValidIndex(i));
  Assert(isValidIndex(i+a.n-1));
  gen_array_equal(a.getStart(),a.stride, getStart()+stride*i,stride, a.n);
}
Пример #8
0
void MatrixTemplate<T>::setRef(const MyT& mat,int i,int j,int _istride,int _jstride,int _m,int _n)
{
  Assert(this != &mat);
  Assert(!allocated);
  vals = mat.vals;
  capacity = mat.capacity;
  allocated = false;
  base = mat.base+i*mat.istride+j*mat.jstride;
  istride = _istride*mat.istride;
  jstride = _jstride*mat.jstride;
  if(mat.isEmpty()) {
    Assert(_m <= 0 && _n <= 0);
    if(_m < 0) m = mat.m;
    else m=0;
    if(_n < 0) n = mat.n;
    else n=0;
    Assert(isValid());
    return;
  }
  if(_m < 0) {
    Assert(istride != 0);
    m = (mat.m-i+_istride-1) / _istride;
  }
  else
    m = _m;
  if(_n < 0) {
    Assert(jstride != 0);
    n = (mat.n-j+_jstride-1) / _jstride;
  }
  else
    n = _n;
  Assert(isValid());
}
Пример #9
0
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;
}
Пример #10
0
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;
  }
}
Пример #11
0
void SparseVectorCompressed<T>::sub(const MyT& a, const MyT& b)
{
  Assert(this != &a && this != &b);
  Assert(a.n == b.n);
  Assert(a.isValid() && b.isValid());
  int ak=0,bk=0;
  int ai,bi;
  int nnz=0;

  while(ak<a.num_entries || bk<b.num_entries) {
    ai = (ak<a.num_entries? a.indices[ak] : n);
    bi = (bk<b.num_entries? b.indices[bk] : n);
    if(ai < bi) ak++;
    else if (bi < ai) bk++;
    else { ak++; bk++; }
    nnz++;
  }
  resize(a.n,nnz);

  nnz=0; ak=0;bk=0;
  while(ak<a.num_entries || bk<b.num_entries) {
    ai = (ak<a.num_entries? a.indices[ak] : n);
    bi = (bk<b.num_entries? b.indices[bk] : n);
    if(ai < bi) {
      indices[nnz] = ai;
      vals[nnz] = a.vals[ak];
      ak++;
    }
    else if(bi < ai) {
      indices[nnz] = bi;
      vals[nnz] = b.vals[bk];
      bk++;
    }
    else {
      indices[nnz] = ai;
      vals[nnz] = a.vals[ak]-b.vals[bk];
      ak++; bk++;
    }
    nnz++;
  }
}
Пример #12
0
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;
}
Пример #13
0
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;
    }
  }
}
Пример #14
0
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;
}
Пример #15
0
void VectorTemplate<T>::div(const MyT& a, T c)
{
	CHECKRESIZE(a.n);
	gen_array_div(getStart(),stride, a.getStart(),a.stride, c, n);
}
Пример #16
0
T VectorTemplate<T>::dot(const MyT& a) const
{
  CHECKEMPTY();
  Assert(size()==a.size());
  return gen_array_dot(getStart(),stride, a.getStart(),a.stride, n);
}
Пример #17
0
T SparseVectorTemplate<T>::distanceSquared(const MyT& b) const
{
  return normSquared() + b.normSquared() - Two*dot(b);
}
Пример #18
0
void VectorTemplate<T>::getRef(MyT& v,int _base,int _stride,int _size) const
{
  v.setRef(*this,_base,_stride,_size);
}
Пример #19
0
void VectorTemplate<T>::getCopy(MyT& v) const
{
  v.copy(*this);
}
Пример #20
0
void VectorTemplate<T>::setNegative(const MyT& a)
{
  CHECKRESIZE(a.n);
  gen_array_negate(getStart(),stride, a.getStart(),a.stride, n);
}
Пример #21
0
void VectorTemplate<T>::madd(const MyT& v, T c)
{
	Assert(size() == v.size());
  gen_array_madd(getStart(),stride, v.getStart(),v.stride, c,n);
}
Пример #22
0
void VectorTemplate<T>::dec(const MyT& v)
{
	Assert(size() == v.size());
  gen_array_dec(getStart(),stride, v.getStart(),v.stride, n);
}
Пример #23
0
void VectorTemplate<T>::axpby(T a,const MyT& x,T b,const MyT& y)
{
  Assert(x.n == y.n);
  CHECKRESIZE(x.n);
  gen_array_axpby(getStart(),stride, a, x.getStart(),x.stride, b, y.getStart(),y.stride,n);
}
Пример #24
0
void VectorTemplate<T>::sub(const MyT& a, const MyT& b)
{
  Assert(a.size() == b.size());
  CHECKRESIZE(a.n);
	gen_array_sub(getStart(),stride, a.getStart(),a.stride, b.getStart(),b.stride, n);
}
Пример #25
0
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());
}
Пример #26
0
void VectorTemplate<T>::copy(const MyT& a)
{
  if(this == &a) return;
  CHECKRESIZE(a.n);
  gen_array_equal(getStart(),stride, a.getStart(),a.stride, n);
}