| 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 <gsl/gsl_linalg.h>
 | 
|---|
| 30 | 
 | 
|---|
| 31 | /** Constructor of class GSLMatrix.
 | 
|---|
| 32 |  * Allocates GSL structures
 | 
|---|
| 33 |  * \param m dimension of matrix
 | 
|---|
| 34 |  */
 | 
|---|
| 35 | GSLMatrix::GSLMatrix(size_t m, size_t n) : rows(m), columns(n)
 | 
|---|
| 36 | {
 | 
|---|
| 37 |   matrix = gsl_matrix_calloc(rows, columns);
 | 
|---|
| 38 | };
 | 
|---|
| 39 | 
 | 
|---|
| 40 | /** Copy constructor of class GSLMatrix.
 | 
|---|
| 41 |  * Allocates GSL structures and copies components from \a *src.
 | 
|---|
| 42 |  * \param *src source matrix
 | 
|---|
| 43 |  */
 | 
|---|
| 44 | GSLMatrix::GSLMatrix(const GSLMatrix * const src) : rows(src->rows), columns(src->columns)
 | 
|---|
| 45 | {
 | 
|---|
| 46 |   matrix = gsl_matrix_alloc(rows, columns);
 | 
|---|
| 47 |   gsl_matrix_memcpy (matrix, src->matrix);
 | 
|---|
| 48 | };
 | 
|---|
| 49 | 
 | 
|---|
| 50 | /** Destructor of class GSLMatrix.
 | 
|---|
| 51 |  * Frees GSL structures
 | 
|---|
| 52 |  */
 | 
|---|
| 53 | GSLMatrix::~GSLMatrix()
 | 
|---|
| 54 | {
 | 
|---|
| 55 |   gsl_matrix_free(matrix);
 | 
|---|
| 56 |   rows = 0;
 | 
|---|
| 57 |   columns = 0;
 | 
|---|
| 58 | };
 | 
|---|
| 59 | 
 | 
|---|
| 60 | /** Assignment operator.
 | 
|---|
| 61 |  * \param &rhs right hand side
 | 
|---|
| 62 |  * \return object itself
 | 
|---|
| 63 |  */
 | 
|---|
| 64 | GSLMatrix& GSLMatrix::operator=(const GSLMatrix& rhs)
 | 
|---|
| 65 | {
 | 
|---|
| 66 |   if (this == &rhs)   // not necessary here, but identity assignment check is generally a good idea
 | 
|---|
| 67 |     return *this;
 | 
|---|
| 68 |   assert(rows == rhs.rows && columns == rhs.columns && "Number of rows and columns do not match!");
 | 
|---|
| 69 | 
 | 
|---|
| 70 |   gsl_matrix_memcpy (matrix, rhs.matrix);
 | 
|---|
| 71 | 
 | 
|---|
| 72 |   return *this;
 | 
|---|
| 73 | };
 | 
|---|
| 74 | 
 | 
|---|
| 75 | /* ============================ Accessing =============================== */
 | 
|---|
| 76 | /** This function sets the matrix from a double array.
 | 
|---|
| 77 |  * Creates a matrix view of the array and performs a memcopy.
 | 
|---|
| 78 |  * \param *x array of values (no dimension check is performed)
 | 
|---|
| 79 |  */
 | 
|---|
| 80 | void GSLMatrix::SetFromDoubleArray(double * x)
 | 
|---|
| 81 | {
 | 
|---|
| 82 |   gsl_matrix_view m = gsl_matrix_view_array (x, rows, columns);
 | 
|---|
| 83 |   gsl_matrix_memcpy (matrix, &m.matrix);
 | 
|---|
| 84 | };
 | 
|---|
| 85 | 
 | 
|---|
| 86 | /** This function returns the i-th element of a matrix.
 | 
|---|
| 87 |  * 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.
 | 
|---|
| 88 |  * \param m row index
 | 
|---|
| 89 |  * \param n colum index
 | 
|---|
| 90 |  * \return (m,n)-th element of matrix
 | 
|---|
| 91 |  */
 | 
|---|
| 92 | double GSLMatrix::Get(size_t m, size_t n)
 | 
|---|
| 93 | {
 | 
|---|
| 94 |   return gsl_matrix_get (matrix, m, n);
 | 
|---|
| 95 | };
 | 
|---|
| 96 | 
 | 
|---|
| 97 | /** This function sets the value of the \a m -th element of a matrix to \a x.
 | 
|---|
| 98 |  *  If \a m or \a n lies outside the allowed range of 0 to GSLMatrix::dimension-1 then the error handler is invoked.
 | 
|---|
| 99 |  * \param m row index
 | 
|---|
| 100 |  * \param m column index
 | 
|---|
| 101 |  * \param x value to set element (m,n) to
 | 
|---|
| 102 |  */
 | 
|---|
| 103 | void GSLMatrix::Set(size_t m, size_t n, double x)
 | 
|---|
| 104 | {
 | 
|---|
| 105 |   gsl_matrix_set (matrix, m, n, x);
 | 
|---|
| 106 | };
 | 
|---|
| 107 | 
 | 
|---|
| 108 | /** These functions return a pointer to the \a m-th element of a matrix.
 | 
|---|
| 109 |  *  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.
 | 
|---|
| 110 |  * \param m index
 | 
|---|
| 111 |  * \return pointer to \a m-th element
 | 
|---|
| 112 |  */
 | 
|---|
| 113 | double *GSLMatrix::Pointer(size_t m, size_t n)
 | 
|---|
| 114 | {
 | 
|---|
| 115 |   return gsl_matrix_ptr (matrix, m, n);
 | 
|---|
| 116 | };
 | 
|---|
| 117 | 
 | 
|---|
| 118 | /** These functions return a constant pointer to the \a m-th element of a matrix.
 | 
|---|
| 119 |  *  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.
 | 
|---|
| 120 |  * \param m index
 | 
|---|
| 121 |  * \return const pointer to \a m-th element
 | 
|---|
| 122 |  */
 | 
|---|
| 123 | const double *GSLMatrix::const_Pointer(size_t m, size_t n)
 | 
|---|
| 124 | {
 | 
|---|
| 125 |   return gsl_matrix_const_ptr (matrix, m, n);
 | 
|---|
| 126 | };
 | 
|---|
| 127 | 
 | 
|---|
| 128 | /* ========================== Initializing =============================== */
 | 
|---|
| 129 | /** This function sets all the elements of the matrix to the value \a x.
 | 
|---|
| 130 |  * \param *x
 | 
|---|
| 131 |  */
 | 
|---|
| 132 | void GSLMatrix::SetAll(double x)
 | 
|---|
| 133 | {
 | 
|---|
| 134 |   gsl_matrix_set_all (matrix, x);
 | 
|---|
| 135 | };
 | 
|---|
| 136 | 
 | 
|---|
| 137 | /** This function sets all the elements of the matrix to zero.
 | 
|---|
| 138 |  */
 | 
|---|
| 139 | void GSLMatrix::SetZero()
 | 
|---|
| 140 | {
 | 
|---|
| 141 |   gsl_matrix_set_zero (matrix);
 | 
|---|
| 142 | };
 | 
|---|
| 143 | 
 | 
|---|
| 144 | /** 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.
 | 
|---|
| 145 |  * This applies to both square and rectangular matrices.
 | 
|---|
| 146 |  */
 | 
|---|
| 147 | void GSLMatrix::SetIdentity()
 | 
|---|
| 148 | {
 | 
|---|
| 149 |   gsl_matrix_set_identity (matrix);
 | 
|---|
| 150 | };
 | 
|---|
| 151 | 
 | 
|---|
| 152 | /* ====================== Exchanging elements ============================ */
 | 
|---|
| 153 | /** This function exchanges the \a i-th and \a j-th row of the matrix in-place.
 | 
|---|
| 154 |  * \param i i-th row to swap with ...
 | 
|---|
| 155 |  * \param j ... j-th row to swap against
 | 
|---|
| 156 |  */
 | 
|---|
| 157 | bool GSLMatrix::SwapRows(size_t i, size_t j)
 | 
|---|
| 158 | {
 | 
|---|
| 159 |   return (gsl_matrix_swap_rows (matrix, i, j) == GSL_SUCCESS);
 | 
|---|
| 160 | };
 | 
|---|
| 161 | 
 | 
|---|
| 162 | /** This function exchanges the \a i-th and \a j-th column of the matrix in-place.
 | 
|---|
| 163 |  * \param i i-th column to swap with ...
 | 
|---|
| 164 |  * \param j ... j-th column to swap against
 | 
|---|
| 165 |  */
 | 
|---|
| 166 | bool GSLMatrix::SwapColumns(size_t i, size_t j)
 | 
|---|
| 167 | {
 | 
|---|
| 168 |   return (gsl_matrix_swap_columns (matrix, i, j) == GSL_SUCCESS);
 | 
|---|
| 169 | };
 | 
|---|
| 170 | 
 | 
|---|
| 171 | /** This function exchanges the \a i-th row and \a j-th column of the matrix in-place.
 | 
|---|
| 172 |  * The matrix must be square for this operation to be possible.
 | 
|---|
| 173 |  * \param i i-th row to swap with ...
 | 
|---|
| 174 |  * \param j ... j-th column to swap against
 | 
|---|
| 175 |  */
 | 
|---|
| 176 | bool GSLMatrix::SwapRowColumn(size_t i, size_t j)
 | 
|---|
| 177 | {
 | 
|---|
| 178 |   assert (rows == columns && "The matrix must be square for swapping row against column to be possible.");
 | 
|---|
| 179 |   return (gsl_matrix_swap_rowcol (matrix, i, j) == GSL_SUCCESS);
 | 
|---|
| 180 | };
 | 
|---|
| 181 | 
 | 
|---|
| 182 | /** This function transposes the matrix.
 | 
|---|
| 183 |  * Note that the function is extended to the non-square case, where the matrix is re-allocated and copied.
 | 
|---|
| 184 |  */
 | 
|---|
| 185 | bool GSLMatrix::Transpose()
 | 
|---|
| 186 | {
 | 
|---|
| 187 |   if (rows == columns)// if square, use GSL
 | 
|---|
| 188 |     return (gsl_matrix_transpose (matrix) == GSL_SUCCESS);
 | 
|---|
| 189 |   else { // otherwise we have to copy a bit
 | 
|---|
| 190 |     gsl_matrix *dest = gsl_matrix_alloc(columns, rows);
 | 
|---|
| 191 |     for (size_t i=0;i<rows; i++)
 | 
|---|
| 192 |       for (size_t j=0;j<columns;j++) {
 | 
|---|
| 193 |         gsl_matrix_set(dest, j,i, gsl_matrix_get(matrix, i,j) );
 | 
|---|
| 194 |       }
 | 
|---|
| 195 |     gsl_matrix_free(matrix);
 | 
|---|
| 196 |     matrix = dest;
 | 
|---|
| 197 |     flip(rows, columns);
 | 
|---|
| 198 |     return true;
 | 
|---|
| 199 |   }
 | 
|---|
| 200 | };
 | 
|---|
| 201 | 
 | 
|---|
| 202 | /* ============================ Properties ============================== */
 | 
|---|
| 203 | /** Checks whether matrix' elements are strictly null.
 | 
|---|
| 204 |  * \return true - is null, false - else
 | 
|---|
| 205 |  */
 | 
|---|
| 206 | bool GSLMatrix::IsNull()
 | 
|---|
| 207 | {
 | 
|---|
| 208 |   return gsl_matrix_isnull (matrix);
 | 
|---|
| 209 | };
 | 
|---|
| 210 | 
 | 
|---|
| 211 | /** Checks whether matrix' elements are strictly positive.
 | 
|---|
| 212 |  * \return true - is positive, false - else
 | 
|---|
| 213 |  */
 | 
|---|
| 214 | bool GSLMatrix::IsPositive()
 | 
|---|
| 215 | {
 | 
|---|
| 216 |   return gsl_matrix_ispos (matrix);
 | 
|---|
| 217 | };
 | 
|---|
| 218 | 
 | 
|---|
| 219 | /** Checks whether matrix' elements are strictly negative.
 | 
|---|
| 220 |  * \return true - is negative, false - else
 | 
|---|
| 221 |  */
 | 
|---|
| 222 | bool GSLMatrix::IsNegative()
 | 
|---|
| 223 | {
 | 
|---|
| 224 |   return gsl_matrix_isneg (matrix);
 | 
|---|
| 225 | };
 | 
|---|
| 226 | 
 | 
|---|
| 227 | /** Checks whether matrix' elements are strictly non-negative.
 | 
|---|
| 228 |  * \return true - is non-negative, false - else
 | 
|---|
| 229 |  */
 | 
|---|
| 230 | bool GSLMatrix::IsNonNegative()
 | 
|---|
| 231 | {
 | 
|---|
| 232 |   return gsl_matrix_isnonneg (matrix);
 | 
|---|
| 233 | };
 | 
|---|
| 234 | 
 | 
|---|
| 235 | /** This function performs a Cholesky decomposition to determine whether matrix is positive definite.
 | 
|---|
| 236 |  * We check whether GSL returns GSL_EDOM as error, indicating that decomposition failed due to matrix not being positive-definite.
 | 
|---|
| 237 |  * \return true - matrix is positive-definite, false - else
 | 
|---|
| 238 |  */
 | 
|---|
| 239 | bool GSLMatrix::IsPositiveDefinite()
 | 
|---|
| 240 | {
 | 
|---|
| 241 |   if (rows != columns)  // only possible for square matrices.
 | 
|---|
| 242 |     return false;
 | 
|---|
| 243 |   else
 | 
|---|
| 244 |     return (gsl_linalg_cholesky_decomp (matrix) != GSL_EDOM);
 | 
|---|
| 245 | };
 | 
|---|
| 246 | 
 | 
|---|
| 247 | 
 | 
|---|
| 248 | /** Calculates the determinant of the matrix.
 | 
|---|
| 249 |  * if matrix is square, uses LU decomposition.
 | 
|---|
| 250 |  */
 | 
|---|
| 251 | double GSLMatrix::Determinant()
 | 
|---|
| 252 | {
 | 
|---|
| 253 |   int signum = 0;
 | 
|---|
| 254 |   assert (rows == columns && "Determinant can only be calculated for square matrices.");
 | 
|---|
| 255 |   gsl_permutation *p = gsl_permutation_alloc(rows);
 | 
|---|
| 256 |   gsl_linalg_LU_decomp(matrix, p, &signum);
 | 
|---|
| 257 |   gsl_permutation_free(p);
 | 
|---|
| 258 |   return gsl_linalg_LU_det(matrix, signum); 
 | 
|---|
| 259 | };
 | 
|---|
| 260 | 
 | 
|---|