Objects - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

Online Help

All Products    Maple    MapleSim


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.

 

Create a new instance of the IntMod class using the IntMod object factory.

i0m5  IntMod 0, 5 ;

i0m5:=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.

i1m5  Object i0m5, 1 ;

i1m5:=1 mod 5

(2)

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

type i1m5, 'IntMod' ;

true

(3)

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

type i1m5, 'IntMod'3 ;

false

(4)

type i1m5, 'IntMod'5 ;

true

(5)

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

i2m5  i1m5 + 1;

i2m5:=2 mod 5

(6)

i3m5  i2m5 + 1;

i3m5:=3 mod 5

(7)

i4m5  i3m5 + 1;

i4m5:=4 mod 5

(8)

i4m5 + 1;

0 mod 5

(9)

i3m5+i4m5;

2 mod 5

(10)

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

4 + i1m5 + 18 + i2m5 + 3 + fx + y;

3 mod 5+fx+y

(11)

IntMod overloads the * and ^ operators.

i3m5 * i4m5 * y * fx;

2 mod 5yfx

(12)

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

i2m5^0;

1

(13)

i2m5^1;

2 mod 5

(14)

i2m5^2;

4 mod 5

(15)

i2m5^3;

3 mod 5

(16)

i2m5^4;

1 mod 5

(17)

i2m5^5;

2 mod 5

(18)

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

convert i3m5, 'integer' ;

3

(19)