FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
TInd/basic_mat.hpp at master · SimplexZhao/TInd · GitHub
SimplexZhao
/
TInd
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
7
Code
Issues
1
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
TInd
/
basic_mat.hpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
357 lines (323 loc) · 9.03 KB
Breadcrumbs
TInd
/
basic_mat.hpp
Copy path
File metadata and controls
357 lines (323 loc) · 9.03 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
//
A class contains Mat Rotate, Multi & Inv.
#
include
<
iomanip
>
#
include
<
iostream
>
using
namespace
std
;
class
MatOperation
{
public:
MatOperation
(){};
virtual
~MatOperation
(){};
//
Generate R matrix, left=R*right
//
left:image right:world
//
flag==2, φ-ω-κ | flag==1, x-y-z| flag==0, z-y-x
static
void
GetRotationMat
(
double
* mat,
double
a1,
double
a2,
double
a3,
int
flag=
1
)
{
if
(flag==
0
)
{
double
r=a1;
double
p=a2;
double
y=a3;
mat[
0
] =
cos
(r)*
cos
(y);
mat[
1
] = -
cos
(r)*
sin
(y);
mat[
2
] = -
sin
(r);
mat[
3
] =
cos
(p)*
sin
(y)+
sin
(r)*
sin
(p)*
cos
(y);
mat[
4
] =
cos
(p)*
cos
(y)-
sin
(r)*
sin
(p)*
sin
(y);
mat[
5
] =
cos
(r)*
sin
(p);
mat[
6
] =
sin
(r)*
cos
(y)*
cos
(p)-
sin
(p)*
sin
(y);
mat[
7
] = -
cos
(p)*
sin
(r)*
sin
(y)-
sin
(p)*
cos
(y);
mat[
8
] =
cos
(r)*
cos
(p);
}
if
(flag==
1
)
{
double
r=a1;
double
p=a2;
double
y=a3;
mat[
0
] =
cos
(p)*
cos
(y);
mat[
1
] = -
cos
(p)*
sin
(y);
mat[
2
] = -
sin
(p);
mat[
3
] =
cos
(r)*
sin
(y)+
sin
(p)*
sin
(r)*
cos
(y);
mat[
4
] =
cos
(r)*
cos
(y)-
sin
(p)*
sin
(r)*
sin
(y);
mat[
5
] =
cos
(p)*
sin
(r);
mat[
6
] =
sin
(p)*
cos
(y)*
cos
(r)-
sin
(r)*
sin
(y);
mat[
7
] = -
cos
(r)*
sin
(p)*
sin
(y)-
sin
(r)*
cos
(y);
mat[
8
] =
cos
(p)*
cos
(r);
}
if
(flag==
2
)
{
double
phi=a1;
//
a1 b1 c1
double
omega=a2;
//
a2 b2 c2
double
kappa=a3;
//
a3 b3 c3
mat[
0
] =
cos
(phi)*
cos
(kappa) -
sin
(phi)*
sin
(omega)*
sin
(kappa);
mat[
1
] =
cos
(omega)*
sin
(kappa);
mat[
2
] =
sin
(phi)*
cos
(kappa) +
cos
(phi)*
sin
(omega)*
sin
(kappa);
mat[
3
] = -
cos
(phi)*
sin
(kappa) -
sin
(phi)*
sin
(omega)*
cos
(kappa);
mat[
4
] =
cos
(omega)*
cos
(kappa);
mat[
5
] = -
sin
(phi)*
sin
(kappa) +
cos
(phi)*
sin
(omega)*
cos
(kappa);
mat[
6
] = -
sin
(phi)*
cos
(omega);
mat[
7
] = -
sin
(omega);
mat[
8
] =
cos
(phi)*
cos
(omega);
}
}
//
get the vector v in unit length
static
double
getunit
(
double
* v,
int
len=
3
)
{
double
m=
0
;
for
(
int
i=
0
;i<len;++i)m+=v[i]*v[i];
m=
sqrt
(m);
if
(m<
1e-8
)
return
m;
for
(
int
i=
0
;i<len;++i)v[i]/=m;
return
m;
}
//
Generate a square unit matrix
static
void
OnesMatrix
(
double
* mat,
int
width)
{
memset
(mat,
0
,
sizeof
(
double
)*width*width);
int
idx=
0
;
for
(
int
i=
0
;i<width;++i)
{
mat[idx]=
1
;
idx+=width+
1
;
}
}
//
3D vector's cross-product
static
void
Xprod_3d
(
double
*v1,
double
*v2,
double
* res)
{
res[
0
]=v1[
1
]*v2[
2
]-v1[
2
]*v2[
1
];
res[
1
]=-v1[
0
]*v2[
2
]+v1[
2
]*v2[
0
];
res[
2
]=v1[
0
]*v2[
1
]-v1[
1
]*v2[
0
];
}
static
double
Dotprod_nd
(
double
*v1,
double
*v2,
int
len)
{
if
(!(len>
0
))
return
-
0.2333
;
double
result=
0
;
for
(
int
i=
0
;i<len;++i)
{
result+=v1[i]*v2[i];
}
return
result;
}
//
result=v1-v2
static
void
Subtraction
(
double
* result,
double
*v1,
double
*v2,
int
len)
{
if
(!(len>
0
))
return
;
for
(
int
i=
0
;i<len;++i)
{
result[i]=v1[i]-v2[i];
}
}
//
Show a m*n matrix; for mx1 vectors n=1 by default
static
void
ShowMat
(
double
*p,
int
precision,
int
m,
int
n=
1
)
{
if
(!m*n>
0
)
return
;
int
iter=
0
;
cout<<endl<<
"
[
"
;
cout.
flags
(ios::fixed);
cout.
precision
(precision);
for
(
int
i=
0
;i<m-
1
;++i)
{
for
(
int
j=
0
;j<n-
1
;++j)
{
cout<<p[iter]<<
"
,
"
;
++iter;
}
cout<<p[iter]<<
"
;
"
<<endl;
++iter;
}
for
(
int
j=
0
;j<n-
1
;++j)
{
cout<<p[iter]<<
"
,
"
;
++iter;
}
cout<<p[iter]<<
"
]
"
<<endl;
}
static
void
WriteMat
(
double
*p,
int
m,
int
n=
1
,
int
floatprecision=
10
)
{
if
(!m*n>
0
)
return
;
int
iter=
0
;
ofstream
ofs
(
"
mat.csv
"
);
ofs.
flags
(ios::fixed);
ofs.
precision
(floatprecision);
for
(
int
i=
0
;i<m-
1
;++i)
{
for
(
int
j=
0
;j<n-
1
;++j)
{
ofs<<p[iter]<<
"
,
"
;
++iter;
}
ofs<<p[iter]
/*
<<";"
*/
<<endl;
++iter;
}
for
(
int
j=
0
;j<n-
1
;++j)
{
ofs<<p[iter]<<
"
,
"
;
++iter;
}
ofs<<p[iter]
/*
<<";"
*/
<<endl;
ofs.
close
();
}
static
void
Zero
(
double
*p,
int
n)
{
if
(n ==
0
)
return
;
while
(n--) *p++ =
0
;
}
//
src m*n, unable to rotate self.
static
bool
Rotate
(
double
*src,
double
*dst,
int
m,
int
n)
{
if
(src ==
0
|| dst ==
0
)
return
0
;
double
*ps = src, *pd = dst;
for
(
int
i =
0
; i < m; i++)
{
pd = dst + i;
for
(
int
j =
0
; j < n; j++)
{
*pd = *ps;
pd += m;
++ps;
}
}
pd = ps =
0
;
return
1
;
}
//
/////////////////////////////////////////////
//
设A为m x l阶矩阵,B为l x n阶矩阵,C为m x n阶矩阵,计算 C=A x B的子程序为:
static
bool
MatrixMulti
(
double
* A,
double
* B,
double
* C,
int
M,
int
N,
int
L)
{
if
(A ==
0
|| B ==
0
|| C ==
0
)
return
0
;
int
i, j, k;
Zero
(C, M*N);
for
(i =
0
; i<M; i++)
{
for
(j =
0
; j<N; j++)
{
//
for (k=0;k<L;k++) C[i*N+j] += A[i*L+k]*B[k*N+j];
for
(k =
0
; k<L; k++) *(C + i*N + j) += *(A + i*L + k)* *(B + k*N + j);
//
等效!!!
}
}
return
1
;
}
//
10*10 以及更小对称正定矩阵求逆 a为n*n阶对称正定矩阵,n为矩阵阶数
static
int
MatrixInversion
(
double
*a,
int
n)
{
int
i, j, k, m;
double
w, g, *b;
b =
new
double
[n];
for
(k =
0
; k <= n -
1
; k++)
{
w = a[
0
] +
1.0e-25
;
m = n - k -
1
;
for
(i =
1
; i < n; i++)
{
g = a[i * n];
b[i] = g / w;
if
(i <= m)
b[i] = -b[i];
int
tmpOffset1 = (i -
1
) * n -
1
;
int
tmpOffset2 = i * n;
for
(j =
1
; j <= i; j++)
a[tmpOffset1 + j] = a[tmpOffset2 + j] + g * b[j];
}
a[n * n -
1
] =
1.0
/ w;
for
(i =
1
; i <= n -
1
; i++)
a[(n -
1
) * n + i -
1
] = b[i];
}
for
(i =
0
; i <= n-
2
; i++)
for
(j = i +
1
; j <= n-
1
; j++)
a[i * n + j] = a[j * n + i];
delete[]
b; b =
0
;
return
(
2
);
}
//
10*10 以及更小矩阵求逆
static
bool
MatrixInversion_General
(
double
*a,
int
n)
{
int
*is, *js, i, j, k, l, u, v;
double
d, p;
is =
new
int
[n];
//
= malloc(n *sizeof(int));
js =
new
int
[n];
//
= malloc(n *sizeof(int));
for
(k =
0
; k <= n -
1
; k++)
{
d =
0.0
;
for
(i = k; i <= n -
1
; i++)
for
(j = k; j <= n -
1
; j++)
{
l = i * n + j;
p =
fabs
(a[l]);
if
(p > d)
{
d = p;
is[k] = i;
js[k] = j;
}
}
if
(d +
1.0
==
1.0
)
{
delete
is;
delete
js;
return
(
0
);
}
if
(is[k] != k)
for
(j =
0
; j <= n -
1
; j++)
{
u = k * n + j;
v = is[k] * n + j;
p = a[u];
a[u] = a[v];
a[v] = p;
}
if
(js[k] != k)
for
(i =
0
; i <= n -
1
; i++)
{
u = i * n + k;
v = i * n + js[k];
p = a[u];
a[u] = a[v];
a[v] = p;
}
l = k * n + k;
a[l] =
1.0
/ a[l];
for
(j =
0
; j <= n -
1
; j++)
if
(j != k)
{
u = k * n + j;
a[u] = a[u] * a[l];
}
for
(i =
0
; i <= n -
1
; i++)
if
(i != k)
for
(j =
0
; j <= n -
1
; j++)
if
(j != k)
{
u = i * n + j;
a[u] = a[u] - a[i * n + k] * a[k * n + j];
}
for
(i =
0
; i <= n -
1
; i++)
if
(i != k)
{
u = i * n + k;
a[u] = -a[u] * a[l];
}
}
for
(k = n -
1
; k >=
0
; k--)
{
if
(js[k] != k)
for
(j =
0
; j <= n -
1
; j++)
{
u = k * n + j;
v = js[k] * n + j;
p = a[u];
a[u] = a[v];
a[v] = p;
}
if
(is[k] != k)
for
(i =
0
; i <= n -
1
; i++)
{
u = i * n + k;
v = i * n + is[k];
p = a[u];
a[u] = a[v];
a[v] = p;
}
}
delete
is;
delete
js;
return
(
1
);
}
//
---------------------------------------------------
};
Back
|
FazBrowse Home
|
New Git URL