I have a function which returns the square of the residual norm of a big linear equation system.
In [1]: import numpy as np
In [2]: A = np.random.rand(3600000, 200)
In [3]: b = np.random.rand(3600000)
In [4]: def f(x):
...: global A
...: global b
...: return np.linalg.norm(A.dot(x) - b)**2
Now I have an algorithm where the function has to be evaluated several times. However, due to the size of the equation system, each function call at a certain x
needs a lot of time.
In [5]: import time
In [6]: def f(x):
...: global A
...: global b
...: start = time.time()
...: res = np.linalg.norm(A.dot(x) - b)**2
...: end = time.time()
...: return res, end - start
In [7]: test = np.random.rand(200)
In [8]: f(test)
Out[8]: (8820030785.528395, 7.467242956161499)
My question is:
Are there any possibilities for reducing the time of such a function call?
I thought about replacing the np.linalg.norm(A.dot(x) - b)**2
with a more efficient expression, but I don't know how this could look.
Technical information. The above code was written on a computer with
- macOS Catalina version 10.15.5
- 2,3 GHz Dual‑Core Intel Core i5 (Turbo Boost up to 3,6 GHz) with 64 MB eDRAM
- 8 GB 2133 MHz LPDDR3 RAM (On‑Board)
-
Memory: Memory Slots: ECC: Disabled Upgradeable Memory: No BANK 0/DIMM0: Size: 4 GB Type: LPDDR3 Speed: 2133 MHz Status: OK (...) BANK 1/DIMM0: Size: 4 GB Type: LPDDR3 Speed: 2133 MHz Status: OK (...)
The result of np.show_config()
is
blas_mkl_info:
libraries = ['blas', 'cblas', 'lapack', 'pthread', 'blas', 'cblas', 'lapack']
library_dirs = ['/Users/me/miniconda3/envs/magpy/lib']
define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
include_dirs = ['/Users/me/miniconda3/envs/magpy/include']
blas_opt_info:
libraries = ['blas', 'cblas', 'lapack', 'pthread', 'blas', 'cblas', 'lapack', 'blas', 'cblas', 'lapack']
library_dirs = ['/Users/me/miniconda3/envs/magpy/lib']
define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
include_dirs = ['/Users/me/miniconda3/envs/magpy/include']
lapack_mkl_info:
libraries = ['blas', 'cblas', 'lapack', 'pthread', 'blas', 'cblas', 'lapack']
library_dirs = ['/Users/me/miniconda3/envs/magpy/lib']
define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
include_dirs = ['/Users/me/miniconda3/envs/magpy/include']
lapack_opt_info:
libraries = ['blas', 'cblas', 'lapack', 'pthread', 'blas', 'cblas', 'lapack', 'blas', 'cblas', 'lapack']
library_dirs = ['/Users/me/miniconda3/envs/magpy/lib']
define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
include_dirs = ['/Users/me/miniconda3/envs/magpy/include']