# Chapter 4 MATLAB-to-Python conversions

This folder contains literal Python conversions of the supplied Chapter 4 MATLAB programs. Variable names, numerical procedures, comments, line colors, line styles, marker sizes, line widths, labels, tick locations, and other plot options were retained wherever the corresponding Python libraries support them.

Every explicitly specified MATLAB font size was reduced by 2 points. Figure dimensions preserve the original aspect ratios while using practical Matplotlib window sizes so the complete plot fits in the plotting window.

## Files

- `table4_2.py` — power method
- `table4_3.py` — power method for a 200 by 200 matrix
- `table4_4.py` — shifted inverse iteration
- `table4_5.py` — inverse iteration with two shifts
- `table4_7.py` — Rayleigh quotient iteration
- `table4_9.py` — orthogonal iteration
- `table4_10.py` — orthogonal iteration for a 10 by 10 matrix
- `table4_11.py` — classical and modified Gram–Schmidt comparisons
- `table4_12.py` — LU, QR, and SVD timing comparison
- `table4_13.py` — QR method
- `table4_15.py` — orthogonal iteration with the inverse power method
- `fig4_2.py` — eigenvalue plot
- `fig4_7.py` — singular-value-decomposition geometry
- `fig4_8.py` — grayscale low-rank image approximations
- `fig4_9.py` — singular-value relative-error plot
- `fig4_10.py` — color low-rank image approximations and error plot
- `p17.jpg`, `flower.jpg`, and `butter.jpeg` — image files used by the figure programs
- `requirements.txt` — required Python packages

## Installation

Open Terminal in this folder and run:

```bash
python3 -m pip install -r requirements.txt
```

## Running a program

For example:

```bash
python3 fig4_2.py
python3 table4_9.py
```

The image programs locate their image files relative to the Python file, so they can be run from any working directory as long as the images remain in this folder.

`fig4_8.py` and `fig4_10.py` retain the MATLAB `pause` behavior. Press Return in Terminal to advance to the next approximation.

## Computational notes

`table4_12.py` deliberately preserves the original matrix sizes and trial counts. Its default run performs many dense LU, QR, and SVD factorizations, including matrices as large as 4000 by 4000, so it can require substantial time and memory. For a quicker test, call it from Python with smaller values:

```python
from table4_12 import table4_12

table4_12(n_values=[50, 100, 200], nr_small=2, nr_large=2)
```

`fig4_10.py` applies an SVD to the three vertically stacked color planes. With the supplied 2048 by 2048 image, this is computationally demanding. The default keeps the original full image resolution. A smaller test can be run with:

```python
from fig4_10 import fig4_10

fig4_10(pause_between=False, max_image_dimension=256)
```

NumPy stores the singular values as a vector internally rather than constructing the very large mostly-zero rectangular MATLAB `S` matrix during reconstruction. This produces the same low-rank approximation while avoiding unnecessary memory use.

The numerical values produced by programs with random starting vectors or matrices will vary from run to run, as they do in the MATLAB originals.
