Skip to content

make it possible to generate sequences of variables easily #11576

Open
@kcrisman

Description

@kcrisman

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
(see sage-support discussion).

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

Apply attachment: trac_11576-indexed_expression.patch

CC: @jasongrout @gvol @orlitzky @eviatarbach @slel

Component: symbolics

Keywords: Cernay2012

Issue created by migration from https://trac.sagemath.org/ticket/11576

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions