# How can I tell the kernel that a variable is Real?

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.

• note that "_Real" means the Head of the matched function should be Real, not its content!!!
– Wjx
Sep 17 '16 at 23:23

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] *)