Re: Re: Can't assign value to symbols
- To: mathgroup at smc.vnet.net
- Subject: [mg58493] Re: [mg58469] Re: Can't assign value to symbols
- From: stephen layland <layland at wolfram.com>
- Date: Mon, 4 Jul 2005 02:24:15 -0400 (EDT)
- Mail-followup-to: Peter Pein <petsie@dordos.net>, mathgroup@smc.vnet.net
- References: <da5jcd$243$1@smc.vnet.net> <200507030757.DAA18271@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
and thus spake Peter Pein [2005.07.03 @ 04:08]:
> Lee Newman schrieb:
> > Thanks for the solutions. I'm running with David Park's solution, but
> > now realize there is one additional problem. Once I've executed the
> > statement MapThread[(#1 = #2) & , {parameters[[1]], parameters[[3]]}] ,
> > because I've specified the first row of "parameters" as symbols, this
> > row now loses the symbol names and takes on the assigned values, such
> > that my attempt to execute the MapThread Statement again for a different
> > set of values, e.g. parameters[[2]], fails.
[ ... ]
> use Clear to clear the assigned values:
As others have already suggested, this is a situation where
Mathematica's *Rule Replacements* shine. Instead of setting and
clearing variables, it would be more efficient to apply diffierent
rules.
First off, translate your parameters list into a list of rules.
Here's a goofy way to do this ;) :
In[1]:= parameters = {{a,b,c},{1,2,3},{4,5,6},{7,8,9}};
In[2]:= rules = Thread/@
Thread[Rule[First@parameters,Rest@parameters],List,{2}]
Out[2]= {{a -> 1, b -> 2, c -> 3}, {a -> 4, b -> 5, c -> 6}, {a -> 7, b
-> 8, c -> 9}}
Now, for each set of rules,do something with them:
In[3]:= f[a_, b_, c_] := x^a - (10 c b) Exp[Sin[c x]]
In[4]:= Plot[f[a,b,c]/.#,{x,0,10}]&/@rules
This is much cleaner than assigning and killing variables in a loop. If
you'd rather work with assigned variables, consider passing your values
into another function, where you can work with localized
variables as you wish. In other words, something like:
In[5]:= doSomething[{a_,b_,c_}]:=
Plot[x^a - (10 c b) Exp[Sin[c x]],{x,0,10}]
In[6]:= doSomething/@Rest[parameters]
There are at least Aleph0 different ways to do things, but functional
and rule based approaches will almost always be the most efficient
in Mathematica.
Happy hacking.
--
/*------------------------------*\
| stephen layland |
| Documentation Programmer |
| http://members.wri.com/layland |
\*------------------------------*/
- References:
- Re: Can't assign value to symbols
- From: Peter Pein <petsie@dordos.net>
- Re: Can't assign value to symbols