File: math.cc

package info (click to toggle)
gri 2.4.2-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 4,540 kB
  • ctags: 1,966
  • sloc: cpp: 32,542; lisp: 3,243; perl: 806; makefile: 548; sh: 253
file content (398 lines) | stat: -rw-r--r-- 11,719 bytes parent folder | download
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#include	<math.h>
#include	<string.h>
#include	"extern.hh"
#include	"image_ex.hh"

//int             i, n;

bool            mathCmd();
static bool     column_math(double *Ptr, int n, int operator_position = 1);
static bool     image_math(void);
static bool     grid_data_math(void);
extern void     highpass_image(void);
extern void     lowpass_image(void);

bool
mathCmd()
{
    double          number, old_value = 0.0;
    if (_nword < 3) {
	err("Proper format: `.x. = 10.0', `.x. *= 2', or `.x. = { rpn ... }'");
	return false;
    }
    // Variable
    if (is_var(_word[0])) {
	if (!getdnum(_word[2], &number))
	    return false;
	if (word_is(1, "=")) {
	    // `.variable. = .value.'
	    PUT_VAR(_word[0], number);
	} else if (word_is(1, "+=")) {
	    get_var(_word[0], &old_value);
	    old_value += number;
	    PUT_VAR(_word[0], old_value);
	} else if (word_is(1, "-=")) {
	    get_var(_word[0], &old_value);
	    old_value -= number;
	    PUT_VAR(_word[0], old_value);
	} else if (word_is(1, "*=")) {
	    get_var(_word[0], &old_value);
	    old_value *= number;
	    PUT_VAR(_word[0], old_value);
	} else if (word_is(1, "/=")) {
	    get_var(_word[0], &old_value);
	    old_value /= number;
	    PUT_VAR(_word[0], old_value);
	} else if (word_is(1, "^=")) {
	    get_var(_word[0], &old_value);
	    old_value = pow(old_value, number);
	    PUT_VAR(_word[0], old_value);
	} else if (word_is(1, "_=")) {
	    get_var(_word[0], &old_value);
	    if (number < 0.0) {
		err("Cannot do log to negative base");
		return false;
	    }
	    if (!gr_missing(old_value) && old_value > 0.0) {
		old_value = log(old_value) / log(number);
		PUT_VAR(_word[0], old_value);
	    } else {
		PUT_VAR(_word[0], gr_currentmissingvalue());
	    }
	} else {
	    err("`\\", _word[1], "' is not a known operator for variables", "'\\");
	    return false;
	}
    } else if (_nword > 3 && word_is(0, "grid") && word_is(1, "data")) {
	grid_data_math();
    } else if (_nword > 3 && word_is(0, "grid") && word_is(1, "x")) {
	if (_num_xmatrix_data)
	    column_math(_xmatrix, _num_xmatrix_data, 2);
	else {
	    err("Can't do math on xmatrix with no data in it already.");
	    return false;
	}
    } else if (_nword > 3 && word_is(0, "grid") && word_is(1, "y")) {
	if (_num_ymatrix_data)
	    column_math(_ymatrix, _num_ymatrix_data, 2);
	else {
	    err("Can't do math on ymatrix with no data in it already.");
	    return false;
	}
    } else if (word_is(0, "u")) {
	if (_colU.size())
	    column_math(_colU.begin(), _colU.size());
	else {
	    err("Can't do math on u-column with no data in it already.");
	    return false;
	}
    } else if (word_is(0, "v")) {
	if (_colV.size())
	    column_math(_colV.begin(), _colV.size());
	else {
	    err("Can't do math on v-column with no data in it already.");
	    return false;
	}
    } else if (word_is(0, "x")) {
	if (_colX.size())
	    column_math(_colX.begin(), _colX.size());
	else {
	    err("Can't do math on x-column with no data in it already.");
	    return false;
	}
    } else if (word_is(0, "y")) {
	if (_colY.size())
	    column_math(_colY.begin(), _colY.size());
	else {
	    err("Can't do math on y-column with no data in it already.");
	    return false;
	}
    } else if (word_is(0, "z")) {
	if (_colZ.size())
	    column_math(_colZ.begin(), _colZ.size());
	else {
	    err("Can't do math on z-column with no data in it already.");
	    return false;
	}
    } else if (word_is(0, "image")) {
	image_math();
    } else {
	err("Cannot do mathematical operation on unrecognized item `\\",
	    _word[0],
	    "'.",
	    "\\");
	return false;
    }
    return true;
}

static bool
column_math(double *Ptr, int n, int operator_position /*= 1*/)
{
    Require(n > 0, gr_Error("Trying to do column-math on non-existent column."));
    double          number;
    int i;
    // Ensure enough space
    if      (word_is(0, "x"))	  {_colX.setDepth(n);     Ptr = _colX.begin();}
    else if (word_is(0, "y"))	  {_colY.setDepth(n);     Ptr = _colY.begin();}
    else if (word_is(0, "u"))	  {_colU.setDepth(n);     Ptr = _colU.begin();}
    else if (word_is(0, "v"))	  {_colV.setDepth(n);     Ptr = _colV.begin();}
    else if (word_is(0, "z"))	  {_colZ.setDepth(n);     Ptr = _colZ.begin();}
    else if (word_is(0, "R"))	  {_colR.setDepth(n);     Ptr = _colR.begin();}
    else if (word_is(0, "THETA")) {_colTHETA.setDepth(n); Ptr = _colTHETA.begin();}

    // special case of `y = x...'
    if (_nword == 3 && word_is(0, "y") && word_is(1, "=") && word_is(2, "x")) {
	for (i = 0; i < n; i++)
	    _colY[i] = _colX[i];
	if (word_is(0, "y") && _need_y_axis && !_user_set_y_axis)
	    create_y_scale();
	return true;
    }

    if (!getdnum(_word[operator_position + 1], &number))
	return false;
    
    // Special case of assignment
    if (word_is(operator_position, "=")) {
	// Make sure column can hold the data
	for (i = 0; i < n; i++, Ptr++) {
	    if (!gr_missing(*Ptr)) {
		*Ptr = number;
	    }
	}
	if (word_is(0, "y") && _need_y_axis && !_user_set_y_axis)
	    create_y_scale();
	return true;
    }

    // Do modification (OP=) cases
    if (word_is(operator_position, "+=")) {
	for (i = 0; i < n; i++, Ptr++)	if (!gr_missing(*Ptr))	*Ptr += number;
    } else if (word_is(operator_position, "-=")) {
	for (i = 0; i < n; i++, Ptr++)	if (!gr_missing(*Ptr))	*Ptr -= number;
    } else if (word_is(operator_position, "*=")) {
	for (i = 0; i < n; i++, Ptr++)	if (!gr_missing(*Ptr))	*Ptr *= number;
    } else if (word_is(operator_position, "/=")) {
	for (i = 0; i < n; i++, Ptr++)	if (!gr_missing(*Ptr))	*Ptr /= number;
    } else if (word_is(operator_position, "^=")) {
	for (i = 0; i < n; i++, Ptr++)	if (!gr_missing(*Ptr))	*Ptr = pow(*Ptr, number);
    } else if (word_is(operator_position, "_=")) {
	if (number < 0.0) {
	    err("Cannot do log to negative base");
	    return false;
	}
	double lbase = log(number);
	for (i = 0; i < n; i++, Ptr++)
	    if (!gr_missing(*Ptr) && *Ptr > 0.0)
		*Ptr = log(*Ptr) / lbase;
	    else
		*Ptr = gr_currentmissingvalue();
    } else {
	err("`\\", _word[operator_position], "' not a known operator", "'\\");
	return false;
    }
    if (word_is(0, "x") && _need_x_axis && !_user_set_x_axis)
	create_x_scale();
    if (word_is(0, "y") && _need_y_axis && !_user_set_y_axis)
	create_y_scale();
    return true;
}

static bool
grid_data_math()
{
    if (_nword < 4) {
	err("No number for operation.");
	return false;
    }
    if (!_grid_exists) {
	err("First `read grid data'");
	return false;
    }
    double          number;
    if (!getdnum(_word[3], &number)) {
	err("Can't read number");
	return false;
    }
    unsigned int row, col;
    if (word_is(2, "=")) {
	for (col = 0; col < _num_xmatrix_data; col++) {
	    for (row = 0; row < _num_ymatrix_data; row++) {
		_f_xy(col, row) = number;
		_legit_xy(col, row) = true;
	    }
	}
	_f_min = _f_max = number;
	return true;
    } else if (word_is(2, "+=")) {
	for (col = 0; col < _num_xmatrix_data; col++)
	    for (row = 0; row < _num_ymatrix_data; row++)
		_f_xy(col, row) += number;
	_f_min += number;
	_f_max += number;
	return true;
    } else if (word_is(2, "-=")) {
	for (col = 0; col < _num_xmatrix_data; col++)
	    for (row = 0; row < _num_ymatrix_data; row++)
		_f_xy(col, row) -= number;
	_f_min -= number;
	_f_max -= number;
	return true;
    } else if (word_is(2, "*=")) {
	for (col = 0; col < _num_xmatrix_data; col++)
	    for (row = 0; row < _num_ymatrix_data; row++)
		_f_xy(col, row) *= number;
	_f_min *= number;
	_f_max *= number;
	return true;
    } else if (word_is(2, "/=")) {
	if (number == 0.0) {
	    warning("Can't divide by zero");
	    return true;
	}
	for (col = 0; col < _num_xmatrix_data; col++)
	    for (row = 0; row < _num_ymatrix_data; row++)
		_f_xy(col, row) /= number;
	_f_min /= number;
	_f_max /= number;
	return true;
    } else if (word_is(2, "^=")) {
	for (col = 0; col < _num_xmatrix_data; col++)
	    for (row = 0; row < _num_ymatrix_data; row++)
		_f_xy(col, row) = pow(_f_xy(col, row), number);
	matrix_limits(&_f_min, &_f_max);
	return true;
    } else if (word_is(2, "_=")) {
	if (number < 0.0) {
	    err("Cannot do log to negative base");
	    return false;
	}
	double lbase = log(number);
	for (col = 0; col < _num_xmatrix_data; col++)
	    for (row = 0; row < _num_ymatrix_data; row++)
		if (!gr_missing(_f_xy(col, row)) && _f_xy(col, row) > 0.0)
		    _f_xy(col, row) = log(_f_xy(col, row)) / lbase;
		else 
		    _f_xy(col, row) = gr_currentmissingvalue();
	matrix_limits(&_f_min, &_f_max);
	return true;
    } else {
	err("`grid data' given unknown operator `\\", _word[2], ";", "\\");
	return false;
    }
}

// `image [grayscale|colorscale] OPERATOR OPERAND'
static bool
image_math()
{
    int i, n;
    unsigned char  *Ptr;
    double          number;
    switch (_nword) {
    case 3:
	// `image OPERATOR OPERAND'
	if (word_is(1, "=")) {
	    if (word_is(2, "highpass"))
		highpass_image();
	    else if (word_is(2, "lowpass"))
		lowpass_image();
	    else {
		err("`image =' what?");
		return false;
	    }
	    return true;
	}
	if (!getdnum(_word[2], &number))
	    return false;
	if (!image_exists()) {
	    err("First `read image' or `convert grid to image'");
	    return false;
	}
	// Convert to image units
	number *= 255.0 / (_image255 - _image0);
	Ptr = _image.image;
	n = _image.ras_length;
	if (word_is(1, "+=")) {
	    for (i = 0; i < n; i++, Ptr++)
		*Ptr = int(*Ptr + number);
	} else if (word_is(1, "-=")) {
	    for (i = 0; i < n; i++, Ptr++)
		*Ptr = int(*Ptr - number);
	} else if (word_is(1, "*=")) {
	    for (i = 0; i < n; i++, Ptr++)
		*Ptr = int(*Ptr * number);
	} else if (word_is(1, "/=")) {
	    for (i = 0; i < n; i++, Ptr++)
		*Ptr = int(*Ptr / number);
	} else if (word_is(1, "^=")) {
	    for (i = 0; i < n; i++, Ptr++)
		*Ptr = int(pow(double(*Ptr), number));
	} else if (word_is(1, "_=")) {
	    if (number < 0.0) {
		err("Cannot do log to negative base");
		return false;
	    }
	    double lbase = log(number);
	    for (i = 0; i < n; i++, Ptr++)
		if (!gr_missing(*Ptr) && *Ptr > 0)
		    *Ptr = int(log(*Ptr) / lbase);
		else
		    *Ptr = 0;	// BUG -- should be something else
	} else {
	    err("Cannot use operator `\\", _word[1], "' on images.", "'\\");
	    return false;
	}
	break;
    case 4:
	if (word_is(1, "colorscale") || word_is(1, "colourscale")) {
	    n = 3 * 256;
	} else if (word_is(1, "greyscale") || word_is(1, "grayscale")) {
	    n = 256;
	} else {
	    err("Second word must be `colorscale', `colourscale', `grayscale' or `greyscale'");
	    return false;
	}
	if (!getdnum(_word[3], &number))
	    return false;
	Ptr = _imageTransform;
	if (word_is(2, "+=")) {
	    for (i = 0; i < n; i++, Ptr++) {
		*Ptr = (int) (255.0 * (double(*Ptr) / 255.0 + number));
	    }
	} else if (word_is(2, "-=")) {
	    for (i = 0; i < n; i++, Ptr++) {
		*Ptr = (int) (255.0 * (double(*Ptr) / 255.0 - number));
	    }
	} else if (word_is(2, "*=")) {
	    for (i = 0; i < n; i++, Ptr++) {
		*Ptr = (int) (255.0 * (double(*Ptr) / 255.0 * number));
	    }
	} else if (word_is(2, "/=")) {
	    for (i = 0; i < n; i++, Ptr++) {
		*Ptr = (int) (255.0 * (double(*Ptr) / 255.0 / number));
	    }
	} else if (word_is(2, "^=")) {
	    for (i = 0; i < n; i++, Ptr++) {
		*Ptr = (int) (255.0 * pow(double(*Ptr) / 255.0, number));
	    }
	} else if (word_is(2, "_=")) {
	    if (number < 0.0) {
		err("Cannot do log to negative base");
		return false;
	    }
	    double lbase = log(number);
	    for (i = 0; i < n; i++, Ptr++)
		*Ptr = int(255.0 * log(double(*Ptr) / 255.0) / lbase);
	} else {
	    err("Cannot use operator `\\", _word[2], "' on image colorscale|grayscale", "'\\");
	    return false;
	}
	break;
    default:
	NUMBER_WORDS_ERROR;
	return false;
    }
    return true;
}