As a new Mathematica user I am puzzled by the following, where the Part[list, n] function - or more accurately list[] - doesn’t seem to have consequent results. For example, in MM 9 on Windows 7 if I say:
This is an order of operations issue - you need to make sure that the second argument to Plot (bounds[[1]]) is evaluated at the appropriate time. One way of doing this is to use the Evaluate function:
For example, this should work:
Plot[curves[[1]], Evaluate@bounds[[1]]]
Note that the reason for this special behavior is that the Plot function has the attribute HoldAll:
Attributes[Plot]
which means that the arguments to Plot are maintained in an unevaluated form.
HoldAll does affect all arguments. The evaluator passes the first argument to Plot unevaluated, which is then free to use it in any way, including evaluation at each sample point.
That makes sense, but in what way is the simple list {x, -10, 10} passed to Plot[ ] when the list is provided as a literal instead of the result of a function call? If both are unevaluated, wha’ts the difference between the literal and the result of bounds[[1]]? Sorry about all the questions, but I’d lke to get a proper handle on how MM does things..
I looked up HoldAll in the documentation, where it’s defined as: an attribute which specifies that all arguments to a function are to be maintained in an unevaluated form. Simple enough, but that doesn’t explain why Plot[ ] then evaluates curves[[1]] but not bounds[[1]].
It might be more clear to you what is happening if you wrapped FullForm around the results:
FullForm[curves[[1]]]
FullForm[bounds[[1]]]
bounds[[1]] has curly braces around it, indicating it is a list. curves[[1]] is just 3 terms added together, so it will have the head Plus (indicating addition).
Thanks Karl - my problem is not with the head of the arguments, it’s with the logic being followed. If HoldAll applies to ALL arguments, why is curves[[1]] evaluated by Plot[ ] and not bounds[[1]]? That’s what seems illogical to me. I though that Plus might be something that needed less(?) evaluation than List and that’s what’s causing the problem? Does the list have to be flattend first before being evaluated and this is what’s causing the ‘problem’?
Perhaps I should have emphasized this -- HoldAll only controls what happens with the arguments before Plot is called. What may take place inside of Plot is a different story, but does not contradict the HoldAll documentation in any way.
There can be, of course, further questions about the design and implementation, for example why does Plot need to be HoldAll. Other people are better qualified to answer that, but I believe it makes some of the advanced visualization machinery much easier to implement.
The problem is very complicated as there are many similar/related commands: Defer, Unevaluated, Hold, HoldForm, HoldComplete, HoldAllComplete, HoldPattern, SequenceHold…
To produce any plot, clearly the function to be plotted needs to be evaluated numerically at the different sampling points. This is why something like Plot[curves[[1]], {x, -10, 10}] works, but note it needs to perform the part taking operation repeatedly for each numerical value of x. It hardly impacts this example, but there are cases where it is more efficient to wrap Evaluate around the first argument.
Another place where evaluation semantics are relevant is the difference between the following two plots