MathGroup Archive 2002

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: TableForm and NumberForm (2)

  • To: mathgroup at smc.vnet.net
  • Subject: [mg34766] Re: TableForm and NumberForm (2)
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Wed, 5 Jun 2002 03:38:27 -0400 (EDT)
  • References: <adhs5i$4gt$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Prompted by Bobby Treat I have tried to make my previous posting a bit more
reproducible and readable (and I have corrected a couple or typing errors).
In particular I have included te line $Line=0 that sets the input/output
numbering so that the next input/output number will be 1.

Also, I post this as a notebook expression.
To read it copy the text between (*Start*) and (*End*) and paste into a
Mathematica notebook;  click the "Yes" button in the panel that pops up.


(* Start*)
Notebook[{
Cell["\<\
Some examples supporting Bobby's explanation that the outer wrapper \
is used
in displaying the output but is not included in the output.
Showing the In and Out labels helps.\
\>", "Text"],

Cell[CellGroupData[{

Cell[BoxData[
    \($Line = 0\)], "Input"],

Cell[BoxData[
    \(0\)], "Output"]
}, Open  ]],

Cell[CellGroupData[{

Cell[BoxData[
    \(e1 = {1.23456789}\)], "Input"],

Cell[BoxData[
    \({1.23456789`}\)], "Output"]
}, Open  ]],

Cell[CellGroupData[{

Cell[BoxData[
    \(e2 = NumberForm[e1, {12, 2}]\)], "Input"],

Cell[BoxData[
    TagBox[
      RowBox[{"{",
        InterpretationBox["\<\"1.23\"\>",
          1.2345678899999999,
          AutoDelete->True], "}"}],
      (NumberForm[ #, {12, 2}]&)]], "Output"]
}, Open  ]],

Cell["\<\
(1) The above means what it says: it is Out[2] displayed using \
NumberForm:
Notice that the NumbeForm is not included in Out[2].\
\>", "Text"],

Cell[CellGroupData[{

Cell[BoxData[
    \(Out[2]\)], "Input"],

Cell[OutputFormData["\<\
{1.23456789}\
\>", "\<\
{1.23457}\
\>"], "Output"]
}, Open  ]],

Cell[CellGroupData[{

Cell[BoxData[
    \(Out[2] // NumberForm[#, {12, 2}] &\)], "Input"],

Cell[OutputFormData["\<\
{1.23456789}\
\>", "\<\
{1.23}\
\>"], "Output"]
}, Open  ]],

Cell["\<\
(2) But NumberForm is is included in e2:\
\>", "Text"],

Cell[CellGroupData[{

Cell[BoxData[
    \(e2\)], "Input"],

Cell[OutputFormData["\<\
{1.23456789}\
\>", "\<\
{1.23}\
\>"], "Output"]
}, Open  ]],

Cell["\<\
(3) Wrappers other those round the whole expression are not \
removed:\
\>", "Text"],

Cell[CellGroupData[{

Cell[BoxData[
    \(e3 = \(NumberForm[#, {12, 2}] &\) /@ e1\)], "Input"],

Cell[OutputFormData["\<\
{NumberForm[1.23456789, {12, 2}]}\
\>", "\<\
{1.23}\
\>"], "Output"]
}, Open  ]],

Cell[CellGroupData[{

Cell[BoxData[
    \(% // InputForm\)], "Input"],

Cell["{NumberForm[1.23456789, {12, 2}]}", "Output"]
}, Open  ]],

Cell["\<\
(4) Multiple wrappers round the full expression are discarded in the \
output.
If the wrappers are of different types then both wrappers are \
effective (in
spite of only NumberForm appearing in the cell label below). If they \
are of the
same type then only the inner one is used (see (5) below)\
\>", "Text"],

Cell[CellGroupData[{

Cell[BoxData[
    \(e4 = Table[Random[], {2}, {2}, {2}]\)], "Input"],

Cell[OutputFormData["\<\
{{{0.3370688535874288, 0.05788748716942282},
  {0.04796294831324889, 0.5300925627462034}},
 {{0.9921185798241328, 0.7817148000400453},
  {0.4000373844310341, 0.533468899804961}}}\
\>", "\<\
{{{0.337069, 0.0578875}, {0.0479629, 0.530093}},

  {{0.992119, 0.781715}, {0.400037, 0.533469}}}\
\>"], "Output"]
}, Open  ]],

Cell[CellGroupData[{

Cell[BoxData[
    \(NumberForm[TableForm[e4], {12, 3}]\)], "Input"],

Cell[OutputFormData["\<\
{{{0.3370688535874288, 0.05788748716942282},
  {0.04796294831324889, 0.5300925627462034}},
 {{0.9921185798241328, 0.7817148000400453},
  {0.4000373844310341, 0.533468899804961}}}\
\>", "\<\
0.337   0.048
0.058   0.53

0.992   0.4
0.782   0.533\
\>"], "Output",
  LineSpacing->{1, 0}]
}, Open  ]],

Cell[CellGroupData[{

Cell[BoxData[
    \(InputForm[%]\)], "Input"],

Cell["\<\
{{{0.3370688535874288, 0.05788748716942282},
  {0.04796294831324889, 0.5300925627462034}},
 {{0.9921185798241328, 0.7817148000400453},
  {0.4000373844310341, 0.533468899804961}}}\
\>", "Output"]
}, Open  ]],

Cell["(5)", "Text"],

Cell[CellGroupData[{

Cell[BoxData[
    \(NumberForm[NumberForm[e4, {12, 3}], {12, 8}]\)], "Input"],

Cell[OutputFormData["\<\
{{{0.3370688535874288, 0.05788748716942282},
  {0.04796294831324889, 0.5300925627462034}},
 {{0.9921185798241328, 0.7817148000400453},
  {0.4000373844310341, 0.533468899804961}}}\
\>", "\<\
{{{0.337, 0.058}, {0.048, 0.53}},

  {{0.992, 0.782}, {0.4, 0.533}}}\
\>"], "Output"]
}, Open  ]],

Cell["\<\
I expect that multiple wrappers round proper parts will behave \
similarly,
except that they will not be discarded.

\
\>", "Text"]
},
FrontEndVersion->"4.2 for Microsoft Windows",
ScreenRectangle->{{0, 1024}, {0, 723}},
WindowSize->{496, 565},
WindowMargins->{{127, Automatic}, {Automatic, 10}}
]

(*End *)

--
Allan

---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565


"DrBob" <majort at cox-internet.com> wrote in message
news:adhs5i$4gt$1 at smc.vnet.net...
> 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: [mg34766] 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
> >
> >
>
>
>
>




  • Prev by Date: Re: Fun 7th grade Algebra problem
  • Next by Date: RE: RE: Re: Is it possible to access internal variables?
  • Previous by thread: Re: repeated entries in File>Palettes list
  • Next by thread: New Units Package On MathSource