Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
robotran
mbsysc
Commits
3cf7d5c6
Commit
3cf7d5c6
authored
Feb 01, 2021
by
Olivier Lantsoght
Browse files
[MBsysPy][Algebra] Addition and substraction
parent
06c5f06a
Changes
1
Hide whitespace changes
Inline
Side-by-side
MBsysC/mbs_interface/MBsysPy/mbs_algebra.py
View file @
3cf7d5c6
...
...
@@ -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
v
res
: 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 unus
ed.
vres :
list or
numpy.ndarray, optional
Vector
containing
the result
if not None
.
Vector
to be substract
ed.
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
v
res
: 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
):
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment