Solve a System of Linear Equations with PreconditioningΒΆ

The preconditioner used for solving can also be provided as a LinearOperator.

import fastmat as fm
import numpy as np
from scipy.sparse.linalg import cgs
# diagonal matrix with no zeros
d = np.random.uniform(1, 20, 2 ** 10)

# fastmat object
H = fm.Diag(d)

# use the new property to generate a scipy linear operator
Hs = H.scipyLinearOperator

# also generate a Preconditioning linear operator,
# which in this case is the exact inverse
Ms = fm.Diag(1.0 / d).scipyLinearOperator

# get a baseline
x = np.random.uniform(1, 20, 2 ** 10)
y = np.linalg.solve(H.array, x)
cgs(Hs, x, tol=1e-10)
cgs(Hs, x, tol=1e-10, M=Ms)