libSBML C++ API  5.18.0
FormulaFormatter.cpp File Reference

Formats an AST formula tree as an SBML formula string. More...

Include dependency graph for FormulaFormatter.cpp:

Functions

char * SBML_formulaToString (const ASTNode_t *tree)
 Converts an AST to a string representation of a formula using a syntax basically derived from SBML Level 1. More...
 

Detailed Description

Formats an AST formula tree as an SBML formula string.

Author
Ben Bornstein

Function Documentation

char* SBML_formulaToString ( const ASTNode_t tree)

Converts an AST to a string representation of a formula using a syntax basically derived from SBML Level 1.

The text-string form of mathematical formulas produced by SBML_formulaToString() and read by SBML_parseFormula() use a simple C-inspired infix notation taken from SBML Level 1. A formula in this text-string form therefore can be handed to a program that understands SBML Level 1 mathematical expressions, or used as part of a formula translation system. The syntax is described in detail in the documentation for ASTNode. The following are illustrative examples of formulas expressed using this syntax:
0.10 * k4^2
(vm * s1)/(km + s1)

Note that this facility is provided as a convenience by libSBML—the MathML standard does not actually define a "string-form" equivalent to MathML expression trees, so the choice of formula syntax is somewhat arbitrary. The approach taken by libSBML is to use the syntax defined by SBML Level 1 (which in fact used a text-string representation of formulas and not MathML). This formula syntax is based mostly on C programming syntax, and may contain operators, function calls, symbols, and white space characters. The following table provides the precedence rules for the different entities that may appear in formula strings.

Token Operation Class Precedence Associates
namesymbol referenceoperand6n/a
(expression)expression groupingoperand6n/a
f(...)function callprefix6left
-negationunary5right
^powerbinary4left
*multiplicationbinary3left
/divisonbinary3left
+additionbinary2left
-subtractionbinary2left
,argument delimiterbinary1left
A table of the expression operators and their precedence in the text-string format for mathematical expressions used by SBML_parseFormula().

In the table above, operand implies the construct is an operand, prefix implies the operation is applied to the following arguments, unary implies there is one argument, and binary implies there are two arguments. The values in the Precedence column show how the order of different types of operation are determined. For example, the expression a * b + c is evaluated as (a * b) + c because the * operator has higher precedence. The Associates column shows how the order of similar precedence operations is determined; for example, a - b + c is evaluated as (a - b) + c because the + and - operators are left-associative.

The function call syntax consists of a function name, followed by optional white space, followed by an opening parenthesis token, followed by a sequence of zero or more arguments separated by commas (with each comma optionally preceded and/or followed by zero or more white space characters, followed by a closing parenthesis token. The function name must be chosen from one of the pre-defined functions in SBML or a user-defined function in the model. The following table lists the names of certain common mathematical functions; this table corresponds to Table 6 in the SBML Level 1 Version 2 specification:

Name Args Formula or meaning Argument Constraints Result constraints
absxabsolute value of x
acosxarc cosine of x in radians-1.0 ≤ x ≤ 1.00 ≤ acos(x) ≤ π
asinxarc sine of x in radians-1.0 ≤ x ≤ 1.00 ≤ asin(x) ≤ π
atanxarc tangent of x in radians0 ≤ atan(x) ≤ π
ceilxsmallest number not less than x whose value is an exact integer
cosxcosine of x
expxe x, where e is the base of the natural logarithm
floorxlargest number not greater than x whose value is an exact integer
logxnatural logarithm of xx > 0
log10xbase 10 logarithm of xx > 0
powx, yx y
sqrxx2
sqrtxxx > 0sqrt(x) ≥ 0
sinxsine of x
tanxtangent of xx ≠ n*π/2, for odd integer n
The names of mathematical functions defined in the SBML Level 1 Version 2 text-string formula syntax.
Warning
There are differences between the symbols used to represent the common mathematical functions and the corresponding MathML token names. This is a potential source of incompatibilities. Note in particular that in this text-string syntax, log(x) represents the natural logarithm, whereas in MathML, the natural logarithm is <ln/>. Application writers are urged to be careful when translating between text forms and MathML forms, especially if they provide a direct text-string input facility to users of their software systems.
Parameters
treethe AST to be converted.
Returns
the formula from the given AST as an SBML Level 1 text-string mathematical formula. The caller owns the returned string and is responsible for freeing it when it is no longer needed.
See also
SBML_formulaToString()
SBML_parseL3FormulaWithSettings()
SBML_parseL3Formula()
SBML_parseL3FormulaWithModel()
SBML_getLastParseL3Error()
SBML_getDefaultL3ParserSettings()
Examples:
printMath.cpp, translateL3Math.cpp, and translateMath.cpp.