MathGroup Archive 2007

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

Search the Archive

Re: Indexed element treated as number?

  • To: mathgroup at
  • Subject: [mg80079] Re: Indexed element treated as number?
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at>
  • Date: Sun, 12 Aug 2007 07:12:52 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • References: <f9jl5e$3c6$>

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;
> 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 computation.

In this case, the correct syntax for a tag set is the name of the array 
without any index followed by the tag set to the desired element as in

In[1]:= a /: Im[a[2]] = 0;

Alternatively, we could use *UpSet* as in

In[2]:= Im[a[3]] ^= 0;

Now we can compare the imaginary part of the elements.

In[3]:= Im[a[2]] == Im[a[3]]

Out[3]= True

In[4]:= Im[a[2]] == Im[a[4]]

Out[4]= 0 == Im[a[4]]

> For a list such as
> kList2={k1, k2, k3, ..., k100};
> there's no such limitations, but since the number of elements is
> large, and is post-given, I couldn't have this before hand.
> Is there any solution(s) to this "problem"?

Well, you could use a function like the one below to create your array 
of symbol when you know what the length of the list is.

In[1]:= createArray[n_Integer /; n > 0, sym_Symbol: k] :=
    Table[ToExpression[StringJoin[ToString /@ {sym, i}]], {i, n}]

In[2]:= createArray[5, a]

Out[2]= {a1, a2, a3, a4, a5}

In[3]:= kList2 = createArray[10]

Out[3]= {k1, k2, k3, k4, k5, k6, k7, k8, k9, k10}


  • Prev by Date: Re: Indexed element treated as number?
  • Next by Date: Re: hardware for Mathematica 6.0
  • Previous by thread: Re: Indexed element treated as number?
  • Next by thread: Re: Indexed element treated as number?