15 #ifndef TBCI_CRMATRIX_H
16 #define TBCI_CRMATRIX_H
18 #include "tbci/matrix_sig.h"
19 #include "tbci/matrix.h"
20 #include "tbci/vector.h"
25 #if !defined(NO_GD) && !defined(AUTO_DECL)
26 # include "tbci/crmatrix_gd.h"
32 #ifndef TBCI_DISABLE_EXCEPT
39 :
NumErr(
"Error in crmatrix library") {}
50 # pragma interface "crmatrix.h"
90 static const char*
mat_info () {
return (
"CRMatrix"); }
100 inline const T&
operator () (
unsigned int row,
unsigned int column)
const;
101 inline const T&
get (
unsigned int row,
unsigned int column)
const
102 {
return (*
this) (row, column); }
105 T&
setval (
unsigned int row,
unsigned int column);
107 {
return setval (row, column); }
115 void set_row(
T* value,
unsigned int row,
unsigned int count,
unsigned int*
index);
120 unsigned int size()
const;
123 inline void resize(
unsigned int newRows,
unsigned int newColumns);
161 friend STD__ ostream& operator<< FGD (STD__ ostream& stream, const CRMatrix<T>& m);
170 template <
typename T>
173 allocate (mat.rows(), mat.columns());
174 for (
unsigned i = 0;
i < n_rows; ++
i)
175 for (
unsigned j = 0; j < n_cols; ++j)
176 (*
this) (
i, j) = mat(
i, j);
180 template <
typename T>
183 BCHK(row >= n_rows || column >= n_cols,
CRMatErr, Illegal
index, (row<column ? row : column), dummy = (
T)0);
184 for (
unsigned int j=0; j<rowSize[row]; j++)
185 if (colIndex[row][j] == column)
187 return (dummy = (
T)0);
192 template <
typename T>
196 for (
unsigned int i=0;
i<n_rows; ++
i)
204 template <
typename T>
207 if (newRows != n_rows || newColumns != n_cols)
209 allocate(newRows, newColumns);
214 template <
typename T>
218 BCHK(column >= n_cols,
CRMatErr, Illegal col index, column, *
this);
220 for (j=0; j<rowSize[row]; ++j)
221 if (colIndex[row][j] == column) {
230 unsigned int* newIndex =
new unsigned int[rowSize[row]+1];
232 T* newComp =
new T[rowSize[row]+1];
235 for (j=0; j<rowSize[row]; ++j)
236 if (colIndex[row][j] < column) {
237 newIndex[j] = colIndex[row][j];
238 newComp[j] = comp[row][j];
242 newIndex[j] = column;
245 for (
unsigned int i=j;
i<rowSize[row];
i++) {
246 newIndex[
i+1] = colIndex[row][
i];
247 newComp[
i+1] = comp[row][
i];
250 if (rowSize[row] != 0) {
251 delete[] colIndex[row];
255 colIndex[row] = newIndex;
260 template <
typename T>
263 BCHK(row >= n_rows || column >= n_cols,
CRMatErr,
"Illegal index", (row<column ? row : column), comp[0][0]);
265 for (j=0; j<rowSize[row]; j++)
266 if (colIndex[row][j] == column)
273 unsigned int* newIndex =
new unsigned int[rowSize[row]+1];
275 T* newComp =
new T[rowSize[row]+1];
278 for (j=0; j<rowSize[row]; j++)
279 if (colIndex[row][j] < column)
281 newIndex[j] = colIndex[row][j];
282 newComp[j] = comp[row][j];
286 unsigned int pos_store(j);
287 newIndex[j] = column;
290 for (
unsigned int i=j;
i<rowSize[row];
i++)
292 newIndex[
i+1] = colIndex[row][
i];
293 newComp[
i+1] = comp[row][
i];
296 if (rowSize[row] != 0)
298 delete[] colIndex[row];
302 colIndex[row] = newIndex;
306 comp[row][pos_store]=0;
307 return comp[row][pos_store];
311 template <
typename T>
316 for (
unsigned int j=0; j<n_cols; j++)
317 RowVec.
set((*
this)(row,j),j);
322 template <
typename T>
326 unsigned int count = 0;
328 for (j=0; j<v.
size(); j++)
329 if (
v(j) != 0.0) count++;
330 if (rowSize[row] != count)
334 delete[] colIndex[row];
337 if ((rowSize[row]=count) != 0)
339 colIndex[row] =
new unsigned int[count];
341 comp[row] =
new T[row];
346 colIndex[row] =
NULL;
351 for (j=0; j<v.
size(); j++)
354 colIndex[row][count] = j;
355 comp[row][count] =
v(j);
360 template <
typename T>
367 template <
typename T>
376 template <
typename T>
379 BCHKNR(row >= n_rows || count >= n_cols,
CRMatErr, Illegal index, (row<count ? row : count));
380 if (rowSize[row] != count)
384 delete[] colIndex[row];
387 if ((rowSize[row]=count) != 0)
389 colIndex[row] =
new unsigned int[count];
391 comp[row] =
new T[row];
396 colIndex[row] =
NULL;
400 for (
unsigned int j=0; j<count; j++)
402 colIndex[row][j] = index[j];
403 comp[row][j] = value[j];
408 template <
typename T>
411 if (
this == &m)
return *
this;
414 for (
unsigned int i=0;
i<n_rows;
i++)
420 delete[] colIndex[
i];
425 colIndex[
i] =
new unsigned int[rowSize[
i]];
427 comp[
i] =
new T[rowSize[
i]];
436 for (
unsigned int j=0; j<rowSize[
i]; j++)
439 comp[
i][j] = m.
comp[
i][j];
451 template <
typename T>
456 for (
unsigned int i=0;
i<n_rows; ++
i)
457 for (
unsigned int j=0; j<n_cols; ++j)
458 if ((*
this)(
i,j) != cm(
i,j))
465 template <
typename T>
469 for (
unsigned int i=0;
i<n_rows; ++
i)
470 for (
unsigned int j=0; j<n_cols; ++j)
471 if ((*
this)(
i,j) != (
T)0)
478 template <
typename T>
483 for (
unsigned int i=0;
i<n_rows; ++
i) {
485 for (
unsigned int j=0; j<rowSize[
i]; ++j)
486 var += comp[
i][j] *
v(colIndex[
i][j]);
493 template <
typename T>
497 return (this->
operator * (v));
501 template <
typename T>
506 for (
unsigned int i=0;
i<n_rows; ++
i) {
508 for (
unsigned int j=0; j<rowSize[
i]; ++j)
509 var += comp[
i][j] * tsv.
get(colIndex[
i][j]);
518 template <
typename T>
522 for (
unsigned int i=0;
i<n_rows; ++
i) {
524 for (
unsigned int j=0; j<rowSize[
i]; ++j)
525 var += comp[
i][j] *
v(colIndex[
i][j]);
531 template <typename T>
535 template <
typename T>
539 for (
unsigned int i=0;
i<this->n_rows; ++
i) {
541 for (
unsigned int j=0; j<this->rowSize[
i]; ++j)
542 var += this->comp[
i][j] * v[this->colIndex[
i][j]];
549 template <
typename T>
557 for (
unsigned int i=0;
i<n_rows; ++
i) {
558 for (
unsigned int j=0; j<rowSize[
i]; ++j) {
559 pos = colIndex[
i][j];
567 template <
typename T>
571 return(this->transMult(v));
575 template <
typename T>
579 return(this->transMult(v));
584 template <
typename T>
588 for (
unsigned int i=0;
i<this->n_rows; ++
i)
589 for (
unsigned int j=0; j<this->rowSize[
i]; ++j)
590 res.
comp[
i][j] = this->comp[
i][j] * z;
596 template <
typename T>
600 for (
unsigned int i=0;
i<n_rows; ++
i)
601 for (
unsigned int j=0; j<rowSize[
i]; ++j)
602 res.
comp[
i][j] = z * comp[
i][j];
607 template <typename T>
609 {
return m.
mult (z); }
613 template <
typename T>
616 for (
unsigned int i=0;
i<n_rows; ++
i)
617 for (
unsigned int j=0; j<rowSize[
i]; ++j)
623 template <
typename T>
627 for (
unsigned int i=0;
i<n_rows; ++
i)
628 for (
unsigned int j=0; j<this->rowSize[
i]; ++j)
629 res.
comp[
i][j] = this->comp[
i][j] / z;
635 template <
typename T>
638 for (
unsigned int i=0;
i<n_rows;
i++)
639 for (
unsigned int j=0; j<rowSize[
i]; j++)
646 template <
typename T>
649 BCHKNR(rows <= 0 || columns <= 0,
CRMatErr, Zero space allocating, 0);
650 n_rows = rows; dummy = 0;
652 rowSize =
new unsigned int[n_rows];
654 colIndex =
new unsigned int*[n_rows];
656 comp =
new T*[n_rows];
658 for (
unsigned int i=0;
i<n_rows;
i++)
668 template <
typename T>
671 for (
unsigned int i=0;
i<n_rows; ++
i) {
672 if (colIndex[
i]!=
NULL)
delete[] colIndex[
i];
673 if (comp[
i]!=
NULL)
delete[] comp[
i];
679 if (colIndex !=
NULL)
685 template <
typename T>
689 n_cols = m.
n_cols; dummy = 0;
690 rowSize =
new unsigned int[n_rows];
692 colIndex =
new unsigned int*[n_rows];
694 comp =
new T*[n_rows];
696 for (
unsigned int i=0;
i<n_rows; ++
i) {
698 colIndex[
i] =
new unsigned int[rowSize[
i]];
700 comp[
i] =
new T[rowSize[
i]];
702 for (
unsigned int j=0; j<rowSize[
i]; ++j) {
704 comp[
i][j] = m.
comp[
i][j];
715 template <
typename T>
718 for (
unsigned int i=0;
i<n_rows; ++
i)
719 for (
unsigned int j=0; j<rowSize[
i]; ++j)
724 template <
typename T>
726 {
return fill ((
T)0); }
728 template <
typename T>
731 BCHK (n_rows != n_cols,
CRMatErr, setunit is meaningless
for non-square matrices, n_cols, *
this);
733 for (
unsigned int i = 0;
i < n_cols; ++
i)
738 template <
typename T>
747 template <
typename T>
751 for (
unsigned r = 0; r < rows(); ++r)
752 for (
unsigned c = 0;
c < columns(); ++
c)
753 m(
c,r) = (*this)(r,
c);
757 template <
typename T>
765 template <typename T>
772 template <
typename T>
773 STD__ ostream& operator<< (STD__ ostream& stream, const CRMatrix<T>& m)
775 for (
unsigned int i = 0;
i < m.
rows(); ++
i) {
777 for (
unsigned int j = 0; j < m.
columns(); ++j)
778 stream << m(
i,j) <<
"\t";
781 return stream <<
STD__ flush;
BdMatrix< T > transpose(BdMatrix< T > &mat)
#define BCHKNR(cond, exc, txt, ind)
T & setval(const unsigned long i) const
CRMatrix< T > mult(const T &z) const
bool operator==(const CRMatrix< T > &m) const
CRMatrix< T > & fill(const T &)
set all defined element to a val
TVector< T > transMult(const Vector< T > &v) const
exception base class for the TBCI NumLib
Common interface definition (signature) for all Matrices.
CRMatrix< T > & clear()
set all elements defined to zero
#define BCHK(cond, exc, txt, ind, rtval)
TVector< T > operator*(const Vector< T > &v) const
CRMatrix< T > & swap(CRMatrix< T > &)
T aligned_value_type TALIGN(MIN_ALIGN2)
T & set(const T &val, const unsigned long i) const
const Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > & z
static const char * mat_info()
CRMatErr(const CRMatErr &ce)
T get(const unsigned long i) const HOT
void set_row(const Vector< T > &v, unsigned int row)
CRMatErr(const char *t, const long i=0)
void allocate(unsigned int rows, unsigned int columns)
C++ class for sparse matrices using compressed row storage.
CRMatrix< T > operator/(const T &z) const
long int Vector< T > & index
CRMatrix(const CRMatrix< T > &m)
CRMatrix(unsigned int rows, unsigned int columns)
CRMatrix< T > & operator/=(const T &z)
unsigned int rows() const
void MatVecMult(Vector< T > &res, const Vector< T > &v) const
CRMatrix< T > transposed_copy() const
Inefficient! Use transMult if possible.
CRMatrix< T > & setunit(const T &=(T) 1)
set CSCMatrix to val times the unit matrix
void SWAP(T &a, T &b)
SWAP function Note: We could implement a swap function without temporaries: a -= b b += a a -= b a = ...
unsigned long size() const
bool operator!=(const CRMatrix< T > &m) const
CRMatrix< T > & transpose()
const T & operator()(unsigned int row, unsigned int column) const
element access (read)
void resize(unsigned int newRows, unsigned int newColumns)
void copy(const CRMatrix< T > &m)
CRMatrix< T > & operator*=(const T &z)
unsigned int columns() const
Temporary Base Class Idiom: Class TVector is used for temporary variables.
unsigned long size() const
void MatVecMult(Vector< T > &res, const CRMatrix< T > &m, const Vector< T > &v)
unsigned int size() const
const Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > long int res
CRMatrix< T > operator-() const
CRMatrix< T > & operator=(const CRMatrix< T > &m)
CRMatrix< T > & setval(const T &z, unsigned int row, unsigned int column)
element access (write)
const Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > long int int char v
< find minimun of func on grid with resolution res
TVector< T > get_row(unsigned int row) const