File: jacobi.c

package info (click to toggle)
grass 6.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 104,028 kB
  • ctags: 40,409
  • sloc: ansic: 419,980; python: 63,559; tcl: 46,692; cpp: 29,791; sh: 18,564; makefile: 7,000; xml: 3,505; yacc: 561; perl: 559; lex: 480; sed: 70; objc: 7
file content (99 lines) | stat: -rw-r--r-- 2,067 bytes parent folder | download | duplicates (3)
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
#include <stdlib.h>
#include <math.h>
#include <grass/gis.h>
#include <grass/gmath.h>


/***************************************************************************/

/* this does not use the Jacobi method, but it should give the same result */

int jacobi(double a[MX][MX], long n, double d[MX], double v[MX][MX])
{
    double *aa[MX], *vv[MX], *dd;
    int i;

    for (i = 0; i < n; i++) {
	aa[i] = &a[i + 1][1];
	vv[i] = &v[i + 1][1];
    }
    dd = &d[1];
    eigen(aa, vv, dd, n);

    return 0;
}

/***************************************************************************/

static int egcmp(const void *pa, const void *pb)
{
    const double *a = *(const double *const *)pa;
    const double *b = *(const double *const *)pb;

    if (*a > *b)
	return -1;
    if (*a < *b)
	return 1;

    return 0;
}

int egvorder(double d[MX], double z[MX][MX], long bands)
{
    double *buff;
    double **tmp;
    int i, j;

    /* allocate temporary matrix */

    buff = (double *)G_malloc(bands * (bands + 1) * sizeof(double));
    tmp = (double **)G_malloc(bands * sizeof(double *));
    for (i = 0; i < bands; i++)
	tmp[i] = &buff[i * (bands + 1)];

    /* concatenate (vertically) z and d into tmp */

    for (i = 0; i < bands; i++) {
	for (j = 0; j < bands; j++)
	    tmp[i][j + 1] = z[j + 1][i + 1];
	tmp[i][0] = d[i + 1];
    }

    /* sort the combined matrix */

    qsort(tmp, bands, sizeof(double *), egcmp);

    /* split tmp into z and d */

    for (i = 0; i < bands; i++) {
	for (j = 0; j < bands; j++)
	    z[j + 1][i + 1] = tmp[i][j + 1];
	d[i + 1] = tmp[i][0];
    }

    /* free temporary matrix */

    G_free(tmp);
    G_free(buff);

    return 0;
}

/***************************************************************************/

int transpose(double eigmat[MX][MX], long bands)
{
    int i, j;

    for (i = 1; i <= bands; i++)
	for (j = 1; j < i; j++) {
	    double tmp = eigmat[i][j];

	    eigmat[i][j] = eigmat[j][i];
	    eigmat[j][i] = tmp;
	}

    return 0;
}

/***************************************************************************/