//=============================================================================
/*! operator() for const object */
inline std::complex<double> zhsmatrix::operator()(const long& i, const long& j) const
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] zhsmatrix::operator()(const long&, const long&) const" << std::endl;
#endif//CPPL_VERBOSE
#ifdef CPPL_DEBUG
if( i<0 || j<0 || N<=i || N<=j ){
std::cerr << "[ERROR] zhsmatrix::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=Line[i].begin(); p!=Line[i].end(); p++){
if(p->first==j){
if(i>=j){ return Array[p->second]; }
else{ return std::conj(Array[p->second]); }
}
}
//// (i,j) component was not found ////
return std::complex<double>(0.0,0.0);
}
//=============================================================================
/*! operator() */
inline __zhecomplex zhsmatrix::operator()(const long& i, const long& j)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] zhsmatrix::operator()(const long&, const long&) const"
<< std::endl;
#endif//CPPL_VERBOSE
#ifdef CPPL_DEBUG
if( i<0 || j<0 || N<=i || N<=j ){
std::cerr << "[ERROR] zhsmatrix::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=Line[i].begin(); p!=Line[i].end(); p++){
if(p->first==j){ return __zhecomplex(Array[p->second], i, j); }
}
//////// (i,j) component not found ////////
//// vol check ////
if(VOL==CAP){
expand(CPPL_SECTOR_SIZE);
#ifdef CPPL_VERBOSE
std::cerr << "# [NOTE] zhsmatrix::put(long&, long&, std::complex<std::complex<std::complex<double>>>&) " << "The matrix volume was automatically expanded." << std::endl;
#endif//CPPL_VERBOSE
}
//// push_back ////
const long ii(std::max(i,j)), jj(std::min(i,j));
Array[VOL]=std::complex<double>(0.0,0.0);
Indx[VOL]=ii;
Jndx[VOL]=jj;
Line[ii].push_back(std::pair<long,long>(jj,VOL));
if(i!=j){
Line[jj].push_back(std::pair<long,long>(ii,VOL));
}
VOL++;
return __zhecomplex(Array[VOL-1], i, j);
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//=============================================================================
/*! put value with volume cheack without isListed check */
inline zhsmatrix& zhsmatrix::put(const long& i, const long& j, const std::complex<double>& v)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] zhsmatrix::put(const long&, const long&, const std::complex<std::complex<std::complex<double>>>&)" << std::endl;
#endif//CPPL_VERBOSE
#ifdef CPPL_DEBUG
if( i<0 || j<0 || N<=i || N<=j ){
std::cerr << "[ERROR] zhsmatrix::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] zhsmatrix::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
//#ifdef CPPL_DEBUG
if( i==j && std::fabs(v.imag()) > DBL_MIN ){
std::cerr << "[WARNING] zhsmatrix::fput(const long&, const long&, const std::complex<double>&)" << std::endl
<< "Diagonal components of a hermitian matrix have to be real numbers." << std::endl
<< "Your input to the (" << i << "," << j << ") element was a complex number, " << v << "." << std::endl;
}
//#endif//CPPL_DEBUG
//// vol check ////
if(VOL==CAP){
expand(CPPL_SECTOR_SIZE);
#ifdef CPPL_VERBOSE
std::cerr << "# [NOTE] zhsmatrix::put(long&, long&, std::complex<std::complex<std::complex<double>>>&)" << "The matrix volume was automatically expanded." << std::endl;
#endif//CPPL_VERBOSE
}
//// push ////
const long ii(std::max(i,j)), jj(std::min(i,j));
if(i>=j){ Array[VOL]=v; }
else{ Array[VOL]=std::conj(v); }
Indx[VOL]=ii;
Jndx[VOL]=jj;
Line[ii].push_back(std::pair<long,long>(jj,VOL));
if(i!=j){
Line[jj].push_back(std::pair<long,long>(ii,VOL));
}
VOL++;
return *this;
}
//=============================================================================
/*! put value without isListed check and volume cheack */
inline zhsmatrix& zhsmatrix::fput(const long& i, const long& j, const std::complex<double>& v)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] zhsmatrix::fput(const long&, const long&, const std::complex<std::complex<std::complex<double>>>&)" << std::endl;
#endif//CPPL_VERBOSE
const long ii(std::max(i,j)), jj(std::min(i,j));
#ifdef CPPL_DEBUG
if( i<0 || j<0 || N<=i || N<=j ){
std::cerr << "[ERROR] zhsmatrix::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] zhsmatrix::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]==ii && Jndx[c]==jj){
std::cerr << "[ERROR] zhsmatrix::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
//#ifdef CPPL_DEBUG
if( i==j && std::fabs(v.imag()) > DBL_MIN ){
std::cerr << "[WARNING] zhsmatrix::fput(const long&, const long&, const std::complex<double>&)" << std::endl
<< "Diagonal components of a hermitian matrix have to be real numbers." << std::endl
<< "Your input to the (" << i << "," << j << ") element was a complex number, " << v << "." << std::endl;
}
//#endif//CPPL_DEBUG
if(i>=j){ Array[VOL]=v; }
else{ Array[VOL]=std::conj(v); }
Indx[VOL]=ii;
Jndx[VOL]=jj;
Line[ii].push_back(std::pair<long,long>(jj,VOL));
if(i!=j){
Line[jj].push_back(std::pair<long,long>(ii,VOL));
}
VOL++;
return *this;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//=============================================================================
/*! delete the entry of a component */
inline zhsmatrix& zhsmatrix::del(const long i, const long j)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] zhsmatrix::del(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::del(long&, long&, std::complex<std::complex<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=Line[i].begin(); p!=Line[i].end(); p++){
if(p->first==j){
VOL--;
long c(p->second);
Array[c]=Array[VOL];
Indx[c]=Indx[VOL];
Jndx[c]=Jndx[VOL];
//// remove the component ////
Line[i].erase(p);
if(i!=j){
for(std::vector< std::pair<long,long> >::iterator pj=Line[j].begin(); pj!=Line[i].end(); pj++){
if(pj->first==i){ Line[j].erase(pj); }
}
}
//// reset the moved ////
long I(Indx[c]), J(Jndx[c]);
for(std::vector< std::pair<long,long> >::iterator q=Line[I].begin(); q<=Line[I].end(); q++){
if(q->first==J){ q->second=c; break; }
}
if(I!=J){
for(std::vector< std::pair<long,long> >::iterator q=Line[J].begin(); q<=Line[J].end(); q++){
if(q->first==I){ q->second=c; break; }
}
}
return *this;
}
}
#ifdef CPPL_DEBUG
std::cerr << "# [NOTE] zhsmatrix::del(long&, long&, std::complex<std::complex<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 zhsmatrix& zhsmatrix::fdel(const long c)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] zhsmatrix::fdel(const long&)" << std::endl;
#endif//CPPL_VERBOSE
#ifdef CPPL_DEBUG
if( c<0 || c>=VOL ){
std::cerr << "[ERROR] zhsmatrix::fdel(const long&)" << std::endl
<< "The required element is out of the matrix volume." << std::endl
<< "Your input was (" << c << ")." << std::endl;
exit(1);
}
#endif//CPPL_DEBUG
#ifdef CPPL_VERBOSE
std::cerr << "# [NOTE] zhsmatrix::fdel(const long&)" << " The (" << Indx[c] << "," << Jndx[c] << ") component is going to be deleted." << std::endl;
#endif//CPPL_VERBOSE
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=Line[i].begin(); q<=Line[i].end(); q++){
if(q->first==j){ Line[i].erase(q); break; }
}
if(i!=j){
for(std::vector< std::pair<long,long> >::iterator q=Line[j].begin(); q<=Line[j].end(); q++){
if(q->first==i){ Line[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=Line[i].begin(); q<=Line[i].end(); q++){
if(q->first==j){ Line[i].erase(q); break; }
}
if(i!=j){
for(std::vector< std::pair<long,long> >::iterator q=Line[j].begin(); q<=Line[j].end(); q++){
if(q->first==i){ Line[j].erase(q); break; }
}
}
for(std::vector< std::pair<long,long> >::iterator q=Line[I].begin(); q<=Line[I].end(); q++){
if(q->first==J){ q->second=c; break; }
}
if(I!=J){
for(std::vector< std::pair<long,long> >::iterator q=Line[J].begin(); q<=Line[J].end(); q++){
if(q->first==I){ q->second=c; break; }
}
}
}
return *this;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//=============================================================================
inline std::ostream& operator<<(std::ostream& s, const zhsmatrix& mat)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] operator<<(std::ostream&, const zhsmatrix&)" << std::endl;
#endif//CPPL_VERBOSE
for(long i=0; i<mat.N; i++){
for(long j=0; j<mat.N; j++){
if( i >= j ){
std::vector< std::pair<long,long> >::iterator q;
for(q=mat.Line[i].begin(); q!=mat.Line[i].end(); q++){
if(q->first==j){ break; }
}
if(q!=mat.Line[i].end()){ s << " " << mat.Array[q->second] << " "; }
else{ s << " x "; }
}
else{//i<j
std::vector< std::pair<long,long> >::iterator q;
for(q=mat.Line[i].begin(); q!=mat.Line[i].end(); q++){
if(q->first==j){ break; }
}
if(q!=mat.Line[i].end()){ s << "{" << std::conj(mat.Array[q->second]) << "}"; }
else{ s << "{x}"; }
}
}
s << std::endl;
}
return s;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//=============================================================================
inline void zhsmatrix::write(const char* filename) const
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] zhsmatrix::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 << "zhsmatrix" << " " << N << " " << CAP << std::endl;
for(long c=0; c<VOL; c++){
ofs << Indx[c] << " " << Jndx[c] << " " << Array[c] << std::endl;
}
ofs.close();
}
//=============================================================================
inline void zhsmatrix::read(const char* filename)
{
#ifdef CPPL_VERBOSE
std::cerr << "# [MARK] zhsmatrix::read(const char*)" << std::endl;
#endif//CPPL_VERBOSE
std::ifstream s( filename );
if(!s){
std::cerr << "[ERROR] zhsmatrix::read(const char*) " << std::endl
<< "The file \"" << filename << "\" can not be opened." << std::endl;
exit(1);
}
std::string id;
s >> id;
if( id != "zhsmatrix" ){
std::cerr << "[ERROR] zhsmatrix::read(const char*) " << std::endl
<< "The type name of the file \"" << filename << "\" is not zhsmatrix." << std::endl
<< "Its type name was " << id << " ." << std::endl;
exit(1);
}
s >> N >> CAP;
resize(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] zhsmatrix::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();
}