mljs / matrix

Matrix manipulation and computation library
https://mljs.github.io/matrix/
MIT License
353 stars 54 forks source link

feat: optimize SVD ? #167

Open lpatiny opened 11 months ago

lpatiny commented 11 months ago

The order of processing loops may be critical in order to optimize speed.

The implemented algorithms EVD, SVD, etc may assume a specific row / col or col / row order. The goal being that in the inner loops the memory is accessed sequentially.

By simply inverting the get / set in matrix (just using square matrices not to check everything) we observe major time difference (in ms)

The best improvement was observe for a SVD of 1000x1000 elements (x3 !) but the inversion can also lead to worse results like for LuDecomposition (/2 ...).

Inverted

┌─────────┬──────┬────────────────────────────┬──────────────────────┬─────────────────────────┐
│ (index) │ size │ SingularValueDecomposition │   LuDecomposition    │ EigenvalueDecomposition │
├─────────┼──────┼────────────────────────────┼──────────────────────┼─────────────────────────┤
│    0    │  10  │    0.047006314899820094    │ 0.006285565016191445 │   0.04497034566020066   │
│    1    │ 100  │     9.556330035184766      │  0.5338207964597829  │    11.88394686695906    │
│    2    │ 500  │     964.0846458325783      │  57.523048850654185  │   1427.5238334983587    │
│    3    │ 1000 │     7546.173541992903      │  563.8283982227246   │   15070.775082990527    │
│    4    │ 1500 │     25270.795874997973     │  2161.1955276678004  │    39320.5441249907     │
└─────────┴──────┴────────────────────────────┴──────────────────────┴─────────────────────────┘

Non inverted

┌─────────┬──────┬────────────────────────────┬───────────────────────┬─────────────────────────┐
│ (index) │ size │ SingularValueDecomposition │    LuDecomposition    │ EigenvalueDecomposition │
├─────────┼──────┼────────────────────────────┼───────────────────────┼─────────────────────────┤
│    0    │  10  │    0.028381821972091822    │ 0.0047396303146609135 │  0.029047660718630176   │
│    1    │ 100  │     7.579296062081497      │  0.39713138298076744  │   11.749581896539393    │
│    2    │ 500  │     1620.0299582481384     │  38.316282882820815   │   1338.5159477517009    │
│    3    │ 1000 │     22116.20337499678      │   307.1329264702166   │   19482.321583002806    │
│    4    │ 1500 │     44339.38079200685      │  1027.2492165982724   │    41522.77608400583    │
└─────────┴──────┴────────────────────────────┴───────────────────────┴─────────────────────────┘

This is just at the stage of reflection that could lead to some speed improvement.

codecov[bot] commented 11 months ago

Codecov Report

All modified lines are covered by tests :white_check_mark:

Comparison is base (bde1c9a) 71.27% compared to head (e82b2ba) 71.28%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #167 +/- ## ========================================== + Coverage 71.27% 71.28% +0.01% ========================================== Files 34 34 Lines 5295 5297 +2 Branches 849 849 ========================================== + Hits 3774 3776 +2 Misses 1516 1516 Partials 5 5 ``` | [Files](https://app.codecov.io/gh/mljs/matrix/pull/167?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mljs) | Coverage Δ | | |---|---|---| | [src/matrix.js](https://app.codecov.io/gh/mljs/matrix/pull/167?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mljs#diff-c3JjL21hdHJpeC5qcw==) | `79.57% <100.00%> (+0.02%)` | :arrow_up: |

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.