I guess that's what I don't understand then: why can I not invoke a structure like this? In fact I need to nest several levels of this type of structure, so I would then define variables b_xx etc as b[a[ii]] and so on. If that is not how these variable definitions work, could someone please tell me how they *do *work? ie in what situation would I be able to use the a[0], a[1] etc defined in Jason's email, as variables (either as loop counters or for anything else) - what syntax is required?
thanks On Fri, Mar 29, 2013 at 11:50 PM, Tom Boothby <[email protected]>wrote: > 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 <[email protected]> > 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 <[email protected]> > 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 [email protected]. > >> To post to this group, send email to [email protected]. > >> 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 a topic in the > Google Groups "sage-support" group. > To unsubscribe from this topic, visit > https://groups.google.com/d/topic/sage-support/GFJdjFvKCvo/unsubscribe?hl=en > . > To unsubscribe from this group and all its topics, send an email to > [email protected]. > To post to this group, send email to [email protected]. > 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 [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sage-support?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
