-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEigen.cc
384 lines (333 loc) · 9.74 KB
/
Eigen.cc
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include "Eigen.h"
#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <math.h>
#include "MeshSimInternal.h"
using namespace std;
#define ABS(x) ((x) < 0 ? -(x) : (x))
// int normVt (double [],double [])
// {
// return 1;
// }
#ifdef SIM
int normVt(double *v1,double *nv)
{
double norm ;
norm = v1[0]*v1[0] + v1[1]*v1[1] + v1[2]*v1[2] ;
norm = 1./sqrt(norm) ;
nv[0] = v1[0]*norm ;
nv[1] = v1[1]*norm ;
nv[2] = v1[2]*norm ;
return(1) ;
}
void diffVt(double *a,double *b,double *v)
{
v[0] = a[0] - b[0] ;
v[1] = a[1] - b[1] ;
v[2] = a[2] - b[2] ;
}
void crossProd(double *v1, double *v2, double *cp)
{
cp[0] = v1[1]*v2[2] - v1[2]*v2[1] ;
cp[1] = v1[2]*v2[0] - v1[0]*v2[2] ;
cp[2] = v1[0]*v2[1] - v1[1]*v2[0] ;
}
double dotProd(double *v1, double *v2)
{
return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2] ;
}
#endif
struct greater_abs
{
bool operator () (const double &a, const double &b)
{
return fabs(a) > fabs(b);
}
};
long eigen (double pos[3][3], double e[3][3], double v[3], int checkOrthogonality)
{
#ifdef DEBUG
// cout<<"\nin eigen(), eigen computation of matrix\n";
// for(int j=0; j< 3; j++){
// cout<<pos[j][0]<<" "<<pos[j][1]<<" "<<pos[j][2]<<"\n";
// }
#endif
// characteristic polynomial of T :
// solve x^3 + (I[2]/I[3])*x^2 + (I[1]/I[3])*x + (I[0]/I[3])*a3 = 0
// I1 : first invariant , trace(T)
// I2 : second invariant , 1/2 (I1^2 -trace(T^2))
// I3 : third invariant , det T
double I[4];
I[3] = 1.0;
I[2] = - trace(pos);
I[1] = 0.5 * (I[2]*I[2] - trace2(pos));
I[0] = - det(pos);
// printf (" %lf x^3 + %lf x^2 + %lf x + %lf = 0\n",
// I[3],I[2],I[1],I[0]);
// solve x^3 + (I[2]/I[3])*x^2 + (I[1]/I[3])*x + (I[0]/I[3])*a3 = 0
// solve x^3 + a1 x^2 + a2 x + a3 = 0
long nbEigen = FindCubicRoots (I,v);
std::sort(v,v+3, greater_abs() );
// printf ("nbEigen = %d %12.5E %12.5E %12.5E\n",nbEigen,v[0],v[1],v[2]);
double result[12];
int nb_vec=0;
while(1)
{
double a[9] = {pos[0][0]-v[nb_vec],pos[0][1],pos[0][2],
pos[1][0],pos[1][1]-v[nb_vec],pos[1][2],
pos[2][0],pos[2][1],pos[2][2]-v[nb_vec]};
// eps smaller gives better eigenvals (orig 1.0e-3)
double eps = 1.0e-5;
int nb = 0;
while (1)
{
nb = NullSpace (a,result,eps,3);
if (nb != 0)break;
eps *= 2.0;
}
int kk=0;
for (int i=nb_vec;i<nb+nb_vec;i++)
{
e[i][0] = result[0+kk*3];
e[i][1] = result[1+kk*3];
e[i][2] = result[2+kk*3];
normVt (e[i], e[i]);
// printf("%d: %f (%f, %f, %f)\n",i,v[nb_vec],e[i][0],e[i][1],e[i][2]);
kk++;
if (i == 2 && checkOrthogonality) {
int factor;
if( !checkUnitaryOthoganal(e,factor) )
{
printf (" %lf x^3 + %lf x^2 + %lf x + %lf = 0\n",I[3],I[2],I[1],I[0]);
printf ("nbEigen = %d %12.5E %12.5E %12.5E\n",nbEigen,v[0],v[1],v[2]);
for(int jj=0; jj<3; jj++ )
printf("%d: %f (%f, %f, %f)\n",jj,v[jj],e[jj][0],e[jj][1],e[jj][2]);
printf("nb=%d nb_vec=%d nbEigen=%d\n",nb,nb_vec,nbEigen);
printf("WARNING: not orthoganal (eigen)\n\n");
}
// // changing the orientation of thrid vector
// // such that it follows right hand rule
// if(factor==-1) {
// for(int icomp=0;icomp<3;icomp++) {
// e[3][icomp]=factor*e[3][icomp];
// }
// // cout<<"Changing orientation for third eigen-vector"<<endl;
// }
return nbEigen;
}// if (i == 2 && checkOrthog
}//for (int i=nb_v
nb_vec += nb;
if (nb_vec == 3)
return nbEigen;
if( nb_vec > 3 )
return nbEigen;
// throw;
if (nb > 3)
throw;
}//while(1)
}
int checkUnitaryOthoganal(double e[3][3], int &factor)
{
int i;
double dot, n[3];
double tol=1e-14;
double cosalpha, alpha;
for( i=0; i<3; i++ ) {
dot=dotProd(e[i],e[i]);
if( dot < tol )
{ printf("the %d vector in zero length\n",i); return 0; }
if( ABS(dot - 1.) > tol )
{ printf("the %d vector not unitary. lenthSq=%f\n",i,dot); return 0; }
}
dot=dotProd(e[0],e[1]);
cosalpha=dot/sqrt(dotProd(e[0],e[0])*dotProd(e[1],e[1]));
alpha = 57.295718*acos(cosalpha);
if( alpha > 95 && alpha<85 ) {
printf("first two base vectors not orthognal. %f\n",alpha);
return 0;
}
crossProd(e[0],e[1],n);
dot=dotProd(e[2],n);
if(dot<0.)
factor=-1;
cosalpha=dot/sqrt(dotProd(e[2],e[2])*dotProd(n,n));
alpha = 57.295718*acos(cosalpha);
if( alpha < 175 && alpha>5 ) {
printf("third base vector not orthognal to first two. %f\n", alpha);
return 0;
}
return 1;
}
double trace (double pos[3][3])
{
return pos[0][0] + pos[1][1] + pos[2][2];
}
double trace2 (double pos[3][3])
{
double a00 = pos[0][0] * pos[0][0] +
pos[1][0] * pos[0][1] +
pos[2][0] * pos[0][2];
double a11 = pos[1][0] * pos[0][1] +
pos[1][1] * pos[1][1] +
pos[1][2] * pos[2][1];
double a22 = pos[2][0] * pos[0][2] +
pos[2][1] * pos[1][2] +
pos[2][2] * pos[2][2];
return a00 + a11 + a22;
}
double det (double pos[3][3])
{
return pos[0][0] * (pos[1][1] * pos[2][2] - pos[1][2] * pos[2][1]) -
pos[0][1] * (pos[1][0] * pos[2][2] - pos[1][2] * pos[2][0]) +
pos[0][2] * (pos[1][0] * pos[2][1] - pos[1][1] * pos[2][0]);
}
// solve x^2 + b x + c = 0
// x[2] is always set to be zero
long FindQuadraticRoots(const double b, const double c, double x[3])
{
// printf("Quadratic roots\n");
x[2]=0.0;
double delt=b*b-4.*c;
if( delt >=0 ) {
delt=sqrt(delt);
x[0]=(-b+delt)/2.0;
x[1]=(-b-delt)/2.0;
return 3;
}
printf("Imaginary roots, impossible, delt=%f\n",delt);
return 1;
}
// solve x^3 + a1 x^2 + a2 x + a3 = 0
long FindCubicRoots(const double coeff[4], double x[3])
{
double a1 = coeff[2] / coeff[3];
double a2 = coeff[1] / coeff[3];
double a3 = coeff[0] / coeff[3];
if( ABS(a3)<1.0e-8 )
return FindQuadraticRoots(a1,a2,x);
double Q = (a1 * a1 - 3 * a2) / 9.;
double R = (2. * a1 * a1 * a1 - 9. * a1 * a2 + 27. * a3) / 54.;
double Qcubed = Q * Q * Q;
double d = Qcubed - R * R;
// printf ("d = %22.15e Q = %12.5E R = %12.5E Qcubed %12.5E\n",d,Q,R,Qcubed);
/// three roots, 2 equal
if(Qcubed == 0.0 || fabs ( Qcubed - R * R ) < 1.e-8 * (fabs ( Qcubed) + fabs( R * R)) )
{
double theta;
if (Qcubed <= 0.0)theta = acos(1.0);
else if (R / sqrt(Qcubed) > 1.0)theta = acos(1.0);
else if (R / sqrt(Qcubed) < -1.0)theta = acos(-1.0);
else theta = acos(R / sqrt(Qcubed));
double sqrtQ = sqrt(Q);
// printf("sqrtQ = %12.5E teta=%12.5E a1=%12.5E\n",sqrt(Q),theta,a1);
x[0] = -2 * sqrtQ * cos( theta / 3) - a1 / 3;
x[1] = -2 * sqrtQ * cos((theta + 2 * M_PI) / 3) - a1 / 3;
x[2] = -2 * sqrtQ * cos((theta + 4 * M_PI) / 3) - a1 / 3;
return (3);
}
/* Three real roots */
if (d >= 0.0) {
double theta = acos(R / sqrt(Qcubed));
double sqrtQ = sqrt(Q);
x[0] = -2 * sqrtQ * cos( theta / 3) - a1 / 3;
x[1] = -2 * sqrtQ * cos((theta + 2 * M_PI) / 3) - a1 / 3;
x[2] = -2 * sqrtQ * cos((theta + 4 * M_PI) / 3) - a1 / 3;
return (3);
}
/* One real root */
else {
printf("IMPOSSIBLE !!!\n");
double e = pow(sqrt(-d) + fabs(R), 1. / 3.);
if (R > 0)
e = -e;
x[0] = (e + Q / e) - a1 / 3.;
return (1);
}
}
#define MAXN 32
#define R(i,j) result[n*(i)+(j)]
long NullSpace(const double *a, double *result, double eps, long n)
{
int r[MAXN], c[MAXN];
register long i, j, k;
int jj, kk, t;
double max, temp;
int ec;
for (i = 0; i < n; i++)
r[i] = c[i] = -1; /* Reset row and column pivot indices */
// copy the input matrix if not in place
if (result != a)
for (i = 0; i < n*n; i++)
result[i] = a[i];
// rest of algorithm is in place wrt result[]
for (i = 0; i < n; i++) {
/* Find the biggest element in the remaining submatrix
* for the next full pivot.
*/
max = 0.0;
for (k = 0; k < n; k++) {
if (r[k] < 0) {
for (j = 0; j < n; j++) {
if ((c[j] < 0) && ((temp = fabs(R(k, j))) > max)) {
kk = k;
jj = j;
max = temp;
}
}
}
}
if (max < eps)
break; /* Consider this and all subsequent pivots to be zero */
c[jj] = kk; /* The row */
r[kk] = jj; /* and column of the next pivot */
temp = 1.0 / R(kk, jj);
R(kk, jj) = 1.0;
for (j = 0; j < n; j++) /* Should this be for j != jj ? */
R(kk, j) *= temp; /* Row equilibration */
for (k = 0; k < n; k++) { /* Row elimination */
if (k == kk)
continue; /* Don't do a thing to the pivot row */
temp = R(k, jj);
R(k, jj) = 0.0;
for (j = 0; j < n; j++) {
R(k, j) -= temp * R(kk, j); /* Subtract row kk from row k */
if (fabs(R(k, j)) < eps)
R(k, j) = 0.0; /* Flush to zero if too small */
}
}
}
/* Sort into a truncated triangular matrix */
for (j = 0; j < n; j++) { /* For all columns... */
while ((c[j] >= 0) && (j != c[j])) {
for (k = 0; k < n; k++) {
if (r[k] < 0) {
/* Aha! a null column vector */
temp = R(k, j); /* Get it on top */
R(k, j) = R(k, c[j]);
R(k, c[j]) = temp;
}
}
t = c[j]; /* Twiddle until pivots are on the diagonal */
c[j] = c[t];
c[t] = t;
}
}
/* Copy the null space vectors into the top of the A matrix */
ec = 0;
for (k = 0; k < n; k++) {
if (r[k] < 0) {
R(k, k) = 1.0; /* Set the pivot equal to 1 */
if (ec != k) {
for (j = 0; j < n; j++) {
R(ec, j) = R(k, j);
}
}
ec++;
}
}
/* The first ec rows of the matrix a are the vectors which are
* orthogonal to the columns of the matrix a.
*/
return (ec);
}