Example of Integrate applying a numerical evaluation N

Here is a minimal example:

Integrate[(a[1] + x)^2, {x, 1., 2.}]

2.33333 + 3. a[1.] + 1. a[1.]^2


The problem is that a[1] has been turned into a[1.], which is essentially different. A stranger result can be generated from

Integrate[(a[1] + x)^2, {x, 1., b}]

-0.333333 + 0.333333 b^3 + 1. b^2 a[1] + 1. b a[1]^2 + 0.333333 a[1]^3 -
1. a[1.] - 1. a[1.]^2 - 0.333333 a[1.]^3


That is, part of the result uses a[1] and another part uses a[1.].

Question: how to stop this behaviour and why is it happening?

-
Artur, I noticed that you had extra bytes when copy/paste some code from Mathematica to Chrome (I guess?). If so there is a Mathematica palette fixing this issue when copying any text (input/output) from Mathematica. –  Öskå Aug 20 '14 at 12:44
Hi Oska, yes you are right thanks for noticing. Which Mathematica palette can I use to stop these extra bytes? –  Artur Gower Aug 20 '14 at 12:50
You might take a look at this page and this comment. (It's not "officially" updated yet). –  Öskå Aug 20 '14 at 12:58
Please tell me if you need assistance, there is a chat for that. –  Öskå Aug 20 '14 at 13:08
I also can not reproduce your second example -- what version? –  george2079 Aug 20 '14 at 18:45

There is a useful attribute, NHoldFirst whose purpose is to protect the function from exactly that. So setting:

SetAttributes[a, NHoldFirst];


and then evaluating the integral works the way you want:

Integrate[(a[1] + x)^2, {x, 1., 2.}]
(*2.33333 + 3. a[1] + 1. a[1]^2*)


The relevant example from the documentation cites "indexed" functions that are otherwise evaluated numerically (spherical harmonics, elliptic etc) but whose first argument needs to stay an integer.

As per Michael E2's comment, if you want to protect more than just the first of your function's arguments from N then NHoldAll is the attribute you need.

-
+1. There's also NHoldAll. It nice to be reminded that while we might think of a[1] as an indexed parameter, M treats it just like function. –  Michael E2 Aug 20 '14 at 13:06
thanks - yep I added that in the answer now. –  gpap Aug 20 '14 at 13:14
nice solution, yet it is not obvious why Integrate applies N to the result in the first place. –  george2079 Aug 20 '14 at 18:47
Thanks - It's cause the limits have been expressed in machine precision arithmetic –  gpap Aug 20 '14 at 18:49
I see that, I mean its not obvious that should cause the entire resulting expression to be evaluated numerically. –  george2079 Aug 20 '14 at 18:54

First, I did not get same result as your answer. I got numerical values in all terms.

int = Integrate[(a[1] + x)^2, {x, 1., b}]


MMA 9:

   (* -0.333333 + 0.333333 b^3 - 1. a[1.] + 1. b^2 a[1.] - 1. a[1.]^2 +
1. b a[1.]^2 + 5.55112*10^-17 a[1.]^3 *)


MMA 10:

(*-0.333333 (1. + a[1.])^3 + 0.333333 (b + a[1.])^3*)


(Note: if you expand result from MMA 10, the last term in result of MMA 9 vanish)

in both cases if I use Rationalize, here is what I got:

Rationalize[int]


MMA 9

(*-0.333333 + b^3/3 - 1. a[1] + b^2 a[1] - 1. a[1]^2 + b a[1]^2 +
5.55112*10^-17 a[1]^3*)


MMA 10:

(*-(1/3) (1 + a[1])^3 + 1/3 (b + a[1])^3*)


(Note: quit interesting to see that MMA 10 can give more accurate and compact form result as compare to MMA 9)

-
I don't know what happened but you seem to have posted the same answer three times. You've deleted one but there's still two left. –  RunnyKine Aug 20 '14 at 15:56
Sorry, I don't know what happened to my computer. I have already deleted the extra answer :) –  Algohi Aug 20 '14 at 19:12