Single header file solver for linear systems (
The square (row major) matrix is zero where no element is defined:
Lower/sub- (
With
If
The cyclic solver uses the property that the matrix can be wrttien as
Solve a cyclic tridiagonal matrix of order 5.
std::vector<std::double_t> upper{ 2, 3, 7, 2, 0 };
std::vector<std::double_t> main{ 2, 2, 8, 3, 5 };
std::vector<std::double_t> lower{ 0, 3, 2, 2, 7 };
std::double_t const alpha = 2;
std::double_t const beta = 3;
std::vector<std::double_t> y{ 6, -6, 1, 2, 3 };
std::vector<std::double_t> x = Tridiagonal::Solve( upper, main, lower, alpha, beta, y );
if ( !x.size() || !Tridiagonal::ValidSolution( upper, main, lower, alpha, beta, y, x ) )
std::cout << "No valid solution found.\n";
else {
std::cout << "Solution found:\n";
for ( std::uint32_t i{ 0 }; i < x.size(); ++i )
std::cout << "x[" << i + 1 << "] = " << x[i] << '\n';
}
Output:
Solution found:
x[1] = -3.48077
x[2] = 4.86923
x[3] = -1.76538
x[4] = 0.769231
x[5] = 1.61154
C++11