Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
671 khor 1
#include <iostream>
2
#include <algorithm>
3
 
680 janba 4
#include <GEL/CGLA/Mat4x4d.h>
5
#include <GEL/CGLA/Mat4x4f.h>
6
#include <GEL/CGLA/Mat2x2f.h>
7
#include <GEL/CGLA/Mat3x3f.h>
8
#include <GEL/CGLA/Mat2x3f.h>
9
#include <GEL/CGLA/eigensolution.h>
671 khor 10
 
680 janba 11
#include <GEL/CGLA/Vec2f.h>
12
#include <GEL/CGLA/Vec2i.h>
13
#include <GEL/CGLA/Vec3i.h>
14
#include <GEL/CGLA/Vec3f.h>
15
#include <GEL/CGLA/Vec3Hf.h>
671 khor 16
 
680 janba 17
#include <GEL/LinAlg/LapackFunc.h>
671 khor 18
 
19
using namespace std;
20
using namespace CGLA;
21
using namespace LinAlg;
22
 
23
double percentdiff(double a, double b)
24
{
25
	return 100.0*fabs(a-b)/fabs(a);
26
}
27
 
28
void power_eigensolution_test()
29
{
30
	gel_srand(0);
31
	double avg_trace_diff=0;
32
	double avg_det_diff=0;
33
	for (int k=0;k<1000;++k)
34
	{
35
		Mat4x4d M;
36
		for(int i=0;i<4;++i) 
37
			for(int j=i;j<4;++j)
38
				M[i][j] = M[j][i] = 1e-5*(gel_rand()-GEL_RAND_MAX/2);
39
 
40
		Mat4x4d Q,L;
41
		power_eigensolution(M,Q,L);
42
 
43
		double det_diff = percentdiff(determinant(M), L[0][0]*L[1][1]*L[2][2]*L[3][3]) ;
44
		double trace_diff= percentdiff(trace(M), L[0][0]+L[1][1]+L[2][2]+L[3][3]);
45
 
46
		if(det_diff > 1)
47
		{
48
			cout << "Matrix " << M << " has Q matrix " <<  Q << " and L " << L << endl;
49
			CMatrix CM(M);
50
			CVector CV = SVD(CM);
51
			cout << "Abs. eigenvalues computed using SVD "; 
52
			for(int i=0;i<4;++i)
53
				cout << (CV[i]) <<  " " ;
54
			cout << endl;
55
			cout << "Det(M) = " << determinant(M) << " trace(m) = " << trace(M) <<endl;
56
 
57
			cout << "Det(M) vs product of eigenvalues, difference = " 
58
				<< det_diff << " %" << endl;
59
			avg_det_diff += det_diff;
60
			cout << "Trace of M vs sum of eigenvalues, difference = " 
61
				<< trace_diff << " %" << endl;
62
			avg_trace_diff += trace_diff;
63
			cout << endl << "-----------------------------------" << endl;
64
		}
65
	}
66
	cout << "average trace diff = " << (avg_trace_diff / 1000) << " %" << endl;
67
	cout << "average det diff = " << (avg_det_diff / 1000) << " %" << endl;
68
 
69
}
70
 
71
 
72
/* This is a non-exhaustive test program for CGLA */
73
int main()
74
{
75
 
76
	Mat2x3f m23(Vec3f(1,1,1),Vec3f(2,2,2));
77
	Mat3x2f m32;
78
 
79
	// Try transpose of non-sq matrix
80
	transpose(m23, m32);
81
	cout << "2 by 3 " << m23 << endl;
82
	cout << "Transpose " << m32 << endl;
83
 
84
 
85
	m32[0] = Vec2f(1,2);
86
	m32[1] = Vec2f(10,20);
87
	m32[2] = Vec2f(100,200);
88
 
89
	Mat2x2f m22;
90
 
91
	// Multiply 2x3 and 3x2 matrices
92
	mul(m23, m32, m22);
93
	cout << "Multiplication of non. sq. mat " << m22 << endl;
94
 
95
	// Check matrix mul using * operator for sq matrices.
96
	Mat2x2f m22_2(1,2,3,4);
97
	cout << "multiplication of sq. mats " << m22_2 * m22 << endl;
98
	cout << "Trace " << trace(m22) << endl;
99
 
100
	m22_2 += m22 + m22;
101
	m22_2 -= 2* m22;
102
 
103
	Mat4x4f m44(0.0f);
104
	m44[0][0] = 1;
105
	m44[1][2] = 2;
106
	m44[2][1] = 3;
107
	m44[3][3] = 4;
108
 
109
	Mat4x4f m44_2 = transpose(m44);
110
	cout << m44 << m44_2 << endl; 
111
 
112
	// Compile should fail if line below is uncommented
113
	// mul(m23, m44, m22);
114
	// Compile should fail if line below is uncommented
115
	// transpose(m23,m44);
116
 
117
	cout << "Determinant of 4x4 " << m44 << determinant(m44) << endl;
118
	cout << "Determinant of 2x2 " << m22 << determinant(m22) << endl;
119
 
120
	Mat4x4f mna(Vec4f(10,120,10,40),
121
							Vec4f(43,10,31254,10),
122
							Vec4f(43,11,54,10),
123
							Vec4f(0,0,0,1));
124
 
125
	cout << fixed << endl;
126
	try
127
		{
128
			cout << "Invert: " << invert_affine(mna) << endl;
129
			cout << "Test: " << invert_affine(mna)*mna << endl;
130
		}
131
	catch(const Mat4x4fException& me)
132
		{
133
			me.print(cout);
134
		}
135
	try
136
		{	
137
			cout << "Invert " << invert(mna) << endl;
138
			cout << "test " << invert(mna)*mna << endl;
139
		}
140
	catch(const Mat4x4fException& me)
141
		{
142
			me.print(cout);
143
		}
144
 
145
	Vec2f v2(2,3);
146
	Vec3f v3(1,2,3);
147
	Mat2x3f m23_2;
148
	outer_product(v2,v3,m23_2);
149
	cout << "outer product of " << v2 << " , " << v3 << " = " << m23_2 << endl;
150
 
151
	m44 = identity_Mat4x4f();
152
 
153
	Mat3x3f m33(Vec3f(1,2,3), Vec3f(4,5,6), Vec3f(7,8,9));
154
 
155
	copy_matrix(m33, m44);
156
 
157
	cout << "The matrix " << m33 << " is copied to " << m44 << endl;
158
 
159
	cout << "Determinant of singular 4x4 matrix" << endl;
160
 
161
 	Mat4x4d m_odd(Vec4d(0, 373, 139129, 1),
162
 								Vec4d(-373, -125, 154754, 1),
163
 								Vec4d(-50.6168, 21.2469, 3013.5, 100),
164
 								Vec4d(-50.6168, 21.2469, 3013.5, 100));
165
 
166
	cout << m_odd << endl;
167
	cout << "Determinant: " << determinant(m_odd) << endl;
168
 
169
	Mat4x4d m_odd_t = transpose(m_odd);
170
 
171
	cout << m_odd_t << endl;
172
	cout << "Det of transpose:" << determinant(m_odd_t) << endl;
173
 
174
	cout << "Determinant of random matrix (MatLab says 0.0491)" << endl;
175
 
176
 
177
	Mat4x4d Mrand(Vec4d(0.9169,0.3529,0.2028,0.1988),
178
								Vec4d(0.4103,0.8132,0.1987,0.0153),
179
								Vec4d(0.8936,0.0099,0.6038,0.7468),
180
								Vec4d(0.0579,0.1389,0.2722,0.4451));
181
 
182
	cout << "det " << determinant(Mrand) << endl;
183
	cout << "det of transpose " << determinant(transpose(Mrand)) << endl;
184
 
185
	cout << "Grand test of power eigensolution scheme " << endl;
186
	power_eigensolution_test();
187
 
188
	return 0;
189
}