File: matrix.c

package info (click to toggle)
grass 8.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 277,040 kB
  • sloc: ansic: 460,798; python: 227,732; cpp: 42,026; sh: 11,262; makefile: 7,007; xml: 3,637; sql: 968; lex: 520; javascript: 484; yacc: 450; asm: 387; perl: 157; sed: 25; objc: 6; ruby: 4
file content (182 lines) | stat: -rw-r--r-- 5,445 bytes parent folder | download | duplicates (2)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*!
 * \author
 * Lubos Mitas (original program and various modifications)
 *
 * \author
 * H. Mitasova,
 * I. Kosinovsky, D. Gerdes,
 * D. McCauley
 * (GRASS4.1 version of the program and GRASS4.2 modifications)
 *
 * \author
 * L. Mitas,
 * H. Mitasova,
 * I. Kosinovsky,
 * D.Gerdes,
 * D. McCauley
 * (1993, 1995)
 *
 * \author modified by McCauley in August 1995
 * \author modified by Mitasova in August 1995, Nov. 1996
 *
 * \copyright
 * (C) 1993-1996 by Lubos Mitas and the GRASS Development Team
 *
 * \copyright
 * This program is free software under the  GNU General Public License (>=v2).
 * Read the file COPYING that comes with GRASS for details.
 */

#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <grass/gis.h>
#include <grass/interpf.h>
#include <grass/gmath.h>

int IL_matrix_create(struct interp_params *params,
                     struct triple *points, /* points for interpolation */
                     int n_points,          /* number of points */
                     double **matrix,       /* matrix */
                     int *indx)
{
    static double *A = NULL;

    if (!A) {
        if (!(A = G_alloc_vector((params->KMAX2 + 2) * (params->KMAX2 + 2) +
                                 1))) {
            fprintf(stderr, "Cannot allocate memory for A\n");
            return -1;
        }
    }
    return IL_matrix_create_alloc(params, points, n_points, matrix, indx, A);
}

/*!
 * \brief Creates system of linear equations from interpolated points
 *
 * Creates system of linear equations represented by matrix using given
 * points and interpolating function interp()
 *
 * \param params struct interp_params *
 * \param points points for interpolation as struct triple
 * \param n_points number of points
 * \param[out] matrix the matrix
 * \param indx
 *
 * \return -1 on failure, 1 on success
 */
int IL_matrix_create_alloc(struct interp_params *params,
                           struct triple *points, /* points for interpolation */
                           int n_points,          /* number of points */
                           double **matrix,       /* matrix */
                           int *indx, double *A
                           /* temporary matrix unique for all threads */)
{
    double xx, yy;
    double rfsta2, r;
    double d;
    int n1, k1, k2, k, i1, l, m, i, j;
    double fstar2 = params->fi * params->fi / 4.;
    double RO, amaxa;
    double rsin = 0, rcos = 0, teta,
           scale = 0; /*anisotropy parameters - added by JH 2002 */
    double xxr, yyr;

    if (params->theta) {
        teta = params->theta * (M_PI / 180); /* deg to rad */
        rsin = sin(teta);
        rcos = cos(teta);
    }
    if (params->scalex)
        scale = params->scalex;

    n1 = n_points + 1;

    /*
       C      GENERATION OF MATRIX
       C      FIRST COLUMN
     */
    A[1] = 0.;
    for (k = 1; k <= n_points; k++) {
        i1 = k + 1;
        A[i1] = 1.;
    }
    /*
       C      OTHER COLUMNS
     */
    RO = -params->rsm;
    /* fprintf (stderr, "sm[%d] = %f,  ro=%f\n", 1, points[1].smooth, RO); */
    for (k = 1; k <= n_points; k++) {
        k1 = k * n1 + 1;
        k2 = k + 1;
        i1 = k1 + k;
        if (params->rsm < 0.) {        /*indicates variable smoothing */
            A[i1] = -points[k - 1].sm; /* added by Mitasova nov. 96 */
            /* G_debug(5, "sm[%d]=%f, a=%f", k, points[k-1].sm, A[i1]); */
        }
        else {
            A[i1] = RO; /* constant smoothing */
        }
        /* if (i1 == 100) fprintf (stderr,i "A[%d] = %f\n", i1, A[i1]); */

        /* A[i1] = RO; */
        for (l = k2; l <= n_points; l++) {
            xx = points[k - 1].x - points[l - 1].x;
            yy = points[k - 1].y - points[l - 1].y;

            if ((params->theta) && (params->scalex)) {
                /* re run anisotropy */
                xxr = xx * rcos + yy * rsin;
                yyr = yy * rcos - xx * rsin;
                xx = xxr;
                yy = yyr;
                r = scale * xx * xx + yy * yy;
                rfsta2 = fstar2 * (scale * xx * xx + yy * yy);
            }
            else {
                r = xx * xx + yy * yy;
                rfsta2 = fstar2 * (xx * xx + yy * yy);
            }

            if (rfsta2 == 0.) {
                fprintf(stderr, "ident. points in segm.\n");
                fprintf(stderr, "x[%d]=%f, x[%d]=%f, y[%d]=%f, y[%d]=%f\n",
                        k - 1, points[k - 1].x, l - 1, points[l - 1].x, k - 1,
                        points[k - 1].y, l - 1, points[l - 1].y);
                return -1;
            }
            i1 = k1 + l;
            A[i1] = params->interp(r, params->fi);
        }
    }

    /* C       SYMMETRISATION */
    amaxa = 1.;
    for (k = 1; k <= n1; k++) {
        k1 = (k - 1) * n1;
        k2 = k + 1;
        for (l = k2; l <= n1; l++) {
            m = (l - 1) * n1 + k;
            A[m] = A[k1 + l];
            amaxa = amax1(A[m], amaxa);
        }
    }
    m = 0;
    for (i = 0; i <= n_points; i++) {
        for (j = 0; j <= n_points; j++) {
            m++;
            matrix[i][j] = A[m];
        }
    }

    G_debug(3, "calling G_ludcmp()  n=%d indx=%d", n_points, *indx);
    if (G_ludcmp(matrix, n_points + 1, indx, &d) <= 0) {
        /* find the inverse of the matrix */
        fprintf(stderr, "G_ludcmp() failed! n=%d  d=%.2f\n", n_points, d);
        return -1;
    }

    /* G_free_vector(A); */
    return 1;
}