Opened 8 years ago

Last modified 2 years ago

#11576 needs_work enhancement

make it possible to generate sequences of variables easily — at Initial Version

Reported by: kcrisman Owned by: burcin
Priority: major Milestone: sage-6.4
Component: symbolics Keywords: Cernay2012
Cc: jason, iandrus, mjo, eviatarbach, slelievre Merged in:
Authors: Reviewers:
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Description

People are always asking how to get sequences of variables, like a1,a2,a3,a4 or the like. See this ask.sagemath.org question, for example.

Jason Grout has an interesting possible solution that should find a home somewhere in Sage.

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]. 

Change History (0)

Note: See TracTickets for help on using tickets.