Subversion Repositories gelsvn

Rev

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

Rev 354 Rev 366
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
    const unsigned int KMAX = 1000000;
17
    const unsigned int KMAX = 1000000;
18
    const double EV_THRESH = 1e-6;
18
    const double EV_THRESH = 1e-6;
19
}
19
}
20
 
20
 
21
namespace CGLA
21
namespace CGLA
22
{
22
{
23
		template <class MT>
23
		template <class MT>
24
		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)
25
		{
25
		{
26
				L = MT(0);
26
				L = MT(0);
27
 
27
 
28
				typedef typename MT::VectorType VT;
28
				typedef typename MT::VectorType VT;
29
				MT A = Ap;
29
				MT A = Ap;
30
				unsigned int n = s_min(MT::get_v_dim(), max_sol);
30
				unsigned int n = s_min(MT::get_v_dim(), max_sol);
31
 
31
 
32
				for(unsigned int i=0;i<n;++i)
32
				for(unsigned int i=0;i<n;++i)
33
				{
33
				{
34
						// Seed the eigenvector estimate
34
						// Seed the eigenvector estimate
35
						VT q(1);
35
						VT q(1);
36
						q.normalize();
36
						q.normalize();
37
						double l,l_old;
37
						double l=123,l_old;
38
 
38
 
39
						// 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
40
						// eigenvalue has not converged, do
40
						// eigenvalue has not converged, do
41
						unsigned int k=0;
41
						unsigned int k=0;
42
						do
42
						do
43
						{
43
						{
44
							const VT z = A * q;
44
							const VT z = A * q;
45
							double z_len = length(z);
45
							double z_len = length(z);
46
							
46
							
47
							if(z_len < EV_THRESH) return i;
47
							if(z_len < EV_THRESH) return i;
48
							
48
							
49
							l_old = l;
49
							l_old = l;
50
							l = dot(q, z)>0 ? z_len : -z_len;
50
							l = dot(q, z)>0 ? z_len : -z_len;
51
							q = z/z_len;
51
							q = z/z_len;
52
								
52
								
53
							if(++k==KMAX)
53
							if(++k==KMAX)
54
								return i;
54
								return i;
55
						}
55
						}
56
						while((fabs(l-l_old) > fabs(EV_THRESH * l)) || k<2);
56
						while((fabs(l-l_old) > fabs(EV_THRESH * l)) || k<2);
57
				
57
				
58
						// Update the solution by adding the eigenvector to Q and
58
						// Update the solution by adding the eigenvector to Q and
59
						// the eigenvalue to the diagonal of L.
59
						// the eigenvalue to the diagonal of L.
60
						Q[i] = q;
60
						Q[i] = q;
61
						L[i][i] = l;
61
						L[i][i] = l;
62
 
62
 
63
						// Update A by subtracting the subspace represented by the 
63
						// Update A by subtracting the subspace represented by the 
64
						// eigensolution just found. This is called the method of 
64
						// eigensolution just found. This is called the method of 
65
						// deflation.
65
						// deflation.
66
						MT B;
66
						MT B;
67
						outer_product(q,q,B);
67
						outer_product(q,q,B);
68
						A = A - l * B;
68
						A = A - l * B;
69
				}
69
				}
70
				return n;
70
				return n;
71
		}
71
		}
72
 
72
 
73
		/* 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 
74
			 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 
75
			 explicitly instantiate the function for the square matrices
75
			 explicitly instantiate the function for the square matrices
76
			 of CGLA */
76
			 of CGLA */
77
		template int power_eigensolution<Mat2x2f>(const Mat2x2f&,
77
		template int power_eigensolution<Mat2x2f>(const Mat2x2f&,
78
																							Mat2x2f&,Mat2x2f&,unsigned int);
78
																							Mat2x2f&,Mat2x2f&,unsigned int);
79
	
79
	
80
		template int power_eigensolution<Mat3x3f>(const Mat3x3f&,
80
		template int power_eigensolution<Mat3x3f>(const Mat3x3f&,
81
																							Mat3x3f&,Mat3x3f&,unsigned int);
81
																							Mat3x3f&,Mat3x3f&,unsigned int);
82
		template int power_eigensolution<Mat4x4f>(const Mat4x4f&,
82
		template int power_eigensolution<Mat4x4f>(const Mat4x4f&,
83
																							Mat4x4f&,Mat4x4f&,unsigned int);
83
																							Mat4x4f&,Mat4x4f&,unsigned int);
84
		template int power_eigensolution<Mat2x2d>(const Mat2x2d&,
84
		template int power_eigensolution<Mat2x2d>(const Mat2x2d&,
85
																							Mat2x2d&,Mat2x2d&,unsigned int);
85
																							Mat2x2d&,Mat2x2d&,unsigned int);
86
	
86
	
87
		template int power_eigensolution<Mat3x3d>(const Mat3x3d&,
87
		template int power_eigensolution<Mat3x3d>(const Mat3x3d&,
88
																							Mat3x3d&,Mat3x3d&,unsigned int);
88
																							Mat3x3d&,Mat3x3d&,unsigned int);
89
		template int power_eigensolution<Mat4x4d>(const Mat4x4d&,
89
		template int power_eigensolution<Mat4x4d>(const Mat4x4d&,
90
																							Mat4x4d&,Mat4x4d&,unsigned int);
90
																							Mat4x4d&,Mat4x4d&,unsigned int);
91
}
91
}
92
 
92
 
93
 
93