custom simplification, was: polarform returns a rectangular expression for float argumen



On 1/19/06, Barton Willis <willisb at unk.edu> wrote:

> I'd like for the function simplus, for example, to look
> at the property list of non-atom addends to see if there
> was a specific function for adding such objects. If there
> was such a function, simplus would ship the addition
> off to that function. That way simplus could be extended
> without hacking the simplus code. That's all. I don't think
> simplus, simptimes, and ... allow this.

well, the existing tellsimp / tellsimpafter code does something
pretty much like this, except it associates rules with operators
instead of operands.

> (%i3) tellsimp(x * interval(a,b), interval(min(x*a,x*b), max(x*a,x*b)))$

it turns out that tellsimp and tellsimpafter construct different
functions for identical rules -- they don't differ just in putting
new rules before or after the built-in rules. maybe there's
a reason for that.

i like tellsimpafter's approach to + and * -- given a pattern stated
as a two-operand addition, it sweeps up multiple operands which
satisfy the same match predicates as each of the two operands.

so i would get started on interval arithmetic like this:

matchdeclare (ii, intervalp, nn, not_intervalp);
intervalp (e) := not atom (e) and op (e) = 'interval;
not_intervalp (e) := not intervalp (e);

tellsimpafter (ii + nn, FOO (ii, nn));

a + 1;  => a + 1
a + interval (3, 7);  =>  FOO(interval(3, 7), a)
a + interval (3, 7) + b + interval (9, 12) + c;  =>  FOO(interval(9,
12) + interval(3, 7), c + b + a)
interval (3, 7) + interval (9, 12);  =>  FOO(interval(9, 12) +
interval(3, 7), 0)

at this point it's easy to write FOO, since it is known the first
argument is a sum of intervals and the second is a sum of
stuff that isn't intervals. FOO presumably constructs and
returns a new interval.

rules for other operators can be defined similarly.
only rules for + and * make use of the sweep-up matching,
although it makes sense for any n-ary commutative operator.
a slightly burdensome aspect of the rule definition stuff is that
a rule has to be declared for each operator, so a single rule
can't cover all the built-in trig functions, for example.

maybe the notation could be less obscure -- i'd like to see
ii + nn --> FOO (ii, nn) assuming intervalp (ii), not_intervalp (nn);
but that's easy to arrange with macros.

here is a link to the reference manual,
http://maxima.sourceforge.net/docs/manual/en/maxima_38.html
and some additional verbiage.
http://maxima.sourceforge.net/wiki/index.php/User-defined%20simplification
http://maxima.sourceforge.net/wiki/index.php/tellsimpafter%20worked%20example%20%28base%2060%20arithmetic%29

sorry to run on so long -- hth,

robert