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
06c5f06a
Commit
06c5f06a
authored
Feb 01, 2021
by
Olivier Lantsoght
Browse files
[MBsysPy][Algebra] Cross product
Numpy.cross is slow
parent
173bf76f
Changes
1
Hide whitespace changes
Inline
Side-by-side
MBsysC/mbs_interface/MBsysPy/mbs_algebra.py
View file @
06c5f06a
...
...
@@ -198,53 +198,44 @@ def vector_diff(v1, v2, vres=None, size_4=True):
def
cross_product
(
v1
,
v2
,
vres
=
None
,
size_4
=
True
):
"""Compute and return the cross product of 2 vectors
with first index is 1
.
"""Compute and return the cross product 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 cross product
.
v2 : numpy.ndarray
Second vector of the cross product
.
vres : numpy.ndarray, optional
Vector
to store
the result if not None.
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
_cros
s : numpy.ndarray
v
re
s : numpy.ndarray
Cross product between v1 and v2 (v1 x v2).
Notes
-----
The function numpy.cross() is not used as it is not optimized for such small
vector.
"""
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 cross product'
)
v_cross
=
np
.
cross
(
v1
,
v2
)
if
vres
is
not
None
:
if
len
(
np
.
shape
(
vres
))
==
1
:
vres
[
1
:]
=
v_cross
else
:
if
(
vres
.
shape
)[
0
]
==
1
:
vres
[
0
,
1
:]
=
v_cross
else
:
vres
[
1
:,
0
]
=
v_cross
else
:
v_cross
=
np
.
append
(
v_cross
.
shape
[
0
],
v_cross
)
return
v_cross
if
vres
is
None
:
vres
=
np
.
zeros
(
3
+
size_4
)
vres
[
size_4
:]
=
[
v1
[
2
]
*
v2
[
3
]
-
v1
[
3
]
*
v2
[
2
],
v1
[
3
]
*
v2
[
1
]
-
v1
[
1
]
*
v2
[
3
],
v1
[
1
]
*
v2
[
2
]
-
v1
[
2
]
*
v2
[
1
]
]
return
vres
def
matrix_vector_product
(
matrix
,
vector
):
...
...
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