Does anyone know What # in for example Root[-1 - 2 #1 - #1^2 + 2开发者_StackOverflow #1^3 + #1^4 &, 1] means in Mathematica?
Then what does Root[-1 - 2 #1 - #1^2 + 2 #1^3 + #1^4 &, 1] exactly mean?
Thanks.
It's a placeholder for a variable.
If you want to define a y(x)=x^2 function, you just could do:
f = #^2 &
The & "pumps in" the variable into the # sign. That is important for pairing & and # when you have nested functions.
In: f[2]
Out: 4
If you have a function operating on two vars, you could do:
f = #1 + #2 &
So
In: f[3,4]
Out: 7
Or you may have a function operating in a list, so:
f = #[[1]] + #[[2]] &
So:
In: f[{3,4}]
Out: 7
About Root[]
According to Mathematica help:
Root[f,k] represents the exact kth root of the polynomial equation f[x]==0 .
So, if your poly is x^2 - 1, using what we saw above:
f = #^2 - 1 &
In[4]:= Root[f, 1]
Out[4]= -1 (* as we expected ! *)
And
In[5]:= Root[f, 2]
Out[5]= 1 (* Thanks God ! *)
But if we try with a higher order polynomial:
f = -1 - 2 #1 - #1^2 + 2 #1^3 + #1^4 &
In[6]:= Root[f, 1]
Out[6]= Root[-1 - 2 #1 - #1^2 + 2 #1^3 + #1^4 &, 1]
That means Mathematica doesn't know how to caculate a symbolic result. It's just the first root of the polynomial. But it does know what is its numerical value:
In[7]:= N@Root[-1 - 2 #1 - #1^2 + 2 #1^3 + #1^4 &, 1]
Out[7]= -2.13224
So, Root[f,k] is a kind of stenographic writing for roots of polynomials with order > 3. I save you from an explanation about radicals and finding polynomial roots ... for the better, I think
How to find out what any built-in syntax means in Mathematica:
- Copy expression
- Do TreeForm[Hold[paste the expression here]].
- Mouse-over parts of the tree to identify the syntax in question, in this case Slot
- Enter "?Slot"
Notation # is (as stated above) used to mean "a variable goes here" in a pure function ("closure" for you traditional developers). It must always be followed at the end by &.
Best example is this: f[x_]:=x+5. This creates a delayed set, that any time a value is passed into a symbol reference f as a functional parameter, that value will be given a local context function-specific name of x (not affecting the global definition of x, if there is one). Then the expression x+5 will be evaluated using this new variable/value. The above process requires that symbol f be initialized, local variable x created, and expression x+5 is permanently held in memory, unless you clear it.
Side note: f=5 and f[x_]:=5 both work with a "Symbol" f. f can be referred to as a function, when square brackets are used to extract its value, and f[x_] can peacefully co-exist with f[x_,y_] without overriding each other. One will be used when one parameter is sent, and another when 2 parameters are sent.
Some times you just need a quick function and do not need to define it and leave it hanging. So, (someValue + 5) becomes (#+5)&, where & says "I'm a pure function, and will work with whatever you send me", and # says "I'm the parameter (or a parameter list) that was sent to the pure function". You can also use #1, #2, #3, etc, if you're sending it more than 1 parameter.
Example of multi-parameter pure function in common use:
Let's say mydata is a list of lists, which you need to sort by median of the lists (e.g. housing price data from various US cities):
Sort[ myData , Median[#1] > Median[#2]& ]
Quick tip, if you're applying a function to a single value, it may look neater and cleaner, and uses less typing to use @ instead of [], which essentially means Prefix. Do not confuse with Map (/@) or Apply(@@). The above command then becomes:
Sort[ myData , Median@#1 > Median@#2 & ]
You can chain @ as such: Reverse@Sort@DeleteDuplicates[...]
#1 represents the first argument in a pure function.
If you have multiple arguments #1, #2, #3... refer to the first, second, third argument and so on.
加载中,请稍侯......
精彩评论