Mathematica: How to Do This? (Rules)

In summary: It then returns the modified expr.In summary, the conversation discusses using the Mathematica software and manipulating expressions using rules. The participants provide examples of using the command x/. to apply a rule to an expression and return the modified expression. They also discuss the purpose of the 'x' in the rule and the function of the /. operator.
  • #1
Saladsamurai
3,020
7
Hi All!

I am trying to get used to Mathematica and rules and all things syntax. What I am trying t do is this:

If I define a variable as such

Code:
m = Solve[x^4 - 4 == 0, x]

The output is:

Code:
{{x -> -Sqrt[2]}, {x -> -I Sqrt[2]}, {x -> I Sqrt[2]}, {x -> Sqrt[2]}}

So I believe what this means is that the variable m now conatins a list of rules for x.

Now if I use the command:

Code:
t = m[[2]]

the output is:

Code:
{x -> -I Sqrt[2]}

which is again a rule. But what if I want, t, to actually take on the value of that solution. That is, I want:

Code:
t [B]=[/B] -I Sqrt[2]

What syntax or command do I need to use to extract this from m?

Thanks!
 
Physics news on Phys.org
  • #2
Surely you know how to apply rules to manipulate expressions. Find an expression you can manipulate to get the solution you want. Then set t equal to that.
 
  • #3
t=x/.m[[2]]
 
  • #4
Hurkyl said:
Surely you know how to apply rules to manipulate expressions. Find an expression you can manipulate to get the solution you want. Then set t equal to that.

Isn't that what I did? Sorry Hurkyl, but your response is kind of cryptic.

DaleSpam said:
t=x/.m[[2]]

Yes. This is what I wanted. But could you elaborate a little on what we just did here DaleSpam?

What is the 'x' for?

Thanks guys!
 
  • #5
Saladsamurai said:
Isn't that what I did? Sorry Hurkyl, but your response is kind of cryptic.
What sorts of things do you know how to rules? I only know of one: apply the rule to an expression. Your goal, then, is to create an expression that, when the rule is applied to it, results in what you want. (or at least, something you can turn into what you want)



Aside: actually, that's not true. I know three things you can do to rules: pass it to a function like Simplify which will simplify the r.h.s. of the rule, and to manipulate its parse tree -- (x -> a)[[3]] is probably a, but I haven't actually experimented with it to see how this works out.
 
  • #6
Saladsamurai said:
What is the 'x' for?
I don't know, you tell me. You wrote the equation x^4 - 4 == 0 which you solved to get the rule {x -> -I Sqrt[2]}. So what is the 'x' for? It is for whatever you made it for.

If you have the rule {x -> -I Sqrt[2]} and you apply it to an expression that doesn't involve x then it won't do anything.
 
  • #7
DaleSpam said:
I don't know, you tell me. You wrote the equation x^4 - 4 == 0 which you solved to get the rule {x -> -I Sqrt[2]}. So what is the 'x' for? It is for whatever you made it for.

If you have the rule {x -> -I Sqrt[2]} and you apply it to an expression that doesn't involve x then it won't do anything.

What I mean is, I don't know what the expression:

t=x/.m[[2]]

is really doing. Could you tell me what the command x/. Is doing in words?
 
  • #8
Saladsamurai said:
What I mean is, I don't know what the expression:

t=x/.m[[2]]

is really doing. Could you tell me what the command x/. Is doing in words?

I will try.
The operator /. applies rules to an expression and returns it.
the LHS of /. is an expression, the RHS is a set of rules.
here, the rule is x-> -I Sqrt[2], and the expression is x.
So it replaces x with -I Sqrt[2] which is then returned.
 
  • #9
thecritic is correct. The /. operator is short for ReplaceAll[expr,rules]. You can look up ReplaceAll or /. in the online help for more details, but basically it scans through expr looking for anything that looks like any of the rules. If it finds something it makes the substitution indicated in the rule.
 

What is Mathematica?

Mathematica is a software program used for mathematical, scientific, and technical computations. It is primarily used for symbolic and numerical calculations, data analysis, and visualization.

How do I input and manipulate data in Mathematica?

To input data in Mathematica, you can use the built-in functions for creating lists, matrices, and tables. You can also import data from external sources such as text files, spreadsheets, and databases. To manipulate data, you can use a variety of functions and operators such as arithmetic, logical, and relational operations.

Can Mathematica perform symbolic calculations?

Yes, Mathematica has a powerful symbolic computation engine that allows you to perform algebraic, calculus, and other mathematical operations symbolically. This means that Mathematica can work with exact values and expressions instead of just numerical approximations.

How can I visualize data in Mathematica?

There are various built-in functions and tools in Mathematica for data visualization, including plots, charts, and graphs. You can also customize the appearance of your visualizations using different options and styling functions.

Is there a specific syntax for writing code in Mathematica?

Yes, Mathematica has a unique syntax and a set of rules for writing code. It uses a combination of functions, operators, and brackets to represent mathematical expressions and calculations. It also has a built-in help system that can assist you in learning the syntax and using the functions effectively.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
358
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
156
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
202
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
3K
Back
Top