[next][previous][up][top][index]
search for:

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 and matrices.

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

o2 = | 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

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

o4 = total: 1 5 9 5
         0: 1 . . .
         1: . . . .
         2: . 5 . .
         3: . . 9 5
i5 : "Betti numbers of " | net C | " are " | be^2

                                                        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 : Divide
i8 : Power(Sum(3,4,5),7)

                7
o8 = (3 + 4 + 5)

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

o9 = 1 + 2 - 3 + 4 + 5

o9 : 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 : Divide
i11 : (Sum(3,4,5))^7

                 7
o11 = (3 + 4 + 5)

o11 : Power
i12 : 1 + 2 + Minus 3 + 4 + 5

o12 = 3 - 3 + 4 + 5

o12 : 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 : 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 peek2 to display the structure of e to depth 2.

i16 : peek2(e,2)

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 : Table
i18 : MatrixExpression{{1,2,3},{a,bb,ccc}}

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

o18 : 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 : Table


[next][previous][up][top][index]
search for: