oops, didn't read carefully enough... that third line just doesn't
make much sense.

On Fri, Mar 29, 2013 at 4:49 PM, Tom Boothby <tomas.boot...@gmail.com> wrote:
> Gary,
>
> The third line
>
> for a[ii] in range(0,d):
>
> should read
>
> for ii in range(0,d):
>
> On Fri, Mar 29, 2013 at 4:38 PM, GaryMak <garymako...@googlemail.com> wrote:
>> Hi guys
>>
>> apologies as usual for how dumb this question's going to sound ... but how
>> do I actually use the variables a[1] etc? What I was hoping to do was to use
>> them in the following way (once I had already apparently successfully
>> invoked Jason's routine), where d is a variable I would like to be able to
>> change:
>>
>> d = 5;
>> for ii in range(0,d):
>>     for a[ii] in range(0,d):
>>         print a[ii]^2 + ii;
>>
>> but I get "TypeError: 'VariableGenerator' object does not support item
>> assignment" seemingly no matter what variant I try.
>>
>> Thanks for any pointers!
>> kind regards
>> Gary
>>
>>
>> On Tuesday, July 5, 2011 1:05:32 AM UTC+1, Jason Grout wrote:
>>>
>>> This question has come up often enough that I thought posting a short
>>> snippet here might be useful.  The question is: how do I automatically
>>> generate variables based on indices (e.g., a[0], a[1], etc. being
>>> variables).  Here is one way:
>>>
>>> class VariableGenerator(object):
>>>      def __init__(self, prefix):
>>>          self.__prefix = prefix
>>>
>>>      @cached_method
>>>      def __getitem__(self, key):
>>>          return SR.var("%s%s"%(self.__prefix,key))
>>>
>>> Now just specify a prefix, and then you can index to your heart's
>>> content to generate variables.
>>>
>>> a=VariableGenerator('a') # some people may like 'a_' as the prefix
>>> a[0], a[1], a[2] # all variables
>>>
>>> Of course, this can easily be extended to using function call syntax:
>>> a(0), or to using multiple indices: a[1,3].  Indeed, you can let your
>>> imagination run wild and even do things like return full symbolic
>>> matrices or vectors with slices: a[0:5, 0:5].
>>>
>>> Perhaps this is useful enough to be in Sage instead of just a snippet
>>> here too...
>>>
>>> Thanks,
>>>
>>> Jason
>>>
>>> --
>>> Jason Grout
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "sage-support" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to sage-support+unsubscr...@googlegroups.com.
>> To post to this group, send email to sage-support@googlegroups.com.
>> Visit this group at http://groups.google.com/group/sage-support?hl=en.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To post to this group, send email to sage-support@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-support?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to