5 #ifndef SPIDAR_GAUSSIAN_ELIMINATION_HPP 6 #define SPIDAR_GAUSSIAN_ELIMINATION_HPP 20 template <
class MatrixType,
class VectorType>
21 static void solve(MatrixType& a, VectorType& b, VectorType& x);
29 template <
class MatrixType,
class VectorType>
32 static_assert(MatrixType::row==MatrixType::column,
"row != column");
33 static_assert(MatrixType::row==VectorType::size,
"matrix size != vector size");
35 const size_t size = VectorType::size;
38 for (
size_t k=0; k<size-1; ++k)
40 for (
size_t j=k+1; j<size; ++j)
46 for (
size_t i=k+1; i<size; ++i)
48 for (
size_t j=k+1; j<size; ++j)
50 a[i][j] -= a[i][k] * a[k][j];
52 b[i] -= a[i][k] * b[k];
57 x[size-1] = b[size-1] / a[size-1][size-1];
59 for (
size_t k=size-2; k<size; --k)
63 for (
size_t j=k+1; j<size; ++j)
65 x[k] -= a[k][j] * x[j];
73 #endif // SPIDAR_GAUSSIAN_ELIMINATION_HPP
static void solve(MatrixType &a, VectorType &b, VectorType &x)
ガウスの消去法を用いて連立一次方程式を解きます.