Message Boards Message Boards

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

When is an array not an array?

Posted 11 years ago
My question is this: I don't understand how the construct solc[] works. It appears to be working as an array
yet it is using functional syntax. The code works. Can anybody explain this?
Thanks in advance.
 p1 = VectorPlot[{u, 3 v}, {u, -3, 3}, {v, -3, 3}] ;
 odec[u0_, v0_] :=
   NDSolve[{u'[t] == u[t], v'[t] == 3 v[t], u[0] == u0,
     v[0] == v0}, {u[t], v[t]}, {t, -3, 3}];
 solc[1] = odec[1, 1]; solc[2] = odec[1, -1]; solc[3] = odec[-1, -1];
 solc[4] = odec[-1, 1]; solc[5] = odec[3, 1]; solc[6] = odec[1, 3];
 solc[7] = odec[-1, -3]; solc[8] = odec[-3, -1];
 p2 = ParametricPlot[
    Evaluate[Table[{u[t], v[t]} /. solc[i], {i, 8}]], {t, -3, 3},
   PlotRange -> {{-3, 3}, {-3, 3}}, PlotPoints -> 100,
   AxesLabel -> {"u", "y"}];
Show[{p1, p2}, PlotRange -> {{-3, 3}, {-3, 3}},
AxesLabel -> {"x", "y"}, Axes -> True]
POSTED BY: steve burk
3 Replies
Hi,

Assignments like f[0] = 10 can be made any time. All occurrences of f[0] will then be replaced with its value, 10. The argument can be any Mathematica expression, so f[0.1] = 10 and f[g+h] = 10 are also possible.

In contrast, in order to be able to make an assignment like a[[1]] = 10, the list a must have been allocated beforehand, e.g., using a = ConstantArray[0,10], a = Range[10] or a = Table[0, {10}]. The index in the double brackets must be an integer and the corresponding element is replaced with the value. Note that a[[0]] is the head of a, i.e., List.

Youngjoo Chung
POSTED BY: Youngjoo Chung
An assignment of this form, say
f[1] = 2
creates a downvalue for f, or a transformation rule which Mathematica applies to replace the pattern f[1] with 2. The list of currently defined DownValues for a symbol can be checked like this
In[2]:= DownValues[f]

Out[2]= {HoldPattern[f[1]] :> 2}
In this example, absent any other rules, expressions like f or f[5] will not change upon evaluation, however f[1] will be substituted with 2.
POSTED BY: Ilian Gachevski
This is done somewhat commonly, especially by new users of Mathematica. Elements of a list are indexed using double square brackets: [[...]]. Functions are given their arguments with single arguments: [...].  So what is essentially happening here is that a function called solc is being defined. It is being defined to take the whole numbers 1 thru 8 and return the values you see. Both of these work of course as you saw.

I myself would normally define solc to be a list like this:
solc = {odec[1, 1], odec[1, -1], odec[-1, -1], odec[-1, 1], odec[3, 1], odec[1, 3], odec[-1, -3], odec[-3, -1]}
You can then acces their elements with the list syntax such as solc[[1]] or solc[[4]].
POSTED BY: Sean Clarke
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