INPUT:
- ``name`` -- The sequence name. For example, if you name the
- sequence `x`, the variables will be called `x0`, `x1`,...
+ sequence `x`, the variables will be called `x_0`, `x_1`,...
- ``latex_name`` -- An optional latex expression (string) to
use instead of `name` when converting the symbols to latex.
sage: a = SymbolSequence('a')
sage: a[0]
- a0
+ a_0
sage: a[1]
- a1
+ a_1
Create polynomials with symbolic coefficients of arbitrary
degree::
sage: a = SymbolSequence('a')
sage: p = sum([ a[i]*x^i for i in range(0,5)])
sage: p
- a4*x^4 + a3*x^3 + a2*x^2 + a1*x + a0
+ a_4*x^4 + a_3*x^3 + a_2*x^2 + a_1*x + a_0
Using a different latex name since 'lambda' is reserved::
sage: l = SymbolSequence('l', '\lambda')
sage: l[0]
- l0
+ l_0
sage: latex(l[0])
\lambda_{0}
sage: a = SymbolSequence('a')
sage: a[0,1,2]
- a012
+ a_0_1_2
sage: latex(a[0,1,2])
a_{0}_{1}_{2}
sage: [ a[i,j] for i in range(0,2) for j in range(0,2) ]
- [a00, a01, a10, a11]
+ [a_0_0, a_0_1, a_1_0, a_1_1]
You can pass slices instead of integers to obtain a list of
symbols::
sage: a = SymbolSequence('a')
sage: a[5:7]
- [a5, a6]
+ [a_5, a_6]
This even works for the second, third, etc. indices::
sage: a = SymbolSequence('a')
sage: a[0:2, 0:2]
- [a00, a01, a10, a11]
+ [a_0_0, a_0_1, a_1_0, a_1_1]
TESTS:
We shouldn't overwrite variables in the global namespace::
sage: a = SymbolSequence('a')
- sage: a0 = 4
+ sage: a_0 = 4
sage: a[0]
- a0
- sage: a0
+ a_0
+ sage: a_0
4
The symbol at a given index should always be the same, even when
sage: a = SymbolSequence('a')
sage: a[3, 0:2]
- [a30, a31]
+ [a_3_0, a_3_1]
sage: a[0:2, 3]
- [a03, a13]
+ [a_0_3, a_1_3]
"""
Return a symbol with the given subscript. Creates the
appropriate name and latex_name before delegating to
SR.symbol().
+
+ EXAMPLES::
+
+ sage: a = SymbolSequence('a', 'alpha', 'real')
+ sage: a_1 = a._create_symbol_(1)
+ sage: a_1
+ a_1
+ sage: latex(a_1)
+ alpha_{1}
+
"""
# Allow creating unnamed symbols, for consistency with
# SR.symbol().
name = None
if self._name is not None:
- name = '%s%d' % (self._name, subscript)
+ name = '%s_%d' % (self._name, subscript)
latex_name = None
if self._latex_name is not None:
to be non-iterable. This is slow, but also works, which is
more than can be said about some of the snappier solutions of
lore.
+
+ EXAMPLES::
+
+ sage: a = SymbolSequence('a')
+ sage: a._flatten_list_([1,2,3])
+ [1, 2, 3]
+ sage: a._flatten_list_([1,[2,3]])
+ [1, 2, 3]
+ sage: a._flatten_list_([1,[2,[3]]])
+ [1, 2, 3]
+ sage: a._flatten_list_([[[[[1,[2,[3]]]]]]])
+ [1, 2, 3]
+
"""
result = []
This handles individual integer arguments, slices, and
tuples. It just hands off the real work to
self._subscript_foo_().
+
+ EXAMPLES:
+
+ An integer argument::
+
+ sage: a = SymbolSequence('a')
+ sage: a.__getitem__(1)
+ a_1
+
+ A tuple argument::
+
+ sage: a = SymbolSequence('a')
+ sage: a.__getitem__((1,2))
+ a_1_2
+
+ A slice argument::
+
+ sage: a = SymbolSequence('a')
+ sage: a.__getitem__(slice(1,4))
+ [a_1, a_2, a_3]
+
"""
if isinstance(key, tuple):
return self._subscript_tuple_(key)
"""
The subscript is a single integer, or something that acts like
one.
+
+ EXAMPLES::
+
+ sage: a = SymbolSequence('a')
+ sage: a._subscript_integer_(123)
+ a_123
+
"""
if n < 0:
# Cowardly refuse to create a variable named "a-1".
We were given a slice. Clean up some of its properties
first. The start/step are default for lists. We make
copies of these because they're read-only.
+
+ EXAMPLES::
+
+ sage: a = SymbolSequence('a')
+ sage: a._subscript_slice_(slice(1,3))
+ [a_1, a_2]
+
"""
(start, step) = (s.start, s.step)
if start is None:
"""
When we have more than one level of subscripts, we pick off
the first one and generate the rest recursively.
+
+ EXAMPLES:
+
+ A simple two-tuple::
+
+ sage: a = SymbolSequence('a')
+ sage: a._subscript_tuple_((1,8))
+ a_1_8
+
+ Nested tuples::
+
+ sage: a._subscript_tuple_(( (1,2), (3,(4,5,6)) ))
+ a_1_2_3_4_5_6
+
"""
# We never call this method without an argument.