Ticket #11381: 11381-vector-simplify.patch

File 11381-vector-simplify.patch, 3.3 KB (added by robertwb, 10 years ago)
  • sage/modules/vector_symbolic_dense.py

    # HG changeset patch
    # User Robert Bradshaw <robertwb@math.washington.edu>
    # Date 1306390972 25200
    # Node ID 0a6f5537a7d268ac6d1a1be28a266c794d06fe85
    # Parent  377c6ecb2d3b5dde8ea70a4faaa2dcf6bba047fb
    [mq]: 11381-vector-simplify.patch
    
    diff -r 377c6ecb2d3b -r 0a6f5537a7d2 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:
     
    4642#*****************************************************************************
    4743
    4844import free_module_element
    49 from sage.symbolic.ring import SR
     45from sage.symbolic.all import SR, Expression
    5046
    5147
     48def apply_map(phi):
     49    """
     50    Returns a function that applies phi to its argument.
     51   
     52    EXAMPLES::
     53   
     54        sage: from sage.modules.vector_symbolic_dense import apply_map
     55        sage: v = vector([1,2,3])
     56        sage: f = apply_map(lambda x: x+1)
     57        sage: f(v)
     58        (2, 3, 4)
     59       
     60    """
     61    def apply(self, *args, **kwds):
     62        """
     63        Generic function use to implement common symbolic operations
     64        elementwise as methods of a vector.
     65       
     66        EXAMPLES::
     67       
     68            sage: var('x,y')
     69            (x, y)
     70            sage: v = vector([sin(x)^2 + cos(x)^2, log(x*y), sin(x/(x^2 + x)), factorial(x+1)/factorial(x)])
     71            sage: v.simplify_trig()
     72            (1, log(x*y), sin(1/(x + 1)), factorial(x + 1)/factorial(x))
     73            sage: v.simplify_radical()
     74            (sin(x)^2 + cos(x)^2, log(x) + log(y), sin(1/(x + 1)), factorial(x + 1)/factorial(x))
     75            sage: v.simplify_rational()
     76            (sin(x)^2 + cos(x)^2, log(x*y), sin(1/(x + 1)), factorial(x + 1)/factorial(x))
     77            sage: v.simplify_factorial()
     78            (sin(x)^2 + cos(x)^2, log(x*y), sin(x/(x^2 + x)), x + 1)
     79            sage: v.simplify_full()
     80            (1, log(x*y), sin(1/(x + 1)), x + 1)
     81
     82            sage: v = vector([sin(2*x), sin(3*x)])
     83            sage: v.simplify_trig()
     84            (2*sin(x)*cos(x), (4*cos(x)^2 - 1)*sin(x))
     85            sage: v.simplify_trig(False)
     86            (sin(2*x), sin(3*x))
     87            sage: v.simplify_trig(expand=False)
     88            (sin(2*x), sin(3*x))
     89        """
     90        return self.apply_map(lambda x: phi(x, *args, **kwds))
     91    apply.__doc__ += "\nSee Expression." + phi.__name__ + "() for optional arguments."
     92    return apply
     93
    5294class Vector_symbolic_dense(free_module_element.FreeModuleElement_generic_dense):
    5395
    5496    def simplify_full(self):
     
    66108
    67109        """
    68110        return (SR**len(self))([fun.simplify_full() for fun in self])
     111
     112# Add elementwise methods.
     113for method in ['simplify', 'simplify_exp', 'simplify_factorial',
     114        'simplify_log', 'simplify_radical', 'simplify_rational',
     115        'simplify_trig', 'trig_expand', 'trig_reduce']:
     116    setattr(Vector_symbolic_dense, method, apply_map(getattr(Expression, method)))