40 #define G_CONSTRUCTOR "GMatrixSymmetric::GMatrixSymmetric(int&, int&)"
41 #define G_MATRIX "GMatrixSymmetric::GMatrixSymmetric(GMatrix&)"
42 #define G_SPARSEMATRIX "GMatrixSymmetric::GMatrixSymmetric(GSparseMatrix&)"
43 #define G_OP_ADD "GMatrixSymmetric::operator+=(GMatrixSymmetric&)"
44 #define G_OP_SUB "GMatrixSymmetric::operator-=(GMatrixSymmetric&)"
45 #define G_OP_MUL_VEC "GMatrixSymmetric::operator*(GVector&)"
46 #define G_OP_MAT_MUL "GMatrixSymmetric::operator*=(GMatrixSymmetric&)"
47 #define G_AT "GMatrixSymmetric::at(int&, int&)"
48 #define G_EXTRACT_ROW "GMatrixSymmetric::row(int&)"
49 #define G_SET_ROW "GMatrixSymmetric::row(int&, GVector&)"
50 #define G_EXTRACT_COLUMN "GMatrixSymmetric::column(int&)"
51 #define G_SET_COLUMN "GMatrixSymmetric::column(int&, GVector&)"
52 #define G_ADD_TO_ROW "GMatrixSymmetric::add_to_row(int&, GVector&)"
53 #define G_ADD_TO_COLUMN "GMatrixSymmetric::add_to_column(int&, GVector&)"
54 #define G_CHOL_DECOMP "GMatrixSymmetric::cholesky_decompose(int&)"
55 #define G_CHOL_SOLVE "GMatrixSymmetric::cholesky_solver(GVector&, int&)"
56 #define G_CHOL_INVERT "GMatrixSymmetric::cholesky_invert(int&)"
57 #define G_COPY_MEMBERS "GMatrixSymmetric::copy_members(GMatrixSymmetric&)"
58 #define G_ALLOC_MEMBERS "GMatrixSymmetric::alloc_members(int&, int&)"
97 #if defined(G_RANGE_CHECK)
99 std::string msg =
"Number of rows "+
gammalib::str(rows)+
" is negative. "
100 "Please specify a non-negative number of rows.";
104 std::string msg =
"Number of columns "+
gammalib::str(columns)+
" is "
105 "negative. Please specify a non-negative number of "
142 for (
int col = 0; col <
m_cols; ++col) {
144 double value_ll = matrix(
row,col);
145 double value_ur = matrix(col,
row);
146 if (value_ll != value_ur) {
148 "matrix element at (row,column)=("+
152 "matrix element at (row,column)=("+
158 (*this)(
row, col) = matrix(row, col);
187 for (
int col = 0; col <
m_cols; ++col) {
189 double value_ll = matrix(
row,col);
190 double value_ur = matrix(col,
row);
191 if (value_ll != value_ur) {
193 "matrix element at (row,column)=("+
197 "matrix element at (row,column)=("+
203 (*this)(
row, col) = matrix(row, col);
261 if (
this != &matrix) {
331 const int& column)
const
362 "Please specify a vector of size "+
371 for (
int col = 0; col <
m_cols; ++col) {
372 sum += (*this)(
row,col) * vector[col];
396 double* ptr = matrix.
m_data;
397 for (
int i = 0; i < matrix.
m_elements; ++i, ++ptr) {
422 " differs from the expected number of "+
429 " differs from the expected number of "+
436 const double* src = matrix.
m_data;
483 " differs from the expected number of "+
490 " differs from the expected number of "+
497 const double* src = matrix.
m_data;
575 if (row < 0 || row >=
m_rows) {
578 if (column < 0 || column >=
m_cols) {
604 if (row < 0 || row >=
m_rows) {
607 if (column < 0 || column >=
m_cols) {
634 #if defined(G_RANGE_CHECK)
635 if (row < 0 || row >=
m_rows) {
644 for (
int col = 0; col <
m_cols; ++col) {
645 result[col] = (*this)(
row,col);
664 #if defined(G_RANGE_CHECK)
665 if (row < 0 || row >=
m_rows) {
689 #if defined(G_RANGE_CHECK)
690 if (column < 0 || column >=
m_cols) {
726 #if defined(G_RANGE_CHECK)
727 if (column < 0 || column >=
m_cols) {
738 "rows. Please specify a vector of size "+
769 #if defined(G_RANGE_CHECK)
770 if (row < 0 || row >=
m_rows) {
781 "columns. Please specify a vector of size "+
807 #if defined(G_RANGE_CHECK)
808 if (column < 0 || column >=
m_cols) {
820 "rows. Please specify a vector of size "+
901 double* dst = matrix.
m_data;
923 for (
int col = 0, i = 0; col <
m_cols; ++col) {
948 double off_diag = 0.0;
957 for (
int col =
row+1; col <
m_cols; ++col) {
963 double result = diag + 2.0 * off_diag;
984 for (
int col = 0; col <=
row; ++col) {
1008 for (
int col =
row; col <
m_cols; ++col) {
1048 int no_zeros = ((compress && (matrix.
m_num_inx == matrix.
m_rows)) || !compress);
1057 for (
int col =
row; col < matrix.
m_cols; ++col, ++ptr) {
1060 for (
int k = 0; k <
row; ++k) {
1066 std::string msg =
"Matrix is not positive definite, "
1068 " occured in row and column "+
1095 for (row = 0, row_ptr = matrix.
m_inx; row < matrix.
m_num_inx; ++row, ++row_ptr) {
1097 for (col = row, col_ptr = matrix.
m_inx + row; col < matrix.
m_num_inx; ++col, ++col_ptr) {
1098 double* ptr = ptr_0 + *col_ptr;
1100 for (k = 0, k_ptr = matrix.
m_inx; k < row; ++k, ++k_ptr) {
1101 int offset = matrix.
m_colstart[*k_ptr] - *k_ptr;
1102 sum -= matrix.
m_data[offset+*row_ptr] *
1103 matrix.
m_data[offset+*col_ptr];
1106 if (*row_ptr == *col_ptr) {
1108 std::string msg =
"Matrix is not positive definite, "
1110 " occured in row and column "+
1126 std::string msg =
"Matrix is not positive definite, all matrix "
1127 "elements are zero.";
1153 const bool& compress)
const
1159 "rows. Please specify a vector of size "+
1175 double sum = vector[
row];
1176 for (
int k = 0; k <
row; ++k) {
1183 for (
int row = m_rows-1;
row >= 0; --
row) {
1187 sum -= *ptr++ * x[k];
1194 else if (m_num_inx > 0) {
1204 double sum = vector[*row_ptr];
1205 double* ptr =
m_data + *row_ptr;
1206 for (k = 0, k_ptr =
m_inx; k <
row; ++k, ++k_ptr) {
1207 sum -= *(ptr +
m_colstart[*k_ptr] - *k_ptr) * x[*k_ptr];
1213 for (row = m_num_inx-1, row_ptr =
m_inx+m_num_inx-1; row >= 0; --
row, --row_ptr) {
1214 double sum = x[*row_ptr];
1216 double* ptr = ptr_diag - *row_ptr;
1217 for (k = row+1, k_ptr =
m_inx+row+1; k <
m_num_inx; ++k, ++k_ptr) {
1218 sum -= *(ptr + *k_ptr) * x[*k_ptr];
1220 x[*row_ptr] = sum/(*ptr_diag);
1226 std::string msg =
"All matrix elements are zero.";
1256 int no_zeros = ((compress && (matrix.
m_num_inx == matrix.
m_rows)) || !compress);
1268 for (
int col =
row+1; col < matrix.
m_cols; ++col) {
1272 double* ptr1 = matrix.
m_data + col -
row;
1274 for (
int k = row; k < col; ++k) {
1275 sum -= *(ptr1-- + matrix.
m_colstart[k]) * *ptr2++;
1286 for (
int col =
row; col < matrix.
m_cols; ++col) {
1289 double* ptr1 = ptr + col -
row;
1291 for (
int k = col; k < matrix.
m_cols; ++k) {
1292 sum += *ptr1++ * *ptr2++;
1295 *(ptr+col-
row) = sum;
1312 for (row = 0, row_ptr = matrix.
m_inx;
1313 row < matrix.
m_num_inx; ++row, ++row_ptr) {
1317 double* ptr_2 = ptr_diag - *row_ptr;
1318 *ptr_diag = 1.0/(*ptr_diag);
1320 for (col = row+1, col_ptr = matrix.
m_inx+row+1;
1321 col < matrix.
m_num_inx; ++col, ++col_ptr) {
1325 double* ptr_1 = matrix.
m_data + *col_ptr;
1326 for (k = row, k_ptr = matrix.
m_inx+row;
1327 k < col; ++k, ++k_ptr) {
1328 sum -= *(ptr_1 + matrix.
m_colstart[*k_ptr] - *k_ptr) *
1333 *(ptr_2 + *col_ptr) =
1339 for (row = 0, row_ptr = matrix.
m_inx;
1340 row < matrix.
m_num_inx; ++row, ++row_ptr) {
1342 double* ptr_1 = ptr_diag - *row_ptr;
1344 for (col = row, col_ptr = matrix.
m_inx+row;
1345 col < matrix.
m_num_inx; ++col, ++col_ptr) {
1350 for (k = col, k_ptr = matrix.
m_inx+col;
1352 sum += *(ptr_1 + *k_ptr) * *(ptr_2 + *k_ptr);
1356 *(ptr_1 + *col_ptr) = sum;
1363 std::string msg =
"All matrix elements are zero.";
1384 result.append(
"=== GMatrixSymmetric ===");
1450 for (
int i = 0; i <
m_cols; ++i) {
1495 int elements = rows*(rows+1)/2;
1498 if (rows != columns) {
1499 std::string msg =
"Number of rows "+
gammalib::str(rows)+
" differs from "
1501 "specify a symmetric matrix.";
1514 m_data =
new double[elements];
1527 for (
int col = 1; col <=
m_cols; ++col) {
1563 for (col = 0; col <
row; ++col) {
1573 for (col = row+1; col <
m_cols; ++col) {
1621 "the first matrix differs from number of "+
1623 "matrix. Please specify a second matrix with "+
1633 for (
int col = 0; col < matrix.
m_cols; ++col) {
1635 for (
int i = 0; i <
m_cols; ++i) {
1636 sum += (*this)(
row,i) * matrix(i,col);
GMatrix extract_lower_triangle(void) const
Extract lower triangle of matrix.
GMatrixSymmetric cholesky_invert(const bool &compress=true) const
Invert matrix using a Cholesky decomposition.
void free_members(void)
Delete class members.
Abstract matrix base class interface definition.
virtual ~GMatrixSymmetric(void)
Destructor.
std::string print_row_compression(const GChatter &chatter=NORMAL) const
Print row compression scheme if it exists.
GMatrixSymmetric(void)
Void constructor.
Sparse matrix class interface definition.
GVector abs(const GVector &vector)
Computes absolute of vector elements.
Generic matrix class definition.
virtual void add_to_row(const int &row, const GVector &vector)
Add row to matrix elements.
int m_rows
Number of rows.
Symmetric matrix class interface definition.
virtual double & operator()(const int &row, const int &column)
Return reference to matrix element.
virtual std::string print(const GChatter &chatter=NORMAL) const
Print matrix.
virtual void clear(void)
Clear matrix.
int m_num_inx
Number of indices in array.
GMatrixSymmetric invert(void) const
Return inverted matrix.
int m_num_colsel
Number of selected columns (for compressed decomposition)
virtual GVector column(const int &column) const
Extract column as vector from matrix.
GVector cholesky_solver(const GVector &vector, const bool &compress=true) const
Cholesky solver.
virtual GMatrixSymmetric & operator-=(const GMatrixSymmetric &matrix)
Unary matrix subtraction operator.
virtual GMatrixSymmetric & operator+=(const GMatrixSymmetric &matrix)
Unary matrix addition operator.
virtual GMatrixSymmetric * clone(void) const
Clone matrix.
GVector sqrt(const GVector &vector)
Computes square root of vector elements.
std::string print_elements(const GChatter &chatter=NORMAL, const int &num=15) const
Print all matrix elements.
double * m_data
Matrix data.
const int & rows(void) const
Return number of matrix rows.
int m_cols
Number of columns.
virtual GMatrixSymmetric operator-(void) const
Negate matrix elements.
virtual GMatrixSymmetric & operator=(const GMatrixSymmetric &matrix)
Matrix assignment operator.
int m_alloc
Size of allocated matrix memory.
std::string print_col_compression(const GChatter &chatter=NORMAL) const
Print column compression scheme if it exists.
Vector class interface definition.
Symmetric matrix class definition.
virtual GVector operator*(const GVector &v) const
Vector multiplication.
GMatrix extract_upper_triangle(void) const
Extract upper triangle of matrix.
void init_members(void)
Initialise class mambers.
int m_elements
Number of elements stored in matrix.
virtual double sum(void) const
Sum matrix elements.
virtual double fill(void) const
Determine fill of matrix.
void copy_members(const GMatrixSymmetric &matrix)
Copy class members.
Sparse matrix class definition.
GMatrixSymmetric abs(void) const
Return absolute of matrix.
GMatrixSymmetric cholesky_decompose(const bool &compress=true) const
Return Cholesky decomposition.
GVector solve(const GVector &vector) const
Solves linear matrix equation.
virtual void add_to_column(const int &column, const GVector &vector)
Add vector column into matrix.
Exception handler interface definition.
virtual GVector row(const int &row) const
Extract row as vector from matrix.
virtual GMatrixBase & operator=(const GMatrixBase &matrix)
Assignment operator.
virtual double & at(const int &row, const int &column)
Return reference to matrix element.
const int & size(void) const
Return size of vector.
Generic matrix class definition.
int m_num_rowsel
Number of selected rows (for compressed decomposition)
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
int * m_inx
Index array of non-zero rows/columns.
int * m_colstart
Column start indices (m_cols+1)
void alloc_members(const int &rows, const int &columns)
Allocates matrix memory.
int * m_rowsel
Row selection (for compressed decomposition)
const int & columns(void) const
Return number of matrix columns.
int * m_colsel
Column selection (for compressed decomposition)
void set_inx(void)
Set index selection.
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.