Re: how to set initial values to symbolic variable array?
- To: mathgroup at smc.vnet.net
- Subject: [mg129976] Re: how to set initial values to symbolic variable array?
- From: Szabolcs Horvát <szhorvat at gmail.com>
- Date: Thu, 28 Feb 2013 21:29:02 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- Delivered-to: l-mathgroup@wolfram.com
- Delivered-to: mathgroup-newout@smc.vnet.net
- Delivered-to: mathgroup-newsend@smc.vnet.net
- References: <kgmn9c$8hp$1@smc.vnet.net>
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