/[ascend]/trunk/base/generic/packages/bisect.c
ViewVC logotype

Contents of /trunk/base/generic/packages/bisect.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 27 - (show annotations) (download) (as text)
Thu Dec 9 18:22:50 2004 UTC (15 years, 7 months ago) by aw0a
File MIME type: text/x-csrc
File size: 6454 byte(s)
Correcting package checkin
1 #include "utilities/ascConfig.h"
2 #include "compiler/packages.h"
3 #include "compiler/atomvalue.h"
4 #include "compiler/instquery.h"
5
6 static int CheckArgTypes(struct gl_list_t *branch)
7 {
8 struct Instance *i;
9 enum inst_t kind;
10 unsigned long len,c;
11
12 len = gl_length(branch);
13 if (!len)
14 return 1;
15 for (c=1;c<=len;c++) {
16 i = (struct Instance *)gl_fetch(branch,c);
17 if (i) {
18 kind = InstanceKind(i);
19 if ((kind!=REAL_ATOM_INST)&&(kind!=REAL_INST)) {
20 FPRINTF(stderr,"Incorrect types of arguements given\n");
21 return 1;
22 }
23 } else {
24 FPRINTF(stderr,"NULL arguements given\n");
25 return 1;
26 }
27 }
28 return 0; /* all should be ok */
29 }
30
31 static int CheckArgVector(struct gl_list_t *branch)
32 {
33 if (!branch) {
34 FPRINTF(stderr,"Empty arglists given\n");
35 return 1;
36 }
37 if (CheckArgTypes(branch))
38 return 1;
39 return 0;
40 }
41
42 /*********************************************************************\
43 * This function expects 3 arguements; the calling protocol
44 * from ASCEND it expects to be invoked as:
45 *
46 * set_values(x1:array of generic_reals,
47 * x2:array of generic_reals,
48 * m: generic_real);
49 *
50 * The dimension of both x1, and x2 are expected to be the same;
51 *
52 \*********************************************************************/
53
54 static int CheckArgs_SetValues(struct gl_list_t *arglist)
55 {
56 struct gl_list_t *branch;
57 unsigned long len, dim1, dim2;
58 char *error_msg = "all ok";
59
60 if (!arglist) {
61 error_msg = "No arguement list given";
62 goto error;
63 }
64 len = gl_length(arglist);
65 if (len!=3) {
66 error_msg = "Incorrect # args given, 3 were expected";
67 goto error;
68 }
69
70 branch = (struct gl_list_t *)gl_fetch(arglist,1);
71 if (CheckArgVector(branch))
72 return 1;
73 dim1 = gl_length(branch);
74
75 branch = (struct gl_list_t *)gl_fetch(arglist,2);
76 if (CheckArgVector(branch))
77 return 1;
78 dim2 = gl_length(branch);
79
80 if (dim1!=dim2) {
81 error_msg = "Inconsistent dimensions for the input vectors";
82 goto error;
83 }
84
85 /* check the multiplying term.
86 */
87 branch = (struct gl_list_t *)gl_fetch(arglist,3);
88 if (!branch) {
89 error_msg = "No multiplier term given";
90 goto error;
91 }
92
93 dim2 = gl_length(branch);
94 if (dim2!=1) {
95 error_msg = "A single multiplying term was expected; more than 1 given";
96 goto error;
97 }
98 return 0; /* if here all should be ok */
99
100 error:
101 FPRINTF(stderr,"%s\n",error_msg);
102 return 1;
103 }
104
105
106 int do_set_values_eval(struct Slv_Interp *slv_interp,
107 struct Instance *i,
108 struct gl_list_t *arglist)
109 {
110 unsigned long dimension,c;
111 struct gl_list_t *inputs, *outputs, *branch;
112 double value,multiplier,calculated;
113 int result;
114
115 result = CheckArgs_SetValues(arglist);
116 if (result)
117 return 1;
118 inputs = (struct gl_list_t *)gl_fetch(arglist,1);
119 outputs = (struct gl_list_t *)gl_fetch(arglist,2);
120 dimension = gl_length(inputs);
121 branch = (struct gl_list_t *)gl_fetch(arglist,3);
122 multiplier = RealAtomValue(gl_fetch(branch,1));
123
124 for (c=1;c<=dimension;c++) {
125 value = RealAtomValue(gl_fetch(inputs,c));
126 i = (struct Instance *)gl_fetch(outputs,c);
127 calculated = value*multiplier;
128 SetRealAtomValue(i,calculated,(unsigned)0);
129 FPRINTF(stdout,"value %g, multiplier %g, newvalue %g\n",
130 value, multiplier,calculated);
131 }
132 return 0;
133 }
134
135
136 /*********************************************************************\
137 * This function expects 3 arguements; the calling protocol
138 * from ASCEND it expects to be invoked as:
139 *
140 * set_values(x1:array of generic_reals,
141 * x2:array of generic_reals,
142 * y:array of generic_reals);
143 *
144 * The dimension of x1, x2 and y are expected to be the same;
145 *
146 \*********************************************************************/
147
148 static int CheckArgs_Bisection(struct gl_list_t *arglist)
149 {
150 struct gl_list_t *branch;
151 unsigned long len,c;
152 unsigned long dim1=0, dim2=0;
153 char *error_msg = "all ok";
154
155 if (!arglist) {
156 error_msg = "No arguement list given";
157 goto error;
158 }
159 len = gl_length(arglist);
160 if (len!=3) {
161 error_msg = "Incorrect # args given, 3 were expected";
162 goto error;
163 }
164
165 for (c=1;c<=len;c++) {
166 branch = (struct gl_list_t *)gl_fetch(arglist,c);
167 if (CheckArgVector(branch))
168 return 1;
169 if (dim1==0) { /* get the dimension of the first vector */
170 dim1 = dim2 = gl_length(branch);
171 } else {
172 dim2 = gl_length(branch);
173 if (dim1!=dim2) {
174 error_msg = "Inconsistent dimensions given";
175 goto error;
176 }
177 }
178 }
179 return 0;
180
181 error:
182 FPRINTF(stderr,"%s\n",error_msg);
183 return 1;
184 }
185
186 int do_bisection_eval(struct Slv_Interp *slv_interp,
187 struct Instance *i,
188 struct gl_list_t *arglist)
189 {
190 unsigned long dimension,c;
191 struct gl_list_t *vector1, *vector2, *outputs;
192 double value1, value2, calculated;
193 int result;
194
195 result = CheckArgs_Bisection(arglist);
196 if (result)
197 return 1;
198 vector1 = (struct gl_list_t *)gl_fetch(arglist,1);
199 vector2 = (struct gl_list_t *)gl_fetch(arglist,2);
200 outputs = (struct gl_list_t *)gl_fetch(arglist,3);
201 dimension = gl_length(vector1); /* get the dimension of the vectors */
202
203 for (c=1;c<=dimension;c++) {
204 value1 = RealAtomValue(gl_fetch(vector1,c));
205 value2 = RealAtomValue(gl_fetch(vector2,c));
206 i = (struct Instance *)gl_fetch(outputs,c);
207 calculated = (value1 + value2)/2.0;
208 SetRealAtomValue(i,calculated,(unsigned)0);
209 }
210 return 0;
211 }
212
213 int Bisection (void)
214 {
215
216 char set_values_help[] =
217 "This function accepts 3 args, The first 2 arg vectors of equal\n\
218 length. The second is a multiplier to be applied to each element\n\
219 of the first vector to yield the second vector.\n\
220 Example: do_set_values(x[1..n], y[1..n], multiplier[1..n]).\n";
221
222 char bisection_help[] =
223 "This function accepts 3 args, each of which must be vectors.\n\
224 It will bisect find the midpoint by bisection.\n\
225 Example: do_bisection(x[1..n],x_par[1..n], y[1..n]). \n";
226
227 int result;
228 result = CreateUserFunction("do_set_values",NULL,
229 do_set_values_eval,NULL,NULL,
230 3,0,set_values_help);
231 result += CreateUserFunction("do_bisection",NULL,
232 do_bisection_eval,NULL,NULL,
233 3,0,bisection_help);
234 return result;
235 }
236
237 /*
238 * To compile for static linking do:
239 * gcc -c -I../compiler -g -DHAVE_PACKAGES -o bisect.o bisect.c
240 *
241 * To compiler for dynamic linking do:
242 * gcc -c -I../compiler -g -fpic -o bisect.o bisect.c
243 * ld -o libbisect.so.1.0 bisect.o
244 */
245
246
247

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22