Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
add index keyword in SR.var
Browse files Browse the repository at this point in the history
  • Loading branch information
mforets committed Apr 29, 2017
1 parent 2f5a28f commit 7b8eb5f
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/sage/symbolic/ring.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ cdef class SymbolicRing(CommutativeRing):

return e

def var(self, name, latex_name=None, domain=None):
def var(self, name, index=None, latex_name=None, domain=None):
"""
Return the symbolic variable defined by x as an element of the
symbolic ring.
Expand All @@ -734,6 +734,11 @@ cdef class SymbolicRing(CommutativeRing):
sage: t = SR.var('theta2'); t
theta2
Automatic indexing is available as well::
sage: SR.var('x', 4)
(x0, x1, x2, x3)
TESTS::
sage: var(' x y z ')
Expand All @@ -756,6 +761,13 @@ cdef class SymbolicRing(CommutativeRing):
sage: var1 = var('var1', latex_name=r'\sigma^2_1'); latex(var1)
{\sigma^2_1}
The index should be an integer greater or equal than 1::
sage: SR.var('K', -273)
Traceback (most recent call last):
...
ValueError: The index should be a positive integer
"""
if is_Expression(name):
return name
Expand All @@ -781,7 +793,13 @@ cdef class SymbolicRing(CommutativeRing):
formatted_latex_name = None
if latex_name is not None:
formatted_latex_name = '{{{0}}}'.format(latex_name)
return self.symbol(name, latex_name=formatted_latex_name, domain=domain)
if index is not None:
if index > 0 and index.is_integer():
return tuple([self.symbol(name+str(i), domain=domain) for i in range(index)])
else:
raise ValueError("The index should be a positive integer")
else:
return self.symbol(name, latex_name=formatted_latex_name, domain=domain)
if len(names_list) > 1:
if latex_name:
raise ValueError("cannot specify latex_name for multiple symbol names")
Expand Down

0 comments on commit 7b8eb5f

Please sign in to comment.