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


Groups > comp.soft-sys.math.mathematica > #13641 > unrolled thread

how to set initial values to symbolic variable array?

Started byJon <devout1977@gmail.com>
First post2013-02-28 04:43 +0000
Last post2013-03-02 07:34 +0000
Articles 4 — 4 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  how to set initial values to symbolic variable array? Jon <devout1977@gmail.com> - 2013-02-28 04:43 +0000
    Re: how to set initial values to symbolic variable array? Frank K <fkampas@gmail.com> - 2013-03-01 02:24 +0000
    Re: how to set initial values to symbolic variable array? Szabolcs Horvát <szhorvat@gmail.com> - 2013-03-01 02:25 +0000
    Re: how to set initial values to symbolic variable array? Ray Koopman <koopman@sfu.ca> - 2013-03-02 07:34 +0000

#13641 — how to set initial values to symbolic variable array?

FromJon <devout1977@gmail.com>
Date2013-02-28 04:43 +0000
Subjecthow to set initial values to symbolic variable array?
Message-ID<kgmn9c$8hp$1@smc.vnet.net>
Dear All,

Suppose I defined a symbolic variable array as

Array[a,4]

and defined a function as

f = a[1] + a[2] + a[3] + a[4]

I need to evaluate the value of f in the following way,

f/.{a[1]->1,a[2]->1, ...}

but specify each element of the array one by one is very troublesome, and I
wonder whether there is something like a group definition like

f/.{a[1:4]->1}

to define the four variables at the same time with the same value.

Thanks for your help,

Sincerely,
John

[toc] | [next] | [standalone]


#13653

FromFrank K <fkampas@gmail.com>
Date2013-03-01 02:24 +0000
Message-ID<kgp3hi$ct5$1@smc.vnet.net>
In reply to#13641
In[1]:= ar = Array[a,4]
Out[1]= {a[1],a[2],a[3],a[4]}

In[2]:= f = Plus @@ ar
Out[2]= a[1]+a[2]+a[3]+a[4]

In[3]:= f/. Thread[ar -> 1]
Out[3]= 4

[toc] | [prev] | [next] | [standalone]


#13658

FromSzabolcs Horvát <szhorvat@gmail.com>
Date2013-03-01 02:25 +0000
Message-ID<kgp3it$cu2$1@smc.vnet.net>
In reply to#13641
On 2013-02-28 04:43:24 +0000, Jon said:

> Dear All,
> 
> Suppose I defined a symbolic variable array as
> 
> Array[a,4]
> 
> and defined a function as
> 
> f = a[1] + a[2] + a[3] + a[4]
> 
> I need to evaluate the value of f in the following way,
> 
> f/.{a[1]->1,a[2]->1, ...}
> 
> but specify each element of the array one by one is very troublesome, and I
> wonder whether there is something like a group definition like
> 
> f/.{a[1:4]->1}
> 
> to define the four variables at the same time with the same value.

The LHS of a rule (->) is a pattern. You can use a more general pattern:

f /. a[_] -> 1

For more precise control over which indices are taken:

f /. a[i_ /; 1 <= i <= 4] -> 1

For odd ones only:

f /. a[_?OddQ] -> 1

Ref: http://reference.wolfram.com/mathematica/tutorial/PatternsOverview.html

[toc] | [prev] | [next] | [standalone]


#13665

FromRay Koopman <koopman@sfu.ca>
Date2013-03-02 07:34 +0000
Message-ID<kgsa1l$in4$1@smc.vnet.net>
In reply to#13641
On Feb 27, 8:43 pm, Jon <devout1...@gmail.com> wrote:
> Dear All,
>
> Suppose I defined a symbolic variable array as
>
> Array[a,4]
>
> and defined a function as
>
> f = a[1] + a[2] + a[3] + a[4]
>
> I need to evaluate the value of f in the following way,
>
> f/.{a[1]->1,a[2]->1, ...}
>
> but specify each element of the array one by one is very troublesome, and I
> wonder whether there is something like a group definition like
>
> f/.{a[1:4]->1}
>
> to define the four variables at the same time with the same value.
>
> Thanks for your help,
>
> Sincerely,
> John

f /. a[_] -> 1

4

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web