//=============================================================================
/*! clear all the matrix data and set the sizes 0 */
inline void zhsmatrix::clear()
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] zhsmatrix::clear()" << std::endl;
std::cerr << "# [NOTE] zhsmatrix::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;
}
//=============================================================================
/*! change the matrix into a zero matrix */
inline zhsmatrix& zhsmatrix::zero()
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] zhsmatrix::zero()" << std::endl;
#endif//CPPL_VERBOSE
VOL =0;
for(long i=0; i<N; i++){ Line[i].clear(); }
return *this;
}
//=============================================================================
/*! change sign(+/-) of the matrix */
inline void zhsmatrix::chsign()
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] zhsmatrix::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 zhsmatrix::copy(const zhsmatrix& mat)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] zhsmatrix::copy(const zhsmatrix&)" << std::endl;
std::cerr << "# [NOTE] zhsmatrix::copy(const zhsmatrix&)" << " A zhsmatrix at " << Array << " is going to be deleted." << std::endl;
#endif//CPPL_VERBOSE
resize(mat.N, mat.CAP);
VOL =mat.VOL;
zcopy_(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]; }
#ifdef CPPL_VERBOSE
std::cerr << " Then, a COPY of a zhsmatrix 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 zhsmatrix::shallow_copy(const _zhsmatrix& mat)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] zhsmatrix::shallow_copy(const _zhsmatrix&)" << std::endl;
std::cerr << "# [NOTE] zhsmatrix:shallow_copy(const _zhsmatrix&) " << "A zhsmatrix 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 void zhsmatrix::resize(const long& _n, const long _c)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] zhsmatrix::resize(const long&, const long&, const long&)" << std::endl;
#endif//CPPL_VERBOSE
#ifdef CPPL_DEBUG
if( _n<0 || _c<0 ){
std::cerr << "[ERROR] zhsmatrix::resize(const long&, const long&, const long&)" << std::endl
<< "Matrix sizes and the length of arrays must be positive integers. " << std::endl
<< "Your input was (" << _n << "," << _c << ")." << std::endl;
exit(1);
}
#endif//CPPL_DEBUG
N =_n;
CAP =_c;
VOL =0;
delete [] Array;
Array =new std::complex<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];
}
//=============================================================================
/*! stretch the matrix size */
inline void zhsmatrix::stretch(const long& dn)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] zhsmatrix::stretch(const long&)" << std::endl;
#endif//CPPL_VERBOSE
#ifdef CPPL_DEBUG
if( n+dn<0 ){
std::cerr << "[ERROR] zhsmatrix::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;
//// delete components over the new size ////
if(dn<0){
for(long c=VOL-1; c>=0; c--){
if(Indx[c]>=N || Jndx[c]>=N){ fdel(c); }
}
}
//// resize Line ////
std::vector< std::pair<long,long> >* _Line(new std::vector< std::pair<long,long> >[N]);
for(long i=0; i<min(N,N-dn); i++){ _Line[i]=Line[i]; }
delete [] Line;
Line=_Line;
}
//=============================================================================
/*! expand the matrix capacity */
inline void zhsmatrix::expand(const long& dc)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] zhsmatrix::expand(const long&)" << std::endl;
#endif//CPPL_VERBOSE
#ifdef CPPL_DEBUG
if( dc<0 ){
std::cerr << "[ERROR] zhsmatrix::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;
std::complex<double>* newArray(new std::complex<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 zhsmatrix::isListed(const long& i, const long& j)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] zhsmatrix::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] zhsmatrix::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 zhsmatrix::number(const long& i, const long& j)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] zhsmatrix::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] zhsmatrix::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 _zrovector zhsmatrix::row(const long& _m) const
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] zhsmatrix::row(const long&)" << std::endl;
#endif//CPPL_VERBOSE
#ifdef CPPL_DEBUG
if( _m<0 || _m>M ){
std::cerr << "[ERROR] zhsmatrix::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
zrovector v( zrovector(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 _zcovector zhsmatrix::col(const long& _n) const
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] zhsmatrix::col(const long&)" << std::endl;
#endif//CPPL_VERBOSE
#ifdef CPPL_DEBUG
if( _n<0 || _n>N ){
std::cerr << "[ERROR] zhsmatrix::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
zcovector v( zcovector(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);
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//=============================================================================
/*! swap two matrices */
inline void swap(zhsmatrix& A, zhsmatrix& B)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] swap(zhsmatrix&, zhsmatrix&)" << std::endl;
#endif//CPPL_VERBOSE
long A_N(A.N), A_CAP(A.CAP), A_VOL(A.VOL);
std::complex<double>* A_Array(A.Array);
long *A_Indx(A.Indx), *A_Jndx(A.Jndx);
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 _zhsmatrix _(zhsmatrix& mat)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] _(zhsmatrix&)" << std::endl;
#endif//CPPL_VERBOSE
_zhsmatrix 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 =NULL;
return newmat;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//=============================================================================
/*! health checkup */
inline void zhsmatrix::checkup()
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] zhsmatrix::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] zhsmatrix::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] zhsmatrix::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] zhsmatrix::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] zhsmatrix::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);
}
//// std::complex<std::complex<std::complex<double>>>-listed ////
for(long C=c+1; C<VOL; C++){
if(Indx[c]==Indx[C] && Jndx[c]==Jndx[C]){
std::cerr << "[ERROR] zhsmatrix::checkup()" << std::endl
<< "The (" << Indx[c] << ", " << Jndx[c]
<< ") component is std::complex<std::complex<std::complex<double>>>-listed at the "
<< c << "th and the" << C << "the elements."<< std::endl;
exit(1);
}
}
}
//////// ijc consistence ////////
//////// NOTE ////////
std::cerr << "# [NOTE] zhsmatrix::checkup() "
<< "This symmetric sparse matrix is fine." << std::endl;
}