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