File: matrix.c

package info (click to toggle)
grass 6.0.2-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 40,044 kB
  • ctags: 31,303
  • sloc: ansic: 321,125; tcl: 25,676; sh: 11,176; cpp: 10,098; makefile: 5,025; fortran: 1,846; yacc: 493; lex: 462; perl: 133; sed: 1
file content (169 lines) | stat: -rw-r--r-- 3,782 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
/*
 *
 * Original program and various modifications:
 * Lubos Mitas 
 *
 * GRASS4.1 version of the program and GRASS4.2 modifications:
 * H. Mitasova,
 * I. Kosinovsky, D. Gerdes
 * D. McCauley 
 *
 * Copyright 1993, 1995:
 * L. Mitas ,
 * H. Mitasova ,
 * I. Kosinovsky,
 * D.Gerdes 
 * D. McCauley 
 *
 * modified by McCauley in August 1995
 * modified by Mitasova in August 1995, Nov. 1996
 *
 */

#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include "gis.h"
#include "interpf.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
)
/*
Creates system of linear equations represented by matrix using given points
and interpolating function interp()
*/
{
    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.;
    static double   *A = NULL;
    double          RO,amaxa;
    double rsin, rcos, teta, scale; /*anisotropy parameters - added by JH 2002*/
    double          xxr, yyr;

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


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

/*
C
C      GENERATION OF MATRIX
C
C      FIRST COLUMN
C
*/
    A[1] = 0.;
    for (k = 1; k <= n_points; k++)
    {
	i1 = k + 1;
	A[i1] = 1.;
    }
/*
C
C      OTHER COLUMNS
C
*/
    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 */
/*           fprintf (stderr,"sm[%d]=%f,a=%f\n",k,points[k-1].sm,A[i1]);*/
        }
      else
        {
           A[i1] = RO; /* constant smoothing*/
        }
        if (i1 == 100) fprintf (stderr,"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
C       SYMMETRISATION
C
*/
    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];
      }
    }

    if (G_ludcmp(matrix,n_points+1,indx,&d)<=0) { /* find the inverse of the mat
rix */
      fprintf(stderr,"G_ludcmp() failed! n=%d\n",n_points);
      return -1;
    }
/*
    G_free_vector(A);
*/
    return 1;
}