Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtain x as result for Re[x] in mathematica

I'm trying to obtain the real part of the result of an operation which involves an undefined variable (let's say x).

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

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
like image 839
GaretJax Avatar asked Jan 04 '12 21:01

GaretJax


4 Answers

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

like image 110
Smi Avatar answered Nov 18 '22 22:11

Smi


Use ComplexExpand. It assumes that the variables are real unless you indicate otherwise. For example:

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
like image 41
Leonid Shifrin Avatar answered Nov 18 '22 22:11

Leonid Shifrin


Two more possibilities:

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

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

Both return x.

like image 43
Arnoud Buzing Avatar answered Nov 18 '22 23:11

Arnoud Buzing


It can at times be useful to define UpValues for a symbol. This is far from robust, but it nevertheless can handle a number of 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]] does not evaluate as you desire, but one of the transformations used by FullSimplify does place it in a form that triggers Re[x]:

Re[Sin[x]] // FullSimplify
Sin[x]
like image 22
Mr.Wizard Avatar answered Nov 18 '22 23:11

Mr.Wizard