2
$\begingroup$

I want to define a function f such that f[x] is real whenever x is real. So I define

f /: Re[f[x_]] /; Element[x, Reals] := f[x];

Now if I try Im[f[1]], it does not simplify. It does after adding another (redundant) rule:

f /: Im[f[x_]] /; Element[x, Reals] := 0;

However, the following still stay unevaluated (i.e. outer Im or Re are not removed) even after adding above two rules:

Re[f[1]^2]      (** should output f[1]^2 **)
Re[f[2]/(1+f[f[1]])]      (** should output f[2]/(1+f[f[1]]) **)
Refine[Re[f[x]^2], x \[Element] Reals]      (** should output f[x]^2 **)
Im[Sin[f[0]]]      (** should output 0 **)

Is there anyway to make the above four (and other similar ones) give out their desired output?

Observe that for a general symbolic x, Refine[Re[Sin[x^2]], x \[Element] Reals] is able to return Sin[x^2]. Therefore I think there are more internal definitions associated to Sin and Power that make Refine works, so perhaps achieving the above would be difficult solely via commands in System`?

$\endgroup$
2
  • $\begingroup$ "They do after adding another (redundant) rule" How? Refine[Im[f[x]], Element[x, Reals]] and Im[f[x]] still returns unevaluated with your rules (and this is expected). $\endgroup$
    – xzczd
    Commented Aug 10, 2020 at 2:58
  • $\begingroup$ @xzczd Yes, thank you. I edited my post. $\endgroup$
    – pisco
    Commented Aug 10, 2020 at 3:29

2 Answers 2

2
$\begingroup$

ComplexExpand is what you want. It tells Mathematica to treat all symbols as real unless specified otherwise.

In order to allow f[x] to be complex for arbitrary arguments you could define

myComplexExpand[expr_] := Module[{g}, ComplexExpand[
    expr /. f[x_] /; x \[Element] Reals :> g[x], f[_]
    ] /. g[x_] :> f[x]
  ]

This then gives reasonable results

myComplexExpand@Re[f[1]^2]
(* f[1]^2 *)
myComplexExpand@Re[f[x]^2]
(* -Im[f[x]]^2 + Re[f[x]]^2 *)

Regarding symbols the only way I see is to define UpValues as you have done

x /: x \[Element] Reals := True;
myComplexExpand@Re[f[x]^2]
(* f[x]^2 *)
$\endgroup$
1
$\begingroup$

Following code does the job.

ClearAll[realFunctions, assumptions, re, im];

realFunctions = {f};

assumptions = Element[x, Reals];

re[expr_] := With[{
functions = 
 Reap[Scan[If[MemberQ[realFunctions, Head[#]], Sow[#]] &, 
     expr, {0, \[Infinity]}]][[2, 1]] /. 
  f[a_] /; 
    UnsameQ[True, Refine[Element[a, Reals]/. Thread[realFunctions -> Identity], assumptions]] :> 
   Nothing
},
Refine[Re[expr], Assumptions -> Element[functions, Reals]]
];

 im[expr_] := With[{
functions = 
 Reap[Scan[If[MemberQ[realFunctions, Head[#]], Sow[#]] &, 
     expr, {0, \[Infinity]}]][[2, 1]] /. 
  f[a_] /; 
    UnsameQ[True, Refine[Element[a, Reals]/. Thread[realFunctions -> Identity], assumptions]] :> 
   Nothing
},
Refine[Im[expr], Assumptions -> Element[functions, Reals]]
];

You specify which functions are real in realFunctions list and also specify the assumptions regarding symbolic parameters in assumptions command. Then re and im commands give real and imaginary parts of any given expression.

For example, above, we defined f to be a real function and x to be a real parameter. Hence we get the expected results:

re[{f[I], f[y], f[x]^2, Sin[f[0]], f[1]^2, f[f[1]], f[2]/(1 + f[1])}]
(* {Re[f[I]], Re[f[y]], f[x]^2, Sin[f[0]], f[1]^2, f[f[1]], f[2] Re[1/(1 + f[1])]} *)

Note that Mathematica does not simplify $\frac{1}{1+x}$ to reals if x is real as the expression can be infinity as well, which is outside the realm of reals. Therefore, the last expression above is correct (in contrast to OP's expectation in the post), i.e.

Refine[Re[1/(1 + x)], Element[x, Reals]]
(*Re[1/(1 + x)]*)
$\endgroup$

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.