Ticket #11576: indexed_expression-experimental-fh.patch

File indexed_expression-experimental-fh.patch, 6.4 KB (added by hivert, 8 years ago)
  • sage/libs/ginac/decl.pxi

    # HG changeset patch
    # User Burcin Erocal <burcin@erocal.org>
    # Date 1278108459 -7200
    # Node ID 8e0f2b958a036e482a6a89ae880f584fb9fe8aae
    # Parent  9e29a3d84c48c399daaf3920bcb8b17273a0e876
    Prototype implementation for indexed symbolic expressions.
    
    diff --git a/sage/libs/ginac/decl.pxi b/sage/libs/ginac/decl.pxi
    a b cdef extern from "ginac_wrap.h": 
    4242
    4343    # forward declaration of GEx
    4444    ctypedef struct GEx "ex"
     45    ctypedef struct GExVector "exvector"
    4546
    4647    ctypedef struct GExListIter "GiNaC::lst::const_iterator":
    4748        void inc "operator++" ()
    cdef extern from "ginac_wrap.h": 
    8283        GEx real_part()               except +
    8384        GEx imag_part()               except +
    8485        bint info(unsigned)           except +
     86        GEx simplify_indexed(unsigned int options) except +
     87        GExVector get_free_indices()  except +
    8588
    8689    GExPair make_pair "std::make_pair" (GEx, GEx)
    8790
    cdef extern from "ginac_wrap.h": 
    208211
    209212    void g_list_symbols "list_symbols" (GEx e, GExSet s)
    210213
     214    # indexed expressions
     215    ctypedef struct GIdx "idx":
     216        pass
     217
     218    GIdx ginac_idx "GiNaC::idx" (GEx v, GEx d) except +
     219    GEx ginac_indexed_p "GiNaC::indexed" (GEx v, GIdx i) except +
     220    GEx ginac_indexed_pp "GiNaC::indexed" (GEx v, GIdx i, GIdx j) except +
     221
    211222    # more is_a tests
    212223    bint is_a_add "is_a<add>" (GEx e)
    213224    bint is_a_mul "is_a<mul>" (GEx e)
  • sage/symbolic/expression.pyx

    diff --git a/sage/symbolic/expression.pyx b/sage/symbolic/expression.pyx
    a b import ring 
    141141import sage.rings.integer
    142142import sage.rings.rational
    143143from sage.structure.element cimport ModuleElement, RingElement, Element
    144 from sage.symbolic.getitem cimport OperandsWrapper
     144from sage.symbolic.pynac cimport exvector_to_PyTuple
     145from sage.symbolic.getitem cimport OperandsWrapper, IndexWrapper
    145146from sage.symbolic.function import get_sfunction_from_serial, SymbolicFunction
    146147from sage.rings.rational import Rational  # Used for sqrt.
    147148from sage.misc.derivative import multi_derivative
    cdef class Expression(CommutativeRingEle 
    39713972            res._expr = self
    39723973            return res
    39733974
     3975    property ind:
     3976        def __get__(self):
     3977            """
     3978            sage: tt = x.ind[1]
     3979            """
     3980            cdef IndexWrapper res = IndexWrapper.__new__(IndexWrapper)
     3981            res._expr = self
     3982            return res
     3983
     3984    def simplify_indexed(self):
     3985        from sage.symbolic.ring import SR
     3986        return new_Expression_from_GEx(SR, self._gobj.simplify_indexed(0))
     3987
     3988    def get_free_indices(self):
     3989        cdef GExVector vec = self._gobj.get_free_indices()
     3990        return exvector_to_PyTuple(vec)
     3991
    39743992    def _numerical_approx(self, prec=None, digits=None):
    39753993        """
    39763994        Return a numerical approximation this symbolic expression as
  • sage/symbolic/getitem.pxd

    diff --git a/sage/symbolic/getitem.pxd b/sage/symbolic/getitem.pxd
    a b from sage.structure.sage_object cimport  
    44
    55cdef class OperandsWrapper(SageObject):
    66    cdef Expression _expr
     7
     8cdef class IndexWrapper(SageObject):
     9    cdef Expression _expr
  • sage/symbolic/getitem.pyx

    diff --git a/sage/symbolic/getitem.pyx b/sage/symbolic/getitem.pyx
    a b cdef class OperandsWrapper(SageObject): 
    164164        TESTS::
    165165
    166166            sage: latex((x^2).op)
    167             \text{Operands wrapper for expression }x^{2}
     167            \text{Operands of expression }x^{2}
    168168        """
    169         return r"\text{Operands wrapper for expression }%s"%(self._expr._latex_())
     169        return r"\text{Operands of expression }%s"%(self._expr._latex_())
    170170
    171171    def __reduce__(self):
    172172        """
    def restore_op_wrapper(expr): 
    188188        Operands of x^2
    189189    """
    190190    return expr.op
     191
     192cdef class IndexWrapper(SageObject):
     193    """
     194    """
     195    def __getitem__(self, arg):
     196        """
     197        TESTS::
     198
     199           sage: t = 1+x+x^2
     200           sage: t.op[1:]
     201           [x, 1]
     202        """
     203        cdef Expression py_ind, py_dim
     204        cdef GIdx ind
     205        if isinstance(arg, slice):
     206            if arg.step:
     207                raise ValueError, "only variable and dimension expected in slice"
     208            py_ind = self._expr.coerce_in(arg.start)
     209            py_dim = self._expr.coerce_in(arg.stop)
     210            ind = ginac_idx(py_ind._gobj, py_dim._gobj)
     211            from sage.symbolic.ring import SR
     212            return new_Expression_from_GEx(SR,
     213                    ginac_indexed_p(self._expr._gobj, ind))
     214        elif isinstance(arg, tuple):
     215            raise NotImplementedError, "don't know what to do with multiple indices yet!"
     216        elif isinstance(arg, SageObject):
     217            from sage.symbolic.ring import SR
     218            py_ind = SR._force_pyobject(arg)
     219            py_dim = SR._force_pyobject(None)
     220            ind = ginac_idx(py_ind._gobj, py_dim._gobj)
     221            return new_Expression_from_GEx(SR,
     222                    ginac_indexed_p(self._expr._gobj, ind))
     223        else:
     224            raise TypeError, "argument must be a slice with variable and dimension, for example, i:3, or a list of slices"
     225
     226
     227    def _repr_(self):
     228        """
     229        TESTS::
     230
     231            sage: (x^2).ind
     232            Index wrapper for expression x^2
     233        """
     234        return "Index wrapper for expression %s"%(self._expr)
     235
     236    def _latex_(self):
     237        r"""
     238        TESTS::
     239
     240            sage: latex((x^2).ind)
     241            \text{Index wrapper for expression }x^{2}
     242        """
     243        return r"\text{Index wrapper for expression }%s"%(self._expr._latex_())
     244
     245    def __reduce__(self):
     246        """
     247        TESTS::
     248
     249            sage: (x^2).ind.__reduce__()
     250            (<built-in function restore_ind_wrapper>, (x^2,))
     251            sage: loads(dumps((x^2).ind))
     252            Index wrapper for expression x^2
     253        """
     254        return restore_op_wrapper, (self._expr,)
     255
     256def restore_ind_wrapper(expr):
     257    """
     258    TESTS::
     259
     260        sage: from sage.symbolic.getitem import restore_op_wrapper
     261        sage: restore_ind_wrapper(x^2)
     262        Index wrapper for expression x^2
     263    """
     264    return expr.ind
  • new file sage/symbolic/pynac.pxd

    diff --git a/sage/symbolic/pynac.pxd b/sage/symbolic/pynac.pxd
    new file mode 100644
    - +  
     1include "../libs/ginac/decl.pxi"
     2
     3cdef public object exvector_to_PyTuple(GExVector seq)