I think I could make good use of SymTuples for code generation with array arguments, and I believe we did reach a conclusion above, so I rebased the patch to current master and created another patch moving SymTuple into the core. They are both in the branch http://github.com/jegerjensen/sympy/tree/fix_SymTuple2 .
The other patch is below, please review. Øyvind >From 77803884ecf3e81e289fd671a292cf50a372ce80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Jensen?= <jensen.oyv...@gmail.com> Date: Thu, 10 Jun 2010 15:43:53 +0200 Subject: [PATCH 2/2] Moved SymTuple to sympy/core/symtuple.py + imported in core/__init__.py - Tests are moved to sympy/core/tests/test_symtuple.py - the decorator _tuple_wrapper was also moved to sympy/core/ symtuple.py, but not imported to __init__.py because it is meant for internal use. --- sympy/core/__init__.py | 1 + sympy/core/symtuple.py | 46 +++++++++++++++++++++++ ++++++ sympy/core/tests/test_symtuple.py | 16 ++++++++++ sympy/physics/secondquant.py | 49 +----------------------------- sympy/physics/tests/test_secondquant.py | 16 +--------- 5 files changed, 66 insertions(+), 62 deletions(-) create mode 100644 sympy/core/symtuple.py create mode 100644 sympy/core/tests/test_symtuple.py diff --git a/sympy/core/__init__.py b/sympy/core/__init__.py index 7d1911e..01a556d 100644 --- a/sympy/core/__init__.py +++ b/sympy/core/__init__.py @@ -18,6 +18,7 @@ expand_trig, expand_complex from sets import Set, Interval, Union, EmptySet from evalf import PrecisionExhausted, N +from symtuple import SymTuple # expose singletons like exp, log, oo, I, etc. for _n, _cls in Basic.singleton.items(): diff --git a/sympy/core/symtuple.py b/sympy/core/symtuple.py new file mode 100644 index 0000000..071c2c6 --- /dev/null +++ b/sympy/core/symtuple.py @@ -0,0 +1,46 @@ +from basic import Basic + +class SymTuple(Basic): + """ + Wrapper around the builtin tuple object + + The SymTuple is a subclass of Basic, so that it works well in the + Sympy framework. The wrapped tuple is available as self.args, but + you can also access elements or slices with [:] syntax. + + >>> from sympy import symbols + >>> from sympy.physics.secondquant import SymTuple + >>> a, b, c, d = symbols('a b c d') + >>> SymTuple(a, b, c)[1:] + SymTuple(b, c) + >>> SymTuple(a, b, c).subs(a, d) + SymTuple(d, b, c) + + """ + + def __getitem__(self,i): + if isinstance(i,slice): + indices = i.indices(len(self)) + return SymTuple(*[self.args[i] for i in range(*indices)]) + return self.args[i] + + def __len__(self): + return len(self.args) + + def __contains__(self,item): + return item in self.args + + +def _tuple_wrapper(method): + """ + Decorator that makes any tuple in arguments into SymTuple + """ + def wrap_tuples(*args, **kw_args): + newargs=[] + for arg in args: + if type(arg) is tuple: + newargs.append(SymTuple(*arg)) + else: + newargs.append(arg) + return method(*newargs, **kw_args) + return wrap_tuples diff --git a/sympy/core/tests/test_symtuple.py b/sympy/core/tests/ test_symtuple.py new file mode 100644 index 0000000..577678e --- /dev/null +++ b/sympy/core/tests/test_symtuple.py @@ -0,0 +1,16 @@ +from sympy import SymTuple, symbols + +def test_SymTuple(): + t = (1, 2, 3, 4) + st = SymTuple(*t) + assert set(t) == set(st) + assert len(t) == len(st) + assert set(t[:2]) == set(st[:2]) + assert isinstance(st[:], SymTuple) + assert st == SymTuple(1, 2, 3, 4) + assert st.func(*st.args) == st + p,q,r,s = symbols('pqrs') + t2=(p,q,r,s) + st2 = SymTuple(*t2) + assert st2.atoms() == set(t2) + diff --git a/sympy/physics/secondquant.py b/sympy/physics/ secondquant.py index 269df9a..5d22f60 100644 --- a/sympy/physics/secondquant.py +++ b/sympy/physics/secondquant.py @@ -7,11 +7,12 @@ from sympy import ( Basic, Expr, Function, Mul, sympify, Integer, Add, sqrt, - zeros, Pow, I, S,Symbol + zeros, Pow, I, S, Symbol, SymTuple ) from sympy.utilities import iff from sympy.core.cache import cacheit +from sympy.core.symtuple import _tuple_wrapper __all__ = [ @@ -44,7 +45,6 @@ 'wicks', 'NO', 'evaluate_deltas', - 'SymTuple', 'AntiSymmetricTensor', 'substitute_dummies', 'PermutationOperator', @@ -145,51 +145,6 @@ class TensorSymbol(Function): is_commutative = True -class SymTuple(Basic): - """ - Wrapper around the builtin tuple object - - The SymTuple is a subclass of Basic, so that it works well in the - Sympy framework. The wrapped tuple is available as self.args, but - you can also access elements or slices with [:] syntax. - - >>> from sympy import symbols - >>> from sympy.physics.secondquant import SymTuple - >>> a, b, c, d = symbols('a b c d') - >>> SymTuple(a, b, c)[1:] - SymTuple(b, c) - >>> SymTuple(a, b, c).subs(a, d) - SymTuple(d, b, c) - - """ - - def __getitem__(self,i): - if isinstance(i,slice): - indices = i.indices(len(self)) - return SymTuple(*[self.args[i] for i in range(*indices)]) - return self.args[i] - - def __len__(self): - return len(self.args) - - def __contains__(self,item): - return item in self.args - - -def _tuple_wrapper(method): - """ - Decorator that makes any tuple in arguments into SymTuple - """ - def wrap_tuples(*args, **kw_args): - newargs=[] - for arg in args: - if type(arg) is tuple: - newargs.append(SymTuple(*arg)) - else: - newargs.append(arg) - return method(*newargs, **kw_args) - return wrap_tuples - class AntiSymmetricTensor(TensorSymbol): diff --git a/sympy/physics/tests/test_secondquant.py b/sympy/physics/ tests/test_secondquant.py index 61b2276..df178d5 100644 --- a/sympy/physics/tests/test_secondquant.py +++ b/sympy/physics/tests/test_secondquant.py @@ -4,7 +4,7 @@ FockState, AnnihilateBoson, CreateBoson, BosonicOperator, F, Fd, FKet, FBra, BosonState, CreateFermion, AnnihilateFermion, evaluate_deltas, AntiSymmetricTensor, contraction, NO, wicks, - PermutationOperator, simplify_index_permutations, SymTuple, + PermutationOperator, simplify_index_permutations ) from sympy import ( @@ -14,20 +14,6 @@ ) -def test_SymTuple(): - t = (1, 2, 3, 4) - st = SymTuple(*t) - assert set(t) == set(st) - assert len(t) == len(st) - assert set(t[:2]) == set(st[:2]) - assert isinstance(st[:], SymTuple) - assert st == SymTuple(1, 2, 3, 4) - assert st.func(*st.args) == st - p,q,r,s = symbols('pqrs') - t2=(p,q,r,s) - st2 = SymTuple(*t2) - assert st2.atoms() == set(t2) - def test_PermutationOperator(): p,q,r,s = symbols('pqrs') f,g,h,i = map(Function, 'fghi') -- 1.6.5 -- You received this message because you are subscribed to the Google Groups "sympy-patches" group. To post to this group, send email to sympy-patc...@googlegroups.com. To unsubscribe from this group, send email to sympy-patches+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sympy-patches?hl=en.