Next: Optional Arguments and Up: Maple Procedures Previous: Returning Unevaluated

Simplifications and Transformation Rules

Often one wants to introduce simplifications which can be described algebraically or by transformation rules. For instance, given a function , we may know that is both commutative and associative. max is in fact such a function, i.e. it is true that and . How can we implement these properties in Maple? What we want is a canonical way for writing expressions involving the max function. We can implement commutativity by sorting the arguments. For associativity we can unnest any nested max calls. I.e. we would transform both and into . Actually this also implements , i.e. max is idempotent. Here is a MAX function to do this


MAX := proc() local a;
    a := [args];
    a := map( flatten, a, MAX ); # unnest nested MAX calls
    'MAX'( op(sort(a)) );
end;
flatten := proc(x,f)
    if type(x,function) and op(0,x) = f then op(x) else x fi
end;

For example


> MAX(a,MAX(c,b),a);

                                MAX(a, a, b, c)

We see that we should also recognize the property that . To do this, instead of putting the arguments in a list, we will put them in a set so that duplicates are removed. Also, since sets are sorted automatically, we can remove the call to sort. Hence we have


MAX := proc() local a;
    a := {args};
    a := map( flatten, a, MAX );
    'MAX'( op(a) );
end;


> MAX(a,MAX(c,b),a);

                                  MAX(a, b, c)

The reader may be a little puzzled as to just what our MAX procedure is doing. We have seen earlier that if we assign a positive integer to the printlevel variable, we get a trace of all statements executed. However, often the output from this simple tracing facility is too much. In this case, we would also get the output from the flatten procedure. The trace function can be used instead to selectively trace procedures. Let's use it to trace the MAX procedure


> trace(MAX);

                                      MAX

> MAX(a,MAX(b,a),c);
--> enter MAX, args = b, a

                                  a := {a, b}

                                  a := {a, b}

                                   MAX(a, b)

<-- exit MAX = MAX(a,b)
--> enter MAX, args = a, MAX(a,b), c

                             a := {a, c, MAX(a, b)}

                                 a := {a, c, b}

                                  MAX(a, c, b)

<-- exit MAX = MAX(a,c,b)

                                  MAX(a, c, b)


bondaren@thsun1.jinr.ru