7 #ifndef __SparseMatrix2x2_h__ 8 #define __SparseMatrix2x2_h__ 16 class FE_DL_EXPORT SparseMatrix2x2 :
public Component
20 typedef Vector<2, t_real> t_v2;
21 typedef Matrix<2,2,t_real> t_matrix;
23 virtual ~SparseMatrix2x2(
void) {}
24 virtual void multiply(std::vector<t_v2> &a_b,
25 const std::vector<t_v2> &a_x)
const = 0;
26 virtual void scale(t_real a_scale) = 0;
31 class FE_DL_EXPORT FullBCRS2 :
public SparseMatrix2x2<T>
35 typedef Vector<2, t_real> t_v2;
36 typedef Matrix<2,2,t_real> t_matrix;
38 virtual ~FullBCRS2(
void);
41 virtual void multiply(std::vector<t_v2> &a_b,
42 const std::vector<t_v2> &a_x)
const;
43 virtual void scale(t_real a_scale);
47 virtual void done(
void);
48 t_matrix &insert(
unsigned int a_columnIndex,
49 const t_matrix &a_block);
51 void sparse(
const sp<FullBCRS2> &a_other);
52 void project(
const sp<FullBCRS2> &a_other);
55 std::vector<t_matrix> &blocks(
void) {
return m_blocks; }
56 std::vector<unsigned int> &colind(
void) {
return m_colind; }
57 std::vector<unsigned int> &rowptr(
void) {
return m_rowptr; }
60 std::vector<t_matrix> m_blocks;
61 std::vector<unsigned int> m_colind;
62 std::vector<unsigned int> m_rowptr;
68 class FE_DL_EXPORT UpperTriangularBCRS2 :
public FullBCRS2<T>
72 typedef Vector<2, t_real> t_v2;
73 typedef Matrix<2,2,t_real> t_matrix;
74 UpperTriangularBCRS2(
void){}
75 virtual ~UpperTriangularBCRS2(
void){}
77 virtual void multiply(std::vector<t_v2> &a_b,
78 const std::vector<t_v2> &a_x)
const;
80 void incompleteSqrt(
void);
81 void incompleteSqrtOpt(
void);
82 void backSub(std::vector<t_v2> &a_x,
const std::vector<t_v2> &a_b);
83 void transposeForeSub(std::vector<t_v2> &a_x,
const std::vector<t_v2> &a_b);
84 void backSolve(std::vector<t_v2> &a_x,
const std::vector<t_v2> &a_b);
85 virtual void done(
void);
88 std::vector<unsigned int> m_ptr;
89 std::vector<t_v2> m_y;
96 inline FullBCRS2<T>::FullBCRS2(
void)
99 m_zeroVector = t_v2(0.0,0.0);
102 template <
typename T>
103 inline FullBCRS2<T>::~FullBCRS2(
void)
107 template <
typename T>
108 inline void FullBCRS2<T>::clear(
void)
116 template <
typename T>
117 inline void FullBCRS2<T>::startrow(
void)
119 m_rowptr.push_back(m_r);
122 template <
typename T>
123 inline void FullBCRS2<T>::done(
void)
125 m_rowptr.push_back(m_r);
128 template <
typename T>
129 inline void UpperTriangularBCRS2<T>::done(
void)
131 m_y.resize(FullBCRS2<T>::m_rowptr.size());
132 FullBCRS2<T>::m_rowptr.push_back(FullBCRS2<T>::m_r);
133 m_ptr.resize(FullBCRS2<T>::m_rowptr.size());
136 template <
typename T>
137 inline Matrix<2,2,T> &FullBCRS2<T>::insert(
unsigned int a_columnIndex,
const t_matrix &a_block)
139 m_blocks.push_back(a_block);
140 m_colind.push_back(a_columnIndex);
142 return m_blocks.back();
145 template <
typename T>
146 inline void FullBCRS2<T>::multiply(std::vector<t_v2> &a_b,
const std::vector<t_v2> &a_x)
const 148 unsigned int n = (
unsigned int)(m_rowptr.size() - 1);
149 for(
unsigned int i = 0; i < n; i++)
151 a_b[i] = m_zeroVector;
152 for(
unsigned int k = m_rowptr[i]; k < m_rowptr[i+1]; k++)
154 a_b[i] += fe::multiply(m_blocks[k], a_x[m_colind[k]]);
159 template <
typename T>
160 inline void FullBCRS2<T>::scale(t_real a_scale)
162 unsigned int n = (
unsigned int)m_blocks.size();
163 for(
unsigned int i = 0; i < n; i++)
165 m_blocks[i] *= a_scale;
169 template <
typename T>
170 inline void UpperTriangularBCRS2<T>::multiply(std::vector<t_v2> &a_b,
const std::vector<t_v2> &a_x)
const 172 unsigned int n = (
unsigned int)(FullBCRS2<T>::m_rowptr.size() - 1);
173 for(
unsigned int i = 0; i < n; i++)
175 a_b[i] = FullBCRS2<T>::m_zeroVector;
177 for(
unsigned int i = 0; i < n; i++)
179 unsigned int k = FullBCRS2<T>::m_rowptr[i];
180 a_b[i] += fe::multiply(FullBCRS2<T>::m_blocks[k], a_x[i]);
181 for(k=k+1; k < FullBCRS2<T>::m_rowptr[i+1]; k++)
183 a_b[i] += fe::multiply(FullBCRS2<T>::m_blocks[k], a_x[FullBCRS2<T>::m_colind[k]]);
184 a_b[FullBCRS2<T>::m_colind[k]] += fe::transposeMultiply(FullBCRS2<T>::m_blocks[k], a_x[i]);
189 template <
typename T>
190 inline void just_upper(Matrix<2,2,T> &a_m)
196 template <
typename T>
197 inline void FullBCRS2<T>::sparse(
const sp<FullBCRS2> &a_other)
205 unsigned int n = a_other->m_rowptr.size()-1;
206 for(
unsigned int i = 0; i < n; i++)
209 int d = a_other->m_rowptr[i];
210 for(
int k=d; k < a_other->m_rowptr[i+1]; k++)
212 int c = a_other->m_colind[k];
213 if(!(zero == a_other->m_blocks[k]))
215 insert(c, a_other->m_blocks[k]);
222 template <
typename T>
225 unsigned int n = a_other->m_rowptr.size()-1;
226 for(
unsigned int i = 0; i < n; i++)
228 int d = a_other->m_rowptr[i];
229 int d_self = m_rowptr[i];
230 for(
int k=d; k < a_other->m_rowptr[i+1]; k++)
232 int c = a_other->m_colind[k];
233 for(
int k_self=d_self; k_self < m_rowptr[i+1]; k_self++)
235 int c_self = m_colind[k_self];
238 m_blocks[k_self] = a_other->m_blocks[k];
245 template <
typename T>
246 inline void UpperTriangularBCRS2<T>::backSub(std::vector<t_v2> &a_x,
const std::vector<t_v2> &a_b)
252 unsigned int n = (
unsigned int)(FullBCRS2<T>::m_rowptr.size()-1);
254 fe::backSub(FullBCRS2<T>::m_blocks[FullBCRS2<T>::m_rowptr[n-1]], a_x[n-1], a_b[n-1]);
257 for(
int i = n-2; i >= 0; i--)
261 int d = FullBCRS2<T>::m_rowptr[i];
262 for(
unsigned int k=d+1; k < FullBCRS2<T>::m_rowptr[i+1]; k++)
264 int c = FullBCRS2<T>::m_colind[k];
265 fe::multiply(tmp, FullBCRS2<T>::m_blocks[k], a_x[c]);
268 if(! (zero == m_blocks[k]))
270 fe_fprintf(stderr,
"bs c %d -- %d <- T %d\n", c, i, k);
271 fprint(stderr, m_blocks[k]);
272 fe_fprintf(stderr,
"\n");
277 fe::backSub(FullBCRS2<T>::m_blocks[d], a_x[i], a_x[i]);
284 template <
typename T>
285 inline void UpperTriangularBCRS2<T>::transposeForeSub(std::vector<t_v2> &a_x,
const std::vector<t_v2> &a_b)
289 unsigned int n = (
unsigned int)(FullBCRS2<T>::m_rowptr.size()-1);
291 for(
unsigned int i = 0 ; i < FullBCRS2<T>::m_rowptr.size(); i++)
293 m_ptr[i] = FullBCRS2<T>::m_rowptr[i];
295 fe::transposeForeSub(FullBCRS2<T>::m_blocks[FullBCRS2<T>::m_rowptr[0]], a_x[0], a_b[0]);
298 for(
unsigned int i = 1; i < n; i++)
302 for(
unsigned int m = 0; m < i; m++)
304 unsigned int c = FullBCRS2<T>::m_colind[m_ptr[m]];
305 while(c < i && m_ptr[m] < FullBCRS2<T>::m_rowptr[m+1]-1)
308 c = FullBCRS2<T>::m_colind[m_ptr[m]];
320 a_x[i] -= fe::transposeMultiply(FullBCRS2<T>::m_blocks[m_ptr[m]], a_x[m]);
324 fe::transposeForeSub(FullBCRS2<T>::m_blocks[FullBCRS2<T>::m_rowptr[i]], a_x[i], a_x[i]);
330 template <
typename T>
331 inline void UpperTriangularBCRS2<T>::backSolve(std::vector<t_v2> &a_x,
const std::vector<t_v2> &a_b)
335 transposeForeSub(m_y, a_b);
340 template <
typename T>
341 inline void UpperTriangularBCRS2<T>::incompleteSqrt(
void)
344 unsigned int n = (
unsigned int)(FullBCRS2<T>::m_rowptr.size()-1);
347 for(
unsigned int k = 0; k < n-1; k++)
349 d = FullBCRS2<T>::m_rowptr[k];
350 just_upper(FullBCRS2<T>::m_blocks[d]);
354 squareroot(FullBCRS2<T>::m_blocks[d], FullBCRS2<T>::m_blocks[d]);
356 z = FullBCRS2<T>::m_blocks[d];
366 for(
unsigned int i=d+1; i < FullBCRS2<T>::m_rowptr[k+1]; i++)
374 fe::multiply(z, FullBCRS2<T>::m_blocks[i], inv_z);
382 FullBCRS2<T>::m_blocks[i] = z;
385 for(
unsigned int i=d+1; i < FullBCRS2<T>::m_rowptr[k+1]; i++)
387 z = transpose(FullBCRS2<T>::m_blocks[i]);
388 unsigned int h = FullBCRS2<T>::m_colind[i];
393 for(
unsigned int j = FullBCRS2<T>::m_rowptr[h]; j < FullBCRS2<T>::m_rowptr[h+1]; j++)
395 for(; g < FullBCRS2<T>::m_rowptr[k+1] && FullBCRS2<T>::m_colind[g] <= FullBCRS2<T>::m_colind[j]; g++)
397 if (FullBCRS2<T>::m_colind[g] == FullBCRS2<T>::m_colind[j])
400 fe::multiply(t, z, FullBCRS2<T>::m_blocks[g]);
402 subtract(FullBCRS2<T>::m_blocks[j], FullBCRS2<T>::m_blocks[j], t);
406 const t_matrix &A = z;
407 const t_matrix &B = FullBCRS2<T>::m_blocks[g];
408 for(
unsigned int ii = 0;ii < 2; ii++)
410 for(
unsigned int jj = 0;jj < 2; jj++)
413 sum += A(ii,0)*B(0,jj);
414 sum += A(ii,1)*B(1,jj);
419 t_matrix &R = FullBCRS2<T>::m_blocks[j];
420 R(0,0) = R(0,0)-t(0,0);
421 R(0,1) = R(0,1)-t(1,0);
422 R(1,0) = R(1,0)-t(0,1);
423 R(1,1) = R(1,1)-t(1,1);
430 d = FullBCRS2<T>::m_rowptr[n-1];
432 just_upper(FullBCRS2<T>::m_blocks[d]);
434 FullBCRS2<T>::m_blocks[d] = tmp;
kernel
Definition: namespace.dox:3
Matrix< 4, 4, T > & invert(Matrix< 4, 4, T > &a_inverted, const Matrix< 4, 4, T > &a_matrix)
4x4 full matrix inversion
Definition: Matrix.h:1033
void project(Vector< 4, T > &a_r, const Matrix< 4, 4, T > &a_m, const Vector< 4, T > &a_v)
project vector through matrix. divides by transformed w
Definition: Matrix.h:1182
void squareroot(Matrix< M, N, T > &a_U, const Matrix< M, N, T > &a_A)
Square root of a matrix.
Definition: Matrix.h:567