2
$\begingroup$

I ran the following code with no problems:

z=t+x;
pr[z_,a_,b_]:= Integrate[z^2,{x,a,b}]
pr[z,a,b]
(OK: a polynomial in a, b, and t *)

However, when I modified the function definition I got in trouble:

z=t+x;
Clear@pr; (* don't forget to do this *)
pr[z_,a_Real,b_Real]:= Integrate[z^2,{x,a,b}]
pr[z,a,b]
(* pr[t+x,a,b] *)

I tried to use Refine together with assumptions that a and b are elements of Reals to solve the problem but that did not work. I guess this was because Refine would affect the Integrate call but not the pr[z,a,b] call.

I managed to deal with this problem by creating a predicate function realQ.

Clear@pr;
realQ[a] = realQ[b] = True;
pr[z_, a_?realQ, b_?realQ] := Integrate[z^2, {x, a, b}]
pr[z, a, b]
(OK: a polynomial in a, b, and t *)

However, I would guess that Mathematica has a better way to do this? Is that the case?

Thanks for the help.

$\endgroup$
1
  • $\begingroup$ note that "_Real" means the Head of the matched function should be Real, not its content!!! $\endgroup$
    – Wjx
    Sep 17, 2016 at 23:23

1 Answer 1

2
$\begingroup$

I think I have the answer!

$Assumptions = {Element[a, Reals], Element[b, Reals]};
z = t + x;
Clear@pr; 
pr[z_, a_, b_] /; Refine[Element[a, Reals] && Element[b, Reals]] := 
Integrate[z^2, {x, a, b}]
pr[z, a, b]
(* OK: a polynomial in a, b, and t *)
pr[z, 0, 1]
(* OK: 1/3 + t + t^2 *)
pr[z,a,I]
(* Does not evaluate, as desired: pr[t + x, a, I] *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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