Singular value decomposition: Difference between revisions

Content added Content deleted
(add RPL)
(Added alternative C++ solution)
Line 179: Line 179:
|-0.7071067812 0.7071067812 |
|-0.7071067812 0.7071067812 |


</pre>

===Alternative using Eigen===
{{libheader|Eigen}}
<syntaxhighlight lang="C++">
#include <Eigen/Dense>
#include <cstdlib>
#include <iomanip>
#include <iostream>

int main() {
using namespace Eigen;
int rows, columns;
if (!(std::cin >> rows >> columns))
return EXIT_FAILURE;
MatrixXd matrix(rows, columns);
for (int row = 0; row < rows; ++row) {
for (int column = 0; column < columns; ++column) {
if (!(std::cin >> matrix(row, column)))
return EXIT_FAILURE;
}
}
auto svd = matrix.bdcSvd().compute(matrix, ComputeFullU | ComputeFullV);
std::cout << std::setprecision(15) << std::fixed << svd.matrixU() << "\n\n"
<< svd.singularValues() << "\n\n"
<< svd.matrixV() << '\n';
return EXIT_SUCCESS;
}
</syntaxhighlight>

{{out}}
<pre>
0.316227766016838 0.948683298050514
0.948683298050514 -0.316227766016838

6.708203932499368
2.236067977499789

0.707106781186547 0.707106781186548
0.707106781186548 -0.707106781186547
</pre>
</pre>