MathGroup Archive 2002

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

Search the Archive

Re: TableForm and NumberForm

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

Bobby, David,

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.


    In[1]:=
        e1= {1.23456789}

    Out[1]=
        {1.23457}

    In[2]:=
        e2= NumberForm[e1,{12,2}]

    Out[2]//NumberForm=
        {1.23}

(1) The above means what it says: it is Out[2] displayed using NumberForm:
Notice that the NumbeForm is not included in Out[2].

    In[3]:=
        Out[2]

    Out[3]=
        {1.23457}

    In[4]:=
        Out[2]//NumberForm[#,{12,3}]&

    Out[4]//NumberForm=
        {1.235}

(2) But NumberForm is is included in e2:

    In[5]:=
        e2

    Out[5]//NumberForm=
        {1.23}


(3) Wrappers other those round the whole expression are not removed:

    In[6]:=
        e3=  NumberForm[#,{12,2}]&/@e1

    Out[6]=
        {1.23}

    In[7]:=
        Out[6]

    Out[7]=
        {1.23}

    In[8]:=
        Out[6]//InputForm

    Out[8]//InputForm=
        {NumberForm[1.23456789, {12, 2}]}


(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. If they are of the
same type then only the inner one is used (see (5) below)


    In[10]:=
        e4= Table[Random[],{2},{2},{2}]

    Out[10]=
        {{{0.980605, 0.828591}, {0.234927, 0.247234}},

          {{0.369566, 0.293526}, {0.450891, 0.857053}}}

    In[11]:=
         NumberForm[TableForm[e4],{12, 3}]

    Out[11]//NumberForm=
        0.981   0.235
        0.829   0.247

        0.37    0.451
        0.294   0.857



    In[12]:=
        InputForm[%]

    Out[12]//InputForm=
        {{{0.9806047229329887, 0.8285912255468133},
           {0.23492703155846179, 0.2472340050517827}},
           {{0.36956643618183393, 0.29352631923750433},
          {0.45089110403603505, 0.8570528523554295}}}

(5)

    In[13]:=
        NumberForm[ NumberForm[e4,{12, 3}],{12,8}]

    Out[13]//NumberForm=
        {{{0.981, 0.829}, {0.235, 0.247}},

          {{0.37, 0.294}, {0.451, 0.857}}}

I expect that multiple wrappers round proper parts will behave similarly,
except that they will not be discarded.

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: [mg34764] 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: RE: Re: Is it possible to access internal variables?
  • Next by Date: Re: Fun 7th grade Algebra problem
  • Previous by thread: RE: TableForm and NumberForm
  • Next by thread: NMinimize function