1
$\begingroup$

How can I make a function, say, R[expression] such that it works on arguments like this Integrate[f[x],{x,0,Infinity}], applying some operator r[] to the function under integral, and leaving the epression unevaluated if the first level is not an integral from zero to infinity?

For instance, R[Integrate[Sin[x],x]] should produce Integrate[r[Sin[x]],x]] but R[Sin[x]] should be left unevaluated.

I have tried to match the pattern this way but it did not work:

R[Integrate[f_[x_], {x_, 0, Infinity}]] := Integrate[r[f[x]], {x, 0, Infinity}]

$\endgroup$
1
  • $\begingroup$ Your function R needs to hold its arguments, otherwise the argument will be evaluated before R can do anything. So use SetAttributes[R, HoldFirst], for example. $\endgroup$ Jul 12, 2022 at 11:36

2 Answers 2

5
$\begingroup$

To elaborate on my comment:

ClearAll[R]
SetAttributes[R, HoldFirst]
R[Integrate[f_[x_], {x_, 0, Infinity}]] := Integrate[r[f[x]], {x, 0, Infinity}]

R[Integrate[Sin[x], {x, 0, Infinity}]]
R[Integrate[Sin[x], {x, 0, 1}]]
R[Integrate[Sin[x], x]]
R[Sin[x]]

You specified that the expression should remain unevaluated if it's not an integral from 0 to Infinity, so I assume the 2nd and 3rd lines should not evaluate.

$\endgroup$
2
  • $\begingroup$ Thanks! This works well! $\endgroup$
    – Anixx
    Jul 12, 2022 at 11:44
  • $\begingroup$ Hmm, it does not work if under the integral a simple expression of x... $\endgroup$
    – Anixx
    Jul 12, 2022 at 12:42
2
$\begingroup$

Not at my computer but the following should give slightly more general solution.

ClearAll[rInt]
SetAttributes[rInt, HoldFirst]
rInt[Integrate[f_, x:{_Symbol,0,Infinity}..]] :=Integrate[r[f], x]

Then the following give the expected result

rInt[Sin[x]]

rInt[Integrate[Sin[u],u]]

rInt[Integrate[Sin[y],{y,0,Infinity}]]

rInt[Integrate[u^2 - Log[v],{u,0,Infinity},{v,0,Infinity}]]

Hope this helps.

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.