1 | /*
|
---|
2 | * MatrixContent.cpp
|
---|
3 | *
|
---|
4 | * Created on: Nov 14, 2010
|
---|
5 | * Author: heber
|
---|
6 | */
|
---|
7 |
|
---|
8 |
|
---|
9 | // include config.h
|
---|
10 | #ifdef HAVE_CONFIG_H
|
---|
11 | #include <config.h>
|
---|
12 | #endif
|
---|
13 |
|
---|
14 | #include "Helpers/MemDebug.hpp"
|
---|
15 |
|
---|
16 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
|
---|
17 | #include "Exceptions/NotInvertibleException.hpp"
|
---|
18 | #include "Helpers/Assert.hpp"
|
---|
19 | #include "Helpers/defs.hpp"
|
---|
20 | #include "Helpers/fast_functions.hpp"
|
---|
21 | #include "LinearAlgebra/Vector.hpp"
|
---|
22 | #include "LinearAlgebra/VectorContent.hpp"
|
---|
23 | #include "LinearAlgebra/MatrixContent.hpp"
|
---|
24 |
|
---|
25 | #include <gsl/gsl_blas.h>
|
---|
26 | #include <gsl/gsl_eigen.h>
|
---|
27 | #include <gsl/gsl_linalg.h>
|
---|
28 | #include <gsl/gsl_matrix.h>
|
---|
29 | #include <gsl/gsl_multimin.h>
|
---|
30 | #include <gsl/gsl_vector.h>
|
---|
31 | #include <cmath>
|
---|
32 | #include <cassert>
|
---|
33 | #include <iostream>
|
---|
34 | #include <set>
|
---|
35 |
|
---|
36 | using namespace std;
|
---|
37 |
|
---|
38 |
|
---|
39 | /** Constructor for class MatrixContent.
|
---|
40 | * \param rows number of rows
|
---|
41 | * \param columns number of columns
|
---|
42 | */
|
---|
43 | MatrixContent::MatrixContent(size_t _rows, size_t _columns) :
|
---|
44 | rows(_rows),
|
---|
45 | columns(_columns)
|
---|
46 | {
|
---|
47 | content = gsl_matrix_calloc(rows, columns);
|
---|
48 | }
|
---|
49 |
|
---|
50 | /** Constructor of class VectorContent.
|
---|
51 | * We need this MatrixBaseCase for the VectorContentView class.
|
---|
52 | * There no content should be allocated, as it is just a view with an internal
|
---|
53 | * gsl_vector_view. Hence, MatrixBaseCase is just dummy class to give the
|
---|
54 | * constructor a unique signature.
|
---|
55 | * \param MatrixBaseCase
|
---|
56 | */
|
---|
57 | MatrixContent::MatrixContent(size_t _rows, size_t _columns, MatrixBaseCase) :
|
---|
58 | rows(_rows),
|
---|
59 | columns(_columns)
|
---|
60 | {}
|
---|
61 |
|
---|
62 | /** Constructor for class MatrixContent.
|
---|
63 | * \param rows number of rows
|
---|
64 | * \param columns number of columns
|
---|
65 | * \param *src array with components to initialize matrix with
|
---|
66 | */
|
---|
67 | MatrixContent::MatrixContent(size_t _rows, size_t _columns, const double *src) :
|
---|
68 | rows(_rows),
|
---|
69 | columns(_columns)
|
---|
70 | {
|
---|
71 | content = gsl_matrix_calloc(rows, columns);
|
---|
72 | set(0,0, src[0]);
|
---|
73 | set(1,0, src[1]);
|
---|
74 | set(2,0, src[2]);
|
---|
75 |
|
---|
76 | set(0,1, src[3]);
|
---|
77 | set(1,1, src[4]);
|
---|
78 | set(2,1, src[5]);
|
---|
79 |
|
---|
80 | set(0,2, src[6]);
|
---|
81 | set(1,2, src[7]);
|
---|
82 | set(2,2, src[8]);
|
---|
83 | }
|
---|
84 |
|
---|
85 | /** Constructor for class MatrixContent.
|
---|
86 | * We embed the given gls_matrix pointer within this class and set it to NULL
|
---|
87 | * afterwards.
|
---|
88 | * \param *src source gsl_matrix vector to embed within this class
|
---|
89 | */
|
---|
90 | MatrixContent::MatrixContent(gsl_matrix *&src) :
|
---|
91 | rows(src->size1),
|
---|
92 | columns(src->size2)
|
---|
93 | {
|
---|
94 | content = gsl_matrix_alloc(src->size1, src->size2);
|
---|
95 | gsl_matrix_memcpy(content,src);
|
---|
96 | // content = src;
|
---|
97 | // src = NULL;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /** Copy constructor for class MatrixContent.
|
---|
101 | * \param &src reference to source MatrixContent
|
---|
102 | */
|
---|
103 | MatrixContent::MatrixContent(const MatrixContent &src) :
|
---|
104 | rows(src.rows),
|
---|
105 | columns(src.columns)
|
---|
106 | {
|
---|
107 | content = gsl_matrix_alloc(src.rows, src.columns);
|
---|
108 | gsl_matrix_memcpy(content,src.content);
|
---|
109 | }
|
---|
110 |
|
---|
111 | /** Copy constructor for class MatrixContent.
|
---|
112 | * \param *src pointer to source MatrixContent
|
---|
113 | */
|
---|
114 | MatrixContent::MatrixContent(const MatrixContent *src) :
|
---|
115 | rows(src->rows),
|
---|
116 | columns(src->columns)
|
---|
117 | {
|
---|
118 | ASSERT(src != NULL, "MatrixContent::MatrixContent - pointer to source matrix is NULL!");
|
---|
119 | content = gsl_matrix_alloc(src->rows, src->columns);
|
---|
120 | gsl_matrix_memcpy(content,src->content);
|
---|
121 | }
|
---|
122 |
|
---|
123 | /** Destructor for class MatrixContent.
|
---|
124 | */
|
---|
125 | MatrixContent::~MatrixContent()
|
---|
126 | {
|
---|
127 | gsl_matrix_free(content);
|
---|
128 | }
|
---|
129 |
|
---|
130 | /** Set matrix to identity.
|
---|
131 | */
|
---|
132 | void MatrixContent::setIdentity()
|
---|
133 | {
|
---|
134 | for(int i=rows;i--;){
|
---|
135 | for(int j=columns;j--;){
|
---|
136 | set(i,j,i==j);
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | /** Set all matrix components to zero.
|
---|
142 | */
|
---|
143 | void MatrixContent::setZero()
|
---|
144 | {
|
---|
145 | for(int i=rows;i--;){
|
---|
146 | for(int j=columns;j--;){
|
---|
147 | set(i,j,0.);
|
---|
148 | }
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | /** Set all matrix components to a given value.
|
---|
153 | * \param _value value to set each component to
|
---|
154 | */
|
---|
155 | void MatrixContent::setValue(double _value)
|
---|
156 | {
|
---|
157 | for(int i=rows;i--;){
|
---|
158 | for(int j=columns;j--;){
|
---|
159 | set(i,j,_value);
|
---|
160 | }
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | /** Copy operator for MatrixContent with self-assignment check.
|
---|
165 | * \param &src matrix to compare to
|
---|
166 | * \return reference to this
|
---|
167 | */
|
---|
168 | MatrixContent &MatrixContent::operator=(const MatrixContent &src)
|
---|
169 | {
|
---|
170 | if(&src!=this){
|
---|
171 | gsl_matrix_memcpy(content,src.content);
|
---|
172 | }
|
---|
173 | return *this;
|
---|
174 | }
|
---|
175 |
|
---|
176 | /** Addition operator.
|
---|
177 | * \param &rhs matrix to add
|
---|
178 | * \return reference to this
|
---|
179 | */
|
---|
180 | const MatrixContent &MatrixContent::operator+=(const MatrixContent &rhs)
|
---|
181 | {
|
---|
182 | gsl_matrix_add(content, rhs.content);
|
---|
183 | return *this;
|
---|
184 | }
|
---|
185 |
|
---|
186 | /** Subtraction operator.
|
---|
187 | * \param &rhs matrix to subtract
|
---|
188 | * \return reference to this
|
---|
189 | */
|
---|
190 | const MatrixContent &MatrixContent::operator-=(const MatrixContent &rhs)
|
---|
191 | {
|
---|
192 | gsl_matrix_sub(content, rhs.content);
|
---|
193 | return *this;
|
---|
194 | }
|
---|
195 |
|
---|
196 | /** Multiplication operator.
|
---|
197 | * Note that here matrix have to have same dimensions.
|
---|
198 | * \param &rhs matrix to multiply with
|
---|
199 | * \return reference to this
|
---|
200 | */
|
---|
201 | const MatrixContent &MatrixContent::operator*=(const MatrixContent &rhs)
|
---|
202 | {
|
---|
203 | ASSERT(rows == rhs.rows,
|
---|
204 | "MatrixContent::operator*=() - row dimension differ: "+toString(rows)+" != "+toString(rhs.rows)+".");
|
---|
205 | ASSERT(columns == rhs.columns,
|
---|
206 | "MatrixContent::operator*=() - columns dimension differ: "+toString(columns)+" != "+toString(rhs.columns)+".");
|
---|
207 | (*this) = (*this)*rhs;
|
---|
208 | return *this;
|
---|
209 | }
|
---|
210 |
|
---|
211 | /** Multiplication with copy operator.
|
---|
212 | * \param &rhs matrix to multiply with
|
---|
213 | * \return reference to newly allocated MatrixContent
|
---|
214 | */
|
---|
215 | const MatrixContent MatrixContent::operator*(const MatrixContent &rhs) const
|
---|
216 | {
|
---|
217 | ASSERT (columns == rhs.rows,
|
---|
218 | "MatrixContent::operator*() - dimensions not match for matrix product (a,b)*(b,c) = (a,c):"
|
---|
219 | "("+toString(rows)+","+toString(columns)+")*("+toString(rhs.rows)+","+toString(rhs.columns)+")");
|
---|
220 | gsl_matrix *res = gsl_matrix_alloc(rows, rhs.columns);
|
---|
221 | gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, content, rhs.content, 0.0, res);
|
---|
222 | // gsl_matrix is taken over by constructor, hence no free
|
---|
223 | MatrixContent tmp(res);
|
---|
224 | gsl_matrix_free(res);
|
---|
225 | return tmp;
|
---|
226 | }
|
---|
227 |
|
---|
228 | /* ========================== Accessing =============================== */
|
---|
229 |
|
---|
230 | /** Accessor for manipulating component (i,j).
|
---|
231 | * \param i row number
|
---|
232 | * \param j column number
|
---|
233 | * \return reference to component (i,j)
|
---|
234 | */
|
---|
235 | double &MatrixContent::at(size_t i, size_t j)
|
---|
236 | {
|
---|
237 | ASSERT((i>=0) && (i<rows),
|
---|
238 | "MatrixContent::at() - Index i="+toString(i)+" for Matrix access out of range [0,"+toString(rows)+"]");
|
---|
239 | ASSERT((j>=0) && (j<columns),
|
---|
240 | "MatrixContent::at() - Index j="+toString(j)+" for Matrix access out of range [0,"+toString(columns)+"]");
|
---|
241 | return *gsl_matrix_ptr (content, i, j);
|
---|
242 | }
|
---|
243 |
|
---|
244 | /** Constant accessor for (value of) component (i,j).
|
---|
245 | * \param i row number
|
---|
246 | * \param j column number
|
---|
247 | * \return const component (i,j)
|
---|
248 | */
|
---|
249 | const double MatrixContent::at(size_t i, size_t j) const
|
---|
250 | {
|
---|
251 | ASSERT((i>=0) && (i<rows),
|
---|
252 | "MatrixContent::at() - Index i="+toString(i)+" for Matrix access out of range [0,"+toString(rows)+"]");
|
---|
253 | ASSERT((j>=0) && (j<columns),
|
---|
254 | "MatrixContent::at() - Index j="+toString(j)+" for Matrix access out of range [0,"+toString(columns)+"]");
|
---|
255 | return gsl_matrix_get(content, i, j);
|
---|
256 | }
|
---|
257 |
|
---|
258 | /** These functions return a pointer to the \a m-th element of a matrix.
|
---|
259 | * If \a m or \a n lies outside the allowed range of 0 to MatrixContent::dimension-1 then the error handler is invoked and a null pointer is returned.
|
---|
260 | * \param m index
|
---|
261 | * \return pointer to \a m-th element
|
---|
262 | */
|
---|
263 | double *MatrixContent::Pointer(size_t m, size_t n)
|
---|
264 | {
|
---|
265 | return gsl_matrix_ptr (content, m, n);
|
---|
266 | };
|
---|
267 |
|
---|
268 | /** These functions return a constant pointer to the \a m-th element of a matrix.
|
---|
269 | * If \a m or \a n lies outside the allowed range of 0 to MatrixContent::dimension-1 then the error handler is invoked and a null pointer is returned.
|
---|
270 | * \param m index
|
---|
271 | * \return const pointer to \a m-th element
|
---|
272 | */
|
---|
273 | const double *MatrixContent::const_Pointer(size_t m, size_t n) const
|
---|
274 | {
|
---|
275 | return gsl_matrix_const_ptr (content, m, n);
|
---|
276 | };
|
---|
277 |
|
---|
278 | /* ========================== Initializing =============================== */
|
---|
279 |
|
---|
280 | /** Setter for component (i,j).
|
---|
281 | * \param i row numbr
|
---|
282 | * \param j column numnber
|
---|
283 | * \param value value to set componnt (i,j) to
|
---|
284 | */
|
---|
285 | void MatrixContent::set(size_t i, size_t j, const double value)
|
---|
286 | {
|
---|
287 | ASSERT((i>=0) && (i<rows),
|
---|
288 | "MatrixContent::set() - Index i="+toString(i)+" for Matrix access out of range [0,"+toString(rows)+"]");
|
---|
289 | ASSERT((j>=0) && (j<columns),
|
---|
290 | "MatrixContent::set() - Index j="+toString(j)+" for Matrix access out of range [0,"+toString(columns)+"]");
|
---|
291 | gsl_matrix_set(content,i,j,value);
|
---|
292 | }
|
---|
293 |
|
---|
294 | /** This function sets the matrix from a double array.
|
---|
295 | * Creates a matrix view of the array and performs a memcopy.
|
---|
296 | * \param *x array of values (no dimension check is performed)
|
---|
297 | */
|
---|
298 | void MatrixContent::setFromDoubleArray(double * x)
|
---|
299 | {
|
---|
300 | gsl_matrix_view m = gsl_matrix_view_array (x, rows, columns);
|
---|
301 | gsl_matrix_memcpy (content, &m.matrix);
|
---|
302 | };
|
---|
303 |
|
---|
304 | /* ====================== Exchanging elements ============================ */
|
---|
305 | /** This function exchanges the \a i-th and \a j-th row of the matrix in-place.
|
---|
306 | * \param i i-th row to swap with ...
|
---|
307 | * \param j ... j-th row to swap against
|
---|
308 | */
|
---|
309 | bool MatrixContent::SwapRows(size_t i, size_t j)
|
---|
310 | {
|
---|
311 | return (gsl_matrix_swap_rows (content, i, j) == GSL_SUCCESS);
|
---|
312 | };
|
---|
313 |
|
---|
314 | /** This function exchanges the \a i-th and \a j-th column of the matrix in-place.
|
---|
315 | * \param i i-th column to swap with ...
|
---|
316 | * \param j ... j-th column to swap against
|
---|
317 | */
|
---|
318 | bool MatrixContent::SwapColumns(size_t i, size_t j)
|
---|
319 | {
|
---|
320 | return (gsl_matrix_swap_columns (content, i, j) == GSL_SUCCESS);
|
---|
321 | };
|
---|
322 |
|
---|
323 | /** This function exchanges the \a i-th row and \a j-th column of the matrix in-place.
|
---|
324 | * The matrix must be square for this operation to be possible.
|
---|
325 | * \param i i-th row to swap with ...
|
---|
326 | * \param j ... j-th column to swap against
|
---|
327 | */
|
---|
328 | bool MatrixContent::SwapRowColumn(size_t i, size_t j)
|
---|
329 | {
|
---|
330 | assert (rows == columns && "The matrix must be square for swapping row against column to be possible.");
|
---|
331 | return (gsl_matrix_swap_rowcol (content, i, j) == GSL_SUCCESS);
|
---|
332 | };
|
---|
333 |
|
---|
334 | /** Return transposed matrix.
|
---|
335 | * \return new matrix that is transposed of this.
|
---|
336 | */
|
---|
337 | MatrixContent MatrixContent::transpose() const
|
---|
338 | {
|
---|
339 | gsl_matrix *res = gsl_matrix_alloc(columns, rows); // column and row dimensions exchanged!
|
---|
340 | gsl_matrix_transpose_memcpy(res, content);
|
---|
341 | MatrixContent newContent(res);
|
---|
342 | gsl_matrix_free(res);
|
---|
343 | return newContent;
|
---|
344 | }
|
---|
345 |
|
---|
346 | /** Turn this matrix into its transposed.
|
---|
347 | * Note that this is only possible if rows == columns.
|
---|
348 | */
|
---|
349 | MatrixContent &MatrixContent::transpose()
|
---|
350 | {
|
---|
351 | ASSERT( rows == columns,
|
---|
352 | "MatrixContent::transpose() - cannot transpose onto itself as matrix not square: "+toString(rows)+"!="+toString(columns)+"!");
|
---|
353 | double tmp;
|
---|
354 | for (size_t i=0;i<rows;i++)
|
---|
355 | for (size_t j=i+1;j<rows;j++) {
|
---|
356 | tmp = at(j,i);
|
---|
357 | at(j,i) = at(i,j);
|
---|
358 | at(i,j) = tmp;
|
---|
359 | }
|
---|
360 | return *this;
|
---|
361 | }
|
---|
362 |
|
---|
363 | /** Transform the matrix to its eigenbasis and return resulting eigenvalues.
|
---|
364 | * Note that we only return real-space part in case of non-symmetric matrix.
|
---|
365 | * \warn return vector has to be freed'd
|
---|
366 | * TODO: encapsulate return value in boost::shared_ptr or in VectorContent.
|
---|
367 | * \return gsl_vector pointer to vector of eigenvalues
|
---|
368 | */
|
---|
369 | gsl_vector* MatrixContent::transformToEigenbasis()
|
---|
370 | {
|
---|
371 | if (rows == columns) { // symmetric
|
---|
372 | gsl_eigen_symmv_workspace *T = gsl_eigen_symmv_alloc(rows);
|
---|
373 | gsl_vector *eval = gsl_vector_alloc(rows);
|
---|
374 | gsl_matrix *evec = gsl_matrix_alloc(rows, rows);
|
---|
375 | gsl_eigen_symmv(content, eval, evec, T);
|
---|
376 | gsl_eigen_symmv_free(T);
|
---|
377 | gsl_matrix_memcpy(content, evec);
|
---|
378 | gsl_matrix_free(evec);
|
---|
379 | return eval;
|
---|
380 | } else { // non-symmetric
|
---|
381 | // blow up gsl_matrix in content to square matrix, fill other components with zero
|
---|
382 | const size_t greaterDimension = rows > columns ? rows : columns;
|
---|
383 | gsl_matrix *content_square = gsl_matrix_alloc(greaterDimension, greaterDimension);
|
---|
384 | for (size_t i=0; i<greaterDimension; i++) {
|
---|
385 | for (size_t j=0; j<greaterDimension; j++) {
|
---|
386 | const double value = ((i < rows) && (j < columns)) ? gsl_matrix_get(content,i,j) : 0.;
|
---|
387 | gsl_matrix_set(content_square, i,j, value);
|
---|
388 | }
|
---|
389 | }
|
---|
390 |
|
---|
391 | // show squared matrix by putting it into a MatrixViewContent
|
---|
392 | MatrixContent *ContentSquare = new MatrixViewContent(gsl_matrix_submatrix(content_square,0,0,content_square->size1, content_square->size2));
|
---|
393 | std::cout << "The squared matrix is " << *ContentSquare << std::endl;
|
---|
394 |
|
---|
395 | // solve eigenvalue problem
|
---|
396 | gsl_eigen_nonsymmv_workspace *T = gsl_eigen_nonsymmv_alloc(rows);
|
---|
397 | gsl_vector_complex *eval = gsl_vector_complex_alloc(greaterDimension);
|
---|
398 | gsl_matrix_complex *evec = gsl_matrix_complex_alloc(greaterDimension, greaterDimension);
|
---|
399 | gsl_eigen_nonsymmv(content_square, eval, evec, T);
|
---|
400 | gsl_eigen_nonsymmv_free(T);
|
---|
401 |
|
---|
402 | // copy eigenvectors real-parts into content_square and ...
|
---|
403 | for (size_t i=0; i<greaterDimension; i++)
|
---|
404 | for (size_t j=0; j<greaterDimension; j++)
|
---|
405 | gsl_matrix_set(content_square, i,j, GSL_REAL(gsl_matrix_complex_get(evec,i,j)));
|
---|
406 |
|
---|
407 | // ... show complex-valued eigenvector matrix
|
---|
408 | std::cout << "The real-value eigenvector matrix is " << *ContentSquare << std::endl;
|
---|
409 | // std::cout << "Resulting eigenvector matrix is [";
|
---|
410 | // for (size_t i=0; i<greaterDimension; i++) {
|
---|
411 | // for (size_t j=0; j<greaterDimension; j++) {
|
---|
412 | // std::cout << "(" << GSL_REAL(gsl_matrix_complex_get(evec,i,j))
|
---|
413 | // << "," << GSL_IMAG(gsl_matrix_complex_get(evec,i,j)) << ")";
|
---|
414 | // if (j < greaterDimension-1)
|
---|
415 | // std::cout << " ";
|
---|
416 | // }
|
---|
417 | // if (i < greaterDimension-1)
|
---|
418 | // std::cout << "; ";
|
---|
419 | // }
|
---|
420 | // std::cout << "]" << std::endl;
|
---|
421 |
|
---|
422 | // copy real-parts of complex eigenvalues and eigenvectors (column-wise orientation)
|
---|
423 | gsl_vector *eval_real = gsl_vector_alloc(columns);
|
---|
424 | size_t I=0;
|
---|
425 | for (size_t i=0; i<greaterDimension; i++) { // only copy real space part
|
---|
426 | if (fabs(GSL_REAL(gsl_vector_complex_get(eval,i))) > MYEPSILON) { // only take eigenvectors with value > 0
|
---|
427 | std::cout << i << "th eigenvalue is (" << GSL_REAL(gsl_vector_complex_get(eval,i)) << "," << GSL_IMAG(gsl_vector_complex_get(eval,i)) << ")" << std::endl;
|
---|
428 | for (size_t j=0; j<greaterDimension; j++) {
|
---|
429 | if (fabs(GSL_IMAG(gsl_matrix_complex_get(evec,j,i))) > MYEPSILON)
|
---|
430 | std::cerr << "MatrixContent::transformToEigenbasis() - WARNING: eigenvectors are complex-valued!" << std::endl;
|
---|
431 | gsl_matrix_set(content, j,I, GSL_REAL(gsl_matrix_complex_get(evec,j,i)));
|
---|
432 | }
|
---|
433 | if (fabs(GSL_IMAG(gsl_vector_complex_get(eval,I))) > MYEPSILON)
|
---|
434 | std::cerr << "MatrixContent::transformToEigenbasis() - WARNING: eigenvectors are complex-valued!" << std::endl;
|
---|
435 | gsl_vector_set(eval_real, I, GSL_REAL(gsl_vector_complex_get(eval, i)));
|
---|
436 | I++;
|
---|
437 | }
|
---|
438 | }
|
---|
439 | gsl_matrix_complex_free(evec);
|
---|
440 | gsl_vector_complex_free(eval);
|
---|
441 | delete ContentSquare;
|
---|
442 |
|
---|
443 | return eval_real;
|
---|
444 | }
|
---|
445 | }
|
---|
446 |
|
---|
447 | /* ============================ Properties ============================== */
|
---|
448 | /** Checks whether matrix' elements are strictly null.
|
---|
449 | * \return true - is null, false - else
|
---|
450 | */
|
---|
451 | bool MatrixContent::IsNull() const
|
---|
452 | {
|
---|
453 | return gsl_matrix_isnull (content);
|
---|
454 | };
|
---|
455 |
|
---|
456 | /** Checks whether matrix' elements are strictly positive.
|
---|
457 | * \return true - is positive, false - else
|
---|
458 | */
|
---|
459 | bool MatrixContent::IsPositive() const
|
---|
460 | {
|
---|
461 | return gsl_matrix_ispos (content);
|
---|
462 | };
|
---|
463 |
|
---|
464 | /** Checks whether matrix' elements are strictly negative.
|
---|
465 | * \return true - is negative, false - else
|
---|
466 | */
|
---|
467 | bool MatrixContent::IsNegative() const
|
---|
468 | {
|
---|
469 | return gsl_matrix_isneg (content);
|
---|
470 | };
|
---|
471 |
|
---|
472 | /** Checks whether matrix' elements are strictly non-negative.
|
---|
473 | * \return true - is non-negative, false - else
|
---|
474 | */
|
---|
475 | bool MatrixContent::IsNonNegative() const
|
---|
476 | {
|
---|
477 | return gsl_matrix_isnonneg (content);
|
---|
478 | };
|
---|
479 |
|
---|
480 | /** This function performs a Cholesky decomposition to determine whether matrix is positive definite.
|
---|
481 | * We check whether GSL returns GSL_EDOM as error, indicating that decomposition failed due to matrix not being positive-definite.
|
---|
482 | * \return true - matrix is positive-definite, false - else
|
---|
483 | */
|
---|
484 | bool MatrixContent::IsPositiveDefinite() const
|
---|
485 | {
|
---|
486 | if (rows != columns) // only possible for square matrices.
|
---|
487 | return false;
|
---|
488 | else
|
---|
489 | return (gsl_linalg_cholesky_decomp (content) != GSL_EDOM);
|
---|
490 | };
|
---|
491 |
|
---|
492 |
|
---|
493 | /** Calculates the determinant of the matrix.
|
---|
494 | * if matrix is square, uses LU decomposition.
|
---|
495 | */
|
---|
496 | double MatrixContent::Determinant() const
|
---|
497 | {
|
---|
498 | int signum = 0;
|
---|
499 | assert (rows == columns && "Determinant can only be calculated for square matrices.");
|
---|
500 | gsl_permutation *p = gsl_permutation_alloc(rows);
|
---|
501 | gsl_linalg_LU_decomp(content, p, &signum);
|
---|
502 | gsl_permutation_free(p);
|
---|
503 | return gsl_linalg_LU_det(content, signum);
|
---|
504 | };
|
---|
505 |
|
---|
506 | /* ============================= Operators =============================== */
|
---|
507 |
|
---|
508 | /** Scalar multiplication operator.
|
---|
509 | * \param factor factor to scale with
|
---|
510 | */
|
---|
511 | const MatrixContent &MatrixContent::operator*=(const double factor)
|
---|
512 | {
|
---|
513 | gsl_matrix_scale(content, factor);
|
---|
514 | return *this;
|
---|
515 | }
|
---|
516 |
|
---|
517 | /** Scalar multiplication and copy operator.
|
---|
518 | * \param factor factor to scale with
|
---|
519 | * \param &mat MatrixContent to scale
|
---|
520 | * \return copied and scaled MatrixContent
|
---|
521 | */
|
---|
522 | const MatrixContent operator*(const double factor,const MatrixContent& mat)
|
---|
523 | {
|
---|
524 | MatrixContent tmp = mat;
|
---|
525 | tmp*=factor;
|
---|
526 | return tmp;
|
---|
527 | }
|
---|
528 |
|
---|
529 | /** Scalar multiplication and copy operator (with operands exchanged).
|
---|
530 | * \param &mat MatrixContent to scale
|
---|
531 | * \param factor factor to scale with
|
---|
532 | * \return copied and scaled MatrixContent
|
---|
533 | */
|
---|
534 | const MatrixContent operator*(const MatrixContent &mat,const double factor)
|
---|
535 | {
|
---|
536 | return factor*mat;
|
---|
537 | }
|
---|
538 |
|
---|
539 | /** Equality operator.
|
---|
540 | * Note that we use numerical sensible checking, i.e. with threshold MYEPSILON.
|
---|
541 | * \param &rhs MatrixContent to checks against
|
---|
542 | */
|
---|
543 | bool MatrixContent::operator==(const MatrixContent &rhs) const
|
---|
544 | {
|
---|
545 | if ((rows == rhs.rows) && (columns == rhs.columns)) {
|
---|
546 | for(int i=rows;i--;){
|
---|
547 | for(int j=columns;j--;){
|
---|
548 | if(fabs(at(i,j)-rhs.at(i,j))>MYEPSILON){
|
---|
549 | return false;
|
---|
550 | }
|
---|
551 | }
|
---|
552 | }
|
---|
553 | return true;
|
---|
554 | }
|
---|
555 | return false;
|
---|
556 | }
|
---|
557 |
|
---|
558 | Vector operator*(const MatrixContent &mat,const Vector &vec)
|
---|
559 | {
|
---|
560 | Vector result;
|
---|
561 | gsl_blas_dgemv( CblasNoTrans, 1.0, mat.content, vec.content->content, 0.0, result.content->content);
|
---|
562 | return result;
|
---|
563 | }
|
---|
564 |
|
---|
565 | std::ostream & operator<<(std::ostream &ost, const MatrixContent &mat)
|
---|
566 | {
|
---|
567 | ost << "[";
|
---|
568 | for (size_t i=0;i<mat.rows;i++) {
|
---|
569 | for (size_t j=0;j<mat.columns;j++) {
|
---|
570 | ost << mat.at(i,j);
|
---|
571 | if (j != mat.columns-1)
|
---|
572 | ost << " ";
|
---|
573 | }
|
---|
574 | if (i != mat.rows-1)
|
---|
575 | ost << "; ";
|
---|
576 | }
|
---|
577 | ost << "]";
|
---|
578 | return ost;
|
---|
579 | }
|
---|