Metadata-Version: 2.4
Name: scipyspectral
Version: 1.0.0
Summary: SVD, PCA, and matrix decomposition from first principles
Author: pyspectral contributors
License: MIT License
        
        Copyright (c) 2024 pyspectral contributors
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/pyspectral/pyspectral
Project-URL: Repository, https://github.com/pyspectral/pyspectral
Project-URL: Documentation, https://pyspectral.readthedocs.io
Project-URL: Bug Tracker, https://github.com/pyspectral/pyspectral/issues
Keywords: svd,pca,linear-algebra,matrix-decomposition,dimensionality-reduction,eigenfaces,image-compression,numerical-computing,machine-learning
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Science/Research
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Education
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Mathematics
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Operating System :: OS Independent
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.24.0
Requires-Dist: scipy>=1.10.0
Requires-Dist: matplotlib>=3.7.0
Requires-Dist: pillow>=9.5.0
Provides-Extra: video
Requires-Dist: imageio>=2.28.0; extra == "video"
Requires-Dist: imageio-ffmpeg>=0.4.8; extra == "video"
Provides-Extra: notebook
Requires-Dist: jupyter>=1.0.0; extra == "notebook"
Requires-Dist: ipywidgets>=8.0.0; extra == "notebook"
Provides-Extra: dev
Requires-Dist: pytest>=7.3.0; extra == "dev"
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
Provides-Extra: docs
Requires-Dist: sphinx>=7.0.0; extra == "docs"
Requires-Dist: furo>=2024.1.29; extra == "docs"
Requires-Dist: sphinx-autodoc-typehints>=1.25.0; extra == "docs"
Requires-Dist: sphinx-copybutton>=0.5.2; extra == "docs"
Requires-Dist: myst-parser>=2.0.0; extra == "docs"
Provides-Extra: all
Requires-Dist: imageio>=2.28.0; extra == "all"
Requires-Dist: imageio-ffmpeg>=0.4.8; extra == "all"
Requires-Dist: jupyter>=1.0.0; extra == "all"
Requires-Dist: ipywidgets>=8.0.0; extra == "all"
Requires-Dist: pytest>=7.3.0; extra == "all"
Dynamic: license-file
Dynamic: requires-python

# pyspectral

SVD, PCA, and matrix decomposition — implemented from scratch in Python.

No `np.linalg.svd`. No `sklearn.decomposition.PCA`. Every algorithm is built from first principles using only NumPy and SciPy as a numerical substrate.

---

## What it does

Starting from power iteration on a random vector, the library builds up a full stack:

```
power iteration
  → eigen decomposition (Hotelling deflation)
    → full SVD
      → truncated SVD (exact + randomized)
        → PCA
          → image compression
          → video compression
          → eigenfaces (face recognition)
```

It's a learning/research library. The goal is algorithmic clarity, not LAPACK performance.

For the full writeup on how it was built and why — [read the blog](blog.md).

---

## Install

```bash
pip install -e .

pip install -e ".[video]" # video compression (imageio)
pip install -e ".[notebook]" # Jupyter support
pip install -e ".[dev]" # pytest
```

---

## Structure

```
pyspectral/
  linalg_core/     power_iteration.py, eigen_decomp.py, svd_full.py, svd_truncated.py
  pca/             covariance.py, pca_model.py
  applications/    image_compression.py, eigenfaces.py, video_compression.py
  benchmarking/    time_complexity.py, memory_profile.py
  experiments/     large_matrix_tests.py
  utils/           matrix_checks.py
  tests/           test_power_iteration.py, test_svd.py, test_pca.py
```

---

## API

### Full SVD

```python
import numpy as np
from pyspectral.linalg_core.svd_full import compute_svd

A = np.random.randn(100, 80)
U, S, Vt = compute_svd(A)

A_rec = U[:, :len(S)] @ np.diag(S) @ Vt[:len(S), :]
```

### Truncated SVD

```python
from pyspectral.linalg_core.svd_truncated import truncated_svd

Uk, Sk, Vkt = truncated_svd(A, k=10)                        # exact
Uk, Sk, Vkt = truncated_svd(A, k=10, method="randomized")   # Halko 2011, much faster

A_approx = Uk @ np.diag(Sk) @ Vkt
```

### PCA

```python
from pyspectral.pca.pca_model import PCAEngine

pca = PCAEngine(n_components=10)
pca.fit(X)                          # X: (n_samples, n_features)

Z    = pca.transform(X)             # -> (n, 10)
Xhat = pca.inverse_transform(Z)     # -> (n, n_features)

print(pca.explained_variance_ratio_)

k, _ = pca.explained_variance(threshold=0.90)
print(f"{k} components explain 90% variance")
```

Use `method="svd"` when features >> samples (avoids building the p×p covariance matrix):

```python
pca = PCAEngine(n_components=50, method="svd")
```

### Image compression

```python
from pyspectral.applications.image_compression import ImageCompressor

c = ImageCompressor("photo.png")
c.compress_and_compare(ranks=[5, 20, 50, 100])
c.print_report()
c.save_results("output/")
```

Output:
```
Rank k    Ratio     PSNR (dB)
   5      55.2x     17.12
  20      13.8x     22.28
  50       5.5x     27.17
 100       2.8x     31.98
```

> SVD compression is not competitive with JPEG at equal file size (~30 dB gap). It stores raw float32 with no entropy coding. Useful for scientific/numerical matrices — not for replacing image codecs.

### Video compression

```python
from pyspectral.applications.video_compression import VideoCompressor

vc = VideoCompressor()
vc.load_synthetic(kind="wave")          # "wave", "bouncing", "noise", "mixed"

vc.compress_frame_by_frame(k=5)        # rank-k SVD per frame
vc.compress_temporal(k=5)             # global SVD across all frames (T, H*W)

vc.save_results("output/video/")
```

### Eigenfaces

```python
from pyspectral.applications.eigenfaces import EigenfacesModel

model = EigenfacesModel(n_components=30, image_size=(32, 32))
model.train_synthetic(n_subjects=10, n_images_per_subject=8)

label, dist, _ = model.recognize_face(query_image)
accuracy, _    = model.evaluate_accuracy()           # leave-one-out CV
model.visualize_eigenfaces(n_show=16, output_path="eigenfaces.png")
```

Real face dataset:
```python
model = EigenfacesModel(n_components=50, image_size=(112, 92))
model.train_eigenfaces("path/to/dataset/")   # one subfolder per person
```

### Matrix utilities

```python
from pyspectral.utils.matrix_checks import matrix_info, condition_number, is_symmetric

matrix_info(A)       # shape, norms, rank, condition number
condition_number(A)  # sigma_max / sigma_min
is_symmetric(A)
```

---

## Running demos and tests

```bash
python -m pyspectral.applications.image_compression
python -m pyspectral.applications.eigenfaces
python -m pyspectral.applications.video_compression
python -m pyspectral.benchmarking.time_complexity
python -m pyspectral.experiments.large_matrix_tests

python -m pytest pyspectral/tests/ -p no:asyncio -q
```

---

MIT License
