Commit 94be5180 authored by Olivier Lantsoght's avatar Olivier Lantsoght
Browse files

[MBsysPy][Algebra] Normalize functions

parent 4597884d
......@@ -24,34 +24,26 @@ def norm(v, size_4=True):
True if vectors have a size of 4.
default: True.
Raises
------
ValueError
If v is not a 1-dim array of size 4 if size_4 is True.
TypeError
If v is not a numpy.ndarray or a list.
Returns
-------
float
Norm of vector v starting at index 1.
"""
v, mat = __get_index_0_matrix__(v, size_4)
if mat:
raise ValueError('Vector must be passed as argument for norm function.')
return np.linalg.norm(v)
return np.linalg.norm(v[size_4:])
def normalize(v, vres=None, size_4=True):
"""Normalize vector and return a copy, starting with index 1.
"""Normalize the provided vector.
The shape of the vector differs according to size_4.
Parameters
----------
v : numpy.ndarray or list
Vector with first element unused.
vres : list or numpy.ndarray, optional
Vector containing the result if not None.
v : numpy.ndarray
Vector to be normalized.
vres : numpy.ndarray, optional
Vector to store the normalized vector. It is allocated if not provided.
default: None
size_4 : bool, optional
True if v has a size of 4.
......@@ -60,28 +52,26 @@ def normalize(v, vres=None, size_4=True):
Raises
------
ZeroDivisionError
If the norm of v is equal to zero
If the norm of v is equal to zero.
Returns
-------
v_norm : numpy.ndarray
Copy of normalized vector v starting at index 1.
vres : numpy.ndarray
The normalized vector v.
"""
n = norm(v, size_4)
if n == 0:
raise ZeroDivisionError('The norm of v is equal to zero')
if vres is None:
vres = np.array(v)
else:
if vres is not None:
if len(np.shape(vres)) == 1:
vres[1:] = v[1:] / n
else:
vres[0, 1:] = v[1:] / n
else:
v_norm = np.array(v, dtype=float)
v_norm[1:] = v_norm[1:] / n
return v_norm
vres[:] = v[:]
vres[size_4:] = vres[size_4:] / n
return vres
def scalar_product(v1, v2, size_4=True):
"""Compute and return the scalar product of 2 vectors with first index is 1.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment