CMR  1.3.0
matrix_internal.h
Go to the documentation of this file.
1 #ifndef CMR_MATRIX_INTERNAL_H
2 #define CMR_MATRIX_INTERNAL_H
3 
4 #include <cmr/matrix.h>
5 
6 #ifdef __cplusplus
7 extern "C" {
8 #endif
9 
14 typedef struct
15 {
16  size_t numRows;
17  size_t numColumns;
18  size_t numNonzeros;
19  size_t * rowSlice;
20  size_t* entryColumns;
21  void* entryValues;
22 } CMR_MATRIX;
23 
29  CMR* cmr,
30  CMR_SUBMAT* submatrix
31 );
32 
39  CMR* cmr,
40  CMR_CHRMAT* matrix,
41  size_t numRows,
42  size_t* rows,
43  size_t numColumns,
44  size_t* columns,
45  CMR_CHRMAT** presult
46 );
47 
48 #ifdef __cplusplus
49 }
50 #endif
51 
52 
53 #ifdef __cplusplus
54 
55 #include "matrix_transposed.hpp"
56 
57 namespace tu
58 {
59 
64  class matrix_binary_pivot_exception: public std::exception
65  {
66  public:
67  const char* what() const throw ()
68  {
69  return "Cannot pivot on a zero entry!";
70  }
71  };
72 
82  template <typename MatrixType>
83  inline void matrix_set_value(MatrixType& matrix, size_t row, size_t column, typename MatrixType::value_type value)
84  {
85  matrix(row, column) = value;
86  }
87 
92  template <typename MatrixType>
93  inline void matrix_set_value(const MatrixType& matrix, size_t row, size_t column, typename MatrixType::value_type value)
94  {
95  CMR_UNUSED(matrix);
96  CMR_UNUSED(row);
97  CMR_UNUSED(column);
98  CMR_UNUSED(value);
99  assert (false);
100  }
101 
102 
111  template <typename MatrixType>
112  void matrix_binary_pivot(MatrixType& matrix, size_t i, size_t j)
113  {
114  typedef typename MatrixType::value_type value_type;
115  const value_type& base_value = matrix(i, j);
116 
117  if (base_value == 0)
118  {
119  throw matrix_binary_pivot_exception();
120  }
121 
122  for (size_t row = 0; row < matrix.size1(); ++row)
123  {
124  if (row == i)
125  {
126  continue;
127  }
128  const value_type& first = matrix(row, j);
129  if (first == 0)
130  {
131  continue;
132  }
133 
134  for (size_t column = 0; column < matrix.size2(); ++column)
135  {
136  if (column == j)
137  {
138  continue;
139  }
140  const value_type& second = matrix(i, column);
141  if (second == 0)
142  {
143  continue;
144  }
145  matrix(row, column) = 1 - matrix(row, column);
146  }
147  }
148  }
149 
158  template <typename MatrixType>
159  void matrix_ternary_pivot(MatrixType& matrix, size_t i, size_t j)
160  {
161  typedef typename MatrixType::value_type value_type;
162  const value_type& base_value = matrix(i, j);
163 
164  if (base_value == 0)
165  {
166  throw matrix_binary_pivot_exception();
167  }
168 
169  for (size_t row = 0; row < matrix.size1(); ++row)
170  {
171  if (row == i)
172  {
173  continue;
174  }
175  const value_type& first = matrix(row, j);
176  if (first == 0)
177  {
178  continue;
179  }
180 
181  for (size_t column = 0; column < matrix.size2(); ++column)
182  {
183  if (column == j)
184  {
185  continue;
186  }
187  const value_type& second = matrix(i, column);
188  if (second == 0)
189  {
190  continue;
191  }
192  value_type value = matrix(row, column) - first * second / base_value;
193  while (value > 1)
194  value -= 3;
195  while (value < -1)
196  value += 3;
197  matrix(row, column) = value;
198  }
199  }
200  }
201 
214  template <typename MatrixType, typename PropertyCheck>
215  inline size_t matrix_count_property_row_series(const MatrixType& matrix, size_t row_first, size_t row_beyond, size_t column_first,
216  size_t column_beyond, PropertyCheck check)
217  {
218  for (size_t row = row_first; row < row_beyond; ++row)
219  {
220  for (size_t column = column_first; column < column_beyond; ++column)
221  check(matrix(row, column));
222  if (!check())
223  return row - row_first;
224  }
225  return row_beyond - row_first;
226  }
227 
240  template <typename MatrixType, typename PropertyCheck>
241  inline size_t matrix_count_property_column_series(const MatrixType& matrix, size_t row_first, size_t row_beyond, size_t column_first,
242  size_t column_beyond, PropertyCheck check)
243  {
244  matrix_transposed <const MatrixType> transposed(matrix);
245  return matrix_count_property_row_series(transposed, column_first, column_beyond, row_first, row_beyond, check);
246  }
247 
254  template <typename MatrixType>
255  inline void matrix_print(const MatrixType& matrix)
256  {
257  for (size_t row = 0; row < matrix.size1(); ++row)
258  {
259  for (size_t column = 0; column < matrix.size2(); ++column)
260  {
261  std::cout << " " << std::setw(2) << matrix(row, column);
262  }
263  std::cout << "\n";
264  }
265  std::cout << std::flush;
266  }
267 
276  template <typename MatrixType1, typename MatrixType2>
277  inline bool matrix_equals(const MatrixType1& matrix1, const MatrixType2& matrix2)
278  {
279  if (matrix1.size1() != matrix2.size1())
280  return false;
281  if (matrix1.size2() != matrix2.size2())
282  return false;
283 
284  for (size_t row = 0; row < matrix1.size1(); ++row)
285  {
286  for (size_t column = 0; column < matrix1.size2(); ++column)
287  {
288  if (matrix1(row, column) != matrix2(row, column))
289  return false;
290  }
291  }
292  return true;
293  }
294 
295 } /* namespace tu */
296 
297 #endif /* __cplusplus */
298 
299 #endif /* CMR_MATRIX_INTERNAL_H */
#define CMR_UNUSED(x)
Definition: env.h:24
CMR_ERROR
Type for return codes of library functions.
Definition: env.h:32
Functionality for sparse matrices.
CMR_ERROR CMRsortSubmatrix(CMR *cmr, CMR_SUBMAT *submatrix)
Sorts the row and column indices of submatrix.
Definition: matrix.c:250
CMR_ERROR CMRchrmatFilter(CMR *cmr, CMR_CHRMAT *matrix, size_t numRows, size_t *rows, size_t numColumns, size_t *columns, CMR_CHRMAT **presult)
Creates a numRows numColumns submatrix of the char matrix indexed by rows and columns.
Definition: matrix.c:2402
Row-wise representation of sparse char matrix.
Definition: matrix.h:204
Definition: env_internal.h:45
Abstract struct for row-wise representations of sparse matrices.
Definition: matrix_internal.h:15
size_t numColumns
Number of columns.
Definition: matrix_internal.h:17
size_t * entryColumns
Array mapping each entry to its column.
Definition: matrix_internal.h:20
size_t * rowSlice
Array mapping each row to the index of its first entry.
Definition: matrix_internal.h:19
size_t numRows
Number of rows.
Definition: matrix_internal.h:16
void * entryValues
Array mapping each entry to its value.
Definition: matrix_internal.h:21
size_t numNonzeros
Number of and memory allocated for nonzeros.
Definition: matrix_internal.h:18
Row and column indices for a submatrix.
Definition: matrix.h:28