Algebra Expressions - CMUCTAT/CTAT GitHub Wiki

CTAT comes with a module that allows manipulation of algebra expressions. The module consists of a grammar and a library of functions for parsing, generating, searching, testing, comparing, and performing transformations on algebra expressions.

Index

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.

  • The grammar supports the following operands:
    • Variables as single letters, upper or lower case (e.g., x, y, z)
    • Numbers in one of the following 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)
    • Symbolic constants (e.g., pi, E)
    • Unknown (?)
  • The grammar supports the following operators, with their representations, in precedence order from highest to lowest:
    • Paretheses:
      • LPAREN: left parenthesis, unary operator: (
      • RPAREN: right parenthesis, unary operator: )
    • Power operators:
      • EXP: exponentiation, binary operator: ^, **
      • SQRT: square root, unary operator: |, √
    • Unary sign operators:
      • UPLUS: positive sign, unary operator: +
      • UMINUS: negative sign, unary operator: -
    • Multiplication operators:
      • TIMES: multiplication, binary operator: *, ×, ·
      • DIVIDE: division, binary operator: /, ÷
      • ITIMES: implicit multiplication, binary operator: (empty operator)
      • IDIVIDE: integer division, binary operator: //
      • REM: remainder, binary operator: %
    • Addition operators:
      • PLUS: addition, binary operator: +
      • MINUS: subtraction, binary operator: -
    • Relational operators:
      • LESS: less than, binary operator: <
      • LESSEQUAL: less than or equal to, binary operator: <=, ≤
      • GREATER: greater than, binary operator: >
      • GREATEREQUAL: greater than or equal to, binary operator: >=, ≥
      • EQUAL: equal to, binary operator: =, ==
      • NOTEQUAL: not equal to, binary operator: !=, /=, <>, ≠

For generated expressions, the grammar will use the first symbol in each group by default.

Examples of algebra expressions

2x^2 + 3x - 5
7x > 5y
2x + |3 = 5

Operator precedence and associativity

The grammar applies the following precedence and associativity to the accepted operators, where lower numbers mean greater precedence.

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

Transformation Rules

Some of the functions below take one or a list of transformation rules, and they apply those rules to expressions given as arguments. Transformations are also useful in expression simplification. These rules correspond to algebra axioms or theorems. Here's a list of the rules that can be applied to expressions. The transformation formulas below are considered formula schemas, in that the variables can be consistently substituted with any other expressions.

flatten

  • Description: Flatten an expression by removing redundant parentheses and converting negation and division to addition and multiplication respectively.
  • Formulas:
    a + (b + c) => a + b + c
    a - (b + c) => a - b - c
    a * (b * c) => a * b * c
    a / (b * c) => a / b / c

removeIdentity

  • Description: Remove identity elements from an expression.
  • Formulas:
    a + 0 => a
    a - 0 => a
    a * 1 => a
    a / 1 => a
    a^0 => 1
    a^1 => a

computeConstants

  • Description: Compute the value of constant expressions.
  • Formulas:
    a + b => c if a and b are constants and c is the sum of a and b
    a - b => c if a and b are constants and c is the difference of a and b
    a * b => c if a and b are constants and c is the product of a and b
    a / b => c if a and b are constants and c is the quotient of a and b
    a^b => c if a and b are constants and c is the power of a to b

combineSimilar

  • Description: Combine terms that have same variables.
  • Formulas:
    ax + bx => cx if a and b are constants and c is the sum of a and b
    a^x * b^x => c^x if a and b are constants and c is the product of a and b

expand

  • Description: Expand an expression by applying the distributive property.
  • Formulas:
    (a + b)^c => (a + b)(a + b)^(c - 1) if c is an integer greater than 1

distribute

  • Description: Apply the distributive property to an expression.
  • Formulas:
    a * (b + c) => a * b + a * c
    (a + b) * (c + d) => a * c + a * d + b * c + b * d
    a^(b + c) => a^b * a^c
    (a * b)^c => a^c * b^c

Functions

Functions in this module are used to manipulate algebra expressions. They can be used in matching formulas to determine whether a student's input is correct.

Expressions are passed to functions either as parse trees or as strings. The functions return values of both types, depending on what kind of expressions they received as arguments.

Some functions take a list of transformation rules, and they apply those rules to expressions given as arguments. Transformations are also useful in expression simplification.

Some functions take a bindings argument. It should be either a generic object whose properties are the variable names, or an object that implements a get method, taking a variable name as an argument. The bindings are used to evaluate expressions that contain variables. If the bindings argument is not given, the function uses values from the tutor's variable table.

Some functions can take options that control how the function behaves. The options are passed as an object with key-value pairs. The keys are the option names, and the values are the option values.

When functions run into errors, they return null.

Function Index

Basic Functions

These functions perform basic operations on expressions, like parsing, generating strings, sorting, evaluating, etc.

algParse

algParse(expression, {order = false, removeParentheses = false})

  • Arguments:

    • expression: <String|Tree> - an algebra expression to parse, as a string or a tree
    • order: <Boolean> - an option boolean indicating whether the result be sorted
    • removeParentheses: <Boolean> - an option boolean indicating whether to remove unnecessary parentheses
  • Result: <Tree> 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. If the expression is already a parse tree a deep copy of it is returned. If order is true, the result is sorted structurally, moving more complex subexpressions towards the end of the expression. If removeParentheses is true, unnecessary parentheses are removed from the result.

  • Usage Examples:

    algParse("2x + x - 3 + 5")

    => a parse tree representing the expression

algGetError

algGetError(expression)

  • Arguments:

    • expression: <String|Tree> - an algebra expression to parse, as a string or a tree
  • Result: <Object> - an error object describing a parse error generated while parsing the given expression

  • Description: Parses the given expression and returns null if successful. If the expression cannot be parsed, it returns the error object generated during the parse. The error object contains two properties: a message string and a hash of additional information. If the expression is already a parse tree it also returns null.

  • Usage Examples:

    algGetError("2(x + 1x - 3) + 2 * / 5")

    => an error object describing the parse error

algStringify

algStringify(expression)

  • Arguments:

    • expression: <String|Tree> - an algebra expression to stringify, as a string or a tree
  • Result: <String> - an algebra expression string representing the given expression

  • Description: Generates an algebra expression string representing the same expression as the input parse tree. If the input expression is a string it is returned unchanged.

  • Usage Examples:

    algStringify(tree)

    => 2(x + 1x - 3) + 2 * 5 if tree is the tree generated from the same expression by algParse

algEvaluate

algEvaluate(expression, bindings = null)

  • Arguments:

    • expression: <String|Tree> - an algebra expression to evaluate
    • bindings: <Object|null> - an option object representing the variable bindings to use in the evaluation
  • Returns: <Number> - result of evaluating the expression

  • Description: Attempt to evaluate an expression given the variable table and BRD-specific settings. The bindings object, if given, should be either a generic object whose properties are the variable names, or an object that implements a get method, taking a variable name as an argument. Missing variables are considered unknown, in which case the function returns false. If bindings is given, the function will use the values specified in it; otherwise it uses values from the tutor's variable table. If the expression contains variables that don't have a value in either place, it returns NaN. If the expression is already a parse tree, the function evaluates it directly. If the expression is a string, it parses it first and then evaluates the resulting parse tree. Usage Examples:

    algEvaluate("4x + 9")

    => 41if x=8 in the variable table.

    algEvaluate("4x + 9", {x: 4})

    => 25

algSort

algSort(expression)

  • Arguments:

    • expression: <String|Tree> - an algebra expression to sort, as a string or a tree
  • Result: <String|Tree> - an algebra expression as a string or a tree, representing the sorted expression

  • Description: Returns the expression sorted structurally. The sorting moves more complex subexpressions towards the end of the expression.

  • Usage Examples:

    algSort("x + 2x - 3 + 5")

    => "2x + x + 5 - 3"

    algSort("5 * 2 + (x + x + (-3)) * 2")

    => "2(x + x + (-3)) + 5 * 2"

Expression Inspection

These functions search a given expression for generic types of elements and return the elements found.

algGetOperator

algGetOperator(expression)

  • Arguments:

    • expression: <String|Tree> - an algebra expression to search, as a string or a tree
  • Result: <String> - an operator strings representing the main operator in the expression

  • Description: Returns the main (top level in parse tree) operator of the given expression. If the expression is given as a string it is parsed first.

  • Usage Examples:

    algGetOperator("5 * 2 + (x + x + (-3)) * 2")

    => "PLUS"

    algGetOperator("(x * y)^3")

    => "EXP"

algGetOperators

algGetOperators(expression)

  • Arguments:

    • expression: <String|Tree> - an algebra expression to search, as a string or a tree
  • Result: <Array<String>> - an array of operator strings representing all operators in the expression

  • Description: Returns the array of operators in the given expression. If the expression is given as a string it is parsed first. The list is sorted by the order of the operators in the expression (inorder tree traversal) and can contain duplicates.

  • Usage Examples:

    algGetOperators("5 * 2 + (x + x + (-3)) * 2")

    => ["TIMES", "PLUS", "PLUS", "PLUS", "UMINUS", "TIMES"]

    algGetOperators("(x * y)^3")

    => ["TIMES", "EXP"]

algGetVariables

algGetVariables(expression)

  • Arguments:

    • expression: <String|Tree> - an algebra expression to search, as a string or a tree
  • Result: <Array<String>> - an array of strings representing all variables in the expression

  • Description: Returns the list (as a Javascript array) of variables names in the given expression. The list is sorted by the order of the variables in the expression (inorder tree traversal) and can contain duplicates.

  • Usage Examples:

    algGetVariables("2(x + 1x - 3) + 2 * / 5")

    => ["x", "x"]

    algGetVariables("(x * y)^3")

    => ["x", "y"]

algGetOperands

algGetOperands(expression)

  • Arguments:

    • expression: <String|Tree> - an algebra expression to search, as a string or a tree
  • Result: <Array<String|Tree>> - an array of subexpressions as strings or trees, representing the operands for the main expression operator

  • Description: Returns the list (as a Javascript array) of operands for the main operator in the given expression. The components of the list are either strings or parse trees, depending on the type of the given expression. The list is sorted in the order the operands occur in the expression.

  • Usage Examples:

    algGetOperands("5 * 2 + (x + x + (-3)) * 2")

    => ["5 * 2", "(x + x + (-3)) * 2"]

    algGetOperands("(x * y)^3")

    => ["(x * y)", "3"]

algGetExpression

algGetExpression(expression, {start = 0, end = Infinity})

  • Arguments:

    • expression: <String|Tree> - an algebra expression to search, as a string or a tree
    • start: <Integer> - an option integer representing the start index in the expression string (defaulting to 0)
    • end: <Integer> - an option integer representing the end index in the expression string (defaulting to the end of the expression)
  • Result: <String|Tree> - a subexpression as string or tree, representing the expression between the given indices

  • Description: Returns a subexpression that represents the expression between the given indices of the whole expression string. It only works on expressions that result from parsing a string, without any other manipulations. The indices need to spread a proper subexpression, otherwise the result is null (but extra spaces around the subexpression are acceptable).

  • Usage Examples:

    algGetExpression("5 * 2 + (x + x + (-3)) * 2")

    => "5 * 2 + (x + x + (-3)) * 2"

Expression Manipulations

These functions perform specific transformations on given expressions.

algFindExpression

algFindExpression(expression, subexpression, {paths = false})

  • Arguments:

    • expression: <String|Tree> - an algebra expression to search, as a string or a tree
    • subexpression: <String|Tree> - an algebra subexpression to search for, as a string or a tree
    • paths: <Boolean> - an option boolean indicating whether to return location information in the result
  • Result: <Array<String|Tree>> - an array of expressions as strings or trees, representing subexpressions equal to the given subexpression

  • Description: Returns a list of subexpressions of the given expression (including possibly the whole expression) that are equal to the given subexpression. The test is done at the parse tree level, not at the string level. The list is sorted by the order of the subexpression operators in the expression (inorder tree traversal). If paths is set to true, the result will include location information in the form of paths that can be used subsequently in transformation operations. If the results are strings, they will be boxed in objects of String type, so path information can be attached.

  • Usage Examples:

    algFindExpression("5 * 2 + (x + x + (-3)) * 2", "5 * 2")

    => ["5 * 2"]

    algFindExpression("5 * 2 + (x + x + (-3)) * 2", "x", {paths: true})

    => ["x", "x"] {path: ...}

algCreateExpression

algCreateExpression(operator, expression, expressions...)

  • Arguments:

    • operator: <String> - an operator string to use in new expression
    • expression: <String|Tree> - an algebra expression, as a string or a tree
    • expressions...: <String|Tree>... - a sequence of one or more algebra expressions, each as a string or a tree
  • Result: <String|Tree> - a new algebra expression as a string or tree, representing the result of creating a new expression with the given operator and operands

  • Description: Returns a new expression created by using the given operator as the main operator, the given expression as the first or only operand, and the other given expressions... as the rest of the operands. Returns null if not enough operands are provided. Ignores any extra operands (but "PLUS" and "MINUS" operators can take any number of operands higher or equal than 2). The operator is one of the string names in the list of operators listed in Algebra Language.

  • Usage Examples:

    algCreateExpression("PLUS", "5 * 2", "(x + x + (-3)) * 2")

    => "5 * 2 + (x + x + (-3)) * 2"

    algCreateExpression("EXP", "(x * y)", "3")

    => "(x * y) ^ 3"

algReplaceExpression

algReplaceExpression(expression, oldsubexpression, newsubexpression, locator = null)

  • Arguments:

    • expression: <String|Tree> - an algebra expression to change, as a string or a tree
    • oldsubexpression: <String|Tree> - an algebra subexpression to replace, as a string or a tree
    • newsubexpression: <String|Tree> - an algebra subexpression to replace with, as a string or a tree
    • locator: <Array|Number|String> - an option object representing the old subexpression location
  • Result: <String|Tree> - an expression as a string or tree, representing the result of the replacement

  • Description: Returns a new expression that is the result of replacing the oldsubexpression with the newsubexpression in the given expression. The locator parameter can be an object used to select among multiple occurrences of the given oldsubexpression. It can be a path array, an index number, or a string representing the path of the subexpression in the whole expression's parse tree. If the location parameter is missing and oldsubexpression is obtained through a previous algFindExpression call with a paths option of true, it already contains location information allowing for its efficient retrieval within expression. If subexpression does not include location information and location is undefined, the function will replace the first occurrence of the oldsubexpression with newsubexpression.

  • Usage Examples:

    algReplaceExpression("(x * x) ^ 3", "x", "z")

    => "(z * x) ^ 3"

    algReplaceExpression("(x * x) ^ 3", "x", "z", ["base"])

    => "z ^ 3"

    algReplaceExpression("(x * x) ^ 3", "x", "z", ["exp"])

    => null

algDeleteExpression

algDeleteExpression(expression, subexpression, locator = null)

  • Arguments:

    • expression: <String|Tree> - an algebra expression to change, as a string or a tree
    • subexpression: <String|Tree> - an algebra subexpression to delete, as a string or a tree
    • locator: ``<Array|Number|String>` - an option object representing the old subexpression location
  • Result: <String|Tree> - an expression as a string or tree, representing the result of the deletion

  • Description: Returns a new expression that is the result of deleting the subexpression, together with its corresponding operators, from the given expression. The locator parameter can be an object used to select among multiple occurrences of the given oldsubexpression. It can be a path array, an index number, or a string representing the path of the subexpression in the whole expression's parse tree. If the locator parameter is missing and subexpression is obtained through a previous algFindExpression call with a paths option of true, it already contains location information allowing for its efficient retrieval within expression. If subexpression does not include location information and location is undefined, the function will delete the first occurrence of the subexpression in expression.

  • Usage Examples:

    algDeleteExpression("(x * x) ^ 3", "x")

    => "x ^ 3"

    algDeleteExpression("(x * x) ^ 3", "x", ["base"])

    => "3"

    algDeleteExpression("(x * x) ^ 3", "x", ["exp"])

    => null

Expression Simplification

algApplyRulesSelectively

algApplyRulesSelectively(expression, rules, indices..., {global = true})

  • Arguments:

    • expression: <String|Tree> - an algebra expression to transform, as a string or a tree
    • rules: <String|Array> - a rule name string or an array of rule names to apply
    • indices: <Array<Integer>> - an option array of integers representing the subset of operands to which the rules should be applied
    • global: <Boolean> - an option boolean indicating application scope
  • Result: <String|Tree> - an expression as a string or tree, representing the result of applying the given rules to one or more subexpressions in the given expression

  • Description: Returns an expression that is the result of applying the rules in the given second argument to subexpression of the first argument specified by the rest of the arguments. This function is useful for expressions with an additive or multiplicative operator, which could have multiple operands. On other expressions it performs the same change as algApplyRules. If index is present, it selects the first operand to apply the rule to, which is also the place of the result if some of the rules results in combining multiple operands. If indices is present, it selects the rest subset of operands to apply the rules to. If both index and indices are missing, the rules are applied to all operands. global indicates whether the rules are applied to the whole expression or rather only to the main operator in the expression. So if it is missing, the rules are applied to the whole expression. If rules is missing or empty, it returns the expression unchanged.

  • Usage Examples:

    algApplyRulesSelectively("2(1x + 1x - 3) + 2 * 5", ["removeIdentity"], 0)

    => "2(x + x - 3) + 2 * 5"

    algApplyRulesSelectively("2(1x + 1x - 3) + 2 * 5", ["removeIdentity"], 1)

    => "2(1x + 1x - 3) + 2 * 5"

    algApplyRulesSelectively("1x + 1x - 3", ["removeIdentity"], 1)

    => "1x + x - 3"

    algApplyRulesSelectively("1x + 1x - 3", ["removeIdentity"], 0, 1)

    => "x + x - 3"

    algApplyRulesSelectively("2(x + 1x - 3) + 2 * 5", ["combineSimilar"], 0)

    => "2(2x - 3) + 2 * 5"

algApplyRules

algApplyRules(expression, rules, {global = true})

  • Arguments:

    • expression: <String|Tree> - an algebra expression to transform, as a string or a tree
    • rules: <String|Array> - a rule name string or an array of rule names to apply
    • global: <Boolean> - an option boolean indicating application scope
  • Result: <String|Tree> - an expression as a string or tree, representing the result of applying the given rules to the given expression

  • Description: Returns an expression that is the result of applying the rule or rules in the given second argument to the expression in the first argument. global indicates whether the rules are applied to the whole expression or rather only to the main operator in the expression. So if it is missing, the rules are applied to the whole expression. If rules is missing or empty, it returns the expression unchanged.

  • Usage Examples:

    algApplyRules("a / (b * c)", ["flatten"])

    => "a / b / c"

    algApplyRules("2(x + 1x - 3) + 2 * 5", ["removeIdentity"])

    => "2(x + x - 3) + 2 * 5"

    algApplyRules("2(x + 1x - 3) + 2 * 5", ["combineSimilar"])

    => "2(2x - 3) + 10"

algPartiallySimplify

algPartiallySimplify(expression, {order = false})

  • Arguments:

    • expression: <String|Tree> - an algebra expression to simplify, as a string or a tree
    • order: <Boolean> - an option boolean indicating whether the result be sorted
  • Result: <String|Tree> - a partially simplified expression equivalent to the given expression, as a string or tree

  • Description: Returns an expression that represents the given expression simplified by applying the transformation rules of flatten and removeIdentity. If order is true, the result will be sorted structurally, moving more complex subexpressions towards the start of the expression.

  • Usage Examples:

    algPartiallySimplify("2 * 5 + 2(x + (1x - 3))")

    => "2 * 5 + 2(x + x - 3)"

    algPartiallySimplify("2 * 5 + 2(x + (1x - 3))", {order: true})

    => "2(x + x - 3) + 2 * 5"

algSimplify

algSimplify(expression, {order = false})

  • Arguments:

    • expression: <String|Tree> - an algebra expression to simplify, as a string or a tree
    • order: <Boolean> - an option boolean indicating whether the result be sorted
  • Result: <String|Tree> - a simplified expression equivalent to the given expression, as a string or tree

  • Description: Returns an expression that represents the given expression simplified by applying all transformation rules. If order is true, the result will be sorted structurally, moving more complex subexpressions towards the start of the expression.

  • Usage Examples:

    algSimplify("2 * 5 + 2(x + (1x - 3))")

    => "4 + 4x"

    algSimplify("2 * 5 + 2(x + (1x - 3))", {order: true})

    => "4x + 4"

Single Expression Tests

These functions perform basic tests corresponding to some of the functions in previous sections, that is they test whether the given expression would return a valid result or the same result when one of the functions is called on it.

algValid

algValid(expression, {ordered = false})

  • Arguments:

    • expression: <String|Tree> - an algebra expression to test, as a string or a tree
    • ordered: <Boolean> - an option boolean indicating whether the expression is sorted
  • Result: <Boolean> - a boolean value indication whether the given expression is valid or not

  • Description: Returns a boolean value that indicates whether the given expression is valid or not. Expression validity is checked by attempting to parse it. It corresponds to function algParse. If ordered is true, the function also checks whether the expression is sorted structurally, moving more complex subexpressions towards the start of the expression. It corresponds to function algSort.

  • Usage Examples:

    algValid("3y + 2x + 4")

    => true

    algValid("3y * / 2x + 4")

    => false

algValued

algValued(expression, bindings = null)

  • Arguments:

    • expression: <String|Tree> - an algebra expression to test, as a string or a tree
    • bindings: <Object|null> - an optional object representing a list of variable bindings
  • Result: <Boolean> - a boolean value indicating whether the given expression has a value or is unknown

  • Description: Returns a boolean value that indicates whether the given expression can be evaluated to number or not (because some variables might not have a value). The test is performed by attempting to evaluate the expression using function algEvaluate. The bindings object, if given, should be either a generic object whose properties are the variable names, or an object that implements a get method, taking a variable name as an argument. Variables in the expression are evaluated against their values in the bindings object, if present, otherwise in the tutor's variable table. Missing variables are considered unknown, in which case the function returns false. It corresponds to function algEvaluate.

  • Usage Examples:

    algValued("4x + 9")

    => trueif x has a value in the variable table, false otherwise

    algValued("4x + 9", {x: 4})

    => true

algPartiallySimplified

algpartiallySimplified(expression, {ordered = false})

  • Arguments:

    • expression: <String|Tree> - an algebra expression to test, as a string or a tree
    • ordered: <Boolean> - an option boolean indicating whether order should matter for the test
  • Result: <Boolean> - a boolean value indicating if the given expression is partially simplified

  • Description: Returns a boolean value that indicates whether the given expression is partially simplified. The test is done by applying the algPartiallySimplify function to the given expression, and comparing the result against the original parsed expression. It corresponds to function algPartiallySimplify.

  • Usage Examples:

    algPartiallySimplified("2 * 5 + 2(x + (1x - 3))")

    => false

    algPartiallySimplified("2 * 5 + 2(x + x - 3)")

    => true

algSimplified

algSimplified(expression, {ordered = false})

  • Arguments:

    • expression: <String|Tree> - an algebra expression to test, as a string or a tree
    • ordered: <Boolean> - an option boolean indicating whether order should matter for the test
  • Result: <Boolean> - a boolean value indicating if the given expression is simplified

  • Description: Returns a boolean value that indicates whether the given expression is fully simplified. The test is done by applying the algSimplify function to the given expression, and comparing the result against the original parsed expression. It corresponds to function algSimplify.

  • Usage Examples:

    algSimplified("2 * 5 + 2(x + x - 3)")

    => false

    algSimplified("4 + 4x")

    => true

    algSimplified("4 + 4x", {ordered: true})

    => false

    algSimplified("4x + 4", {ordered: true})

    => true

Expression Comparison Tests

These functions perform comparison tests on two given expressions to determine if they are 'equivalent' from a specific point of view. They mostly work by applying one of the previous functions on the two expressions and comparing the results.

algIdentical

algIdentical(expression1, expression2, {sameOrder = false, ignoreParentheses = false})

  • Arguments:

    • expression1: <String|Tree> - an algebra expression to compare, as a string or a tree
    • expression2: <String|Tree> - an algebra expression to compare, as a string or a tree
    • sameOrder: <Boolean> - an option boolean indicating whether order should matter for the test
    • ignoreParentheses: <Boolean> - an option boolean indicating whether parentheses should be ignored for the test
  • Result: <Boolean> - a boolean value indicating whether the given expressions are identical

  • Description: Returns a boolean value that indicates whether the two given expressions are identical. Identity is tested by parsing and sorting the two expressions and comparing the results, so two expressions ordered differently are still considered identical. If sameOrder is true, the expressions must also be ordered the same way, so they are not sorted before being compared. It uses the function algSort for sorting. If ignoreParentheses is true, the expressions are compared ignoring parentheses.

  • Usage Examples:

    algIdentical("2 * 5 + 2(x + x - 3)", "2(x + x - 3) + 2 * 5")

    => true

    algIdentical("2 * 5 + 2(x + x - 3)", "2(x + x - 3) + 2 * 5", {sameOrder: true})

    => false

    algIdentical("2 * 5 + 2(x + x - 3)", "2(x + (x - 3)) + 2 * 5")

    => false

    algIdentical("2 * 5 + 2(x + x - 3)", "2(x + (x - 3)) + 2 * 5", {ignoreParentheses: true})

    => true

algEqual

algEqual(expression1, expression2, bindings = null)

  • Arguments:

    • expression1: <String|Tree> - an algebra expression to compare, as a string or a tree
    • expression2: <String|Tree> - an algebra expression to compare, as a string or a tree
    • bindings: <Object|null> - an optional object representing a list of variable bindings
  • Result: <Boolean> - a boolean value indicating whether the given expressions have the same value

  • Description: Returns a boolean value that indicates whether the two given expressions evaluate to the same numeric value. The test is performed by evaluating the two expressions using function algEvaluate, and comparing the results. Variables in the expression are evaluated against their values in the bindings object, if present, otherwise in the tutor's variable table. The bindings object should be either a generic object whose properties are the variable names, or an object that implements a get method, taking a variable name as an argument. If one of the expressions cannot be evaluated to a number, the function returns false.

  • Usage Examples:

    algEqual("4x + 9", "3x + 14")

    => true if x has a value of 5 in the variable table, false otherwise

    algEqual("4x + 9", "3x + 14", {x: 5})

    => true

    algEqual("4x + 9", "3x + 14", {x: 4})

    => false

algPartiallyEquivalent

algPartiallyEquivalent(expression1, expression2, {sameOrder = false})

  • Arguments:

    • expression1: <String|Tree> - an algebra expression to compare, as a string or a tree
    • expression2: <String|Tree> - an algebra expression to compare, as a string or a tree
    • sameOrder: <Boolean> - an option boolean indicating whether order should matter for the test
  • Result: <Boolean> - a boolean value indicating whether the given expressions are partially equivalent

  • Description: Returns a boolean value that indicates whether the two given expressions are partially equivalent. The test is performed by partially simplifying the two expressions and comparing the results. It uses function algPartiallySimplify for simplification. If sameOrder is true, the expressions must also be ordered the same way, so they are not sorted before being compared. It uses the function algSort for sorting. It corresponds to function algPartiallySimplify.

  • Usage Examples:

    The following table shows examples of results for the function call algPartiallyEquivalent(expression1, expression2):

    expression1 expression2 Result
    2x + x - 3 + 5 2 * x + x + (-3) + 5 true
    2x + x - 3 + 5 x * 2 + 1x + 5 - 3 true
    2x + x - 3 + 5 3x + 2 false
    2(x + x - 3) + 5 * 2 2(x + 1x - 3) + 2 * 5 true
    2(x + x - 3) + 5 * 2 (x + 1x - 3)2 + 2 * 5 true
    2(x + x - 3) + 5 * 2 (x + x * 1 - 3 * 1)2 + 2 * 5 true
    2(x + x - 3) + 5 * 2 5 * 2 + (x + x + (-3))2 true

    The following table shows examples of results for the function call algPartiallyEquivalent(expression1, expression2, {sameOrder: true}):

    expression1 expression2 Result
    2x + x - 3 + 5 2 * x + x + (-3) + 5 true
    2x + x - 3 + 5 x * 2 + 1x + 5 - 3 false
    2x + x - 3 + 5 3x + 2 false
    2(x + x - 3) + 5 * 2 2(x + 1x - 3) + 2 * 5 false
    2(x + x - 3) + 5 * 2 (x + 1x - 3)2 + 2 * 5 false
    2(x + x - 3) + 5 * 2 (x + x * 1 - 3 * 1)2 + 2 * 5 false
    2(x + x - 3) + 5 * 2 5 * 2 + (x + x + (-3))2 false

algEquivalent

algEquivalent(expression1, expression2, {sameOrder = false})

  • Arguments:

    • expression1: - <String> An algebraic expression
    • expression2: - <String> An algebraic expression
    • sameOrder: - <Boolean> Whether to require terms to be in the same order [optional, default is false]
  • Returns: <Boolean> - a boolean value indicating whether the given expressions are equivalent

  • Description: Returns a boolean value that indicates whether the two given expressions are equivalent. The test is performed by simplifying the two expressions and comparing the results. It uses function algSimplify for simplification. If sameOrder is true, the expressions must also be ordered the same way, so they are not sorted before being compared. It uses the function algSort for sorting. It corresponds to function algSimplify.

  • Usage Examples:

    The following table shows examples of results for the function call algEquivalent(expression1, expression2):

    expression1 expression2 Result
    2x + x - 3 + 5 2 * x + x + (-3) + 5 true
    2x + x - 3 + 5 x * 2 + 1x + 5 - 3 true
    2x + x - 3 + 5 3x + 2 true
    2(x + x - 3) + 5 * 2 2(x + 1x - 3) + 2 * 5 true
    2(x + x - 3) + 5 * 2 (x + 1x - 3)2 + 2 * 5 true
    2(x + x - 3) + 5 * 2 (x + x * 1 - 3 * 1)2 + 2 * 5 true
    2(x + x - 3) + 5 * 2 5 * 2 + (x + x + (-3))2 true

    The following table shows examples of results for the function call algEquivalent(expression1, expression2, {sameOrder: true}):

    expression1 expression2 Result
    2x + x - 3 + 5 2 * x + x + (-3) + 5 true
    2x + x - 3 + 5 x * 2 + 1x + 5 - 3 true
    2x + x - 3 + 5 3x + 2 true
    2(x + x - 3) + 5 * 2 2(x + 1x - 3) + 2 * 5 true
    2(x + x - 3) + 5 * 2 (x + 1x - 3)2 + 2 * 5 true
    2(x + x - 3) + 5 * 2 (x + x * 1 - 3 * 1)2 + 2 * 5 true
    2(x + x - 3) + 5 * 2 5 * 2 + (x + x + (-3))2 false

Back to top

Next >> Logic Expressions

⚠️ **GitHub.com Fallback** ⚠️