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::ostreamwostream& out, const matrix<scalar_type>& a) {
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();
out << std::fixed <vector<size_t> std::setprecisionwidth(5columns);
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(8width[column]) << a(row, column);
}
out << '\n'(top ? box_top_right : (bottom ? box_bottom_right : box_right));
out << L'\n';
}
}
Line 708 ⟶ 730:
pivot(i, perm[i]) = 1;
 
std::coutwcout << L"A\n";
print(std::coutwcout, input);
std::coutwcout << L"\nL\n";
print(std::coutwcout, lower);
std::coutwcout << L"\nU\n";
print(std::coutwcout, upper);
std::coutwcout << L"\nP\n";
print(std::coutwcout, pivot);
}
 
int main() {
std::cout << "Example 1wcout.imbue(std:\n:locale(""));
std::wcout << L"Example 1:\n";
matrix<double> matrix1(3, 3,
{{1, 3, 5},
Line 725 ⟶ 748:
{1, 1, 0}});
lu_decompose(matrix1);
std::coutwcout << '\n';
 
std::coutwcout << L"Example 2:\n";
matrix<double> matrix2(4, 4,
{{11, 9, 24, 2},
Line 734 ⟶ 757:
{2, 5, 7, 1}});
lu_decompose(matrix2);
std::coutwcout << '\n';
std::coutwcout << L"Example 3:\n";
matrix<double> matrix3(3, 3,
{{-5, -6, -3},
Line 750 ⟶ 773:
Example 1:
A
1⎡1.00000 3.00000 5.0000000000⎤
2⎢2.00000 4.00000 7.0000000000⎥
1⎣1.00000 1.00000 0.0000000000⎦
 
L
1⎡1.00000 0.00000 0.0000000000⎤
0⎢0.50000 1.00000 0.0000000000⎥
0⎣0.50000 -1.00000 1.0000000000⎦
 
U
2⎡2.00000 4.00000 7.0000000000⎤
0⎢0.00000 1.00000 1.5000050000⎥
0⎣0.00000 0.00000 -2.0000000000⎦
 
P
0⎡0.00000 1.00000 0.0000000000⎤
1⎢1.00000 0.00000 0.0000000000⎥
0⎣0.00000 0.00000 1.0000000000⎦
 
Example 2:
A
11⎡11.00000 9.00000 24.00000 2.0000000000⎤
1.00000 5.00000 2.00000 6.0000000000⎥
3.00000 17.00000 18.00000 1.0000000000⎥
2.00000 5.00000 7.00000 1.0000000000⎦
 
L
1⎡1.00000 0.00000 0.00000 0.0000000000⎤
0⎢0.27273 1.00000 0.00000 0.0000000000⎥
0⎢0.09091 0.28750 1.00000 0.0000000000⎥
0⎣0.18182 0.23125 0.00360 1.0000000000⎦
 
U
11⎡11.00000 9.00000 24.00000 2.0000000000⎤
0.00000 14.54545 11.45455 0.4545545455⎥
0.00000 0.00000 -3.47500 5.6875068750⎥
0.00000 0.00000 0.00000 0.5107951079⎦
 
P
1⎡1.00000 0.00000 0.00000 0.0000000000⎤
0⎢0.00000 0.00000 1.00000 0.0000000000⎥
0⎢0.00000 1.00000 0.00000 0.0000000000⎥
0⎣0.00000 0.00000 0.00000 1.0000000000⎦
 
Example 3:
A
-5.00000 -6.00000 -3.0000000000⎤
-1.00000 0.00000 -2.0000000000⎥
-3.00000 -4.00000 -7.0000000000⎦
 
L
1⎡1.00000 0.00000 0.0000000000⎤
0⎢0.20000 1.00000 0.0000000000⎥
0⎣0.60000 -0.33333 1.0000000000⎦
 
U
-5.00000 -6.00000 -3.0000000000⎤
0.00000 1.20000 -1.4000040000⎥
0.00000 0.00000 -5.6666766667⎦
 
P
1⎡1.00000 0.00000 0.0000000000⎤
0⎢0.00000 1.00000 0.0000000000⎥
0⎣0.00000 0.00000 1.0000000000⎦
</pre>
 
1,777

edits