ó
¡¼™\c           @   s  d  Z  d d l Z d d l m Z d d l m Z d d l m Z d d l m	 Z	 m
 Z
 m Z m Z m Z m Z d d l m Z e d d	 d
 d d d ƒ j ƒ  d „  Z d „  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d S(   s^   
Solution of equations using dense matrices.

The dense matrix is stored as a list of lists.

iÿÿÿÿN(   t   range(   t   isqrt(   t   symbols(   t   augmentt   colt   conjugate_transposet   eyet   rowaddt   rowmul(   t   SymPyDeprecationWarningt   featuret
   densesolvet   issuei—1  t   deprecated_since_versions   1.1c         C   sÔ   t  j |  ƒ } t | ƒ } x² t | ƒ D]¤ } | | | d k rx | | | d k rx t | | d | | | | ƒ n  xQ t | d | ƒ D]< } | | | d k rŒ t | | | | | | | ƒ qŒ qŒ Wq( W| S(   s¦  
    Returns the row echelon form of a matrix with diagonal elements
    reduced to 1.

    Examples
    ========

    >>> from sympy.matrices.densesolve import row_echelon
    >>> from sympy import QQ
    >>> a = [
    ... [QQ(3), QQ(7), QQ(4)],
    ... [QQ(2), QQ(4), QQ(5)],
    ... [QQ(6), QQ(2), QQ(3)]]
    >>> row_echelon(a, QQ)
    [[1, 7/3, 4/3], [0, 1, -7/2], [0, 0, 1]]

    See Also
    ========

    rref
    i   i    (   t   copyt   deepcopyt   lenR    R   R   (   t   matlistt   Kt   result_matlistt   nrowt   it   j(    (    s8   lib/python2.7/site-packages/sympy/matrices/densesolve.pyt   row_echelon   s    ("*c         C   s’   t  j |  ƒ } t | | ƒ } t | ƒ } xa t | ƒ D]S } | | | d k r7 x6 t | ƒ D]% } t | | | | | | | ƒ q^ Wq7 q7 W| S(   s{  
    Returns the reduced row echelon form of a Matrix.

    Examples
    ========

    >>> from sympy.matrices.densesolve import rref
    >>> from sympy import QQ
    >>> a = [
    ... [QQ(1), QQ(2), QQ(1)],
    ... [QQ(-2), QQ(-3), QQ(1)],
    ... [QQ(3), QQ(5), QQ(0)]]
    >>> rref(a, QQ)
    [[1, 0, -5], [0, 1, 3], [0, 0, 0]]

    See Also
    ========

    row_echelon
    i   (   R   R   R   R   R    R   (   R   R   R   R   R   R   (    (    s8   lib/python2.7/site-packages/sympy/matrices/densesolve.pyt   rref8   s    *i    c      	   C   sÎ   t  |  ƒ } t | | ƒ t j |  ƒ } } x– t | ƒ D]ˆ } x t | d | ƒ D]j } | | | d k rR | | | | | | | | | <t | | | | | | | | | | ƒ qR qR Wq8 W| | f S(   sÕ  
    It computes the LU decomposition of a matrix and returns L and U
    matrices.

    Examples
    ========

    >>> from sympy.matrices.densesolve import LU
    >>> from sympy import QQ
    >>> a = [
    ... [QQ(1), QQ(2), QQ(3)],
    ... [QQ(2), QQ(-4), QQ(6)],
    ... [QQ(3), QQ(-9), QQ(-3)]]
    >>> LU(a, QQ)
    ([[1, 0, 0], [2, 1, 0], [3, 15/8, 1]], [[1, 2, 3], [0, -8, 0], [0, 0, -12]])

    See Also
    ========

    upper_triangle
    lower_triangle
    i   i    (   R   R   R   R   R    R   (   R   R   t   reverseR   t   new_matlist1t   new_matlist2R   R   (    (    s8   lib/python2.7/site-packages/sympy/matrices/densesolve.pyt   LUW   s    "6c   	      C   s  t  j |  ƒ } t | ƒ } t | | ƒ } xÂ t | ƒ D]´ } x« t | d ƒ D]™ } | j } x2 t | ƒ D]$ } | | | | | | | 7} qj W| | k rÁ t | | | | ƒ | | | <qN | | | | | | | | | | <qN Wq7 W| t | | ƒ f S(   sÂ  
    Performs the cholesky decomposition of a Hermitian matrix and
    returns L and it's conjugate transpose.

    Examples
    ========

    >>> from sympy.matrices.densesolve import cholesky
    >>> from sympy import QQ
    >>> cholesky([[QQ(25), QQ(15), QQ(-5)], [QQ(15), QQ(18), QQ(0)], [QQ(-5), QQ(0), QQ(11)]], QQ)
    ([[5, 0, 0], [3, 3, 0], [-1, 1, 3]], [[5, 3, -1], [0, 3, 1], [0, 0, 3]])

    See Also
    ========

    cholesky_solve
    i   (   R   R   R   R   R    t   zeroR   R   (	   R   R   t   new_matlistR   t   LR   R   t   at   k(    (    s8   lib/python2.7/site-packages/sympy/matrices/densesolve.pyt   choleskyx   s    	"#.c   
      C   s  t  j |  ƒ } t | ƒ } t | | ƒ t | | ƒ } } xÈ t | ƒ D]º } x± t | d ƒ D]Ÿ } | j } x> t | ƒ D]0 }	 | | | |	 | | |	 | |	 |	 7} qz W| | k r× | | | | | | | <q^ | | | | | | | | | | <q^ WqG W| | t | | ƒ f S(   só  
    Performs the LDL decomposition of a hermitian matrix and returns L, D and
    transpose of L. Only applicable to rational entries.

    Examples
    ========

    >>> from sympy.matrices.densesolve import LDL
    >>> from sympy import QQ

    >>> a = [
    ... [QQ(4), QQ(12), QQ(-16)],
    ... [QQ(12), QQ(37), QQ(-43)],
    ... [QQ(-16), QQ(-43), QQ(98)]]
    >>> LDL(a, QQ)
    ([[1, 0, 0], [3, 1, 0], [-4, 5, 1]], [[4, 0, 0], [0, 1, 0], [0, 0, 9]], [[1, 3, -4], [0, 1, 5], [0, 0, 1]])

    i   (   R   R   R   R   R    R   R   (
   R   R   R   R   R   t   DR   R   R    R!   (    (    s8   lib/python2.7/site-packages/sympy/matrices/densesolve.pyt   LDL™   s    	..c         C   s(   t  j |  ƒ } t | | ƒ \ } } | S(   sÐ  
    Transforms a given matrix to an upper triangle matrix by performing
    row operations on it.

    Examples
    ========

    >>> from sympy.matrices.densesolve import upper_triangle
    >>> from sympy import QQ
    >>> a = [
    ... [QQ(4,1), QQ(12,1), QQ(-16,1)],
    ... [QQ(12,1), QQ(37,1), QQ(-43,1)],
    ... [QQ(-16,1), QQ(-43,1), QQ(98,1)]]
    >>> upper_triangle(a, QQ)
    [[4, 12, -16], [0, 1, 5], [0, 0, 9]]

    See Also
    ========

    LU
    (   R   R   R   (   R   R   t   copy_matlistt   lower_trianglet   upper_triangle(    (    s8   lib/python2.7/site-packages/sympy/matrices/densesolve.pyR'   »   s    c         C   s.   t  j |  ƒ } t | | d d ƒ\ } } | S(   sË  
    Transforms a given matrix to a lower triangle matrix by performing
    row operations on it.

    Examples
    ========

    >>> from sympy.matrices.densesolve import lower_triangle
    >>> from sympy import QQ
    >>> a = [
    ... [QQ(4,1), QQ(12,1), QQ(-16)],
    ... [QQ(12,1), QQ(37,1), QQ(-43,1)],
    ... [QQ(-16,1), QQ(-43,1), QQ(98,1)]]
    >>> lower_triangle(a, QQ)
    [[1, 0, 0], [3, 1, 0], [-4, 5, 1]]

    See Also
    ========

    LU
    R   i   (   R   R   R   (   R   R   R%   R&   R'   (    (    s8   lib/python2.7/site-packages/sympy/matrices/densesolve.pyR&   Ö   s    c         C   s=   t  j |  ƒ } t | | | ƒ } t | | ƒ } t | d ƒ S(   sø  
    Solves a system of equations using reduced row echelon form given
    a matrix of coefficients, a vector of variables and a vector of constants.

    Examples
    ========

    >>> from sympy.matrices.densesolve import rref_solve
    >>> from sympy import QQ
    >>> from sympy import Dummy
    >>> x, y, z = Dummy('x'), Dummy('y'), Dummy('z')
    >>> coefficients = [
    ... [QQ(25), QQ(15), QQ(-5)],
    ... [QQ(15), QQ(18), QQ(0)],
    ... [QQ(-5), QQ(0), QQ(11)]]
    >>> constants = [
    ... [QQ(2)],
    ... [QQ(3)],
    ... [QQ(1)]]
    >>> variables = [
    ... [x],
    ... [y],
    ... [z]]
    >>> rref_solve(coefficients, variables, constants, QQ)
    [[-1/225], [23/135], [4/45]]

    See Also
    ========

    row_echelon
    augment
    iÿÿÿÿ(   R   R   R   R   R   (   R   t   variablet   constantR   R   t	   augmentedt   solution(    (    s8   lib/python2.7/site-packages/sympy/matrices/densesolve.pyt
   rref_solveñ   s    !c   
      C   s€   t  j |  ƒ } t | ƒ } t | | ƒ \ } } g  t d | ƒ D] } | g ^ qA }	 t | |	 | | ƒ t | | |	 | ƒ | S(   sÿ  
    Solves a system of equations using  LU decomposition given a matrix
    of coefficients, a vector of variables and a vector of constants.

    Examples
    ========

    >>> from sympy.matrices.densesolve import LU_solve
    >>> from sympy import QQ
    >>> from sympy import Dummy
    >>> x, y, z = Dummy('x'), Dummy('y'), Dummy('z')
    >>> coefficients = [
    ... [QQ(2), QQ(-1), QQ(-2)],
    ... [QQ(-4), QQ(6), QQ(3)],
    ... [QQ(-4), QQ(-2), QQ(8)]]
    >>> variables = [
    ... [x],
    ... [y],
    ... [z]]
    >>> constants = [
    ... [QQ(-1)],
    ... [QQ(13)],
    ... [QQ(-6)]]
    >>> LU_solve(coefficients, variables, constants, QQ)
    [[2], [3], [1]]

    See Also
    ========

    LU
    forward_substitution
    backward_substitution
    s   y:%i(   R   R   R   R   R   t   forward_substitutiont   backward_substitution(
   R   R(   R)   R   R   R   R   t   UR   t   y(    (    s8   lib/python2.7/site-packages/sympy/matrices/densesolve.pyt   LU_solve  s    "&c   
      C   s€   t  j |  ƒ } t | ƒ } t | | ƒ \ } } g  t d | ƒ D] } | g ^ qA }	 t | |	 | | ƒ t | | |	 | ƒ | S(   s}  
    Solves a system of equations using Cholesky decomposition given
    a matrix of coefficients, a vector of variables and a vector of constants.

    Examples
    ========

    >>> from sympy.matrices.densesolve import cholesky_solve
    >>> from sympy import QQ
    >>> from sympy import Dummy
    >>> x, y, z = Dummy('x'), Dummy('y'), Dummy('z')
    >>> coefficients = [
    ... [QQ(25), QQ(15), QQ(-5)],
    ... [QQ(15), QQ(18), QQ(0)],
    ... [QQ(-5), QQ(0), QQ(11)]]
    >>> variables = [
    ... [x],
    ... [y],
    ... [z]]
    >>> coefficients = [
    ... [QQ(2)],
    ... [QQ(3)],
    ... [QQ(1)]]
    >>> cholesky_solve([[QQ(25), QQ(15), QQ(-5)], [QQ(15), QQ(18), QQ(0)], [QQ(-5), QQ(0), QQ(11)]], [[x], [y], [z]], [[QQ(2)], [QQ(3)], [QQ(1)]], QQ)
    [[-1/225], [23/135], [4/45]]

    See Also
    ========

    cholesky
    forward_substitution
    backward_substitution
    s   y:%i(   R   R   R   R"   R   R-   R.   (
   R   R(   R)   R   R   R   R   t   LstarR   R0   (    (    s8   lib/python2.7/site-packages/sympy/matrices/densesolve.pyt   cholesky_solveC  s    "&c   
      C   s    t  j |  ƒ } t | ƒ } g  } xx t | ƒ D]j } | j } x2 t | ƒ D]$ }	 | | | |	 | |	 d 7} qJ W| | d | | | | | | d <q. W| S(   sÑ  
    Performs forward substitution given a lower triangular matrix, a
    vector of variables and a vector of constants.

    Examples
    ========

    >>> from sympy.matrices.densesolve import forward_substitution
    >>> from sympy import QQ
    >>> from sympy import Dummy
    >>> x, y, z = Dummy('x'), Dummy('y'), Dummy('z')
    >>> a = [
    ... [QQ(1), QQ(0), QQ(0)],
    ... [QQ(-2), QQ(1), QQ(0)],
    ... [QQ(-2), QQ(-1), QQ(1)]]
    >>> variables = [
    ... [x],
    ... [y],
    ... [z]]
    >>> constants = [
    ... [QQ(-1)],
    ... [QQ(13)],
    ... [QQ(-6)]]
    >>> forward_substitution(a, variables, constants, QQ)
    [[-1], [11], [3]]

    See Also
    ========

    LU_solve
    cholesky_solve
    i    (   R   R   R   R    R   (
   R&   R(   R)   R   t   copy_lower_triangleR   t   resultR   R    R   (    (    s8   lib/python2.7/site-packages/sympy/matrices/densesolve.pyR-   n  s    !	"*c   
      C   s³   t  j |  ƒ } t | ƒ } g  } x‹ t t | ƒ ƒ D]w } | j } x? t t | d | ƒ ƒ D]$ }	 | | | |	 | |	 d 7} q] W| | d | | | | | | d <q4 W| S(   sÍ  
    Performs forward substitution given a lower triangular matrix,
    a vector of variables and a vector constants.

    Examples
    ========

    >>> from sympy.matrices.densesolve import backward_substitution
    >>> from sympy import QQ
    >>> from sympy import Dummy
    >>> x, y, z = Dummy('x'), Dummy('y'), Dummy('z')
    >>> a = [
    ... [QQ(2), QQ(-1), QQ(-2)],
    ... [QQ(0), QQ(4), QQ(-1)],
    ... [QQ(0), QQ(0), QQ(3)]]
    >>> variables = [
    ... [x],
    ... [y],
    ... [z]]
    >>> constants = [
    ... [QQ(-1)],
    ... [QQ(11)],
    ... [QQ(3)]]
    >>> backward_substitution(a, variables, constants, QQ)
    [[2], [3], [1]]

    See Also
    ========

    LU_solve
    cholesky_solve
    i   i    (   R   R   R   t   reversedR    R   (
   R'   R(   R)   R   t   copy_upper_triangleR   R5   R   R    R   (    (    s8   lib/python2.7/site-packages/sympy/matrices/densesolve.pyR.   š  s    !	 "*(   t   __doc__R   t   sympy.core.compatibilityR    t   sympy.core.powerR   t   sympy.core.symbolR   t   sympy.matrices.densetoolsR   R   R   R   R   R   t   sympy.utilities.exceptionsR	   t   warnR   R   R   R"   R$   R'   R&   R,   R1   R3   R-   R.   (    (    (    s8   lib/python2.7/site-packages/sympy/matrices/densesolve.pyt   <module>   s,   .	!	!	!	"			'	+	+	,