|
| 1 | +import { expect } from 'vitest' |
1 | 2 | import Matrix from '../../../lib/util/matrix.js' |
2 | 3 | import Tensor from '../../../lib/util/tensor.js' |
3 | 4 |
|
@@ -6252,28 +6253,6 @@ describe('Matrix', () => { |
6252 | 6253 | }) |
6253 | 6254 | }) |
6254 | 6255 |
|
6255 | | - describe('singularValuePowerIteration', () => { |
6256 | | - test.each([ |
6257 | | - [10, 6], |
6258 | | - [6, 10], |
6259 | | - ])('size [%i, %i]', (r, c) => { |
6260 | | - const mat = Matrix.randn(r, c) |
6261 | | - const sv = mat.singularValuePowerIteration() |
6262 | | - expect(sv).toBeGreaterThanOrEqual(0) |
6263 | | - |
6264 | | - const [ev] = mat.tDot(mat).eigenPowerIteration() |
6265 | | - expect(sv ** 2).toBeCloseTo(ev) |
6266 | | - }) |
6267 | | - |
6268 | | - test('iteration not converged', () => { |
6269 | | - const mat = new Matrix(2, 2, [ |
6270 | | - [-1, -2], |
6271 | | - [2, -2], |
6272 | | - ]) |
6273 | | - expect(() => mat.singularValuePowerIteration(1)).toThrow('singularValuePowerIteration not converged.') |
6274 | | - }) |
6275 | | - }) |
6276 | | - |
6277 | 6256 | describe('svd', () => { |
6278 | 6257 | test.each([ |
6279 | 6258 | [10, 10], |
@@ -6503,6 +6482,39 @@ describe('Matrix', () => { |
6503 | 6482 | }) |
6504 | 6483 | }) |
6505 | 6484 |
|
| 6485 | + describe('svdPowerIteration', () => { |
| 6486 | + test.each([ |
| 6487 | + [10, 6], |
| 6488 | + [6, 10], |
| 6489 | + ])('size [%i, %i]', (r, c) => { |
| 6490 | + const mat = Matrix.randn(r, c) |
| 6491 | + const [u, sv, v] = mat.svdPowerIteration() |
| 6492 | + expect(sv).toBeGreaterThanOrEqual(0) |
| 6493 | + expect(u.norm()).toBeCloseTo(1) |
| 6494 | + expect(v.norm()).toBeCloseTo(1) |
| 6495 | + |
| 6496 | + const mv = mat.dot(v) |
| 6497 | + for (let i = 0; i < r; i++) { |
| 6498 | + expect(mv.at(i, 0)).toBeCloseTo(sv * u.at(i, 0)) |
| 6499 | + } |
| 6500 | + const mu = mat.tDot(u) |
| 6501 | + for (let i = 0; i < c; i++) { |
| 6502 | + expect(mu.at(i, 0)).toBeCloseTo(sv * v.at(i, 0)) |
| 6503 | + } |
| 6504 | + |
| 6505 | + const [ev] = mat.tDot(mat).eigenPowerIteration() |
| 6506 | + expect(sv ** 2).toBeCloseTo(ev) |
| 6507 | + }) |
| 6508 | + |
| 6509 | + test('iteration not converged', () => { |
| 6510 | + const mat = new Matrix(2, 2, [ |
| 6511 | + [-1, -2], |
| 6512 | + [2, -2], |
| 6513 | + ]) |
| 6514 | + expect(() => mat.svdPowerIteration(1)).toThrow('svdPowerIteration not converged.') |
| 6515 | + }) |
| 6516 | + }) |
| 6517 | + |
6506 | 6518 | describe('cholesky', () => { |
6507 | 6519 | test('success', () => { |
6508 | 6520 | const n = 10 |
|
0 commit comments