// E03: exact hand-worked SVD. U*diag(s)*V^T = [[0,3],[1,0]]. Standard C++17.
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
int main() {
    const double A[2][2]={{0.,3.},{1.,0.}};
    const double U[2][2]={{1.,0.},{0.,1.}};
    const double s[2]={3.,1.};
    const double VT[2][2]={{0.,1.},{1.,0.}};
    double maximum_error=0.;
    std::cout << "E03 | A_rec = sum_k s[k] * U[:,k] * VT[k,:]\n";
    for (int i=0;i<2;++i) {
        for (int j=0;j<2;++j) {
            double reconstructed=0.;
            for (int k=0;k<2;++k) reconstructed+=U[i][k]*s[k]*VT[k][j];
            maximum_error=std::max(maximum_error,std::abs(reconstructed-A[i][j]));
            std::cout << std::setw(8) << reconstructed;
        }
        std::cout << '\n';
    }
    std::cout << "singular values: 3, 1; max reconstruction error = " << maximum_error << '\n';
    return maximum_error<1e-14 ? 0 : 1;
}
