Getting started#
[1]:
import sweepystats as sw
import numpy as np
The SweepMatrix class#
SweepMatrix is a thin wrapper over numpy darrays. The input must be a symmetric matrix. We convert all matrices into double-precision numpy arrays with column-major order, if they aren’t already.
[2]:
# intantiate a SweepMatrix from symmetric input
A_numpy = np.array([[1., 2, 3],
[2, 5, 6],
[3, 6, 9]],
order='F') # column-major format
A = sw.SweepMatrix(A_numpy)
A
[2]:
SweepMatrix(array([[1., 2., 3.],
[2., 5., 6.],
[3., 6., 9.]]))
NOTE: No data is copied if the input is already double-precision in column-major format. The latter requirement is because the sweeping operation is blessed by level-3 BLAS, which we call internally.
[3]:
# modifying entries of A also changes the original
A[0, 0] = 10
A_numpy
[3]:
array([[10., 2., 3.],
[ 2., 5., 6.],
[ 3., 6., 9.]])
A SweepMatrix can be swept forward and backwards:
[4]:
A.sweep(verbose=False) # forward sweep through all diagonal entries of A
A
[4]:
SweepMatrix(array([[-1.11111111e-01, -4.85722573e-17, 3.70370370e-02],
[-4.85722573e-17, -1.00000000e+00, 6.66666667e-01],
[ 3.70370370e-02, 6.66666667e-01, -5.67901235e-01]]))
[5]:
A.sweep(inv=True, verbose=False) # inverse sweep recovers the original data
A
[5]:
SweepMatrix(array([[10., 2., 3.],
[ 2., 5., 6.],
[ 3., 6., 9.]]))
We can also sweep a on the kth diagonal element
[6]:
A.sweep_k(1) # sweep the kth diagonal
A
[6]:
SweepMatrix(array([[ 9.2, 0.4, 0.6],
[ 0.4, -0.2, 1.2],
[ 0.6, 1.2, 1.8]]))
[7]:
A.sweep_k(1, inv=True) # unweep the kth diagonal
A
[7]:
SweepMatrix(array([[10., 2., 3.],
[ 2., 5., 6.],
[ 3., 6., 9.]]))