# 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[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?

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


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

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

Answered by Natas on August 16, 2020

## Related Questions

### using DeleteCases with CoprimeQ

2  Asked on July 25, 2021

### Collect Exponential With Pattern

1  Asked on July 25, 2021

### Mathematica does not simplify divergent integrals

1  Asked on July 25, 2021

### How to speed up Minimum Spanning Tree algorithm?

1  Asked on July 25, 2021 by crobartie

### Bug in NMaximize in 12.2?

1  Asked on July 25, 2021

### How can I find a functional square root in Mathematica?

1  Asked on July 25, 2021

### Sum all the adjacent values in an array, with some conditions

5  Asked on July 25, 2021

### Mean of ItoProcess

0  Asked on July 25, 2021 by mvc

### Creating a matrix

2  Asked on July 24, 2021 by rf_1

### I want to maximise my function over a particular domain

4  Asked on July 24, 2021

### Understanding the Correlation[] function in Mathematica

1  Asked on July 24, 2021

### Find sum of positive geometric progression numbers

3  Asked on July 24, 2021 by emmaakachukwu

### Modelling transparent boundary conditions on a three-bonded quantum graph

1  Asked on July 24, 2021 by zanhesl

### How to get v12.0 to print PGP public key and secret key BLOCKS after generating a key pair using GenerateAsymmetricKeyPair[]?

1  Asked on July 24, 2021

### Combining histograms with a scatter plot

3  Asked on July 23, 2021

### Is it possible to do partial fraction expansion with specific degree of the variable?

1  Asked on July 23, 2021 by filllost

### NDSolve uses different difference order for different spatial derivative when solving PDE

2  Asked on July 23, 2021

### Ridges on Breather surface

1  Asked on July 23, 2021

### Why does the InterpolationPointsSubdivision method fails to calculate this integral correctly?

0  Asked on July 23, 2021

### Linear diophantine equation

1  Asked on July 23, 2021

### Ask a Question

Get help from others!