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

2 Asked on July 25, 2021

1 Asked on July 25, 2021

1 Asked on July 25, 2021 by crobartie

graphs and networks mathematical optimization performance tuning

1 Asked on July 25, 2021

5 Asked on July 25, 2021

4 Asked on July 24, 2021

1 Asked on July 24, 2021

3 Asked on July 24, 2021 by emmaakachukwu

1 Asked on July 24, 2021 by zanhesl

boundary conditions differential equations equation solving graphs and networks

1 Asked on July 24, 2021

1 Asked on July 23, 2021 by filllost

2 Asked on July 23, 2021

differential equations implementation details numerical integration numerics

1 Asked on July 23, 2021

0 Asked on July 23, 2021

1 Asked on July 23, 2021

diophantine equations equation solving mathematical optimization

Get help from others!

Recent Questions

- How Do I Get The Ifruit App Off Of Gta 5 / Grand Theft Auto 5
- Iv’e designed a space elevator using a series of lasers. do you know anybody i could submit the designs too that could manufacture the concept and put it to use
- Need help finding a book. Female OP protagonist, magic
- Why is the WWF pending games (“Your turn”) area replaced w/ a column of “Bonus & Reward”gift boxes?
- Does Google Analytics track 404 page responses as valid page views?

Recent Answers

- haakon.io on Why fry rice before boiling?
- Jon Church on Why fry rice before boiling?
- Lex on Does Google Analytics track 404 page responses as valid page views?
- Peter Machado on Why fry rice before boiling?
- Joshua Engel on Why fry rice before boiling?

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP