Loading...
Searching...
No Matches
doc_what_is_astnode Class Reference

## Detailed Description

  An AST node in libSBML is a recursive tree structure; each node has a type, a pointer to a value, and a list of children nodes. Each ASTNode node may have none, one, two, or more children depending on its type. There are node types to represent numbers (with subtypes to distinguish integer, real, and rational numbers), names (e.g., constants or variables), simple mathematical operators, logical or relational operators and functions. The following diagram illustrates an example of how the mathematical expression `"1 + 2"` is represented as an AST with one plus node having two integer children nodes for the numbers `1` and `2`. The figure also shows the corresponding MathML representation:

Example AST representation of a mathematical expression.
Infix | AST | MathML
--- | --- | ---
`1 + 2` |  | `<math xmlns="http://www.w3.org/1998/Math/MathML">` `<apply>` `<plus/>` `<cn type="integer"> 1 </cn>` `<cn type="integer"> 2 </cn>` `</apply>` `</math>`

The following are other noteworthy points about the AST representation in libSBML:

- A numerical value represented in MathML as a real number with an exponent is preserved as such in the AST node representation, even if the number could be stored in a `double` data type. This is done so that when an SBML model is read in and then written out again, the amount of change introduced by libSBML to the SBML during the round-trip activity is minimized.

- Rational numbers are represented in an AST node using separate numerator and denominator values. These can be retrieved using the methods ASTNode::getNumerator() and ASTNode::getDenominator().

- The children of an ASTNode are other ASTNode objects. The list of children is empty for nodes that are leaf elements, such as numbers. For nodes that are actually roots of expression subtrees, the list of children points to the parsed objects that make up the rest of the expression.

For many applications, the details of ASTs are irrelevant because libSBML provides text-string based translation functions such as formulaToL3String() and parseL3Formula() . If you find the complexity of using the AST representation of expressions too high for your purposes, perhaps the string-based functions will be more suitable.