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