Mathematica Globally Define Variable as Real

In summary, the user is having trouble getting Mathematica to recognize specified variables as real only. They have tried using "Assumptions" and "Assuming" but it did not work. They are looking for a way to define the variables globally as real only. Another user suggests using $Assumptions and gives an example of how to use it. After some discussion, it is suggested to also use ComplexExpand for the desired result.
  • #1
teroenza
195
5
I want to write a line of code at the top of my .nb and have Mathematica treat the specified variables as real only. I've tried using "Assumptions" and "Assuming", but it just won't work. I still get back answers where the entire expression is returned inside the Re[ ] command, like it is confused over which variables might have imaginary parts.

Can it just be defined somehow globally that {x,y,z} are real only?
 
Physics news on Phys.org
  • #2
Using $Assumptions should work. You will need to give a specific example if you want more help.
 
  • #3
Code:
Assuming[x \[Element] Reals && y \[Element] Reals && \[Theta] \[Element] Reals,Re[(x + I y)^2 Sin[\[Theta]/2]^2]]

Simplify[Re[(x + I y)^2 Sin[\[Theta]/2]^2],Assumptions ->  x \[Element] Reals &&   y \[Element] Reals && \[Theta] \[Element] Reals]

returns
Code:
Re[(x + I y)^2 Sin[\[Theta]/2]^2]

Re[(x + I y)^2] Sin[\[Theta]/2]^2
 
  • #4
I see. You need to do also a ComplexExpand:
Code:
Assuming[x \[Element] Reals && y \[Element] Reals && \[Theta] \[Element] Reals, 
Simplify[ComplexExpand[Re[(x + I y)^2 Sin[\[Theta]/2]^2]]]]
 
  • #5


I understand the frustration of trying to get Mathematica to treat variables as real only. However, there is a way to globally define variables as real in Mathematica.

One way to do this is by using the "Set" command. For example, if you want to define x, y, and z as real variables, you can type the following at the top of your .nb file:

Set[x, Element[x, Reals]]
Set[y, Element[y, Reals]]
Set[z, Element[z, Reals]]

This will tell Mathematica that x, y, and z are elements of the set of real numbers, and it will treat them as such in any calculations or expressions.

Another option is to use the "Assuming" command, but you need to specify the variables you want to be treated as real within the command. For example:

Assuming[{x, y, z} \[Element] Reals, expression]

This will tell Mathematica to assume that x, y, and z are real variables only when evaluating the expression.

I hope this helps and allows you to effectively use real variables in your Mathematica code.
 

Related to Mathematica Globally Define Variable as Real

What is the purpose of globally defining a variable as real in Mathematica?

Globally defining a variable as real in Mathematica ensures that the variable is treated as a real number in all calculations and functions, rather than an abstract symbol. This can prevent potential errors and improve the accuracy of computations.

How do you globally define a variable as real in Mathematica?

To globally define a variable as real in Mathematica, you can use the command "Assuming" followed by the condition "Element[x, Reals]", where x is the variable you want to define. This will set x as a real number for all subsequent calculations.

Can you globally define multiple variables as real in Mathematica?

Yes, you can globally define multiple variables as real in Mathematica by using the same "Assuming" command followed by the condition "Element[{x,y,z}, Reals]" where x, y, and z are the variables you want to define.

Is it possible to locally define a variable as real in Mathematica?

Yes, it is possible to locally define a variable as real in Mathematica using the "Real" function. This function takes a variable or expression as an argument and returns it with the assumption that all symbols are real. This only applies within the scope of the function or calculation where it is used.

What is the difference between globally and locally defining a variable as real in Mathematica?

Globally defining a variable as real in Mathematica sets the variable as a real number for all subsequent calculations and functions. Locally defining a variable as real using the "Real" function only applies within the scope of the function or calculation where it is used. This means that the variable may still be treated as a symbol in other parts of the code.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
297
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • Programming and Computer Science
Replies
13
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
22
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
16K
Back
Top