Re: Re: Re: Re: Can't assign value to symbols

*To*: mathgroup at smc.vnet.net*Subject*: [mg58528] Re: [mg58513] Re: [mg58458] Re: [mg58444] Re: [mg58409] Can't assign value to symbols*From*: Andrzej Kozlowski <akozlowski at gmail.com>*Date*: Wed, 6 Jul 2005 03:11:13 -0400 (EDT)*References*: <31324031.1120574129912.JavaMail.root@vms064.mailsrvcs.net>*Sender*: owner-wri-mathgroup at wolfram.com

Bruce The only way I can imagine of dealing with this situation is if you first make sure that the data that are originally imported into Mathematica are imported as a string and not an expression. (This is easy to do with external data with Mathematica functions ReadList or Import ). So, for example, if to start with T has the form T = "{{a,b,c},{3,4,2},{12,2,1},{6,5,-3}}"; Then all you need to do is: Scan[Clear, StringCases[T, LetterCharacter]] and all the prior values of a, b, and c will b cleared. You can then convert T to Expression with ToExpression. Andrzej On 5 Jul 2005, at 23:35, Bruce Colletti wrote: > Andrzej > > Yes, your idea does work! Thanks. > > I've moved your Scan to the inside-end of the loop, since it kills > two birds with one stone. The code below works as intended unless > a,b,c have values (and then error messages abound). > > The only way I know to avoid this problem is to make Scan > [Clear,Names["Global`*"]] the new first line...except that this > (unfortunately) zaps ALL variables. > > There must be a better way (than my above idea) to clear the T[[1]]- > variables (that may have values coming into the code below). > > How would you tell Mathematica to hold unevaluated the T[[1]]- > elements so that they can be used to clear the associated variables? > > Bruce > > T={{a,b,c},{3,4,2},{12,2,1},{6,5,-3}}; > X=Map[SymbolName,T[[1]]]; > > Do[Print@MapThread[Set,{ToExpression[X,StandardForm,Unevaluated],T > [[k]]}]; > Scan[Clear,X], > {k,2,Length@T}]; > > > ===================== > From: Andrzej Kozlowski <akozlowski at gmail.com> To: mathgroup at smc.vnet.net > Date: Tue Jul 05 04:38:50 CDT 2005 > To: Bruce Colletti <vze269bv at verizon.net> > Cc: mathgroup at smc.vnet.net > Subject: [mg58528] Re: [mg58513] Re: [mg58458] Re: [mg58444] Re: [mg58409] > Can't assign value to symbols > > I see. In this case, does not > > Scan[Clear,X] > > after the Do loop do what you want? > > Andrzej > > On 5 Jul 2005, at 17:31, Bruce Colletti wrote: > > > > >> Andrzej >> >> The aim is to explictly avoid (throughout the code) referring to >> the variables. >> >> For instance, suppose matrix T (in my code) springs from an Excel >> spreadsheet imported into Mathematica, one whose top row declares >> the variable names, and whose later rows give the variables' values >> for each iteration in a simulation. Suppose further that the >> number of columns may change. >> >> Bruce >> >> >> Bruce >> >> ===================== >> From: Andrzej Kozlowski <akozlowski at gmail.com> To: mathgroup at smc.vnet.net >> Date: Tue Jul 05 00:57:52 CDT 2005 >> To: mathgroup at smc.vnet.net >> Subject: [mg58528] [mg58513] Re: [mg58458] Re: [mg58444] Re: [mg58409] Can't >> assign value to symbols >> >> To tell the truth, I don't understand what you are trying to do. Why >> do you want to reevaluate the line X=... ? >> >> If you do not want at the end of your Do loop the variables a,b,c to >> have assigned values the simplest way is to use Block: >> >> Block[{a, b, c}, Do[ >> Print@MapThread[Set, {ToExpression[X, >> StandardForm, Unevaluated], T[[k]]}], >> {k, 2, Length@T}]]; >> >> All the assignments will now take place locally inside Block and the >> variables {a,b,c} are automatically cleared on exit-ting Block. >> >> Andrzej Kozlowski >> >> >> On 5 Jul 2005, at 00:18, Bruce Colletti wrote: >> >> >> >> >> >>> Andrzej >>> >>> Using your reply, I offer this code for Lee's consideration. >>> >>> Although it works when first run, if re-run without quitting the >>> kernal, the "X= line" causes an error because a,b,c are >>> instantiated. >>> >>> At the start of the program, how can I clear these variables >>> WITHOUT explicitly declaring them? I've failed to find the right >>> argument for Clear (e.g., Clear at Subscript...can't find the right >>> counterpart for Subscript). >>> >>> Thankx. >>> >>> Bruce >>> >>> >>> T={{a,b,c},{3,4,2},{12,2,1},{6,5,-3}}; >>> >>> X=Map[SymbolName,T[[1]]]; >>> >>> Do[Apply[Clear,ToExpression[X,StandardForm,Unevaluated]]; >>> Print@MapThread[Set,{ToExpression[X,StandardForm,Unevaluated],T >>> [[k]]}], >>> {k,2,Length@T}]; >>> >>> ===================== >>> From: Andrzej Kozlowski <akozlowski at gmail.com> To: mathgroup at smc.vnet.net >>> >>> >>> >>> >> To: mathgroup at smc.vnet.net >> >> >> >> >>> Date: Sun Jul 03 02:57:11 CDT 2005 >>> Subject: [mg58528] [mg58513] [mg58458] Re: [mg58444] Re: [mg58409] Can't >>> assign value >>> to symbols >>> >>> Instead of ToExpression use >>> >>> ToExpression[#, StandardForm, Unevaluated] & >>> >>> For example: >>> >>> >>> In[1]:= >>> parameters = {{"a", "b", "c"}, {1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; >>> >>> In[2]:= >>> MapThread[(#1 = #2) & , {ToExpression[#,StandardForm,Unevaluated] >>> & /@ >>> parameters[[1]], >>> parameters[[3]]}] ; >>> >>> In[3]:= >>> a >>> >>> Out[3]= >>> 4 >>> >>> In[4]:= >>> MapThread[(#1 = #2) & , {ToExpression[#,StandardForm,Unevaluated] >>> & /@ >>> parameters[[1]], >>> parameters[[2]]}] ; >>> >>> In[5]:= >>> a >>> >>> Out[5]= >>> 1 >>> >>> >>> Of course instead of Standardform you can use TraditionalForm or >>> even >>> InputForm. >>> >>> Andrzej Kozlowski >>> >>> Chiba, Japan >>> >>> >>> On 2 Jul 2005, at 17:07, Lee Newman wrote: >>> >>> >>> >>> >>> >>> >>>> 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. >>>> >>>> in: MapThread[(#1 = #2) & , {parameters[[1]], parameters[[3]]}] ; >>>> in: {a,b,c} >>>> Out: {4,5,6} >>>> in: MapThread[(#1 = #2) & , {parameters[[1]], parameters[[2]]}] ; >>>> out: Set:: setraw : Cannot assign to raw object 4 ... etc >>>> >>>> I want to preserve the first row of "parameters" as the symbol >>>> names, so >>>> one solution is to use strings, and modify David Park's solution to >>>> turn >>>> the strings into symbol names when assigned, This works the first >>>> time >>>> it is run, and parameters[[1]] still contains the strings, but ut >>>> still >>>> fails the second time. but why? >>>> in: parameters = {{"a", "b", "c"}, {1, 2, 3}, {4, 5, 6}, {7, 8, >>>> 9}}; >>>> in: MapThread[(#1 = #2) & , {ToExpression /@ parameters[[1]], >>>> parameters[[3]]}] ; >>>> in: {a,b,c} >>>> out {4,5,6} >>>> in: MapThread[(#1 = #2) & , {ToExpression /@ parameters[[1]], >>>> parameters[[3]]}] ; >>>> out: Set:: setraw : Cannot assign to raw object 4 ... etc >>>> >>>> why does the MapThread statement fail the second time? It seems >>>> ToExpression/@parameters[[1]] converts the strings "a", "b", "c" to >>>> symbols but then evaluates them prior to returning them, so the >>>> attempt >>>> to assign to them in MapThread fails. >>>> >>>> How can I modify the MapThread statement so I can call it multiple >>>> times, each time assigning values to the symbols identified by the >>>> strings in the first row?? >>>> >>>> Lee >>>> >>>> >>>> >>>> David Park wrote: >>>> >>>> >>>> >>>> >>>> >>>> >>>>> Lee, >>>>> >>>>> Set (=) has a higher precedence Than Function (&). So all you have >>>>> to do is >>>>> add parentheses. >>>>> >>>>> parameters = {{a, b, c}, {1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; >>>>> >>>>> MapThread[(#1 = #2) & , {parameters[[1]], parameters[[3]]}] >>>>> {4, 5, 6} >>>>> >>>>> David Park >>>>> djmp at earthlink.net >>>>> http://home.earthlink.net/~djmp/ >>>>> >>>>> From: Lee Newman [mailto:leenewm at umich.edu] To: mathgroup at smc.vnet.net >>>>> >>>>> >>>>> >>>>> >> To: mathgroup at smc.vnet.net >> >> >> >> >>>>> >>>>> >>>>> >>>>> Hello, >>>>> >>>>> Situation: I have a table that contains parameters and sets of >>>>> values >>>>> that I want to assign to the parameters for the purposes of >>>>> running a >>>>> simulation. >>>>> - parameters = {{a,b,c},{1,2,3},{4,5,6},{7,8,9}} >>>>> - want to assign values in a given row to the symbols listed in >>>>> the >>>>> first row. >>>>> - tried using: MapThread[ #1 = #2 &, {parameters[[1]], >>>>> parameters >>>>> [[3]]} ] >>>>> - fails with error "Tag Slot in #1 is Protected" >>>>> - tried adding Unprotect[#1] and a variety of other attemps, but >>>>> can't >>>>> get it to work. >>>>> >>>>> Anyone know how might accomplish this? >>>>> >>>>> Thanks, >>>>> Lee >>>>> >>>>> >>>>> >>>>> >>>>> >>>>> >>>>> >>>>> >>>> >>>> >>>> >>>> >>>> >>>> >>>> >>>> >>>> >>> >>> >>> >>> >>> >>> >> >> >> >> >> > > > >