From 9a08c865c94e7efd3350e65ff73f67341c191be5 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Fri, 24 Oct 2014 17:23:59 -0400 Subject: [PATCH] Update SymbolSequence to use underscores. --- mjo/symbol_sequence.py | 102 +++++++++++++++++++++++++++++++++++------ 1 file changed, 87 insertions(+), 15 deletions(-) diff --git a/mjo/symbol_sequence.py b/mjo/symbol_sequence.py index 757c60d..cd9e02d 100644 --- a/mjo/symbol_sequence.py +++ b/mjo/symbol_sequence.py @@ -8,7 +8,7 @@ class SymbolSequence: 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. @@ -26,9 +26,9 @@ class SymbolSequence: sage: a = SymbolSequence('a') sage: a[0] - a0 + a_0 sage: a[1] - a1 + a_1 Create polynomials with symbolic coefficients of arbitrary degree:: @@ -36,13 +36,13 @@ class SymbolSequence: 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} @@ -50,34 +50,34 @@ class SymbolSequence: 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 @@ -93,9 +93,9 @@ class SymbolSequence: 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] """ @@ -116,12 +116,22 @@ class SymbolSequence: 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: @@ -136,6 +146,19 @@ class SymbolSequence: 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 = [] @@ -153,6 +176,27 @@ class SymbolSequence: 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) @@ -170,6 +214,13 @@ class SymbolSequence: """ 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". @@ -187,6 +238,13 @@ class SymbolSequence: 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: @@ -209,6 +267,20 @@ class SymbolSequence: """ 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. -- 2.43.2