Objects - Maple Help

Objects in Maple 16

With the addition of objects to the language, Maple users can write code that integrates much more closely with Maple.  For a general overview of objects see the object help page.  For details on declaring new objects, see the object,create page.  Objects can implement methods (see object,methods), overload built-ins (see object,builtins) and various operators (see object,operators)  that will be invoked when those objects appear in standard Maple syntax.  This allows user defined objects to integrate into Maple as naturally as built-in Maple types.  In the following example, we implement an object that represents an integer modulo n.



 >
 ${\mathrm{i0m5}}{:=}{\mathrm{0 mod 5}}$ (1)

Notice the nicely printed form.

We can also create a new object by copying an existing one.   In this case we will specify a new value, but the base will be copied from the given object.

 >
 ${\mathrm{i1m5}}{:=}{\mathrm{1 mod 5}}$ (2)

We can test that these newly create objects are instances of the IntMod class

 >
 ${\mathrm{true}}$ (3)

The ModuleType procedure allows the IntMod type check to support specifying a base.

 >
 ${\mathrm{false}}$ (4)
 >
 ${\mathrm{true}}$ (5)

ModInt implements a '+' operator.  We can use this to generate the rest of the integers mod 5

 >
 ${\mathrm{i2m5}}{:=}{\mathrm{2 mod 5}}$ (6)
 >
 ${\mathrm{i3m5}}{:=}{\mathrm{3 mod 5}}$ (7)
 >
 ${\mathrm{i4m5}}{:=}{\mathrm{4 mod 5}}$ (8)
 >
 ${\mathrm{0 mod 5}}$ (9)
 > $\mathrm{i3m5}+\mathrm{i4m5};$
 ${\mathrm{2 mod 5}}$ (10)

The implementation of + also supports non-integer values, by leaving them alone.

 >
 ${\mathrm{3 mod 5}}{+}{f}\left({x}\right){+}{y}$ (11)



IntMod overloads the * and ^ operators.

 >
 ${\mathrm{2 mod 5}}{}{y}{}{f}\left({x}\right)$ (12)

Note that the ^ does not get called in the 0 case, automatic simplification gets in the way.

 > $\mathrm{i2m5}^0;$
 ${1}$ (13)
 > $\mathrm{i2m5}^1;$
 ${\mathrm{2 mod 5}}$ (14)
 > $\mathrm{i2m5}^2;$
 ${\mathrm{4 mod 5}}$ (15)
 > $\mathrm{i2m5}^3;$
 ${\mathrm{3 mod 5}}$ (16)
 > $\mathrm{i2m5}^4;$
 ${\mathrm{1 mod 5}}$ (17)
 > $\mathrm{i2m5}^5;$
 ${\mathrm{2 mod 5}}$ (18)

The ModInt object also overloads the convert function.  This allows the conversion of IntMods to integers.

 >
 ${3}$ (19)