42 #define G_CONSTRUCTOR "GMatrixSparse::GMatrixSparse(int&, int&, int&)"
43 #define G_OP_MUL_VEC "GMatrixSparse::operator*(GVector&)"
44 #define G_OP_ADD "GMatrixSparse::operator+=(GMatrixSparse&)"
45 #define G_OP_SUB "GMatrixSparse::operator-=(GMatrixSparse&)"
46 #define G_OP_MAT_MUL "GMatrixSparse::operator*=(GMatrixSparse&)"
47 #define G_AT "GMatrixSparse::at(int&, int&)"
48 #define G_EXTRACT_ROW "GMatrixSparse::row(int&)"
49 #define G_SET_ROW "GMatrixSparse::row(int&, GVector&)"
50 #define G_EXTRACT_COLUMN "GMatrixSparse::column(int&)"
51 #define G_SET_COLUMN "GMatrixSparse::column(int&, GVector&)"
52 #define G_SET_COLUMN2 "GMatrixSparse::column(int&, double*, int*, int)"
53 #define G_ADD_TO_COLUMN "GMatrixSparse::add_to_column(int&, GVector&)"
54 #define G_ADD_TO_COLUMN2 "GMatrixSparse::add_to_column(int&, double*,"\
56 #define G_CHOL_DECOMP "GMatrixSparse::cholesky_decompose(bool)"
57 #define G_CHOL_SOLVE "GMatrixSparse::cholesky_solver(GVector&, bool)"
58 #define G_STACK_INIT "GMatrixSparse::stack_init(int&, int&)"
59 #define G_STACK_PUSH1 "GMatrixSparse::stack_push_column(GVector&, int&)"
60 #define G_STACK_PUSH2 "GMatrixSparse::stack_push_column(double*, int*, int&,"\
62 #define G_STACK_FLUSH "GMatrixSparse::stack_flush(void)"
63 #define G_COPY_MEMBERS "GMatrixSparse::copy_members(GMatrixSparse&)"
64 #define G_ALLOC_MEMBERS "GMatrixSparse::alloc_members(int&, int&, int&)"
65 #define G_GET_INDEX "GMatrixSparse::get_index(int&, int&)"
66 #define G_ALLOC "GMatrixSparse::alloc_elements(int&, int&)"
67 #define G_FREE "GMatrixSparse::free_elements(int&, int&)"
68 #define G_REMOVE_ZERO "GMatrixSparse::remove_zero_row_col(void)"
69 #define G_INSERT_ROW "GMatrixSparse::insert_row(int&, GVector&, bool&)"
70 #define G_SYMPERM "cs_symperm(GMatrixSparse*, int*, int&)"
71 #define G_TRANSPOSE "cs_transpose(GMatrixSparse*, int)"
74 #define G_MIN(a,b) (((a) < (b)) ? (a) : (b))
75 #define G_MAX(a,b) (((a) > (b)) ? (a) : (b))
131 const int& elements) :
136 #if defined(G_RANGE_CHECK)
138 std::string msg =
"Number of rows "+
gammalib::str(rows)+
" is negative. "
139 "Please specify a non-negative number of rows.";
143 std::string msg =
"Number of columns "+
gammalib::str(columns)+
" is "
144 "negative. Please specify a non-negative number of "
179 for (
int col = 0; col <
m_cols; ++col) {
181 this->
column(col, vector);
207 for (
int col = 0; col <
m_cols; ++col) {
209 this->
column(col, vector);
268 if (
this != &matrix) {
311 for (
int col = 0; col <=
m_cols; ++col) {
325 for (
int col = 0; col <
m_cols; ++col) {
326 this->
column(col, column);
392 const int& column)
const
406 value = (
double*)&(
m_data[inx]);
435 "Please specify a vector of size "+
447 for (
int col = 0; col <
m_cols; ++col) {
450 double* ptr_data =
m_data + i_start;
451 int* ptr_rowinx =
m_rowinx + i_start;
452 for (
int i = i_start; i < i_stop; ++i) {
453 result[*ptr_rowinx++] += *ptr_data++ * vector[col];
460 #if defined(G_DEBUG_SPARSE_PENDING)
463 <<
") has been used." << std::endl;
488 double* ptr = matrix.
m_data;
489 for (
int i = 0; i < matrix.
m_elements; ++i, ++ptr) {
519 for (
int col = 0; col <
m_cols; ++col) {
520 if ((*
this)(
row,col) != matrix(
row,col)) {
576 " differs from the expected number of "+
583 " differs from the expected number of "+
590 for (
int col = 0; col <
m_cols; ++col) {
593 v_result += v_operand;
612 for (
int col = 0; col <
m_cols; ++col) {
641 " differs from the expected number of "+
648 " differs from the expected number of "+
655 for (
int col = 0; col <
m_cols; ++col) {
658 v_result -= v_operand;
677 for (
int col = 0; col <
m_cols; ++col) {
707 "the first matrix differs from number of "+
709 "matrix. Please specify a second matrix with "+
722 for (
int col = 0; col < matrix.
m_cols; ++col) {
724 for (
int i = 0; i <
m_cols; ++i) {
725 sum += (*this)(
row,i) * matrix(i,col);
788 if (row < 0 || row >=
m_rows) {
791 if (column < 0 || column >=
m_cols) {
796 return ((*
this)(row, column));
813 if (row < 0 || row >=
m_rows) {
816 if (column < 0 || column >=
m_cols) {
821 return ((*
this)(row, column));
839 #if defined(G_RANGE_CHECK)
840 if (row < 0 || row >=
m_rows) {
849 for (
int col = 0; col <
m_cols; ++col) {
856 if (i_stop > i_start) {
859 while ((i_stop - i_start) > 1) {
860 int mid = (i_start+i_stop) / 2;
871 result[col] =
m_data[i_start];
918 #if defined(G_RANGE_CHECK)
919 if (column < 0 || column >=
m_cols) {
933 for (
int i = i_start; i < i_stop; ++i) {
967 #if defined(G_DEBUG_SPARSE_INSERTION)
968 std::cout <<
"GMatrixSparse::column(";
969 std::cout << column <<
", [" << vector <<
"]):" << std::endl;
970 std::cout <<
" In Data : ";
972 std::cout <<
m_data[i] <<
" ";
974 std::cout << std::endl <<
" In Row .: ";
978 std::cout << std::endl <<
" In Col .: ";
979 for (
int i = 0; i <
m_cols+1; ++i) {
982 std::cout << std::endl;
986 #if defined(G_RANGE_CHECK)
987 if (column < 0 || column >= m_cols) {
998 "rows. Please specify a vector of size "+
1006 #if defined(G_DEBUG_SPARSE_PENDING)
1009 ") became obsolete" << std::endl;
1019 if (vector[
row] != 0.0) {
1028 int n_exist = i_stop - i_start;
1034 int n_diff = n_vector - n_exist;
1039 #if defined(G_DEBUG_SPARSE_INSERTION)
1040 std::cout <<
" Insert .: " << n_diff <<
" elements at index " << i_start << std::endl;
1043 else if (n_diff < 0) {
1045 #if defined(G_DEBUG_SPARSE_INSERTION)
1046 std::cout <<
" Remove .: " << -n_diff <<
" elements at index " << i_start << std::endl;
1053 if (vector[
row] != 0.0) {
1062 for (
int i = column+1; i <=
m_cols; ++i) {
1067 #if defined(G_DEBUG_SPARSE_INSERTION)
1068 std::cout <<
" Out Data: ";
1070 std::cout <<
m_data[i] <<
" ";
1072 std::cout << std::endl <<
" Out Row : ";
1076 std::cout << std::endl <<
" Out Col : ";
1077 for (
int i = 0; i < m_cols+1; ++i) {
1080 std::cout << std::endl;
1108 const int* rows,
int number)
1111 #if defined(G_DEBUG_SPARSE_INSERTION)
1112 std::cout <<
"GMatrixSparse::column(";
1113 std::cout << column <<
", values, rows, " << number <<
"):" << std::endl;
1114 std::cout <<
" Matrix Data : ";
1116 std::cout <<
m_data[i] <<
" ";
1118 std::cout << std::endl <<
" Matrix Row .: ";
1122 std::cout << std::endl <<
" Matrix Col .: ";
1123 for (
int i = 0; i <
m_cols+1; ++i) {
1126 std::cout << std::endl;
1127 std::cout <<
" Array Data .: ";
1128 for (
int i = 0; i <
number; ++i) {
1129 std::cout << values[i] <<
" ";
1131 std::cout << std::endl <<
" Array Row ..: ";
1132 for (
int i = 0; i <
number; ++i) {
1133 std::cout << rows[i] <<
" ";
1135 std::cout << std::endl;
1139 #if defined(G_RANGE_CHECK)
1140 if (column < 0 || column >= m_cols) {
1148 if (rows[number-1] >=
m_rows) {
1149 std::string msg =
"Row number "+
gammalib::str(rows[number-1])+
" for "
1152 "rows. Please specify only row numbers smaller than "+
1160 #if defined(G_DEBUG_SPARSE_PENDING)
1161 std::cout << G_INSERT_COL2 <<
": pending value " <<
m_fill_val <<
1163 ") became obsolete" << std::endl;
1174 int n_exist = i_stop - i_start;
1178 if (!values || !rows) {
1186 int n_diff = number - n_exist;
1191 #if defined(G_DEBUG_SPARSE_INSERTION)
1192 std::cout <<
" Insert .: " << n_diff <<
" elements at index " << i_start << std::endl;
1195 else if (n_diff < 0) {
1197 #if defined(G_DEBUG_SPARSE_INSERTION)
1198 std::cout <<
" Remove .: " << -n_diff <<
" elements at index " << i_start << std::endl;
1211 for (
int i = column+1; i <=
m_cols; ++i) {
1216 #if defined(G_DEBUG_SPARSE_INSERTION)
1217 std::cout <<
" Out Data: ";
1219 std::cout <<
m_data[i] <<
" ";
1221 std::cout << std::endl <<
" Out Row : ";
1225 std::cout << std::endl <<
" Out Col : ";
1226 for (
int i = 0; i < m_cols+1; ++i) {
1229 std::cout << std::endl;
1273 #if defined(G_DEBUG_SPARSE_ADDITION)
1274 std::cout <<
"GMatrixSparse::add_col(";
1275 std::cout << column <<
", [" << v <<
"]):" << std::endl;
1276 std::cout <<
" In Data : ";
1278 std::cout <<
m_data[i] <<
" ";
1280 std::cout << std::endl <<
" In Row .: ";
1284 std::cout << std::endl <<
" In Col .: ";
1285 for (
int i = 0; i <
m_cols+1; ++i) {
1288 std::cout << std::endl;
1299 if (non_zero == 0) {
1309 #if defined(G_RANGE_CHECK)
1310 if (column < 0 || column >= m_cols) {
1321 "rows. Please specify a vector of size "+
1348 this->
column(column, v_column);
1353 #if defined(G_DEBUG_SPARSE_ADDITION)
1354 std::cout <<
" Out Data: ";
1356 std::cout <<
m_data[i] <<
" ";
1358 std::cout << std::endl <<
" Out Row : ";
1362 std::cout << std::endl <<
" Out Col : ";
1363 for (
int i = 0; i < m_cols+1; ++i) {
1366 std::cout << std::endl;
1394 const int* rows,
int number)
1397 #if defined(G_DEBUG_SPARSE_ADDITION)
1398 std::cout <<
"GMatrixSparse::add_col(";
1399 std::cout << column <<
", values, rows, " << number <<
"):" << std::endl;
1400 std::cout <<
" Matrix Data : ";
1402 std::cout <<
m_data[i] <<
" ";
1404 std::cout << std::endl <<
" Matrix Row .: ";
1408 std::cout << std::endl <<
" Matrix Col .: ";
1409 for (
int i = 0; i <
m_cols+1; ++i) {
1412 std::cout << std::endl;
1413 std::cout <<
" Array Data .: ";
1414 for (
int i = 0; i <
number; ++i) {
1415 std::cout << values[i] <<
" ";
1417 std::cout << std::endl <<
" Array Row ..: ";
1418 for (
int i = 0; i <
number; ++i) {
1419 std::cout << rows[i] <<
" ";
1421 std::cout << std::endl;
1438 if (!values || !rows || (number < 1)) {
1443 #if defined(G_RANGE_CHECK)
1444 if (column < 0 || column >= m_cols) {
1452 if (rows[number-1] >=
m_rows) {
1453 std::string msg =
"Row number "+
gammalib::str(rows[number-1])+
" for "
1456 "rows. Please specify only row numbers smaller than "+
1469 if (i_start < i_stop) {
1475 int wrk_size = number + i_stop - i_start;
1476 double* wrk_double =
new double[wrk_size];
1477 int* wrk_int =
new int[wrk_size];
1482 values, rows, number,
1483 wrk_double, wrk_int, &num_mix);
1486 this->
column(column, wrk_double, wrk_int, num_mix);
1490 delete [] wrk_double;
1496 this->
column(column, values, rows, number);
1500 #if defined(G_DEBUG_SPARSE_ADDITION)
1501 std::cout <<
" Out Data: ";
1503 std::cout <<
m_data[i] <<
" ";
1505 std::cout << std::endl <<
" Out Row : ";
1509 std::cout << std::endl <<
" Out Col : ";
1510 for (
int i = 0; i < m_cols+1; ++i) {
1513 std::cout << std::endl;
1627 double* ptr = matrix.
m_data;
1628 for (
int i = 0; i < matrix.
m_elements; ++i, ++ptr) {
1648 double result = 0.0;
1660 result = double(num) / double(size);
1679 if (
m_data[i] < result) {
1686 if ((result > 0.0) && (m_elements < (
m_rows*
m_cols))) {
1705 if (
m_data[i] > result) {
1712 if ((result < 0.0) && (m_elements < (
m_rows*
m_cols))) {
1757 int matrix_rows = matrix.
m_rows;
1758 int matrix_cols = matrix.
m_cols;
1797 for (
int i = 0; i < matrix.
m_elements; ++i) {
1801 for (
int col = 0; col <= matrix.
m_cols; ++col) {
1833 const bool& compress)
const
1836 #if defined(G_DEBUG_SPARSE_COMPRESSION)
1837 std::cout <<
"GMatrixSparse::cholesky_solver" << std::endl;
1838 std::cout <<
" Input vector .....: " << vector << std::endl;
1845 "rows. Please specify a vector of size "+
1852 std::string msg =
"Matrix not factorised. Please call method "
1853 "GMatrixSparse::cholesky_decompose() before calling "
1860 std::string msg =
"Matrix not factorised. Please call method "
1861 "GMatrixSparse::cholesky_decompose() before calling "
1871 int no_zero = !(compress && (row_compressed || col_compressed));
1891 for (
int i = 0; i < vector.
size(); ++i) {
1896 for (
int col = 0; col <
m_cols; ++col) {
1897 x[col] /= Lx[Lp[col]];
1898 for (
int p = Lp[col]+1; p < Lp[col+1]; p++) {
1899 x[Li[p]] -= Lx[p] * x[col];
1904 for (
int col = m_cols-1; col >= 0; --col) {
1905 for (
int p = Lp[col]+1; p < Lp[col+1]; p++)
1906 x[col] -= Lx[p] * x[Li[p]];
1907 x[col] /= Lx[Lp[col]];
1911 for (
int i = 0; i <
m_cols; ++i) {
1921 int* row_map =
new int[
m_rows];
1922 int* col_map =
new int[
m_cols];
1927 if (row_compressed) {
1940 #if defined(G_DEBUG_SPARSE_COMPRESSION)
1941 std::cout <<
" Row mapping ......:";
1943 std::cout <<
" " << row_map[
row];
1945 std::cout << std::endl;
1952 if (col_compressed) {
1953 for (
int col = 0; col <
m_cols; ++col) {
1961 for (
int col = 0; col <
m_cols; ++col) {
1965 #if defined(G_DEBUG_SPARSE_COMPRESSION)
1966 std::cout <<
" Column mapping ...:";
1967 for (
int col = 0; col <
m_cols; ++col) {
1968 std::cout <<
" " << col_map[col];
1970 std::cout << std::endl;
1979 x[c_row] = vector[
m_rowsel[c_row]];
1985 #if defined(G_DEBUG_SPARSE_COMPRESSION)
1986 std::cout <<
" Compressed vector : " << x << std::endl;
1991 #if defined(G_DEBUG_SPARSE_COMPRESSION)
1992 std::cout <<
" Permutated vector : " << x << std::endl;
1997 for (
int col = 0; col <
m_cols; ++col) {
1998 int c_col = col_map[col];
2000 x[c_col] /= Lx[Lp[col]];
2001 for (
int p = Lp[col]+1; p < Lp[col+1]; p++) {
2002 int c_row = row_map[Li[p]];
2004 x[c_row] -= Lx[p] * x[c_col];
2009 #if defined(G_DEBUG_SPARSE_COMPRESSION)
2010 std::cout <<
" Solve Lx=x .......: " << x << std::endl;
2015 for (
int col = m_cols-1; col >= 0; --col) {
2016 int c_col = col_map[col];
2018 for (
int p = Lp[col]+1; p < Lp[col+1]; p++) {
2019 int c_row = row_map[Li[p]];
2021 x[c_col] -= Lx[p] * x[c_row];
2024 x[c_col] /= Lx[Lp[col]];
2027 #if defined(G_DEBUG_SPARSE_COMPRESSION)
2028 std::cout <<
" Solve L'x=x ......: " << x << std::endl;
2033 #if defined(G_DEBUG_SPARSE_COMPRESSION)
2034 std::cout <<
" Permutated vector : " << x << std::endl;
2041 result[
m_colsel[c_col]] = x[c_col];
2047 #if defined(G_DEBUG_SPARSE_COMPRESSION)
2048 std::cout <<
" Restored vector ..: " << result << std::endl;
2082 for (
int col = 0; col <
m_cols; ++col) {
2131 result.append(
"=== GMatrixSparse ===");
2163 result.append(
" (none)");
2236 #if defined(G_RANGE_CHECK)
2237 if (col < 0 || col >=
m_cols) {
2247 "rows. Please specify a vector of size "+
2256 for (
int i = 0; i < vector.
size(); ++i) {
2257 if (vector[i] != 0.0) {
2297 const int&
number,
const int& col)
2300 #if defined(G_RANGE_CHECK)
2301 if (col < 0 || col >=
m_cols) {
2314 if (!values || !rows || (number < 1)) {
2321 if (rows[number-1] >=
m_rows) {
2322 std::string msg =
"Row number "+
gammalib::str(rows[number-1])+
" for "
2325 "rows. Please specify only row numbers smaller than "+
2338 #if defined(G_DEBUG_SPARSE_STACK_PUSH)
2339 std::cout <<
"GMatrixSparse::stack_push_column(v, i, n, col=" << col <<
")";
2340 std::cout << std::endl;
2341 std::cout <<
" Data to push on stack ...:";
2342 for (
int i = 0; i <
number; ++i) {
2343 std::cout <<
" " << values[i];
2345 std::cout << std::endl;
2346 std::cout <<
" Row indices of data .....:";
2347 for (
int i = 0; i <
number; ++i) {
2348 std::cout <<
" " << rows[i];
2350 std::cout << std::endl;
2384 rows, number, &num_1, &num_2, &num_mix);
2385 int num_request = num_1 + num_2 + num_mix;
2401 values, rows, number,
2420 if (remaining == 0) {
2426 for (
int i = 0; i <
number; ++i) {
2443 #if defined(G_DEBUG_SPARSE_STACK_PUSH)
2444 std::cout <<
" Number of stack entries .: " <<
m_stack_entries << std::endl;
2445 std::cout <<
" Stack entry columns .....:";
2449 std::cout << std::endl;
2450 std::cout <<
" Stack entry starts ......:";
2455 std::cout <<
" Stack data ..............:";
2459 std::cout << std::endl;
2460 std::cout <<
" Stack rows ..............:";
2464 std::cout << std::endl;
2497 #if defined(G_DEBUG_SPARSE_STACK_FLUSH)
2498 std::cout <<
"GMatrixSparse::stack_flush" << std::endl;
2499 std::cout <<
" Number of columns on stack : " <<
m_stack_entries << std::endl;
2501 std::cout <<
" Number of matrix elements .: " <<
m_elements << std::endl;
2502 std::cout <<
" Col.start at end of matrix : " <<
m_colstart[
m_cols] << std::endl;
2507 for (
int col = 0; col <
m_cols; ++col) {
2517 int new_elements = 0;
2518 #if defined(G_DEBUG_SPARSE_STACK_FLUSH)
2519 int num_columns = 0;
2541 #if defined(G_DEBUG_SPARSE_STACK_FLUSH)
2566 &num_1, &num_2, &num_mix);
2569 new_elements += num_2;
2570 #if defined(G_DEBUG_SPARSE_STACK_FLUSH)
2571 num_matrix += num_1;
2573 num_both += num_mix;
2590 #if defined(G_DEBUG_SPARSE_STACK_FLUSH)
2591 std::cout <<
" Valid columns on stack ....: " << num_columns << std::endl;
2592 std::cout <<
" Valid elements on stack ...: " << new_elements << std::endl;
2600 double* new_data =
new double[
m_alloc];
2601 int* new_rowinx =
new int[
m_alloc];
2606 for (
int col = 0; col <
m_cols; ++col) {
2620 new_data[index] =
m_data[i];
2623 #if defined(G_DEBUG_SPARSE_STACK_FLUSH)
2637 #if defined(G_DEBUG_SPARSE_STACK_FLUSH)
2662 &(new_data[index]), &(new_rowinx[index]), &num);
2677 #if defined(G_DEBUG_SPARSE_STACK_FLUSH)
2678 std::cout <<
" Added elements ............: " << index;
2679 std::cout <<
" (should be " << elements <<
")" << std::endl;
2680 std::cout <<
" - Matrix only .............: " << num_matrix << std::endl;
2681 std::cout <<
" - Stack only ..............: " << num_stack << std::endl;
2682 std::cout <<
" - Matrix & Stack ..........: " << num_both << std::endl;
2686 for (
int col = m_cols; col > 0; --col) {
2701 m_stack_entries = 0;
2776 for (
int i = 0; i < matrix.
m_elements; ++i) {
2834 const int& elements)
2837 if (rows > 0 && columns > 0) {
2852 for (
int col = 0; col <=
m_cols; ++col) {
2945 #if defined(G_RANGE_CHECK)
2946 if (row < 0 || row >=
m_rows) {
2949 if (column < 0 || column >=
m_cols) {
2969 int i_start = *ptr_colstart++;
2970 int i_stop = *ptr_colstart;
2973 if (i_stop > i_start) {
2976 while ((i_stop - i_start) > 1) {
2977 int mid = (i_start+i_stop) / 2;
3017 #if defined(G_DEBUG_SPARSE_PENDING) || defined(G_DEBUG_SPARSE_INSERTION)
3018 std::cout <<
"GMatrixSparse::fill_pending(): pending value " <<
3019 m_fill_val <<
" will be filled in location (" <<
3032 int i_start = *ptr_colstart++;
3033 int i_stop = *ptr_colstart;
3034 int* ptr_rowinx =
m_rowinx + i_start;
3035 for (inx_insert = i_start; inx_insert < i_stop; ++inx_insert) {
3036 int row_test = *ptr_rowinx++;
3061 #if defined(G_DEBUG_SPARSE_PENDING) || defined(G_DEBUG_SPARSE_INSERTION)
3062 std::cout <<
" Data: ";
3064 std::cout <<
m_data[i] <<
" ";
3066 std::cout << std::endl <<
" Row.: ";
3070 std::cout << std::endl <<
" Col.: ";
3071 for (
int i = 0; i <
m_cols+1; ++i) {
3074 std::cout << std::endl;
3100 #if defined(G_DEBUG_SPARSE_MALLOC)
3101 std::cout <<
"GMatrixSparse::alloc_elements(start=" << start <<
", num=" <<
3102 num <<
")" << std::endl;
3103 std::cout <<
" Before allocation : " <<
m_elements <<
" " <<
m_alloc << std::endl;
3122 for (
int i =
m_elements - 1; i >= start; --i) {
3128 for (
int i = start; i < start+num; ++i) {
3144 m_alloc = (new_size > new_propose) ? new_size : new_propose;
3147 double* new_data =
new double[
m_alloc];
3148 int* new_rowinx =
new int[
m_alloc];
3151 for (
int i = 0; i < start; ++i) {
3157 for (
int i = start; i < start+num; ++i) {
3164 new_data[i+num] =
m_data[i];
3182 #if defined(G_DEBUG_SPARSE_MALLOC)
3183 std::cout <<
" After allocation .: " <<
m_elements <<
" " <<
m_alloc << std::endl;
3205 #if defined(G_DEBUG_SPARSE_MALLOC)
3206 std::cout <<
"GMatrixSparse::free_elements(start=" << start <<
", num=" <<
3207 num <<
")" << std::endl;
3238 double* new_data =
new double[
m_alloc];
3239 int* new_rowinx =
new int[
m_alloc];
3242 for (
int i = 0; i < start; ++i) {
3248 for (
int i = start; i < new_size; ++i) {
3249 new_data[i] =
m_data[i+num];
3268 for (
int i = start; i < new_size; ++i) {
3301 #if defined(G_DEBUG_SPARSE_COMPRESSION)
3302 std::cout <<
"GMatrixSparse::remove_zero_row_col" << std::endl;
3318 std::string msg =
"All matrix elements are zero.";
3323 int* row_map =
new int[
m_rows];
3342 #if defined(G_DEBUG_SPARSE_COMPRESSION)
3344 for (
int col = 0; col <=
m_cols; ++col)
3345 std::cout <<
" " << m_colstart[col];
3346 std::cout << std::endl;
3353 int i_start = m_colstart[
m_colsel[c_col]];
3354 int i_stop = m_colstart[m_colsel[c_col]+1];
3360 for (
int i = i_start; i < i_stop; ++i) {
3364 *d_rowinx++ = c_row;
3370 m_colstart[c_col+1] = m_colstart[c_col] + num;
3375 #if defined(G_DEBUG_SPARSE_COMPRESSION)
3376 std::cout <<
" after compression (" << m_colstart[
m_num_colsel] <<
") :";
3378 std::cout <<
" " << m_colstart[c_col];
3379 std::cout << std::endl;
3407 #if defined(G_DEBUG_SPARSE_COMPRESSION)
3408 std::cout <<
"GMatrixSparse::insert_zero_row_col(" << rows <<
"," << cols <<
3428 #if defined(G_DEBUG_SPARSE_COMPRESSION)
3431 std::cout <<
" " << m_colstart[c_col];
3432 std::cout << std::endl;
3439 int col_stop = cols - 1;
3443 int col_start =
m_colsel[c_col-1] + 1;
3444 int col_value = m_colstart[c_col];
3445 for (
int col = col_start; col <= col_stop; ++col) {
3446 m_colstart[col] = col_value;
3448 col_stop = col_start - 1;
3452 for (
int col = 0; col <= col_stop; ++col) {
3453 m_colstart[col] = 0;
3461 #if defined(G_DEBUG_SPARSE_COMPRESSION)
3462 std::cout <<
" after restoration (" << m_colstart[cols] <<
") :";
3463 for (
int col = 0; col <= cols; ++col) {
3464 std::cout <<
" " << m_colstart[col];
3466 std::cout << std::endl;
3497 const int* src2_row,
int src2_num,
3498 int* num_1,
int* num_2,
int* num_mix)
3508 int row_1 = src1_row[inx_1];
3509 int row_2 = src2_row[inx_2];
3514 while (inx_1 < src1_num && inx_2 < src2_num) {
3517 if (row_1 == row_2) {
3521 if (inx_1 < src1_num) {
3522 row_1 = src1_row[inx_1];
3524 if (inx_2 < src2_num) {
3525 row_2 = src2_row[inx_2];
3530 else if (row_1 < row_2) {
3533 if (inx_1 < src1_num) {
3534 row_1 = src1_row[inx_1];
3542 if (inx_2 < src2_num) {
3543 row_2 = src2_row[inx_2];
3551 if (inx_1 < src1_num) {
3552 *num_1 += (src1_num - inx_1);
3557 if (inx_2 < src2_num) {
3558 *num_2 += (src2_num - inx_2);
3582 const int* src1_row,
3584 const double* src2_data,
3585 const int* src2_row,
3595 int row_1 = src1_row[inx_1];
3596 int row_2 = src2_row[inx_2];
3599 while (inx_1 < src1_num && inx_2 < src2_num) {
3602 if (row_1 == row_2) {
3603 dst_data[inx] = src1_data[inx_1++] + src2_data[inx_2++];
3604 dst_row[inx] = row_1;
3605 if (inx_1 < src1_num) {
3606 row_1 = src1_row[inx_1];
3608 if (inx_2 < src2_num) {
3609 row_2 = src2_row[inx_2];
3615 else if (row_1 < row_2) {
3616 dst_data[inx] = src1_data[inx_1++];
3617 dst_row[inx] = row_1;
3618 if (inx_1 < src1_num) {
3619 row_1 = src1_row[inx_1];
3626 dst_data[inx] = src2_data[inx_2++];
3627 dst_row[inx] = row_2;
3628 if (inx_2 < src2_num) {
3629 row_2 = src2_row[inx_2];
3641 for (
int i = inx_1; i < src1_num; ++i, ++inx) {
3642 dst_data[inx] = src1_data[i];
3643 dst_row[inx] = src1_row[i];
3648 for (
int i = inx_2; i < src2_num; ++i, ++inx) {
3649 dst_data[inx] = src2_data[i];
3650 dst_row[inx] = src2_row[i];
3680 #if defined(G_RANGE_CHECK)
3681 if (row < 0 || row >=
m_rows) {
3690 "columns. Please specify a vector of size "+
3700 std::vector<int> new_columns;
3701 std::vector<int> k_insert;
3702 for (
int i =
m_cols-1; i >= 0; --i) {
3703 if (vector[i] != 0.0) {
3706 bool add_column =
true;
3707 for (; k < istop; ++k) {
3723 new_columns.push_back(i);
3724 k_insert.push_back(k);
3731 if (!new_columns.empty()) {
3734 int new_size =
m_elements + new_columns.size();
3737 bool new_memory =
false;
3738 double* new_data =
m_data;
3749 m_alloc = (new_size > new_propose) ? new_size : new_propose;
3752 new_data =
new double[
m_alloc];
3753 new_rowinx =
new int[
m_alloc];
3761 int n_insert = new_columns.size();
3763 for (
int i = 0; i < new_columns.size(); ++i) {
3766 for (
int k = k_insert[i]; k < k_last; ++k) {
3767 new_data[k+n_insert] =
m_data[k];
3768 new_rowinx[k+n_insert] =
m_rowinx[k];
3773 new_data[k_insert[i]+n_insert-1] = vector[new_columns[i]];
3774 new_rowinx[k_insert[i]+n_insert-1] =
row;
3778 k_last = k_insert[i];
3784 n_insert = new_columns.size();
3785 for (
int i =
m_cols; i > 0; --i) {
3786 if (new_columns[i_insert] == i) {
3838 int i, j, p, q, i2, j2;
3844 double* Ax = matrix.
m_data;
3851 int* wrk_int =
new int[wrk_size];
3852 for (i = 0; i < wrk_size; ++i) {
3862 for (j = 0; j < n; j++) {
3865 j2 = pinv ? pinv[j] : j;
3868 for (p = Ap[j]; p < Ap[j+1]; p++) {
3870 if (i > j)
continue;
3871 i2 = pinv ? pinv[i] : i;
3872 wrk_int[
G_MAX(i2, j2)]++;
3880 for (j = 0 ; j < n ; j++) {
3883 j2 = pinv ? pinv[j] : j;
3886 for (p = Ap[j]; p < Ap[j+1]; p++) {
3888 if (i > j)
continue;
3889 i2 = pinv ? pinv[i] : i;
3890 Ci[q = wrk_int[
G_MAX(i2,j2)]++] =
G_MIN(i2,j2);
3891 if (Cx) Cx[q] = Ax[p];
3930 int wrk_size = matrix.
m_rows;
3931 int* wrk_int =
new int[wrk_size];
3932 for (
int i = 0; i < wrk_size; ++i) {
3951 for (
int col = 0; col < matrix.
m_cols; ++col) {
3953 int i = wrk_int[matrix.
m_rowinx[p]]++;
3962 for (
int col = 0; col < matrix.
m_cols; ++col) {
3991 if (!p || !c)
return (-1);
3998 for (
int i = 0; i < n; ++i) {
virtual double & operator()(const int &row, const int &column)
Return reference to matrix element.
virtual GMatrixSparse & operator=(const GMatrixSparse &matrix)
Matrix assignment operator.
virtual double max(void) const
Return maximum matrix element.
GMatrixSparse cs_symperm(const GMatrixSparse &matrix, const int *pinv)
cs_symperm
Abstract matrix base class interface definition.
int get_index(const int &row, const int &column) const
Determines element index for (row,column)
std::string number(const std::string &noun, const int &number)
Convert singular noun into number noun.
std::string print_row_compression(const GChatter &chatter=NORMAL) const
Print row compression scheme if it exists.
void cholesky_symbolic_analysis(int order, const GMatrixSparse &m)
int m_fill_col
Column into which element needs to be filled.
Sparse matrix numeric analysis class definition.
void stack_destroy(void)
Destroy matrix stack.
const int & size(void) const
Return number of matrix elements.
GMatrixSparse cholesky_invert(const bool &compress=true) const
Invert matrix using a Cholesky decomposition.
Sparse matrix class interface definition.
virtual double fill(void) const
Returns fill of matrix.
void remove_zero_row_col(void)
Remove rows and columns with zeros.
GVector abs(const GVector &vector)
Computes absolute of vector elements.
void insert_zero_row_col(const int &rows, const int &cols)
Insert zero rows and columns.
Generic matrix class definition.
GMatrixSparse cs_transpose(const GMatrixSparse &matrix, int values)
int m_rows
Number of rows.
Sparse matrix symbolic analysis class definition.
virtual GMatrixSparse * clone(void) const
Clone matrix.
virtual GMatrixSparse & operator*=(const GMatrixSparse &matrix)
Unary matrix multiplication operator.
virtual GVector column(const int &column) const
Extract column as vector from matrix.
Symmetric matrix class interface definition.
virtual ~GMatrixSparse(void)
Destructor.
GMatrixSparse invert(void) const
Return inverted matrix.
virtual double & at(const int &row, const int &column)
Return reference to matrix element.
int m_num_colsel
Number of selected columns (for compressed decomposition)
virtual GVector column(const int &column) const
Extract column as vector from matrix.
GMatrixSparse(void)
Void matrix constructor.
virtual void clear(void)
Clear matrix.
int m_stack_max_entries
Maximum number of entries in the stack.
int * m_stack_work
Stack flush integer working array [m_cols].
GMatrixSparse transpose(void) const
Return transposed matrix.
virtual void add_to_column(const int &column, const GVector &vector)
Add vector column into matrix.
Sparse matrix symbolic analysis class.
void insert_row(const int &row, const GVector &vector, const bool &add)
Insert row in matrix.
GVector solve(const GVector &vector) const
Solves linear matrix equation.
int * m_stack_rowinx
Stack row indices [m_stack_size].
void stack_flush(void)
Flush matrix stack.
std::string print_elements(const GChatter &chatter=NORMAL, const int &num=15) const
Print all matrix elements.
GVector perm(const GVector &vector, const int *p)
Computes vector permutation.
friend class GSparseNumeric
double * m_data
Matrix data.
const int & rows(void) const
Return number of matrix rows.
int non_zeros(void) const
Returns number of non-zero elements in vector.
int * m_stack_colinx
Column index for each entry [m_stack_entries].
GMatrixSparse abs(void) const
Return absolute of matrix.
int * m_pinv
Inverse row permutation for QR, fill reduce permutation for Cholesky.
virtual GMatrixSparse & operator-=(const GMatrixSparse &matrix)
Unary matrix subtraction operator.
void free_elements(const int &start, const int &num)
Free memory for obsolete matrix elements.
int m_cols
Number of columns.
virtual double sum(void) const
Sum matrix elements.
double * m_stack_data
Stack data [m_stack_size].
double cs_cumsum(int *p, int *c, int n)
cs_cumsum
void free_stack_members(void)
Delete fill-stack class members.
void init_stack_members(void)
Initialise fill stack.
int * m_rowinx
Row-indices of all elements.
int m_alloc
Size of allocated matrix memory.
virtual void add_to_row(const int &row, const GVector &vector)
Add row to matrix elements.
Sparse matrix numeric analysis class.
int m_stack_entries
Number of entries in the stack.
std::string print_col_compression(const GChatter &chatter=NORMAL) const
Print column compression scheme if it exists.
int * m_stack_start
Start in stack for each entry [m_stack_entries+1].
Vector class interface definition.
void copy_members(const GMatrixSparse &m)
Copy class members.
void fill_pending(void)
Fills pending matrix element.
#define G_SPARSE_MATRIX_DEFAULT_STACK_SIZE
virtual GVector row(const int &row) const
Extract row as vector from matrix.
GSparseSymbolic * m_symbolic
Holds GSparseSymbolic object after decomposition.
double m_fill_val
Element to be filled.
Symmetric matrix class definition.
virtual double min(void) const
Return minimum matrix element.
virtual GMatrixSparse operator-(void) const
Negate matrix elements.
int m_elements
Number of elements stored in matrix.
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.
virtual GVector column(const int &column) const
Extract column as vector from matrix.
Sparse matrix class definition.
virtual bool operator==(const GMatrixSparse &matrix) const
Equalty operator.
GVector cholesky_solver(const GVector &vector, const bool &compress=true) const
Cholesky solver.
friend GMatrixSparse cs_transpose(const GMatrixSparse &matrix, int values)
int * m_stack_rows
Stack push integer working array [m_cols].
int m_fill_row
Row into which element needs to be filled.
void stack_init(const int &size=0, const int &entries=0)
Initialises matrix filling stack.
Exception handler interface definition.
virtual GMatrixBase & operator=(const GMatrixBase &matrix)
Assignment operator.
void init_members(void)
Initialise class mambers.
const int & size(void) const
Return size of vector.
GVector iperm(const GVector &vector, const int *p)
Computes vector inverse permutation.
Generic matrix class definition.
virtual std::string print(const GChatter &chatter=NORMAL) const
Print matrix.
virtual GVector operator*(const GVector &vector) const
Vector multiplication.
int m_num_rowsel
Number of selected rows (for compressed decomposition)
void alloc_members(const int &rows, const int &cols, const int &elements=0)
Allocate matrix.
GSparseNumeric * m_numeric
Holds GSparseNumeric object after decomposition.
virtual GMatrixSparse & operator+=(const GMatrixSparse &matrix)
Unary matrix addition operator.
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
friend class GSparseSymbolic
void cholesky_numeric_analysis(const GMatrixSparse &m, const GSparseSymbolic &s)
int * m_colstart
Column start indices (m_cols+1)
int * m_rowsel
Row selection (for compressed decomposition)
void select_non_zero(void)
Setup compressed matrix factorisation.
#define G_SPARSE_MATRIX_DEFAULT_MEM_BLOCK
int m_mem_block
Memory block to be allocated at once.
const int & columns(void) const
Return number of matrix columns.
GMatrixSparse cholesky_decompose(const bool &compress=true) const
Return Cholesky decomposition.
int m_stack_size
Maximum number of elements in the stack.
int * m_colsel
Column selection (for compressed decomposition)
bool is_empty(void) const
Check if matrix is empty.
virtual bool operator!=(const GMatrixSparse &matrix) const
Non-equality operator.
double m_zero
The zero element (needed for data access)
void alloc_elements(int start, const int &num)
Allocate memory for new matrix elements.
double * m_stack_values
Stack push double buffer [m_cols].
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.
void free_members(void)
Delete class members.
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
int stack_push_column(const GVector &vector, const int &col)
Push a vector column on the matrix stack.