# Symbolic properties of Re and Im

Mathematica Asked by pisco on August 16, 2020

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], 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^2]      (** should output f^2 **)
Re[f/(1+f[f])]      (** should output f/(1+f[f]) **)
Refine[Re[f[x]^2], x [Element] Reals]      (** should output f[x]^2 **)
Im[Sin[f]]      (** 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?

Following code does the job.

ClearAll[realFunctions, assumptions, re, im];

realFunctions = {f};

assumptions = Element[x, Reals];

re[expr_] := With[{
functions =
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 =
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], f^2, f[f], f/(1 + f)}]
(* {Re[f[I]], Re[f[y]], f[x]^2, Sin[f], f^2, f[f], f Re[1/(1 + f)]} *)


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


Answered by Soner on August 16, 2020

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

[email protected][f^2]
(* f^2 *)
[email protected][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;
[email protected][f[x]^2]
(* f[x]^2 *)
`

Answered by Natas on August 16, 2020

## Related Questions

### How to define a function reminding of names of the independent variables?

3  Asked on January 4, 2021 by alexei-boulbitch

### TogglerBar linked to Buttons that override input list

1  Asked on January 4, 2021 by kmutiny

### Mathematica Minecraft

1  Asked on January 4, 2021 by faleichik

### Removing a crease artifact in a surface of revolution SphericalPlot

1  Asked on January 4, 2021

### Intra day data with TradingChart

2  Asked on January 4, 2021 by vanillaspinice

### Select one among multiple solutions that satisfies a certain condition and 3D Plot it with varying simulation values

2  Asked on January 4, 2021 by ppp

### Speed up the intersection of the diagonals of a regular polygon

4  Asked on January 4, 2021 by expression

### How to draw tilted axes for spacetime diagrams

2  Asked on January 4, 2021 by fritz-dewit

### Fourier Series of ODE

1  Asked on January 4, 2021 by mord-fustang199

### Changing initial conditions in a recursive polynomial family definition. Then, finding the generation function for this family

1  Asked on January 3, 2021

### Ellipsoid in GraphicsComplex

1  Asked on January 3, 2021 by markus-roellig

### How can I add the tangent line from Fun 2 to Fun 3 to this function?

1  Asked on January 3, 2021 by qy-ln

### How to change vertex color and vertex size at the same time of Graph

2  Asked on January 3, 2021

### Solving a linear system with a badly conditioned matrix

4  Asked on January 2, 2021 by yair-m

### Why does ComplexListPlot labeling function require an Evaluate?

0  Asked on January 1, 2021

3  Asked on January 1, 2021 by bill-s

### Multinomial logistic regression

3  Asked on January 1, 2021 by ibn-opcit

### Is there a workaround to dereference a list element that’s the name of an undefined symbol, and iteratively assign values to the symbols in the list?

2  Asked on January 1, 2021 by dustin-darcy

### Find the best pair of whole numbers that gives a real solution for a system of equations

1  Asked on January 1, 2021

### Trouble with ParametricPlot and custom options

0  Asked on January 1, 2021