home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 8
/
FreshFishVol8-CD1.bin
/
gnu
/
info
/
calc.info-21
(
.txt
)
< prev
next >
Wrap
GNU Info File
|
1994-12-22
|
50KB
|
841 lines
This is Info file calc.info, produced by Makeinfo-1.55 from the input
file calc.texinfo.
This file documents Calc, the GNU Emacs calculator.
Copyright (C) 1990, 1991 Free Software Foundation, Inc.
Permission is granted to make and distribute verbatim copies of this
manual provided the copyright notice and this permission notice are
preserved on all copies.
Permission is granted to copy and distribute modified versions of
this manual under the conditions for verbatim copying, provided also
that the section entitled "GNU General Public License" is included
exactly as in the original, and provided that the entire resulting
derived work is distributed under the terms of a permission notice
identical to this one.
Permission is granted to copy and distribute translations of this
manual into another language, under the above conditions for modified
versions, except that the section entitled "GNU General Public License"
may be included in a translation approved by the author instead of in
the original English.
File: calc.info, Node: Conditional Rewrite Rules, Next: Algebraic Properties of Rewrite Rules, Prev: Basic Rewrite Rules, Up: Rewrite Rules
Conditional Rewrite Rules
-------------------------
A rewrite rule can also be "conditional", written in the form `OLD :=
NEW :: COND'. (There is also the obsolete form `[OLD, NEW, COND]'.)
If a COND part is present in the rule, this is an additional condition
that must be satisfied before the rule is accepted. Once OLD has been
successfully matched to the target expression, COND is evaluated (with
all the meta-variables substituted for the values they matched) and
simplified with `a s' (`calc-simplify'). If the result is a nonzero
number or any other object known to be nonzero (*note Declarations::.),
the rule is accepted. If the result is zero or if it is a symbolic
formula that is not known to be nonzero, the rule is rejected. *Note
Logical Operations::, for a number of functions that return 1 or 0
according to the results of various tests.
For example, the formula `n > 0' simplifies to 1 or 0 if `n' is
replaced by a positive or nonpositive number, respectively (or if `n'
has been declared to be positive or nonpositive). Thus, the rule
`f(x,y) := g(y+x,x) :: x+y > 0' would apply to `f(0, 4)' but not to
`f(-3, 2)' or `f(12, a+1)' (assuming no outstanding declarations for
`a'). In the case of `f(-3, 2)', the condition can be shown not to be
satisfied; in the case of `f(12, a+1)', the condition merely cannot be
shown to be satisfied, but that is enough to reject the rule.
While Calc will use declarations to reason about variables in the
formula being rewritten, declarations do not apply to meta-variables.
For example, the rule `f(a) := g(a+1)' will match for any values of
`a', such as complex numbers, vectors, or formulas, even if `a' has
been declared to be real or scalar. If you want the meta-variable `a'
to match only literal real numbers, use `f(a) := g(a+1) :: real(a)'.
If you want `a' to match only reals and formulas which are provably
real, use `dreal(a)' as the condition.
The `::' operator is a shorthand for the `condition' function; `OLD
:= NEW :: COND' is equivalent to the formula `condition(assign(OLD,
NEW), COND)'.
If you have several conditions, you can use `... :: c1 :: c2 :: c3'
or `... :: c1 && c2 && c3'. The two are entirely equivalent.
It is also possible to embed conditions inside the pattern: `f(x ::
x>0, y) := g(y+x, x)'. This is purely a notational convenience,
though; where a condition appears in a rule has no effect on when it is
tested. The rewrite-rule compiler automatically decides when it is
best to test each condition while a rule is being matched.
Certain conditions are handled as special cases by the rewrite rule
system and are tested very efficiently: Where `x' is any
meta-variable, these conditions are `integer(x)', `real(x)',
`constant(x)', `negative(x)', `x >= y' where `y' is either a constant
or another meta-variable and `>=' may be replaced by any of the six
relational operators, and `x % a = b' where `a' and `b' are constants.
Other conditions, like `x >= y+1' or `dreal(x)', will be less efficient
to check since Calc must bring the whole evaluator and simplifier into
play.
An interesting property of `::' is that neither of its arguments
will be touched by Calc's default simplifications. This is important
because conditions often are expressions that cannot safely be
evaluated early. For example, the `typeof' function never remains in
symbolic form; entering `typeof(a)' will put the number 100 (the type
code for variables like `a') on the stack. But putting the condition
`... :: typeof(a) = 6' on the stack is safe since `::' prevents the
`typeof' from being evaluated until the condition is actually used by
the rewrite system.
Since `::' protects its lefthand side, too, you can use a dummy
condition to protect a rule that must itself not evaluate early. For
example, it's not safe to put `a(f,x) := apply(f, [x])' on the stack
because it will immediately evaluate to `a(f,x) := f(x)', where the
meta-variable-ness of `f' on the righthand side has been lost. But
`a(f,x) := apply(f, [x]) :: 1' is safe, and of course the condition `1'
is always true (nonzero) so it has no effect on the functioning of the
rule. (The rewrite compiler will ensure that it doesn't even impact
the speed of matching the rule.)
File: calc.info, Node: Algebraic Properties of Rewrite Rules, Next: Other Features of Rewrite Rules, Prev: Conditional Rewrite Rules, Up: Rewrite Rules
Algebraic Properties of Rewrite Rules
-------------------------------------
The rewrite mechanism understands the algebraic properties of functions
like `+' and `*'. In particular, pattern matching takes the
associativity and commutativity of the following functions into account:
+ - * = != && || and or xor vint vunion vxor gcd lcm max min beta
For example, the rewrite rule:
a x + b x := (a + b) x
will match formulas of the form,
a x + b x, x a + x b, a x + x b, x a + b x
Rewrites also understand the relationship between the `+' and `-'
operators. The above rewrite rule will also match the formulas,
a x - b x, x a - x b, a x - x b, x a - b x
by matching `b' in the pattern to `-b' from the formula.
Applied to a sum of many terms like `r + a x + s + b x + t', this
pattern will check all pairs of terms for possible matches. The rewrite
will take whichever suitable pair it discovers first.
In general, a pattern using an associative operator like `a + b'
will try 2 n different ways to match a sum of n terms like `x + y + z -
w'. First, `a' is matched against each of `x', `y', `z', and `-w' in
turn, with `b' being matched to the remainders `y + z - w', `x + z -
w', etc. If none of these succeed, then `b' is matched against each of
the four terms with `a' matching the remainder. Half-and-half matches,
like `(x + y) + (z - w)', are not tried.
Note that `*' is not commutative when applied to matrices, but
rewrite rules pretend that it is. If you type `m v' to enable matrix
mode (*note Matrix Mode::.), rewrite rules will match `*' literally,
ignoring its usual commutativity property. (In the current
implementation, the associativity also vanishes--it is as if the
pattern had been enclosed in a `plain' marker; see below.) If you are
applying rewrites to formulas with matrices, it's best to enable matrix
mode first to prevent algebraically incorrect rewrites from occurring.
The pattern `-x' will actually match any expression. For example,
the rule
f(-x) := -f(x)
will rewrite `f(a)' to `-f(-a)'. To avoid this, either use a `plain'
marker as described below, or add a `negative(x)' condition. The
`negative' function is true if its argument "looks" negative, for
example, because it is a negative number or because it is a formula
like `-x'. The new rule using this condition is:
f(x) := -f(-x) :: negative(x) or, equivalently,
f(-x) := -f(x) :: negative(-x)
In the same way, the pattern `x - y' will match the sum `a + b' by
matching `y' to `-b'.
The pattern `a b' will also match the fo