MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: understanding code

  • To: mathgroup at smc.vnet.net
  • Subject: [mg114196] Re: understanding code
  • From: Leonid Shifrin <lshifr at gmail.com>
  • Date: Fri, 26 Nov 2010 05:31:44 -0500 (EST)

Sam,

i[1] is in this context what is called "indexed variable". It can be used in
most cases where the variable can,
except in some where Symbol is required explicitly, such as a part
assignment or local variables in scoping
constructs or pattern names etc. The part assignment restriction is
illustrated by the following:

In[1]:= a[1]={1,2,3}
Out[1]= {1,2,3}

In[2]:= a[1][[2]]
Out[2]= 2

In[3]:= a[1][[2]]=0
During evaluation of In[3]:= Set::setps: a[1] in the part assignment is not
a symbol. >>
Out[3]= 0

Similarly with other cases where Symbol is required. I briefly discussed
indexed variables here:

http://www.mathprogramming-intro.org/book/node91.html

In this particular case, however, since there is i[_] as well, you can also
interpret i as a function,
defined both in general and on some specific values of the argument. The
difference between
functions (pattern-based) and variables is somewhat blurred in Mathematica.

Hope this helps.

Regards,
Leonid


On Thu, Nov 25, 2010 at 1:57 PM, Sam Takoy <sam.takoy at yahoo.com> wrote:

> Hi,
>
> The following code:
>
> i[_] = 0;
> i[1]++
> i[1]++
> i[1]++
> i[1]++
>
>
> return 0 1 2 3 4. I'd like to understand whats going on here. That is,
>
> What is i?  Is it a function? And what is i[1]? Is it a function or a
> value? Etc...
>
>
> Many thanks in advance,
>
> Sam
>
>


  • Prev by Date: Re: disappointing CUDA speed
  • Next by Date: Finding local maxima in multidimensional array (efficiently)
  • Previous by thread: Re: understanding code
  • Next by thread: Re: understanding code