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 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
|
/**
* @file evaluateMath.c
* @brief Evaluates and outputs infix expressions
* @author Rainer Machne <raim@tbi.univie.ac.at>
* @author Ben Bornstein
* @author Michael Hucka
*
* Copyright 2003 Rainer Machne
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation. A copy of the license agreement is
* provided in the file named "LICENSE.txt" included with this software
* distribution. It is also available online at
* http://sbml.org/software/libsbml/license.html
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <sbml/util/util.h>
#include "util.h"
#include <sbml/math/FormulaFormatter.h>
#include <sbml/math/FormulaParser.h>
#define SQR(x) ((x)*(x))
#define SQRT(x) pow((x),(.5))
double
evalAST(ASTNode_t *n);
/**
* This program asks the user to enter an infix formula, translates it to
* an Abstract Syntax tree using the function:
*
* ASTNode_t *SBML_parseFormula(char *)
*
* evaluates the formula and returns the result. See comments for
* double evalAST(ASTNode_t *n) for further information.
*/
int
main()
{
char *line;
#ifdef __BORLANDC__
unsigned long start, stop;
#else
unsigned long long start, stop;
#endif
ASTNode_t *n;
double result;
printf( "\n" );
printf( "This program evaluates math formulas in infix notation.\n" );
printf( "Typing 'enter' triggers evaluation.\n" );
printf( "Typing 'enter' on an empty line stops the program.\n" );
printf( "\n" );
while (1)
{
printf( "Enter an infix formula (empty line to quit):\n\n" );
printf( "> " );
if ( strlen(line = trim_whitespace(get_line( stdin ))) == 0 ) break;
n = SBML_parseFormula(line);
start = getCurrentMillis();
result = evalAST(n);
stop = getCurrentMillis();
printf("\n%s\n= %.10g\n\n", SBML_formulaToString(n), result);
printf("evaluation time: %llu ms\n\n", stop - start);
free(line);
ASTNode_free(n);
}
return 0;
}
/**
* The function evalAST(ASTNode_t) evaluates the formula of an
* Abstract Syntax Tree by simple recursion and returns the result
* as a double value.
*
* If variables (ASTNodeType_t AST_NAME) occur in the formula the user is
* asked to provide a numerical value. When evaluating ASTs within an SBML
* document or simulating an SBML model this node type includes parameters
* and variables of the model. Parameters should be retrieved from the
* SBML file, time and variables from current values of the simulation.
*
* Not implemented:
*
* - PIECEWISE, LAMBDA, and the SBML model specific functions DELAY and
* TIME and user-defined functions.
*
* - Complex numbers and/or checking for domains of trigonometric and root
* functions.
*
* - Checking for precision and rounding errors.
*
* The Nodetypes AST_TIME, AST_DELAY and AST_PIECEWISE default to 0. The
* SBML DELAY function and unknown functions (SBML user-defined functions)
* use the value of the left child (first argument to function) or 0 if the
* node has no children.
*/
double
evalAST(ASTNode_t *n)
{
int i;
double result;
int childnum = ASTNode_getNumChildren(n);
ASTNode_t **child = (ASTNode_t **) malloc(childnum * sizeof(ASTNode_t*));
for (i = 0; i < childnum; i++)
{
child[i] = ASTNode_getChild(n, i);
}
switch (ASTNode_getType(n))
{
case AST_INTEGER:
result = (double) ASTNode_getInteger(n);
break;
case AST_REAL:
result = ASTNode_getReal(n);
break;
case AST_REAL_E:
result = ASTNode_getReal(n);
break;
case AST_RATIONAL:
result = ASTNode_getReal(n);
break;
case AST_NAME:
{
char *l;
double var;
printf("\n-------------MESSAGE FROM EVALUATION FUNCTION-------------\n");
printf("Please enter a number for the variable!\n");
printf("If you do not enter a valid number (empty or characters), the \n");
printf("evaluation will proceed with a current internal value and the \n");
printf("result will make no sense.\n");
printf("%s=",ASTNode_getName(n));
l = get_line(stdin);
sscanf(l, "%lf", &var);
free(l);
printf("%s = %f\n", ASTNode_getName(n), var);
printf("-----------------------END MESSAGE--------------------------\n\n");
result = var;
}
break;
case AST_FUNCTION_DELAY:
printf("\n-------------MESSAGE FROM EVALUATION FUNCTION-------------\n");
printf("Delays can only be evaluated during a time series simulation.\n");
printf("The value of the first child (ie. the first argument to the function)\n");
printf("is used for this evaluation. If the function node has no children the\n");
printf("value defaults to 0.\n");
printf("-----------------------END MESSAGE--------------------------\n\n");
if(i>0)
result = evalAST(child[0]);
else
result = 0.0;
break;
case AST_NAME_TIME:
printf("\n-------------MESSAGE FROM EVALUATION FUNCTION-------------\n");
printf("The time can only be evaluated during a time series simulation.\n");
printf("The value of defaults to 0\n");
printf("-----------------------END MESSAGE--------------------------\n\n");
result = 0.0;
break;
case AST_CONSTANT_E:
/* exp(1) is used to adjust exponentiale to machine precision */
result = exp(1);
break;
case AST_CONSTANT_FALSE:
result = 0.0;
break;
case AST_CONSTANT_PI:
/* pi = 4 * atan 1 is used to adjust Pi to machine precision */
result = 4.*atan(1.);
break;
case AST_CONSTANT_TRUE:
result = 1.0;
break;
case AST_PLUS:
result = evalAST(child[0]) + evalAST(child[1]);
break;
case AST_MINUS:
if(childnum==1)
result = - (evalAST(child[0]));
else
result = evalAST(child[0]) - evalAST(child[1]);
break;
case AST_TIMES:
result = evalAST(child[0]) * evalAST(child[1]) ;
break;
case AST_DIVIDE:
result = evalAST(child[0]) / evalAST(child[1]);
break;
case AST_POWER:
result = pow(evalAST(child[0]),evalAST(child[1]));
break;
case AST_LAMBDA:
printf("\n-------------MESSAGE FROM EVALUATION FUNCTION-------------\n");
printf("This function is not implemented yet.\n");
printf("The value defaults to 0.\n");
printf("-----------------------END MESSAGE--------------------------\n\n");
result = 0.0;
break;
case AST_FUNCTION:
printf("\n-------------MESSAGE FROM EVALUATION FUNCTION-------------\n");
printf("This function is not known.\n");
printf("Within an SBML document new functions can be defined by the user or \n");
printf("application. The value of the first child (ie. the first argument to \n");
printf("the function) is used for this evaluation. If the function node has\n");
printf("no children the value defaults to 0.\n");
printf("-----------------------END MESSAGE--------------------------\n\n");
if(childnum>0)
result = evalAST(child[0]);
else
result = 0.0;
break;
case AST_FUNCTION_ABS:
result = (double) fabs(evalAST(child[0]));
break;
case AST_FUNCTION_ARCCOS:
result = acos(evalAST(child[0])) ;
break;
case AST_FUNCTION_ARCCOSH:
#ifndef WIN32
result = acosh(evalAST(child[0]));
#else
result = log(evalAST(child[0]) + SQR(evalAST(child[0]) * evalAST(child[0]) - 1.));
#endif
break;
case AST_FUNCTION_ARCCOT:
/* arccot x = arctan (1 / x) */
result = atan(1./ evalAST(child[0]));
break;
case AST_FUNCTION_ARCCOTH:
/* arccoth x = 1/2 * ln((x+1)/(x-1)) */
result = ((1./2.)*log((evalAST(child[0])+1.)/(evalAST(child[0])-1.)) );
break;
case AST_FUNCTION_ARCCSC:
/* arccsc(x) = Arctan(1 / sqrt((x - 1)(x + 1))) */
result = atan( 1. / SQRT( (evalAST(child[0])-1.)*(evalAST(child[0])+1.) ) );
break;
case AST_FUNCTION_ARCCSCH:
/* arccsch(x) = ln((1 + sqrt(1 + x^2)) / x) */
result = log((1.+SQRT((1+SQR(evalAST(child[0]))))) /evalAST(child[0]));
break;
case AST_FUNCTION_ARCSEC:
/* arcsec(x) = arctan(sqrt((x - 1)(x + 1))) */
result = atan( SQRT( (evalAST(child[0])-1.)*(evalAST(child[0])+1.) ) );
break;
case AST_FUNCTION_ARCSECH:
/* arcsech(x) = ln((1 + sqrt(1 - x^2)) / x) */
result = log((1.+pow((1-SQR(evalAST(child[0]))),0.5))/evalAST(child[0]));
break;
case AST_FUNCTION_ARCSIN:
result = asin(evalAST(child[0]));
break;
case AST_FUNCTION_ARCSINH:
#ifndef WIN32
result = asinh(evalAST(child[0]));
#else
result = log(evalAST(child[0]) + SQR(evalAST(child[0]) * evalAST(child[0]) + 1.));
#endif
break;
case AST_FUNCTION_ARCTAN:
result = atan(evalAST(child[0]));
break;
case AST_FUNCTION_ARCTANH:
#ifndef WIN32
result = atanh(evalAST(child[0]));
#else
result = log((1. / evalAST(child[0]) + 1.) / (1. / evalAST(child[0]) - 1.)) / 2.;
#endif
break;
case AST_FUNCTION_CEILING:
result = ceil(evalAST(child[0]));
break;
case AST_FUNCTION_COS:
result = cos(evalAST(child[0]));
break;
case AST_FUNCTION_COSH:
result = cosh(evalAST(child[0]));
break;
case AST_FUNCTION_COT:
/* cot x = 1 / tan x */
result = (1./tan(evalAST(child[0])));
break;
case AST_FUNCTION_COTH:
/* coth x = cosh x / sinh x */
result = cosh(evalAST(child[0])) / sinh(evalAST(child[0]));
break;
case AST_FUNCTION_CSC:
/* csc x = 1 / sin x */
result = (1./sin(evalAST(child[0])));
break;
case AST_FUNCTION_CSCH:
/* csch x = 1 / cosh x */
result = (1./cosh(evalAST(child[0])));
break;
case AST_FUNCTION_EXP:
result = exp(evalAST(child[0]));
break;
case AST_FUNCTION_FACTORIAL:
{
printf("\n-------------MESSAGE FROM EVALUATION FUNCTION-------------\n");
printf("The factorial is only implemented for integer values. If a floating\n");
printf("point number is passed, the floor value is used for calculation!\n");
printf("-----------------------END MESSAGE--------------------------\n\n");
i = (int)floor(evalAST(child[0]));
for(result=1;i>1;--i)
result *= i;
}
break;
case AST_FUNCTION_FLOOR:
result = floor(evalAST(child[0]));
break;
case AST_FUNCTION_LN:
result = log(evalAST(child[0]));
break;
case AST_FUNCTION_LOG:
result = log10(evalAST(child[0]));
break;
case AST_FUNCTION_PIECEWISE:
printf("\n-------------MESSAGE FROM EVALUATION FUNCTION-------------\n");
printf("This function is not implemented yet.\n");
printf("The value defaults to 0.\n");
printf("-----------------------END MESSAGE--------------------------\n\n");
result = 0.0;
break;
case AST_FUNCTION_POWER:
result = pow(evalAST(child[0]),evalAST(child[1]));
break;
case AST_FUNCTION_ROOT:
result = pow(evalAST(child[1]),(1./evalAST(child[0])));
break;
case AST_FUNCTION_SEC:
/* sec x = 1 / cos x */
result = 1./cos(evalAST(child[0]));
break;
case AST_FUNCTION_SECH:
/* sech x = 1 / sinh x */
result = 1./sinh(evalAST(child[0]));
break;
case AST_FUNCTION_SIN:
result = sin(evalAST(child[0]));
break;
case AST_FUNCTION_SINH:
result = sinh(evalAST(child[0]));
break;
case AST_FUNCTION_TAN:
result = tan(evalAST(child[0]));
break;
case AST_FUNCTION_TANH:
result = tanh(evalAST(child[0]));
break;
case AST_LOGICAL_AND:
result = (double) ((evalAST(child[0])) && (evalAST(child[1])));
break;
case AST_LOGICAL_NOT:
result = (double) (!(evalAST(child[0])));
break;
case AST_LOGICAL_OR:
result = (double) ((evalAST(child[0])) || (evalAST(child[1])));
break;
case AST_LOGICAL_XOR:
result = (double) ((!(evalAST(child[0])) && (evalAST(child[1])))
|| ((evalAST(child[0])) && !(evalAST(child[1]))));
break;
case AST_RELATIONAL_EQ :
result = (double) ((evalAST(child[0])) == (evalAST(child[1])));
break;
case AST_RELATIONAL_GEQ:
result = (double) ((evalAST(child[0])) >= (evalAST(child[1])));
break;
case AST_RELATIONAL_GT:
result = (double) ((evalAST(child[0])) > (evalAST(child[1])));
break;
case AST_RELATIONAL_LEQ:
result = (double) ((evalAST(child[0])) <= (evalAST(child[1])));
break;
case AST_RELATIONAL_LT :
result = (double) ((evalAST(child[0])) < (evalAST(child[1])));
break;
default:
result = 0;
break;
}
free(child);
return result;
}
|