29 const int BUFFER_CHUNK_SIZE = 256;
43 extern MATHLIB IterativeMethod itmethod_general;
44 extern MATHLIB IterativeMethod itmethod_complex;
45 extern MATHLIB
int IterCount;
86 double &tol,
int maxit = 0);
150 virtual void New (
int nrows,
int ncols);
152 virtual MT
Get (
int r,
int c)
const = 0;
164 virtual bool Exists (
int r,
int c)
const = 0;
166 virtual void Unlink ();
169 void Initialise (
int nv,
const MT *data);
170 void Initialise (
int nv, MT *data, CopyMode cmode);
178 inline int nVal()
const {
return nval; }
180 inline MT &Val (
int i)
181 { dASSERT(i >= 0 && i < nval,
"Index out of range");
186 inline MT *ValPtr () {
return val; }
187 inline const MT *ValPtr ()
const {
return val; }
189 virtual MT &operator() (
int r,
int c) = 0;
192 virtual void Add (
int r,
int c,
const MT &val)
193 { (*this)(r,c) += val; }
195 virtual int Get_index (
int r,
int c)
const = 0;
199 virtual MT GetNext (
int &r,
int &c)
const = 0;
225 inline double FillFraction ()
const
227 (
double)nval/(
double)(this->rows * this->cols) : 0.0); }
230 virtual void Display (std::ostream &os)
const;
233 virtual void PrintFillinGraph (
const char *fname,
int maxdim = 600,
234 bool binary =
true,
bool antialias =
true);
257 static void GlobalSelectIterativeSolver (IterativeMethod method);
258 static void GlobalSelectIterativeSolver_complex (IterativeMethod method);
259 static IterativeMethod GetGlobalIterativeSolver ();
260 static IterativeMethod GetGlobalIterativeSolver_complex ();
303 double &tol,
int maxit);
312 void Append (MT v = 0);
321 static void cg_loop1(
void*,
int,
int);
322 static void cg_loop2(
void*,
int,
int);
323 static void cg_loop3(
void*,
int,
int);
364 int nv,
const MT *data)
368 Initialise (nv, data);
375 int nv, MT *data, CopyMode cmode)
379 Initialise (nv, data, cmode);
391 int nsize = (nbuf > nval ? nbuf : nval);
394 for (
int i = 0; i < nval; i++) val[i] = m.val[i];
420 if (nbuf)
delete []val;
431 if (nbuf)
delete []val;
432 if ((nbuf = (nval = nv))) val =
new MT[nv];
434 if (data)
for (i = 0; i < nv; i++) val[i] = data[i];
435 else for (i = 0; i < nv; i++) val[i] = (MT)0;
443 if (cmode == SHALLOW_COPY) {
444 dASSERT (data,
"Nonzero data buffer reference required");
451 }
else Initialise (nv, data);
459 for (
int i = 0; i < nval; i++)
468 for (
int i = 0; i < nval; i++)
479 MT *tmp_val =
new MT[nbuf+BUFFER_CHUNK_SIZE];
480 for (
int i = 0; i < nval; i++) tmp_val[i] = val[i];
481 if (nbuf)
delete []val;
483 nbuf += BUFFER_CHUNK_SIZE;
493 for (
int r = 0; r < this->rows; r++)
494 for (
int c = 0; c < this->cols; c++)
495 os << Get(r,c) << (c < this->cols-1 ?
' ' :
'\n');
499 inline int scale (
double x)
500 {
return (
int)(x*2.56e7+0.5); }
504 bool binary,
bool antialias)
506 int i, v, nx, ny, c, r = -1, *img;
508 if (this->cols <= maxdim && this->rows <= maxdim) {
509 nx = this->cols, ny = this->rows;
510 img =
new int[nx*ny];
511 memset (img, 0, nx*ny*
sizeof(
int));
519 double d, x, y, rx, ry, wx0, wx1, wx2, wy0, wy1, wy2, dummy;
520 if (this->cols > this->rows)
521 nx = maxdim, ny = (maxdim*this->rows)/this->cols;
523 ny = maxdim, nx = (maxdim*this->cols)/this->rows;
524 d = 0.5*(double)nx/(
double)this->cols;
525 img =
new int[nx*ny];
526 memset (img, 0, nx*ny*
sizeof(
int));
530 x = (c+0.5)/(double)this->cols * (
double)nx;
531 rx = modf (x, &dummy);
532 ix = (int)(dummy+0.5);
533 y = (r+0.5)/(double)this->rows * (
double)ny;
534 ry = modf (y, &dummy);
535 iy = (int)(dummy+0.5);
541 wx0 = d-rx, wx1 = d+rx, wx2 = 0.0;
542 }
else if (rx > 1.0-d) {
543 wx0 = 0.0, wx1 = d+1.0-rx, wx2 = d-1.0+rx;
545 wx0 = wx2 = 0.0, wx1 = d+d;
548 wy0 = d-ry, wy1 = d+ry, wy2 = 0.0;
549 }
else if (ry > 1.0-d) {
550 wy0 = 0.0, wy1 = d+1.0-ry, wy2 = d-1.0+ry;
552 wy0 = wy2 = 0.0, wy1 = d+d;
554 img[iy*nx+ix] += scale(wx1*wy1);
556 img[iy*nx+ix-1] += scale(wx0*wy1);
557 if (wy0 && iy) img[(iy-1)*nx+ix-1] += scale(wx0*wy0);
558 else if (wy2 && iy<ny-1) img[(iy+1)*nx+ix-1] += scale(wx0*wy2);
559 }
else if (wx2 && ix<nx-1) {
560 img[iy*nx+ix+1] += scale(wx2*wy1);
561 if (wy0 && iy) img[(iy-1)*nx+ix+1] += scale(wx2*wy0);
562 else if (wy2 && iy<ny-1) img[(iy+1)*nx+ix+1] += scale(wx2*wy2);
565 img[(iy-1)*nx+ix] += scale(wx1*wy0);
566 }
else if (wy2 && iy<ny-1) {
567 img[(iy+1)*nx+ix] += scale(wx1*wy2);
571 for (i = 0; i < nx*ny; i++)
572 if ((img[i] /= 100000) > 255) img[i] = 255;
574 std::ofstream ofs (fname);
575 ofs << (binary ?
"P5" :
"P2") << std::endl;
576 ofs <<
"# CREATOR: TOAST TGenericSparseMatrix::PrintFillinGraph" << std::endl;
577 ofs << nx <<
' ' << ny << std::endl;
578 ofs << 255 << std::endl;
579 for (r = i = 0; r < ny; r++) {
580 for (c = 0; c < nx; c++) {
582 if (binary) ofs << (
unsigned char)v;
583 else ofs << v << (++i % 35 ?
' ' :
'\n');
594 (IterativeMethod method)
595 { itmethod_general = method; }
599 (IterativeMethod method)
600 { itmethod_complex = method; }
604 {
return itmethod_general; }
608 {
return itmethod_complex; }
614 RGenericSparseMatrix Re(
const CGenericSparseMatrix &);
615 RGenericSparseMatrix Im(
const CGenericSparseMatrix &);
618 #endif // !__GSMATRIX_H
Virtual base class for sparse matrix types.
Definition: gsmatrix.h:47
virtual bool Exists(int r, int c) const =0
Checks allocation of a matrix element.
Definition: gsmatrix.h:48
virtual ~TGenericSparseMatrix()
Destructor.
Definition: gsmatrix.h:401
virtual void New(int nrows, int ncols)
Resize and reset the matrix.
Definition: matrix.h:462
virtual MT Get(int r, int c) const =0
Retrieve a matrix element.
TGenericSparseMatrix()
Create a sparse matrix of size 0 x 0.
Definition: gsmatrix.h:354
Virtual base class for all matrix types (dense and sparse)
Definition: matrix.h:43
virtual void New(int nrows, int ncols)
Reset the matrix dimensions.
Definition: gsmatrix.h:409
virtual void ExportRCV(std::ostream &os)
Write sparse matrix to ASCII output stream.
Definition: gsmatrix.h:255