Skip to content

Commit

Permalink
[REF] Improve numerical stability of Frobenius estimator (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
f-dangel committed Jul 3, 2024
1 parent 92ccd9f commit 4aaec2b
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions curvlinops/norm/hutchinson.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Hutchinson-style matrix norm estimation."""

from numpy import dot
from scipy.sparse.linalg import LinearOperator

from curvlinops.trace.hutchinson import HutchinsonTraceEstimator
from curvlinops.sampling import random_vector


class HutchinsonSquaredFrobeniusNormEstimator:
Expand Down Expand Up @@ -43,7 +44,7 @@ def __init__(self, A: LinearOperator):
Args:
A: Linear operator whose squared Frobenius norm will be estimated.
"""
self._trace_estimator = HutchinsonTraceEstimator(A.T @ A)
self._A = A

def sample(self, distribution: str = "rademacher") -> float:
"""Draw a sample from the squared Frobenius norm estimator.
Expand All @@ -59,4 +60,7 @@ def sample(self, distribution: str = "rademacher") -> float:
Returns:
Sample from the squared Frobenius norm estimator.
"""
return self._trace_estimator.sample(distribution=distribution)
dim = self._A.shape[1]
v = random_vector(dim, distribution)
Av = self._A @ v
return dot(Av, Av)

0 comments on commit 4aaec2b

Please sign in to comment.