Mathematica Mathematica: How to Do This? (Rules)

  • Thread starter Thread starter Saladsamurai
  • Start date Start date
  • Tags Tags
    Mathematica Rules
AI Thread Summary
The discussion focuses on using Mathematica to extract values from a list of rules generated by the Solve function. The user defines a variable 'm' containing solutions to an equation and seeks to assign a specific solution to another variable 't'. The correct syntax to achieve this is provided as 't = x /. m[[2]]', where the '/.' operator applies the rule to the expression 'x'. Participants clarify that this operator replaces 'x' with its corresponding value from the rule, effectively allowing 't' to take on the desired solution. The conversation emphasizes understanding the mechanics of rule application in Mathematica.
Saladsamurai
Messages
3,009
Reaction score
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
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.
 
t=x/.m[[2]]
 
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!
 
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.
 
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.
 
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?
 
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.
 
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.
 

Similar threads

Replies
19
Views
2K
Replies
4
Views
2K
Replies
1
Views
2K
Replies
2
Views
2K
Replies
1
Views
2K
Replies
1
Views
1K
Replies
2
Views
1K
Replies
13
Views
2K
Back
Top