| 1 | /*
 | 
|---|
| 2 |  * linearsystemofequations.cpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Jan 8, 2010
 | 
|---|
| 5 |  *      Author: heber
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | // include config.h
 | 
|---|
| 9 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 10 | #include <config.h>
 | 
|---|
| 11 | #endif
 | 
|---|
| 12 | 
 | 
|---|
| 13 | #include "Helpers/MemDebug.hpp"
 | 
|---|
| 14 | 
 | 
|---|
| 15 | #include "defs.hpp"
 | 
|---|
| 16 | #include "LinearAlgebra/gslmatrix.hpp"
 | 
|---|
| 17 | #include "LinearAlgebra/gslvector.hpp"
 | 
|---|
| 18 | #include "LinearAlgebra/linearsystemofequations.hpp"
 | 
|---|
| 19 | #include "Helpers/logger.hpp"
 | 
|---|
| 20 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #include <cassert>
 | 
|---|
| 23 | #include <gsl/gsl_permutation.h>
 | 
|---|
| 24 | 
 | 
|---|
| 25 | /** Constructor for class LinearSystemOfEquations.
 | 
|---|
| 26 |  * Allocates Vector and Matrix classes.
 | 
|---|
| 27 |  * \param m column dimension
 | 
|---|
| 28 |  * \param n row dimension
 | 
|---|
| 29 |  */
 | 
|---|
| 30 | LinearSystemOfEquations::LinearSystemOfEquations(int m, int n) : rows(m), columns(n), IsSymmetric(false)
 | 
|---|
| 31 | {
 | 
|---|
| 32 |   A = new GSLMatrix(m, n);
 | 
|---|
| 33 |   b = new GSLVector(m);
 | 
|---|
| 34 |   x = new GSLVector(n);
 | 
|---|
| 35 | };
 | 
|---|
| 36 | 
 | 
|---|
| 37 | /** Destructor for class LinearSystemOfEquations.
 | 
|---|
| 38 |  * Destructs Vector and Matrix classes.
 | 
|---|
| 39 |  */
 | 
|---|
| 40 | LinearSystemOfEquations::~LinearSystemOfEquations()
 | 
|---|
| 41 | {
 | 
|---|
| 42 |   delete(A);
 | 
|---|
| 43 |   delete(b);
 | 
|---|
| 44 |   delete(x);
 | 
|---|
| 45 | };
 | 
|---|
| 46 | 
 | 
|---|
| 47 | /** Sets whether matrix is to be regarded as symmetric.
 | 
|---|
| 48 |  * Note that we do not check whether it really is, just take upper diagonal.
 | 
|---|
| 49 |  * \param symmetric true or false
 | 
|---|
| 50 |  */
 | 
|---|
| 51 | bool LinearSystemOfEquations::SetSymmetric(bool symmetric)
 | 
|---|
| 52 | {
 | 
|---|
| 53 |   assert (rows == columns && "Rows and columns don't have equal size! Setting symmetric not possible.");
 | 
|---|
| 54 |   return (IsSymmetric = symmetric);
 | 
|---|
| 55 | };
 | 
|---|
| 56 | 
 | 
|---|
| 57 | /** Initializes vector b to the components of the given vector.
 | 
|---|
| 58 |  * \param *x Vector with equal dimension (no check!)
 | 
|---|
| 59 |  */
 | 
|---|
| 60 | void LinearSystemOfEquations::Setb(Vector *x)
 | 
|---|
| 61 | {
 | 
|---|
| 62 |   assert ( columns == NDIM && "Vector class is always three-dimensional, unlike this LEqS!");
 | 
|---|
| 63 |   b->SetFromVector(*x);
 | 
|---|
| 64 | };
 | 
|---|
| 65 | 
 | 
|---|
| 66 | /** Initializes vector b to the components of the given vector.
 | 
|---|
| 67 |  * \param *x array with equal dimension (no check!)
 | 
|---|
| 68 |  */
 | 
|---|
| 69 | void LinearSystemOfEquations::Setb(double *x)
 | 
|---|
| 70 | {
 | 
|---|
| 71 |   b->SetFromDoubleArray(x);
 | 
|---|
| 72 | };
 | 
|---|
| 73 | 
 | 
|---|
| 74 | /** Initializes matrix a to the components of the given array.
 | 
|---|
| 75 |  * note that sort order should be
 | 
|---|
| 76 |  * \param *x array with equal dimension (no check!)
 | 
|---|
| 77 |  */
 | 
|---|
| 78 | void LinearSystemOfEquations::SetA(double *x)
 | 
|---|
| 79 | {
 | 
|---|
| 80 |   A->SetFromDoubleArray(x);
 | 
|---|
| 81 | };
 | 
|---|
| 82 | 
 | 
|---|
| 83 | /** Returns the solution vector x \f$A \cdot x = b\f$ as an array of doubles.
 | 
|---|
| 84 |  * \param *array pointer allocated array for solution on return (no bounds check, dimension must match)
 | 
|---|
| 85 |  * \return true - solving possible, false - some error occured.
 | 
|---|
| 86 |  */
 | 
|---|
| 87 | bool LinearSystemOfEquations::GetSolutionAsArray(double *&array)
 | 
|---|
| 88 | {
 | 
|---|
| 89 |   bool status = Solve();
 | 
|---|
| 90 | 
 | 
|---|
| 91 |   // copy solution
 | 
|---|
| 92 |   for (size_t i=0;i<x->dimension;i++) {
 | 
|---|
| 93 |     array[i] = x->Get(i);
 | 
|---|
| 94 |   }
 | 
|---|
| 95 |   return status;
 | 
|---|
| 96 | };
 | 
|---|
| 97 | 
 | 
|---|
| 98 | /** Returns the solution vector x \f$A \cdot x = b\f$ as an array of doubles.
 | 
|---|
| 99 |  * \param &x solution vector on return (must be 3-dimensional)
 | 
|---|
| 100 |  * \return true - solving possible, false - some error occured.
 | 
|---|
| 101 |  */
 | 
|---|
| 102 | bool LinearSystemOfEquations::GetSolutionAsVector(Vector &v)
 | 
|---|
| 103 | {
 | 
|---|
| 104 |   assert(rows == NDIM && "Solution can only be returned as vector if number of columns is NDIM.");
 | 
|---|
| 105 |   bool status = Solve();
 | 
|---|
| 106 | 
 | 
|---|
| 107 |   // copy solution
 | 
|---|
| 108 |   for (size_t i=0;i<x->dimension;i++)
 | 
|---|
| 109 |     v[i] = x->Get(i);
 | 
|---|
| 110 |   return status;
 | 
|---|
| 111 | };
 | 
|---|
| 112 | 
 | 
|---|
| 113 | /** Solves the given system of \f$A \cdot x = b\f$.
 | 
|---|
| 114 |  * Use either LU or Householder decomposition.
 | 
|---|
| 115 |  * Solution is stored in LinearSystemOfEquations::x
 | 
|---|
| 116 |  * \return true - solving possible, false - some error occured.
 | 
|---|
| 117 |  */
 | 
|---|
| 118 | bool LinearSystemOfEquations::Solve()
 | 
|---|
| 119 | {
 | 
|---|
| 120 |   // calculate solution
 | 
|---|
| 121 |   int s;
 | 
|---|
| 122 |   if (IsSymmetric) { // use LU
 | 
|---|
| 123 |     gsl_permutation * p = gsl_permutation_alloc (x->dimension);
 | 
|---|
| 124 |     gsl_linalg_LU_decomp (A->matrix, p, &s);
 | 
|---|
| 125 |     gsl_linalg_LU_solve (A->matrix, p, b->vector, x->vector);
 | 
|---|
| 126 |     gsl_permutation_free (p);
 | 
|---|
| 127 |   } else {  // use Householder
 | 
|---|
| 128 |     //GSLMatrix *backup = new GSLMatrix(rows,columns);
 | 
|---|
| 129 |     //*backup = *A;
 | 
|---|
| 130 |     gsl_linalg_HH_solve (A->matrix, b->vector, x->vector);
 | 
|---|
| 131 |     //*A = *backup;
 | 
|---|
| 132 |     //delete(backup);
 | 
|---|
| 133 |   }
 | 
|---|
| 134 |   return true;
 | 
|---|
| 135 | };
 | 
|---|
| 136 | 
 | 
|---|