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