Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #1813

Re: Converting list of subscripted variables into an array

From Bill Rowe <readnews@sbcglobal.net>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Converting list of subscripted variables into an array
Date 2011-04-21 07:11 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <ioolbi$bq6$1@smc.vnet.net> (permalink)

Show all headers | View raw


On 4/20/11 at 4:27 AM, cy56@comcast.net (Christopher O. Young) wrote:

>I have a long list that's supposed to represent points on 9
>different curves. I'd like to put them into an array C[j,k] where j
>is the index for the curves and k is the index for the points.

>Subscript[c1, 1] = {1.796875, 1.34375}

>Subscript[c1, 2] = {2.15625, 1.34375}

>Subscript[c1, 3] = {2.296875, 1.359375}

>Subscript[c1, 4] = {2.375, 1.46875}

>Subscript[c1, 5] = {2.46875, 1.671875} ......

>I'm new at pattern matching, and I'm having a hard time getting
>started here. Can anyone steer me in the right direction?

It is difficult to know what to tell you. You ask about pattern
matching but show code with strings which really isn't the same.
In Mathematica, part of an array c is accessed as c[[i,j]]. The
notation c[i,j] is the function c with arguments j,k. At times,
it is convenient to thick of c[j,k] as an array c indexed by j,k
but it is really different.

For example

In[4]:= c = RandomReal[1, {3, 3}];

In[5]:= ArrayQ[c]

Out[5]= True

In[6]:= x[1, 1] = 1; x[1, 2] = 2; x[2, 1] = 3; x[2, 2] = 4;

In[7]:= ArrayQ[x]

Out[7]= False

And for functions that have the property LIstable such as Sqrt,
Sqrt[c] returns an array where each of the elements of is the
square root of the corresponding element of c. But Sqrt[x]
returns unevaluated.

Also note, I used a lower case c not an upper case c. You cannot
use an upper case C since this already has built-in meaning. In
general, it is a bad idea to use single upper case letters as
variables due to conflicts with built-in objects.

>Chris Young cy56@comcast.net IntuMath.org

>In[180]:= "c" <> IntegerString[1]
>Out[180]= "c1"

>In[181]:= Subscript["c" <> IntegerString[1], 2]
>Out[181]= Subscript["c1", 2]

>In[182]:= Evaluate[%]

Instead do either:

In[1]:= Subscript[Evaluate@ToExpression["c1"], 1]

Out[1]= Subscript[c1, 1]

or

In[2]:= ToExpression["Subscript[c1,1]"]

Out[2]= Subscript[c1, 1]

Also note, while you can do

In[3]:= Subscript[c1, 1] = {2, 3}; Subscript[c1, 1]

Out[3]= {2,3}

Clear[Subscript[c1,1]]

fails with an error message. You can make subscripted variables
in Mathematica that will behave just like unsubscripted
variables by using the Notation package (see
Notation/guide/NotationPackage in the Documentation Center).
=46ailure to use the Notation package will result in behavior you
probably won't be expecting. Frankly, I see the effort required
to make subscripted variables work nicely in Mathematica to be
more trouble than it is worth and rarely use subscripted variables.

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: Converting list of subscripted variables into an array Bill Rowe <readnews@sbcglobal.net> - 2011-04-21 07:11 +0000

csiph-web