Singular value decomposition: Difference between revisions

m
(add RPL)
m (→‎{{header|Wren}}: Minor tidy)
 
(One intermediate revision by one other user not shown)
Line 179:
|-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>
 
Line 1,127 ⟶ 1,167:
{{libheader|Wren-fmt}}
An embedded solution so we can use GSL to perform SVD on any m x n matrix though the example here is for a 2 x 2 matrix.
<syntaxhighlight lang="ecmascriptwren">/* svd_emdeddedSingular_value_decomposition.wren */
 
import "./fmt" for Fmt
Line 1,163 ⟶ 1,203:
 
We now embed this Wren script in the following C program, compile and run it.
<syntaxhighlight lang="c">/* gcc svd_embeddedSingular_value_decomposition.c -o svd_embeddedSingular_value_decomposition -lgsl -lgslcblas -lwren -lm */
 
#include <stdio.h>
Line 1,276 ⟶ 1,316:
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "svd_embeddedSingular_value_decomposition.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
9,476

edits