modulus



> I would like to define an operator like this:
> infix(§);
> a§b:=rat(a),modulus:b;
> 
> and then make for instance
> 489^2281§3293;
> 
> - but maxima won't accept it. How can I do it correctly?
> I wonder why this operator is not a standard built-in 
> operator in maxima?

This is a more complicated question than you might think.  There is in
fact a built-in function mod(a,b) which gives mod(5^7,3) => -1.
However, since it is a normal function, it evaluates its arguments
before calculating the modulus -- it does not do the calculation itself
in a modular context.  So the kind of large modular exponentiations
needed for cryptography, e.g. mod(234234234^234234,3), fail.

The reason rat(234234^234234),modulus:3 works is that the "," syntax
sets up a local evaluation environment.  This "," syntax is only
available on the (Cn) command lines -- in other contexts, use
ev(...,...) -- that is why your definition didn't work.  To set up a
local evaluation environment with your own function requires not a
normal function, but a macro.

Here is an appropriate macro definition:

 a § b ::= BUILDQ([x, y], ev(rat(x), modulus : y))

What a macro does is to return an expression to evaluate in the place of
the original expression.  So 5^7 § 3 is rewritten as ev(rat(5^7),
modulus : 3).

Examples:
 
    5 § 5 => 0   OK
    ((x+1)^3) § 2 => x^3+x^2+x+1  OK
    x § 5 => x   ... note that unlike normal arithmetic operators,
                     the symbolic result does *not* mean the same
                     as the original expression

You will probably also want to adjust the operator precedence so that
489 ^ 2281 § 3293 parses as (489 ^ 2281) § 3293 and not as 489 ^(2281 §
3293), e.g.

    infix(§,90)$

Hope this helps.

     -s