# SVD-fitting laboratory: independent C++ teaching examples

**AGH University of Krakow · Numerical Methods in Physics · Hamzeh Khanpour**

These are **fully solved, small educational examples**. They are **not** a completed solution to the official assessed `y=x*cos(x)` fitting laboratory. Please build your official project independently.

| Example | Purpose | Slides | Dependencies |
|---|---|---|---|
| E01 `01_polynomial_design_matrix.cpp` | Three points, monomial design matrix, Horner/polynomial evaluation | 8–10 | C++17 |
| E02 `02_least_squares_residuals.cpp` | Verify residuals and known least-squares line | 12–14 | C++17 |
| E03 `03_svd_reconstruction_2x2.cpp` | Exact hand-provided 2×2 singular vectors and matrix reconstruction | 22–24 | C++17 |
| E04 `04_lapacke_thin_svd.cpp` | Genuine row-major LAPACKE thin SVD for a 4×2 matrix | 25–27, 36–40 | LAPACKE |
| E05 `05_svd_linear_fit_toy.cpp` | SVD solution of **three-point straight-line** fitting | 29–31, 41 | LAPACKE |
| E06 `06_optional_small_singular_values.cpp` | Optional numerical-cutoff demonstration | 32 | LAPACKE |

## Requirements and build

Linux: install a C++17 compiler and LAPACKE development files; on Debian/Ubuntu this typically means `g++ make liblapacke-dev liblapack-dev libblas-dev` (installation depends on OS/repository). Set custom paths with `CPPFLAGS` and `LAPACK_LIBS` if necessary.

```bash
make               # builds E01–E05
make check         # builds and runs all five core examples; nonzero exit code indicates a failed check
make optional      # build E06
./bin/06_optional_small_singular_values
make clean
```

To compile E04 by hand with a system LAPACKE installation:

```bash
g++ -std=c++17 -O2 -Wall -Wextra -pedantic \
  examples/04_lapacke_thin_svd.cpp -o e04 -llapacke -llapack -lblas
./e04
```

**Row-major convention:** `A[i*n+j]`; with thin SVD (`m>=n`), `U[i*n+k]`, `VT[k*n+j]`. `VT` represents **transposed V**, so do not interpret its rows as columns of V. The LAPACKE SVD routine can overwrite `A`; preserve `original` for reconstruction tests. Compare orthogonality and reconstruction errors rather than the signs of singular vectors.

## What to verify

- E01: design matrix rows `(1,0)`, `(1,1)`, `(1,2)`.
- E02: `c0=7/6`, `c1=1/2`, squared residual norm `SSE=1/6`, `A^T r ≈ 0`.
- E03: exact reconstruction of the matrix `[[0,3],[1,0]]`.
- E04: nonnegative descending singular values, reconstruction and orthogonality errors near machine precision.
- E05: fitted coefficients `(7/6,1/2)` and squared residual norm `1/6`.
- E06: small singular values may amplify data noise; truncation changes the fitted answer and is **optional**, not part of the specified assessed recipe.

Sample recorded outputs are under `sample_outputs/`. A separate plot script in `plotting/` regenerates **teaching-only** figures. See the four-page student notes and official project statement for the assessed tasks and required three figures.

Official source: https://galaxy.agh.edu.pl/~chwiej/nmp/labs/nmp_svd_fitting_data.pdf
