Symbolic expressions
Symbolic expressions (formulas, equations, polynomials, etc) are enclosed in single quotes (' ') and may include any mathematically valid combination of numbers, constants, variables, functions and operators.
Examples of valid symbolic expressions are:
'X+1'
'√(X+2)/(1-z)^2'
Working with expressions
Expressions can be manipulated in various ways:
Normal operations using the stack: 'X+1'
2
*
will produce the expression '(X+1)*2'
as expected. The result of operations involving expressions is always an expression. No other processing is performed, the expression remains as the user input, for example: '(X+1)*2'
2
/
will produce '((X+1)*2)/2'
.
Evaluating the expression: Using the commands EVAL1
, EVAL
and →NUM
will replace the contents of any variables that exist in the current directory (or upper) or as local variables while running a program, into the expression. The behavior of each command is only slightly different:
EVAL
will replace variables with their content, and if those contents include variables, it will recursively replace them as well until no more replacements are possible. Variables that are not defined remain as symbolic variables. Numerical operations will be performed as long as the result is guaranteed exact and integer, otherwise they will remain as a symbolic expression. For example 1+1/2
EVAL
will result in '3/2'. After EVAL
the expression is always simplified using AUTOSIMPLIFY
automatically (see the command AUTOSIMPLIFY
).
EVAL1
will replace variables with their contents, but will not recurse into the contents of the variables. For example assume the variable X
is defined as 'X+1'
. Evaluating the expression 'X+5'
with EVAL
would produce a circular reference error. Using EVAL1
would return 'X+6'
, after replacing X
only one time. After EVAL1
the expression is always simplified using AUTOSIMPLIFY
automatically (see the command AUTOSIMPLIFY
).
→NUM
will replace variables with their contents, recursively just as EVAL
but it requires the final result to be numeric. If any variable is undefined it will error.
Algebraic manipulation: Expressions can be modified by a number of commands that perform changes to the structure of the expression but preserving its symbolic nature. Most of the commands that modify an expression apply a combination of algebraic rules with other algorithms to produce the result. An example of this class of commands is the AUTOSIMPLIFY
command, which performs numeric operations and applies a series of simple rules.
Algebraic Rules
Rules are expressions used to match and replace parts of an expression with other expressions, and are the base of symbolic manipulation.
Rules are symbolic expressions themselves, which have a left and right parts, separated with the rule operator :→
. Rules can be applied to expressions with the command RULEAPPLY
and RULEAPPLY1
. The difference between them is (just like EVAL
and EVAL1
) that RULEAPPLY
will apply the rule multiple times until no more changes are produced to the expression, while RULEAPPLY1
will apply the rule only once. Both commands return the number of replacements that were performed, or zero if nothing was changed.
For example, 'X+Y+1'
'X+1:→Z'
RULEAPPLY
it will search into the expression 'X+Y+1'
and replace every occurrence of X+1
with Z
. The rules engine always assumes the +
operator is commutative, therefore it will try all permutations of the terms, resulting in the expression 'Z+Y'
and 1
indicating it performed one replacement.
The usefulness of rules only becomes evident when wildcards are used. Wildcards are special variable names that will match different objects. The rules engine recognizes wildcards by the 2 first characters of the variable name. All wildcards start with a dot, and the character that follows indicates what type of wildcard it is (what objects it will match). Other characters are an arbitrary name assigned by the user. In the table below the name VAR
is used as an example and can be substituted freely.
Wildcard | Meaning |
---|---|
.iVAR | Match a single integer number |
.oVAR | Match a single odd integer number |
.eVAR | Match a single even integer number |
.nVAR | Match a single number (not necessarily an integer) |
.NVAR | Match the largest possible numeric sub-expression |
.vVAR | Match a single variable |
.mVAR | Match any algebraic sub-expression, use non-commutative multiplication |
.xVAR | Match any algebraic sub-expression |
.MVAR | Match the largest possible algebraic sub-expression (non-commutative multiplication) |
.XVAR | Match the largest possible algebraic sub-expression |
For examples on the use of wildcards, let's use the expression '√3*X^2+2.5*X+5*X*Y+(A+B)*Y^2'
and apply some rules to it using RULEAPPLY
:
Rule | Result | Why? |
---|---|---|
'X^.iN:→Z^.iN' | '√3*Z^2+2.5*X+5*X*Y+(A+B)*Y^2' | Only the first term has the form X^i |
'.vX^.iN:→Z^.iN' | '√3*Z^2+2.5*X+5*X*Y+(A+B)*Z^2' | The variable wildcard .vX matches both X and Y and makes 2 replacements. Because we used RULEAPPLY , it tries again and then matches Z and replaces it in 2 places, reporting 4 total changes after stopping due to the expression not changing |
'.nN*.vX:→.nN*(.vX-.nN)' | '√3*X^2+2.5*(X-2.5)+5*(X-5)*(Y-5)+(A+B)*Y^2' | It finds 2.5*X and replaces, then finds 5*X , replaces with 5*(X-5) then on the same term finds 5*Y and replaces it with 5*(Y-5) , reporting a total of 3 replacements |
'.nN*.vX^2:→.nN*(.vX-1)^2' | '√3*X^2+2.5*X+5*X*Y+(A+B)*Y^2' | Does not make any replacements, since √3 is not a single number but a numeric expression |
'.NN*.vX^2:→.NN*(.vX-1)^2' | '√3*(X-1)^2+2.5*X+5*X*Y+(A+B)*Y^2' | Using .NN instead, matches √3 and does the replacement |
'.xN*.vX^2:→.xN*(.vX-1)^2' | '√3*(X-1)^2+2.5*X+5*X*Y+(A+B)*(Y-1)^2' | Using .xN instead, matches √3 and (A+B) as well |
'.NN*.vX^2+.XR:→.XR' | '2.5*X+5*X*Y+(A+B)*Y^2' | Finds √3*X^2'+… where .XR represents the rest of the expression, and removes the term |
'.xN*.vX^2+.XR:→.XR' | '2.5*X+5*X*Y' | Finds √3*X^2 ' and removes the term, then also finds (A+B)*Y^2 and removes it as well |
Attributes
Attributes are hints that the user can include in an expression to increase the knowledge that the system has about certain variables. For example, if variables A
and B
in the expression 'A*B*INV(A)'
represent a matrix, the system should not simplify that expression to 'B'
. Furthermore, if A
and B
are real numbers, the simplification is only valid when A
is known not to be zero.
Attributes allow the user to let the system know that A
is indeed a real number and it cannot be zero, allowing the rules to perform the desired simplifications. Attributes can simply be typed immediately after the variable name enclosed by colons. Following with the previous example 'A:R>0:'
is an identifier representing variable A
, but it includes a hint showing that A
is a finite positive real value and cannot be zero. If that hint is included wherever A
is used within the expression, the system will know that 'A*B*INV(A)'
can be safely simplified to 'B'
.
Notice that these attributes are only visible when editing the expression. Once the expression is in the stack, only the name of the variable will be visible. Ideally, the user should provide the same attributes to the same variables all throughout the expression (in other words, assumptions about a variable must be consistent, the variable cannot represents different things in different parts of the same expression).
The simplest way to assure the attributes are used consistently is using the command ASSUME
. For example 'X^2+3*X' 'X:R>0:' ASSUME
will replace all occurrences of X
within the given expression with the new identifier that includes the attributes. After the ASSUME
command, trying to edit the expression will reveal the attributes: 'X:R>0:^2+3*X:R>0:'
which may make the expression more difficult to read. To remove the attributes, simply use an identifier without any attributes as the second argument: 'X^2+3*X' 'X' ASSUME
will replace all occurrences of X
with the new identifier which makes no assumptions (no attributes included).
Attributes are also useful within rules. If a variable (or wildcard special variable) has any attributes given within a rule definition, it will only match variables (or expressions) that have compatible attributes. For example a rule to cancel out factors in an expression could be: '.xX/.xX:→1'
. But this is not correct if the expression being canceled may be zero. Using attributes, we can write '.xX:R≠0:/.xX:R≠0::→1'
and now it will only match expressions that are known to be real and are known not to be zero.
Default attributes
Variables that aren't given any attributes are by default assumed to be real if complex mode is disabled, and complex if complex mode is enabled. No other assumptions are made about their value (could be in any range, could be zero or infinite, etc.).
Encoding of attributes
The syntax for attributes is intuitive but very strict. Trying to enter invalid attributes will result in a syntax error.
Attribute strings consist of:
- One or two characters indicating the content type of the variable (real, complex, matrix, etc.)
- Optional two characters indicating a subset of the field (positive only, negative only, non-zero, etc.)
First one or two characters (type): | |
---|---|
Value | Meaning |
* 1) | Nothing is known about this variable |
R∞ | Variable is known to be real, may be infinite |
R | Variable is known to be real (and finite) |
Z | Variable is known to be integer (and finite) |
Z∞ | Variable is known to be integer, may be infinite |
O | Variable is known to be integer and odd (and finite) |
E | Variable is known to be integer and even (and finite) |
C∞ | Variable is known to be complex, may be infinite |
C | Variable is known to be complex (and finite) |
M | Variable is known to be a matrix |
? 2) | Variable is known to be of unknown type |
Notice that some combinations above are not valid, for example E∞
is not valid since infinity cannot be odd or even. Also a matrix cannot be infinite.
Optional subset: | |
---|---|
Value | Meaning |
≠0 | Value is known not to be zero 3) |
≥0 | Value is known not to be negative (therefore it's >=0) |
>0 | Value is known not to be negative and not to be zero (therefore it's >0) |
≤0 | Value is known not to be positive (therefore it's ≤0) |
<0 | Value is known not to be positive and not to be zero (therefore it's <0) |
The subsets above are only applicable to real numbers and integers, with the exception of ≠0
which also applies to complex numbers. Other types cannot have subsets (for example, cannot define what's a positive complex, or a negative matrix).
Using rules and attributes, examples
Here are a few examples where using attributes is useful to decide whether to apply a rule or not.
Rule | Effect |
---|---|
'ABS(.xX:R∞≥0:):→.xX:R∞≥0:' | Simplify absolute value of an expression that is known to be real ≥0 |
'ABS(.xX:R∞<0:):→-.xX:R∞<0:' | Simplify absolute value of an expression that is known to be real <0 |
The above rules, for example, it can be applied to expressions with different attributes in its variables giving different results:
Test cases | Result | Explanation |
---|---|---|
Y*ABS(X) | Y*ABS(X) | No rules are applied because X doesn't fit within the subsets defined in the rules |
Y*ABS(X:R>0:) | Y*X | The expression matches the first rule because X is known to be a real >0 |
Y*ABS(-4) | Y*(-(-4)) | The expression matches the second rule because -4 is known to be a real <0 |
Y*ABS(X:R>0:+1) | Y*(X+1) | The expression matches the first rule because X+1 is known to be a real >0 |
Y*ABS(X:R>0:-1) | Y*ABS(X-1) | The expression doesn't match either rule because X-1 could be <0 for 0<x<1 |
Y*ABS((X:R>0:-1)^2) | Y*(X-1)^2 | The expression matches because (X-1)^2 is known to be >=0 |
Y*ABS((X-1)^2) | Y*(X-1)^2 | The expression matches because (X-1)^2 is known to be >=0 regardless of X |