43#define G_CONSTRUCTOR "GMatrixSparse::GMatrixSparse(int&, int&, int&)"
44#define G_OP_MUL_VEC "GMatrixSparse::operator*(GVector&)"
45#define G_OP_ADD "GMatrixSparse::operator+=(GMatrixSparse&)"
46#define G_OP_SUB "GMatrixSparse::operator-=(GMatrixSparse&)"
47#define G_OP_MAT_MUL "GMatrixSparse::operator*=(GMatrixSparse&)"
48#define G_AT "GMatrixSparse::at(int&, int&)"
49#define G_EXTRACT_ROW "GMatrixSparse::row(int&)"
50#define G_SET_ROW "GMatrixSparse::row(int&, GVector&)"
51#define G_EXTRACT_COLUMN "GMatrixSparse::column(int&)"
52#define G_SET_COLUMN "GMatrixSparse::column(int&, GVector&)"
53#define G_SET_COLUMN2 "GMatrixSparse::column(int&, double*, int*, int)"
54#define G_ADD_TO_COLUMN1 "GMatrixSparse::add_to_column(int&, GVector&)"
55#define G_ADD_TO_COLUMN2 "GMatrixSparse::add_to_column(int&,"\
56 " const GVectorSparse& vector)"
57#define G_ADD_TO_COLUMN3 "GMatrixSparse::add_to_column(int&,"\
58 " double*, int*, int)"
59#define G_MULTIPLY_COLUMN "GMatrixSparse::multiply_column(int&, GVector&)"
60#define G_CHOL_DECOMP "GMatrixSparse::cholesky_decompose(bool)"
61#define G_CHOL_SOLVE "GMatrixSparse::cholesky_solver(GVector&, bool)"
62#define G_STACK_INIT "GMatrixSparse::stack_init(int&, int&)"
63#define G_STACK_PUSH1 "GMatrixSparse::stack_push_column(GVector&, int&)"
64#define G_STACK_PUSH2 "GMatrixSparse::stack_push_column(double*, int*, int&,"\
66#define G_STACK_FLUSH "GMatrixSparse::stack_flush(void)"
67#define G_COLUMN_TO_VECTOR1 "GMatrixSparse::column_to_vector(int&, GVector*)"
68#define G_COLUMN_TO_VECTOR2 "GMatrixSparse::column_to_sparse_vector("\
69 "int&, GVectorSparse*)"
70#define G_COLFILL_SET_COLUMN "GMatrixSparse::colfill_set_column(int&,"\
72#define G_COLFILL_FLUSH "GMatrixSparse::colfill_flush()"
73#define G_COPY_MEMBERS "GMatrixSparse::copy_members(GMatrixSparse&)"
74#define G_ALLOC_MEMBERS "GMatrixSparse::alloc_members(int&, int&, int&)"
75#define G_GET_INDEX "GMatrixSparse::get_index(int&, int&)"
76#define G_ALLOC "GMatrixSparse::alloc_elements(int&, int&)"
77#define G_FREE "GMatrixSparse::free_elements(int&, int&)"
78#define G_REMOVE_ZERO "GMatrixSparse::remove_zero_row_col(void)"
79#define G_INSERT_ROW "GMatrixSparse::insert_row(int&, GVector&, bool&)"
80#define G_SYMPERM "cs_symperm(GMatrixSparse*, int*, int&)"
81#define G_TRANSPOSE "cs_transpose(GMatrixSparse*, int)"
84#define G_MIN(a,b) (((a) < (b)) ? (a) : (b))
85#define G_MAX(a,b) (((a) > (b)) ? (a) : (b))
141 const int& elements) :
146 #if defined(G_RANGE_CHECK)
149 "Please specify a non-negative number of rows.";
154 "negative. Please specify a non-negative number of "
189 for (
int col = 0; col <
m_cols; ++col) {
191 this->
column(col, vector);
217 for (
int col = 0; col <
m_cols; ++col) {
219 this->
column(col, vector);
278 if (
this != &matrix) {
321 for (
int col = 0; col <=
m_cols; ++col) {
335 for (
int col = 0; col <
m_cols; ++col) {
402 const int& column)
const
416 value = (
double*)&(
m_data[inx]);
445 "Please specify a vector of size "+
457 for (
int col = 0; col <
m_cols; ++col) {
460 double* ptr_data =
m_data + i_start;
461 int* ptr_rowinx =
m_rowinx + i_start;
462 for (
int i = i_start; i < i_stop; ++i) {
463 result[*ptr_rowinx++] += *ptr_data++ * vector[col];
470 #if defined(G_DEBUG_SPARSE_PENDING)
473 <<
") has been used." << std::endl;
498 double* ptr = matrix.
m_data;
499 for (
int i = 0; i < matrix.
m_elements; ++i, ++ptr) {
529 for (
int col = 0; col <
m_cols; ++col) {
530 if ((*
this)(
row,col) != matrix(
row,col)) {
585 " differs from the expected number of "+
592 " differs from the expected number of "+
610 for (
int col = 0; col <
m_cols; ++col) {
622 bool remains_left = (i_left < v_left.
elements());
623 bool remains_right = (i_right < v_right.
elements());
626 while (remains_left || remains_right) {
629 if (remains_left && remains_right) {
632 int inx_left = v_left.
inx(i_left);
633 int inx_right = v_right.
inx(i_right);
637 if (inx_left == inx_right) {
638 v_result.
m_inx[i_result] = inx_left;
647 else if (inx_left < inx_right) {
648 v_result.
m_inx[i_result] = inx_left;
656 v_result.
m_inx[i_result] = inx_right;
665 else if (remains_left) {
666 v_result.
m_inx[i_result] = v_left.
inx(i_left);
674 v_result.
m_inx[i_result] = v_right.
inx(i_right);
681 remains_left = (i_left < v_left.
elements());
682 remains_right = (i_right < v_right.
elements());
714 for (
int col = 0; col <
m_cols; ++col) {
742 " differs from the expected number of "+
749 " differs from the expected number of "+
767 for (
int col = 0; col <
m_cols; ++col) {
779 bool remains_left = (i_left < v_left.
elements());
780 bool remains_right = (i_right < v_right.
elements());
783 while (remains_left || remains_right) {
786 if (remains_left && remains_right) {
789 int inx_left = v_left.
inx(i_left);
790 int inx_right = v_right.
inx(i_right);
794 if (inx_left == inx_right) {
795 v_result.
m_inx[i_result] = inx_left;
804 else if (inx_left < inx_right) {
805 v_result.
m_inx[i_result] = inx_left;
814 v_result.
m_inx[i_result] = inx_right;
823 else if (remains_left) {
824 v_result.
m_inx[i_result] = v_left.
inx(i_left);
832 v_result.
m_inx[i_result] = v_right.
inx(i_right);
839 remains_left = (i_left < v_left.
elements());
840 remains_right = (i_right < v_right.
elements());
872 for (
int col = 0; col <
m_cols; ++col) {
902 "the first matrix differs from number of "+
904 "matrix. Please specify a second matrix with "+
917 for (
int col = 0; col < matrix.
m_cols; ++col) {
919 for (
int i = 0; i <
m_cols; ++i) {
920 sum += (*this)(
row,i) * matrix(i,col);
1034 #if defined(G_RANGE_CHECK)
1044 for (
int col = 0; col <
m_cols; ++col) {
1051 if (i_stop > i_start) {
1054 while ((i_stop - i_start) > 1) {
1055 int mid = (i_start+i_stop) / 2;
1066 result[col] =
m_data[i_start];
1113 #if defined(G_RANGE_CHECK)
1128 for (
int i = i_start; i < i_stop; ++i) {
1162 #if defined(G_DEBUG_SPARSE_INSERTION)
1163 std::cout <<
"GMatrixSparse::column(";
1164 std::cout <<
column <<
", [" << vector <<
"]):" << std::endl;
1165 std::cout <<
" In Data : ";
1167 std::cout <<
m_data[i] <<
" ";
1169 std::cout << std::endl <<
" In Row .: ";
1173 std::cout << std::endl <<
" In Col .: ";
1174 for (
int i = 0; i <
m_cols+1; ++i) {
1177 std::cout << std::endl;
1181 #if defined(G_RANGE_CHECK)
1193 "rows. Please specify a vector of size "+
1201 #if defined(G_DEBUG_SPARSE_PENDING)
1204 ") became obsolete" << std::endl;
1227 int n_exist = i_stop - i_start;
1233 int n_diff = n_vector - n_exist;
1238 #if defined(G_DEBUG_SPARSE_INSERTION)
1239 std::cout <<
" Insert .: " << n_diff <<
" elements at index " << i_start << std::endl;
1242 else if (n_diff < 0) {
1244 #if defined(G_DEBUG_SPARSE_INSERTION)
1245 std::cout <<
" Remove .: " << -n_diff <<
" elements at index " << i_start << std::endl;
1252 if (vector[
row] != 0.0) {
1266 #if defined(G_DEBUG_SPARSE_INSERTION)
1267 std::cout <<
" Out Data: ";
1269 std::cout <<
m_data[i] <<
" ";
1271 std::cout << std::endl <<
" Out Row : ";
1275 std::cout << std::endl <<
" Out Col : ";
1276 for (
int i = 0; i <
m_cols+1; ++i) {
1279 std::cout << std::endl;
1307 const int* rows,
int number)
1310 #if defined(G_DEBUG_SPARSE_INSERTION)
1311 std::cout <<
"GMatrixSparse::column(";
1312 std::cout <<
column <<
", values, rows, " << number <<
"):" << std::endl;
1313 std::cout <<
" Matrix Data : ";
1315 std::cout <<
m_data[i] <<
" ";
1317 std::cout << std::endl <<
" Matrix Row .: ";
1321 std::cout << std::endl <<
" Matrix Col .: ";
1322 for (
int i = 0; i <
m_cols+1; ++i) {
1325 std::cout << std::endl;
1326 std::cout <<
" Array Data .: ";
1327 for (
int i = 0; i < number; ++i) {
1328 std::cout << values[i] <<
" ";
1330 std::cout << std::endl <<
" Array Row ..: ";
1331 for (
int i = 0; i < number; ++i) {
1332 std::cout <<
rows[i] <<
" ";
1334 std::cout << std::endl;
1338 #if defined(G_RANGE_CHECK)
1351 "rows. Please specify only row numbers smaller than "+
1359 #if defined(G_DEBUG_SPARSE_PENDING)
1360 std::cout << G_INSERT_COL2 <<
": pending value " <<
m_fill_val <<
1362 ") became obsolete" << std::endl;
1373 int n_exist = i_stop - i_start;
1377 if (!values || !
rows) {
1385 int n_diff = number - n_exist;
1390 #if defined(G_DEBUG_SPARSE_INSERTION)
1391 std::cout <<
" Insert .: " << n_diff <<
" elements at index " << i_start << std::endl;
1394 else if (n_diff < 0) {
1396 #if defined(G_DEBUG_SPARSE_INSERTION)
1397 std::cout <<
" Remove .: " << -n_diff <<
" elements at index " << i_start << std::endl;
1403 for (
int row = 0, i = i_start;
row < number; ++
row, ++i) {
1415 #if defined(G_DEBUG_SPARSE_INSERTION)
1416 std::cout <<
" Out Data: ";
1418 std::cout <<
m_data[i] <<
" ";
1420 std::cout << std::endl <<
" Out Row : ";
1424 std::cout << std::endl <<
" Out Col : ";
1425 for (
int i = 0; i <
m_cols+1; ++i) {
1428 std::cout << std::endl;
1472 #if defined(G_DEBUG_SPARSE_ADDITION)
1473 std::cout <<
"GMatrixSparse::add_col(";
1474 std::cout <<
column <<
", [" << v <<
"]):" << std::endl;
1475 std::cout <<
" In Data : ";
1477 std::cout <<
m_data[i] <<
" ";
1479 std::cout << std::endl <<
" In Row .: ";
1483 std::cout << std::endl <<
" In Col .: ";
1484 for (
int i = 0; i <
m_cols+1; ++i) {
1487 std::cout << std::endl;
1498 if (non_zero == 0) {
1508 #if defined(G_RANGE_CHECK)
1520 "rows. Please specify a vector of size "+
1547 this->
column(column, v_column);
1552 #if defined(G_DEBUG_SPARSE_ADDITION)
1553 std::cout <<
" Out Data: ";
1555 std::cout <<
m_data[i] <<
" ";
1557 std::cout << std::endl <<
" Out Row : ";
1561 std::cout << std::endl <<
" Out Col : ";
1562 for (
int i = 0; i <
m_cols+1; ++i) {
1565 std::cout << std::endl;
1586 const double* values = vector.
m_data;
1618 const int* rows,
int number)
1621 #if defined(G_DEBUG_SPARSE_ADDITION)
1622 std::cout <<
"GMatrixSparse::add_col(";
1623 std::cout <<
column <<
", values, rows, " << number <<
"):" << std::endl;
1624 std::cout <<
" Matrix Data : ";
1626 std::cout <<
m_data[i] <<
" ";
1628 std::cout << std::endl <<
" Matrix Row .: ";
1632 std::cout << std::endl <<
" Matrix Col .: ";
1633 for (
int i = 0; i <
m_cols+1; ++i) {
1636 std::cout << std::endl;
1637 std::cout <<
" Array Data .: ";
1638 for (
int i = 0; i < number; ++i) {
1639 std::cout << values[i] <<
" ";
1641 std::cout << std::endl <<
" Array Row ..: ";
1642 for (
int i = 0; i < number; ++i) {
1643 std::cout <<
rows[i] <<
" ";
1645 std::cout << std::endl;
1662 if (!values || !
rows || (number < 1)) {
1667 #if defined(G_RANGE_CHECK)
1680 "rows. Please specify only row numbers smaller than "+
1693 if (i_start < i_stop) {
1699 int wrk_size = number + i_stop - i_start;
1700 double* wrk_double =
new double[wrk_size];
1701 int* wrk_int =
new int[wrk_size];
1706 values,
rows, number,
1707 wrk_double, wrk_int, &num_mix);
1710 this->
column(column, wrk_double, wrk_int, num_mix);
1714 delete [] wrk_double;
1724 #if defined(G_DEBUG_SPARSE_ADDITION)
1725 std::cout <<
" Out Data: ";
1727 std::cout <<
m_data[i] <<
" ";
1729 std::cout << std::endl <<
" Out Row : ";
1733 std::cout << std::endl <<
" Out Col : ";
1734 for (
int i = 0; i <
m_cols+1; ++i) {
1737 std::cout << std::endl;
1761 #if defined(G_RANGE_CHECK)
1773 "rows. Please specify a vector of size "+
1898 double* ptr = matrix.
m_data;
1899 for (
int i = 0; i < matrix.
m_elements; ++i, ++ptr) {
1900 *ptr = std::abs(*ptr);
1919 double result = 0.0;
1931 result = double(num) / double(
size);
1950 if (
m_data[i] < result) {
1976 if (
m_data[i] > result) {
2028 int matrix_rows = matrix.
m_rows;
2029 int matrix_cols = matrix.
m_cols;
2068 for (
int i = 0; i < matrix.
m_elements; ++i) {
2072 for (
int col = 0; col <= matrix.
m_cols; ++col) {
2104 const bool& compress)
const
2107 #if defined(G_DEBUG_SPARSE_COMPRESSION)
2108 std::cout <<
"GMatrixSparse::cholesky_solver" << std::endl;
2109 std::cout <<
" Input vector .....: " << vector << std::endl;
2116 "rows. Please specify a vector of size "+
2123 std::string msg =
"Matrix not factorised. Please call method "
2124 "GMatrixSparse::cholesky_decompose() before calling "
2131 std::string msg =
"Matrix not factorised. Please call method "
2132 "GMatrixSparse::cholesky_decompose() before calling "
2142 int no_zero = !(compress && (row_compressed || col_compressed));
2162 for (
int i = 0; i < vector.
size(); ++i) {
2167 for (
int col = 0; col <
m_cols; ++col) {
2168 x[col] /= Lx[Lp[col]];
2169 for (
int p = Lp[col]+1; p < Lp[col+1]; p++) {
2170 x[Li[p]] -= Lx[p] * x[col];
2175 for (
int col =
m_cols-1; col >= 0; --col) {
2176 for (
int p = Lp[col]+1; p < Lp[col+1]; p++)
2177 x[col] -= Lx[p] * x[Li[p]];
2178 x[col] /= Lx[Lp[col]];
2182 for (
int i = 0; i <
m_cols; ++i) {
2192 int* row_map =
new int[
m_rows];
2193 int* col_map =
new int[
m_cols];
2198 if (row_compressed) {
2211 #if defined(G_DEBUG_SPARSE_COMPRESSION)
2212 std::cout <<
" Row mapping ......:";
2214 std::cout <<
" " << row_map[
row];
2216 std::cout << std::endl;
2223 if (col_compressed) {
2224 for (
int col = 0; col <
m_cols; ++col) {
2232 for (
int col = 0; col <
m_cols; ++col) {
2236 #if defined(G_DEBUG_SPARSE_COMPRESSION)
2237 std::cout <<
" Column mapping ...:";
2238 for (
int col = 0; col <
m_cols; ++col) {
2239 std::cout <<
" " << col_map[col];
2241 std::cout << std::endl;
2250 x[c_row] = vector[
m_rowsel[c_row]];
2256 #if defined(G_DEBUG_SPARSE_COMPRESSION)
2257 std::cout <<
" Compressed vector : " << x << std::endl;
2262 #if defined(G_DEBUG_SPARSE_COMPRESSION)
2263 std::cout <<
" Permutated vector : " << x << std::endl;
2268 for (
int col = 0; col <
m_cols; ++col) {
2269 int c_col = col_map[col];
2271 x[c_col] /= Lx[Lp[col]];
2272 for (
int p = Lp[col]+1; p < Lp[col+1]; p++) {
2273 int c_row = row_map[Li[p]];
2275 x[c_row] -= Lx[p] * x[c_col];
2280 #if defined(G_DEBUG_SPARSE_COMPRESSION)
2281 std::cout <<
" Solve Lx=x .......: " << x << std::endl;
2286 for (
int col =
m_cols-1; col >= 0; --col) {
2287 int c_col = col_map[col];
2289 for (
int p = Lp[col]+1; p < Lp[col+1]; p++) {
2290 int c_row = row_map[Li[p]];
2292 x[c_col] -= Lx[p] * x[c_row];
2295 x[c_col] /= Lx[Lp[col]];
2298 #if defined(G_DEBUG_SPARSE_COMPRESSION)
2299 std::cout <<
" Solve L'x=x ......: " << x << std::endl;
2304 #if defined(G_DEBUG_SPARSE_COMPRESSION)
2305 std::cout <<
" Permutated vector : " << x << std::endl;
2312 result[
m_colsel[c_col]] = x[c_col];
2318 #if defined(G_DEBUG_SPARSE_COMPRESSION)
2319 std::cout <<
" Restored vector ..: " << result << std::endl;
2353 for (
int col = 0; col <
m_cols; ++col) {
2440 #if defined(G_RANGE_CHECK)
2441 if (col < 0 || col >=
m_cols) {
2451 "rows. Please specify a vector of size "+
2460 for (
int i = 0; i < vector.
size(); ++i) {
2461 if (vector[i] != 0.0) {
2502 const int& number,
const int& col)
2505 #if defined(G_RANGE_CHECK)
2506 if (col < 0 || col >=
m_cols) {
2513 int remaining = number;
2519 if (!values || !
rows || (number < 1)) {
2530 "rows. Please specify only row numbers smaller than "+
2543 #if defined(G_DEBUG_SPARSE_STACK_PUSH)
2544 std::cout <<
"GMatrixSparse::stack_push_column(v, i, n, col=" << col <<
")";
2545 std::cout << std::endl;
2546 std::cout <<
" Data to push on stack ...:";
2547 for (
int i = 0; i < number; ++i) {
2548 std::cout <<
" " << values[i];
2550 std::cout << std::endl;
2551 std::cout <<
" Row indices of data .....:";
2552 for (
int i = 0; i < number; ++i) {
2553 std::cout <<
" " <<
rows[i];
2555 std::cout << std::endl;
2589 rows, number, &num_1, &num_2, &num_mix);
2590 int num_request = num_1 + num_2 + num_mix;
2606 values,
rows, number,
2625 if (remaining == 0) {
2631 for (
int i = 0; i < number; ++i) {
2648 #if defined(G_DEBUG_SPARSE_STACK_PUSH)
2649 std::cout <<
" Number of stack entries .: " <<
m_stack_entries << std::endl;
2650 std::cout <<
" Stack entry columns .....:";
2654 std::cout << std::endl;
2655 std::cout <<
" Stack entry starts ......:";
2660 std::cout <<
" Stack data ..............:";
2664 std::cout << std::endl;
2665 std::cout <<
" Stack rows ..............:";
2669 std::cout << std::endl;
2702 #if defined(G_DEBUG_SPARSE_STACK_FLUSH)
2703 std::cout <<
"GMatrixSparse::stack_flush" << std::endl;
2704 std::cout <<
" Number of columns on stack : " <<
m_stack_entries << std::endl;
2706 std::cout <<
" Number of matrix elements .: " <<
m_elements << std::endl;
2707 std::cout <<
" Col.start at end of matrix : " <<
m_colstart[
m_cols] << std::endl;
2712 for (
int col = 0; col <
m_cols; ++col) {
2722 int new_elements = 0;
2723 #if defined(G_DEBUG_SPARSE_STACK_FLUSH)
2724 int num_columns = 0;
2746 #if defined(G_DEBUG_SPARSE_STACK_FLUSH)
2771 &num_1, &num_2, &num_mix);
2774 new_elements += num_2;
2775 #if defined(G_DEBUG_SPARSE_STACK_FLUSH)
2776 num_matrix += num_1;
2778 num_both += num_mix;
2795 #if defined(G_DEBUG_SPARSE_STACK_FLUSH)
2796 std::cout <<
" Valid columns on stack ....: " << num_columns << std::endl;
2797 std::cout <<
" Valid elements on stack ...: " << new_elements << std::endl;
2805 double* new_data =
new double[
m_alloc];
2806 int* new_rowinx =
new int[
m_alloc];
2811 for (
int col = 0; col <
m_cols; ++col) {
2825 new_data[index] =
m_data[i];
2828 #if defined(G_DEBUG_SPARSE_STACK_FLUSH)
2842 #if defined(G_DEBUG_SPARSE_STACK_FLUSH)
2867 &(new_data[index]), &(new_rowinx[index]), &num);
2882 #if defined(G_DEBUG_SPARSE_STACK_FLUSH)
2883 std::cout <<
" Added elements ............: " << index;
2884 std::cout <<
" (should be " << elements <<
")" << std::endl;
2885 std::cout <<
" - Matrix only .............: " << num_matrix << std::endl;
2886 std::cout <<
" - Stack only ..............: " << num_stack << std::endl;
2887 std::cout <<
" - Matrix & Stack ..........: " << num_both << std::endl;
2891 for (
int col =
m_cols; col > 0; --col) {
2954 #if defined(G_RANGE_CHECK)
2962 if (vector == NULL) {
2963 std::string msg =
"NULL vector argument provided. Please specify a "
2964 "valid GVector object.";
2971 " differs from number of matrix rows "+
2973 "with length identical to the number of matrix "
2979 for (
int i = 0; i <
m_rows; ++i) {
2988 for (
int i = i_start; i < i_stop; ++i) {
3021 #if defined(G_RANGE_CHECK)
3029 if (vector == NULL) {
3030 std::string msg =
"NULL sparse vector argument provided. Please "
3031 "specify a valid GVectorSparse object.";
3053 for (
int i = i_start, idst = 0; i < i_stop; ++i, ++idst) {
3089 for (
int i = 0; i <
m_cols; ++i) {
3128 std::string msg =
"No memory for column filling was allocated. "
3129 "Please call colfill_init() method before invoking "
3135 #if defined(G_RANGE_CHECK)
3147 "rows. Please specify a vector of size "+
3170 int* rowinx =
new int[n_vector];
3171 double* data =
new double[n_vector];
3179 if (vector[
row] != 0.0) {
3180 *data++ = vector[
row];
3217 std::string msg =
"No memory for column filling was allocated. "
3218 "Please call colfill_init() method before invoking "
3224 #if defined(G_RANGE_CHECK)
3236 "rows. Please specify a vector of size "+
3256 int* rowinx =
new int[vector.
elements()];
3257 double* data =
new double[vector.
elements()];
3264 for (
int i = 0; i < vector.
elements(); ++i) {
3265 *data++ = vector.
m_data[i];
3266 *rowinx++ = vector.
m_inx[i];
3289 std::string msg =
"No memory for column filling was allocated. "
3290 "Please call colfill_init() method before invoking "
3308 for (
int i = 0; i <
m_cols; ++i) {
3325 for (
int col = 0; col <
m_cols; ++col) {
3388 result.append(
"=== GMatrixSparse ===");
3420 result.append(
" (none)");
3486 for (
int i = 0; i < matrix.
m_elements; ++i) {
3552 for (
int i = 0; i <
m_cols; ++i) {
3561 for (
int i = 0; i <
m_cols; ++i) {
3586 const int& elements)
3604 for (
int col = 0; col <=
m_cols; ++col) {
3697 #if defined(G_RANGE_CHECK)
3721 int i_start = *ptr_colstart++;
3722 int i_stop = *ptr_colstart;
3725 if (i_stop > i_start) {
3728 while ((i_stop - i_start) > 1) {
3729 int mid = (i_start+i_stop) / 2;
3769 #if defined(G_DEBUG_SPARSE_PENDING) || defined(G_DEBUG_SPARSE_INSERTION)
3770 std::cout <<
"GMatrixSparse::fill_pending(): pending value " <<
3771 m_fill_val <<
" will be filled in location (" <<
3784 int i_start = *ptr_colstart++;
3785 int i_stop = *ptr_colstart;
3786 int* ptr_rowinx =
m_rowinx + i_start;
3787 for (inx_insert = i_start; inx_insert < i_stop; ++inx_insert) {
3788 int row_test = *ptr_rowinx++;
3813 #if defined(G_DEBUG_SPARSE_PENDING) || defined(G_DEBUG_SPARSE_INSERTION)
3814 std::cout <<
" Data: ";
3816 std::cout <<
m_data[i] <<
" ";
3818 std::cout << std::endl <<
" Row.: ";
3822 std::cout << std::endl <<
" Col.: ";
3823 for (
int i = 0; i <
m_cols+1; ++i) {
3826 std::cout << std::endl;
3852 #if defined(G_DEBUG_SPARSE_MALLOC)
3853 std::cout <<
"GMatrixSparse::alloc_elements(start=" << start <<
", num=" <<
3854 num <<
")" << std::endl;
3855 std::cout <<
" Before allocation : " <<
m_elements <<
" " <<
m_alloc << std::endl;
3874 for (
int i =
m_elements - 1; i >= start; --i) {
3880 for (
int i = start; i < start+num; ++i) {
3896 m_alloc = (new_size > new_propose) ? new_size : new_propose;
3899 double* new_data =
new double[
m_alloc];
3900 int* new_rowinx =
new int[
m_alloc];
3903 for (
int i = 0; i < start; ++i) {
3909 for (
int i = start; i < start+num; ++i) {
3916 new_data[i+num] =
m_data[i];
3934 #if defined(G_DEBUG_SPARSE_MALLOC)
3935 std::cout <<
" After allocation .: " <<
m_elements <<
" " <<
m_alloc << std::endl;
3957 #if defined(G_DEBUG_SPARSE_MALLOC)
3958 std::cout <<
"GMatrixSparse::free_elements(start=" << start <<
", num=" <<
3959 num <<
")" << std::endl;
3990 double* new_data =
new double[
m_alloc];
3991 int* new_rowinx =
new int[
m_alloc];
3994 for (
int i = 0; i < start; ++i) {
4000 for (
int i = start; i < new_size; ++i) {
4001 new_data[i] =
m_data[i+num];
4020 for (
int i = start; i < new_size; ++i) {
4053 #if defined(G_DEBUG_SPARSE_COMPRESSION)
4054 std::cout <<
"GMatrixSparse::remove_zero_row_col" << std::endl;
4070 std::string msg =
"All matrix elements are zero.";
4075 int* row_map =
new int[
m_rows];
4094 #if defined(G_DEBUG_SPARSE_COMPRESSION)
4096 for (
int col = 0; col <=
m_cols; ++col)
4098 std::cout << std::endl;
4112 for (
int i = i_start; i < i_stop; ++i) {
4116 *d_rowinx++ = c_row;
4127 #if defined(G_DEBUG_SPARSE_COMPRESSION)
4131 std::cout << std::endl;
4159 #if defined(G_DEBUG_SPARSE_COMPRESSION)
4160 std::cout <<
"GMatrixSparse::insert_zero_row_col(" <<
rows <<
"," << cols <<
4180 #if defined(G_DEBUG_SPARSE_COMPRESSION)
4184 std::cout << std::endl;
4191 int col_stop = cols - 1;
4195 int col_start =
m_colsel[c_col-1] + 1;
4197 for (
int col = col_start; col <= col_stop; ++col) {
4200 col_stop = col_start - 1;
4204 for (
int col = 0; col <= col_stop; ++col) {
4213 #if defined(G_DEBUG_SPARSE_COMPRESSION)
4214 std::cout <<
" after restoration (" <<
m_colstart[cols] <<
") :";
4215 for (
int col = 0; col <= cols; ++col) {
4218 std::cout << std::endl;
4249 const int* src2_row,
int src2_num,
4250 int* num_1,
int* num_2,
int* num_mix)
4260 int row_1 = src1_row[inx_1];
4261 int row_2 = src2_row[inx_2];
4266 while (inx_1 < src1_num && inx_2 < src2_num) {
4269 if (row_1 == row_2) {
4273 if (inx_1 < src1_num) {
4274 row_1 = src1_row[inx_1];
4276 if (inx_2 < src2_num) {
4277 row_2 = src2_row[inx_2];
4282 else if (row_1 < row_2) {
4285 if (inx_1 < src1_num) {
4286 row_1 = src1_row[inx_1];
4294 if (inx_2 < src2_num) {
4295 row_2 = src2_row[inx_2];
4303 if (inx_1 < src1_num) {
4304 *num_1 += (src1_num - inx_1);
4309 if (inx_2 < src2_num) {
4310 *num_2 += (src2_num - inx_2);
4334 const int* src1_row,
4336 const double* src2_data,
4337 const int* src2_row,
4347 int row_1 = src1_row[inx_1];
4348 int row_2 = src2_row[inx_2];
4351 while (inx_1 < src1_num && inx_2 < src2_num) {
4354 if (row_1 == row_2) {
4355 dst_data[inx] = src1_data[inx_1++] + src2_data[inx_2++];
4356 dst_row[inx] = row_1;
4357 if (inx_1 < src1_num) {
4358 row_1 = src1_row[inx_1];
4360 if (inx_2 < src2_num) {
4361 row_2 = src2_row[inx_2];
4367 else if (row_1 < row_2) {
4368 dst_data[inx] = src1_data[inx_1++];
4369 dst_row[inx] = row_1;
4370 if (inx_1 < src1_num) {
4371 row_1 = src1_row[inx_1];
4378 dst_data[inx] = src2_data[inx_2++];
4379 dst_row[inx] = row_2;
4380 if (inx_2 < src2_num) {
4381 row_2 = src2_row[inx_2];
4393 for (
int i = inx_1; i < src1_num; ++i, ++inx) {
4394 dst_data[inx] = src1_data[i];
4395 dst_row[inx] = src1_row[i];
4400 for (
int i = inx_2; i < src2_num; ++i, ++inx) {
4401 dst_data[inx] = src2_data[i];
4402 dst_row[inx] = src2_row[i];
4432 #if defined(G_RANGE_CHECK)
4442 "columns. Please specify a vector of size "+
4452 std::vector<int> new_columns;
4453 std::vector<int> k_insert;
4454 for (
int i =
m_cols-1; i >= 0; --i) {
4455 if (vector[i] != 0.0) {
4458 bool add_column =
true;
4459 for (; k < istop; ++k) {
4475 new_columns.push_back(i);
4476 k_insert.push_back(k);
4483 if (!new_columns.empty()) {
4486 int new_size =
m_elements + new_columns.size();
4489 bool new_memory =
false;
4490 double* new_data =
m_data;
4501 m_alloc = (new_size > new_propose) ? new_size : new_propose;
4504 new_data =
new double[
m_alloc];
4505 new_rowinx =
new int[
m_alloc];
4513 int n_insert = new_columns.size();
4515 for (
int i = 0; i < new_columns.size(); ++i) {
4518 for (
int k = k_insert[i]; k < k_last; ++k) {
4519 new_data[k+n_insert] =
m_data[k];
4520 new_rowinx[k+n_insert] =
m_rowinx[k];
4525 new_data[k_insert[i]+n_insert-1] = vector[new_columns[i]];
4526 new_rowinx[k_insert[i]+n_insert-1] =
row;
4530 k_last = k_insert[i];
4536 n_insert = new_columns.size();
4537 for (
int i =
m_cols; i > 0; --i) {
4538 if (new_columns[i_insert] == i) {
4590 int i, j, p, q, i2, j2;
4596 double* Ax = matrix.
m_data;
4603 int* wrk_int =
new int[wrk_size];
4604 for (i = 0; i < wrk_size; ++i) {
4614 for (j = 0; j < n; j++) {
4617 j2 = pinv ? pinv[j] : j;
4620 for (p = Ap[j]; p < Ap[j+1]; p++) {
4622 if (i > j)
continue;
4623 i2 = pinv ? pinv[i] : i;
4624 wrk_int[
G_MAX(i2, j2)]++;
4632 for (j = 0 ; j < n ; j++) {
4635 j2 = pinv ? pinv[j] : j;
4638 for (p = Ap[j]; p < Ap[j+1]; p++) {
4640 if (i > j)
continue;
4641 i2 = pinv ? pinv[i] : i;
4642 Ci[q = wrk_int[
G_MAX(i2,j2)]++] =
G_MIN(i2,j2);
4643 if (Cx) Cx[q] = Ax[p];
4682 int wrk_size = matrix.
m_rows;
4683 int* wrk_int =
new int[wrk_size];
4684 for (
int i = 0; i < wrk_size; ++i) {
4703 for (
int col = 0; col < matrix.
m_cols; ++col) {
4705 int i = wrk_int[matrix.
m_rowinx[p]]++;
4714 for (
int col = 0; col < matrix.
m_cols; ++col) {
4743 if (!p || !c)
return (-1);
4750 for (
int i = 0; i < n; ++i) {
Exception handler interface definition.
#define G_COLFILL_SET_COLUMN
#define G_COLUMN_TO_VECTOR1
GMatrixSparse cs_transpose(const GMatrixSparse &matrix, int values)
#define G_COLUMN_TO_VECTOR2
double cs_cumsum(int *p, int *c, int n)
cs_cumsum
GMatrixSparse cs_symperm(const GMatrixSparse &matrix, const int *pinv)
cs_symperm
#define G_MULTIPLY_COLUMN
Sparse matrix class definition.
#define G_SPARSE_MATRIX_DEFAULT_MEM_BLOCK
#define G_SPARSE_MATRIX_DEFAULT_STACK_SIZE
Symmetric matrix class definition.
Generic matrix class definition.
Sparse matrix numeric analysis class definition.
Sparse matrix symbolic analysis class definition.
Sparce vector class interface definition.
GVector iperm(const GVector &vector, const int *p)
Computes vector inverse permutation.
GVector perm(const GVector &vector, const int *p)
Computes vector permutation.
Vector class interface definition.
Abstract matrix base class interface definition.
const int & rows(void) const
Return number of matrix rows.
int m_num_rowsel
Number of selected rows (for compressed decomposition)
const int & size(void) const
Return number of matrix elements.
std::string print_col_compression(const GChatter &chatter=NORMAL) const
Print column compression scheme if it exists.
int m_cols
Number of columns.
int m_rows
Number of rows.
const int & columns(void) const
Return number of matrix columns.
virtual GMatrixBase & operator=(const GMatrixBase &matrix)
Assignment operator.
int * m_colsel
Column selection (for compressed decomposition)
double * m_data
Matrix data.
bool is_empty(void) const
Check if matrix is empty.
int m_num_colsel
Number of selected columns (for compressed decomposition)
std::string print_row_compression(const GChatter &chatter=NORMAL) const
Print row compression scheme if it exists.
void select_non_zero(void)
Setup compressed matrix factorisation.
std::string print_elements(const GChatter &chatter=NORMAL, const int &num=15) const
Print all matrix elements.
int m_alloc
Size of allocated matrix memory.
int * m_colstart
Column start indices (m_cols+1)
int * m_rowsel
Row selection (for compressed decomposition)
int m_elements
Number of elements stored in matrix.
Sparse matrix class interface definition.
virtual GVector column(const int &column) const
Extract column as vector from matrix.
double ** m_columns_data
Data for all columns.
GMatrixSparse transpose(void) const
Return transposed matrix.
void stack_destroy(void)
Destroy matrix stack.
virtual void add_to_column(const int &column, const GVector &vector)
Add vector column into matrix.
virtual void clear(void)
Clear matrix.
void free_colfill_members(void) const
Delete quick column filling class members.
void stack_init(const int &size=0, const int &entries=0)
Initialises matrix filling stack.
void column_to_vector(const int &column, GVector *vector) const
Extract matrix into vector.
virtual GMatrixSparse * clone(void) const
Clone matrix.
void free_members(void)
Delete class members.
GSparseSymbolic * m_symbolic
Holds GSparseSymbolic object after decomposition.
friend class GSparseNumeric
virtual std::string print(const GChatter &chatter=NORMAL) const
Print matrix.
virtual ~GMatrixSparse(void)
Destructor.
GMatrixSparse abs(void) const
Return absolute of matrix.
void init_members(void)
Initialise class mambers.
void insert_zero_row_col(const int &rows, const int &cols)
Insert zero rows and columns.
GMatrixSparse cholesky_invert(const bool &compress=true) const
Invert matrix using a Cholesky decomposition.
virtual double fill(void) const
Returns fill of matrix.
void init_stack_members(void)
Initialise fill stack.
void remove_zero_row_col(void)
Remove rows and columns with zeros.
GMatrixSparse invert(void) const
Return inverted matrix.
friend GMatrixSparse cs_transpose(const GMatrixSparse &matrix, int values)
int m_mem_block
Memory block to be allocated at once.
virtual GMatrixSparse & operator+=(const GMatrixSparse &matrix)
Unary matrix addition operator.
virtual bool operator!=(const GMatrixSparse &matrix) const
Non-equality operator.
GMatrixSparse cholesky_decompose(const bool &compress=true) const
Return Cholesky decomposition.
virtual double & operator()(const int &row, const int &column)
Return reference to matrix element.
virtual double sum(void) const
Sum matrix elements.
int ** m_columns_rowinx
Row indices for all columns.
void mix_column(const double *src1_data, const int *src1_row, int src1_num, const double *src2_data, const int *src2_row, int src2_num, double *dst_data, int *dst_row, int *dst_num)
Mix of sparse columns.
GVector solve(const GVector &vector) const
Solves linear matrix equation.
int m_stack_max_entries
Maximum number of entries in the stack.
virtual double min(void) const
Return minimum matrix element.
int * m_stack_colinx
Column index for each entry [m_stack_entries].
virtual GVector row(const int &row) const
Extract row as vector from matrix.
double m_zero
The zero element (needed for data access)
friend class GSparseSymbolic
void multiply_column(const int &column, const GVector &vector)
Multiply matrix column with vector.
double * m_stack_values
Stack push double buffer [m_cols].
GVector cholesky_solver(const GVector &vector, const bool &compress=true) const
Cholesky solver.
void stack_flush(void)
Flush matrix stack.
GSparseNumeric * m_numeric
Holds GSparseNumeric object after decomposition.
int m_stack_size
Maximum number of elements in the stack.
int * m_stack_rowinx
Stack row indices [m_stack_size].
int m_fill_col
Column into which element needs to be filled.
int * m_rowinx
Row-indices of all elements.
void alloc_members(const int &rows, const int &cols, const int &elements=0)
Allocate matrix.
void free_stack_members(void)
Delete fill-stack class members.
void colfill_set_column(const int &column, const GVector &vector)
Set column for column filling.
int * m_columns_length
Length of all columns.
void colfill_flush(void)
Flush column filling memory into sparse matrix.
virtual GMatrixSparse operator-(void) const
Negate matrix elements.
void colfill_init(void) const
Initialise memory for column filling.
int m_stack_entries
Number of entries in the stack.
virtual double & at(const int &row, const int &column)
Return reference to matrix element.
int stack_push_column(const GVector &vector, const int &col)
Push a vector column on the matrix stack.
virtual GVector operator*(const GVector &vector) const
Vector multiplication.
GMatrixSparse(void)
Void matrix constructor.
virtual double max(void) const
Return maximum matrix element.
void alloc_elements(int start, const int &num)
Allocate memory for new matrix elements.
virtual GMatrixSparse & operator-=(const GMatrixSparse &matrix)
Unary matrix subtraction operator.
int * m_stack_start
Start in stack for each entry [m_stack_entries+1].
virtual GMatrixSparse & operator=(const GMatrixSparse &matrix)
Matrix assignment operator.
int m_fill_row
Row into which element needs to be filled.
void mix_column_prepare(const int *src1_row, int src1_num, const int *src2_row, int src2_num, int *num_1, int *num_2, int *num_mix)
Prepare mix of sparse columns.
void free_elements(const int &start, const int &num)
Free memory for obsolete matrix elements.
virtual GMatrixSparse & operator*=(const GMatrixSparse &matrix)
Unary matrix multiplication operator.
double m_fill_val
Element to be filled.
double * m_stack_data
Stack data [m_stack_size].
int * m_stack_rows
Stack push integer working array [m_cols].
void copy_members(const GMatrixSparse &m)
Copy class members.
void insert_row(const int &row, const GVector &vector, const bool &add)
Insert row in matrix.
int get_index(const int &row, const int &column) const
Determines element index for (row,column)
virtual bool operator==(const GMatrixSparse &matrix) const
Equalty operator.
virtual void add_to_row(const int &row, const GVector &vector)
Add row to matrix elements.
int * m_stack_work
Stack flush integer working array [m_cols].
void fill_pending(void)
Fills pending matrix element.
Symmetric matrix class interface definition.
virtual GVector column(const int &column) const
Extract column as vector from matrix.
Generic matrix class definition.
virtual GVector column(const int &column) const
Extract column as vector from matrix.
Sparse matrix numeric analysis class.
void cholesky_numeric_analysis(const GMatrixSparse &m, const GSparseSymbolic &s)
Sparse matrix symbolic analysis class.
int * m_pinv
Inverse row permutation for QR, fill reduce permutation for Cholesky.
void cholesky_symbolic_analysis(int order, const GMatrixSparse &m)
int m_elements
Number of elements in vector.
void alloc_members(const int &alloc)
Allocate memory for elements.
int m_alloc
Number of allocated elements.
const int & size(void) const
Return full size of sparse vector.
const int & inx(const int &index) const
Return inx for sparse vector element.
const int & elements(void) const
Return number of elements in sparse vector.
double * m_data
Data array.
int m_colinx
Column index.
const int & size(void) const
Return size of vector.
int non_zeros(void) const
Returns number of non-zero elements in vector.
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.