SPIDAR API Library  0x16033101
Space Interface Device for Artificial Reality
GaussianElimination.hpp
Go to the documentation of this file.
1 
5 #ifndef SPIDAR_GAUSSIAN_ELIMINATION_HPP
6 #define SPIDAR_GAUSSIAN_ELIMINATION_HPP
7 
8 #include <cmath>
9 
10 namespace Spidar
11 {
12 namespace Math
13 {
18 {
19 public:
20  template <class MatrixType, class VectorType>
21  static void solve(MatrixType& a, VectorType& b, VectorType& x);
22 
23 }; // end of class GaussianElimination.
24 
29 template <class MatrixType, class VectorType>
30 void GaussianElimination::solve(MatrixType& a, VectorType& b, VectorType& x)
31 {
32  static_assert(MatrixType::row==MatrixType::column, "row != column");
33  static_assert(MatrixType::row==VectorType::size, "matrix size != vector size");
34 
35  const size_t size = VectorType::size;
36 
37  // 前進消去 (forward elimination)
38  for (size_t k=0; k<size-1; ++k)
39  {
40  for (size_t j=k+1; j<size; ++j)
41  {
42  a[k][j] /= a[k][k];
43  }
44  b[k] /= a[k][k];
45 
46  for (size_t i=k+1; i<size; ++i)
47  {
48  for (size_t j=k+1; j<size; ++j)
49  {
50  a[i][j] -= a[i][k] * a[k][j];
51  }
52  b[i] -= a[i][k] * b[k];
53  }
54  }
55 
56  // 後退代入 (backward substitution)
57  x[size-1] = b[size-1] / a[size-1][size-1];
58 
59  for (size_t k=size-2; k<size; --k)
60  {
61  x[k] = b[k];
62 
63  for (size_t j=k+1; j<size; ++j)
64  {
65  x[k] -= a[k][j] * x[j];
66  }
67  }
68 }
69 
70 } // end of namespace Math.
71 } // end of namespace Spidar.
72 
73 #endif // SPIDAR_GAUSSIAN_ELIMINATION_HPP
74 
75 // end of file.
SPIDARライブラリのルート名前空間です.
ガウスの消去法のクラスです.
static void solve(MatrixType &a, VectorType &b, VectorType &x)
ガウスの消去法を用いて連立一次方程式を解きます.