Mathematica: How to Do This? (Rules)

  • Context: Mathematica 
  • Thread starter Thread starter Saladsamurai
  • Start date Start date
  • Tags Tags
    Mathematica Rules
Click For Summary

Discussion Overview

The discussion revolves around using Mathematica to manipulate rules and extract values from solutions. Participants explore syntax and commands related to defining variables, applying rules, and understanding the functionality of specific operators within Mathematica.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant defines a variable using the Solve function and expresses confusion about how to extract a value from a rule.
  • Another participant suggests manipulating expressions to achieve the desired outcome.
  • A participant proposes using the command t = x /. m[[2]] to extract the value, indicating that this is the correct approach.
  • There is a request for clarification on the meaning of the command x /. and its function in the context of the discussion.
  • One participant explains that the /. operator applies rules to an expression and returns the result, specifying that it replaces x with -I Sqrt[2].
  • Another participant confirms the explanation of the /. operator, describing it as a shorthand for ReplaceAll and detailing its functionality in scanning expressions for applicable rules.

Areas of Agreement / Disagreement

Participants generally agree on the use of the /. operator for applying rules, but there is some confusion regarding the underlying mechanics and the role of the variable x in the expressions discussed. The discussion remains somewhat unresolved as participants seek further clarification.

Contextual Notes

There are limitations in understanding the broader implications of applying rules, as well as the specific context in which the variable x is used. The discussion does not resolve the participants' uncertainties about the syntax and its applications.

Who May Find This Useful

Individuals learning Mathematica, particularly those interested in manipulating expressions and understanding the syntax related to rules and substitutions.

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.
 
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 ·
Replies
19
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K