LU decomposition: Difference between revisions
Content added Content deleted
m (C++ bug fix) |
m (C++ - added test for singular matrix) |
||
Line 610: | Line 610: | ||
#include <iomanip> |
#include <iomanip> |
||
#include <iostream> |
#include <iostream> |
||
#include <limits> |
|||
#include <numeric> |
#include <numeric> |
||
#include <sstream> |
#include <sstream> |
||
Line 708: | Line 709: | ||
} |
} |
||
} |
} |
||
if (max_value <= std::numeric_limits<scalar_type>::epsilon()) |
|||
throw std::runtime_error("matrix is singular"); |
|||
if (j != max_index) |
if (j != max_index) |
||
std::swap(perm[j], perm[max_index]); |
std::swap(perm[j], perm[max_index]); |
||
Line 736: | Line 739: | ||
template <typename scalar_type> |
template <typename scalar_type> |
||
void show_lu_decomposition(const matrix<scalar_type>& input) { |
void show_lu_decomposition(const matrix<scalar_type>& input) { |
||
try { |
|||
⚫ | |||
std::wcout << L"A\n"; |
std::wcout << L"A\n"; |
||
print(std::wcout, input); |
print(std::wcout, input); |
||
⚫ | |||
⚫ | |||
std::wcout << L"\nL\n"; |
|||
std::wcout < |
print(std::wcout, std::get<0>(result)); |
||
std::wcout << L"\nU\n"; |
|||
std::wcout < |
print(std::wcout, std::get<1>(result)); |
||
std::wcout << L"\nP\n"; |
|||
print(std::wcout, std::get<2>(result)); |
|||
} catch (const std::exception& ex) { |
|||
std::cerr << ex.what() << '\n'; |
|||
} |
|||
} |
} |
||
Line 772: | Line 779: | ||
{-3, -4, -7}}); |
{-3, -4, -7}}); |
||
show_lu_decomposition(matrix3); |
show_lu_decomposition(matrix3); |
||
⚫ | |||
std::wcout << L"Example 4:\n"; |
|||
matrix<double> matrix4(3, 3, |
|||
{{1, 2, 3}, |
|||
{4, 5, 6}, |
|||
{7, 8, 9}}); |
|||
show_lu_decomposition(matrix4); |
|||
return 0; |
return 0; |
||
Line 844: | Line 859: | ||
⎢0.00000 1.00000 0.00000⎥ |
⎢0.00000 1.00000 0.00000⎥ |
||
⎣0.00000 0.00000 1.00000⎦ |
⎣0.00000 0.00000 1.00000⎦ |
||
Example 4: |
|||
A |
|||
⎡1.00000 2.00000 3.00000⎤ |
|||
⎢4.00000 5.00000 6.00000⎥ |
|||
⎣7.00000 8.00000 9.00000⎦ |
|||
matrix is singular |
|||
</pre> |
</pre> |
||