Package geometry :: Module basic_utils
[hide private]
[frames] | no frames]

Source Code for Module geometry.basic_utils

 1  from . import GeometryConstants, contract, np, new_contract 
 2  import warnings 
 3   
 4  new_contract('R1', 'array[1]') 
 5  new_contract('R2', 'array[2]') 
 6  new_contract('R3', 'array[3]') 
7 8 9 @new_contract 10 @contract(x='array') 11 -def finite(x):
12 # TODO: make into standard thing 13 return np.isfinite(x).all()
14
15 16 @contract(s='array') 17 -def normalize_length(s, norm=2):
18 ''' Normalize an array such that it has unit length in the given norm. ''' 19 sn = np.linalg.norm(s, norm) 20 if np.allclose(sn, 0, atol=GeometryConstants.atol_zero_norm): 21 raise ValueError('Norm is zero') 22 else: 23 return s / sn
24
25 26 @contract(s='array') 27 -def normalize_length_or_zero(s, norm=2):
28 ''' 29 Normalize an array such that it has unit length in the given norm; 30 if the norm is close to zero, the zero vector is returned. 31 ''' 32 sn = np.linalg.norm(s, norm) 33 if np.allclose(sn, 0, atol=GeometryConstants.atol_zero_norm): 34 return s 35 else: 36 return s / sn
37
38 39 -def deprecated(func):
40 """This is a decorator which can be used to mark functions 41 as deprecated. It will result in a warning being emitted 42 when the function is used.""" 43 def new_func(*args, **kwargs): 44 warnings.warn("Call to deprecated function %s." % func.__name__, 45 category=DeprecationWarning, stacklevel=2) 46 return func(*args, **kwargs)
47 new_func.__name__ = func.__name__ 48 new_func.__doc__ = func.__doc__ 49 new_func.__dict__.update(func.__dict__) 50 return new_func 51
52 53 -def safe_arccos(x):
54 ''' 55 Returns the arcosine of x, clipped between -1 and 1. 56 57 Use this when you know x is a cosine, but it might be 58 slightly over 1 or below -1 due to numerical errors. 59 ''' 60 return np.arccos(np.clip(x, -1.0, 1.0))
61