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
|
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void elmhes(float **a, int n);
#define SWAP(g,h) {y=(g);(g)=(h);(h)=y;}
main ()
{
/* METHODE 1 */
float **test_a;
int taille = 3, taille2 = 3, i, j;
/* Allocation de la 1er dimension */
test_a = malloc ( sizeof(*test_a) * taille);
/* Allocation des tableaux */
for (i=0; i<taille; i++)
{
test_a[i] = malloc ( sizeof(**test_a) * taille2);
}
test_a [0][0] = 1;
test_a [0][1] = 2;
test_a [0][2] = 3;
test_a [1][0] = 4;
test_a [1][1] = 5;
test_a [1][2] = 6;
test_a [2][0] = 7;
test_a [2][1] = 8;
test_a [2][2] = 9;
elmhes(test_a, 3);
/* Printing */
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
//printf ("xn(%d) = %f\n", i, xn[i]);
//printf ("test_X(%d,%d) = %f\n", i, j, gsl_matrix_get (test_X, i, j));
//sort = * test_a [i][i];
printf ("test_a(%d,%d) = %f\n", i, j, test_a[i][j]);
}
}
}
void elmhes(float **a, int n)
{
/*
Reduction to Hessenberg form by the elimination method. The real, nonsymmetric matrix
a[1..n][1..n] is replaced by an upper Hessenberg matrix with identical eigenvalues. Recommended,
but not required, is that this routine be preceded by balanc. On output, the
Hessenberg matrix is in elements a[i][j] with i = j+1. Elements with i > j+1 are to be
thought of as zero, but are returned with random values.
*/
int m,j,i;
float y,x;
for (m=2;m<n;m++) {
x=0.0;
i=m;
for (j=m;j<=n;j++) {
if (fabs(a[j][m-1]) > fabs(x)) {
x=a[j][m-1];
i=j;
//printf("x=%f\n", x);
}
}
if (i != m) {
for (j=m-1;j<=n;j++) SWAP(a[i][j],a[m][j])
for (j=1;j<=n;j++) SWAP(a[j][i],a[j][m])
}
if (x) {
for (i=m+1;i<=n;i++) {
if ((y=a[i][m-1]) != 0.0) {
y /= x;
a[i][m-1]=y;
for (j=m;j<=n;j++)
a[i][j] -= y*a[m][j];
for (j=1;j<=n;j++)
a[j][m] += y*a[j][i];
}
}
}
}
} |