Ticket #11381: 11381-vector-simplify-reviewer-changes.2.patch

File 11381-vector-simplify-reviewer-changes.2.patch, 3.8 KB (added by robertwb, 10 years ago)

apply only this patch

  • sage/modules/vector_symbolic_dense.py

    # HG changeset patch
    # User Robert Bradshaw <robertwb@math.washington.edu>
    # Date 1306390972 25200
    # Node ID 987158037a789b28e11903cc4a2b0c3f1719b2ae
    # Parent  5fdd05d6b87e3552620f96e4b7d90c91b8cfca87
    #11381 - more vector simplification methods
    
    diff -r 5fdd05d6b87e -r 987158037a78 sage/modules/vector_symbolic_dense.py
    a b  
    11"""
    22Vectors over the symbolic ring.
    33
    4 Implements vectors over the symbolic ring.  Currently, this class only
    5 provides methods for the simplification of symbolic vectors, as this
    6 functionality was needed during the development of Trac #10132.  In the
    7 long run, this class could be extended along the lines of
    8 ``sage.matrix.matrix_symbolic_dense``.
     4Implements vectors over the symbolic ring.
    95
    106
    117AUTHOR:
    128
    13     -- Joris Vankerschaver (2011-05-15)
     9    -- Robert Bradshaw (2011-05-25): Added more element-wise simplification methods
     10
     11    -- Joris Vankerschaver (2011-05-15): Initial version
     12
    1413
    1514EXAMPLES::
    1615
     
    4645#*****************************************************************************
    4746
    4847import free_module_element
    49 from sage.symbolic.ring import SR
     48from sage.symbolic.all import SR, Expression
     49
     50
     51def apply_map(phi):
     52    """
     53    Returns a function that applies phi to its argument.
     54   
     55    EXAMPLES::
     56   
     57        sage: from sage.modules.vector_symbolic_dense import apply_map
     58        sage: v = vector([1,2,3])
     59        sage: f = apply_map(lambda x: x+1)
     60        sage: f(v)
     61        (2, 3, 4)
     62       
     63    """
     64    def apply(self, *args, **kwds):
     65        """
     66        Generic function used to implement common symbolic operations
     67        elementwise as methods of a vector.
     68       
     69        EXAMPLES::
     70       
     71            sage: var('x,y')
     72            (x, y)
     73            sage: v = vector([sin(x)^2 + cos(x)^2, log(x*y), sin(x/(x^2 + x)), factorial(x+1)/factorial(x)])
     74            sage: v.simplify_trig()
     75            (1, log(x*y), sin(1/(x + 1)), factorial(x + 1)/factorial(x))
     76            sage: v.simplify_radical()
     77            (sin(x)^2 + cos(x)^2, log(x) + log(y), sin(1/(x + 1)), factorial(x + 1)/factorial(x))
     78            sage: v.simplify_rational()
     79            (sin(x)^2 + cos(x)^2, log(x*y), sin(1/(x + 1)), factorial(x + 1)/factorial(x))
     80            sage: v.simplify_factorial()
     81            (sin(x)^2 + cos(x)^2, log(x*y), sin(x/(x^2 + x)), x + 1)
     82            sage: v.simplify_full()
     83            (1, log(x*y), sin(1/(x + 1)), x + 1)
     84
     85            sage: v = vector([sin(2*x), sin(3*x)])
     86            sage: v.simplify_trig()
     87            (2*sin(x)*cos(x), (4*cos(x)^2 - 1)*sin(x))
     88            sage: v.simplify_trig(False)
     89            (sin(2*x), sin(3*x))
     90            sage: v.simplify_trig(expand=False)
     91            (sin(2*x), sin(3*x))
     92        """
     93        return self.apply_map(lambda x: phi(x, *args, **kwds))
     94    apply.__doc__ += "\nSee Expression." + phi.__name__ + "() for optional arguments."
     95    return apply
    5096
    5197
    5298class Vector_symbolic_dense(free_module_element.FreeModuleElement_generic_dense):
     99    pass
    53100
    54     def simplify_full(self):
    55         """
    56         Applies :meth:`simplify_full` to the entries of self.
    57 
    58         EXAMPLES::
    59 
    60             sage: u = vector([sin(x)^2 + cos(x)^2, 1])
    61             sage: u.simplify_full()
    62             (1, 1)
    63             sage: v = vector([log(exp(x))])
    64             sage: v.simplify_full()
    65             (x)
    66 
    67         """
    68         return (SR**len(self))([fun.simplify_full() for fun in self])
     101# Add elementwise methods.
     102for method in ['simplify', 'simplify_exp', 'simplify_factorial',
     103        'simplify_log', 'simplify_radical', 'simplify_rational',
     104        'simplify_trig', 'simplify_full', 'trig_expand', 'trig_reduce']:
     105    setattr(Vector_symbolic_dense, method, apply_map(getattr(Expression, method)))