Arithmetic Expressions - CMUCTAT/CTAT GitHub Wiki
- algEquiv, algEquivalent
- algEquivTerms
- algEquivTermsSameOrder
- algEval
- eval
- expressionMatches
- isAlgValid
algEquiv(expression1, expression2[, sameOrder]) or algEquivalent(expression1, expression2[, sameOrder])
-
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]
-
expression1 -
-
Returns:
<Boolean>- TRUE or FALSE - Description: Determine whether 2 algebraic expressions are equivalent when fully simplified.
Example Usage
algEquiv(input, "4x+9")returns true if the student input is "9 + 4x" or "9+2x+2x"
More examples of behavior, where == and != indicate that the function returns true or false, respectively:
| expression1 | expression2 | |
|---|---|---|
| 2x + x - 3 + 5 | == | 2*x + x + (-3) + 5 |
| 2x + x - 3 + 5 | == | x*2 + 1x + 5 - 3 |
| 2x + x - 3 + 5 | == | 3x + 2 |
| 2(x + x - 3) + 5*2 | == | 2(x + 1x - 3) + 2*5 |
| 2(x + x - 3) + 5*2 | == | (x + 1x - 3)2 + 2*5 |
| 2(x + x - 3) + 5*2 | == | (x + x*1 - 3*1)2 + 2*5 |
| 2(x + x - 3) + 5*2 | == | 5*2 + (x + x + (-3))2 |
algEquivTerms(expression1, expression2)
-
Arguments:
-
expression1 -
<String>An algebraic expression -
expression2 -
<String>An algebraic expression
-
expression1 -
-
Returns:
<Boolean>- TRUE or FALSE - Description: Determine whether 2 algebraic expressions are equivalent when simplified to discrete terms. This function will declare 2 expressions equivalent if their differences are limited to order of commutative operands, redundant parentheses and notational variants (2*x and 2x).
Example Usage
algEquivTerms(input, "4x+9")returns true if the student input is "9 + 4x" but false if it's "9+2x+2x".
More examples of behavior, where == and != indicate that the function returns true or false, respectively:
| expression1 | expression2 | |
|---|---|---|
| 2x + x - 3 + 5 | == | 2*x + x + (-3) + 5 |
| 2x + x - 3 + 5 | == | x*2 + 1x + 5 - 3 |
| 2x + x - 3 + 5 | != | 3x + 2 |
| 2(x + x - 3) + 5*2 | == | 2(x + 1x - 3) + 2*5 |
| 2(x + x - 3) + 5*2 | == | (x + 1x - 3)2 + 2*5 |
| 2(x + x - 3) + 5*2 | == | (x + x*1 - 3*1)2 + 2*5 |
| 2(x + x - 3) + 5*2 | == | 5*2 + (x + x + (-3))2 |
algEquivTermsSameOrder(expression1, expression2)
-
Arguments:
-
expression1 -
<String>An algebraic expression -
expression2 -
<String>An algebraic expression
-
expression1 -
-
Returns:
<Boolean>- TRUE or FALSE - Description: Determine whether 2 algebraic expressions are equivalent when simplified to discrete terms. This function will declare 2 expressions equivalent if their differences are limited to order of commutative factor, redundant parentheses and notational variants (2*x and 2x). Unlike algEquivTerms, it will require that addends be in the same order.
Example Usage
algEquivTermsSameOrder(input, "4x+9")returns true if the student input is "x*4 + 9" but false if it's "9+4x" or "2x+2x+9"
More examples of behavior, where == and != indicate that the function returns true or false, respectively:
| expression1 | expression2 | |
|---|---|---|
| 2x + x - 3 + 5 | == | 2*x + x + (-3) + 5 |
| 2x + x - 3 + 5 | != | x*2 + 1x + 5 - 3 |
| 2x + x - 3 + 5 | != | 3x + 2 |
| 2(x + x - 3) + 5*2 | == | 2(x + 1x - 3) + 2*5 |
| 2(x + x - 3) + 5*2 | == | (x + 1x - 3)2 + 2*5 |
| 2(x + x - 3) + 5*2 | == | (x + x*1 - 3*1)2 + 2*5 |
| 2(x + x - 3) + 5*2 | != | 5*2 + (x + x + (-3))2 |
algEval(expression1)
-
Arguments:
-
expression1 -
<String>An algebraic expression
-
expression1 -
-
Returns:
<Number>- Result of evaluating the expression - Description: Attempt to evaluate an expression given the variable table and BRD-specific settings.
Example Usage
algEval("4x+9")returns 41 if x=8 in the variable table.
Simplify an expression.
In most cases, it is unnecessary to use the eval function explicitly in formulas. Instead, you can use the expression itself as the formula, e.g., "input/2".
Example Usage
eval(input/2)returns 5 if the student input is 10.
Same as algEquivalent(expression1, expression2, true); see algequiv, algEquivalent
isAlgValid(expression)
-
Arguments:
-
expression -
<String>An algebraic expression
-
expression -
-
Returns:
<Boolean>- TRUE or FALSE - Description: Determine whether the argument is a valid algebraic expression.
Example Usage
isAlgValid(input) returns true if the student input is "3y+2x+4"; returns false if the student input is "3y*+2x+4".