Algebra Expressions - CMUCTAT/CTAT GitHub Wiki

Algebra Expressions

CTAT comes with a module for manipulating algebra expressions. It consists of a grammar and a collection of functions for parsing, generating, evaluating, searching, comparing, and simplifying algebra expressions.

Algebra Language

The algebra grammar accepts expressions that cover the needs of middle-school algebra and more. An algebra expression consists of operands connected by usual math operators. Parentheses can also be used to group subexpressions together.

  • Operands can be variables or constants:

    • Variables are specified by single letters, upper case or lowercase
    • Constants are numbers in one of several formats:
      • Decimal integers, as a sequence of digits optionally preceded by a sign (1234, -5678)
      • Real decimal numbers, as a sequence of digits including a decimal dot, and optionally preceded by a sign (3.456, -0.678)
      • Real decimal numbers in scientific notation, as a real or integer number followed by the character E or e and by an (optionally signed) integer representing the 10-based exponent (1.234E+10, -0.056e-5)
      • Integers in bases 2, 8, or 16, as a 0 followed by a B or b for binary, O or o for octal, X or x for hexadecimal, and by a sequence of digits in the corresponding base (0B010001101, 0o1234567, 0X1234abc)
  • Operators can be in one of several groups, in order of decreasing precedence:

    • Power operators:
      • EXP: exponential operator, represented by one of: ^, **
      • SQRT: square root operator, represented by one of: |,
    • Unary sign operators:
      • UPLUS: unary plus operator, represented by: +
      • UMINUS: unary minus operator, represented by: -
    • Multiplication operators:
      • TIMES: multiplication operator, represented by one of: *, ×,
      • DIVIDE: division operator, represented by one of: '/', '÷'
      • ITIMES: implicit multiplication operator, with no explicit symbol representation
      • IDIVIDE: integer division operator, represented by: '//'
      • REM: reminder operator, represented by: '%'
    • Addition operators:
      • PLUS: addition operator, represented by: '+'
      • MINUS: subtraction operator, represented by: '-'
    • Relational operators:
      • LESS: less than operator, represented by: '<'
      • GREATER: greater than operator, represented by: '>'
      • LESSEQUAL: less than or equal operator, represented by one of: <=,
      • GREATEREQUAL: greater than or equal operator, represented by one of: >=,
      • EQUAL: equality operator, represented by one of: =, ==
      • NOTEQUAL: inequality operator, represented by one of: !=, /=, <>,

Operator precedence and associativity

The precedence and associativity rules are the usual for algebra expressions, as detailed below, where lower numbers mean greater precedence.

Operator Precedence Associativity
EXP, SQRT 1 right
UPLUS, UMINUS 2 right
TIMES, DIVIDE, ITIMES, IDIVIDE, REM 3 left
PLUS, MINUS 4 left
LESS, GREATER, LESSEQUAL, GREATEREQUAL, EQUAL, NOTEQUAL 5 none

Functions

The collection of algebra functions are meant to perform a variety of operations on algebra expressions given either as strings or as parse trees (resulted from previous function calls). The structure of parse tree nodes is considered proprietary and subject to change without notice, so authors should not rely on a specific configuration.

Functions that generate expressions can generate them as either strings or as parse trees. Usually the type of the result is the same as the type of the arguments, except for functions that are specifically designed to convert between the two formats.

Function Index

Basic Functions

These are functions that perform basic operations on algebra expressions.

algParse

algParse(expression)

  • Arguments:
    • expression - an algebra expression to parse, as a string or a tree
  • Result: a parse tree representing the given expression
  • Description: Parses the given expression and returns the parse tree. If the expression cannot be parsed, it returns null or raises a parse error, depending on the value of the parseErrors option. If the expression is already a parse tree it is returned unchanged.
  • Usage Examples: