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}) |
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 |
i4 : be = betti C |
i5 : "Betti numbers of " | net C | " are " | be^2 |
You could even learn how to display algebraic expressions with nets.
i6 : "x" | "2"^1 |
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) |
i8 : Power(Sum(3,4,5),7) |
i9 : Sum(1,2, Minus 3, 4,5) |
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 |
i11 : (Sum(3,4,5))^7 |
i12 : 1 + 2 + Minus 3 + 4 + 5 |
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 |
i14 : e = expression g |
i15 : peek e |
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) |
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}} |
i18 : MatrixExpression{{1,2,3},{a,bb,ccc}} |
i19 : Table{{"Example 1","Example 2"}, |