Re: index problem

• To: mathgroup at yoda.physics.unc.edu
• Subject: Re: index problem
• From: withoff (David Withoff)
• Date: Tue, 19 Apr 1994 12:39:49 -0500

```> Dear MMA group:
>
> a[2]  ={1., 1., 1.}
> a[1]  ={2., 2., 2.}
>
> MMA sometimes does convert integers into reals apparently by
> default as in the next example:
>
> NIntegrate[trhoBE[x,1/2], {x,.1,.9}]
> NIntegrate[trhoBE[x,N[1/2]], {x,.1,.9}]
>
> Out[29]=
> 0.4
>
> Out[30]=
> 0.4
>
> Note that both answers are WRONG. We expect 3 * (0.9 - 0.1) = 2.4.
> In a piece of complicated code, it is extremely hard to nail a problem
> down to this.
>
> Funny, isn't it?
>
>         -Rudo
>
> ###########################################################################
> # Rudolf A. Roemer (RAR)         Room:   306 James Fletcher Building      #
> # Department of Physics          Email:  rar at mail.physics.utah.edu        #
> # University of Utah             FAX:    USA (801) 581 4801               #
> # Salt Lake City, Utah 84112     Phone:  USA (801) 581 6424               #
> # USA                                    USA (801) 461 4450 (home)        #
> ###########################################################################

All of these examples (including a few that I omitted to save space)
can be explained using the fact that a[2] doesn't match a[2.0], and
the fact that exact integers will be converted into reals by
functions like N and NIntegrate.  Both behaviors are, of course,
entirely intentional.

It might perhaps be useful to comment on how this applies to
the following example.

> NIntegrate[
>  Sum[
>   a[2][[j]],
>   {j,1,Length[a[2]]}
>  ],
>  {x,.1,.9}
> ]
> NIntegrate[
>  Sum[
>   a[N[2]][[j]],
>   {j,1,Length[a[N[2]]]}
>  ],
>  {x,.1,.9}
> ]
>
> Out[31]=
> 2.4             (* YES, that's RIGHT! *)
>
> Out[32]=
> 1.6             (* WRONG again *)

NIntegrate[expr, {x, .1, .9}] is evaluated by repeatedly assigning
values to x, and (effectively) evaluating N[expr] after each
assignment.  These assignments have no effect on either a[2]
from the first integral or a[N[2]] from the second integral.
The a[2] in the first integral will match the rule for a[2],
and the a[N[2]] in the second integral won't.

Dave Withoff
Research and Development
Wolfram Research

```

• Prev by Date: Question
• Next by Date: Re: ListContourPlot3D ?
• Previous by thread: index problem
• Next by thread: "Mathematica Supplement" ordering this title?