Subversion Repositories gelsvn

Rev

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

Rev Author Line No. Line
83 jab 1
//disable the stl warning of debug information conflicting with names of longer than 255 char.
2
#pragma warning( disable :4786 )  
3
 
4
#include <iostream>
5
#include <vector>
6
#include "LinAlg/Matrix.h"
7
#include "LinAlg/Vector.h"
8
#include "LinAlg/LapackFunc.h"
9
 
10
using namespace std;
11
using namespace LinAlg;
12
 
13
int main()
14
{
15
		// Matrix row and column dimensions
16
		int N=4,M=4;
17
 
18
		// Fill the matrix with random values.
19
		srand(time(0));
20
		CMatrix mat(N,M,0);
21
		for(int i=0;i<N;++i)
22
				for(int j=0;j<M;++j)
23
						mat.set(i,j,rand()/double(RAND_MAX)-0.5);
24
 
25
		// Create a vector of random values
26
		CVector b(N);
27
		for(int i=0;i<N;++i)
28
				b.set(i, rand()/double(RAND_MAX)-0.5);
29
 
30
		// Some output
31
		cout << "Matrix rows, cols = "; 
32
		cout << mat.Rows() << " , " << mat.Cols() << endl;
33
		cout << "Matrix: " << mat << endl;
34
		cout << "Vector: " << b << endl;
35
 
36
		// Use lapack function to do least squares solution
37
		CVector x(M);
38
		x = LinearLSSolve(mat,b);
39
		cout << "Least squares solution: " << x << endl;
40
 
41
		// Of course if the system has full rank, we can use
42
		//CVector LinearSolve(const CMatrix& A,const CVector&b);
43
 
44
		// Manual (and less efficient least squares solution ...
45
		CMatrix mat_T = mat.Transposed(); 
46
		CMatrix mat2 = mat_T * mat;
47
		Invert(mat2);
48
		mat2 = mat2 * mat_T;
49
 
50
		CVector x2(M);
51
		x2 = mat2 * b;
52
		cout << "Least squares solution: " << x2 << endl;
53
 
54
}