class SymbolSequence:
"""
- A callable object which imitates a function from ZZ^n to a
- sequence with n subscripts.
+ An iterable object which acts like a sequence of symbolic
+ expressions (variables).
INPUT:
- - ``name`` -- The sequence name.
+ - ``name`` -- The sequence name. For example, if you name the
+ sequence `x`, the variables will be called `x0`, `x1`,...
- ``latex_name`` -- An optional latex expression (string) to
use instead of `name` when converting the symbols to latex.
OUTPUT:
- A callable object returning symbolic expressions.
+ An iterable object containing symbolic expressions.
EXAMPLES:
+ The simplest use case::
+
+ sage: a = SymbolSequence('a')
+ sage: a[0]
+ a0
+ sage: a[1]
+ a1
+
Create coefficients for polynomials of arbitrary degree::
sage: a = SymbolSequence('a')
- sage: p = sum([ a(i)*x^i for i in range(0,5)])
+ 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
Using a different latex name since 'lambda' is reserved::
sage: l = SymbolSequence('l', '\lambda')
- sage: l(0)
+ sage: l[0]
l0
- sage: latex(l(0))
+ sage: latex(l[0])
\lambda_{0}
Using multiple indices::
sage: a = SymbolSequence('a')
- sage: a(0,1,2)
+ sage: a[0,1,2]
a012
- sage: latex(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) ]
+ sage: [ a[i,j] for i in range(0,2) for j in range(0,2) ]
[a00, a01, a10, a11]
- If no index is given, an unsubscripted symbol is returned::
-
- sage: a = SymbolSequence('a')
- sage: a()
- a
-
You can pass slice objects instead of integers to obtain a list of
symbols::
sage: a = SymbolSequence('a')
- sage: a(slice(5,7))
+ sage: a[5:7]
[a5, a6]
This even works for the second, third, etc. indices::
sage: a = SymbolSequence('a')
- sage: a(slice(0,2), slice(0,2))
+ sage: a[0:2, 0:2]
[a00, a01, a10, a11]
- You can also index with the list index operator::
-
- sage: a = SymbolSequence('a')
- sage: a[1]
- a1
-
- This allows you to retrieve one-dimensional slices easily::
-
- sage: a = SymbolSequence('a')
- sage: a[0:5]
- [a0, a1, a2, a3, a4]
-
- If for whatever reason you prefer square brackets, the following
- also works::
-
- sage: a = SymbolSequence('a')
- sage: a[1,3]
- a13
-
TESTS:
We shouldn't overwrite variables in the global namespace::
sage: a = SymbolSequence('a')
sage: a0 = 4
- sage: a(0)
+ sage: a[0]
a0
sage: a0
4
representation and compare because the output is unpredictable::
sage: a = SymbolSequence()
- sage: a0str = str(a(0))
+ sage: a0str = str(a[0])
sage: str(a(0)) == a0str
True
Slices and single indices work when combined::
sage: a = SymbolSequence('a')
- sage: a(3, slice(0,2))
+ sage: a[3, 0:2]
[a30, a31]
- sage: a(slice(0,2), 3)
+ sage: a[0:2, 3]
[a03, a13]
"""