-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor out iterative refinement function
- Loading branch information
1 parent
5c47269
commit 85645ae
Showing
3 changed files
with
54 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
from . import ( | ||
healpix_ffts, | ||
iterative_refinement, | ||
jax_primitive, | ||
quadrature, | ||
quadrature_jax, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
"""Iterative scheme for improving accuracy of linear transforms.""" | ||
|
||
from collections.abc import Callable | ||
from typing import TypeVar | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
def forward_with_iterative_refinement( | ||
f: T, | ||
n_iter: int, | ||
forward_function: Callable[[T], T], | ||
backward_function: Callable[[T], T], | ||
) -> T: | ||
""" | ||
Apply forward transform with iterative refinement to improve accuracy. | ||
`Iterative refinement <https://en.wikipedia.org/wiki/Iterative_refinement>`_ is a | ||
general approach for improving the accuracy of numerial solutions to linear systems. | ||
In the context of spherical harmonic transforms, given a forward transform which is | ||
an _approximate_ inverse to a corresponding backward ('inverse') transform, | ||
iterative refinement allows defining an iterative forward transform which is a more | ||
accurate | ||
Args: | ||
f: Array argument to forward transform (signal on sphere) to compute iteratively | ||
refined forward transform at. | ||
n_iter: Number of refinement iterations to use, non-negative. | ||
forward_function: Function computing forward transform (approximate inverse of | ||
backward transform). | ||
backward_function: Function computing backward ('inverse') transform. | ||
Returns: | ||
Array output from iteratively refined forward transform (spherical harmonic | ||
coefficients). | ||
""" | ||
flm = forward_function(f) | ||
for _ in range(n_iter): | ||
f_recov = backward_function(flm) | ||
f_error = f - f_recov | ||
flm += forward_function(f_error) | ||
return flm |