Message Boards Message Boards

0
|
7153 Views
|
3 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Understanding the use of Slot (#)

Posted 11 years ago
Dear,

Could you explain me how does the slot works in the command bellow? I also show the complete code lower. This code is from http://demonstrations.wolfram.com/CubicEquation/.nb file. The "Download Author Code »(preview »)" is wrong and  I had to get the code from the .nb file.

 COMMAND
 (If[Flatten[#1] === {}, {},
 
             (Point[{#1,
          Function[x$, a3*x$^3 + a2*x$^2 + a1*x$ + a0][#1]}] & ) /@
               (x /. #1)] & )[{ToRules[
    Quiet[Reduce[a3*x^3 + a2*x^2 + a1*x + a0 == 0, x,
                Reals]]]}]
 


(*Entire Code*)
Manipulate[

Plot[a3*x^3 + a2*x^2 + a1*x + a0, {x, -2, 2}, PlotRange -> 10,
     PlotLabel ->
                       2       3
         a0 + a1 x + a2 x  + a3 x
      , PlotStyle -> Thickness[0.005], Epilog -> {PointSize[0.015],
         {RGBColor[1, 0.26, 0.], Point[{1.2, 7}]},
    Text[Style["zeros", 12, Italic],
           {1.3, 7}, {-1, 0}], {RGBColor[0.12, 0.61, 0.78],
     Point[{1.2, 6}]},
         Text[
     Style["critical points", 12, Italic], {1.3, 6}, {-1, 0}],
         {RGBColor[0.67, 0.75, 0.15], Point[{1.2, 5}]},
         Text[
     Style["inflection points", 12, Italic], {1.3, 5}, {-1, 0}],
         RGBColor[1, 0.26, 0.], (If[Flatten[#1] === {}, {},
                (Point[{#1,
             Function[x$, a3*x$^3 + a2*x$^2 + a1*x$ + a0][#1]}] & ) /@
                  (x /. #1)] & )[{ToRules[
       Quiet[Reduce[a3*x^3 + a2*x^2 + a1*x + a0 == 0, x,
                   Reals]]]}], RGBColor[0.12, 0.61, 0.78],
         (If[Flatten[#1] === {}, {},
                (Point[{#1,
             Function[x$, a3*x$^3 + a2*x$^2 + a1*x$ + a0][#1]}] & ) /@
                  (x /. #1)] & )[{ToRules[
       Quiet[Reduce[(3*x^2)*a3 + (2*a2)*x + a1 == 0, x,
                   Reals]]]}], RGBColor[0.67, 0.75, 0.15],
         (If[Flatten[#1] === {}, {},
                (Point[{#1,
             Function[x$, a3*x$^3 + a2*x$^2 + a1*x$ + a0][#1]}] & ) /@
                  (x /. #1)] & )[{ToRules[
       Quiet[Reduce[(6*x)*a3 + 2*a2 == 0, x,
                   Reals]]]}]}, ImageSize -> {500, 400}], {a3, -5.,
  5.}, {a2, -5., 5.},
   {a1, -5., 5.}, {a0, -5., 5.}, ControllerLinking -> True]

Thank you,
Ana
POSTED BY: Ana Squadri
3 Replies
There's a lot here, and describing what the code does step by step is probably more than can be done here. The code is itself very dense and isn't particularly readible or simple. 

Here's a good resource mentioned elsewhere on how this kind of syntax works. 
http://mathematica.stackexchange.com/a/25616


T
he entire code is the application of a function:
(If[Flatten[#1] === {}, {}, (Point[{#1,  Function[x$, a3*x$^3 + a2*x$^2 + a1*x$ + a0][#1]}] & ) /@  (x /. #1)] & )
To an argument:
{ToRules[ Quiet[Reduce[a3*x^3 + a2*x^2 + a1*x + a0 == 0 Reals]]]}
The code doesn't seem to work by itself which means we'd have to dig apart the manipulate. 
POSTED BY: Sean Clarke
Posted 11 years ago
Dear:

I agree with you. But Could youu tell me what #1 is? I do no understand why he choosed the ToRule because this means that the solution will come in the following format: {{x->k}}, this could this be found through Solve. Does #1 contains {a0,a1,a2,a3} (see Flatten[#1]==={}? 

Thank you Sean,
Ana
POSTED BY: Ana Squadri
Posted 11 years ago
Let me try this step by step from the inside out.

Reduce is a powerful tool to find solutions, but it will return a solution in the form of x==2 or perhaps no solution and it is difficult to extract that 2.
ToRules is the first step to extract that value by then returning the solution in the form x->2.
Next ToRules is inside of { } so the result will either be an empty list if there is no solution or { x->2 }.

Next { ToRules[ ... ] } is inside of [ ]. Things inside of [ ] are arguments to functions, like [ theta ] is the argument for Sin[ theta ].
So the stuff before the [ { ToRules[ ... ] } ] must be a function, even though it might not look as simple as Sin.
We need to understand how all that stuff is just a function and what that function is.
If you carefully match up ( ) and [ ] and { } you see the function starts with (If[ ... and ends with ] & ) but there are two different & and you need the right one.

Using # and & to define a function or #1 and even #2 and & is a shorthand way of defining functions.
If you look at https://reference.wolfram.com/mathematica/ref/Function.html it may not be enough to really understand how this works.
One way of defining a function would be sqr[ x_ ] := x^2; sqr[ 4 ].
A different way of doing this is #1^2&[4] where the #1 acts like the x, the & says it is the end of the function definition and the [ 4 ] is an argument for the function.

So that is what the #1 is, it is telling Mathematica that this is the name of the argument to use in defining this function.

What the code appears to be doing is first test the result from Reduce, if there is no solution then return an empty list.
If there is a solution then create a point using that value and the result of another nested Function[ ] inside all this.
And it uses x /. #1 to extract the value of that solution.

I realize this is a lot to throw at you at once. The first dozen times trying to understand how #1 and & work can be confusing and frustrating.
See if you can match up any parts of this description with parts of the original code.
If you can tell me what parts of this description I have not explained well enough then I'll see if I can do a better job.
POSTED BY: Bill Simpson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract