two dimensional formatting

We have seen that nets are potentially useful for two dimesional 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 Macaulay 2 for formatting, for example, for formatting of polynomials.

i1 : R = ZZ/101[x,y,z];
i2 : f = random(R^1,R^{5:-3})

o2 = {0} | 42x3-50x2y+9xy2+50y3+39x2z-15xyz+45y2z-22xz2-29yz2-39z3 30x3+19x2y+2xy2-16y3-38x2z-4xyz-6y2z-36xz2-32yz2+31z3 -32x3-38x2y+24xy2-41y3+31x2z-42xyz+15y2z-50xz2+17yz2-28z3 37x3-22x2y+45xy2-9y3-19x2z-8xyz+32y2z-31xz2-4yz2+4z3 -2x3+24x2y-45xy2-10y3+x2z+15xyz-2y2z-15xz2+21z3 |

             1       5
o2 : Matrix R  <--- R

Normally matrices such as the one above are displayed in compact notation that originated with Macaulay. Setting the global flag compactMatrixForm to false will modify that behavior.

i3 : compactMatrixForm = false

o3 = false
i4 : f

     |    3      2        2      3      2                 2         2        2      3     3      2        2      3      2               2         2        2      3       3      2         2      3      2                 2         2        2      3     3      2         2     3      2                2         2       2     3      3      2         2      3    2                2         2      3 |
o4 = | 42x  - 50x y + 9x*y  + 50y  + 39x z - 15x*y*z + 45y z - 22x*z  - 29y*z  - 39z   30x  + 19x y + 2x*y  - 16y  - 38x z - 4x*y*z - 6y z - 36x*z  - 32y*z  + 31z   - 32x  - 38x y + 24x*y  - 41y  + 31x z - 42x*y*z + 15y z - 50x*z  + 17y*z  - 28z   37x  - 22x y + 45x*y  - 9y  - 19x z - 8x*y*z + 32y z - 31x*z  - 4y*z  + 4z   - 2x  + 24x y - 45x*y  - 10y  + x z + 15x*y*z - 2y z - 15x*z  + 21z  |

             1       5
o4 : 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).

i5 : C = resolution cokernel f

      1      5      9      5
o5 = R  <-- R  <-- R  <-- R
                          
     0      1      2      3

o5 : ChainComplex
i6 : be = betti C

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

o6 : Net
i7 : "Betti numbers of " | net C | " are " | be^2

                                                  total: 1 5 9 5
                       1      5      9      5         0: 1 . . .
o7 = Betti numbers of R  <-- R  <-- R  <-- R  are     1: . . . .
                                                      2: . 5 . .
                      0      1      2      3          3: . . 9 5

o7 : Net

You could even learn how to display algebraic expressions with nets.

i8 : "x" | "2"^1

      2
o8 = x

o8 : Net

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 which we can use for this.

i9 : Divide(Minus a,b)

     -a
o9 = --
      b

o9 : Divide
i10 : Power(Sum(3,4,5),7)

                 7
o10 = (3 + 4 + 5)

o10 : Power
i11 : Sum(1,2, Minus 3, 4,5)

o11 = 1 + 2 - 3 + 4 + 5

o11 : 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.

i12 : Minus a / b

      -a
o12 = --
       b

o12 : Divide
i13 : (Sum(3,4,5))^7

                 7
o13 = (3 + 4 + 5)

o13 : Power
i14 : 1 + 2 + Minus 3 + 4 + 5

o14 = 3 - 3 + 4 + 5

o14 : 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.

i15 : g = (x+y)^2

       2           2
o15 = x  + 2x*y + y 

o15 : R
i16 : e = expression g

       2           2
o16 = x  + 2x*y + y 

o16 : Sum
i17 : peek e

           2       2
o17 = Sum{x ,2x*y,y }

o17 : Net

In the example above, we see that peek extracts only one level of the structure. We may use peek2 to display the structure of e to depth 2.

i18 : peek2(e,2)

o18 = Sum{Power{x,2},Product{2,x,y},Power{y,2}}

o18 : Net

Other types of Expression which can be used for formatting nested lists as two dimensional arrays are MatrixExpression and Table.

i19 : Table{{1,2,3},{a,bb,ccc}}

o19 =  1   2   3  
      
       a  bb  ccc 

o19 : Table
i20 : MatrixExpression{{1,2,3},{a,bb,ccc}}

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

o20 : MatrixExpression
i21 : Table{{"Example 1","Example 2"},
            {Table{{1,2},{3,4}},Table{{11,22},{3,444}}}}

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

o21 : Table


topindexpreviousupnext