Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2006
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2006

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

Search the Archive

Re: Convert from String to variable

  • To: mathgroup at smc.vnet.net
  • Subject: [mg70248] Re: [mg70229] Convert from String to variable
  • From: "Peng Yu" <pengyu.ut at gmail.com>
  • Date: Mon, 9 Oct 2006 01:56:05 -0400 (EDT)
  • References: <200610080604.CAA11869@smc.vnet.net> <NDBBJGNHKLMPLILOIPPOAECNFDAA.djmp@earthlink.net> <366c6f340610081232x7f030299lc3f5f5827d3b757e@mail.gmail.com> <366c6f340610081242h1de28809k4dcf80adbfdb81a@mail.gmail.com> <22d35c5a0610081512o60a5c424m87888113b756953@mail.gmail.com>

On 10/8/06, Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com> wrote:
> On 10/8/06, Peng Yu <pengyu.ut at gmail.com> wrote:
> > Hi,
> >
> > I replied the mail to mathgroup at smc.vnet.net. It seems that I haven't
> > subscribe to that mailing list. So I forward the mail to you. Would
> > you please take some time to answer it?
> >
> > Thanks,
> > Peng
> >
> > ---------- Forwarded message ----------
> > From: Peng Yu <pengyu.ut at gmail.com>
To: mathgroup at smc.vnet.net
> > Subject: [mg70248] Re: [mg70229] Convert from String to variable
> >
> >
> > On 10/8/06, David Park <djmp at earthlink.net> wrote:
> > > Peng,
> > >
> > > Just use Symbol.
> > >
> > > Symbol["a" <> ToString[1] <> ToString[2] <> ToString[3]]
> > > Head[%]
> > > a123
> > > Symbol
> >
> > What I really intent to define is a function like the following. l, m
> > and n could go from 0 to any positive integer. Here the parameter list
> > of CD should also be changed correspondingly.
> >
> > CD[Ith_, z_, d_, Ith0_, a000_, a001_, a002_, a010_, a011_, a012_, a020_,
> >     a021_, a022_, a100_, a101_, a102_, a110_, a111_, a112_, a120_, a121_,
> >     a122_, a200_, a201_, a202_, a210_, a211_, a212_, a220_, a221_, a222_] :=
> >     Sum[ToExpression["a" <> ToString[l] <> ToString[m] <> ToString[n]]*(
> >     Ith - Ith0)^l*z^(2*m)*d^(n), {l, 0, 2}, {m, 0, 2}, {n, 0, 2}]
> >
> >
> > I tried this, but it is not working. The difficulty is in the left
> > hand side. Can I write a function to define a set of other functions?
> >
> > CD[Ith_, z_, d_, Ith0_, Flatten@
> >     Table[ToExpression["a" <> ToString[l] <> ToString[m] <> ToString[n] <>
> >            "_"], {l, 0, 2}, {m, 0, 2}, {n, 0, 2}]] := Sum[ToExpression["a" <>
> >             ToString[l] <> ToString[m] <> ToString[n]]*(Ith - Ith0)^l*z^(
> >           2*m)*d^(n), {l, 0, 2}, {m, 0, 2}, {n, 0, 2}]
> >
>
> You should use an immediate assigment '=' rather than a delayed assighment ':='
>
> In[1]:=
> CD[Ith_, z_, d_, Ith0_, a000_, a001_, a002_, a010_,
>    a011_, a012_, a020_, a021_, a022_, a100_, a101_,
>    a102_, a110_, a111_, a112_, a120_, a121_, a122_,
>    a200_, a201_, a202_, a210_, a211_, a212_, a220_,
>    a221_, a222_] =
>   Sum[ToExpression[StringJoin["a", ToString[l],
>       ToString[m], ToString[n]]]*(Ith - Ith0)^l*
>     z^(2*m)*d^n, {l, 0, 2}, {m, 0, 2}, {n, 0, 2}]
>
> Out[1]=
> a000 + a001*d + a002*d^2 + a100*(Ith - Ith0) +
>   a101*d*(Ith - Ith0) + a102*d^2*(Ith - Ith0) +
>   a200*(Ith - Ith0)^2 + a201*d*(Ith - Ith0)^2 +
>   a202*d^2*(Ith - Ith0)^2 + a010*z^2 + a011*d*z^2 +
>   a012*d^2*z^2 + a110*(Ith - Ith0)*z^2 +
>   a111*d*(Ith - Ith0)*z^2 + a112*d^2*(Ith - Ith0)*
>    z^2 + a210*(Ith - Ith0)^2*z^2 +
>   a211*d*(Ith - Ith0)^2*z^2 + a212*d^2*(Ith - Ith0)^2*
>    z^2 + a020*z^4 + a021*d*z^4 + a022*d^2*z^4 +
>   a120*(Ith - Ith0)*z^4 + a121*d*(Ith - Ith0)*z^4 +
>   a122*d^2*(Ith - Ith0)*z^4 + a220*(Ith - Ith0)^2*
>    z^4 + a221*d*(Ith - Ith0)^2*z^4 +
>   a222*d^2*(Ith - Ith0)^2*z^4

":=" works fine for me. Please the results below. My question was how
to define a set of CD function easily, like
CD[Ith_,z_,d_,Ith0_,a000_,a001_,a010_,a011_,a020_,a021_,a100_,a101_,a110_,a111_,a120_,a121_,a200_,a201,a210_,a211_,a220_,a221_]:=
    Sum[ToExpression[StringJoin["a",ToString[l],ToString[m],ToString[n]]]*(
    Ith-Ith0)^l*z^(2*m)*d^n,{l,0,2},{m,0,2},{n,0,1}]
or CD[Ith_,z_,d_,Ith0_,a000_,a001_,a010_,a011_,a100_,a101_,a110_,a111_,a200_,a201,a210_,a211_,a220_,a221_]:=
    Sum[ToExpression[StringJoin["a",ToString[l],ToString[m],ToString[n]]]*(
    Ith-Ith0)^l*z^(2*m)*d^n,{l,0,2},{m,0,1},{n,0,1}]



In[74]:=
CD[Ith_,z_,d_,Ith0_,a000_,a001_,a002_,a010_,a011_,a012_,a020_,a021_,a022_,\
a100_,a101_,a102_,a110_,a111_,a112_,a120_,a121_,a122_,a200_,a201_,a202_,a210_,\
a211_,a212_,a220_,a221_,a222_]:=
    Sum[ToExpression[StringJoin["a",ToString[l],ToString[m],ToString[n]]]*(
    Ith-Ith0)^l*z^(2*m)*d^n,{l,0,2},{m,0,2},{n,0,2}]

In[75]:=
CD[Ith,z,d,Ith0,a000,a001,a002,a010,a011,a012,a020,a021,a022,a100,a101,a102,\
a110,a111,a112,a120,a121,a122,a200,a201,a202,a210,a211,a212,a220,a221,a222]

Out[75]=
\!\(a000 + a001\ d + a002\ d\^2 + a100\ \((Ith - Ith0)\) + a101\
    d\ \((Ith - Ith0)\) + a102\ d\^2\ \((Ith - Ith0)\) +
    a200\ \((Ith - Ith0)\)\^2 + a201\ d\ \((Ith - Ith0)\)\^2 + a202\ d\^2\ \
\((Ith - Ith0)\)\^2 + a010\ z\^2 + a011\ d\
    z\^2 + a012\ d\^2\ z\^2 + a110\ \((Ith - Ith0)\)\ z\^2 + a111\ d\ \((Ith \
- Ith0)\)\ z\^2 + a112\ d\^2\ \((Ith - Ith0)\)\
    z\^2 + a210\ \((Ith - Ith0)\)\^2\ z\^2 + a211\ d\ \((Ith - Ith0)\)\^2\ \
z\^2 + a212\ d\^2\ \((
    Ith - Ith0)\)\^2\ z\^2 + a020\ z\^4 + a021\ d\ z\^4 + a022\ d\^2\ z\^4 + \
a120\ \((Ith -
         Ith0)\)\ z\^4 + a121\ d\ \((Ith - Ith0)\)\ z\^4 + a122\ d\^2\ \((
        Ith - Ith0)\)\ z\^4 + a220\ \((Ith - Ith0)\)\^2\
      z\^4 + a221\ d\ \((Ith -
        Ith0)\)\^2\ z\^4 + a222\ d\^2\ \((Ith - Ith0)\)\^2\ z\^4\)


  • Prev by Date: RE: Formal operations with vectors and scalars
  • Next by Date: Re: a simple product, but with "i = 1...n AND i != x"
  • Previous by thread: Re: Convert from String to variable
  • Next by thread: RE: Convert from String to variable