next | previous | forward | backward | up | top | index | toc | Macaulay2 web site
Macaulay2Doc > The Macaulay2 language > printing and formatting for new classes

printing and formatting for new classes

After making a new type, it's desirable to install methods for displaying the instances of the new type in various formats.
i1 : Qu = new Type of List

o1 = Qu

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

o2 = {1, -2, 0, 4}

o2 : Qu
For example, it's desirable to display the quaternion above so it looks like a quaternion. One way to achieve this is to install first a method for creating an Expression from a quaternion, since there are methods already installed for converting expressions to common forms of output, such as to nets, which are used most commonly.
i3 : expression Qu := z -> (
                    expression z#0 +
                    expression z#1 * expression "I" +
                    expression z#2 * expression "J" +
                    expression z#3 * expression "K");
i4 : net Qu := z -> net expression z;
i5 : toString Qu := z -> toString expression z;
i6 : tex Qu := z -> tex expression z;
i7 : html Qu := z -> html expression z;
i8 : w

o8 = 1 - 2*I + 4*K

o8 : Qu
i9 : toString w

o9 = 1-2*I+4*K
i10 : tex w

o10 = $1-2\,\texttt{I%
      }+4\,\texttt{K%
      }$
i11 : html w

o11 = 1-2I+4K
Of course, now that we've decided that there should be certain quaternions called I, J, and K, perhaps we should install them, too.
i12 : I = new Qu from {0,1,0,0}

o12 = I

o12 : Qu
i13 : J = new Qu from {0,0,1,0}

o13 = J

o13 : Qu
i14 : K = new Qu from {0,0,0,1}

o14 = K

o14 : Qu
i15 : 2*I + 5*J

o15 = 2*I + 5*J

o15 : Qu
i16 : peek oo

o16 = {0, 2, 5, 0}