Changes between Version 27 and Version 33 of Ticket #11576


Ignore:
Timestamp:
11/11/16 06:09:17 (2 years ago)
Author:
slelievre
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #11576

    • Property Cc slelievre added
    • Property Status changed from needs_review to needs_work
    • Property Milestone changed from sage-5.11 to sage-6.4
  • Ticket #11576 – Description

    v27 v33  
    11People are ''always'' asking how to get sequences of variables, like `a1,a2,a3,a4` or the like.  See [http://ask.sagemath.org/question/611/implicitly-defining-a-sequence-of-variables this ask.sagemath.org] question, for example.
    22
    3 Jason Grout has an interesting possible solution that should find a home somewhere in Sage.
    4 {{{
     3Jason Grout has an interesting possible solution that should find a home somewhere in Sage
     4(see [[https://groups.google.com/d/topic/sage-support/GFJdjFvKCvo/discussion|sage-support discussion]]).
    55
    6 class VariableGenerator(object):
    7      def __init__(self, prefix):
    8          self.__prefix = prefix
    9      @cached_method
    10      def __getitem__(self, key):
    11          return SR.var("%s%s"%(self.__prefix,key))
    12 Now just specify a prefix, and then you can index to your heart's
    13 content to generate variables.
    14 a=VariableGenerator('a') # some people may like 'a_' as the prefix
    15 a[0], a[1], a[2] # all variables
    16 Of course, this can easily be extended to using function call syntax:
    17 a(0), or to using multiple indices: a[1,3].  Indeed, you can let your
    18 imagination run wild and even do things like return full symbolic
    19 matrices or vectors with slices: a[0:5, 0:5].
    20 }}}
     6> {{{
     7> class VariableGenerator(object):
     8>      def __init__(self, prefix):
     9>          self.__prefix = prefix
     10>      @cached_method
     11>      def __getitem__(self, key):
     12>          return SR.var("%s%s"%(self.__prefix,key))
     13> }}}
     14> Now just specify a prefix, and then you can index to your heart's
     15> content to generate variables.
     16> {{{
     17> a = VariableGenerator('a') # some people may like 'a_' as the prefix
     18> a[0], a[1], a[2] # all variables
     19> }}}
     20> Of course, this can easily be extended to using function call syntax:
     21> a(0), or to using multiple indices: a[1,3].  Indeed, you can let your
     22> imagination run wild and even do things like return full symbolic
     23> matrices or vectors with slices: a[0:5, 0:5].
    2124
    2225Apply [attachment:trac_11576-indexed_expression.patch]