void Array<scalar,index>::swap(Array& arr) { if(arr.size()!=size()) throw COException("the size of two arrays must be the same if anyone wants to swap them!"); else if(arr.isReferred()||this->isReferred()) throw COException("Swap requires that both arrays are not referred array"); else blas::copt_blas_swap(__size,const_cast<scalar*>(arr.dataPtr()),1,__data_ptr,1); }
scalar& Array<scalar,index>::operator[](index i) { if ( i < 0 ){ // index less than zero throw COException("Vector error, index less than zero."); } else if ( i >= __size ){ // out of range throw COException("Vector error, index larger than the length."); } else return __data_ptr[i*__inter]; }
VectorBase<scalar,index,SizeAtCompileTime> VectorBase<scalar,index,SizeAtCompileTime>::vecE(index size,index i,const scalar s) { if ( i < 0 || i >= size ) throw COException("Index error: out of range!"); VectorBase<scalar,index,Dynamic> vec(size); vec[i] = s; return vec; }
void Array<scalar,index>::setArray(const Array &arr) { if ( __referred ) throw COException("referred array is not allowed to be reset "); else{ resize(arr.size(),arr.interval()); blas::copt_blas_copy(__size,arr.dataPtr(),arr.interval(),__data_ptr,__inter); } }
void Array<scalar,index>::setArray(const index size, const scalar *data, const index inter) { if (__referred) throw COException("referred array is not allowed to be reset "); else{ resize(size,inter); blas::copt_blas_copy(__size,data,1,__data_ptr,__inter); } }
void VectorBase<scalar,index,SizeAtCompileTime>::blockFromVector(const VectorBase& vec,const std::vector<index>& indices) { this->resize(indices.size()); for ( index i = 0 ; i < indices.size() ; ++ i ){ if(indices[i] >= vec.size() ) { throw COException("Index out of range in Vector blocking!"); } this->operator[](i)=vec[indices[i]]; } }
VectorBase<scalar,index,SizeAtCompileTime> VectorBase<scalar,index,SizeAtCompileTime>::block(const std::vector<index>& indices) const { VectorBase<scalar,index,SizeAtCompileTime> result(indices.size()); for ( index i = 0 ; i < indices.size() ; ++ i ){ if (indices[i] >= this->size()) { throw COException("Index out of range in Vector blocking!"); } result[i] = this->operator[](indices[i]); } return result; }
typename VectorBase<scalar,index,SizeAtCompileTime>::scalar VectorBase<scalar,index,SizeAtCompileTime>::dot(const VectorBase<scalar,index,S> &vec) const { if(this->size()!=vec.size()) { std::cerr<<"one size is "<<this->size()<<" and another size is "<<vec.size()<<std::endl; throw COException("VectorBase dot operation error: the length of two VectorBases do not equal to each other"); } else{ scalar sum = blas::copt_blas_dot(this->size(),this->dataPtr(),this->interval(),vec.dataPtr(),vec.interval()); return sum; } }
typename Cholesky<Matrix>::DMatrix Cholesky<Matrix>::doSolve( const AbstractMatrix& b ) { if ( this->rowNum() != b.rows() ) { std::cerr<<"The order of matrix is "<<this->rowNum()<<" and the dimension of vector is "<<b.rows()<<std::endl; throw COException("Cholesky solving error: the size if not consistent!"); } DMatrix result(b); copt_lapack_potrs('U',this->rowNum(),b.cols(),__a,this->lda(),result.dataPtr(),result.lda(),&__info); if ( __info != 0 ) std::cerr<<"Warning in Cholesky solver: solving is wrong!"<<std::endl; return result; }
void VectorBase<scalar,index,SizeAtCompileTime>::blockFromVector(const VectorBase& vec,const std::set<index>& indices) { if( *indices.rbegin() >= vec.size() ) { throw COException("Index out of range in Vector blocking!"); } this->resize(indices.size()); index i = 0; for ( const auto& s : indices ){ this->operator[](i) = vec[s]; ++ i; } }
VectorBase<scalar,index,SizeAtCompileTime> VectorBase<scalar,index,SizeAtCompileTime>::operator- (const VectorBase<scalar,index,S> &vec) const { if(this->size()!=vec.size()) { std::cerr<<"one size is "<<this->size()<<" another size is "<<vec.size()<<std::endl; throw COException("VectorBase summation error: the length of two VectorBases do not equal to each other"); } VectorBase<scalar,index,Dynamic> result(this->size()); for ( index i = 0 ; i < this->size() ; ++ i ){ result[i] = this->operator[](i)-vec[i]; } return result; }
VectorBase<scalar,index,SizeAtCompileTime> VectorBase<scalar,index,SizeAtCompileTime>::block(const std::set<index>& indices) const { if( *indices.rbegin() >= this->size() ) { throw COException("Index out of range in Vector blocking!"); } VectorBase<scalar,index,SizeAtCompileTime> result(indices.size()); index i = 0; for ( const auto& s : indices ){ result[i] = this->operator[](s); ++ i; } return result; }
void Array<scalar,index>::resize(const index size, const index inter) { if(__referred) throw COException("referred array is not allowed to be resized!"); else { if(__size != size || __inter != inter ){ __size = size; __inter = inter; SAFE_DELETE_ARRAY(__data_ptr); __data_ptr = new scalar[__size*__inter]; for ( index i = 0 ; i < __size ; ++ i ) __data_ptr[i*__inter] = static_cast<scalar>(0.0); } else{ for ( index i = 0 ; i < __size ; ++ i ) __data_ptr[i*__inter] = static_cast<scalar>(0.0); } } }
MSize(T m,T n){throw COException("Error: unknown size type! Check your template carefully!");}