END_TEST START_TEST (test_SBML_parseFormula_12) { ASTNode_t *r = SBML_parseFormula("2 * foo^bar + 3.0"); ASTNode_t *c; fail_unless( ASTNode_getType (r) == AST_PLUS, NULL ); fail_unless( ASTNode_getCharacter (r) == '+', NULL ); fail_unless( ASTNode_getNumChildren(r) == 2 , NULL ); c = ASTNode_getLeftChild(r); fail_unless( ASTNode_getType (c) == AST_TIMES, NULL ); fail_unless( ASTNode_getCharacter (c) == '*', NULL ); fail_unless( ASTNode_getNumChildren(c) == 2 , NULL ); c = ASTNode_getRightChild(r); fail_unless( ASTNode_getType (c) == AST_REAL, NULL ); fail_unless( ASTNode_getReal (c) == 3.0, NULL ); fail_unless( ASTNode_getNumChildren(c) == 0 , NULL ); c = ASTNode_getLeftChild( ASTNode_getLeftChild(r) ); fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL ); fail_unless( ASTNode_getInteger (c) == 2, NULL ); fail_unless( ASTNode_getNumChildren(c) == 0, NULL ); c = ASTNode_getRightChild( ASTNode_getLeftChild(r) ); fail_unless( ASTNode_getType (c) == AST_POWER, NULL ); fail_unless( ASTNode_getCharacter (c) == '^', NULL ); fail_unless( ASTNode_getNumChildren(c) == 2 , NULL ); c = ASTNode_getLeftChild( ASTNode_getRightChild( ASTNode_getLeftChild(r) ) ); fail_unless( ASTNode_getType(c) == AST_NAME , NULL ); fail_unless( !strcmp(ASTNode_getName(c), "foo") , NULL ); fail_unless( ASTNode_getNumChildren(c) == 0 , NULL ); c = ASTNode_getRightChild( ASTNode_getRightChild( ASTNode_getLeftChild(r) ) ); fail_unless( ASTNode_getType(c) == AST_NAME , NULL ); fail_unless( !strcmp(ASTNode_getName(c), "bar") , NULL ); fail_unless( ASTNode_getNumChildren(c) == 0 , NULL ); ASTNode_free(r); }
END_TEST START_TEST (test_SBML_parseFormula_negZero) { ASTNode_t *r = SBML_parseFormula("-0.0"); fail_unless( ASTNode_getType(r) == AST_REAL, NULL ); fail_unless( util_isNegZero(ASTNode_getReal(r)) == 1, NULL ); fail_unless( ASTNode_getNumChildren(r) == 0, NULL ); ASTNode_free(r); }
END_TEST START_TEST (test_SBML_parseFormula_negInf) { ASTNode_t *r = SBML_parseFormula("-inf"); fail_unless( ASTNode_getType(r) == AST_REAL, NULL ); fail_unless( util_isInf(ASTNode_getReal(r)) == -1, NULL ); fail_unless( ASTNode_getNumChildren(r) == 0, NULL ); ASTNode_free(r); }
END_TEST START_TEST (test_SBML_parseFormula_2) { ASTNode_t *r = SBML_parseFormula("2.1"); fail_unless( ASTNode_getType (r) == AST_REAL, NULL ); fail_unless( ASTNode_getReal (r) == 2.1, NULL ); fail_unless( ASTNode_getNumChildren(r) == 0, NULL ); ASTNode_free(r); }
/** * Formats the given ASTNode as a real number and appends the result to * the given StringBuffer. */ void L3FormulaFormatter_formatReal (StringBuffer_t *sb, const ASTNode_t *node, const L3ParserSettings_t *settings) { double value = ASTNode_getReal(node); int sign; char * units; if (ASTNode_isInteger(node)) { value = ASTNode_getInteger(node); } if (util_isNaN(value)) { StringBuffer_append(sb, "NaN"); } else if ((sign = util_isInf(value)) != 0) { if (sign == -1) { StringBuffer_appendChar(sb, '-'); } StringBuffer_append(sb, "INF"); } else if (util_isNegZero(value)) { StringBuffer_append(sb, "-0"); } else { if (ASTNode_getType(node) == AST_REAL_E) { StringBuffer_appendExp(sb, value); } else { StringBuffer_appendReal(sb, value); } } if (L3ParserSettings_getParseUnits(settings)) { if (ASTNode_hasUnits(node)) { StringBuffer_appendChar( sb, ' '); units = ASTNode_getUnits(node); StringBuffer_append( sb, units); safe_free(units); } } }
END_TEST START_TEST (test_SBML_parseFormula_10) { ASTNode_t *r = SBML_parseFormula("1 + -2e100 / 3"); ASTNode_t *c; fail_unless( ASTNode_getType (r) == AST_PLUS, NULL ); fail_unless( ASTNode_getCharacter (r) == '+', NULL ); fail_unless( ASTNode_getNumChildren(r) == 2 , NULL ); c = ASTNode_getLeftChild(r); fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL ); fail_unless( ASTNode_getInteger (c) == 1, NULL ); fail_unless( ASTNode_getNumChildren(c) == 0, NULL ); c = ASTNode_getRightChild(r); fail_unless( ASTNode_getType (c) == AST_DIVIDE, NULL ); fail_unless( ASTNode_getCharacter (c) == '/', NULL ); fail_unless( ASTNode_getNumChildren(c) == 2 , NULL ); c = ASTNode_getLeftChild(c); fail_unless( ASTNode_getType (c) == AST_REAL_E, NULL ); fail_unless( ASTNode_getMantissa (c) == -2, NULL ); fail_unless( ASTNode_getExponent (c) == 100, NULL ); fail_unless( ASTNode_getReal (c) == -2e+100, NULL ); fail_unless( ASTNode_getNumChildren(c) == 0, NULL ); c = ASTNode_getRightChild( ASTNode_getRightChild(r) ); fail_unless( ASTNode_getType (c) == AST_INTEGER, NULL ); fail_unless( ASTNode_getInteger (c) == 3, NULL ); fail_unless( ASTNode_getNumChildren(c) == 0, NULL ); ASTNode_free(r); }
/** * Formats the given ASTNode as a real number and returns the result as * a string. */ char * FormulaGraphvizFormatter_formatReal (const ASTNode_t *node) { StringBuffer_t *p = StringBuffer_create(128); double value = ASTNode_getReal(node); int sign; char *s; if (util_isNaN(value)) { s = "NaN"; } else if ((sign = util_isInf(value)) != 0) { if (sign == -1) { s = "-INF"; } else { s = "INF"; } } else if (util_isNegZero(value)) { s = "-0"; } else { StringBuffer_appendReal(p, value); s = StringBuffer_toString(p); } free(p); return s; }
/** * Formats the given ASTNode as a real number and appends the result to * the given StringBuffer. */ void FormulaFormatter_formatReal (StringBuffer_t *sb, const ASTNode_t *node) { double value = ASTNode_getReal(node); int sign; if (util_isNaN(value)) { StringBuffer_append(sb, "NaN"); } else if ((sign = util_isInf(value)) != 0) { if (sign == -1) { StringBuffer_appendChar(sb, '-'); } StringBuffer_append(sb, "INF"); } else if (util_isNegZero(value)) { StringBuffer_append(sb, "-0"); } else { if (ASTNode_getType(node) == AST_REAL_E) { StringBuffer_appendExp(sb, value); } else { StringBuffer_appendReal(sb, value); } } }
/** * 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; }
SBML_ODESOLVER_API double evaluateAST(ASTNode_t *n, cvodeData_t *data) { int i, j, childnum; int found, datafound; int true; time_series_t *ts=data->model->time_series; double findtol=1e-5; ASTNodeType_t type; /* ASTNode_t **child; */ /* value* are for intermediate values. */ double value1, value2, value3, result; if ( n == NULL ) { SolverError_error(FATAL_ERROR_TYPE, SOLVER_ERROR_AST_UNKNOWN_NODE_TYPE, "evaluateAST: empty Abstract Syntax Tree (AST)."); return (0); } if ( ASTNode_isUnknown(n) ) { SolverError_error(FATAL_ERROR_TYPE, SOLVER_ERROR_AST_UNKNOWN_NODE_TYPE, "evaluateAST: unknown ASTNode type"); } result = 0; childnum = ASTNode_getNumChildren(n); type = ASTNode_getType(n); switch(type) { 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: /** VARIABLES: find the value of the variable in the data->value array. SOSlib's extension to libSBML's AST allows to add the index of the variable in this array to AST_NAME (ASTIndexName). If the ASTNode is not indexed, its array index is searched via the data->model->names array, which corresponds to the data->value array. For nodes with name `Time' or `time' the data->currenttime is returned. If no value is found a fatal error is produced. */ found = 0; if ( ASTNode_isSetIndex(n) ) { if ( ASTNode_isSetData(n) ) { /* if continuous data is observed, obtain interpolated result */ if ( (data->model->discrete_observation_data != 1) || (data->model->compute_vector_v != 1) ) { result = call(ASTNode_getIndex(n), data->currenttime, ts); } else /* if discrete data is observed, simply obtain value from time_series */ { datafound = 0; i = data->TimeSeriesIndex; if ( fabs(data->currenttime - ts->time[i]) < findtol ) { result = ts->data[ASTNode_getIndex(n)][i]; datafound++; } if ( datafound != 1) { SolverError_error(FATAL_ERROR_TYPE, SOLVER_ERROR_AST_EVALUATION_FAILED_DISCRETE_DATA, "use of discrete time series data failed; none or several time points matching current time"); result = 0; /* break; */ } else found = 1; } } else { /* majority case: just return the according value from data->values from the index stored by SOSlib ASTIndexNameNode sub-class of libSBML's ASTNode */ result = data->value[ASTNode_getIndex(n)]; } found++; } if ( found == 0 ) { for ( j=0; j<data->nvalues; j++ ) { if ( (strcmp(ASTNode_getName(n),data->model->names[j]) == 0) ) { result = data->value[j]; found++; } } } if ( found == 0 ) { SolverError_error(FATAL_ERROR_TYPE, SOLVER_ERROR_AST_EVALUATION_FAILED_MISSING_VALUE, "No value found for AST_NAME %s . Defaults to Zero " "to avoid program crash", ASTNode_getName(n)); result = 0; } break; case AST_FUNCTION_DELAY: SolverError_error(FATAL_ERROR_TYPE, SOLVER_ERROR_AST_EVALUATION_FAILED_DELAY, "Solving ODEs with Delay is not implemented. " "Defaults to 0 to avoid program crash"); result = 0.0; break; case AST_NAME_TIME: result = (double) data->currenttime; 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 = 0.0; for ( i=0; i<childnum; i++) result += evaluateAST(child(n,i),data); break; case AST_MINUS: if ( childnum<2 ) result = - (evaluateAST(child(n,0),data)); else result = evaluateAST(child(n,0),data) - evaluateAST(child(n,1),data); break; case AST_TIMES: result = 1.0; for ( i=0; i<childnum; i++) { result *= evaluateAST(child(n,i),data); if (result == 0.0) break; /* small optimization by skipping the rest of children */ } break; case AST_DIVIDE: result = evaluateAST(child(n,0),data) / evaluateAST(child(n,1),data); break; case AST_POWER: result = pow(evaluateAST(child(n,0),data),evaluateAST(child(n,1),data)); break; case AST_LAMBDA: SolverError_error(FATAL_ERROR_TYPE, SOLVER_ERROR_AST_EVALUATION_FAILED_LAMBDA, "Lambda can only be used in SBML function definitions." " Defaults to 0 to avoid program crash"); result = 0.0; break; /** FUNCTIONS: */ case AST_FUNCTION: /** Evaluate external functions, if it was set with setUserDefinedFunction */ if ( UsrDefFunc == NULL ) { SolverError_error(FATAL_ERROR_TYPE, SOLVER_ERROR_AST_EVALUATION_FAILED_FUNCTION, "The function %s() has not been defined " "in the SBML input model or as an externally " "supplied function. Defaults to 0 to avoid " "program crash", ASTNode_getName(n)); result = 0.0; } else { double *func_vals = NULL; ASSIGN_NEW_MEMORY_BLOCK(func_vals, childnum+1, double, 0); for ( i=0; i<childnum; i++ ) func_vals[i] = evaluateAST(child(n,i), data); result = UsrDefFunc((char *)ASTNode_getName(n), childnum, func_vals); free(func_vals); } break; case AST_FUNCTION_ABS: result = fabs(evaluateAST(child(n,0),data)); break; case AST_FUNCTION_ARCCOS: result = acos(evaluateAST(child(n,0),data)) ; break; case AST_FUNCTION_ARCCOSH: result = aCosh(evaluateAST(child(n,0),data)); break; case AST_FUNCTION_ARCCOT: /** arccot x = arctan (1 / x) */ result = atan(1./ evaluateAST(child(n,0),data)); break; case AST_FUNCTION_ARCCOTH: /** arccoth x = 1/2 * ln((x+1)/(x-1)) */ value1 = evaluateAST(child(n,0),data); result = log((value1 + 1) / (value1 - 1))/2; break; case AST_FUNCTION_ARCCSC: /** arccsc(x) = Arctan(1 / sqrt((x - 1)(x + 1))) */ value1 = evaluateAST(child(n,0),data); result = atan(1/sqrt((value1-1)*(value1+1))); break; case AST_FUNCTION_ARCCSCH: /** arccsch(x) = ln((1/x) + sqrt((1/(x*x)) + 1)) */ value1 = evaluateAST(child(n,0),data); result = log(1/value1 + sqrt(1/(value1*value1)+1)); break; case AST_FUNCTION_ARCSEC: /** arcsec(x) = arccos(1/x) */ result = acos(1/evaluateAST(child(n,0),data)); break; case AST_FUNCTION_ARCSECH: /* arcsech(x) = arccosh(1/x) */ result = aCosh( 1. / evaluateAST(child(n,0),data)); break; case AST_FUNCTION_ARCSIN: result = asin(evaluateAST(child(n,0),data)); break; case AST_FUNCTION_ARCSINH: result = aSinh(evaluateAST(child(n,0),data)); break; case AST_FUNCTION_ARCTAN: result = atan(evaluateAST(child(n,0),data)); break; case AST_FUNCTION_ARCTANH: result = aTanh(evaluateAST(child(n,0),data)); break; case AST_FUNCTION_CEILING: result = ceil(evaluateAST(child(n,0),data)); break; case AST_FUNCTION_COS: result = cos(evaluateAST(child(n,0),data)); break; case AST_FUNCTION_COSH: result = cosh(evaluateAST(child(n,0),data)); break; case AST_FUNCTION_COT: /** cot x = 1 / tan x */ result = (1./tan(evaluateAST(child(n,0),data))); break; case AST_FUNCTION_COTH: /** coth x = cosh x / sinh x */ result = 1/tanh(evaluateAST(child(n,0),data)); break; case AST_FUNCTION_CSC: /** csc x = 1 / sin x */ result = (1./sin(evaluateAST(child(n,0),data))); break; case AST_FUNCTION_CSCH: /** csch x = 1 / sinh x */ result = (1./sinh(evaluateAST(child(n,0),data))); break; case AST_FUNCTION_EXP: result = exp(evaluateAST(child(n,0),data)); break; case AST_FUNCTION_FACTORIAL: { int j; value1 = evaluateAST(child(n,0),data); j = floor(value1); if ( value1 != j ) SolverError_error(FATAL_ERROR_TYPE, SOLVER_ERROR_AST_EVALUATION_FAILED_FLOAT_FACTORIAL, "The factorial is only implemented." "for integer values. The floor value of the " "passed float is used for calculation!"); for(result=1;j>1;--j) result *= j; } break; case AST_FUNCTION_FLOOR: result = floor(evaluateAST(child(n,0),data)); break; case AST_FUNCTION_LN: result = log(evaluateAST(child(n,0),data)); break; case AST_FUNCTION_LOG: /** log(x,y) = log10(y)/log10(x) (where x is the base) */ result = log10(evaluateAST(child(n,1),data)) / log10(evaluateAST(child(n,0),data)); break; case AST_FUNCTION_PIECEWISE: /** Piecewise: */ found = 0; /** Go through n pieces with 2 AST children for each piece, */ for ( i=0; i<(childnum-1); i=i+2 ) { if ( evaluateAST(child(n, i+1), data) ) { found++; result = evaluateAST(child(n, i), data); } } /** odd number of AST children: if no piece was true, otherwise remains */ /* i should be equal to childnum for even number piecewise AST and equal to childnum-1 for odd numbered piecewise ASTs */ if ( i == childnum-1 && found == 0 ) { found++; result = evaluateAST(child(n, i), data); } if ( found == 0 ) SolverError_error(FATAL_ERROR_TYPE, SOLVER_ERROR_AST_EVALUATION_FAILED_PIECEWISE, "Piecewise function failed; no true piece"); if ( found > 1 ) SolverError_error(FATAL_ERROR_TYPE, SOLVER_ERROR_AST_EVALUATION_FAILED_PIECEWISE, "Piecewise function failed; several true pieces"); break; case AST_FUNCTION_POWER: result = pow(evaluateAST(child(n,0),data),evaluateAST(child(n,1),data)); break; case AST_FUNCTION_ROOT: /*!!! ALSO do this in compiled code */ value1 = evaluateAST(child(n,1),data); value2 = evaluateAST(child(n,0),data); value3 = floor(value2); /* for odd root degrees, negative numbers are OK */ if ( value2 == value3 ) /* check whether degree is integer */ { if ( (value1 < 0) && ((int)value2 % 2 != 0) ) result = - pow(fabs(value1), 1./value2); else result = pow(value1, 1./value2); } else result = pow(value1, 1./value2); break; case AST_FUNCTION_SEC: /** sec x = 1 / cos x */ result = 1./cos(evaluateAST(child(n,0),data)); break; case AST_FUNCTION_SECH: /** sech x = 1 / cosh x */ result = 1./cosh(evaluateAST(child(n,0),data)); break; case AST_FUNCTION_SIN: result = sin(evaluateAST(child(n,0),data)); break; case AST_FUNCTION_SINH: result = sinh(evaluateAST(child(n,0),data)); break; case AST_FUNCTION_TAN: result = tan(evaluateAST(child(n,0),data)); break; case AST_FUNCTION_TANH: result = tanh(evaluateAST(child(n,0),data)); break; case AST_LOGICAL_AND: /** AND: all children must be true */ true = 0; for ( i=0; i<childnum; i++ ) true += evaluateAST(child(n,i),data); if ( true == childnum ) result = 1.0; else result = 0.0; break; case AST_LOGICAL_NOT: result = (double) (!(evaluateAST(child(n,0),data))); break; case AST_LOGICAL_OR: /** OR: at least one child must be true */ true = 0; for ( i=0; i<childnum; i++ ) true += evaluateAST(child(n,i),data); if ( true > 0 ) result = 1.0; else result = 0.0; break; case AST_LOGICAL_XOR: /* n-ary: true if an odd number of children is true */ true = 0; for ( i=0; i<childnum; i++ ) true += evaluateAST(child(n,i),data); if ( true % 2 != 0 ) result = 1.0; else result = 0.0; break; /* !!! check n-ary definitions for relational operators !!! */ case AST_RELATIONAL_EQ: /** n-ary: all children must be equal */ result = 1.0; if (childnum <= 1) break; value1 = evaluateAST(child(n,0),data); for ( i=1; i<childnum; i++ ) if ( value1 != evaluateAST(child(n,i),data) ) { result = 0.0; break; } break; case AST_RELATIONAL_GEQ: /** n-ary: each child must be greater than or equal to the following */ result = 1.0; if (childnum <= 1) break; value2 = evaluateAST(child(n,0),data); for ( i=1; i<childnum; i++ ) { value1 = value2; value2 = evaluateAST(child(n,i),data); if (value1 < value2) { result = 0.0; break; } } break; case AST_RELATIONAL_GT: /** n-ary: each child must be greater than the following */ result = 1.0; if (childnum <= 1) break; value2 = evaluateAST(child(n,0),data); for ( i=1; i<childnum; i++ ) { value1 = value2; value2 = evaluateAST(child(n,i),data); if (value1 <= value2) { result = 0.0; break; } } break; case AST_RELATIONAL_LEQ: /** n-ary: each child must be lower than or equal to the following */ result = 1.0; if (childnum <= 1) break; value2 = evaluateAST(child(n,0),data); for ( i=1; i<childnum; i++ ) { value1 = value2; value2 = evaluateAST(child(n,i),data); if (value1 > value2) { result = 0.0; break; } } break; case AST_RELATIONAL_LT : /* n-ary: each child must be lower than the following */ result = 1.0; if (childnum <= 1) break; value2 = evaluateAST(child(n,0),data); for ( i=1; i<childnum; i++ ) { value1 = value2; value2 = evaluateAST(child(n,i),data); if (value1 >= value2) { result = 0.0; break; } } break; case AST_RELATIONAL_NEQ: /* binary "not equal" */ result = (evaluateAST(child(n, 0), data) != evaluateAST(child(n, 1), data)); break; default: SolverError_error(FATAL_ERROR_TYPE, SOLVER_ERROR_AST_UNKNOWN_NODE_TYPE, "evaluateAST: unknown ASTNode type: %d", type); result = 0; break; } return result; }
/** * Reduces the given stack (containing SLR parser states and ASTNodes) by * the given grammar rule. */ ASTNode_t * FormulaParser_reduceStackByRule (Stack_t *stack, long rule) { ASTNode_t *result = NULL; ASTNode_t *lexpr, *rexpr, *operator; /** * Rule 1: Stmt -> Expr * Rule 9: Expr -> NUMBER * Rule 10: Expr -> NAME * Rule 13: OptArgs -> Args */ if (rule == 1 || rule == 9 || rule == 10 || rule == 13) { Stack_pop(stack); result = Stack_pop(stack); if (rule == 10) { /** * Convert result to a recognized L2 function constant (if * applicable). */ ASTNode_canonicalize(result); } } /** * Rule 2: Expr -> Expr PLUS Expr * Rule 3: Expr -> Expr MINUS Expr * Rule 4: Expr -> Expr TIMES Expr * Rule 5: Expr -> Expr DIVIDE Expr * Rule 6: Expr -> Expr POWER Expr */ else if (rule >= 2 && rule <= 6) { Stack_pop(stack); rexpr = Stack_pop(stack); Stack_pop(stack); operator = Stack_pop(stack); Stack_pop(stack); lexpr = Stack_pop(stack); ASTNode_addChild(operator, lexpr); ASTNode_addChild(operator, rexpr); result = operator; } /** * Rule 7: Expr -> MINUS Expr */ else if (rule == 7) { Stack_pop(stack); lexpr = Stack_pop(stack); Stack_pop(stack); operator = Stack_pop(stack); /** * Perform a simple tree reduction, if possible. * * If Expr is an AST_INTEGER or AST_REAL (or AST_REAL_E), simply negate * the numeric value. Otheriwse, a (unary) AST_MINUS node should be * returned. */ if (ASTNode_getType(lexpr) == AST_INTEGER) { ASTNode_setInteger(lexpr, - ASTNode_getInteger(lexpr)); ASTNode_free(operator); result = lexpr; } else if ( ASTNode_getType(lexpr) == AST_REAL) { ASTNode_setReal(lexpr, - ASTNode_getReal(lexpr)); ASTNode_free(operator); result = lexpr; } else if (ASTNode_getType(lexpr) == AST_REAL_E) { ASTNode_setRealWithExponent( lexpr, - ASTNode_getMantissa(lexpr), ASTNode_getExponent(lexpr) ); ASTNode_free(operator); result = lexpr; } else { ASTNode_addChild(operator, lexpr); result = operator; } } /** * Rule 8: Expr -> LPAREN Expr RPAREN */ else if (rule == 8) { Stack_pop(stack); ASTNode_free( Stack_pop(stack) ); Stack_pop(stack); result = Stack_pop(stack); Stack_pop(stack); ASTNode_free( Stack_pop(stack) ); } /** * Rule 11: Expr -> NAME LPAREN OptArgs RPAREN */ else if (rule == 11) { Stack_pop(stack); ASTNode_free( Stack_pop(stack) ); Stack_pop(stack); lexpr = Stack_pop(stack); Stack_pop(stack); ASTNode_free( Stack_pop(stack) ); Stack_pop(stack); result = Stack_pop(stack); ASTNode_setType(result, AST_FUNCTION); if (lexpr != NULL) { /** * Swap child pointers. In effect the NAME / AST_FUNCTION * represented by result (which has no children) will "adopt" the * children of OptArgs which are the arguments to the AST_FUNCTION. * * After this, OptArgs (lexpr) is no longer needed. */ ASTNode_swapChildren(lexpr, result); ASTNode_free(lexpr); } /** * Convert result to a recognized L2 function constant (if applicable). */ ASTNode_canonicalize(result); } /** * Rule 12: OptArgs -> [empty] */ else if (rule == 12) { result = NULL; } /** * Rule 14: Args -> Expr */ else if (rule == 14) { Stack_pop(stack); lexpr = Stack_pop(stack); result = ASTNode_create(); ASTNode_addChild(result, lexpr); } /** * Rule 15: Args -> Args COMMA Expr */ else if (rule == 15) { Stack_pop(stack); lexpr = Stack_pop(stack); Stack_pop(stack); ASTNode_free( Stack_pop(stack) ); Stack_pop(stack); result = Stack_pop(stack); ASTNode_addChild(result, lexpr); } return result; }
void check_AST(ASTNode_t* node, ASTNode_t* parent) { unsigned int i; int type; ASTNode_t *checker; if(node == NULL) { return; } checker = parent; for(i=0; i<ASTNode_getNumChildren(node); i++) { parent = node; check_AST(ASTNode_getChild(node, i), parent); } type = ASTNode_getType(node); switch(type) { case AST_PLUS: TRACE(("+ ")); break; case AST_MINUS: TRACE(("- ")); break; case AST_TIMES: TRACE(("* ")); break; case AST_DIVIDE: TRACE(("/ ")); break; case AST_POWER: TRACE(("pow ")); break; case AST_INTEGER: TRACE(("integer(%ld) ", ASTNode_getInteger(node))); break; case AST_REAL: TRACE(("real(%lf) ", ASTNode_getReal(node))); break; case AST_REAL_E: TRACE(("real_E ")); break; case AST_RATIONAL: TRACE(("rational ")); break; case AST_NAME: TRACE(("name(%s) ", ASTNode_getName(node))); break; case AST_NAME_AVOGADRO: TRACE(("avogadro ")); break; case AST_NAME_TIME: TRACE(("time ")); break; case AST_CONSTANT_E: TRACE(("constant ")); break; case AST_CONSTANT_FALSE: TRACE(("constant_false ")); break; case AST_CONSTANT_PI: TRACE(("pi ")); break; case AST_CONSTANT_TRUE: TRACE(("constant_true ")); break; case AST_LAMBDA: TRACE(("lambda ")); break; case AST_FUNCTION: TRACE(("function(%s) ", ASTNode_getName(node))); break; case AST_FUNCTION_ABS: TRACE(("abs ")); break; case AST_FUNCTION_ARCCOS: TRACE(("arccos ")); break; case AST_FUNCTION_ARCCOSH: TRACE(("arccosh ")); break; case AST_FUNCTION_ARCCOT: TRACE(("arccot ")); break; case AST_FUNCTION_ARCCOTH: TRACE(("arccoth ")); break; case AST_FUNCTION_ARCCSC: TRACE(("arccsc ")); break; case AST_FUNCTION_ARCCSCH: TRACE(("arccsch ")); break; case AST_FUNCTION_ARCSEC: TRACE(("arcsec ")); break; case AST_FUNCTION_ARCSECH: TRACE(("arcsech ")); break; case AST_FUNCTION_ARCSIN: TRACE(("arcsin ")); break; case AST_FUNCTION_ARCSINH: TRACE(("arcsinh ")); break; case AST_FUNCTION_ARCTAN: TRACE(("arctan ")); break; case AST_FUNCTION_ARCTANH: TRACE(("arctanh ")); break; case AST_FUNCTION_CEILING: TRACE(("ceil ")); break; case AST_FUNCTION_COS: TRACE(("cos ")); break; case AST_FUNCTION_COSH: TRACE(("cosh ")); break; case AST_FUNCTION_COT: TRACE(("cot ")); break; case AST_FUNCTION_COTH: TRACE(("coth ")); break; case AST_FUNCTION_CSC: TRACE(("csc ")); break; case AST_FUNCTION_CSCH: TRACE(("csch ")); break; case AST_FUNCTION_DELAY: TRACE(("delay ")); break; case AST_FUNCTION_EXP: TRACE(("exp ")); break; case AST_FUNCTION_FACTORIAL: TRACE(("! ")); break; case AST_FUNCTION_FLOOR: TRACE(("floor ")); break; case AST_FUNCTION_LN: TRACE(("ln ")); break; case AST_FUNCTION_LOG: TRACE(("log10 ")); break; case AST_FUNCTION_PIECEWISE: TRACE(("piecewise ")); break; case AST_FUNCTION_POWER: TRACE(("f_pow ")); break; case AST_FUNCTION_ROOT: TRACE(("sqrt ")); break; case AST_FUNCTION_SEC: TRACE(("sec ")); break; case AST_FUNCTION_SECH: TRACE(("sech ")); break; case AST_FUNCTION_SIN: TRACE(("sin ")); break; case AST_FUNCTION_SINH: TRACE(("sinh ")); break; case AST_FUNCTION_TAN: TRACE(("tan ")); break; case AST_FUNCTION_TANH: TRACE(("tanh ")); break; case AST_LOGICAL_AND: TRACE(("and ")); break; case AST_LOGICAL_NOT: TRACE(("not ")); break; case AST_LOGICAL_OR: TRACE(("or ")); break; case AST_LOGICAL_XOR: TRACE(("xor ")); break; case AST_RELATIONAL_EQ: TRACE(("eq ")); break; case AST_RELATIONAL_GEQ: TRACE(("geq ")); break; case AST_RELATIONAL_GT: TRACE(("gt ")); break; case AST_RELATIONAL_LEQ: TRACE(("leq ")); break; case AST_RELATIONAL_LT: TRACE(("lt ")); break; case AST_RELATIONAL_NEQ: TRACE(("neq ")); break; case AST_UNKNOWN: TRACE(("unknown ")); break; } if(checker == NULL) { TRACE(("\n\n")); } }