next | previous | forward | backward | up | top | index | toc | Macaulay2 web site
Macaulay2Doc > The Macaulay2 language > making new classes

making new classes

All new classes are made with the operator new. You may choose to implement the instances of your new class either as basic lists or as hash tables, or you may even base it on a subclass of BasicList or a subclass of HashTable, if you find a class that has some of the methods you need already implemented.

As an example, we may wish to implement quaternions as lists of four real numbers. We know that lists already have a method for addition that treats them as vectors, and we could use the same code for addition of quaternions.

i1 : Qu = new Type of List

o1 = Qu

o1 : Type
i2 : w = new Qu from {1,2,3,4}

o2 = {1, 2, 3, 4}

o2 : Qu
i3 : w+w

o3 = {2, 4, 6, 8}

o3 : Qu
Now all we have to do is to install a method for multiplying quaternions.
i4 : Qu * Qu := (x,y) -> new Qu from { 
               x#0*y#0 - x#1*y#1 - x#2*y#2 - x#3*y#3,
               x#0*y#1 + x#1*y#0 + x#2*y#3 - x#3*y#2,
               x#0*y#2 + x#2*y#0 + x#3*y#1 - x#1*y#3,
               x#0*y#3 + x#3*y#0 + x#1*y#2 - x#2*y#1
               };
i5 : w*w

o5 = {-28, 4, 6, 8}

o5 : Qu