AnswerBun.com

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`?

2 Answers

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

[email protected][f[1]^2]
(* f[1]^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

Add your own answers!

Related Questions

Mathematica Minecraft

1  Asked on January 4, 2021 by faleichik

         

Intra day data with TradingChart

2  Asked on January 4, 2021 by vanillaspinice

     

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

     

Ellipsoid in GraphicsComplex

1  Asked on January 3, 2021 by markus-roellig

 

Coneheads instead of Arrowheads

3  Asked on January 1, 2021 by bill-s

     

Multinomial logistic regression

3  Asked on January 1, 2021 by ibn-opcit

   

Ask a Question

Get help from others!

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