//=============================================================================
/*! operator() for const object */
inline std::complex<double> zgsmatrix::operator()(const long& i, const long& j) const
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] zgsmatrix::operator()(const long&, const long&) const"
<< std::endl;
#endif//CPPL_VERBOSE
#ifdef CPPL_DEBUG
if( i<0 || j<0 || M<=i || N<=j ){
std::cerr << "[ERROR] zgsmatrix::operator()(long, 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
//// search (i,j) component ////
for(std::vector< std::pair<long,long> >::iterator p=Rows[i].begin(); p!=Rows[i].end(); p++){
if(p->first==j){ return Array[p->second]; }
}
//// (i,j) component was not found ////
return std::complex<double>(0.0,0.0);
}
//=============================================================================
/*! operator() for const object */
inline std::complex<double>& zgsmatrix::operator()(const long& i, const long& j)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] zgsmatrix::operator()(const long&, const long&) const"
<< std::endl;
#endif//CPPL_VERBOSE
#ifdef CPPL_DEBUG
if( i<0 || j<0 || M<=i || N<=j ){
std::cerr << "[ERROR] zgsmatrix::operator()(long, 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
//////// search (i,j) component ////////
for(std::vector< std::pair<long,long> >::iterator p=Rows[i].begin(); p!=Rows[i].end(); p++){
if(p->first==j){ return Array[p->second]; }
}
//////// (i,j) component not found ////////
//// vol check ////
if(VOL==CAP){
expand(CPPL_SECTOR_SIZE);
#ifdef CPPL_DEBUG
std::cerr << "[NOTE] zgsmatrix::put(long&, long&, std::complex<double>&) "
<< "The matrix volume was automatically expanded." << std::endl;
#endif//CPPL_DEBUG
}
//// push_back ////
Array[VOL]=std::complex<double>(0.0,0.0);
Indx[VOL]=i;
Jndx[VOL]=j;
Rows[i].push_back(std::pair<long,long>(j,VOL));
Cols[j].push_back(std::pair<long,long>(i,VOL));
VOL++;
return Array[VOL-1];
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//=============================================================================
/*! put value with volume cheack without isListed check */
inline zgsmatrix& zgsmatrix::put(const long& i, const long& j, const std::complex<double>& v)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] zgsmatrix::put(const long&, const long&, const std::complex<double>&)"
<< std::endl;
#endif//CPPL_VERBOSE
#ifdef CPPL_DEBUG
if( i<0 || j<0 || M<=i || N<=j ){
std::cerr << "[ERROR] zgsmatrix::put(long&, long&, std::complex<double>&)" << std::endl
<< "The required component is out of the matrix size."
<< std::endl
<< "Your input was (" << i << "," << j << ")." << std::endl;
exit(1);
}
for(long c=0; c<VOL; c++){
if(Indx[c]==i && Jndx[c]==j){
std::cerr << "[ERROR] zgsmatrix::put"
<< "(const long&, const long&, const std::complex<double>&)" << std::endl
<< "The required component is already listed." << std::endl
<< "Your input was (" << i << "," << j << ")." << std::endl;
exit(1);
}
}
#endif//CPPL_DEBUG
//// vol check ////
if(VOL==CAP){
expand(CPPL_SECTOR_SIZE);
#ifdef CPPL_DEBUG
std::cerr << "[NOTE] zgsmatrix::put(long&, long&, std::complex<double>&) "
<< "The matrix volume was automatically expanded." << std::endl;
#endif//CPPL_DEBUG
}
//// push ////
Array[VOL]=v;
Indx[VOL]=i;
Jndx[VOL]=j;
Rows[i].push_back(std::pair<long,long>(j,VOL));
Cols[j].push_back(std::pair<long,long>(i,VOL));
VOL++;
return *this;
}
//=============================================================================
/*! put value without isListed check and volume cheack */
inline zgsmatrix& zgsmatrix::fput(const long& i, const long& j, const std::complex<double>& v)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] zgsmatrix::fput(const long&, const long&, const std::complex<double>&)"
<< std::endl;
#endif//CPPL_VERBOSE
#ifdef CPPL_DEBUG
if( i<0 || j<0 || M<=i || N<=j ){
std::cerr << "[ERROR] zgsmatrix::fput"
<< "(const long&, lconst ong&, const std::complex<double>&)" << std::endl
<< "The required component is out of the matrix size."
<< std::endl
<< "Your input was (" << i << "," << j << ")." << std::endl;
exit(1);
}
if(VOL>=CAP){
std::cerr << "[ERROR] zgsmatrix::fput"
<< "(const long&, const long&, const std::complex<double>&)" << std::endl
<< "There is no free space to set a new entry." << std::endl;
exit(1);
}
for(long c=0; c<VOL; c++){
if(Indx[c]==i && Jndx[c]==j){
std::cerr << "[ERROR] zgsmatrix::fput"
<< "(const long&, const long&, const std::complex<double>&)" << std::endl
<< "The required component is already listed." << std::endl
<< "Your input was (" << i << "," << j << ")." << std::endl;
exit(1);
}
}
#endif//CPPL_DEBUG
Array[VOL]=v;
Indx[VOL]=i;
Jndx[VOL]=j;
Rows[i].push_back(std::pair<long,long>(j,VOL));
Cols[j].push_back(std::pair<long,long>(i,VOL));
VOL++;
return *this;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//=============================================================================
/*! delete the entry of a component */
inline zgsmatrix& zgsmatrix::del(const long i, const long j)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] zgsmatrix::del(const long&, const long&)"
<< std::endl;
#endif//CPPL_VERBOSE
#ifdef CPPL_DEBUG
if( i<0 || j<0 || M<=i || N<=j ){
std::cerr << "[ERROR] zgsmatrix::del(long&, long&, std::complex<double>&)" << 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
//// search (i,j) component ////
for(std::vector< std::pair<long,long> >::iterator p=Rows[i].begin(); p!=Rows[i].end(); p++){
if(p->first==j){
VOL--;
long c(p->second);
Array[c]=Array[VOL];
Indx[c]=Indx[VOL];
Jndx[c]=Jndx[VOL];
Rows[i].erase(p);
for(std::vector< std::pair<long,long> >::iterator q=Cols[j].begin(); q<=Cols[j].end(); q++){
if(q->first==i){ Cols[j].erase(q); break; }
}
long I(Indx[c]), J(Jndx[c]);
for(std::vector< std::pair<long,long> >::iterator q=Rows[I].begin(); q<=Rows[I].end(); q++){
if(q->first==J){ q->second=c; break; }
}
for(std::vector< std::pair<long,long> >::iterator q=Cols[J].begin(); q<=Cols[J].end(); q++){
if(q->first==I){ q->second=c; break; }
}
return *this;
}
}
#ifdef CPPL_DEBUG
std::cerr << "# [NOTE] zgsmatrix::del(long&, long&, std::complex<double>&) "
<< "The required component was not listed. "
<< "Your input was (" << i << "," << j << ")." << std::endl;
#endif//CPPL_DEBUG
return *this;
}
//=============================================================================
/*! delete the entry of an element */
inline zgsmatrix& zgsmatrix::fdel(const long c)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] zgsmatrix::fdel(const long&)"
<< std::endl;
#endif//CPPL_VERBOSE
#ifdef CPPL_DEBUG
if( c<0 || c>=VOL ){
std::cerr << "[ERROR] zgsmatrix::fdel(const long&)" << std::endl
<< "The required element is out of the matrix volume."
<< std::endl
<< "Your input was (" << c << ")." << std::endl;
exit(1);
}
std::cerr << "# [NOTE] zgsmatrix::fdel(const long&) "
<< "The (" << Indx[c] << "," << Jndx[c]
<< ") component is going to be deleted." << std::endl;
#endif//CPPL_DEBUG
if(c==VOL-1){//if c is the last element
VOL--;
long i(Indx[c]), j(Jndx[c]);
for(std::vector< std::pair<long,long> >::iterator q=Rows[i].begin(); q<=Rows[i].end(); q++){
if(q->first==j){ Rows[i].erase(q); break; }
}
for(std::vector< std::pair<long,long> >::iterator q=Cols[j].begin(); q<=Cols[j].end(); q++){
if(q->first==i){ Cols[j].erase(q); break; }
}
}
else{
VOL--;
long i(Indx[c]), j(Jndx[c]), I(Indx[VOL]), J(Jndx[VOL]);
Array[c]=Array[VOL];
Indx[c]=Indx[VOL];
Jndx[c]=Jndx[VOL];
//std::cerr << "c=" << c << " i=" << i << " j=" << j << " C=" << VOL << " I=" << I << " J=" << J << std::endl;
for(std::vector< std::pair<long,long> >::iterator q=Rows[i].begin(); q<=Rows[i].end(); q++){
if(q->first==j){ Rows[i].erase(q); break; }
}
for(std::vector< std::pair<long,long> >::iterator q=Cols[j].begin(); q<=Cols[j].end(); q++){
if(q->first==i){ Cols[j].erase(q); break; }
}
for(std::vector< std::pair<long,long> >::iterator q=Rows[I].begin(); q<=Rows[I].end(); q++){
if(q->first==J){ q->second=c; break; }
}
for(std::vector< std::pair<long,long> >::iterator q=Cols[J].begin(); q<=Cols[J].end(); q++){
if(q->first==I){ q->second=c; break; }
}
}
return *this;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//=============================================================================
inline std::ostream& operator<<(std::ostream& s, const zgsmatrix& mat)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] operator<<(std::ostream&, const zgsmatrix&)"
<< std::endl;
#endif//CPPL_VERBOSE
for(long i=0; i<mat.M; i++){
for(long j=0; j<mat.N; j++){
std::vector< std::pair<long,long> >::iterator q;
for(q=mat.Rows[i].begin(); q!=mat.Rows[i].end(); q++){
if(q->first==j){ break; }
}
if(q!=mat.Rows[i].end()){ s << mat.Array[q->second] << " "; }
else{ s << "x "; }
}
s << std::endl;
}
return s;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//=============================================================================
inline void zgsmatrix::write(const char* filename) const
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] zgsmatrix::write(const char*) const" << std::endl;
#endif//CPPL_VERBOSE
std::ofstream ofs(filename, std::ios::trunc);
ofs.setf(std::cout.flags());
ofs.precision(std::cout.precision());
ofs.width(std::cout.width());
ofs.fill(std::cout.fill());
ofs << "zgsmatrix" << " " << M << " " << N << " " << CAP << std::endl;
for(long c=0; c<VOL; c++){
ofs << Indx[c] << " " << Jndx[c] << " " << Array[c] << std::endl;
}
ofs.close();
}
//=============================================================================
inline void zgsmatrix::read(const char* filename)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] zgsmatrix::read(const char*)"
<< std::endl;
#endif//CPPL_VERBOSE
std::ifstream s( filename );
if(!s){
std::cerr << "[ERROR] zgsmatrix::read(const char*) " << std::endl
<< "The file \"" << filename << "\" can not be opened."
<< std::endl;
exit(1);
}
std::string id;
s >> id;
if( id != "zgsmatrix" ){
std::cerr << "[ERROR] zgsmatrix::read(const char*) " << std::endl
<< "The type name of the file \"" << filename
<< "\" is not zgsmatrix." << std::endl
<< "Its type name was " << id << " ." << std::endl;
exit(1);
}
s >> M >> N >> CAP;
resize(M, N, CAP);
std::complex<double> val;
long i, j, pos, tmp;
while(!s.eof() && VOL<CAP){
s >> i >> j >> val;
fput(i,j, val);
pos =s.tellg(); s >> tmp; s.seekg(pos);
}
if(!s.eof()){
std::cerr << "[ERROR] zgsmatrix::read(const char*) " << std::endl
<< "There is something is wrong with the file \""
<< filename << " ." << std::endl
<< "Most likely, there are too many data components "
<< "over the context." << std::endl;
exit(1);
}
s.close();
}