From ed732c4988e07cdb8ef85f74edbe18f626e7f8f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicholas=20Kr=C3=A4mer?= Date: Tue, 21 Nov 2023 13:37:05 +0100 Subject: [PATCH] Renamed decomposition algorithms --- matfree/decomp.py | 10 +++++----- matfree/slq.py | 4 ++-- tests/test_decomp/test_bidiagonal_full_reortho.py | 10 +++++----- tests/test_decomp/test_svd.py | 2 +- tests/test_decomp/test_tridiagonal_full_reortho.py | 4 ++-- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/matfree/decomp.py b/matfree/decomp.py index 07f9362..b33f29e 100644 --- a/matfree/decomp.py +++ b/matfree/decomp.py @@ -20,7 +20,7 @@ class _Alg(containers.NamedTuple): """Range of the for-loop used to decompose a matrix.""" -def tridiagonal_full_reortho(depth, /): +def lanczos_tridiag_full_reortho(depth, /): """Construct an implementation of **tridiagonalisation**. Uses pre-allocation. Fully reorthogonalise vectors at every step. @@ -83,7 +83,7 @@ def extract(state: State, /): return _Alg(init=init, step=apply, extract=extract, lower_upper=(0, depth + 1)) -def bidiagonal_full_reortho(depth, /, matrix_shape): +def lanczos_bidiag_full_reortho(depth, /, matrix_shape): """Construct an implementation of **bidiagonalisation**. Uses pre-allocation. Fully reorthogonalise vectors at every step. @@ -161,7 +161,7 @@ def _gram_schmidt_orthogonalise(vec1, vec2): return vec_ortho, coeff -def svd( +def svd_approx( v0: Array, depth: int, Av: Callable, vA: Callable, matrix_shape: Tuple[int, ...] ): """Approximate singular value decomposition. @@ -185,7 +185,7 @@ def svd( Shape of the matrix involved in matrix-vector and vector-matrix products. """ # Factorise the matrix - algorithm = bidiagonal_full_reortho(depth, matrix_shape=matrix_shape) + algorithm = lanczos_bidiag_full_reortho(depth, matrix_shape=matrix_shape) u, (d, e), vt, _ = decompose_fori_loop(v0, Av, vA, algorithm=algorithm) # Compute SVD of factorisation @@ -222,7 +222,7 @@ class _DecompAlg(containers.NamedTuple): """Decomposition algorithm type. For example, the output of -[matfree.decomp.tridiagonal_full_reortho(...)][matfree.decomp.tridiagonal_full_reortho]. +[matfree.decomp.lanczos_tridiag_full_reortho(...)][matfree.decomp.lanczos_tridiag_full_reortho]. """ diff --git a/matfree/slq.py b/matfree/slq.py index dea6414..35f0a0a 100644 --- a/matfree/slq.py +++ b/matfree/slq.py @@ -22,7 +22,7 @@ def _quadratic_form_slq_spd(matfun, order, Av, /): """ def quadform(v0, /): - algorithm = decomp.tridiagonal_full_reortho(order) + algorithm = decomp.lanczos_tridiag_full_reortho(order) _, tridiag = decomp.decompose_fori_loop(v0, Av, algorithm=algorithm) (diag, off_diag) = tridiag @@ -85,7 +85,7 @@ def _quadratic_form_slq_product(matfun, depth, *matvec_funs, matrix_shape): def quadform(v0, /): # Decompose into orthogonal-bidiag-orthogonal - algorithm = decomp.bidiagonal_full_reortho(depth, matrix_shape=matrix_shape) + algorithm = decomp.lanczos_bidiag_full_reortho(depth, matrix_shape=matrix_shape) output = decomp.decompose_fori_loop(v0, *matvec_funs, algorithm=algorithm) u, (d, e), vt, _ = output diff --git a/tests/test_decomp/test_bidiagonal_full_reortho.py b/tests/test_decomp/test_bidiagonal_full_reortho.py index 76c6cf1..09becdb 100644 --- a/tests/test_decomp/test_bidiagonal_full_reortho.py +++ b/tests/test_decomp/test_bidiagonal_full_reortho.py @@ -18,12 +18,12 @@ def A(nrows, ncols, num_significant_singular_vals): @testing.parametrize("ncols", [49]) @testing.parametrize("num_significant_singular_vals", [4]) @testing.parametrize("order", [6]) # ~1.5 * num_significant_eigvals -def test_bidiagonal_full_reortho(A, order): +def test_lanczos_bidiag_full_reortho(A, order): """Test that Lanczos tridiagonalisation yields an orthogonal-tridiagonal decomp.""" nrows, ncols = np.shape(A) key = prng.prng_key(1) v0 = prng.normal(key, shape=(ncols,)) - alg = decomp.bidiagonal_full_reortho(order, matrix_shape=np.shape(A)) + alg = decomp.lanczos_bidiag_full_reortho(order, matrix_shape=np.shape(A)) def Av(v): return A @ v @@ -74,7 +74,7 @@ def test_error_too_high_depth(A): max_depth = min(nrows, ncols) - 1 with testing.raises(ValueError): - _ = decomp.bidiagonal_full_reortho(max_depth + 1, matrix_shape=np.shape(A)) + _ = decomp.lanczos_bidiag_full_reortho(max_depth + 1, matrix_shape=np.shape(A)) @testing.parametrize("nrows", [5]) @@ -84,7 +84,7 @@ def test_error_too_low_depth(A): """Assert that a ValueError is raised when the depth is negative.""" min_depth = 0 with testing.raises(ValueError): - _ = decomp.bidiagonal_full_reortho(min_depth - 1, matrix_shape=np.shape(A)) + _ = decomp.lanczos_bidiag_full_reortho(min_depth - 1, matrix_shape=np.shape(A)) @testing.parametrize("nrows", [15]) @@ -93,7 +93,7 @@ def test_error_too_low_depth(A): def test_no_error_zero_depth(A): """Assert the corner case of zero-depth does not raise an error.""" nrows, ncols = np.shape(A) - algorithm = decomp.bidiagonal_full_reortho(0, matrix_shape=np.shape(A)) + algorithm = decomp.lanczos_bidiag_full_reortho(0, matrix_shape=np.shape(A)) key = prng.prng_key(1) v0 = prng.normal(key, shape=(ncols,)) diff --git a/tests/test_decomp/test_svd.py b/tests/test_decomp/test_svd.py index bda7e0f..75e64d6 100644 --- a/tests/test_decomp/test_svd.py +++ b/tests/test_decomp/test_svd.py @@ -34,7 +34,7 @@ def vA(v): return v @ A v0 = np.ones((ncols,)) - U, S, Vt = decomp.svd(v0, depth, Av, vA, matrix_shape=np.shape(A)) + U, S, Vt = decomp.svd_approx(v0, depth, Av, vA, matrix_shape=np.shape(A)) U_, S_, Vt_ = linalg.svd(A, full_matrices=False) tols_decomp = {"atol": 1e-5, "rtol": 1e-5} diff --git a/tests/test_decomp/test_tridiagonal_full_reortho.py b/tests/test_decomp/test_tridiagonal_full_reortho.py index beb861e..9695045 100644 --- a/tests/test_decomp/test_tridiagonal_full_reortho.py +++ b/tests/test_decomp/test_tridiagonal_full_reortho.py @@ -22,7 +22,7 @@ def test_max_order(A): order = n - 1 key = prng.prng_key(1) v0 = prng.normal(key, shape=(n,)) - alg = decomp.tridiagonal_full_reortho(order) + alg = decomp.lanczos_tridiag_full_reortho(order) Q, (d_m, e_m) = decomp.decompose_fori_loop(v0, lambda v: A @ v, algorithm=alg) # Lanczos is not stable. @@ -63,7 +63,7 @@ def test_identity(A, order): n, _ = np.shape(A) key = prng.prng_key(1) v0 = prng.normal(key, shape=(n,)) - alg = decomp.tridiagonal_full_reortho(order) + alg = decomp.lanczos_tridiag_full_reortho(order) Q, tridiag = decomp.decompose_fori_loop(v0, lambda v: A @ v, algorithm=alg) (d_m, e_m) = tridiag