forked from scipy/scipy
/
misc.py
32 lines (26 loc) · 962 Bytes
/
misc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import numpy as np
from numpy.linalg import LinAlgError
import blas
__all__ = ['LinAlgError', 'norm']
_nrm2_prefix = {'f' : 's', 'F': 'sc', 'D': 'dz'}
def norm(a, ord=None):
# Differs from numpy only in non-finite handling and the use of
# blas
a = np.asarray_chkfinite(a)
if ord in (None, 2) and (a.ndim == 1) and (a.dtype.char in 'fdFD'):
# use blas for fast and stable euclidean norm
func_name = _nrm2_prefix.get(a.dtype.char, 'd') + 'nrm2'
nrm2 = getattr(blas, func_name)
return nrm2(a)
return np.linalg.norm(a, ord=ord)
norm.__doc__ = np.linalg.norm.__doc__
def _datacopied(arr, original):
"""
Strict check for `arr` not sharing any data with `original`,
under the assumption that arr = asarray(original)
"""
if arr is original:
return False
if not isinstance(original, np.ndarray) and hasattr(original, '__array__'):
return False
return arr.base is None