Re: Indexed element treated as number?

• To: mathgroup at smc.vnet.net
• Subject: [mg80090] Re: Indexed element treated as number?
• From: Bill Rowe <readnewsciv at sbcglobal.net>
• Date: Sun, 12 Aug 2007 07:18:34 -0400 (EDT)

```On 8/11/07 at 2:16 AM, jushen at gmail.com (Jung-Tsung Shen) wrote:

>To get an array of length n, where n is large and post-given, one
>issues the command, for example,

>n=100; kList1=Array[a, n];

>but it seems that there are some limitations to this approach. For
>example, in cases that I know all of the elements are real, and
>would like to issue, say,

>a[37]/:Im[a[37]]=0;

What is it that you think you are trying to do here?

In Mathematica syntax, a[37] is a function named a to be
evaluated at 37. Since you have not defined a and there is no
standard function with this name, Mathematica will have no way
to evaluated a[37] and will return it unevaluated to the
function Im. Again, since a[37] is undefined, Im cannot
determine whether a[37] has an imaginary part or not and will
return unevaluated to Set. And it is this unevaluated result you
are trying to set to 0. Which of course results in an error message

>I get an error message saying "TagSet::sym: Argument a[37] at
>position 1 is expected to be a symbol."

>To declare the element to be explicitly zero sometimes speed up the comput=
ation.

OK fine. If you want a function to have a predefined value of 0
for integer arguments from 1 to n then

n=100;
k=Table[a[m]=0, {m,n}];

will do the trick.

I suspect the heart of your problem is confusion.

My guess is you are interpreting the call Array[a,n] to be
creating something named a that has properties equivalent to an
array as defined in typical programming languages such as C.
This simply is not the case.

The analog of C arrays in Mathematica are lists. The call
Array[a, n] returns a single list (array if you like) of n
elements. But it does not define a as a list (array). Array
assumes a is a function and will evaluate it at the integers 1
to n. It is the results of those evaluations that are returned
as a list.

To see this is the case consider the following.

In[43]:= Clear[a, k];
k = Array[a, 10]

Out[44]= {a(1),a(2),a(3),a(4),a(5),a(6),a(7),a(8),a(9),a(10)}

By using Clear, I've ensured neither k nor a are defined.

In[45]:= Length[k]

Out[45]= 10

showing k is a 10 element long list as expected. But

In[46]:= Length[a]

Out[46]= 0

since it is not a list and has no elements.

Now I define what a should return with an argument = 100 by

In[47]:= a[100] = 1

Out[47]= 1

verifying to be absolutely certain

In[48]:= a[100]

Out[48]= 1

and

In[49]:= Length[a]

Out[49]= 0

since a is still not a list and still has no elements. Of course
k has not been altered since none of its elements include
a[100]. And just to confirm this beyond doubt here is k

In[50]:= k

Out[50]= {a(1),a(2),a(3),a(4),a(5),a(6),a(7),a(8),a(9),a(10)}

But if I now choose to define a[4] to be zero by:

a[4] = 0;

k will be changed as shown by:

In[53]:= k

Out[53]= {a(1),a(2),a(3),0,a(5),a(6),a(7),a(8),a(9),a(10)}

In Mathematica, a list element is obtained using Part or a
double square bracket. This is the equivalent of array indexing
as it would be done in say C. That is to get the 7th element of
k I would do

In[54]:= k[[7]]

Out[54]= a(7)

and get the expected result. And since k is a list of only 10
elements, doing

k[[1000]] or k[[Pi]] or k[[1/2]] will result in error messages.
But Mathematica will no complain one bit about a[1000] or a[Pi]
or a[1/2] since a is a function not a list.
--
To reply via email subtract one hundred and four

```

• Prev by Date: Re: question in mathematica
• Next by Date: Re: Sign Declaration
• Previous by thread: Re: Indexed element treated as number?
• Next by thread: Re: Indexed element treated as number?