[3bc926] | 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 |
|
---|
[cca9ef] | 16 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
|
---|
[3bc926] | 17 | #include "Exceptions/NotInvertibleException.hpp"
|
---|
| 18 | #include "Helpers/Assert.hpp"
|
---|
[e4fe8d] | 19 | #include "Helpers/defs.hpp"
|
---|
[6d5a10] | 20 | #include "Helpers/fast_functions.hpp"
|
---|
[3bc926] | 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>
|
---|
[0d4424] | 27 | #include <gsl/gsl_linalg.h>
|
---|
[3bc926] | 28 | #include <gsl/gsl_matrix.h>
|
---|
| 29 | #include <gsl/gsl_multimin.h>
|
---|
| 30 | #include <gsl/gsl_vector.h>
|
---|
| 31 | #include <cmath>
|
---|
[b4cf2b] | 32 | #include <cassert>
|
---|
[3bc926] | 33 | #include <iostream>
|
---|
[b4cf2b] | 34 | #include <set>
|
---|
[3bc926] | 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 |
|
---|
[8e9ce1] | 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 |
|
---|
[3bc926] | 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) :
|
---|
[8e9ce1] | 68 | rows(_rows),
|
---|
| 69 | columns(_columns)
|
---|
[3bc926] | 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) :
|
---|
[8e9ce1] | 91 | rows(src->size1),
|
---|
| 92 | columns(src->size2)
|
---|
[3bc926] | 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) :
|
---|
[8e9ce1] | 104 | rows(src.rows),
|
---|
| 105 | columns(src.columns)
|
---|
[3bc926] | 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) :
|
---|
[8e9ce1] | 115 | rows(src->rows),
|
---|
| 116 | columns(src->columns)
|
---|
[3bc926] | 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 |
|
---|
[60dada] | 130 | /** Getter for MatrixContent::rows.
|
---|
| 131 | * \return MatrixContent::rows
|
---|
| 132 | */
|
---|
[35fbef] | 133 | const size_t MatrixContent::getRows() const
|
---|
[60dada] | 134 | {
|
---|
| 135 | return rows;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | /** Getter for MatrixContent::columns.
|
---|
| 139 | * \return MatrixContent::columns
|
---|
| 140 | */
|
---|
[35fbef] | 141 | const size_t MatrixContent::getColumns() const
|
---|
[60dada] | 142 | {
|
---|
| 143 | return columns;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | /** Return a VectorViewContent of the \a column -th column vector.
|
---|
| 147 | *
|
---|
| 148 | * @param column index of column
|
---|
| 149 | * @return column of matrix as VectorContent
|
---|
| 150 | */
|
---|
| 151 | VectorContent *MatrixContent::getColumnVector(size_t column) const
|
---|
| 152 | {
|
---|
| 153 | ASSERT(column < columns,
|
---|
| 154 | "MatrixContent::getColumnVector() - requested column "+toString(column)
|
---|
| 155 | +" greater than dimension "+toString(columns));
|
---|
[aab177] | 156 | return (new VectorViewContent(gsl_matrix_column(content,column)));
|
---|
[60dada] | 157 | }
|
---|
| 158 |
|
---|
| 159 | /** Returns a VectorViewContent of the \a row -th row vector.
|
---|
| 160 | * @param row row index
|
---|
| 161 | * @return VectorContent of row vector
|
---|
| 162 | */
|
---|
| 163 | VectorContent *MatrixContent::getRowVector(size_t row) const
|
---|
| 164 | {
|
---|
| 165 | ASSERT(row < rows,
|
---|
| 166 | "MatrixContent::getColumnVector() - requested row "+toString(row)
|
---|
| 167 | +" greater than dimension "+toString(rows));
|
---|
[aab177] | 168 | return (new VectorViewContent(gsl_matrix_row(content,row)));
|
---|
[60dada] | 169 | }
|
---|
| 170 |
|
---|
| 171 | /** Returns the main diagonal of the matrix as VectorContent.
|
---|
| 172 | * @return diagonal as VectorContent.
|
---|
| 173 | */
|
---|
| 174 | VectorContent *MatrixContent::getDiagonalVector() const
|
---|
| 175 | {
|
---|
| 176 | return (new VectorViewContent(gsl_matrix_diagonal(content)));
|
---|
| 177 | }
|
---|
| 178 |
|
---|
[3bc926] | 179 | /** Set matrix to identity.
|
---|
| 180 | */
|
---|
| 181 | void MatrixContent::setIdentity()
|
---|
| 182 | {
|
---|
| 183 | for(int i=rows;i--;){
|
---|
| 184 | for(int j=columns;j--;){
|
---|
[7d059d] | 185 | set(i,j,(double)(i==j));
|
---|
[3bc926] | 186 | }
|
---|
| 187 | }
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | /** Set all matrix components to zero.
|
---|
| 191 | */
|
---|
| 192 | void MatrixContent::setZero()
|
---|
| 193 | {
|
---|
| 194 | for(int i=rows;i--;){
|
---|
| 195 | for(int j=columns;j--;){
|
---|
| 196 | set(i,j,0.);
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
| 199 | }
|
---|
| 200 |
|
---|
[0d4424] | 201 | /** Set all matrix components to a given value.
|
---|
| 202 | * \param _value value to set each component to
|
---|
| 203 | */
|
---|
| 204 | void MatrixContent::setValue(double _value)
|
---|
| 205 | {
|
---|
| 206 | for(int i=rows;i--;){
|
---|
| 207 | for(int j=columns;j--;){
|
---|
| 208 | set(i,j,_value);
|
---|
| 209 | }
|
---|
| 210 | }
|
---|
| 211 | }
|
---|
| 212 |
|
---|
[3bc926] | 213 | /** Copy operator for MatrixContent with self-assignment check.
|
---|
| 214 | * \param &src matrix to compare to
|
---|
| 215 | * \return reference to this
|
---|
| 216 | */
|
---|
| 217 | MatrixContent &MatrixContent::operator=(const MatrixContent &src)
|
---|
| 218 | {
|
---|
| 219 | if(&src!=this){
|
---|
| 220 | gsl_matrix_memcpy(content,src.content);
|
---|
| 221 | }
|
---|
| 222 | return *this;
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | /** Addition operator.
|
---|
| 226 | * \param &rhs matrix to add
|
---|
| 227 | * \return reference to this
|
---|
| 228 | */
|
---|
| 229 | const MatrixContent &MatrixContent::operator+=(const MatrixContent &rhs)
|
---|
| 230 | {
|
---|
| 231 | gsl_matrix_add(content, rhs.content);
|
---|
| 232 | return *this;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | /** Subtraction operator.
|
---|
| 236 | * \param &rhs matrix to subtract
|
---|
| 237 | * \return reference to this
|
---|
| 238 | */
|
---|
| 239 | const MatrixContent &MatrixContent::operator-=(const MatrixContent &rhs)
|
---|
| 240 | {
|
---|
| 241 | gsl_matrix_sub(content, rhs.content);
|
---|
| 242 | return *this;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | /** Multiplication operator.
|
---|
| 246 | * Note that here matrix have to have same dimensions.
|
---|
| 247 | * \param &rhs matrix to multiply with
|
---|
| 248 | * \return reference to this
|
---|
| 249 | */
|
---|
| 250 | const MatrixContent &MatrixContent::operator*=(const MatrixContent &rhs)
|
---|
| 251 | {
|
---|
[a062e1] | 252 | ASSERT(rhs.columns == rhs.rows,
|
---|
| 253 | "MatrixContent::operator*=() - rhs matrix is not square: "+toString(rhs.columns)+" != "+toString(rhs.rows)+".");
|
---|
| 254 | ASSERT(columns == rhs.rows,
|
---|
[742371] | 255 | "MatrixContent::operator*=() - columns dimension differ: "+toString(columns)+" != "+toString(rhs.rows)+".");
|
---|
[3bc926] | 256 | (*this) = (*this)*rhs;
|
---|
| 257 | return *this;
|
---|
| 258 | }
|
---|
| 259 |
|
---|
| 260 | /** Multiplication with copy operator.
|
---|
| 261 | * \param &rhs matrix to multiply with
|
---|
| 262 | * \return reference to newly allocated MatrixContent
|
---|
| 263 | */
|
---|
| 264 | const MatrixContent MatrixContent::operator*(const MatrixContent &rhs) const
|
---|
| 265 | {
|
---|
[17fa81] | 266 | ASSERT (columns == rhs.rows,
|
---|
| 267 | "MatrixContent::operator*() - dimensions not match for matrix product (a,b)*(b,c) = (a,c):"
|
---|
| 268 | "("+toString(rows)+","+toString(columns)+")*("+toString(rhs.rows)+","+toString(rhs.columns)+")");
|
---|
[b4cf2b] | 269 | gsl_matrix *res = gsl_matrix_alloc(rows, rhs.columns);
|
---|
[3bc926] | 270 | gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, content, rhs.content, 0.0, res);
|
---|
| 271 | // gsl_matrix is taken over by constructor, hence no free
|
---|
| 272 | MatrixContent tmp(res);
|
---|
| 273 | gsl_matrix_free(res);
|
---|
| 274 | return tmp;
|
---|
| 275 | }
|
---|
| 276 |
|
---|
[d85c28] | 277 | /** Hadamard multiplication with copy operator.
|
---|
| 278 | * The Hadamard product is component-wise matrix product.
|
---|
| 279 | * \param &rhs matrix to hadamard-multiply with
|
---|
| 280 | * \return reference to newly allocated MatrixContent
|
---|
| 281 | */
|
---|
| 282 | const MatrixContent MatrixContent::operator&(const MatrixContent &rhs) const
|
---|
| 283 | {
|
---|
| 284 | ASSERT ((rows == rhs.rows) && (columns == rhs.columns),
|
---|
| 285 | "MatrixContent::operator&() - dimensions not match for matrix product (a,b) != (b,c):"
|
---|
| 286 | "("+toString(rows)+","+toString(columns)+") != ("+toString(rhs.rows)+","+toString(rhs.columns)+")");
|
---|
| 287 | gsl_matrix *res = gsl_matrix_alloc(rows, rhs.columns);
|
---|
| 288 | for (size_t i=0;i<rows;++i)
|
---|
| 289 | for (size_t j=0;j<columns;++j)
|
---|
| 290 | gsl_matrix_set(res, i,j, gsl_matrix_get(content, i,j)*gsl_matrix_get(rhs.content, i,j));
|
---|
| 291 | // gsl_matrix is taken over by constructor, hence no free
|
---|
| 292 | MatrixContent tmp(res);
|
---|
| 293 | gsl_matrix_free(res);
|
---|
| 294 | return tmp;
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | /** Hadamard multiplication with copy operator.
|
---|
| 298 | * The Hadamard product is component-wise matrix product.
|
---|
| 299 | * Note that Hadamard product can easily be done on top of \a *this matrix.
|
---|
| 300 | * Hence, we don't need to use the multiply and copy operator as in the case of
|
---|
| 301 | * MatrixContent::operator*=().
|
---|
| 302 | * \param &rhs matrix to hadamard-multiply with
|
---|
| 303 | * \return reference to newly allocated MatrixContent
|
---|
| 304 | */
|
---|
| 305 | const MatrixContent &MatrixContent::operator&=(const MatrixContent &rhs)
|
---|
| 306 | {
|
---|
| 307 | ASSERT ((rows == rhs.rows) && (columns == rhs.columns),
|
---|
| 308 | "MatrixContent::operator&() - dimensions not match for matrix product (a,b) != (b,c):"
|
---|
| 309 | "("+toString(rows)+","+toString(columns)+") != ("+toString(rhs.rows)+","+toString(rhs.columns)+")");
|
---|
| 310 | for (size_t i=0;i<rows;++i)
|
---|
| 311 | for (size_t j=0;j<columns;++j)
|
---|
| 312 | gsl_matrix_set(content, i,j, gsl_matrix_get(content, i,j)*gsl_matrix_get(rhs.content, i,j));
|
---|
| 313 | return *this;
|
---|
| 314 | }
|
---|
| 315 |
|
---|
[0d4424] | 316 | /* ========================== Accessing =============================== */
|
---|
| 317 |
|
---|
[3bc926] | 318 | /** Accessor for manipulating component (i,j).
|
---|
| 319 | * \param i row number
|
---|
| 320 | * \param j column number
|
---|
| 321 | * \return reference to component (i,j)
|
---|
| 322 | */
|
---|
| 323 | double &MatrixContent::at(size_t i, size_t j)
|
---|
| 324 | {
|
---|
| 325 | ASSERT((i>=0) && (i<rows),
|
---|
| 326 | "MatrixContent::at() - Index i="+toString(i)+" for Matrix access out of range [0,"+toString(rows)+"]");
|
---|
| 327 | ASSERT((j>=0) && (j<columns),
|
---|
| 328 | "MatrixContent::at() - Index j="+toString(j)+" for Matrix access out of range [0,"+toString(columns)+"]");
|
---|
| 329 | return *gsl_matrix_ptr (content, i, j);
|
---|
| 330 | }
|
---|
| 331 |
|
---|
| 332 | /** Constant accessor for (value of) component (i,j).
|
---|
| 333 | * \param i row number
|
---|
| 334 | * \param j column number
|
---|
| 335 | * \return const component (i,j)
|
---|
| 336 | */
|
---|
| 337 | const double MatrixContent::at(size_t i, size_t j) const
|
---|
| 338 | {
|
---|
| 339 | ASSERT((i>=0) && (i<rows),
|
---|
| 340 | "MatrixContent::at() - Index i="+toString(i)+" for Matrix access out of range [0,"+toString(rows)+"]");
|
---|
| 341 | ASSERT((j>=0) && (j<columns),
|
---|
| 342 | "MatrixContent::at() - Index j="+toString(j)+" for Matrix access out of range [0,"+toString(columns)+"]");
|
---|
| 343 | return gsl_matrix_get(content, i, j);
|
---|
| 344 | }
|
---|
| 345 |
|
---|
[0d4424] | 346 | /** These functions return a pointer to the \a m-th element of a matrix.
|
---|
[6c1cd1] | 347 | * 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.
|
---|
[0d4424] | 348 | * \param m index
|
---|
| 349 | * \return pointer to \a m-th element
|
---|
| 350 | */
|
---|
| 351 | double *MatrixContent::Pointer(size_t m, size_t n)
|
---|
| 352 | {
|
---|
| 353 | return gsl_matrix_ptr (content, m, n);
|
---|
| 354 | };
|
---|
| 355 |
|
---|
| 356 | /** These functions return a constant pointer to the \a m-th element of a matrix.
|
---|
[6c1cd1] | 357 | * 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.
|
---|
[0d4424] | 358 | * \param m index
|
---|
| 359 | * \return const pointer to \a m-th element
|
---|
| 360 | */
|
---|
| 361 | const double *MatrixContent::const_Pointer(size_t m, size_t n) const
|
---|
| 362 | {
|
---|
| 363 | return gsl_matrix_const_ptr (content, m, n);
|
---|
| 364 | };
|
---|
| 365 |
|
---|
| 366 | /* ========================== Initializing =============================== */
|
---|
| 367 |
|
---|
[3bc926] | 368 | /** Setter for component (i,j).
|
---|
| 369 | * \param i row numbr
|
---|
| 370 | * \param j column numnber
|
---|
| 371 | * \param value value to set componnt (i,j) to
|
---|
| 372 | */
|
---|
| 373 | void MatrixContent::set(size_t i, size_t j, const double value)
|
---|
| 374 | {
|
---|
| 375 | ASSERT((i>=0) && (i<rows),
|
---|
| 376 | "MatrixContent::set() - Index i="+toString(i)+" for Matrix access out of range [0,"+toString(rows)+"]");
|
---|
| 377 | ASSERT((j>=0) && (j<columns),
|
---|
| 378 | "MatrixContent::set() - Index j="+toString(j)+" for Matrix access out of range [0,"+toString(columns)+"]");
|
---|
| 379 | gsl_matrix_set(content,i,j,value);
|
---|
| 380 | }
|
---|
| 381 |
|
---|
[0d4424] | 382 | /** This function sets the matrix from a double array.
|
---|
| 383 | * Creates a matrix view of the array and performs a memcopy.
|
---|
| 384 | * \param *x array of values (no dimension check is performed)
|
---|
| 385 | */
|
---|
| 386 | void MatrixContent::setFromDoubleArray(double * x)
|
---|
| 387 | {
|
---|
| 388 | gsl_matrix_view m = gsl_matrix_view_array (x, rows, columns);
|
---|
| 389 | gsl_matrix_memcpy (content, &m.matrix);
|
---|
| 390 | };
|
---|
| 391 |
|
---|
| 392 | /* ====================== Exchanging elements ============================ */
|
---|
| 393 | /** This function exchanges the \a i-th and \a j-th row of the matrix in-place.
|
---|
| 394 | * \param i i-th row to swap with ...
|
---|
| 395 | * \param j ... j-th row to swap against
|
---|
| 396 | */
|
---|
| 397 | bool MatrixContent::SwapRows(size_t i, size_t j)
|
---|
| 398 | {
|
---|
| 399 | return (gsl_matrix_swap_rows (content, i, j) == GSL_SUCCESS);
|
---|
| 400 | };
|
---|
| 401 |
|
---|
| 402 | /** This function exchanges the \a i-th and \a j-th column of the matrix in-place.
|
---|
| 403 | * \param i i-th column to swap with ...
|
---|
| 404 | * \param j ... j-th column to swap against
|
---|
| 405 | */
|
---|
| 406 | bool MatrixContent::SwapColumns(size_t i, size_t j)
|
---|
| 407 | {
|
---|
| 408 | return (gsl_matrix_swap_columns (content, i, j) == GSL_SUCCESS);
|
---|
| 409 | };
|
---|
| 410 |
|
---|
| 411 | /** This function exchanges the \a i-th row and \a j-th column of the matrix in-place.
|
---|
| 412 | * The matrix must be square for this operation to be possible.
|
---|
| 413 | * \param i i-th row to swap with ...
|
---|
| 414 | * \param j ... j-th column to swap against
|
---|
| 415 | */
|
---|
| 416 | bool MatrixContent::SwapRowColumn(size_t i, size_t j)
|
---|
| 417 | {
|
---|
| 418 | assert (rows == columns && "The matrix must be square for swapping row against column to be possible.");
|
---|
| 419 | return (gsl_matrix_swap_rowcol (content, i, j) == GSL_SUCCESS);
|
---|
| 420 | };
|
---|
| 421 |
|
---|
[3bc926] | 422 | /** Return transposed matrix.
|
---|
| 423 | * \return new matrix that is transposed of this.
|
---|
| 424 | */
|
---|
| 425 | MatrixContent MatrixContent::transpose() const
|
---|
| 426 | {
|
---|
[0d4424] | 427 | gsl_matrix *res = gsl_matrix_alloc(columns, rows); // column and row dimensions exchanged!
|
---|
[3bc926] | 428 | gsl_matrix_transpose_memcpy(res, content);
|
---|
| 429 | MatrixContent newContent(res);
|
---|
| 430 | gsl_matrix_free(res);
|
---|
| 431 | return newContent;
|
---|
| 432 | }
|
---|
| 433 |
|
---|
| 434 | /** Turn this matrix into its transposed.
|
---|
| 435 | * Note that this is only possible if rows == columns.
|
---|
| 436 | */
|
---|
| 437 | MatrixContent &MatrixContent::transpose()
|
---|
| 438 | {
|
---|
| 439 | ASSERT( rows == columns,
|
---|
| 440 | "MatrixContent::transpose() - cannot transpose onto itself as matrix not square: "+toString(rows)+"!="+toString(columns)+"!");
|
---|
| 441 | double tmp;
|
---|
| 442 | for (size_t i=0;i<rows;i++)
|
---|
| 443 | for (size_t j=i+1;j<rows;j++) {
|
---|
| 444 | tmp = at(j,i);
|
---|
| 445 | at(j,i) = at(i,j);
|
---|
| 446 | at(i,j) = tmp;
|
---|
| 447 | }
|
---|
| 448 | return *this;
|
---|
| 449 | }
|
---|
| 450 |
|
---|
[17fa81] | 451 | /** Transform the matrix to its eigenbasis and return resulting eigenvalues.
|
---|
[b4cf2b] | 452 | * Note that we only return real-space part in case of non-symmetric matrix.
|
---|
[3bc926] | 453 | * \warn return vector has to be freed'd
|
---|
[b4cf2b] | 454 | * TODO: encapsulate return value in boost::shared_ptr or in VectorContent.
|
---|
[3bc926] | 455 | * \return gsl_vector pointer to vector of eigenvalues
|
---|
| 456 | */
|
---|
| 457 | gsl_vector* MatrixContent::transformToEigenbasis()
|
---|
| 458 | {
|
---|
[b4cf2b] | 459 | if (rows == columns) { // symmetric
|
---|
| 460 | gsl_eigen_symmv_workspace *T = gsl_eigen_symmv_alloc(rows);
|
---|
| 461 | gsl_vector *eval = gsl_vector_alloc(rows);
|
---|
| 462 | gsl_matrix *evec = gsl_matrix_alloc(rows, rows);
|
---|
| 463 | gsl_eigen_symmv(content, eval, evec, T);
|
---|
| 464 | gsl_eigen_symmv_free(T);
|
---|
| 465 | gsl_matrix_memcpy(content, evec);
|
---|
| 466 | gsl_matrix_free(evec);
|
---|
| 467 | return eval;
|
---|
| 468 | } else { // non-symmetric
|
---|
| 469 | // blow up gsl_matrix in content to square matrix, fill other components with zero
|
---|
| 470 | const size_t greaterDimension = rows > columns ? rows : columns;
|
---|
| 471 | gsl_matrix *content_square = gsl_matrix_alloc(greaterDimension, greaterDimension);
|
---|
| 472 | for (size_t i=0; i<greaterDimension; i++) {
|
---|
| 473 | for (size_t j=0; j<greaterDimension; j++) {
|
---|
| 474 | const double value = ((i < rows) && (j < columns)) ? gsl_matrix_get(content,i,j) : 0.;
|
---|
| 475 | gsl_matrix_set(content_square, i,j, value);
|
---|
| 476 | }
|
---|
| 477 | }
|
---|
| 478 |
|
---|
[17fa81] | 479 | // show squared matrix by putting it into a MatrixViewContent
|
---|
| 480 | MatrixContent *ContentSquare = new MatrixViewContent(gsl_matrix_submatrix(content_square,0,0,content_square->size1, content_square->size2));
|
---|
[8e9ce1] | 481 | std::cout << "The squared matrix is " << *ContentSquare << std::endl;
|
---|
| 482 |
|
---|
[b4cf2b] | 483 | // solve eigenvalue problem
|
---|
| 484 | gsl_eigen_nonsymmv_workspace *T = gsl_eigen_nonsymmv_alloc(rows);
|
---|
| 485 | gsl_vector_complex *eval = gsl_vector_complex_alloc(greaterDimension);
|
---|
| 486 | gsl_matrix_complex *evec = gsl_matrix_complex_alloc(greaterDimension, greaterDimension);
|
---|
| 487 | gsl_eigen_nonsymmv(content_square, eval, evec, T);
|
---|
| 488 | gsl_eigen_nonsymmv_free(T);
|
---|
| 489 |
|
---|
[8e9ce1] | 490 | // copy eigenvectors real-parts into content_square and ...
|
---|
[17fa81] | 491 | for (size_t i=0; i<greaterDimension; i++)
|
---|
| 492 | for (size_t j=0; j<greaterDimension; j++)
|
---|
[b4cf2b] | 493 | gsl_matrix_set(content_square, i,j, GSL_REAL(gsl_matrix_complex_get(evec,i,j)));
|
---|
| 494 |
|
---|
[17fa81] | 495 | // ... show complex-valued eigenvector matrix
|
---|
| 496 | std::cout << "The real-value eigenvector matrix is " << *ContentSquare << std::endl;
|
---|
| 497 | // std::cout << "Resulting eigenvector matrix is [";
|
---|
| 498 | // for (size_t i=0; i<greaterDimension; i++) {
|
---|
| 499 | // for (size_t j=0; j<greaterDimension; j++) {
|
---|
| 500 | // std::cout << "(" << GSL_REAL(gsl_matrix_complex_get(evec,i,j))
|
---|
| 501 | // << "," << GSL_IMAG(gsl_matrix_complex_get(evec,i,j)) << ")";
|
---|
| 502 | // if (j < greaterDimension-1)
|
---|
| 503 | // std::cout << " ";
|
---|
| 504 | // }
|
---|
| 505 | // if (i < greaterDimension-1)
|
---|
| 506 | // std::cout << "; ";
|
---|
| 507 | // }
|
---|
| 508 | // std::cout << "]" << std::endl;
|
---|
| 509 |
|
---|
| 510 | // copy real-parts of complex eigenvalues and eigenvectors (column-wise orientation)
|
---|
| 511 | gsl_vector *eval_real = gsl_vector_alloc(columns);
|
---|
[b4cf2b] | 512 | size_t I=0;
|
---|
| 513 | for (size_t i=0; i<greaterDimension; i++) { // only copy real space part
|
---|
[17fa81] | 514 | if (fabs(GSL_REAL(gsl_vector_complex_get(eval,i))) > MYEPSILON) { // only take eigenvectors with value > 0
|
---|
| 515 | std::cout << i << "th eigenvalue is (" << GSL_REAL(gsl_vector_complex_get(eval,i)) << "," << GSL_IMAG(gsl_vector_complex_get(eval,i)) << ")" << std::endl;
|
---|
[b4cf2b] | 516 | for (size_t j=0; j<greaterDimension; j++) {
|
---|
[17fa81] | 517 | if (fabs(GSL_IMAG(gsl_matrix_complex_get(evec,j,i))) > MYEPSILON)
|
---|
| 518 | std::cerr << "MatrixContent::transformToEigenbasis() - WARNING: eigenvectors are complex-valued!" << std::endl;
|
---|
| 519 | gsl_matrix_set(content, j,I, GSL_REAL(gsl_matrix_complex_get(evec,j,i)));
|
---|
[b4cf2b] | 520 | }
|
---|
| 521 | if (fabs(GSL_IMAG(gsl_vector_complex_get(eval,I))) > MYEPSILON)
|
---|
| 522 | std::cerr << "MatrixContent::transformToEigenbasis() - WARNING: eigenvectors are complex-valued!" << std::endl;
|
---|
| 523 | gsl_vector_set(eval_real, I, GSL_REAL(gsl_vector_complex_get(eval, i)));
|
---|
| 524 | I++;
|
---|
| 525 | }
|
---|
| 526 | }
|
---|
| 527 | gsl_matrix_complex_free(evec);
|
---|
| 528 | gsl_vector_complex_free(eval);
|
---|
[17fa81] | 529 | delete ContentSquare;
|
---|
| 530 |
|
---|
[b4cf2b] | 531 | return eval_real;
|
---|
| 532 | }
|
---|
[3bc926] | 533 | }
|
---|
| 534 |
|
---|
[0d4424] | 535 | /* ============================ Properties ============================== */
|
---|
| 536 | /** Checks whether matrix' elements are strictly null.
|
---|
| 537 | * \return true - is null, false - else
|
---|
| 538 | */
|
---|
| 539 | bool MatrixContent::IsNull() const
|
---|
| 540 | {
|
---|
| 541 | return gsl_matrix_isnull (content);
|
---|
| 542 | };
|
---|
| 543 |
|
---|
| 544 | /** Checks whether matrix' elements are strictly positive.
|
---|
| 545 | * \return true - is positive, false - else
|
---|
| 546 | */
|
---|
| 547 | bool MatrixContent::IsPositive() const
|
---|
| 548 | {
|
---|
| 549 | return gsl_matrix_ispos (content);
|
---|
| 550 | };
|
---|
| 551 |
|
---|
| 552 | /** Checks whether matrix' elements are strictly negative.
|
---|
| 553 | * \return true - is negative, false - else
|
---|
| 554 | */
|
---|
| 555 | bool MatrixContent::IsNegative() const
|
---|
| 556 | {
|
---|
| 557 | return gsl_matrix_isneg (content);
|
---|
| 558 | };
|
---|
| 559 |
|
---|
| 560 | /** Checks whether matrix' elements are strictly non-negative.
|
---|
| 561 | * \return true - is non-negative, false - else
|
---|
| 562 | */
|
---|
| 563 | bool MatrixContent::IsNonNegative() const
|
---|
| 564 | {
|
---|
| 565 | return gsl_matrix_isnonneg (content);
|
---|
| 566 | };
|
---|
| 567 |
|
---|
| 568 | /** This function performs a Cholesky decomposition to determine whether matrix is positive definite.
|
---|
| 569 | * We check whether GSL returns GSL_EDOM as error, indicating that decomposition failed due to matrix not being positive-definite.
|
---|
| 570 | * \return true - matrix is positive-definite, false - else
|
---|
| 571 | */
|
---|
| 572 | bool MatrixContent::IsPositiveDefinite() const
|
---|
| 573 | {
|
---|
| 574 | if (rows != columns) // only possible for square matrices.
|
---|
| 575 | return false;
|
---|
| 576 | else
|
---|
| 577 | return (gsl_linalg_cholesky_decomp (content) != GSL_EDOM);
|
---|
| 578 | };
|
---|
| 579 |
|
---|
| 580 |
|
---|
| 581 | /** Calculates the determinant of the matrix.
|
---|
| 582 | * if matrix is square, uses LU decomposition.
|
---|
| 583 | */
|
---|
| 584 | double MatrixContent::Determinant() const
|
---|
| 585 | {
|
---|
| 586 | int signum = 0;
|
---|
| 587 | assert (rows == columns && "Determinant can only be calculated for square matrices.");
|
---|
| 588 | gsl_permutation *p = gsl_permutation_alloc(rows);
|
---|
| 589 | gsl_linalg_LU_decomp(content, p, &signum);
|
---|
| 590 | gsl_permutation_free(p);
|
---|
| 591 | return gsl_linalg_LU_det(content, signum);
|
---|
| 592 | };
|
---|
| 593 |
|
---|
| 594 | /* ============================= Operators =============================== */
|
---|
| 595 |
|
---|
[3bc926] | 596 | /** Scalar multiplication operator.
|
---|
| 597 | * \param factor factor to scale with
|
---|
| 598 | */
|
---|
| 599 | const MatrixContent &MatrixContent::operator*=(const double factor)
|
---|
| 600 | {
|
---|
| 601 | gsl_matrix_scale(content, factor);
|
---|
| 602 | return *this;
|
---|
| 603 | }
|
---|
| 604 |
|
---|
| 605 | /** Scalar multiplication and copy operator.
|
---|
| 606 | * \param factor factor to scale with
|
---|
| 607 | * \param &mat MatrixContent to scale
|
---|
| 608 | * \return copied and scaled MatrixContent
|
---|
| 609 | */
|
---|
| 610 | const MatrixContent operator*(const double factor,const MatrixContent& mat)
|
---|
| 611 | {
|
---|
| 612 | MatrixContent tmp = mat;
|
---|
| 613 | tmp*=factor;
|
---|
| 614 | return tmp;
|
---|
| 615 | }
|
---|
| 616 |
|
---|
| 617 | /** Scalar multiplication and copy operator (with operands exchanged).
|
---|
| 618 | * \param &mat MatrixContent to scale
|
---|
| 619 | * \param factor factor to scale with
|
---|
| 620 | * \return copied and scaled MatrixContent
|
---|
| 621 | */
|
---|
| 622 | const MatrixContent operator*(const MatrixContent &mat,const double factor)
|
---|
| 623 | {
|
---|
| 624 | return factor*mat;
|
---|
| 625 | }
|
---|
| 626 |
|
---|
| 627 | /** Equality operator.
|
---|
| 628 | * Note that we use numerical sensible checking, i.e. with threshold MYEPSILON.
|
---|
| 629 | * \param &rhs MatrixContent to checks against
|
---|
| 630 | */
|
---|
| 631 | bool MatrixContent::operator==(const MatrixContent &rhs) const
|
---|
| 632 | {
|
---|
| 633 | if ((rows == rhs.rows) && (columns == rhs.columns)) {
|
---|
| 634 | for(int i=rows;i--;){
|
---|
| 635 | for(int j=columns;j--;){
|
---|
| 636 | if(fabs(at(i,j)-rhs.at(i,j))>MYEPSILON){
|
---|
| 637 | return false;
|
---|
| 638 | }
|
---|
| 639 | }
|
---|
| 640 | }
|
---|
| 641 | return true;
|
---|
| 642 | }
|
---|
| 643 | return false;
|
---|
| 644 | }
|
---|
| 645 |
|
---|
[b4cf2b] | 646 |
|
---|
| 647 | std::ostream & operator<<(std::ostream &ost, const MatrixContent &mat)
|
---|
| 648 | {
|
---|
| 649 | ost << "[";
|
---|
| 650 | for (size_t i=0;i<mat.rows;i++) {
|
---|
| 651 | for (size_t j=0;j<mat.columns;j++) {
|
---|
| 652 | ost << mat.at(i,j);
|
---|
| 653 | if (j != mat.columns-1)
|
---|
| 654 | ost << " ";
|
---|
| 655 | }
|
---|
| 656 | if (i != mat.rows-1)
|
---|
| 657 | ost << "; ";
|
---|
| 658 | }
|
---|
| 659 | ost << "]";
|
---|
| 660 | return ost;
|
---|
| 661 | }
|
---|