| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | /*
 | 
|---|
| 9 |  * gslmatrix.cpp
 | 
|---|
| 10 |  *
 | 
|---|
| 11 |  *  Created on: Jan 8, 2010
 | 
|---|
| 12 |  *      Author: heber
 | 
|---|
| 13 |  */
 | 
|---|
| 14 | 
 | 
|---|
| 15 | // include config.h
 | 
|---|
| 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 17 | #include <config.h>
 | 
|---|
| 18 | #endif
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include "Helpers/MemDebug.hpp"
 | 
|---|
| 21 | 
 | 
|---|
| 22 | using namespace std;
 | 
|---|
| 23 | 
 | 
|---|
| 24 | #include "LinearAlgebra/gslmatrix.hpp"
 | 
|---|
| 25 | #include "Helpers/helpers.hpp"
 | 
|---|
| 26 | #include "Helpers/fast_functions.hpp"
 | 
|---|
| 27 | 
 | 
|---|
| 28 | #include <cassert>
 | 
|---|
| 29 | #include <iostream>
 | 
|---|
| 30 | #include <gsl/gsl_linalg.h>
 | 
|---|
| 31 | #include <gsl/gsl_blas.h>
 | 
|---|
| 32 | #include <gsl/gsl_eigen.h>
 | 
|---|
| 33 | #include <gsl/gsl_matrix.h>
 | 
|---|
| 34 | #include <gsl/gsl_vector.h>
 | 
|---|
| 35 | 
 | 
|---|
| 36 | /** Constructor of class GSLMatrix.
 | 
|---|
| 37 |  * Allocates GSL structures
 | 
|---|
| 38 |  * \param m dimension of matrix
 | 
|---|
| 39 |  */
 | 
|---|
| 40 | GSLMatrix::GSLMatrix(size_t m, size_t n) : rows(m), columns(n)
 | 
|---|
| 41 | {
 | 
|---|
| 42 |   matrix = gsl_matrix_calloc(rows, columns);
 | 
|---|
| 43 | };
 | 
|---|
| 44 | 
 | 
|---|
| 45 | /** Copy constructor of class GSLMatrix.
 | 
|---|
| 46 |  * Allocates GSL structures and copies components from \a *src.
 | 
|---|
| 47 |  * \param *src source matrix
 | 
|---|
| 48 |  */
 | 
|---|
| 49 | GSLMatrix::GSLMatrix(const GSLMatrix * const src) : rows(src->rows), columns(src->columns)
 | 
|---|
| 50 | {
 | 
|---|
| 51 |   matrix = gsl_matrix_alloc(rows, columns);
 | 
|---|
| 52 |   gsl_matrix_memcpy (matrix, src->matrix);
 | 
|---|
| 53 | };
 | 
|---|
| 54 | 
 | 
|---|
| 55 | /** Copy constructor of class GSLMatrix.
 | 
|---|
| 56 |  * We take over pointer to gsl_matrix and set the parameter pointer to NULL
 | 
|---|
| 57 |  * afterwards.
 | 
|---|
| 58 |  * \param m row count
 | 
|---|
| 59 |  * \param n column count
 | 
|---|
| 60 |  * \param *&src source gsl_matrix
 | 
|---|
| 61 |  */
 | 
|---|
| 62 | GSLMatrix::GSLMatrix(size_t m, size_t n, gsl_matrix *&src) :
 | 
|---|
| 63 |     rows(m),
 | 
|---|
| 64 |     columns(n)
 | 
|---|
| 65 | {
 | 
|---|
| 66 |   matrix = src;
 | 
|---|
| 67 |   src = NULL;
 | 
|---|
| 68 | };
 | 
|---|
| 69 | 
 | 
|---|
| 70 | /** Destructor of class GSLMatrix.
 | 
|---|
| 71 |  * Frees GSL structures
 | 
|---|
| 72 |  */
 | 
|---|
| 73 | GSLMatrix::~GSLMatrix()
 | 
|---|
| 74 | {
 | 
|---|
| 75 |   gsl_matrix_free(matrix);
 | 
|---|
| 76 |   rows = 0;
 | 
|---|
| 77 |   columns = 0;
 | 
|---|
| 78 | };
 | 
|---|
| 79 | 
 | 
|---|
| 80 | /** Assignment operator.
 | 
|---|
| 81 |  * \param &rhs right hand side
 | 
|---|
| 82 |  * \return object itself
 | 
|---|
| 83 |  */
 | 
|---|
| 84 | GSLMatrix& GSLMatrix::operator=(const GSLMatrix& rhs)
 | 
|---|
| 85 | {
 | 
|---|
| 86 |   if (this == &rhs)   // not necessary here, but identity assignment check is generally a good idea
 | 
|---|
| 87 |     return *this;
 | 
|---|
| 88 |   assert(rows == rhs.rows && columns == rhs.columns && "Number of rows and columns do not match!");
 | 
|---|
| 89 | 
 | 
|---|
| 90 |   gsl_matrix_memcpy (matrix, rhs.matrix);
 | 
|---|
| 91 | 
 | 
|---|
| 92 |   return *this;
 | 
|---|
| 93 | };
 | 
|---|
| 94 | 
 | 
|---|
| 95 | /* ============================ Accessing =============================== */
 | 
|---|
| 96 | /** This function sets the matrix from a double array.
 | 
|---|
| 97 |  * Creates a matrix view of the array and performs a memcopy.
 | 
|---|
| 98 |  * \param *x array of values (no dimension check is performed)
 | 
|---|
| 99 |  */
 | 
|---|
| 100 | void GSLMatrix::SetFromDoubleArray(double * x)
 | 
|---|
| 101 | {
 | 
|---|
| 102 |   gsl_matrix_view m = gsl_matrix_view_array (x, rows, columns);
 | 
|---|
| 103 |   gsl_matrix_memcpy (matrix, &m.matrix);
 | 
|---|
| 104 | };
 | 
|---|
| 105 | 
 | 
|---|
| 106 | /** This function returns the i-th element of a matrix.
 | 
|---|
| 107 |  * If \a m or \a n lies outside the allowed range of 0 to GSLMatrix::dimension-1 then the error handler is invoked and 0 is returned.
 | 
|---|
| 108 |  * \param m row index
 | 
|---|
| 109 |  * \param n colum index
 | 
|---|
| 110 |  * \return (m,n)-th element of matrix
 | 
|---|
| 111 |  */
 | 
|---|
| 112 | double GSLMatrix::Get(size_t m, size_t n) const
 | 
|---|
| 113 | {
 | 
|---|
| 114 |   return gsl_matrix_get (matrix, m, n);
 | 
|---|
| 115 | };
 | 
|---|
| 116 | 
 | 
|---|
| 117 | /** This function sets the value of the \a m -th element of a matrix to \a x.
 | 
|---|
| 118 |  *  If \a m or \a n lies outside the allowed range of 0 to GSLMatrix::dimension-1 then the error handler is invoked.
 | 
|---|
| 119 |  * \param m row index
 | 
|---|
| 120 |  * \param m column index
 | 
|---|
| 121 |  * \param x value to set element (m,n) to
 | 
|---|
| 122 |  */
 | 
|---|
| 123 | void GSLMatrix::Set(size_t m, size_t n, double x)
 | 
|---|
| 124 | {
 | 
|---|
| 125 |   gsl_matrix_set (matrix, m, n, x);
 | 
|---|
| 126 | };
 | 
|---|
| 127 | 
 | 
|---|
| 128 | /** These functions return a pointer to the \a m-th element of a matrix.
 | 
|---|
| 129 |  *  If \a m or \a n lies outside the allowed range of 0 to GSLMatrix::dimension-1 then the error handler is invoked and a null pointer is returned.
 | 
|---|
| 130 |  * \param m index
 | 
|---|
| 131 |  * \return pointer to \a m-th element
 | 
|---|
| 132 |  */
 | 
|---|
| 133 | double *GSLMatrix::Pointer(size_t m, size_t n)
 | 
|---|
| 134 | {
 | 
|---|
| 135 |   return gsl_matrix_ptr (matrix, m, n);
 | 
|---|
| 136 | };
 | 
|---|
| 137 | 
 | 
|---|
| 138 | /** These functions return a constant pointer to the \a m-th element of a matrix.
 | 
|---|
| 139 |  *  If \a m or \a n lies outside the allowed range of 0 to GSLMatrix::dimension-1 then the error handler is invoked and a null pointer is returned.
 | 
|---|
| 140 |  * \param m index
 | 
|---|
| 141 |  * \return const pointer to \a m-th element
 | 
|---|
| 142 |  */
 | 
|---|
| 143 | const double *GSLMatrix::const_Pointer(size_t m, size_t n) const
 | 
|---|
| 144 | {
 | 
|---|
| 145 |   return gsl_matrix_const_ptr (matrix, m, n);
 | 
|---|
| 146 | };
 | 
|---|
| 147 | 
 | 
|---|
| 148 | /** Returns the number of rows of the matrix.
 | 
|---|
| 149 |  * \return number of rows
 | 
|---|
| 150 |  */
 | 
|---|
| 151 | size_t GSLMatrix::getRowCount() const
 | 
|---|
| 152 | {
 | 
|---|
| 153 |   return rows;
 | 
|---|
| 154 | }
 | 
|---|
| 155 | 
 | 
|---|
| 156 | /** Returns the number of columns of the matrix.
 | 
|---|
| 157 |  * \return number of columns
 | 
|---|
| 158 |  */
 | 
|---|
| 159 | size_t GSLMatrix::getColumnCount() const
 | 
|---|
| 160 | {
 | 
|---|
| 161 |   return columns;
 | 
|---|
| 162 | }
 | 
|---|
| 163 | 
 | 
|---|
| 164 | /* ========================== Initializing =============================== */
 | 
|---|
| 165 | /** This function sets all the elements of the matrix to the value \a x.
 | 
|---|
| 166 |  * \param *x
 | 
|---|
| 167 |  */
 | 
|---|
| 168 | void GSLMatrix::SetAll(double x)
 | 
|---|
| 169 | {
 | 
|---|
| 170 |   gsl_matrix_set_all (matrix, x);
 | 
|---|
| 171 | };
 | 
|---|
| 172 | 
 | 
|---|
| 173 | /** This function sets all the elements of the matrix to zero.
 | 
|---|
| 174 |  */
 | 
|---|
| 175 | void GSLMatrix::SetZero()
 | 
|---|
| 176 | {
 | 
|---|
| 177 |   gsl_matrix_set_zero (matrix);
 | 
|---|
| 178 | };
 | 
|---|
| 179 | 
 | 
|---|
| 180 | /** This function sets the elements of the matrix to the corresponding elements of the identity matrix, \f$m(i,j) = \delta(i,j)\f$, i.e. a unit diagonal with all off-diagonal elements zero.
 | 
|---|
| 181 |  * This applies to both square and rectangular matrices.
 | 
|---|
| 182 |  */
 | 
|---|
| 183 | void GSLMatrix::SetIdentity()
 | 
|---|
| 184 | {
 | 
|---|
| 185 |   gsl_matrix_set_identity (matrix);
 | 
|---|
| 186 | };
 | 
|---|
| 187 | 
 | 
|---|
| 188 | /* ====================== Exchanging elements ============================ */
 | 
|---|
| 189 | /** This function exchanges the \a i-th and \a j-th row of the matrix in-place.
 | 
|---|
| 190 |  * \param i i-th row to swap with ...
 | 
|---|
| 191 |  * \param j ... j-th row to swap against
 | 
|---|
| 192 |  */
 | 
|---|
| 193 | bool GSLMatrix::SwapRows(size_t i, size_t j)
 | 
|---|
| 194 | {
 | 
|---|
| 195 |   return (gsl_matrix_swap_rows (matrix, i, j) == GSL_SUCCESS);
 | 
|---|
| 196 | };
 | 
|---|
| 197 | 
 | 
|---|
| 198 | /** This function exchanges the \a i-th and \a j-th column of the matrix in-place.
 | 
|---|
| 199 |  * \param i i-th column to swap with ...
 | 
|---|
| 200 |  * \param j ... j-th column to swap against
 | 
|---|
| 201 |  */
 | 
|---|
| 202 | bool GSLMatrix::SwapColumns(size_t i, size_t j)
 | 
|---|
| 203 | {
 | 
|---|
| 204 |   return (gsl_matrix_swap_columns (matrix, i, j) == GSL_SUCCESS);
 | 
|---|
| 205 | };
 | 
|---|
| 206 | 
 | 
|---|
| 207 | /** This function exchanges the \a i-th row and \a j-th column of the matrix in-place.
 | 
|---|
| 208 |  * The matrix must be square for this operation to be possible.
 | 
|---|
| 209 |  * \param i i-th row to swap with ...
 | 
|---|
| 210 |  * \param j ... j-th column to swap against
 | 
|---|
| 211 |  */
 | 
|---|
| 212 | bool GSLMatrix::SwapRowColumn(size_t i, size_t j)
 | 
|---|
| 213 | {
 | 
|---|
| 214 |   assert (rows == columns && "The matrix must be square for swapping row against column to be possible.");
 | 
|---|
| 215 |   return (gsl_matrix_swap_rowcol (matrix, i, j) == GSL_SUCCESS);
 | 
|---|
| 216 | };
 | 
|---|
| 217 | 
 | 
|---|
| 218 | /** This function transposes the matrix.
 | 
|---|
| 219 |  * Note that the function is extended to the non-square case, where the matrix is re-allocated and copied.
 | 
|---|
| 220 |  */
 | 
|---|
| 221 | bool GSLMatrix::Transpose()
 | 
|---|
| 222 | {
 | 
|---|
| 223 |   if (rows == columns)// if square, use GSL
 | 
|---|
| 224 |     return (gsl_matrix_transpose (matrix) == GSL_SUCCESS);
 | 
|---|
| 225 |   else { // otherwise we have to copy a bit
 | 
|---|
| 226 |     gsl_matrix *dest = gsl_matrix_alloc(columns, rows);
 | 
|---|
| 227 |     for (size_t i=0;i<rows; i++)
 | 
|---|
| 228 |       for (size_t j=0;j<columns;j++) {
 | 
|---|
| 229 |         gsl_matrix_set(dest, j,i, gsl_matrix_get(matrix, i,j) );
 | 
|---|
| 230 |       }
 | 
|---|
| 231 |     gsl_matrix_free(matrix);
 | 
|---|
| 232 |     matrix = dest;
 | 
|---|
| 233 |     flip(rows, columns);
 | 
|---|
| 234 |     return true;
 | 
|---|
| 235 |   }
 | 
|---|
| 236 | };
 | 
|---|
| 237 | 
 | 
|---|
| 238 | /* ============================ Properties ============================== */
 | 
|---|
| 239 | /** Checks whether matrix' elements are strictly null.
 | 
|---|
| 240 |  * \return true - is null, false - else
 | 
|---|
| 241 |  */
 | 
|---|
| 242 | bool GSLMatrix::IsNull() const
 | 
|---|
| 243 | {
 | 
|---|
| 244 |   return gsl_matrix_isnull (matrix);
 | 
|---|
| 245 | };
 | 
|---|
| 246 | 
 | 
|---|
| 247 | /** Checks whether matrix' elements are strictly positive.
 | 
|---|
| 248 |  * \return true - is positive, false - else
 | 
|---|
| 249 |  */
 | 
|---|
| 250 | bool GSLMatrix::IsPositive() const
 | 
|---|
| 251 | {
 | 
|---|
| 252 |   return gsl_matrix_ispos (matrix);
 | 
|---|
| 253 | };
 | 
|---|
| 254 | 
 | 
|---|
| 255 | /** Checks whether matrix' elements are strictly negative.
 | 
|---|
| 256 |  * \return true - is negative, false - else
 | 
|---|
| 257 |  */
 | 
|---|
| 258 | bool GSLMatrix::IsNegative() const
 | 
|---|
| 259 | {
 | 
|---|
| 260 |   return gsl_matrix_isneg (matrix);
 | 
|---|
| 261 | };
 | 
|---|
| 262 | 
 | 
|---|
| 263 | /** Checks whether matrix' elements are strictly non-negative.
 | 
|---|
| 264 |  * \return true - is non-negative, false - else
 | 
|---|
| 265 |  */
 | 
|---|
| 266 | bool GSLMatrix::IsNonNegative() const
 | 
|---|
| 267 | {
 | 
|---|
| 268 |   return gsl_matrix_isnonneg (matrix);
 | 
|---|
| 269 | };
 | 
|---|
| 270 | 
 | 
|---|
| 271 | /** This function performs a Cholesky decomposition to determine whether matrix is positive definite.
 | 
|---|
| 272 |  * We check whether GSL returns GSL_EDOM as error, indicating that decomposition failed due to matrix not being positive-definite.
 | 
|---|
| 273 |  * \return true - matrix is positive-definite, false - else
 | 
|---|
| 274 |  */
 | 
|---|
| 275 | bool GSLMatrix::IsPositiveDefinite() const
 | 
|---|
| 276 | {
 | 
|---|
| 277 |   if (rows != columns)  // only possible for square matrices.
 | 
|---|
| 278 |     return false;
 | 
|---|
| 279 |   else
 | 
|---|
| 280 |     return (gsl_linalg_cholesky_decomp (matrix) != GSL_EDOM);
 | 
|---|
| 281 | };
 | 
|---|
| 282 | 
 | 
|---|
| 283 | 
 | 
|---|
| 284 | /** Calculates the determinant of the matrix.
 | 
|---|
| 285 |  * if matrix is square, uses LU decomposition.
 | 
|---|
| 286 |  */
 | 
|---|
| 287 | double GSLMatrix::Determinant() const
 | 
|---|
| 288 | {
 | 
|---|
| 289 |   int signum = 0;
 | 
|---|
| 290 |   assert (rows == columns && "Determinant can only be calculated for square matrices.");
 | 
|---|
| 291 |   gsl_permutation *p = gsl_permutation_alloc(rows);
 | 
|---|
| 292 |   gsl_linalg_LU_decomp(matrix, p, &signum);
 | 
|---|
| 293 |   gsl_permutation_free(p);
 | 
|---|
| 294 |   return gsl_linalg_LU_det(matrix, signum); 
 | 
|---|
| 295 | };
 | 
|---|
| 296 | 
 | 
|---|
| 297 | 
 | 
|---|
| 298 | /* ============================ Properties ============================== */
 | 
|---|
| 299 | 
 | 
|---|
| 300 | const GSLMatrix &GSLMatrix::operator+=(const GSLMatrix &rhs)
 | 
|---|
| 301 | {
 | 
|---|
| 302 |   gsl_matrix_add(matrix, rhs.matrix);
 | 
|---|
| 303 |   return *this;
 | 
|---|
| 304 | }
 | 
|---|
| 305 | 
 | 
|---|
| 306 | const GSLMatrix &GSLMatrix::operator-=(const GSLMatrix &rhs)
 | 
|---|
| 307 | {
 | 
|---|
| 308 |   gsl_matrix_sub(matrix, rhs.matrix);
 | 
|---|
| 309 |   return *this;
 | 
|---|
| 310 | }
 | 
|---|
| 311 | 
 | 
|---|
| 312 | const GSLMatrix &GSLMatrix::operator*=(const GSLMatrix &rhs)
 | 
|---|
| 313 | {
 | 
|---|
| 314 |   (*this) = (*this)*rhs;
 | 
|---|
| 315 |   return *this;
 | 
|---|
| 316 | }
 | 
|---|
| 317 | 
 | 
|---|
| 318 | const GSLMatrix GSLMatrix::operator+(const GSLMatrix &rhs) const
 | 
|---|
| 319 | {
 | 
|---|
| 320 |   GSLMatrix tmp = *this;
 | 
|---|
| 321 |   tmp+=rhs;
 | 
|---|
| 322 |   return tmp;
 | 
|---|
| 323 | }
 | 
|---|
| 324 | 
 | 
|---|
| 325 | const GSLMatrix GSLMatrix::operator-(const GSLMatrix &rhs) const
 | 
|---|
| 326 | {
 | 
|---|
| 327 |   GSLMatrix tmp = *this;
 | 
|---|
| 328 |   tmp-=rhs;
 | 
|---|
| 329 |   return tmp;
 | 
|---|
| 330 | }
 | 
|---|
| 331 | 
 | 
|---|
| 332 | const GSLMatrix GSLMatrix::operator*(const GSLMatrix &rhs) const
 | 
|---|
| 333 | {
 | 
|---|
| 334 |   gsl_matrix *res = gsl_matrix_alloc(rhs.rows, rhs.columns);
 | 
|---|
| 335 |   gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, matrix, rhs.matrix, 0.0, res);
 | 
|---|
| 336 |   return GSLMatrix(rhs.rows, rhs.columns, res);
 | 
|---|
| 337 | }
 | 
|---|
| 338 | 
 | 
|---|
| 339 | /** Print matrix in a matlab style manner.
 | 
|---|
| 340 |  * \param &ost reference to output stream
 | 
|---|
| 341 |  * \param &mat reference to matrix to print
 | 
|---|
| 342 |  * \return reference to obtained output stream for concatenation
 | 
|---|
| 343 |  */
 | 
|---|
| 344 | ostream &operator<<(ostream &ost,const GSLMatrix &mat)
 | 
|---|
| 345 | {
 | 
|---|
| 346 |   ost << "[";
 | 
|---|
| 347 |   for(size_t i = 0;i<mat.rows;++i){
 | 
|---|
| 348 |     for(size_t j = 0; j<mat.columns;++j){
 | 
|---|
| 349 |       ost << mat.Get(i,j);
 | 
|---|
| 350 |       if(j!=mat.columns-1)
 | 
|---|
| 351 |         ost << " ";
 | 
|---|
| 352 |     }
 | 
|---|
| 353 |     if(i!=mat.rows-1)
 | 
|---|
| 354 |       ost << "; ";
 | 
|---|
| 355 |   }
 | 
|---|
| 356 |   ost << "]";
 | 
|---|
| 357 |   return ost;
 | 
|---|
| 358 | }
 | 
|---|
| 359 | 
 | 
|---|