-
Notifications
You must be signed in to change notification settings - Fork 8
/
massmatrix.py
181 lines (148 loc) · 5.5 KB
/
massmatrix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# coding=utf-8
"""Functions for working with mass matrices.
"""
__author__ = (u"Sébastien BARTHÉLEMY <[email protected]>")
import arboris.homogeneousmatrix as Hg
from numpy import diag, eye, dot, array, allclose
from numpy.linalg import eig, eigvals, det, norm
def ismassmatrix(M, semi=False):
"""Check whether M is a valid mass matrix.
Return ``True`` if M is correctly shaped and symmetric positive
definite.
When ``semi`` is set to ``True``, positive *semi*-definite matrices
are also considered valid.
"""
common = M.shape == (6,6) and allclose(M, M.T) and \
allclose(M[3:6, 3:6], M[3,3]*eye(3))
if semi:
return common and (eigvals(M) >= 0.).all()
else:
return common and (eigvals(M) > 0.).all()
def transport(M, H):
"""Transport (express) the mass matrix into another frame.
:param M: the mass matrix expressed in the original frame (say, `a`)
:type M: (6,6)-shaped array
:param H: homogeneous matrix from the new frame (say `b`) to the
original one: `H_{ab}`
:type H: (4,4)-shaped array
:rtype: (6,6)-shaped array
**Example:**
>>> M_a = diag((3., 2., 4., 1., 1., 1.))
>>> H_ab = Hg.transl(1., 3., 0.)
>>> M_b = transport(M_a, H_ab)
>>> allclose(M_b, [[ 12., -3., 0., 0., 0., -3.],
... [ -3., 3., 0., 0., 0., 1.],
... [ 0., 0., 14., 3., -1., 0.],
... [ 0., 0., 3., 1., 0., 0.],
... [ 0., 0., -1., 0., 1., 0.],
... [ -3., 1., 0., 0., 0., 1.]])
True
>>> ismassmatrix(M_b)
True
>>> from math import pi
>>> H_ab = Hg.rotx(pi/4)
>>> M_b = transport(M_a, H_ab)
>>> allclose(M_b, [[ 3., 0., 0., 0., 0., 0.],
... [ 0., 3., 1., 0., 0., 0.],
... [ 0., 1., 3., 0., 0., 0.],
... [ 0., 0., 0., 1., 0., 0.],
... [ 0., 0., 0., 0., 1., 0.],
... [ 0., 0., 0., 0., 0., 1.]])
True
>>> ismassmatrix(M_b)
True
"""
assert ismassmatrix(M)
assert Hg.ishomogeneousmatrix(H)
Ad = Hg.adjoint(H)
return dot(Ad.T, dot(M, Ad))
def principalframe(M):
"""Find the principal frame of inertia of a mass matrix.
:param M: mass matrix expressed in any frame (say `a`)
:type M: (6,6)-shaped array
:rtype: (4,4)-shaped array
Returns the homogeneous matrix `H_{am}` to the principal inertia
frame `m`
**Example:**
>>> M_a = diag((3.,2.,4.,1.,1.,1.))
>>> H_ab = Hg.transl(1., 3., 0.)
>>> M_b = transport(M_a, H_ab)
>>> H_ba = principalframe(M_b)
>>> dot(H_ab, H_ba)
array([[ 1., 0., 0., 0.],
[ 0., 1., 0., 0.],
[ 0., 0., 1., 0.],
[ 0., 0., 0., 1.]])
"""
assert ismassmatrix(M)
m = M[5,5]
rx = M[0:3, 3:6]/m
H = eye(4)
H[0:3, 3] = array([rx[2, 1], rx[0, 2], rx[1, 0]]) #TODO: use a function
RSR = M[0:3, 0:3] + m*dot(rx, rx)
[S, R] = eig(RSR)
if det(R)<0.:
iI = array([[0, 0, 1],[0, 1, 0], [1, 0, 0]])
R = dot(R, iI)
S = dot(iI, dot(S, iI))
H[0:3, 0:3] = R
return H
def box(half_extents, mass):
"""Mass matrix of an homogeneous parallelepiped.
**Example:**
>>> box((2., 4., 6.), 3.)
array([[ 52., 0., 0., 0., 0., 0.],
[ 0., 40., 0., 0., 0., 0.],
[ 0., 0., 20., 0., 0., 0.],
[ 0., 0., 0., 3., 0., 0.],
[ 0., 0., 0., 0., 3., 0.],
[ 0., 0., 0., 0., 0., 3.]])
"""
(x, y, z) = half_extents
Ix = mass/3.*(y**2+z**2)
Iy = mass/3.*(x**2+z**2)
Iz = mass/3.*(x**2+y**2)
return diag( (Ix, Iy, Iz, mass, mass, mass) )
def ellipsoid(radii, mass):
"""Mass matrix of an homogeneous ellipsoid.
Dimensions are expressed at x,y,z coordinates
>>> ellipsoid((3.,1.,2.), 5.)
array([[ 5., 0., 0., 0., 0., 0.],
[ 0., 13., 0., 0., 0., 0.],
[ 0., 0., 10., 0., 0., 0.],
[ 0., 0., 0., 5., 0., 0.],
[ 0., 0., 0., 0., 5., 0.],
[ 0., 0., 0., 0., 0., 5.]])
"""
(x, y, z) = radii
Ix = mass/5.*(y**2+z**2)
Iy = mass/5.*(x**2+z**2)
Iz = mass/5.*(x**2+y**2)
return diag( (Ix, Iy, Iz, mass, mass, mass) )
def cylinder(length, radius, mass):
"""Mass matrix of an homogeneous cylinder, whose symmetry axis is along the z-axis.
>>> cylinder(1., 0.1, 12.)
array([[ 1.03, 0. , 0. , 0. , 0. , 0. ],
[ 0. , 1.03, 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0.06, 0. , 0. , 0. ],
[ 0. , 0. , 0. , 12. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 12. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 12. ]])
"""
L = length
R = radius
Itan = mass*(R**2/4. + L**2/12.)
Iaxe = mass*R**2/2.
return diag( (Itan, Itan, Iaxe, mass, mass, mass) )
def sphere(radius, mass):
"""Mass matrix of an homogeneous sphere.
>>> sphere(1., 5.)
array([[ 2., 0., 0., 0., 0., 0.],
[ 0., 2., 0., 0., 0., 0.],
[ 0., 0., 2., 0., 0., 0.],
[ 0., 0., 0., 5., 0., 0.],
[ 0., 0., 0., 0., 5., 0.],
[ 0., 0., 0., 0., 0., 5.]])
"""
I = 2.*mass*radius**2/5.
return diag( (I, I, I, mass, mass, mass) )