RE: TableForm and NumberForm

*To*: mathgroup at smc.vnet.net*Subject*: [mg34746] RE: TableForm and NumberForm*From*: "DrBob" <majort at cox-internet.com>*Date*: Tue, 4 Jun 2002 03:42:05 -0400 (EDT)*Reply-to*: <drbob at bigfoot.com>*Sender*: owner-wri-mathgroup at wolfram.com

Here's another manifestation of the problem (notice a semicolon in the fifth line but not the second): NumberForm[tb2, {12, 11}]; % % // FullForm NumberForm[tb2, {12, 11}]; %; % // FullForm It is display of NumberForm objects (not the use of TableForm) that sometimes removes the NumberForm head. It only removes an outermost NumberForm head, not NumberForm heads embedded within another Head. Isn't that the pattern we're seeing? In a construct like TableForm[NumberForm...]...] we want to remove NumberForm heads that enclose lists but not when they enclose numbers. And, we want to propagate those NumberForm heads downward TO the numbers rather than simply removing them, so we have this option: numRule := NumberForm[a_List, b_] :> (NumberForm[#, b] & /@ a) TableForm[NumberForm[tb2, {12, 11}] //. numRule] This is no easier than mapping NumberForm onto individual array elements (that IS what we're doing), but it postpones that to the last moment, in case that's useful. However, it would be tedious to format different columns differently with a variation of this. For that, I gave a better solution earlier: formRule := {a_Real, b_Real, c_Real, d_Real} :> { NumberForm[a, {15, 14}], NumberForm[b, {3, 2}], ScientificForm[c, {3, 2}], AccountingForm[d, 8]} TableForm[tb2 /. formRule, TableHeadings -> {None, {"a", "b", "c", "d"}}] Use of numRule is simpler if all numbers in the table need the same formatting. In more complicated settings, both techniques could be used at once. The key (am I right?) is that TableForm doesn't remove NumberForm heads at all, and displaying an object only removes the outermost NumberForm head. Bobby Treat -----Original Message----- From: David Park [mailto:djmp at earthlink.net] To: mathgroup at smc.vnet.net Subject: [mg34746] TableForm and NumberForm I've renamed this thread because it is a separate subject, probably interesting to a number of people. What is the interaction between NumberForm and TableForm? It seems like I've butted my head against this problem a number of times. In the following, NumberForm produces an array with the desired number format. If you look at FullForm, the whole array is wrapped in NumberForm. But when the TableForm statement is evaluated, the NumberForm is thrown away. It is as if TableForm was Applied. Or else NumberForm is stripped away. We revert to the default 6 place display. tbl = Table[Random[], {3}, {4}]; NumberForm[tbl, {12, 11}] TableForm[%] In the following NumberForm is mapped onto the individual array elements. The array displays with the desired number format. Now, when we use TableForm, the number format is preserved and we obtain the desired display. So, why is NumberForm stripped away in the first case and not in this case. tb2 = Table[Random[], {3}, {4}]; Map[NumberForm[#, {12, 11}] &, %, {-1}] TableForm[%] If we map NumberForm onto each row in the array, then TableForm acts as if it had a list of three elements, each element with the Head NumberForm. This displays as a column of three lists. tb3 = Table[Random[], {3}, {4}]; Map[NumberForm[#, {12, 11}] &, %, {1}] TableForm[%] So why didn't the first case act as a TableForm of a single element? If we rewrite the first case this way, it does. We obtain a single element in our table, namely the entire array wrapped in NumberForm. tb4 = Table[Random[], {3}, {4}]; TableForm[NumberForm[tb4, {12, 11}]] In any case, it appears that when using TableForm and NumberForm, the surest procedure is to map NumberForm onto the individual array elements. David Park djmp at earthlink.net http://home.earthlink.net/~djmp/ > From: DrBob [mailto:majort at cox-internet.com] To: mathgroup at smc.vnet.net > > Bob, > > Thanks! My first attempt was evidently too simplistic. But look at the > difference between these two output forms: > > tbl = Table[Random[], {3}, {4}] > NumberForm[TableForm[tbl], {12, 11}] > TableForm[NumberForm[tbl, {12, 11}]] > > Is that what you'd expect? (I'm seeing brackets, etc. displayed in the > second version.) > > Bobby > > From: BobHanlon at aol.com [mailto:BobHanlon at aol.com] To: mathgroup at smc.vnet.net > > > In a message dated 6/3/02 3:12:04 AM, majort at cox-internet.com writes: > > >ODDLY ENOUGH, when I tried to use NumberForm within TableForm, the > >result didn't display as a table, so I don't know how to show different > >columns of the table at different display precisions. > > > >QUESTION: Does anybody know how to do that? > > TableForm[ > Map[ > NumberForm[#, {4, 3}, > NumberPadding->{"", "0"}]&, > > Table[Random[], {3}, {4}], {2}], > > TableHeadings->{Automatic, Automatic}] > > TableForm[ > Table[ > NumberForm[ > Random[], {5, n}, > NumberPadding->{"", "0"}], > > {3}, {n, 4}], > TableHeadings->{Automatic, Automatic}] > > > Bob Hanlon > Chantilly, VA USA > >