# Chapter 3 MATLAB-to-Python conversions

This folder contains literal Python conversions of the supplied MATLAB files:

- `cores.py`
- `fig3_1.py`
- `fig3_3.py`
- `fig3_4A.py`
- `fig3_4B.py`
- `fig3_5.py`
- `fig3_6.py`
- `table3_3.py`
- `table3_4.py`
- `table3_5.py`
- `table3_7.py`

The original variable names, comments, colors, line styles, marker sizes, line widths, axes, grids, tick locations, and labels have been retained as closely as possible. MATLAB figure positions were converted to practical Matplotlib `figsize` values, with subplot spacing adjusted so each plot fits its window. Every explicitly specified MATLAB font size was reduced by 2 points; for example, 16 became 14 and 22 became 20.

## Installation

From Terminal, change to this folder and run:

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

## Running a file

Each file can be run directly. For example:

```bash
python3 fig3_5.py
python3 table3_7.py
```

Each MATLAB function was also converted to a Python function with the same base name, so it can be imported:

```python
from fig3_5 import fig3_5

fig3_5()
```

## Notes on the large computations

`cores.py` retains the original matrix sizes through 32,000 by 32,000. Those sizes require very large amounts of memory and can take a long time. For a quick test, call it with smaller optional arrays:

```python
from cores import cores

cores(n_values=[50, 100], nr_values=[2, 2], random_seed=1)
```

To limit BLAS/LAPACK to one core on macOS, set the thread variables before starting Python:

```bash
OPENBLAS_NUM_THREADS=1 OMP_NUM_THREADS=1 VECLIB_MAXIMUM_THREADS=1 python3 cores.py
```

`table3_5.py` likewise retains the original dense matrices through size 16,000. A quick test can use:

```python
from table3_5 import table3_5

table3_5(n_values=[4, 8, 12, 16], Nv=4)
```

`table3_3.py` and `table3_4.py` also accept an optional `n_values` argument for smaller test cases.

## MATLAB-to-Matplotlib details

- MATLAB `subaxis` calls were represented with Matplotlib subplot spacing controls.
- MATLAB `surfc` was represented by a 3-D surface plus contours projected onto the lower z-plane.
- The explicit MATLAB color-order arrays are retained through Matplotlib's property cycle.
- The scripts use `plt.show()` and can also be imported and called from an interactive Python session.
