0

This is probably very simple but I don't see the solution now. Let us say I have a variable e defined as

e=n

ReplaceAll can subsitute n with value 3 using

e/.n->3

The problem is when e is given by an integral of some function f[{n_}, r_] with n as a parameter

e=Integrate[f[{n}, r],{r, rmin, rmax}]

Then the use of

e/.n->3

will take too long if the integral in question is hard to evaluate and so, better would be FIRST to replace n with its numerical value and THEN evaluate the integral.

If e was defined as a function

e[n_]:=Integrate[f[{n}, r],{r, rmin, rmax}]

there would be no problem. But my function f depends on quite many definitions (using "n") I made earlier. It works ok but then in the end of my code there is a complicated f which makes the integration (i.e. evaluation of "e") too long - and I don't want to change all the definitions I made before.

What to do if I want to keep "e" as above, i.e. I don't want to define it as a function?

wondering
  • 595
  • 3
  • 18

1 Answers1

5

Update

The first method I recommended does not work properly. I fooled myself because I have a $PrePrint definition by default. However following your update I think you do not need ReplaceAll. Instead try Block:

e := Integrate[1/(x^3 + 1), {x, 0, n}];

Block[{n = 2.0}, e]
1.09

Not the use of SetDelayed to keep the definition from evaluating early.

It happens that Table uses a Block-like mechanism(1) therefore you can also use:

Table[e, {n, 1`, 4`, 1`}]
{0.835649, 1.09, 1.15445, 1.17814}

I recommend that you also read:

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • +1. But, two or more levels of Unevaluated usually signal (to me), that Unevaluated is generally the wrong tool for the job (not in this particular case). – Leonid Shifrin Jan 29 '15 at 00:25
  • Thanks. I corrected the $E$ and added some motivation on why I do not want to define a function. To your answer - for some reason I am not gettin "1.09" as output but rather $\text{Unevaluated}\left[\int_0^{2.} \frac{1}{x^3+1} , dx\right]$ Why is that? – wondering Jan 29 '15 at 01:13
  • @wondering You're right, my result only appears because I am using a $PrePrint definition. I forgot to test this in a default configuration. Sorry. Let me see what I can do. – Mr.Wizard Jan 29 '15 at 01:49
  • @wondering Please see the update. – Mr.Wizard Jan 29 '15 at 01:59
  • @Mr.Wizard Thanks, this works. Actually this solution (with SetDelayed + Table came to my mind but because I have two parameters m and n from which only n one takes several values, m only one, I would have had anyway to define m beforehand as 2012rcampion suggested (or to make a Table[e,{n,nmin,nmax},{m,m0,m0}], but that is not very good). I think the best solution for this situation is a combination of your two suggestions, with Block and Table, i.e.: Block[{m=1}, Table[e, {n, 1, 5}]] +1 – wondering Jan 29 '15 at 03:07
  • @wondering I'm glad I could help, and thanks for the Accept. Did you try Albert Retey's blockrules from (3864)? How about my blockSet from (69590)? Either of these may make your task easier. Let me know if you have trouble applying them. – Mr.Wizard Jan 29 '15 at 03:44