Re: Two Questions
- To: mathgroup at smc.vnet.net
- Subject: [mg102919] Re: [mg102883] Two Questions
- From: "David Park" <djmpark at comcast.net>
- Date: Wed, 2 Sep 2009 04:03:10 -0400 (EDT)
- References: <5282041.1251792077869.JavaMail.root@n11>
Chris, I usually can't resist Table questions because I'm so much in need of practice with them. I think that the best approach is to use Grid instead of TableForm. Grid gives you much more control but also requires more learning. Let's say we want to make our table out of sub-parts. In this case this is the table proper, the x and y headings and a little something in the upper left hand corner. I think there are two approaches to this. 1) Consider the entire Grid as one table and write a function to fill in the elements. 2) Write separate Grids for each of the sub-parts and then assemble them. Here is the first approach: f[x_, y_] := (2 x^2)/y^2 g[x_, y_] = Piecewise[{{"x\y", x == y == 0}, {y, x == 0}, {x, y == 0}}, f[x, y]] Then calculate all of the elements, Notice I used 0 for the heading row and column and positive integers for the table proper. elements = Table[g[x, y], {x, {0, Sequence @@ Range[5, 20]}}, {y, {0, Sequence @@ Range[3, 10]}}]; Then put everything together in a Labeled Grid with Frame, Dividers and Background for the various sub-parts. The options for Grid are so complicated and they do most of the work so it is nice to calculate the elements outside of the Grid statement just to simplify things. The most fiddly thing is the Background, which has a lot of parentheses in it. We use -1 to designate the last position in a range. Labeled[ Grid[elements, Frame -> True, Dividers -> {{2 -> Black}, {2 -> Black}}, Background -> {None, None, { {1, 1} -> Pink, {{1, 1}, {2, -1}} -> GrayLevel[.9], {{2, -1}, {1, 1}} -> GrayLevel[.9]} } ], Style["A Custom Table", FontFamily -> "Helvetica"]] For the second method we calculate sub-grids for each of the pieces of the table. If we want everything to fit together properly we have to specify the ItemSizes for the entries. If we don't make the ItemSizes large enough the Grid statement will give us mismatches. We have to specify the Background coloring in the outer Grid. With[ {xmin = 5, xmax = 20, ymin = 3, ymax = 10, width1 = 1.7, width2 = 1.5, height1 = 1.5, height2 = 2.1}, grid[1, 1] = Grid[{{"x\y"}}, ItemSize -> {width1, height1}]; grid[1, 2] = Grid[{Table[y, {y, ymin, ymax}]}, ItemSize -> {width2, height1}]; grid[2, 1] = Grid[Transpose[{Table[x, {x, xmin, xmax}]}], ItemSize -> {width1, height2}]; grid[2, 2] = Grid[Table[f[x, y], {x, xmin, xmax}, {y, ymin, ymax}], ItemSize -> {width2, height2}]; Labeled[ Grid[ {{grid[1, 1], grid[1, 2]}, {grid[2, 1], grid[2, 2]}}, Frame -> True, Dividers -> {{2 -> Black}, {2 -> Black}}, Background -> {None, None, {{1, 1} -> Pink, {1, 2} -> GrayLevel[.9], {2, 1} -> GrayLevel[.9] } }](* Grid *), Style["A Custom Table", FontFamily -> "Helvetica"]](* Labeled *) ] David Park djmpark at comcast.net http://home.comcast.net/~djmpark/ From: cmoller [mailto:cmoller at dpbioventures.com] Hi, If I create a Table from this function f[x_, y_] := (2 x^2)/y^2 using Table[f[x,y],{x,5,20},(y,3,10}] can I add a statement that will adjust the table headings to conform with the selected ranges for x and y? I have tried TableForm with TableHeadings -> etc, but I must specify the exact headings. Can I use a formula with TableHeadings? To be direct, in the case above I want the labels to read 5,6..20 and 3,4..10. My second question is can I call Mathematica routines with Python? All the best.. and thanks, Chris