Next: Basic Rewrite Rules, Up: Rewrite Rules [Contents][Index]
Rewrite rules normally use the “assignment” operator
‘old := new’.
This operator is equivalent to the function call ‘assign(old, new)’.
The assign
function is undefined by itself in Calc, so an
assignment formula such as a rewrite rule will be left alone by ordinary
Calc commands. But certain commands, like the rewrite system, interpret
assignments in special ways.
For example, the rule ‘sin(x)^2 := 1-cos(x)^2’ says to replace every occurrence of the sine of something, squared, with one minus the square of the cosine of that same thing. All by itself as a formula on the stack it does nothing, but when given to the a r command it turns that command into a sine-squared-to-cosine-squared converter.
To specify a set of rules to be applied all at once, make a vector of rules.
When a r prompts you to enter the rewrite rules, you can answer in several ways:
If you enter the rules directly (as opposed to using rules stored in a variable), those rules will be put into the Trail so that you can retrieve them later. See Trail Commands.
It is most convenient to store rules you use often in a variable and
invoke them by giving the variable name. The s e
(calc-edit-variable
) command is an easy way to create or edit a
rule set stored in a variable. You may also wish to use s p
(calc-permanent-variable
) to save your rules permanently;
see Other Operations on Variables.
Rewrite rules are compiled into a special internal form for faster matching. If you enter a rule set directly it must be recompiled every time. If you store the rules in a variable and refer to them through that variable, they will be compiled once and saved away along with the variable for later reference. This is another good reason to store your rules in a variable.
Calc also accepts an obsolete notation for rules, as vectors ‘[old, new]’. But because it is easily confused with a vector of two rules, the use of this notation is no longer recommended.
Next: Basic Rewrite Rules, Up: Rewrite Rules [Contents][Index]