Ticket #10552: trac-10552-symbolic-matrices.patch

File trac-10552-symbolic-matrices.patch, 6.7 KB (added by jvkersch, 10 years ago)

New version of simplify patch

  • sage/matrix/matrix_symbolic_dense.pyx

    # HG changeset patch
    # User Joris Vankerschaver <joris.vankerschaver@gmail.com>
    # Date 1306807493 25200
    # Node ID e921c17d877aa7f635286f9148417c28b39b07f8
    # Parent  ce324e28c3334398d3552640e2cb1520d22465a3
    #11552 -- elementwise simplification of matrices
    
    diff -r ce324e28c333 -r e921c17d877a sage/matrix/matrix_symbolic_dense.pyx
    a b  
    477477            [    x^2 y^2 + 2]           
    478478        """
    479479        return self.parent()([x.simplify() for x in self.list()])
    480    
    481480
    482     def simplify_trig(self):
     481    def simplify_full(self):
    483482        """
     483        Simplifies self by applying :meth:`simplify_full` to each element
     484        of self.  See :meth:`simplify_full` for more details.
     485
     486        ALIAS: simplify_full and full_simplify are the same.
     487
     488        EXAMPLES::
     489       
     490            sage: m = matrix([[log(8)/log(2), 0], [sin(x)^2 + cos(x)^2, log(exp(x))]]); m
     491            [      log(8)/log(2)                   0]
     492            [sin(x)^2 + cos(x)^2            log(e^x)]
     493            sage: m.simplify_full()
     494            [3 0]
     495            [1 x]
     496        """
     497        return self.apply_map(lambda x : x.simplify_full())
     498
     499    full_simplify = simplify_full
     500
     501    def simplify_trig(self, expand=True):
     502        """
     503        Simplifies `self` using trigonometric identities such as
     504        sin(x)^2 + cos(x)^2 = 1, etc. See :meth:`simplify_trig` for
     505        more information.
     506
     507        INPUT:
     508
     509        - ``expand`` - (default:True) if True, expands trigonometric
     510          and hyperbolic functions of sums of angles and of multiple
     511          angles occurring in ``self`` first. For best results,
     512          ``self`` should be expanded.
     513
     514        ALIAS: :meth:`trig_simplify` and :meth:`simplify_trig` are the same
     515
    484516        EXAMPLES::
    485517       
    486518            sage: theta = var('theta')
     
    492524            [ cos(theta) -sin(theta)]
    493525            [ sin(theta)  cos(theta)]
    494526        """
    495         return self._maxima_(maxima).trigexpand().trigsimp()._sage_()
     527
     528        if expand:
     529            return self._maxima_(maxima).trigexpand().trigsimp()._sage_()
     530        else:
     531            return self._maxima_(maxima).trigsimp()._sage_()
     532
     533    trig_simplify = simplify_trig
    496534
    497535    def simplify_rational(self):
    498536        """
     537        Simplifies self by applying rational simplifications.
     538
    499539        EXAMPLES::
    500540       
    501541            sage: M = matrix(SR, 3, 3, range(9)) - var('t')
     
    508548            [0 1 0]
    509549            [0 0 1]
    510550        """
    511         return self._maxima_(maxima).fullratsimp()._sage_()
     551        return self._maxima_(maxima).fullratsimp()._sage_()
     552
     553    def simplify_factorial(self):
     554        """
     555        Simplifies self by applying :meth:`simplify_factorial` to each element
     556        of self.  See :meth:`simplify_factorial` for more details.
     557
     558        ALIAS: factorial_simplify and simplify_factorial are the same
     559
     560        EXAMPLES::
     561
     562            sage: n = var('n')
     563            sage: M = matrix(SR, [[factorial(n+2), factorial(n+1)], [factorial(n), factorial(n-1)]]); M
     564            [factorial(n + 2) factorial(n + 1)]
     565            [    factorial(n) factorial(n - 1)]
     566            sage: MD = M/factorial(n); MD
     567            [factorial(n + 2)/factorial(n) factorial(n + 1)/factorial(n)]
     568            [                            1 factorial(n - 1)/factorial(n)]
     569            sage: MD.simplify_factorial()
     570            [(n + 1)*(n + 2)           n + 1]
     571            [              1             1/n]
     572       
     573        """
     574        return self.apply_map(lambda x : x.simplify_factorial())
     575
     576    factorial_simplify = simplify_factorial
     577
     578    def simplify_radical(self):
     579        """
     580        Simplifies self by applying :meth:`simplify_radical` to each element
     581        of self.  See :meth:`simplify_radical` for more details.
     582
     583        ALIAS: radical_simplify, simplify_radical, exp_simplify, simplify_exp
     584        are all the same
     585
     586        EXAMPLES::
     587       
     588            sage: x, y = var('x, y')
     589            sage: M = matrix([[log(x*y), 0], [0, (e^x-1)/(1+e^(x/2))]]); M
     590            [                 log(x*y)                         0]
     591            [                        0 (e^x - 1)/(e^(1/2*x) + 1)]
     592            sage: M.simplify_radical()
     593            [log(x) + log(y)               0]
     594            [              0   e^(1/2*x) - 1]
     595
     596        """
     597        return self.apply_map(lambda x : x.simplify_radical())
     598
     599    radical_simplify = simplify_radical
     600    simplify_exp = exp_simplify = simplify_radical
     601
     602    def simplify_log(self,algorithm=None):
     603        """
     604        Simplifies self by applying :meth:`simplify_log` to each element
     605        of self.  See :meth:`simplify_log` for more details.
     606       
     607        INPUT:
     608
     609        - ``algorithm`` - simplification algorithm to use.  See :meth:`simplify_log` for more details.
     610
     611        ALIAS: log_simplify and simplify_log are the same.
     612
     613        EXAMPLES::
     614
     615            sage: x, y = var('x, y')
     616            sage: M = matrix(SR, [[log(x)+log(y), 2*log(x)], [log(x)/2, log(x)-log(y)]])
     617            sage: M.simplify_log()
     618            [  log(x*y)   log(x^2)]
     619            [1/2*log(x)   log(x/y)]
     620
     621        """
     622        return self.apply_map(lambda x : x.simplify_log())
     623
     624    log_simplify = simplify_log
     625
     626    def reduce_trig(self, var=None):
     627        """
     628        Reduces self by applying :meth:`reduce_trig` to each element
     629        of self.  See :meth:`reduce_trig` for more details.
     630       
     631        ALIASES: :meth:`trig_reduce` and :meth:`reduce_trig` are the same
     632
     633        INPUT:
     634
     635        - ``var`` - (default: None) the variable which is used for simplification.  If `none` is specified, all variables are used.
     636
     637        EXAMPLES::
     638
     639            sage: x, y = var('x, y')
     640            sage: M = matrix([[cos(x)^2, cos(y)^3], [0, 1/sin(y)^2]]); M
     641            [   cos(x)^2    cos(y)^3]
     642            [          0 sin(y)^(-2)]
     643            sage: M.reduce_trig()
     644            [       1/2*cos(2*x) + 1/2 1/4*cos(3*y) + 3/4*cos(y)]
     645            [                        0                  csc(y)^2]
     646
     647        To reduce only the expressions involving one variable, we supply
     648        that variable as an optional parameter::
     649       
     650            sage: M.reduce_trig(x)
     651            [1/2*cos(2*x) + 1/2           cos(y)^3]
     652            [                 0        sin(y)^(-2)]
     653            sage: M.reduce_trig(y)
     654            [                 cos(x)^2 1/4*cos(3*y) + 3/4*cos(y)]
     655            [                        0                  csc(y)^2]
     656
     657        """
     658        return self.apply_map(lambda x : x.reduce_trig(var))
     659
     660    trig_reduce = reduce_trig
    512661
    513662    def factor(self):
    514663        """