matfree.decomp
matfree.decomp
Matrix-free matrix decompositions.
This module includes various Lanczos-decompositions of matrices (tri-diagonal, bi-diagonal, etc.).
For stochastic Lanczos quadrature and matrix-function-vector products, see matfree.funm.
matfree.decomp.bidiag(num_matvecs: int, /, materialize: bool = True, reortho: str = 'full')
Construct an implementation of bidiagonalisation.
Uses pre-allocation and full reorthogonalisation.
Works for arbitrary matrices. No symmetry required.
Decompose a matrix into a product of orthogonal-bidiagonal-orthogonal matrices. Use this algorithm for approximate singular value decompositions.
Internally, Matfree uses JAX to turn matrix-vector- into vector-matrix-products.
A note about differentiability
Unlike tridiag_sym or hessenberg, this function's reverse-mode derivatives are not efficient. Custom gradients for bidiagonalisation are a work in progress. In the meantime, if you need to differentiate the decompositions, consider using tridiag_sym instead (if possible).
Source code in matfree/decomp.py
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 | |
matfree.decomp.hessenberg(num_matvecs, /, *, reortho: str, custom_vjp: bool = True, reortho_vjp: str = 'match')
Construct a Hessenberg-factorisation via the Arnoldi iteration.
Uses pre-allocation, and full reorthogonalisation if reortho is set to "full".
It tends to be a good idea to use full reorthogonalisation.
This algorithm works for arbitrary matrices.
Setting custom_vjp to True implies using efficient, numerically stable
gradients of the Arnoldi iteration according to what has been proposed by
Krämer et al. (2024).
These gradients are exact, so there is little reason not to use them.
If you use this configuration,
please consider citing Krämer et al. (2024; bibtex below).
BibTex for Krämer et al. (2024)
@article{kraemer2024gradients,
title={Gradients of functions of large matrices},
author={Kr\"amer, Nicholas and Moreno-Mu\~noz, Pablo and
Roy, Hrittik and Hauberg, S{\o}ren},
journal={arXiv preprint arXiv:2405.17277},
year={2024}
}
Source code in matfree/decomp.py
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 | |
matfree.decomp.tridiag_sym(num_matvecs: int, /, *, materialize: bool = True, reortho: str = 'full', custom_vjp: bool = True)
Construct an implementation of tridiagonalisation.
Decompose a symmetric matrix into a product of orthogonal-tridiagonal-orthogonal matrices.
Use this algorithm for approximate eigenvalue decompositions.
The present implementation allocates all Lanczos vectors before running the
algorithm. If reortho is set to "full", it also uses full reorthogonalisation.
It is usually a good idea to use full reorthogonalisation.
Matrix-free tridiagonalisation uses Lanczos' (1950) algorithm:
BibTex for Lanczos (1950)
@article{lanczos1950iteration,
title={An iteration method for the solution of the eigenvalue problem of linear differential and integral operators},
author={Lanczos, Cornelius},
journal={Journal of research of the National Bureau of Standards},
volume={45},
number={4},
pages={255--282},
year={1950}
}
Setting custom_vjp to True implies using efficient, numerically stable
gradients of the Lanczos iteration which was proposed by Krämer et al. (2024).
These gradients are exact, so there is little reason not to use them.
If you use this configuration, please cite Krämer et al. (2024):
BibTex for Krämer et al. (2024)
@article{kraemer2024gradients,
title={Gradients of functions of large matrices},
author={Kr{\"a}mer, Nicholas and Moreno-Mu{\~n}oz, Pablo and Roy, Hrittik and Hauberg, S{\o}ren},
journal={Advances in Neural Information Processing Systems},
volume={37},
pages={49484--49518},
year={2024}
}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_matvecs
|
int
|
The number of matrix-vector products aka the depth of the Krylov space. The deeper the Krylov space, the more accurate the factorisation tends to be. However, the computational complexity increases linearly with the number of matrix-vector products. |
required |
materialize
|
bool
|
The value of this flag indicates whether the tridiagonal matrix should be returned in a sparse format (which means, as a tuple of diagonas) or as a dense matrix. The dense matrix is helpful if different decompositions should be used interchangeably. The sparse representation requires less memory. |
True
|
reortho
|
str
|
The value of this parameter indicates whether to reorthogonalise the basis vectors during the forward pass. Reorthogonalisation makes the forward pass more expensive, but helps (significantly) with numerical stability. |
'full'
|
custom_vjp
|
bool
|
The value of this flag indicates whether to use a custom vector-Jacobian product as proposed by Krämer et al. (2024; bibtex above). Generally, using a custom VJP tends to be a good idea. However, due to JAX's mechanics, a custom VJP precludes the use of forward-mode differentiation (see here), so don't use a custom VJP if you need forward-mode differentiation. |
True
|
Returns:
| Type | Description |
|---|---|
decompose
|
A decomposition function that maps
|
Source code in matfree/decomp.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |