|
#!/usr/bin/env python |
|
""" time numpy / scipy linalg eigvalsh solve ... on sparse.random .A, dense """ |
|
# https://github.com/numpy/numpy/issues/16741 |
|
# scipy lapack-3.9.0.tar.gz 2019, numpy lapack-lite-3.1.1.tgz 2007 |
|
# http://www.netlib.org/lapack https://github.com/Reference-LAPACK/lapack/issues |
|
|
|
from collections import OrderedDict |
|
from functools import partial |
|
import platform |
|
import re |
|
import sys |
|
from time import time |
|
import numpy as np |
|
import scipy |
|
import scipy.linalg, scipy.linalg.lapack, scipy.sparse |
|
|
|
try: |
|
from testmatrix import testmatrix # powerlaw, suitesparse ... |
|
except ImportError: |
|
testmatrix = None |
|
|
|
__version__ = "2020-07-16 July denis-bz-py t-online.de" |
|
|
|
np.set_printoptions( threshold=10, edgeitems=3, linewidth=120, |
|
formatter = dict( float = lambda x: "%.3g" % x, # float arrays %.3g |
|
complexfloat = " {0.real:.3g} {0.imag:+.3g}j " .format )) |
|
|
|
print( 80 * "▄" ) |
|
print( "versions: numpy %s scipy %s python %s plaform %s " % ( |
|
np.__version__, scipy.__version__ , sys.version.split()[0], platform.platform() )) |
|
print( "scipy lapack ilaver:", scipy.linalg.lapack.ilaver() ) # (3, 9, 0) |
|
|
|
#........................................................................... |
|
run = "." # "eigvals" ... match np_sp_linalg keys below |
|
test = "randomsparse" |
|
# test = "Norris-fv1" # suitesparse https://sparse.tamu.edu/Norris |
|
n = 10000 |
|
density = .001 |
|
seed = 0 |
|
incdiag = .01 |
|
dtype = np.float64 |
|
order = 'C' # 'F' same |
|
rcond = 1e-6 |
|
|
|
# to change these params, run this.py a=1 b=None 'c = expr' ... in sh or ipython -- |
|
for arg in sys.argv[1:]: |
|
exec( arg ) |
|
|
|
#........................................................................... |
|
if test == "randomsparse" or testmatrix is None: |
|
Asparse = scipy.sparse.random( n, n, density=density, random_state=seed ) # uniform 0 - 1 |
|
else: |
|
Asparse = testmatrix( test, n=n ) |
|
Asparse += incdiag * scipy.sparse.eye( n ) # shift away from singular, wikipedia Circular_law |
|
A = Asparse.A.astype( dtype=dtype, order=order ) # dense |
|
print( "%s A %s density %.3g %s order %s incdiag %g" % ( |
|
test, A.shape, density, A.dtype, order, incdiag )) |
|
print( "%.0f mb dense" % (A.nbytes / 1e6 )) |
|
b = np.ones( n, dtype=dtype ) |
|
|
|
#........................................................................... |
|
# run some or all of these, e.g. run="solve" |
|
np_sp_linalg = OrderedDict( |
|
np_eigvalsh = np.linalg.eigvalsh, # _syevd |
|
sp_eigvalsh_ev = partial( scipy.linalg.eigvalsh, driver="ev" ), # ev evd evr evx |
|
sp_eigvalsh_evd = partial( scipy.linalg.eigvalsh, driver="evd" ), |
|
sp_eigvalsh_evr = partial( scipy.linalg.eigvalsh, driver="evr" ), |
|
|
|
# evecs too -- |
|
np_eigh = np.linalg.eigh, |
|
sp_eigh_evd = partial( scipy.linalg.eigh, driver="evd" ), |
|
sp_eigh_evr = partial( scipy.linalg.eigh, driver="evr" ), |
|
# ev evd evr evx / gv gvd gvx generalized |
|
|
|
# complex evals -- |
|
np_eigvals = np.linalg.eigvals, # _geev |
|
sp_eigvals = scipy.linalg.eigvals, |
|
|
|
np_lstsq = partial( np.linalg.lstsq, b=b, rcond=rcond ), |
|
sp_lstsq = partial( scipy.linalg.lstsq, b=b, cond=rcond ), |
|
|
|
np_solve = partial( np.linalg.solve, b=b ), |
|
sp_solve = partial( scipy.linalg.solve, b=b ), |
|
np_svd = partial( np.linalg.svd, compute_uv=False ), # gesdd |
|
sp_svd = partial( scipy.linalg.svd, compute_uv=False ), # lapack_driver : {'gesdd', 'gesvd'} |
|
) |
|
|
|
for name, func in np_sp_linalg.items(): |
|
if not re.search( run, name ): |
|
continue |
|
t0 = time() |
|
|
|
x = func( A ) |
|
if isinstance( x, tuple ): |
|
xtup = x |
|
x = x[0] |
|
t = time() - t0 |
|
x = x.real |
|
print( "%s: %.0f sec %s" % (name, t, np.sort( x ))) |