LU decomposition: Difference between revisions
m
C++ - pretty print the output
(C++ bug fixed, added another test case) |
m (C++ - pretty print the output) |
||
Line 611:
#include <iostream>
#include <numeric>
#include <sstream>
#include <vector>
Line 653 ⟶ 654:
template <typename scalar_type>
void print(std::
const wchar_t* box_top_left = L"\x23a1";
const wchar_t* box_top_right = L"\x23a4";
const wchar_t* box_left = L"\x23a2";
const wchar_t* box_right = L"\x23a5";
const wchar_t* box_bottom_left = L"\x23a3";
const wchar_t* box_bottom_right = L"\x23a6";
const int precision = 5;
size_t rows = a.rows(), columns = a.columns();
for (size_t column = 0; column < columns; ++column) {
size_t max_width = 0;
for (size_t row = 0; row < rows; ++row) {
std::ostringstream str;
str << std::fixed << std::setprecision(precision) << a(row, column);
max_width = std::max(max_width, str.str().length());
}
width[column] = max_width;
}
out << std::fixed << std::setprecision(precision);
for (size_t row = 0; row < rows; ++row) {
const bool top(row == 0), bottom(row + 1 == rows);
out << (top ? box_top_left : (bottom ? box_bottom_left : box_left));
for (size_t column = 0; column < columns; ++column) {
if (column > 0)
out << L' ';
out << std::setw(
}
out <<
out << L'\n';
}
}
Line 708 ⟶ 730:
pivot(i, perm[i]) = 1;
std::
print(std::
std::
print(std::
std::
print(std::
std::
print(std::
}
int main() {
std::
std::wcout << L"Example 1:\n";
matrix<double> matrix1(3, 3,
{{1, 3, 5},
Line 725 ⟶ 748:
{1, 1, 0}});
lu_decompose(matrix1);
std::
std::
matrix<double> matrix2(4, 4,
{{11, 9, 24, 2},
Line 734 ⟶ 757:
{2, 5, 7, 1}});
lu_decompose(matrix2);
std::
std::
matrix<double> matrix3(3, 3,
{{-5, -6, -3},
Line 750 ⟶ 773:
Example 1:
A
L
U
P
Example 2:
A
L
U
P
Example 3:
A
⎡-5.00000 -6.00000 -3.
⎢-1.00000 0.00000 -2.
⎣-3.00000 -4.00000 -7.
L
U
⎡-5.00000 -6.00000 -3.
P
</pre>
|