File: differ.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 (202 lines) | stat: -rw-r--r-- 5,872 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
#include <string>
#include <stdio.h>
#include <stddef.h>

#include        "gr.hh"
#include	"extern.hh"
#include	"private.hh"

bool            differentiateCmd(void);

// `differentiate {{x|y} wrt index|{y|x}} | {grid wrt x|y}'
bool
differentiateCmd()
{
    unsigned int i, j;
    switch (_nword) {
    case 4:
	if (!strcmp(_word[2], "wrt") && !strcmp(_word[1], "grid")) {
	    // `differentiate grid wrt ...'
	    if (!_xgrid_exists && !_ygrid_exists && !_grid_exists) {
		err("`differentiate grid' -- no grid exists yet\n");
		return false;
	    }
	    if (!strcmp(_word[3], "x")) {
		// `differentiate grid wrt x'
		if (_num_xmatrix_data < 3) {
		    err("`differentiate grid wrt x' -- too few data");
		    return false;
		}
		vector<bool> ok((size_t)_num_xmatrix_data, false);
		vector<double> tmp((size_t)_num_xmatrix_data, 0.0);
		for (j = 0; j < _num_ymatrix_data; j++) {
		    for (i = 1; i < _num_xmatrix_data - 1; i++) {
			if (_legit_xy(i - 1, j) == true
			    && _legit_xy(i + 1, j) == true
			    && _xmatrix[i + 1] != _xmatrix[i - 1]) {
			    tmp[i] = (_f_xy(i + 1, j) - _f_xy(i - 1, j))
				/ (_xmatrix[i + 1] - _xmatrix[i - 1]);
			    ok[i] = true;
			} else {
			    ok[i] = false;
			}
		    }
		    for (i = 1; i < _num_xmatrix_data - 1; i++) {
			_f_xy(i, j) = tmp[i];
			if (ok[i] == true) {
			    _f_xy(i, j) = tmp[i];
			    _legit_xy(i, j) = true;
			} else {
			    _legit_xy(i, j) = false;
			}
		    }
		    _f_xy(0, j) = _f_xy(1, j);
		    _legit_xy(0, j) = _legit_xy(1, j);
		    _f_xy(_num_xmatrix_data - 1, j) = _f_xy(_num_xmatrix_data - 2, j);
		    _legit_xy(_num_xmatrix_data - 1, j) = _legit_xy(_num_xmatrix_data - 2, j);
		}
	    } else if (!strcmp(_word[3], "y")) {
		// `differentiate grid wrt y'
		if (_num_ymatrix_data < 3) {
		    err("`differentiate grid wrt y' -- too few data");
		    return false;
		}
		vector<bool>   ok((size_t)_num_ymatrix_data, false);
		vector<double> tmp((size_t)_num_ymatrix_data, 0.0);
		for (i = 0; i < _num_xmatrix_data; i++) {
		    for (j = 1; j < _num_ymatrix_data - 1; j++) {
			if (_legit_xy(i, j - 1) == true
			    && _legit_xy(i, j + 1) == true
			    && _ymatrix[j + 1] != _ymatrix[j - 1]) {
			    tmp[j] = (_f_xy(i, j + 1) - _f_xy(i, j - 1))
				/ (_ymatrix[j + 1] - _ymatrix[j - 1]);
			    ok[j] = true;
			} else {
			    ok[j] = false;
			}
		    }
		    for (j = 1; j < _num_ymatrix_data - 1; j++) {
			if (ok[j] == true) {
			    _f_xy(i, j) = tmp[j];
			    _legit_xy(i, j) = true;
			} else {
			    _legit_xy(i, j) = false;
			}
		    }
		    _f_xy(i, 0) = _f_xy(i, 1);
		    _legit_xy(i, 0) = _legit_xy(i, 1);
		    _f_xy(i, _num_ymatrix_data - 1) = _f_xy(i, _num_ymatrix_data - 2);
		    _legit_xy(i, _num_ymatrix_data - 1) = _legit_xy(i, _num_ymatrix_data - 2);
		}
	    } else {
		err("Can only differentiate grid wrt to `x' or `y'");
		return false;
	    }
	    matrix_limits(&_f_min, &_f_max);
	    return true;
	} else if (!strcmp(_word[2], "wrt")) {
	    // `differentiate ... wrt ...'
	    if (!strcmp(_word[1], "x")) {
		// `differentiate x wrt ...'
		if (_colX.size() < 1) {
		    err("No x column exists yet\n");
		    return false;
		}
		if (_colX.size() < 2) {
		    err("Sorry, x column has only 1 element\n");
		    return false;
		}
		if (!strcmp(_word[3], "index")) {
		    // `differentiate x wrt index'
		    for (i = 1; i < _colX.size(); i++) {
			double x0 = _colX[i];
			double xleft = _colX[i - 1];
			if (!gr_missingx(x0) && !gr_missingx(xleft)) {
			    _colX[i - 1] = x0 - xleft;
			} else {
			    _colX[i - 1] = gr_currentmissingvalue();
			}
		    }
		} else if (!strcmp(_word[3], "y")) {
		    // `differentiate x wrt y'
		    for (i = 1; i < _colX.size(); i++) {
			double x0 = _colX[i];
			double xleft = _colX[i - 1];
			double y0 = _colY[i];
			double yleft = _colY[i - 1];
			if (!gr_missingx(x0) && !gr_missingy(y0)
			    && !gr_missingx(xleft) && !gr_missingy(yleft)) {
			    _colX[i - 1] = (x0  - xleft) / (y0 - yleft);
			} else {
			    _colX[i - 1] = gr_currentmissingvalue();
			}
		    }
		} else {
		    err("Wrong word; must be `index' or `x'");
		    return false;
		}
		// make something up for last point
		_colX[_colX.size() - 1] = _colX[_colX.size() - 2];
		create_x_scale();
		return true;
	    } else if (!strcmp(_word[1], "y")) {
		// `differentiate y wrt ...'
		if (_colY.size() < 1) {
		    err("No y column exists yet\n");
		    return false;
		}
		if (_colY.size() < 2) {
		    err("Sorry, y column has only 1 element\n");
		    return false;
		}
		if (!strcmp(_word[3], "index")) {
		    // `differentiate y wrt index'
		    for (i = 1; i < _colY.size(); i++) {
			double y0 = _colY[i];
			double yleft = _colY[i - 1];
			if (!gr_missingy(y0) && !gr_missingy(yleft)) {
			    _colY[i - 1] = y0 - yleft;
			} else {
			    _colY[i - 1] = gr_currentmissingvalue();
			}
		    }
		} else if (!strcmp(_word[3], "x")) {
		    // `differentiate y wrt x'
		    for (i = 1; i < _colY.size(); i++) {
			double x0 = _colX[i];
			double xleft = _colX[i - 1];
			double y0 = _colY[i];
			double yleft = _colY[i - 1];
			if (!gr_missingy(y0) && !gr_missingx(x0)
			    && !gr_missingy(y0) && !gr_missingy(yleft)) {
			    _colY[i - 1] = (y0 - yleft) / (x0 - xleft);
			} else {
			    _colY[i - 1] = gr_currentmissingvalue();
			}
		    }
		} else {
		    err("Wrong word; must be `index' or `y'");
		    return false;
		}
		// make something up for last point
		_colY[_colY.size() - 1] = _colY[_colY.size() - 2];
		create_y_scale();
		return true;
	    } else {
		err("Must be `differentiate x|y ...'");
		demonstrate_command_usage();
		err("Can't understand command.");
		return false;
	    }
	} else {
	    err("Need word `wrt'");	// not sure can get here
	    demonstrate_command_usage();
	    NUMBER_WORDS_ERROR;
	    return false;
	}
    default:
	demonstrate_command_usage();
	NUMBER_WORDS_ERROR;
	return false;
    }
}