-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_funcs.pyx
306 lines (234 loc) · 6.96 KB
/
helper_funcs.pyx
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# distutils: language=c++
# distutils: define_macros=NPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION
"""
Created on Fri Mar 18 08:48:51 2022
Auxiliary mathematical and array manipulation funcions
@author: leonardo
"""
import numpy as np
import cython
cimport numpy as np
@cython.boundscheck(False)
@cython.wraparound(False)
cpdef unitaryVector(double [:] vec):
'''
Returns an unit vector (norm = 1) in the same direction of the input vector
PARAMETERS
----------
vec: double array
input vector
RETURNS
-------
array, double
the unitary vector and the original vector norm
'''
cdef Py_ssize_t i
cdef double norma = 0
cdef double[:] unitVec = np.zeros(3)
for i in range(vec.shape[0]):
norma += vec[i] * vec[i]
if norma != 0:
norma = norma**0.5
for i in range(vec.shape[0]):
unitVec[i] = vec[i] / norma
else:
norma = 0.0
for i in range(vec.shape[0]):
unitVec[i] = 0.0
return np.array(unitVec), norma
cpdef singleRotationMatrix(double angleRad, Py_ssize_t order):
'''
singleRotationMatrix
Returns the 3x3 rotation matrix around the axis specified by order.
Parameters
----------
angleRad: double
The angle value in radians
order: long integer
The axis of the rotation.
Returns
-------
R: array of doubles (C style)
A 3x3 array with the rotation matrix
'''
cdef double [3][3] R
cdef double ca, sa
cdef list line
cdef Py_ssize_t i, j, k
for i in range(3):
for j in range(3):
if i == j:
R[i][j] = 1.
else:
R[i][j] = 0.
if angleRad != 0:
ca = np.cos(angleRad)
sa = np.sin(angleRad)
line = [ca,-sa,sa,ca]
k = 0
for i in range(3):
if i != order:
for j in range(3):
if j != order:
R[i][j] = line[k]
k += 1
return R
cpdef singleRotationMatrixDerivative(double angleRad, Py_ssize_t order):
'''
singleRotationMatrixDarivative
Returns the dreivative of the 3x3 rotation matrix around the axis specified by order.
Parameters
----------
angleRad: double
The angle value in radians
order: long integer
The axis of the rotation.
Returns
-------
R: array of doubles (C style)
A 3x3 array with the rotation matrix derivative with respect to order
'''
cdef double [3][3] R
cdef double ca, sa
cdef list line
cdef Py_ssize_t i, j, k
for i in range(3):
for j in range(3):
R[i][j] = 0.
ca = np.cos(angleRad)
sa = np.sin(angleRad)
line = [-sa,-ca,ca,-sa]
k = 0
for i in range(3):
if i != order:
for j in range(3):
if j != order:
R[i][j] = line[k]
k += 1
return R
cpdef cardanRotationMatrix(double[:] anglesInRad):
'''
rotationMatrix(anglesInRad)
Creates a rotation matrix given a vector angleInRad containing the three
rotations in the order 1,2,3 (Cardan angles)
Parameters
----------
anglesInRad : array
Angles of rotation given in rad following Cardan's convention.
Returns
-------
The rotation matrix.
'''
cdef double a,b,c
a = anglesInRad[0]
b = anglesInRad[1]
c = anglesInRad[2]
if b == c == 0.:
# rotation around x
return np.array(singleRotationMatrix(a,0))
elif a == b == 0.:
# rotation around z
return np.array(singleRotationMatrix(c,2))
elif a == c == 0.:
# rotation around y
return np.array(singleRotationMatrix(-b,1))
elif a == 0.:
# rotation around y and z
cb = np.cos(b)
sb = np.sin(b)
cc = np.cos(c)
sc = np.sin(c)
return np.array([
[cb*cc, -sc, sb*cc],
[sc*cb, cc, sb*sc],
[-sb, 0.0, cb ]
])
elif b == 0.:
# rotation around x and z
ca = np.cos(a)
sa = np.sin(a)
cc = np.cos(c)
sc = np.sin(c)
return np.array([
[cc, -sc*ca, sa*sc],
[sc, ca*cc, -sa*cc],
[0.0, sa, ca]
])
elif c == 0.:
# rotation around x and y
ca = np.cos(a)
sa = np.sin(a)
cb = np.cos(b)
sb = np.sin(b)
return np.array([
[cb, sa*sb, sb*ca],
[0., ca, -sa],
[-sb, sa*cb, ca*cb]
])
else:
# all rotations
ca, sa = np.cos(a), np.sin(a)
cb, sb = np.cos(b), np.sin(b)
cc, sc = np.cos(c), np.sin(c)
return np.array([
[cb*cc, sa*sb*cc - sc*ca, sa*sc + sb*ca*cc],
[sc*cb, sa*sb*sc + ca*cc, -sa*cc + sb*sc*ca],
[ -sb, sa*cb, ca*cb]
])
cpdef cardanRotationMatrixDerivative(double[:] anglesInRad, long derAxis):
'''
rotationMatrix(anglesInRad)
Creates a rotation matrix given a vector angleInRad containing the three
rotations in the order 1,2,3 (Cardan angles)
Parameters
----------
anglesInRad : array
Angles of rotation given in rad following Cardan's convention.
derAxis: long integer
The number (0,1, or 2) that the defines the axis of the derivative
dR
----------
da_derAXis
Returns
-------
The rotation matrix.
'''
cdef double a,b,c
cdef Py_ssize_t i
cdef list R = []
for i in range(3):
if i == derAxis:
R.append(np.array(singleRotationMatrixDerivative(anglesInRad[i],i)))
else:
R.append(np.array(singleRotationMatrix(anglesInRad[i],i)))
return R[2].dot(R[1].dot(R[0]))
cpdef skew(double [:] vec):
'''
Returns the skew symmetric matrix associated to the vector
Parameters
----------
vec : array of 3 doubles
THe input vector
Returns
-------
skew : np.array of size 3x3
The skew symmetric matrix associated to the vector
'''
return np.array([[0.0,-vec[2],vec[1]],
[vec[2],0,-vec[0]],
[-vec[1],vec[0],0.0]])
cpdef mvRow2List(Py_ssize_t[:,:] arr, Py_ssize_t row):
"""
Convert a memory view row into a list
"""
cdef int ncols = arr.shape[1]
cdef Py_ssize_t[:] mv_row = np.zeros(ncols, dtype=np.int64)
cdef Py_ssize_t i
for i in range(ncols):
mv_row[i] = arr[row, i]
return mv_row
if __name__ == "__main__":
v = np.zeros(3)
u = unitaryVector(v)