Better way to do a replacement to define a function

I have a long analytical process where I have defined a stand in function Int[] to act as a placeholder for either NIntegrate[] or Integrate[]. This way I can do algebra on equations that involve integrals without having Mathematica try to actually do the integration. However, at the end when I place in some numbers and leave some parameters to be defied later I always get a warning messages. Below is a very simple example that demos the issue:

After the analytical work say I'm left with:

fun = 42 Int[Exp[-(x/a)^2] + b, {x, 0, 5}]


I now want to create a function with the parameters a and b

NewFun[a_, b_] := Evaluate[fun /. {Int -> NIntegrate}]


I get the warning: NIntegrate::inumr

Is there a better way to replace Int with NIntegrate then define my function? I realize I could just turn off the message but that feels like cheating.

2 Answers

You have to pass numeric-parameters a,b to function fun!

Try

fun[a_?NumericQ, b_?NumericQ] := 42 Int[Exp[-(x/a)^2] + b, {x, 0, 5}]
NewFun[a_, b_] := fun[a, b] /. {Int -> NIntegrate}
NewFun[1,1]
(*247.222*)

• In my process I have to start with fun=stuff but I can do funaux[a_?NumericQ,b_?NumericQ]:=Evaluate[fun]; then use funaux. But when I look at your ?NewFun it still has the replacement to do once called. Is there not a way to get the replacement done in the definition of NewFun ? – c186282 Dec 17 '20 at 17:28
f[a_, b_] := Inactivate[Integrate[a x + b, {x, 0, 5}], Integrate]
f[3, 4] // Activate