//=============================================================================
/*! clear all the matrix data and set the sizes 0 */
inline void dssmatrix::clear()
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] dssmatrix::clear()" << std::endl;
std::cerr << "# [NOTE] dssmatrix::clear()" << " An array at " << Array << " is going to be cleared." << std::endl;
#endif//CPPL_VERBOSE
N =CAP =VOL=0;
delete [] Array; Array=NULL;
delete [] Indx; Indx=NULL;
delete [] Jndx; Jndx=NULL;
//delete [] Line; Line=NULL;
Line.clear();
}
//=============================================================================
/*! change the matrix into a zero matrix */
inline dssmatrix& dssmatrix::zero()
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] dssmatrix::zero()" << std::endl;
#endif//CPPL_VERBOSE
VOL =0;
for(long i=0; i<N; i++){ Line[i].resize(0); }
return *this;
}
//=============================================================================
/*! change sign(+/-) of the matrix */
inline void dssmatrix::chsign()
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] dssmatrix::chsign()" << std::endl;
#endif//CPPL_VERBOSE
for(long i=0; i<VOL; i++){ Array[i] =-Array[i]; }
}
//=============================================================================
/*! make a deep copy of the matrix */
inline void dssmatrix::copy(const dssmatrix& mat)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] dssmatrix::copy(const dssmatrix&)" << std::endl;
std::cerr << "# [NOTE] dssmatrix::copy(const dssmatrix&)" << " A dssmatrix at " << Array << " is going to be deleted." << std::endl;
#endif//CPPL_VERBOSE
resize(mat.N, mat.CAP);
VOL =mat.VOL;
dcopy_(VOL, mat.Array, 1, Array, 1);
for(long i=0; i<VOL; i++){
Indx[i] =mat.Indx[i];
Jndx[i] =mat.Jndx[i];
}
//for(long i=0; i<N; i++){ Line[i] =mat.Line[i]; }
Line =mat.Line;
#ifdef CPPL_VERBOSE
std::cerr << " Then, a COPY of a dssmatrix has been cleated at " << Array << "." << std::endl;
#endif//CPPL_VERBOSE
}
//=============================================================================
/*! make a shallow copy of the matrix\n
This function is not designed to be used in project codes. */
inline void dssmatrix::shallow_copy(const _dssmatrix& mat)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] dssmatrix::shallow_copy(const _dssmatrix&)" << std::endl;
std::cerr << "# [NOTE] dssmatrix:shallow_copy(const _dssmatrix&) " << "A dssmatrix at " << Array << " is going to be deleted, " << "and point at " << mat.Array << " instead." << std::endl;
#endif//CPPL_VERBOSE
delete [] Array;
delete [] Indx;
delete [] Jndx;
//delete [] Line;
N =mat.N;
CAP =mat.CAP;
VOL =mat.VOL;
Array=mat.Array;
Indx =mat.Indx;
Jndx =mat.Jndx;
Line =mat.Line;
mat.nullify();
}
//=============================================================================
/*! resize the matrix */
inline dssmatrix& dssmatrix::resize(const long& _n, const long _c, const long _l)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] dssmatrix::resize(const long&, const long&, const long&)" << std::endl;
#endif//CPPL_VERBOSE
#ifdef CPPL_DEBUG
if( _n<0 || _c<0 || _l<0 ){
std::cerr << "[ERROR] dssmatrix::resize(const long&, const long&, const long&)" << std::endl
<< "Matrix sizes, the length of arrays, and line size must be positive integers. " << std::endl
<< "Your input was (" << _n << "," << _c << "," << _l << ")." << std::endl;
exit(1);
}
#endif//CPPL_DEBUG
N =_n;
CAP =_c;
VOL =0;
delete [] Array;
Array =new double[CAP];
delete [] Indx;
Indx =new long[CAP];
delete [] Jndx;
Jndx =new long[CAP];
//delete [] Line;
//Line =new std::vector< std::pair<long,long> >[N];
Line.resize(N);
for(long i=0; i<N; i++){ Line[i].reserve(_l); }
return *this;
}
//=============================================================================
/*! stretch the matrix size */
inline void dssmatrix::stretch(const long& dn)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] dssmatrix::stretch(const long&)" << std::endl;
#endif//CPPL_VERBOSE
if(dn==0){ return; }
#ifdef CPPL_DEBUG
if( n+dn<0 ){
std::cerr << "[ERROR] dssmatrix::stretch(const long&)" << std::endl
<< "The new matrix size must be larger than zero." << std::endl
<< "Your input was (" << dn << ")." << std::endl;
exit(1);
}
#endif//CPPL_DEBUG
N +=dn;
if(dn<0){
//// delete components over the new size ////
for(long c=VOL-1; c>=0; c--){
if(Indx[c]>=N || Jndx[c]>=N){ fdel(c); }
}
for(long i=0; i<-dn; i++){ Line.pop_back(); }
}
else{//dn>0
for(long i=0; i<dn; i++){ Line.push_back( std::vector< std::pair<long,long> >() ); }
}
}
//=============================================================================
/*! expand the matrix capacity */
inline void dssmatrix::expand(const long& dc)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] dssmatrix::expand(const long&)" << std::endl;
#endif//CPPL_VERBOSE
if(dc==0){ return; }
#ifdef CPPL_DEBUG
if( dc<0 ){
std::cerr << "[ERROR] dssmatrix::expand(const long&)" << std::endl
<< "The argument must be a positive integer. " << std::endl
<< "Your input was (" << dc << ")." << std::endl;
exit(1);
}
#endif//CPPL_DEBUG
CAP+=dc;
double* newArray(new double[CAP]);
long *newIndx(new long[CAP]), *newJndx(new long[CAP]);
for(int c=0; c<VOL; c++){
newArray[c] =Array[c];
newIndx[c] =Indx[c];
newJndx[c] =Jndx[c];
}
delete [] Array;
delete [] Indx;
delete [] Jndx;
Array =newArray;
Indx =newIndx;
Jndx =newJndx;
}
//=============================================================================
/*! check if the component is listed */
inline bool dssmatrix::isListed(const long& i, const long& j)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] dssmatrix::isListed(const long&, const long&)" << std::endl;
#endif//CPPL_VERBOSE
#ifdef CPPL_DEBUG
if( i<0 || j<0 || N<=i || N<=j ){
std::cerr << "[ERROR] dssmatrix::isListed(const long&, const long&)" << std::endl
<< "The required component is out of the matrix size." << std::endl
<< "Your input was (" << i << "," << j << ")." << std::endl;
exit(1);
}
#endif//CPPL_DEBUG
for(std::vector< std::pair<long,long> >::iterator p=Line[i].begin(); p!=Line[i].end(); p++){
if(p->first==j){ return 1; }
}
return 0;
}
//=============================================================================
/*! return the element number of the component */
inline long dssmatrix::number(const long& i, const long& j)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] dssmatrix::number(const long&, const long&)" << std::endl;
#endif//CPPL_VERBOSE
#ifdef CPPL_DEBUG
if( i<0 || j<0 || N<=i || N<=j ){
std::cerr << "[ERROR] dssmatrix::number(const long&, const long&)" << std::endl
<< "The required component is out of the matrix size." << std::endl
<< "Your input was (" << i << "," << j << ")." << std::endl;
exit(1);
}
#endif//CPPL_DEBUG
for(std::vector< std::pair<long,long> >::iterator p=Line[i].begin(); p!=Line[i].end(); p++){
if(p->first==j){ return p->second; }
}
return -1;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//=============================================================================
/*! get row of the matrix */
inline _drovector dssmatrix::row(const long& _m) const
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] dssmatrix::row(const long&)" << std::endl;
#endif//CPPL_VERBOSE
#ifdef CPPL_DEBUG
if( _m<0 || _m>M ){
std::cerr << "[ERROR] dssmatrix::row(const long&)" << std::endl
<< "Input row number must be between 0 and " << M << "." << std::endl
<< "Your input was " << _m << "." << std::endl;
exit(1);
}
#endif//CPPL_DEBUG
drovector v( drovector(N).zero() );
for(std::vector< std::pair<long,long> >::const_iterator p=line[_m].begin(); p!=line[_m].end(); p++){
v(p->first)=Array[p->second];
}
return _(v);
}
//=============================================================================
/*! get column of the matrix */
inline _dcovector dssmatrix::col(const long& _n) const
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] dssmatrix::col(const long&)" << std::endl;
#endif//CPPL_VERBOSE
#ifdef CPPL_DEBUG
if( _n<0 || _n>N ){
std::cerr << "[ERROR] dssmatrix::col(const long&)" << std::endl
<< "Input row number must be between 0 and " << N << "." << std::endl
<< "Your input was " << _n << "." << std::endl;
exit(1);
}
#endif//CPPL_DEBUG
dcovector v( dcovector(M).zero() );
for(std::vector< std::pair<long,long> >::const_iterator p=line[_n].begin(); p!=line[_n].end(); p++){
v(p->first)=Array[p->second];
}
return _(v);
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//=============================================================================
/*! erase components less than DBL_MIN */
inline void dssmatrix::diet(const double eps)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] dssmatrix::diet()" << std::endl;
#endif//CPPL_VERBOSE
for(long c=VOL-1; c>=0; c--){
if( fabs(Array[c])<eps ){ fdel(c); }
}
}
//=============================================================================
/*! health checkup */
inline void dssmatrix::checkup()
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] dssmatrix::checkup()" << std::endl;
#endif//CPPL_VERBOSE
//////// write ////////
for(long c=0; c<VOL; c++){
std::cerr << "Array[" << c << "] = (" << Indx[c] << "," << Jndx[c] << ") = " << Array[c] << std::endl;
}
std::cerr << std::endl;
for(long i=0; i<N; i++){
std::cerr << "Line[" << i << "] =" << std::flush;
for(unsigned long k=0; k<Line[i].size(); k++){
std::cerr << " <" << Line[i][k].first << "," << Line[i][k].second << ">" << std::flush;
}
std::cerr << std::endl;
}
std::cerr << std::endl;
//////// CAP ////////
if(CAP<0){
std::cerr << "[ERROR] dssmatrix::checkup()" << std::endl
<< "The cap is not valid." << std::endl
<< "CAP was " << CAP << "." << std::endl;
exit(1);
}
//////// VOL ////////
if(VOL<0 || VOL>CAP){
std::cerr << "[ERROR] dssmatrix::checkup()" << std::endl
<< "The vol is not valid." << std::endl
<< "VOL was " << VOL << "while CAP is " << CAP << "."
<< std::endl;
exit(1);
}
//////// Elements ////////
for(long c=0; c<VOL; c++){
//// m bound ////
if(Indx[c]<0 || Indx[c]>=N){
std::cerr << "[ERROR] dssmatrix::checkup()" << std::endl
<< "The indx of the " << c
<< "th element is out of the matrix size." << std::endl
<< "Indx[" << c << "] was " << Indx[c] << "." << std::endl;
exit(1);
}
//// n bound ////
if(Jndx[c]<0 || Jndx[c]>=N){
std::cerr << "[ERROR] dssmatrix::checkup()" << std::endl
<< "The jndx of the " << c
<< "th element is out of the matrix size." << std::endl
<< "Jndx[" << c << "] was " << Jndx[c] << "." << std::endl;
exit(1);
}
//// double-listed ////
for(long C=c+1; C<VOL; C++){
if(Indx[c]==Indx[C] && Jndx[c]==Jndx[C]){
std::cerr << "[ERROR] dssmatrix::checkup()" << std::endl
<< "The (" << Indx[c] << ", " << Jndx[c]
<< ") component is double-listed at the "
<< c << "th and the" << C << "the elements."<< std::endl;
exit(1);
}
}
}
//////// ijc consistence ////////
//////// NOTE ////////
std::cerr << "# [NOTE] dssmatrix::checkup() "
<< "This symmetric sparse matrix is fine." << std::endl;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//=============================================================================
/*! swap two matrices */
inline void swap(dssmatrix& A, dssmatrix& B)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] swap(dssmatrix&, dssmatrix&)" << std::endl;
#endif//CPPL_VERBOSE
long A_N(A.N), A_CAP(A.CAP), A_VOL(A.VOL);
double* A_Array(A.Array);
long *A_Indx(A.Indx), *A_Jndx(A.Jndx);
//std::vector< std::pair<long,long> >* A_Line(A.Line);
std::vector< std::vector< std::pair<long,long> > > A_Line(A.Line);
A.N=B.N; A.CAP=B.CAP; A.VOL=B.VOL; A.Array=B.Array; A.Indx=B.Indx; A.Jndx=B.Jndx; A.Line=B.Line;
B.N=A_N; B.CAP=A_CAP; B.VOL=A_VOL; B.Array=A_Array; B.Indx=A_Indx; B.Jndx=A_Jndx; B.Line=A_Line;
}
//=============================================================================
/*! convert user object to smart-temporary object */
inline _dssmatrix _(dssmatrix& mat)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] _(dssmatrix&)" << std::endl;
#endif//CPPL_VERBOSE
_dssmatrix newmat;
//////// shallow copy ////////
newmat.N =mat.N;
newmat.CAP =mat.CAP;
newmat.VOL =mat.VOL;
newmat.Array =mat.Array;
newmat.Indx =mat.Indx;
newmat.Jndx =mat.Jndx;
newmat.Line =mat.Line;
//////// nullify ////////
mat.N =0;
mat.CAP =0;
mat.VOL =0;
mat.Array =NULL;
mat.Indx =NULL;
mat.Jndx =NULL;
mat.Line.clear();
return newmat;
}