API Documentation
Loading...
Searching...
No Matches
BandMatrix.hpp
Go to the documentation of this file.
1#pragma once
2#include <NDEVR/Buffer.h>
3#include <NDEVR/Vertex.h>
4namespace NDEVR
5{
6 /**--------------------------------------------------------------------------------------------------
7 \brief Band Matrix solver
8
9 **/
10 template<class t_type>
12 {
13 private:
14 Buffer<Buffer<t_type>> m_upper; // upper band
15 Buffer<Buffer<t_type>> m_lower; // lower band
16 public:
17 BandMatrix() {}; // constructor
18 ~BandMatrix() {}; // matrix dimension
20 {
21 return m_upper.size() - 1;
22 }
24 {
25 return m_lower.size() - 1;
26 }
28 {
29 resize(dim, n_u, n_l);
30 }
31 void resize(uint04 dim, uint04 n_u, uint04 n_l)
32 {
33 lib_assert(dim > 0U, "dim > 0");
34 m_upper.setSize(n_u + 1);
35 m_lower.setSize(n_l + 1);
36 for (uint04 i = 0; i < m_upper.size(); i++)
37 m_upper[i].setSize(dim);
38 for (uint04 i = 0; i < m_lower.size(); i++)
39 m_lower[i].setSize(dim);
40 }
41 uint04 dim() const
42 {
43 if (m_upper.size() > 0)
44 return m_upper[0].size();
45 else
46 return 0;
47 }
48 // defines the new operator (), so that we can access the elements
49 // by A(i,j), index going from i=0,...,dim()-1
51 {
52 if (j >= i)
53 return m_upper[j - i][i];
54 else
55 return m_lower[i - j][i];
56 }
57 t_type operator() (uint04 i, uint04 j) const
58 {
59 if (j >= i)
60 return m_upper[j - i][i];
61 else
62 return m_lower[i - j][i];
63 }
64 // second diag (used in LU decomposition), saved in m_lower
65 t_type savedDiag(uint04 i) const
66 {
67 lib_assert(i < dim(), "Bad band location");
68 return m_lower[0][i];
69 }
70 t_type& savedDiag(uint04 i)
71 {
72 lib_assert(i < dim(), "Bad band location");
73 return m_lower[0][i];
74 }
75
76 // LR-Decomposition of a band matrix
78 {
79 for (uint04 i = 0; i < dim(); i++)
80 {
81 lib_assert((*this)(i, i) != 0.0, "Bad decompision");
82 savedDiag(i) = 1.0 / (*this)(i, i);
83 uint04 j_min = getMax(0U, i - lowerCount());
84 uint04 j_max = getMin(dim() - 1, i + upperCount());
85 for (uint04 j = j_min; j <= j_max; j++)
86 (*this)(i, j) *= savedDiag(i);
87 (*this)(i, i) = 1.0; // prevents rounding errors
88 }
89
90 // Gauss LR-Decomposition
91 for (uint04 k = 0; k < dim(); k++)
92 {
93 uint04 i_max = getMin(dim() - 1, k + lowerCount()); // lowerCount not a mistake!
94 for (uint04 i = k + 1; i <= i_max; i++)
95 {
96 lib_assert((*this)(k, k) != 0.0, "get minimum");
97 t_type x = -(*this)(i, k) / (*this)(k, k);
98 (*this)(i, k) = -x; // assembly part of L
99 uint04 j_max = getMin(dim() - 1, k + upperCount());
100 for (uint04 j = k + 1; j <= j_max; j++)
101 {
102 // assembly part of R
103 (*this)(i, j) = (*this)(i, j) + x * (*this)(k, j);
104 }
105 }
106 }
107 }
108 // solves Ly=b
109 template<class t_l_type>
111 {
112 lib_assert(dim() == b.size(), "Bad size");
114 x.setSize(dim());
115 for (uint04 i = 0; i < dim(); i++)
116 {
117 t_l_type sum(0);
118 uint04 j_start = getMax(0U, i - lowerCount());
119 for (uint04 j = j_start; j < i; j++)
120 sum += (*this)(i, j) * x[j];
121 x[i] = (b[i] * savedDiag(i)) - sum;
122 }
123 return x;
124 }
125 // solves Rx=y
126 template<class t_r_type>
128 {
129 lib_assert(dim() == b.size(), "dimensions not equal");
131 x.setSize(dim());
132 for (uint04 i = dim() - 1; !IsInvalid(i); i--)
133 {
134 t_r_type sum(0);
135 uint04 j_stop = getMin(dim() - 1, i + upperCount());
136 for (uint04 j = i + 1; j <= j_stop; j++)
137 sum += (*this)(i, j) * x[j];
138 x[i] = (b[i] - sum) / (*this)(i, i);
139 }
140 return x;
141 }
142 template<class t_l_type>
143 Buffer<t_l_type> luSolve(const Buffer<t_l_type>& b, bool is_lu_decomposed = false)
144 {
145 lib_assert(dim() == b.size(), "Bad size");
146 if (!is_lu_decomposed)
147 luDecompose();
150 return x;
151 }
152 };
153}
#define lib_assert(expression, message)
Definition LibAssert.h:61
Band Matrix solver.
Definition BandMatrix.hpp:12
~BandMatrix()
Definition BandMatrix.hpp:18
t_type savedDiag(uint04 i) const
Definition BandMatrix.hpp:65
uint04 dim() const
Definition BandMatrix.hpp:41
uint04 upperCount() const
Definition BandMatrix.hpp:19
Buffer< t_l_type > luSolve(const Buffer< t_l_type > &b, bool is_lu_decomposed=false)
Definition BandMatrix.hpp:143
void luDecompose()
Definition BandMatrix.hpp:77
Buffer< t_r_type > rSolve(const Buffer< t_r_type > &b) const
Definition BandMatrix.hpp:127
t_type & operator()(uint04 i, uint04 j)
Definition BandMatrix.hpp:50
BandMatrix(uint04 dim, uint04 n_u, uint04 n_l)
Definition BandMatrix.hpp:27
BandMatrix()
Definition BandMatrix.hpp:17
uint04 lowerCount() const
Definition BandMatrix.hpp:23
Buffer< t_l_type > lSolve(const Buffer< t_l_type > &b) const
Definition BandMatrix.hpp:110
void resize(uint04 dim, uint04 n_u, uint04 n_l)
Definition BandMatrix.hpp:31
t_type & savedDiag(uint04 i)
Definition BandMatrix.hpp:70
The equivelent of std::vector but with a bit more control. The basic array unit of the library.
Definition Buffer.hpp:56
constexpr t_index_type size() const
Definition Buffer.hpp:823
void setSize(t_index_type new_size)
Definition Buffer.hpp:803
Definition ACIColor.h:37
constexpr bool IsInvalid(const t_type &value)
Query if 'value' is valid or invalid. Invalid values should return invalid if used for calculations o...
Definition BaseFunctions.hpp:170
constexpr t_type getMax(const t_type &left, const t_type &right)
Finds the max of the given arguments using the > operator The only requirement is that t_type have > ...
Definition BaseFunctions.hpp:94
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:96
constexpr t_type getMin(const t_type &left, const t_type &right)
Finds the minimum of the given arguments based on the < operator Author: Tyler Parke Date: 2017-11-05...
Definition BaseFunctions.hpp:56