-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheigens.c
144 lines (144 loc) · 3.01 KB
/
eigens.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <stdio.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_eigen.h>
int main (void)
{
double data[] = { 1.0 , 1/2.0, 1/3.0, 1/4.0,
1/2.0, 1/3.0, 1/4.0, 1/5.0,
1/3.0, 1/4.0, 1/5.0, 1/6.0,
1/4.0, 1/5.0, 1/6.0, 1/7.0 };
gsl_matrix_view m = gsl_matrix_view_array (data, 4, 4);
gsl_vector *eval = gsl_vector_alloc (4);
gsl_matrix *evec = gsl_matrix_alloc (4, 4);
gsl_eigen_symmv_workspace * w =
gsl_eigen_symmv_alloc (4);
gsl_eigen_symmv (&m.matrix, eval, evec, w);
gsl_eigen_symmv_free (w);
gsl_eigen_symmv_sort (eval, evec,
GSL_EIGEN_SORT_ABS_ASC);
{
int i;
for (i = 0; i < 4; i++)
{
double eval_i
= gsl_vector_get (eval, i);
gsl_vector_view evec_i
= gsl_matrix_column (evec, i);
printf ("eigenvalue = %g\n", eval_i);
printf ("eigenvector = \n");
gsl_vector_fprintf (stdout,
&evec_i.vector, "%g");
}
}
gsl_vector_free (eval);
gsl_matrix_free (evec);
//Chapter 15: Eigensystems
//170
return 0;
}
/*Here is the beginning of the output from the program,*/
/* $ ./a.out*/
//eigenvalue = 9.67023e-05
//eigenvector =
//-0.0291933
//0.328712
//-0.791411
//0.514553
//...
//This can be compared with the corresponding output from
//gnu octave
//,
//octave> [v,d] = eig(hilb(4));
//octave> diag(d)
//ans =
//9.6702e-05
//6.7383e-03
//1.6914e-01
//1.5002e+00
//octave> v
//v =
//0.029193 0.179186 -0.582076 0.792608
//-0.328712 -0.741918 0.370502 0.451923
//0.791411 0.100228 0.509579 0.322416
//-0.514553 0.638283 0.514048 0.252161
//Note that the eigenvectors can differ by a change of sign, since
//the sign of an eigenvector is
//arbitrary.
//The following program illustrates the use of the nonsymmetr
//ic eigensolver, by computing
//the eigenvalues and eigenvectors of the Vandermonde matrix
//V
//(
//x
//;
//i,j
//) =
//x
//n
//−
//j
//i
//with
//x
//=
//(
//−
//1
//,
//−
//2
//,
//3
//,
//4).
//#include <stdio.h>
//#include <gsl/gsl_math.h>
//#include <gsl/gsl_eigen.h>
//int
//main (void)
//{
//double data[] = { -1.0, 1.0, -1.0, 1.0,
//-8.0, 4.0, -2.0, 1.0,
//27.0, 9.0, 3.0, 1.0,
//64.0, 16.0, 4.0, 1.0 };
//Chapter 15: Eigensystems
//171
//gsl_matrix_view m
//= gsl_matrix_view_array (data, 4, 4);
//gsl_vector_complex *eval = gsl_vector_complex_alloc (4)
//;
//gsl_matrix_complex *evec = gsl_matrix_complex_alloc (4,
//4);
//gsl_eigen_nonsymmv_workspace * w =
//gsl_eigen_nonsymmv_alloc (4);
//gsl_eigen_nonsymmv (&m.matrix, eval, evec, w);
//gsl_eigen_nonsymmv_free (w);
//gsl_eigen_nonsymmv_sort (eval, evec,
//GSL_EIGEN_SORT_ABS_DESC);
//{
//int i, j;
//for (i = 0; i < 4; i++)
//{
//gsl_complex eval_i
//= gsl_vector_complex_get (eval, i);
//gsl_vector_complex_view evec_i
//= gsl_matrix_complex_column (evec, i);
//printf ("eigenvalue = %g + %gi\n",
//GSL_REAL(eval_i), GSL_IMAG(eval_i));
//'printf ("eigenvector = \n");
//for (j = 0; j < 4; ++j)
//{
//gsl_complex z =
//gsl_vector_complex_get(&evec_i.vector, j);
//printf("%g + %gi\n", GSL_REAL(z), GSL_IMAG(z));
//}
//}
//}
//gsl_vector_complex_free(eval);
//gsl_matrix_complex_free(evec);
//return 0;
//}
//Here is the beginning of the output from the program,
//$ ./a.out
//././
//