1 | |
---|
2 | #include <stdio.h> |
---|
3 | #include <math.h> |
---|
4 | #include "matrix.h" |
---|
5 | |
---|
6 | |
---|
7 | Vector Matrix::eigenValues() const |
---|
8 | { |
---|
9 | Vector eigVl; |
---|
10 | |
---|
11 | float a = 0; |
---|
12 | float b = 0; |
---|
13 | |
---|
14 | float c[3]; |
---|
15 | |
---|
16 | // c[0] is the determinante of mat |
---|
17 | c[0] = this->m11 * this->m22 * this->m33 + |
---|
18 | 2* this->m12 * this->m13 * this->m23 - |
---|
19 | this->m11 * this->m23 * this->m23 - |
---|
20 | this->m22 * this->m13 * this->m13 - |
---|
21 | this->m33 * this->m12 * this->m12; |
---|
22 | |
---|
23 | // c[1] is the trace of a |
---|
24 | c[1] = this->m11 * this->m22 - |
---|
25 | this->m12 * this->m12 + |
---|
26 | this->m11 * this->m33 - |
---|
27 | this->m13 * this->m13 + |
---|
28 | this->m22 * this->m33 - |
---|
29 | this->m23 * this->m23; |
---|
30 | |
---|
31 | // c[2] is the sum of the diagonal elements |
---|
32 | c[2] = this->m11 + |
---|
33 | this->m22 + |
---|
34 | this->m33; |
---|
35 | |
---|
36 | |
---|
37 | // Computing the roots: |
---|
38 | a = (3.0*c[1] - c[2]*c[2]) / 3.0; |
---|
39 | b = (-2.0*c[2]*c[2]*c[2] + 9.0*c[1]*c[2] - 27.0*c[0]) / 27.0; |
---|
40 | |
---|
41 | float Q = b*b/4.0 + a*a*a/27.0; |
---|
42 | |
---|
43 | // 3 distinct Roots |
---|
44 | if (Q < 0) |
---|
45 | { |
---|
46 | printf("good\n"); |
---|
47 | float psi = atan2(sqrt(-Q), -b/2.0); |
---|
48 | float p = sqrt((b/2.0)*(b/2.0) - Q); |
---|
49 | |
---|
50 | eigVl.x = c[2]/3.0 + 2 * pow(p, 1/3.0) * cos(psi/3.0); |
---|
51 | eigVl.y = c[2]/3.0 - pow(p, 1/3.0) * (cos(psi/3.0) |
---|
52 | + sqrt(3.0) * sin(psi/3.0)); |
---|
53 | eigVl.z = c[2]/3.0 - pow(p, 1/3.0) * (cos(psi/3.0) |
---|
54 | - sqrt(3.0) * sin(psi/3.0)); |
---|
55 | |
---|
56 | } |
---|
57 | // 2 Distinct Roots |
---|
58 | else if (Q == 0) |
---|
59 | { |
---|
60 | eigVl.x = c[2]/3.0 + pow(b/2.0, 1.0/3.0); |
---|
61 | eigVl.y = c[2]/3.0 + pow(b/2.0, 1.0/3.0); |
---|
62 | eigVl.z = c[2]/3.0 + 2* pow(b/2.0, 1.0/3.0); |
---|
63 | } |
---|
64 | // 1 Root (not calculating anything.) |
---|
65 | else if (Q > 0) |
---|
66 | { |
---|
67 | printf("A is multiple of Identity matrix (lambda * I3))\n"); |
---|
68 | eigVl.x = eigVl.y = eigVl.z = 1; |
---|
69 | } |
---|
70 | printf("%f %f %f\n", eigVl.x, eigVl.y, eigVl.z); |
---|
71 | return eigVl; |
---|
72 | |
---|
73 | } |
---|
74 | |
---|
75 | void Matrix::eigenVectors(Vector& a, Vector& b, Vector& c) const |
---|
76 | { |
---|
77 | Vector eigVl = this->eigenValues(); |
---|
78 | |
---|
79 | float eigVc[9]; |
---|
80 | // EigenVectors |
---|
81 | for (int i = 0; i < 3; ++i) |
---|
82 | { |
---|
83 | printf (":: i = %d\n", i); |
---|
84 | Matrix M = *this - Matrix::identity() * eigVl.x; |
---|
85 | Vector m1, m2, m3; |
---|
86 | |
---|
87 | M.getTransposed().toVectors(m1, m2, m3); |
---|
88 | |
---|
89 | Vector u1, u2, u3; |
---|
90 | |
---|
91 | u1 = m2.cross(m3); u1 /= u1.len(); |
---|
92 | u2 = m3.cross(m1); u2 /= u2.len(); |
---|
93 | u3 = m1.cross(m2); u3 /= u3.len(); |
---|
94 | |
---|
95 | printf("%f, %f, %f\n", u1.x, u1.y, u1.z); |
---|
96 | printf("%f, %f, %f\n", u2.x, u2.y, u2.z); |
---|
97 | printf("%f, %f, %f\n", u3.x, u3.y, u3.z); |
---|
98 | |
---|
99 | // u1 = M*u1; |
---|
100 | // u2 = M*u2; |
---|
101 | // u3 = M*u3; |
---|
102 | // |
---|
103 | // printf("%f, %f, %f\n", u1.x, u1.y, u1.z); |
---|
104 | // printf("%f, %f, %f\n", u2.x, u2.y, u2.z); |
---|
105 | // printf("%f, %f, %f\n", u3.x, u3.y, u3.z); |
---|
106 | // printf("\n\n"); |
---|
107 | } |
---|
108 | |
---|
109 | Vector eigVc[3]; |
---|
110 | /* eigenvec test */ |
---|
111 | for(int i = 0; i < 3; i++) |
---|
112 | { |
---|
113 | eigVc[i].x =-1/this->m13*(this->m33 - eigValue[i]) + (this->m32*(-this->m31*this->m32 + this->m12*this->m33 - this->m12*eigValue[i])) / |
---|
114 | this->m13*(-this->m13*this->m22 - this->m12*this->m23 + this->m13*eigValue[i]); |
---|
115 | eigVc[i].y = -( -this->m13*this->m23 + this->m12*this->m33 - this->m12*eigValue[i]) / |
---|
116 | (-this->m31*this->m22 + this->m12*this->m23 + this->m13*eigValue[i]); |
---|
117 | eigVc[i].z = 1.0f; |
---|
118 | |
---|
119 | |
---|
120 | printf("home brewn: %f, %f, %f\n", eigVc[i].x, eigVc[i].y, eigVc[i].z); |
---|
121 | |
---|
122 | } |
---|
123 | |
---|
124 | |
---|
125 | |
---|
126 | this->debug(); |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | void Matrix::debug() const |
---|
131 | { |
---|
132 | printf("input: | %f | %f | %f |\n", this->m11, this->m12, this->m13 ); |
---|
133 | printf(" | %f | %f | %f |\n", this->m21, this->m22, this->m23 ); |
---|
134 | printf(" | %f | %f | %f |\n", this->m31, this->m32, this->m33 ); |
---|
135 | |
---|
136 | } |
---|
137 | |
---|