| Rev | Line | |
|---|
| [48b662] | 1 | /**
|
|---|
| 2 | * @file givens.hpp
|
|---|
| 3 | * @author Julian Iseringhausen <isering@ins.uni-bonn.de>
|
|---|
| 4 | * @date Mon Apr 18 13:10:15 2011
|
|---|
| 5 | *
|
|---|
| 6 | * @brief Solves system of linear equations using Givens rotations.
|
|---|
| 7 | * Compare to Meister, Numerik lineare Gleichungssysteme.
|
|---|
| 8 | *
|
|---|
| 9 | */
|
|---|
| 10 |
|
|---|
| 11 | #ifndef GIVENS_HPP_
|
|---|
| 12 | #define GIVENS_HPP_
|
|---|
| 13 |
|
|---|
| 14 | #include <cfloat>
|
|---|
| 15 |
|
|---|
| 16 | #include "solver/solver.hpp"
|
|---|
| 17 |
|
|---|
| 18 | namespace VMG
|
|---|
| 19 | {
|
|---|
| 20 |
|
|---|
| 21 | template<class T>
|
|---|
| 22 | class Givens : public T
|
|---|
| 23 | {
|
|---|
| [ac6d04] | 24 | public:
|
|---|
| 25 | Givens() :
|
|---|
| 26 | T()
|
|---|
| 27 | {}
|
|---|
| 28 |
|
|---|
| 29 | Givens(int size) :
|
|---|
| 30 | T(size)
|
|---|
| 31 | {}
|
|---|
| 32 |
|
|---|
| 33 | protected:
|
|---|
| [48b662] | 34 | void Compute();
|
|---|
| 35 | };
|
|---|
| 36 |
|
|---|
| 37 | template<class T>
|
|---|
| 38 | void Givens<T>::Compute()
|
|---|
| 39 | {
|
|---|
| 40 | int n = this->Size();
|
|---|
| 41 | vmg_float c,s,t;
|
|---|
| 42 |
|
|---|
| 43 | for (int i=0; i<n-1; i++)
|
|---|
| 44 | for (int j=i+1; j<n; j++)
|
|---|
| 45 | if (fabs(this->Mat(j,i)) > DBL_EPSILON) {
|
|---|
| 46 |
|
|---|
| 47 | t = 1.0 / sqrt(this->Mat(i,i)*this->Mat(i,i) + this->Mat(j,i)*this->Mat(j,i));
|
|---|
| 48 | s = t * this->Mat(j,i);
|
|---|
| 49 | c = t * this->Mat(i,i);
|
|---|
| 50 |
|
|---|
| 51 | for (int k=i; k<n; k++) {
|
|---|
| 52 |
|
|---|
| 53 | t = c * this->Mat(i,k) + s * this->Mat(j,k);
|
|---|
| 54 |
|
|---|
| 55 | if (k != i)
|
|---|
| 56 | this->Mat(j,k) = c * this->Mat(j,k) - s * this->Mat(i,k);
|
|---|
| 57 |
|
|---|
| 58 | this->Mat(i,k) = t;
|
|---|
| 59 |
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | t = c * this->Rhs(i) + s * this->Rhs(j);
|
|---|
| 63 |
|
|---|
| 64 | this->Rhs(j) = c * this->Rhs(j) - s * this->Rhs(i);
|
|---|
| 65 |
|
|---|
| 66 | this->Rhs(i) = t;
|
|---|
| 67 |
|
|---|
| 68 | this->Mat(j,i) = 0.0;
|
|---|
| 69 |
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | for (int i=n-1; i>=0; i--) {
|
|---|
| 73 |
|
|---|
| 74 | for (int j=i+1; j<n; j++)
|
|---|
| 75 | this->Rhs(i) -= this->Mat(i,j) * this->Sol(j);
|
|---|
| 76 |
|
|---|
| 77 | this->Sol(i) = this->Rhs(i) / this->Mat(i,i);
|
|---|
| 78 |
|
|---|
| 79 | }
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | #endif /* GIVENS_HPP_ */
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.