Subversion Repositories gelsvn

Rev

Rev 326 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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