File: function.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 (302 lines) | stat: -rw-r--r-- 6,793 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include <math.h>
#include <grass/gis.h>
#include <grass/vector.h>
#include <grass/glocale.h>
#include "global.h"

static double (*kernelfn)(double term, double bandwidth, double x);

/*********************** Gaussian ****************************/
/* probability for gaussian distribution */
double gaussian2dBySigma(double d, double sigma)
{
    double res;

    res = 1. / (2. * M_PI * sigma * sigma) * exp(-d * d / (2. * sigma * sigma));

    return (res);
}

double gaussianFunction(double x, double sigma, double dimension)
{
    return ((1. / (pow(2. * M_PI, dimension / 2.) * pow(sigma, dimension))) *
            exp(-0.5 * pow(x / sigma, 2.)));
}

/* probability for gaussian distribution */
double gaussianKernel(double x, double termx)
{
    return (termx * exp(-(x * x) / 2.));
}

/*
   term1 = 1./(2.*M_PI*sigma*sigma)
   term2 = 2.*sigma*sigma;
 */
double gaussian2dByTerms(double d, double term1, double term2)
{
    double res;

    res = term1 * exp(-d * d / term2);

    return (res);
}

double segno(double x)
{
    double y;

    y = (x > 0 ? 1. : 0.) + (x < 0 ? -1. : 0.);

    return y;
}

double kernel1(double d, double rs, double lambda)
{
    double res;
    double a = lambda - 1.;

    if (lambda == 1.) {
        res = 1. / (M_PI * (d * d + rs * rs));
    }
    else {
        res = segno(a) * (a / M_PI) * (pow(rs, 2. * a)) *
              (1 / pow(d * d + rs * rs, lambda));
    }

    /*  res=1./(M_PI*(d*d+rs*rs)); */
    return (res);
}

double invGaussian2d(double sigma, double prob)
{
    double d;

    d = sqrt(-2 * sigma * sigma * log(prob * M_PI * 2 * sigma * sigma));

    return (d);
}

/* euclidean distance between vectors x and y of length n */
double euclidean_distance(double *x, double *y, int n)
{
    int j;
    double out = 0.0;
    double tmp;

    for (j = 0; j < n; j++) {
        tmp = x[j] - y[j];
        out += tmp * tmp;
    }

    return sqrt(out);
}

/*****************kernel density functions******************************/

double gaussianKernel4(double term, double bandwidth, double x)
{
    /* term is set by setKernelFunction */
    /* bandwidth is here SD */

    /*
       double term =
       1. / (pow(bandwidth, dimension) * pow((2. * M_PI), dimension / 2.));
     */

    x /= bandwidth;

    /* SD = radius (bandwidth) / 4 */
    return (term * exp((x * x) / -2.));
}

/* Note: these functions support currently only 1D and 2D, consider this for
 * example before using them for 3d grid */
double uniformKernel(double term, double bandwidth, double x)
{
    /* term is set by setKernelFunction */

    if (x > bandwidth)
        return 0;

    /*
       if (dimension == 2)
       term = 2. / (M_PI * pow(bandwidth, 2));
       else
       term = 1. / bandwidth;
       term *= (1. / 2);

       x /= bandwidth;
     */

    return term;
}

double triangularKernel(double term, double bandwidth, double x)
{
    /* term is set by setKernelFunction */

    if (x > bandwidth)
        return 0;

    /*
       if (dimension == 2)
       term = 3. / (M_PI * pow(bandwidth, 2));
       else
       term = 1. / bandwidth;
     */

    x /= bandwidth;

    return term * (1 - x);
}

double epanechnikovKernel(double term, double bandwidth, double x)
{
    /* term is set by setKernelFunction */

    if (x > bandwidth)
        return 0;

    /*
       if (dimension == 2)
       term = 8. / (M_PI * 3. * pow(bandwidth, 2));
       else
       term = 1. / bandwidth;
       term *= (3. / 4.);
     */

    x /= bandwidth;

    /* return term * (3. / 4. * (1 - x * x)); */
    return term * (1 - x * x);
}

double quarticKernel(double term, double bandwidth, double x)
{
    /* term is set by setKernelFunction */

    if (x > bandwidth)
        return 0;

    /*
       if (dimension == 2)
       term = 16. / (M_PI * 5. * pow(bandwidth, 2));
       else
       term = 1. / bandwidth;
       term *= (15. / 16.);
     */

    x /= bandwidth;

    /* return term * (15. / 16. * pow(1 - x * x, 2)); */
    return term * pow(1 - x * x, 2);
}

double triweightKernel(double term, double bandwidth, double x)
{
    /* term is set by setKernelFunction */

    if (x > bandwidth)
        return 0;

    /*
       if (dimension == 2)
       term = 128. / (M_PI * 35. * pow(bandwidth, 2));
       else
       term = 1. / bandwidth;
       term *= (35. / 32);
     */

    x /= bandwidth;

    /* return term * (35. / 32 * pow(1 - x * x, 3)); */
    return term * pow(1 - x * x, 3);
}

double cosineKernel(double term, double bandwidth, double x)
{
    /* term is set by setKernelFunction */

    if (x > bandwidth)
        return 0;

    /*
       if (dimension == 2)
       term = 1. / (2 * (M_PI / 2 - 1) * pow(bandwidth, 2));
       else
       term = 1. / bandwidth;
       term *= (M_PI / 4.);
     */

    x /= bandwidth;

    /* return term * (M_PI / 4. * cos(M_PI / 2. * x)); */
    return term * cos(M_PI / 2. * x);
}

double kernelFunction(double term, double bandwidth, double x)
{
    return kernelfn(term, bandwidth, x);
}

void setKernelFunction(int function, int dimension, double bandwidth,
                       double *term)
{
    switch (function) {
    case KERNEL_UNIFORM:
        kernelfn = uniformKernel;
        if (dimension == 2)
            *term = 2. / (M_PI * pow(bandwidth, 2));
        else
            *term = 1. / bandwidth;
        *term *= (1. / 2);
        break;
    case KERNEL_TRIANGULAR:
        kernelfn = triangularKernel;
        if (dimension == 2)
            *term = 3. / (M_PI * pow(bandwidth, 2));
        else
            *term = 1. / bandwidth;
        break;
    case KERNEL_EPANECHNIKOV:
        kernelfn = epanechnikovKernel;
        if (dimension == 2)
            *term = 8. / (M_PI * 3. * pow(bandwidth, 2));
        else
            *term = 1. / bandwidth;
        *term *= (3. / 4.);
        break;
    case KERNEL_QUARTIC:
        kernelfn = quarticKernel;
        if (dimension == 2)
            *term = 16. / (M_PI * 5. * pow(bandwidth, 2));
        else
            *term = 1. / bandwidth;
        *term *= (15. / 16.);
        break;
    case KERNEL_TRIWEIGHT:
        kernelfn = triweightKernel;
        if (dimension == 2)
            *term = 128. / (M_PI * 35. * pow(bandwidth, 2));
        else
            *term = 1. / bandwidth;
        *term *= (35. / 32);
        break;
    case KERNEL_GAUSSIAN:
        kernelfn = gaussianKernel4;
        *term =
            1. / (pow(bandwidth, dimension) * pow((2. * M_PI), dimension / 2.));
        break;
    case KERNEL_COSINE:
        kernelfn = cosineKernel;
        if (dimension == 2)
            *term = 1. / (2 * (M_PI / 2 - 1) * pow(bandwidth, 2));
        else
            *term = 1. / bandwidth;
        *term *= (M_PI / 4.);
        break;
    default:
        G_fatal_error("Unknown kernel function");
    }
}