Get x as the result for Re [x] in math

I am trying to get the real part of the result of an operation that includes an undefined variable (say x ).

How can I return Mathematica x when I execute Re[x] if I know that x will never be a complex number? I think this involves passing Mathematica that x is real, but I don't know how to do it.

In my case, the expression for which I want the real part is more complicated than a simple variable, but the concept will remain the same.

Some examples:

 INPUT OUTPUT DESIRED RESULT ----- ------ -------------- Re[x] Re[x] x Re[1] 1 1 Re[Sin[x]] Re[Sin[x]] Sin[x] Re[1+x+I] 1 + Re[x] 1+x Re[1 + x*I] 1-Im[x] 1 
+6
source share
4 answers

You can use, for example, the input Simplify[Re[x], x \[Element] Reals] , which will give x as output.

+13
source

Use ComplexExpand . It assumes the variables are real unless you specify otherwise. For instance:

 In[76]:= ComplexExpand[Re[x]] Out[76]= x In[77]:= ComplexExpand[Re[Sin[x]]] Out[77]= Sin[x] In[78]:= ComplexExpand[Re[1+x+I]] Out[78]= 1+x 
+13
source

Two more possibilities:

 Assuming[x \[Element] Reals, Refine[Re[x]]] Refine[Re[x], x \[Element] Reals] 

Both return x .

+5
source

Sometimes it may be useful to define UpValues for a character. It is far from stable, but, nevertheless, can handle several cases.

 Re[x] ^= x; Im[x] ^= 0; Re[x] Re[1] Re[1 + x + I] Re[1 + x*I] 

x

1

1 + x

1

Re[Sin[x]] not evaluated as you wish, but one of the transformations used by FullSimplify puts it in a form that runs Re[x] :

 Re[Sin[x]] // FullSimplify 
 Sin[x] 
+1
source

Source: https://habr.com/ru/post/905145/


All Articles