Subversion Repositories gelsvn

Rev

Rev 342 | Rev 354 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 342 Rev 351
1
#include "eigensolution.h"
1
#include "eigensolution.h"
2
 
2
 
3
#include "Mat2x2f.h"
3
#include "Mat2x2f.h"
4
#include "Mat3x3f.h"
4
#include "Mat3x3f.h"
5
#include "Mat4x4f.h"
5
#include "Mat4x4f.h"
6
#include "Mat2x2d.h"
6
#include "Mat2x2d.h"
7
#include "Mat3x3d.h"
7
#include "Mat3x3d.h"
8
#include "Mat4x4d.h"
8
#include "Mat4x4d.h"
9
 
9
 
10
#include <iostream>
10
#include <iostream>
11
 
11
 
12
using namespace std;
12
using namespace std; 
13
 
13
 
14
 
14
 
15
namespace
15
namespace
16
{
16
{
17
		// During experiments 925 iterations were observed for a hard problem
-
 
18
		// Ten times that should be safe.
-
 
19
		const unsigned int KMAX = 1e6;
17
		const unsigned int KMAX = 1e6;
20
 
-
 
21
		// The threshold below is the smallest that seems to give reliable
-
 
22
		// solutions.
-
 
23
		const double EV_THRESH = 1e-6;
18
		const double EV_THRESH = 1e-6;
24
}
19
}
25
 
20
 
26
namespace CGLA
21
namespace CGLA
27
{
22
{
28
		template <class MT>
23
		template <class MT>
29
		int power_eigensolution(const MT& Ap, MT& Q, MT& L, unsigned int max_sol)
24
		int power_eigensolution(const MT& Ap, MT& Q, MT& L, unsigned int max_sol)
30
		{
25
		{
31
				L = MT(0);
26
				L = MT(0);
32
 
27
 
33
				typedef typename MT::VectorType VT;
28
				typedef typename MT::VectorType VT;
34
				MT A = Ap;
29
				MT A = Ap;
35
				unsigned int n = s_min(MT::get_v_dim(), max_sol);
30
				unsigned int n = s_min(MT::get_v_dim(), max_sol);
36
 
31
 
37
				for(unsigned int i=0;i<n;++i)
32
				for(unsigned int i=0;i<n;++i)
38
				{
33
				{
39
						// Seed the eigenvector estimate
34
						// Seed the eigenvector estimate
40
						VT q(1);
35
						VT q(1);
41
						q.normalize();
36
						q.normalize();
42
						double l,l_old;
37
						double l,l_old;
43
 
38
 
44
						// As long as we haven't reached the max iterations and the
39
						// As long as we haven't reached the max iterations and the
45
						// eigenvalue has not converged, do
40
						// eigenvalue has not converged, do
46
						unsigned int k=0;
41
						unsigned int k=0;
47
						do
42
						do
48
						{
43
						{
49
							const VT z = A * q;
44
							const VT z = A * q;
50
							double z_len = length(z);
45
							double z_len = length(z);
51
							
46
							
52
							if(z_len < EV_THRESH) return i;
47
							if(z_len < EV_THRESH) return i;
53
							
48
							
54
							l_old = l;
49
							l_old = l;
55
							l = dot(q, z)>0 ? z_len : -z_len;
50
							l = dot(q, z)>0 ? z_len : -z_len;
56
							q = z/z_len;
51
							q = z/z_len;
57
								
52
								
58
							if(++k==KMAX)
53
							if(++k==KMAX)
59
								return i;
54
								return i;
60
						}
55
						}
61
						while((fabs(l-l_old) > fabs(EV_THRESH * l)) || k<2);
56
						while((fabs(l-l_old) > fabs(EV_THRESH * l)) || k<2);
62
				
57
				
63
						// Update the solution by adding the eigenvector to Q and
58
						// Update the solution by adding the eigenvector to Q and
64
						// the eigenvalue to the diagonal of L.
59
						// the eigenvalue to the diagonal of L.
65
						Q[i] = q;
60
						Q[i] = q;
66
						L[i][i] = l;
61
						L[i][i] = l;
67
 
62
 
68
						// Update A by subtracting the subspace represented by the 
63
						// Update A by subtracting the subspace represented by the 
69
						// eigensolution just found. This is called the method of 
64
						// eigensolution just found. This is called the method of 
70
						// deflation.
65
						// deflation.
71
						MT B;
66
						MT B;
72
						outer_product(q,q,B);
67
						outer_product(q,q,B);
73
						A = A - l * B;
68
						A = A - l * B;
74
				}
69
				}
75
				return n;
70
				return n;
76
		}
71
		}
77
 
72
 
78
		/* There is no reason to put this template in a header file, since 
73
		/* There is no reason to put this template in a header file, since 
79
			 we will only use it on matrices defined in CGLA. Instead, we 
74
			 we will only use it on matrices defined in CGLA. Instead, we 
80
			 explicitly instantiate the function for the square matrices
75
			 explicitly instantiate the function for the square matrices
81
			 of CGLA */
76
			 of CGLA */
82
		template int power_eigensolution<Mat2x2f>(const Mat2x2f&,
77
		template int power_eigensolution<Mat2x2f>(const Mat2x2f&,
83
																							Mat2x2f&,Mat2x2f&,unsigned int);
78
																							Mat2x2f&,Mat2x2f&,unsigned int);
84
	
79
	
85
		template int power_eigensolution<Mat3x3f>(const Mat3x3f&,
80
		template int power_eigensolution<Mat3x3f>(const Mat3x3f&,
86
																							Mat3x3f&,Mat3x3f&,unsigned int);
81
																							Mat3x3f&,Mat3x3f&,unsigned int);
87
		template int power_eigensolution<Mat4x4f>(const Mat4x4f&,
82
		template int power_eigensolution<Mat4x4f>(const Mat4x4f&,
88
																							Mat4x4f&,Mat4x4f&,unsigned int);
83
																							Mat4x4f&,Mat4x4f&,unsigned int);
89
		template int power_eigensolution<Mat2x2d>(const Mat2x2d&,
84
		template int power_eigensolution<Mat2x2d>(const Mat2x2d&,
90
																							Mat2x2d&,Mat2x2d&,unsigned int);
85
																							Mat2x2d&,Mat2x2d&,unsigned int);
91
	
86
	
92
		template int power_eigensolution<Mat3x3d>(const Mat3x3d&,
87
		template int power_eigensolution<Mat3x3d>(const Mat3x3d&,
93
																							Mat3x3d&,Mat3x3d&,unsigned int);
88
																							Mat3x3d&,Mat3x3d&,unsigned int);
94
		template int power_eigensolution<Mat4x4d>(const Mat4x4d&,
89
		template int power_eigensolution<Mat4x4d>(const Mat4x4d&,
95
																							Mat4x4d&,Mat4x4d&,unsigned int);
90
																							Mat4x4d&,Mat4x4d&,unsigned int);
96
}
91
}
97
 
92
 
98
 
93