Commit 3cf7d5c6 authored by Olivier Lantsoght's avatar Olivier Lantsoght
Browse files

[MBsysPy][Algebra] Addition and substraction

parent 06c5f06a
......@@ -98,103 +98,69 @@ def scalar_product(v1, v2, size_4=True):
def vector_sum(v1, v2, vres=None, size_4=True):
"""Compute and return the sum of 2 vectors with first index is 1.
"""Compute and return the sum of 2 vectors.
The vectors have unused index 0.
The shape of the vector differs according to size_4.
Parameters
----------
v1 : list or numpy.ndarray
Vector with first element unused.
v2 : list or numpy.ndarray
Vector with first element unused.
vres : list or numpy.ndarray, optional
Vector containing the result if not None.
v1 : numpy.ndarray
First vector of the addition.
v2 : numpy.ndarray
Second vector of the addition.
vres : numpy.ndarray, optional
Vector to store the result of v1+v2. It is allocated if not provided.
default: None
size_4 : bool, optional
True if vectors have a size of 4.
default: True.
Raises
------
ValueError
If v1 or v2 is not a 1-dim array of size 4 if size_4 is True.
TypeError
If v1 or v2 is not a numpy.ndarray or a list.
Returns
-------
v_sum : numpy.ndarray
vres : numpy.ndarray
Sum of v1 and v2.
"""
v1, mat1 = __get_index_0_matrix__(v1, size_4)
v2, mat2 = __get_index_0_matrix__(v2, size_4)
if mat1 or mat2:
raise ValueError('Vectors must be passed as argument for addition')
v_sum = v1 + v2
if vres is not None:
if len(np.shape(vres)) == 1:
vres[1:] = v_sum
else:
if (vres.shape)[0] == 1:
vres[0, 1:] = v_sum
else:
vres[1:, 0] = v_sum
else:
v_sum = np.append(v_sum.shape[0], v_sum)
return v_sum
if vres is None:
vres = np.zeros(3 + size_4)
vres[size_4:] = v1[size_4:] + v2[size_4:]
return vres
def vector_diff(v1, v2, vres=None, size_4=True):
"""Compute and return the substraction of 2 vectors with first index is 1.
"""Compute and return the substraction of 2 vectors.
The shape of the vector differs according to size_4.
The vectors have unused index 0.
Parameters
----------
v1 : list or numpy.ndarray
Vector with first element unused.
v1 : numpy.ndarray
First vector of the substraction.
v2 : list or numpy.ndarray
Vector with first element unused.
vres : list or numpy.ndarray, optional
Vector containing the result if not None.
Vector to be substracted.
vres : numpy.ndarray, optional
Vector to store the result of v1-v2. It is allocated if not provided.
default: None
size_4 : bool, optional
True if vectors have a size of 4.
True if vectors have a size of 4 (the first index being disregarded).
default: True.
Raises
------
ValueError
If v1 or v2 is not a 1-dim array of size 4 if size_4 is True.
TypeError
If v1 or v2 is not a numpy.ndarray or a list.
Returns
-------
v_diff : numpy.ndarray
vres : numpy.ndarray
Substraction of v1 and v2 (v1 - v2).
"""
v1, mat1 = __get_index_0_matrix__(v1, size_4)
v2, mat2 = __get_index_0_matrix__(v2, size_4)
if mat1 or mat2:
raise ValueError('Vectors must be passed as argument for substraction')
v_diff = v1 - v2
if vres is not None:
if len(np.shape(vres)) == 1:
vres[1:] = v_diff
else:
if (vres.shape)[0] == 1:
vres[0, 1:] = v_diff
else:
vres[1:, 0] = v_diff
else:
v_diff = np.append(v_diff.shape[0], v_diff)
return v_diff
if vres is None:
vres = np.zeros(3 + size_4)
vres[size_4:] = v1[size_4:] - v2[size_4:]
return vres
def cross_product(v1, v2, vres=None, size_4=True):
......
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