Macaulay2 » Documentation
Packages » Macaulay2Doc > The Macaulay2 language > two dimensional formatting
next | previous | forward | backward | up | index | toc

two dimensional formatting

We have seen that nets (see Net) are potentially useful for two dimensional formatting of output to an ascii terminal with limited graphical ability. We present now a few more hints about putting this idea into practice. Nets are used extensively in Macaulay2 for formatting, for example, for formatting of polynomials and matrices.
i1 : R = ZZ/101[x,y,z];
i2 : f = random(R^1,R^{5:-3})

o2 = | 24x3-36x2y-29xy2-10y3-30x2z+19xyz-29y2z+19xz2-8yz2-22z3
     ------------------------------------------------------------------------
     -29x3-24x2y-16xy2+34y3-38x2z+39xyz+19y2z+21xz2-47yz2-39z3
     ------------------------------------------------------------------------
     -18x3-13x2y-15xy2+38y3-43x2z-28xyz+2y2z-47xz2+16yz2+22z3
     ------------------------------------------------------------------------
     45x3-34x2y-47xy2-16y3-48x2z+47xyz+7y2z+19xz2+15yz2-23z3
     ------------------------------------------------------------------------
     39x3+43x2y-11xy2+35y3-17x2z+48xyz+11y2z+36xz2-38yz2+33z3 |

             1      5
o2 : Matrix R  <-- R
Output of routines such as betti and net that return nets can be easily incorporated into more complex displays using standard operations on nets (see Net).
i3 : C = resolution cokernel f

      1      5      9      5
o3 = R  <-- R  <-- R  <-- R  <-- 0
                                  
     0      1      2      3      4

o3 : ChainComplex
i4 : be = betti C

            0 1 2 3
o4 = total: 1 5 9 5
         0: 1 . . .
         1: . . . .
         2: . 5 . .
         3: . . 9 5

o4 : BettiTally
i5 : "Betti numbers of " | net C | " are " | (net be)^2

                                                               0 1 2 3
                                                        total: 1 5 9 5
                       1      5      9      5               0: 1 . . .
o5 = Betti numbers of R  <-- R  <-- R  <-- R  <-- 0 are     1: . . . .
                                                            2: . 5 . .
                      0      1      2      3      4         3: . . 9 5
You could even learn how to display algebraic expressions with nets.
i6 : "x" | "2"^1

      2
o6 = x
There is an easier way to display algebraic expressions, using a type of thing called an Expression. It allows you to set up things that print out as powers, sums, products, matrices, and so on. There are various types of expression, such as Power, Sum, Divide, Minus, and Product that we can use for this.
i7 : Divide(Minus a,b)

     -a
o7 = --
      b

o7 : Expression of class Divide
i8 : Power(Sum(3,4,5),7)

                7
o8 = (3 + 4 + 5)

o8 : Expression of class Power
i9 : Sum(1,2, Minus 3, 4,5)

o9 = 1 + 2 - 3 + 4 + 5

o9 : Expression of class Sum
Actually, the formation of such expressions is contagious, in the sense that the basic algebraic operations will construct expressions for you if one of their two operands is already an expression.
i10 : Minus a / b

      -a
o10 = --
       b

o10 : Expression of class Divide
i11 : (Sum(3,4,5))^7

                 7
o11 = (3 + 4 + 5)

o11 : Expression of class Power
i12 : 1 + 2 + Minus 3 + 4 + 5

o12 = 3 - 3 + 4 + 5

o12 : Expression of class Sum
In the last example above, 1 + 2 was evaluated first, so it yielded 3 but after that the contagion set in.

The function expression can be used to prepare things such as polynomials for formatting using the mechanism introduced above.
i13 : g = (x+y)^2

       2           2
o13 = x  + 2x*y + y

o13 : R
i14 : e = expression g

       2           2
o14 = x  + 2x*y + y

o14 : Expression of class Sum
i15 : peek e

           2         2
o15 = Sum{x , 2x*y, y }
In the example above, we see that peek extracts only one level of the structure. We may use peek' to display the structure of e to depth 2.
i16 : peek'(2,e)

o16 = Sum{Power{x, 2}, Product{2, x, y}, Power{y, 2}}
Other types of Expression that can be used for formatting nested lists as two dimensional arrays are MatrixExpression and Table.
i17 : Table{{1,2,3},{a,bb,ccc}}

o17 = 1   2   3 
             
      a  bb  ccc

o17 : Expression of class Table
i18 : MatrixExpression{{1,2,3},{a,bb,ccc}}

o18 = | 1 2  3   |
      | a bb ccc |

o18 : Expression of class MatrixExpression
i19 : Table{{"Example 1","Example 2"},
            {Table{{1,2},{3,4}},Table{{11,22},{3,444}}}}

o19 = Example 1  Example 2
                 
         1  2     11   22 
                      
         3  4      3  444

o19 : Expression of class Table