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