ó
~9­\c           @  s  d  d l  m Z m Z d  d l Z d  d l m Z d  d l m Z m Z m	 Z	 m
 Z
 d  d l m Z d  d l m Z d  d l m Z d  d l m Z d  d	 l m Z d  d
 l m Z d d l m Z d d l m Z d d l m Z m Z d e f d „  ƒ  YZ d e e f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   divisiont   print_functionN(   t   defaultdict(   t   Callablet   as_intt   is_sequencet   range(   t   Dict(   t   Expr(   t   S(   t   Abs(   t   sqrt(   t   uniqi   (   t   a2idx(   t   Matrix(   t
   MatrixBaset
   ShapeErrort   SparseMatrixc           B  sÓ  e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z d „  Z d „  Z d „  Z e d „  ƒ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z e d „  ƒ Z d „  Z d „  Z d „  Z e d „  ƒ Z  d „  Z! d „  Z" d „  Z# d  „  Z$ d! „  Z% d" „  Z& d# „  Z' d$ „  Z( d% „  Z) d& „  Z* d' „  Z+ d( „  Z, d) „  Z- d* d+ „ Z. d* d, „ Z/ e e+ d. d. d- ƒ Z1 e e& d. d. d- ƒ Z2 RS(/   sj  
    A sparse matrix (a matrix with a large number of zero elements).

    Examples
    ========

    >>> from sympy.matrices import SparseMatrix
    >>> SparseMatrix(2, 2, range(4))
    Matrix([
    [0, 1],
    [2, 3]])
    >>> SparseMatrix(2, 2, {(1, 1): 2})
    Matrix([
    [0, 0],
    [0, 2]])

    See Also
    ========
    sympy.matrices.dense.Matrix
    c         O  s  t  j |  ƒ } t | ƒ d k rn t | d t ƒ rn | d j | _ | d j | _ t | d j ƒ | _ | Si  | _ t | ƒ d k rˆt	 | d ƒ | _ t	 | d ƒ | _ t | d t
 ƒ rI| d } x¶t | j ƒ D]f } x] t | j ƒ D]L } | j | | j | ƒ | j | ƒ ƒ ƒ } | rò | | j | | f <qò qò WqÜ Wqt | d t t f ƒ r­x | d j ƒ  D]3 } | d | }	 |	 rs| j |	 ƒ | j | <qsqsWqt | d ƒ rt | d ƒ | j | j k r
t d t | d ƒ | j | j f ƒ ‚ n  | d }
 xn t | j ƒ D]Z } xQ t | j ƒ D]@ } | j |
 | | j | ƒ } | r:| | j | | f <q:q:Wq$Wqn t j | Œ  \ } } } | | _ | | _ xb t | j ƒ D]Q } xH t | j ƒ D]7 } | | j | | } | rØ| | j | | f <qØqØWqÂW| S(   Ni   i    i   i   s%   List length (%s) != rows*columns (%s)(   t   objectt   __new__t   lent
   isinstanceR   t   rowst   colst   dictt   _smatR   R   R   t   _sympifyR   t   keysR   t
   ValueErrorR   t   _handle_creation_inputs(   t   clst   argst   kwargst   selft   opt   it   jt   valuet   keyt   vt	   flat_listt   rt   ct   _list(    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyR   )   sV    %	
$!  '
$		c         C  s’   t  |  d d  ƒ } t  | d d  ƒ } d  | | f k r: t S| | k rJ t St | t ƒ ri |  j | j k St | t ƒ rŽ |  j t | ƒ j k Sd  S(   Nt   shape(   t   getattrt   Nonet   FalseR   R   R   R   t   MutableSparseMatrix(   R!   t   othert
   self_shapet   other_shape(    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   __eq__\   s    c   
      C  s€  t  | t ƒ r®| \ } } y5 |  j | ƒ \ } } |  j j | | f t j ƒ SWq®t t f k
 rªt  | t	 ƒ r‘ t
 t |  j ƒ ƒ | } no t | ƒ r  n` t  | t ƒ rÙ | j rÙ d d l m } | |  | | ƒ S| |  j k r÷ t d ƒ ‚ n  | g } t  | t	 ƒ r+t
 t |  j ƒ ƒ | } no t | ƒ r:n` t  | t ƒ rs| j rsd d l m } | |  | | ƒ S| |  j k r‘t d ƒ ‚ n  | g } |  j | | ƒ SXn  t  | t	 ƒ r=| j t |  ƒ ƒ d  \ } } g  } xT t | | ƒ D]C } t | |  j ƒ \ } }	 | j |  j j | |	 f t j ƒ ƒ qòW| St t | t |  ƒ ƒ |  j ƒ \ } } |  j j | | f t j ƒ S(   Niÿÿÿÿ(   t   MatrixElements   Row index out of boundss   Col index out of boundsi   (   R   t   tuplet   key2ijR   t   getR	   t   Zerot	   TypeErrort
   IndexErrort   slicet   listR   R   R   R   t	   is_numbert"   sympy.matrices.expressions.matexprR5   R   t   extractt   indicesR   t   divmodt   appendR   (
   R!   R&   R#   R$   R5   t   lot   hit   Lt   mt   n(    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   __getitem__h   sH     		)'c         C  s   t  ƒ  ‚ d  S(   N(   t   NotImplementedError(   R!   R&   R%   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   __setitem__–   s    c         C  s1   |  j  ƒ  } | j | ƒ } | j j | ƒ } | S(   N(   t   _cholesky_sparset   _lower_triangular_solvet   Tt   _upper_triangular_solve(   R!   t   rhsRF   t   Yt   rv(    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   _cholesky_solve™   s    c   	   	   C  sÇ  |  j  ƒ  } |  j |  j ƒ } x¢t t | ƒ ƒ D]Ž} x…| | D]y} | | k r(|  | | f | | | f <d } xx | | D]l } | | k  r xW | | D]G } | | k  râ | | k rã | | | | f | | | f 7} qã qœ Pqœ WPq q W| | | f c | 8<| | | f c | | | f <qB |  | | f | | | f <d } x: | | D]. } | | k  r€| | | | f d 7} qSPqSW| | | f c | 8<t | | | f ƒ | | | f <qB Wq1 W| S(   s@   Algorithm for numeric Cholesky factorization of a sparse matrix.i    i   (   t   row_structure_symbolic_choleskyt   zerosR   R   R   R   (	   R!   t	   Crowstruct   CR#   R$   t   summt   p1t   p2t   k(    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyRL   ¤   s4    (#(c           s"   ˆ j  ˆ j d ‡  ‡ f d †  ƒ S(   s   Diagonal solve.i   c           s   ˆ  |  d f ˆ |  |  f S(   Ni    (    (   R#   R$   (   RP   R!   (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   <lambda>È   t    (   t   _newR   (   R!   RP   (    (   RP   R!   s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   _diagonal_solveÆ   s    c         K  s7  |  j  ƒ  } |  j ƒ  } | j | j ƒ } | sf | j } | d d d … f } | | } | | } n  | j d d ƒ } | d k r | j } n( | d k r¨ | j } n t d | ƒ ‚ | j	 g  t
 | j ƒ D]" }	 | | d d … |	 f ƒ ^ qÎ Œ  }
 | s*| |
 d d … d f d } |
 | }
 n  |  j |
 ƒ S(   sÚ  Return the matrix inverse using Cholesky or LDL (default)
        decomposition as selected with the ``method`` keyword: 'CH' or 'LDL',
        respectively.

        Examples
        ========

        >>> from sympy import SparseMatrix, Matrix
        >>> A = SparseMatrix([
        ... [ 2, -1,  0],
        ... [-1,  2, -1],
        ... [ 0,  0,  2]])
        >>> A.inv('CH')
        Matrix([
        [2/3, 1/3, 1/6],
        [1/3, 2/3, 1/3],
        [  0,   0, 1/2]])
        >>> A.inv(method='LDL') # use of 'method=' is optional
        Matrix([
        [2/3, 1/3, 1/6],
        [1/3, 2/3, 1/3],
        [  0,   0, 1/2]])
        >>> A * _
        Matrix([
        [1, 0, 0],
        [0, 1, 0],
        [0, 0, 1]])

        i    Nt   methodt   LDLt   CHs$   Method may be "CH" or "LDL", not %s.(   i    i    (   t   is_symmetrict
   as_mutablet   eyeR   RN   R8   t
   _LDL_solveRS   RJ   t   hstackR   R   R^   (   R!   R    t   symt   Mt   It   tt   r1R`   t   solveR#   RR   t   scale(    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   _eval_inverseÊ   s(    	
Ac         C  s   |  j  d „  ƒ S(   Nc         S  s
   t  |  ƒ S(   N(   R
   (   t   x(    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyR\   ÿ   R]   (   t	   applyfunc(   R!   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt	   _eval_Absþ   s    c         C  sÁ   t  | t ƒ s  |  |  j | ƒ Si  } |  j d ƒ } xp t ƒ  j |  j j ƒ  | j j ƒ  ƒ D]G } |  j j | | ƒ | j j | | ƒ } | d k r] | | | <q] q] W|  j |  j	 |  j
 | ƒ S(   sW   If `other` is a SparseMatrix, add efficiently. Otherwise,
        do standard addition.i    (   R   R   R^   R   t   sett   unionR   R   R8   R   R   (   R!   R1   t   smatt   zeroR&   t   sum(    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt	   _eval_add  s    .(c         C  sÜ   t  | t ƒ s t | ƒ } n  i  } xU |  j j ƒ  D]D \ } } | \ } } | | k rh | | j 7} n  | | | | f <q4 Wx= | j j ƒ  D], \ } } | \ } } | | | | | f <qŒ W|  j |  j |  j | j | ƒ S(   N(   R   R   R   t   itemsR   R^   R   (   R!   t   icolR1   t   new_smatR&   t   valt   rowt   col(    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   _eval_col_insert  s    c         C  s2   d „  |  j  j ƒ  Dƒ } |  j |  j |  j | ƒ S(   Nc         S  s%   i  |  ] \ } } | j  ƒ  | “ q S(    (   t	   conjugate(   t   .0R&   R|   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pys
   <dictcomp>   s   	 (   R   Ry   R^   R   R   (   R!   Ru   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   _eval_conjugate  s    c         C  s  t  t | ƒ ƒ } t  t | ƒ ƒ } i  } t | ƒ t | ƒ t |  j ƒ k  r± xÃ t | ƒ D]N \ } } x? t | ƒ D]1 \ } }	 |  j j | |	 f d ƒ | | | f <qu Wq\ Wnd xa |  j D]V \ }
 } |
 | k r» | | k r» |  j |
 | f | | j |
 ƒ | j | ƒ f <q» q» W|  j t | ƒ t | ƒ | ƒ } t | ƒ t | ƒ k r§xV t | ƒ D]E \ } } | j | ƒ } | | k r[| j | | j	 | ƒ ƒ } q[q[Wn  t | ƒ t | ƒ k rxV t | ƒ D]E \ } }	 | j |	 ƒ } | | k rÌ| j
 | | j | ƒ ƒ } qÌqÌWn  | S(   Ni    (   R=   R   R   R   t	   enumerateR8   t   indexR^   t
   row_insertR}   t
   col_insertR~   (   R!   t   rowsListt   colsListt   urowt   ucolRu   R#   R)   R$   R*   t   rkt   ckRR   t
   i_previous(    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   _eval_extract#  s,    %06!%%c         C  s2   d „  t  t | | ƒ ƒ Dƒ } |  j | | | ƒ S(   Nc         S  s"   i  |  ] } t  j | | f “ q S(    (   R	   t   One(   R   R#   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pys
   <dictcomp>D  s   	 (   R   t   minR^   (   R   R   R   t   entries(    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt	   _eval_eyeB  s    c           s`   t  j j ˆ  Œ  } t ˆ j ƒ ˆ j ˆ j k r: t } n  t ‡  ‡ f d †  ˆ j Dƒ ƒ p_ | S(   Nc         3  s"   |  ] } ˆ | j  ˆ  Œ  Vq d  S(   N(   t   has(   R   R&   (   t   patternsR!   (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pys	   <genexpr>N  s    (	   R	   R9   R“   R   R   R   R   R/   t   any(   R!   R”   t   zhas(    (   R”   R!   s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt	   _eval_hasG  s    	c           s?   t  ‡  f d †  t ˆ  j ƒ Dƒ ƒ s) t St ˆ  j ƒ ˆ  j k S(   Nc         3  s%   |  ] } ˆ  | | f d  k Vq d S(   i   N(    (   R   R#   (   R!   (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pys	   <genexpr>Q  s    (   t   allR   R   R/   R   R   (   R!   (    (   R!   s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   _eval_is_IdentityP  s    %c         C  s,   |  |  j  j | ƒ } t | j ƒ  ƒ d k S(   Ni    (   RN   Rq   R   t   values(   R!   t   simpfunct   diff(    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   _eval_is_symmetricU  s    c           sU  t  | t ƒ s  |  |  j | ƒ St t ƒ ‰ x1 |  j j ƒ  D]  \ \ } } } | ˆ | | <q< Wt t ƒ ‰ x1 | j j ƒ  D]  \ \ } } } | ˆ | | <q| Wi  } x“ ˆ j ƒ  D]… ‰ x| ˆ j ƒ  D]n ‰  t ˆ ˆ  j ƒ  ƒ t ˆ ˆ j ƒ  ƒ @} | rÆ t	 ‡  ‡ ‡ ‡ f d †  | Dƒ ƒ } | | ˆ ˆ  f <qÆ qÆ Wq³ W|  j |  j
 | j | ƒ S(   s:   Fast multiplication exploiting the sparsity of the matrix.c         3  s)   |  ] } ˆ ˆ | ˆ ˆ  | Vq d  S(   N(    (   R   R[   (   R~   t
   col_lookupR}   t
   row_lookup(    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pys	   <genexpr>n  s    (   R   R   R^   R   R   R   Ry   R   Rs   Rw   R   R   (   R!   R1   R#   R$   R|   Ru   RA   (    (   R~   Rž   R}   RŸ   s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   _eval_matrix_mulY  s     ""*%c         C  sÜ   t  | t ƒ s t | ƒ } n  i  } xU |  j j ƒ  D]D \ } } | \ } } | | k rh | | j 7} n  | | | | f <q4 Wx= | j j ƒ  D], \ } } | \ } } | | | | | f <qŒ W|  j |  j | j |  j | ƒ S(   N(   R   R   R   Ry   R   R^   R   (   R!   t   irowR1   R{   R&   R|   R}   R~   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   _eval_row_insertr  s    c           s   |  j  ‡  f d †  ƒ S(   Nc           s   |  ˆ  S(   N(    (   Rp   (   R1   (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyR\   ƒ  R]   (   Rq   (   R!   R1   (    (   R1   s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   _eval_scalar_mul‚  s    c           s   |  j  ‡  f d †  ƒ S(   Nc           s   ˆ  |  S(   N(    (   Rp   (   R1   (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyR\   †  R]   (   Rq   (   R!   R1   (    (   R1   s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   _eval_scalar_rmul…  s    c         C  s2   d „  |  j  j ƒ  Dƒ } |  j |  j |  j | ƒ S(   sK  Returns the transposed SparseMatrix of this SparseMatrix.

        Examples
        ========

        >>> from sympy.matrices import SparseMatrix
        >>> a = SparseMatrix(((1, 2), (3, 4)))
        >>> a
        Matrix([
        [1, 2],
        [3, 4]])
        >>> a.T
        Matrix([
        [1, 3],
        [2, 4]])
        c         S  s+   i  |  ]! \ \ } } } | | | f “ q S(    (    (   R   R#   R$   R|   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pys
   <dictcomp>™  s   	 (   R   Ry   R^   R   R   (   R!   Ru   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   _eval_transposeˆ  s    c         C  s/   g  |  j  j ƒ  D] \ } } | j s | ^ q S(   N(   R   Ry   t   is_zero(   R!   R[   R'   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   _eval_valuesœ  s    c         C  s   |  j  | | i  ƒ S(   N(   R^   (   R   R   R   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   _eval_zerosŸ  s    c         C  s@   |  j  ƒ  \ } } | j | ƒ } | j | ƒ } | j j | ƒ S(   N(   t   _LDL_sparseRM   R_   RN   RO   (   R!   RP   RF   t   Dt   ZRQ   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyRf   £  s    c   
   	   C  sð  |  j  ƒ  } |  j |  j ƒ } |  j |  j |  j ƒ } x­t t | ƒ ƒ D]™} x| | D]„} | | k rN|  | | f | | | f <d } x† | | D]z } | | k  rxe | | D]U } | | k  r| | k r	| | | | f | | | f | | | f 7} q	q´ Pq´ Wq— Pq— W| | | f c | 8<| | | f c | | | f <qZ | | k rZ |  | | f | | | f <d } xH | | D]< }	 |	 | k  rÀ| | | |	 f d | |	 |	 f 7} q…Pq…W| | | f c | 8<qZ qZ WqI W| | f S(   sL   Algorithm for numeric LDL factization, exploiting sparse structure.
        i    i   (   RT   Re   R   RU   R   R   R   (
   R!   t	   LrowstrucRF   Rª   R#   R$   RX   RY   RZ   R[   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyR©   ®  s6    6#)!c      	   C  sú   g  t  |  j ƒ D] } g  ^ q } xC |  j ƒ  D]5 \ } } } | | k r/ | | j | | f ƒ q/ q/ W| j ƒ  } xv t  |  j ƒ D]e } x< | | D]0 \ } } | | d f c | | | d f 8<q• W| | d f c |  | | f <q„ W|  j | ƒ S(   ss   Fast algorithm for solving a lower-triangular system,
        exploiting the sparsity of the given matrix.
        i    (   R   R   t   row_listRC   t   copyR^   (   R!   RP   R#   R   R$   R'   t   X(    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyRM   Ò  s    "($c         C  s
   t  |  ƒ S(   ss   Return a list of matrix elements.  Some routines
        in DenseMatrix use `_mat` directly to speed up operations.(   R=   (   R!   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   _matá  s    c      	   C  s  g  t  |  j ƒ D] } g  ^ q } xC |  j ƒ  D]5 \ } } } | | k  r/ | | j | | f ƒ q/ q/ W| j ƒ  } xŽ t  |  j d d d ƒ D]s } | | j ƒ  x< | | D]0 \ } } | | d f c | | | d f 8<q­ W| | d f c |  | | f <qŽ W|  j | ƒ S(   st   Fast algorithm for solving an upper-triangular system,
        exploiting the sparsity of the given matrix.
        i   iÿÿÿÿi    (   R   R   R­   RC   R®   t   reverseR^   (   R!   RP   R#   R   R$   R'   R¯   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyRO   ç  s    " ($c         C  s€   t  | ƒ s t d ƒ ‚ n  |  j ƒ  } xR |  j j ƒ  D]A \ } } | | ƒ } | re | | j | <q7 | j j | d ƒ q7 W| S(   sa  Apply a function to each element of the matrix.

        Examples
        ========

        >>> from sympy.matrices import SparseMatrix
        >>> m = SparseMatrix(2, 2, lambda i, j: i*2+j)
        >>> m
        Matrix([
        [0, 1],
        [2, 3]])
        >>> m.applyfunc(lambda i: 2*i)
        Matrix([
        [0, 2],
        [4, 6]])

        s   `f` must be callable.N(   t   callableR:   R®   R   Ry   t   popR.   (   R!   t   ft   outR[   R'   t   fv(    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyRq   ø  s    c         C  s   d d l  m } | |  ƒ S(   s,   Returns an Immutable version of this Matrix.i   (   t   ImmutableSparseMatrix(   t	   immutableR·   (   R!   R·   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   as_immutable  s    c         C  s
   t  |  ƒ S(   sC  Returns a mutable version of this matrix.

        Examples
        ========

        >>> from sympy import ImmutableMatrix
        >>> X = ImmutableMatrix([[1, 2], [3, 4]])
        >>> Y = X.as_mutable()
        >>> Y[1, 1] = 5 # Can set values in Y
        >>> Y
        Matrix([
        [1, 2],
        [3, 5]])
        (   R0   (   R!   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyRd     s    c         C  s}   d d l  m } m } |  j ƒ  s1 t d ƒ ‚ n  |  j ƒ  j ƒ  } | j | ƒ sa | j | ƒ rp t d ƒ ‚ n  |  j | ƒ S(   sö  
        Returns the Cholesky decomposition L of a matrix A
        such that L * L.T = A

        A must be a square, symmetric, positive-definite
        and non-singular matrix

        Examples
        ========

        >>> from sympy.matrices import SparseMatrix
        >>> A = SparseMatrix(((25,15,-5),(15,18,0),(-5,0,11)))
        >>> A.cholesky()
        Matrix([
        [ 5, 0, 0],
        [ 3, 3, 0],
        [-1, 1, 3]])
        >>> A.cholesky() * A.cholesky().T == A
        True
        iÿÿÿÿ(   t   nant   oos:   Cholesky decomposition applies only to symmetric matrices.sA   Cholesky decomposition applies only to positive-definite matrices(	   t   sympy.core.numbersRº   R»   Rc   R   Rd   RL   R“   R^   (   R!   Rº   R»   Ri   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   cholesky,  s    c         C  sF   g  t  t |  j j ƒ  ƒ d d „  ƒD] } t | |  | f ƒ ^ q% S(   s—  Returns a column-sorted list of non-zero elements of the matrix.

        Examples
        ========

        >>> from sympy.matrices import SparseMatrix
        >>> a=SparseMatrix(((1, 2), (3, 4)))
        >>> a
        Matrix([
        [1, 2],
        [3, 4]])
        >>> a.CL
        [(0, 0, 1), (1, 0, 3), (0, 1, 2), (1, 1, 4)]

        See Also
        ========
        col_op
        row_list
        R&   c         S  s   t  t |  ƒ ƒ S(   N(   R=   t   reversed(   R[   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyR\   `  R]   (   t   sortedR=   R   R   R6   (   R!   R[   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   col_listL  s    c         C  s   |  j  |  j |  j |  j ƒ S(   N(   R^   R   R   R   (   R!   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyR®   b  s    c         C  s°   d d l  m } m } |  j ƒ  s1 t d ƒ ‚ n  |  j ƒ  j ƒ  \ } } | j | ƒ s… | j | ƒ s… | j | ƒ s… | j | ƒ r” t d ƒ ‚ n  |  j | ƒ |  j | ƒ f S(   sý  
        Returns the LDL Decomposition (matrices ``L`` and ``D``) of matrix
        ``A``, such that ``L * D * L.T == A``. ``A`` must be a square,
        symmetric, positive-definite and non-singular.

        This method eliminates the use of square root and ensures that all
        the diagonal entries of L are 1.

        Examples
        ========

        >>> from sympy.matrices import SparseMatrix
        >>> A = SparseMatrix(((25, 15, -5), (15, 18, 0), (-5, 0, 11)))
        >>> L, D = A.LDLdecomposition()
        >>> L
        Matrix([
        [   1,   0, 0],
        [ 3/5,   1, 0],
        [-1/5, 1/3, 1]])
        >>> D
        Matrix([
        [25, 0, 0],
        [ 0, 9, 0],
        [ 0, 0, 9]])
        >>> L * D * L.T == A
        True

        iÿÿÿÿ(   Rº   R»   s5   LDL decomposition applies only to symmetric matrices.s<   LDL decomposition applies only to positive-definite matrices(	   R¼   Rº   R»   Rc   R   Rd   R©   R“   R^   (   R!   Rº   R»   RF   Rª   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   LDLdecompositione  s    <c   	      C  s!  g  t  |  j ƒ D] } g  ^ q } x= |  j ƒ  D]/ \ } } } | | k r/ | | j | ƒ q/ q/ Wt | ƒ } | g |  j } | g |  j } x† t  |  j ƒ D]u } xl | | d  D]\ } x. | | | k  ré | | } | | | <| } q¼ W| | | k r³ | | | <| | <q³ q³ Wqž W| | f S(   s˜  Liu's algorithm, for pre-determination of the Elimination Tree of
        the given matrix, used in row-based symbolic Cholesky factorization.

        Examples
        ========

        >>> from sympy.matrices import SparseMatrix
        >>> S = SparseMatrix([
        ... [1, 0, 3, 2],
        ... [0, 0, 1, 0],
        ... [4, 0, 0, 5],
        ... [0, 6, 7, 0]])
        >>> S.liupc()
        ([[0], [], [0], [1, 2]], [4, 3, 4, 4])

        References
        ==========

        Symbolic Sparse Cholesky Factorization using Elimination Trees,
        Jeroen Van Grondelle (1999)
        http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.39.7582
        iÿÿÿÿ(   R   R   R­   RC   R   (	   R!   R)   t   RR*   t   _t   inft   parentt   virtualRk   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   liupc  s     "


c         C  s   t  |  j ƒ S(   s2   Returns the number of non-zero elements in Matrix.(   R   R   (   R!   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   nnz¹  s    c         C  sF   g  t  t |  j j ƒ  ƒ d d „  ƒD] } t | |  | f ƒ ^ q% S(   s–  Returns a row-sorted list of non-zero elements of the matrix.

        Examples
        ========

        >>> from sympy.matrices import SparseMatrix
        >>> a = SparseMatrix(((1, 2), (3, 4)))
        >>> a
        Matrix([
        [1, 2],
        [3, 4]])
        >>> a.RL
        [(0, 0, 1), (0, 1, 2), (1, 0, 3), (1, 1, 4)]

        See Also
        ========
        row_op
        col_list
        R&   c         S  s
   t  |  ƒ S(   N(   R=   (   R[   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyR\   Ò  R]   (   R¿   R=   R   R   R6   (   R!   R[   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyR­   ½  s    c         C  sº   |  j  ƒ  \ } } t | ƒ } t j | ƒ } x† t |  j ƒ D]u } xL | | D]@ } x7 | | k r | | k r | | j | ƒ | | } qW WqN Wt t t	 | | ƒ ƒ ƒ | | <q= W| S(   s  Symbolic cholesky factorization, for pre-determination of the
        non-zero structure of the Cholesky factororization.

        Examples
        ========

        >>> from sympy.matrices import SparseMatrix
        >>> S = SparseMatrix([
        ... [1, 0, 3, 2],
        ... [0, 0, 1, 0],
        ... [4, 0, 0, 5],
        ... [0, 6, 7, 0]])
        >>> S.row_structure_symbolic_cholesky()
        [[0], [], [0], [1, 2]]

        References
        ==========

        Symbolic Sparse Cholesky Factorization using Elimination Trees,
        Jeroen Van Grondelle (1999)
        http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.39.7582
        (
   RÇ   R   R®   t   deepcopyR   R   RC   R=   R¿   Rs   (   R!   RÂ   RÅ   RÄ   t   LrowR[   R$   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyRT   Ô  s    $c         C  sm   |  j  |  j Œ  } | ri xN |  j D]@ } | |  j | } | rO | | j | <q" | j j | d ƒ q" Wn  | S(   s"   Scalar element-wise multiplicationN(   RU   R,   R   R³   R.   (   R!   t   scalarRi   R#   R'   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   scalar_multiply÷  s    Ra   c         C  s%   |  j  } | |  j d | ƒ | | S(   sý  Return the least-square fit to the data.

        By default the cholesky_solve routine is used (method='CH'); other
        methods of matrix inversion can be used. To find out which are
        available, see the docstring of the .inv() method.

        Examples
        ========

        >>> from sympy.matrices import SparseMatrix, Matrix, ones
        >>> A = Matrix([1, 2, 3])
        >>> B = Matrix([2, 3, 4])
        >>> S = SparseMatrix(A.row_join(B))
        >>> S
        Matrix([
        [1, 2],
        [2, 3],
        [3, 4]])

        If each line of S represent coefficients of Ax + By
        and x and y are [2, 3] then S*xy is:

        >>> r = S*Matrix([2, 3]); r
        Matrix([
        [ 8],
        [13],
        [18]])

        But let's add 1 to the middle value and then solve for the
        least-squares value of xy:

        >>> xy = S.solve_least_squares(Matrix([8, 14, 18])); xy
        Matrix([
        [ 5/3],
        [10/3]])

        The error is given by S*xy - r:

        >>> S*xy - r
        Matrix([
        [1/3],
        [1/3],
        [1/3]])
        >>> _.norm().n(2)
        0.58

        If a different xy is used, the norm will be higher:

        >>> xy += ones(2, 1)/10
        >>> (S*xy - r).norm().n(2)
        1.5

        R`   (   RN   t   inv(   R!   RP   R`   Rk   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   solve_least_squares  s    6	c         C  sf   |  j  sN |  j |  j k  r* t d ƒ ‚ qb |  j |  j k rb t d ƒ ‚ qb n |  j d | ƒ | Sd S(   s–   Return solution to self*soln = rhs using given inversion method.

        For a list of possible inversion methods, see the .inv() docstring.
        s   Under-determined system.s]   For over-determined system, M, having more rows than columns, try M.solve_least_squares(rhs).R`   N(   t	   is_squareR   R   R   RÍ   (   R!   RP   R`   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyRm   <  s    	s   Alternate faster representationN(3   t   __name__t
   __module__t   __doc__R   R4   RI   RK   RS   RL   R_   Ro   Rr   Rx   R   R‚   RŽ   t   classmethodR’   R—   R™   R   R    R¢   R£   R¤   R¥   R§   R¨   Rf   R©   RM   t   propertyR°   RO   Rq   R¹   Rd   R½   RÀ   R®   RÁ   RÇ   RÈ   R­   RT   RÌ   RÎ   Rm   R.   t   RLt   CL(    (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyR      s\   	3		.			"		4																	$						 			(	,			#	9R0   c           B  s›   e  Z e d  „  ƒ Z d „  Z d „  Z d Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         O  s
   |  | Œ  S(   N(    (   R   R   R    (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyR^   P  s    c         C  sx   |  j  | | ƒ } | d k	 rt | \ } } } | rI | |  j | | f <qt | | f |  j k rt |  j | | f =qt n  d S(   s?  Assign value to position designated by key.

        Examples
        ========

        >>> from sympy.matrices import SparseMatrix, ones
        >>> M = SparseMatrix(2, 2, {})
        >>> M[1] = 1; M
        Matrix([
        [0, 1],
        [0, 0]])
        >>> M[1, 1] = 2; M
        Matrix([
        [0, 1],
        [0, 2]])
        >>> M = SparseMatrix(2, 2, {})
        >>> M[:, 1] = [1, 1]; M
        Matrix([
        [0, 1],
        [0, 1]])
        >>> M = SparseMatrix(2, 2, {})
        >>> M[1, :] = [[1, 1]]; M
        Matrix([
        [0, 0],
        [1, 1]])


        To replace row r you assign to position r*m where m
        is the number of columns:

        >>> M = SparseMatrix(4, 4, {})
        >>> m = M.cols
        >>> M[3*m] = ones(1, m)*2; M
        Matrix([
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [2, 2, 2, 2]])

        And to replace column c you can assign to position c:

        >>> M[2] = ones(m, 1)*4; M
        Matrix([
        [0, 0, 4, 0],
        [0, 0, 4, 0],
        [0, 0, 4, 0],
        [2, 2, 4, 2]])
        N(   t   _setitemR.   R   (   R!   R&   R%   RR   R#   R$   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyRK   T  s    1c         C  s
   |  j  ƒ  S(   N(   R®   (   R!   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyRd     s    c         C  sª   i  } t  | |  j ƒ } xs |  j D]h \ } } | | k r= q" | | k rm |  j | | f | | | d f <q" |  j | | f | | | f <q" W| |  _ |  j d 8_ d S(   s}  Delete the given column of the matrix.

        Examples
        ========

        >>> from sympy.matrices import SparseMatrix
        >>> M = SparseMatrix([[0, 0], [0, 1]])
        >>> M
        Matrix([
        [0, 0],
        [0, 1]])
        >>> M.col_del(0)
        >>> M
        Matrix([
        [0],
        [1]])

        See Also
        ========

        row_del
        i   N(   R   R   R   (   R!   R[   t   newDR#   R$   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   col_del’  s    $!	c   	      C  sY  |  j  d k r@ |  j | j k r@ |  j d | j g  ƒ j | ƒ S|  | } } | j | j k sk t ƒ  ‚ n  | j ƒ  } t | t ƒ sd } | j } x« t	 | j  ƒ D]W } xN t	 | j ƒ D]= } | | } | rî | | j
 | | j  | f <n  | d 7} q» Wq¥ Wn@ x= | j
 j ƒ  D], \ \ } } } | | j
 | | j  | f <qW| j  | j  7_  | S(   s,  Returns B augmented beneath A (row-wise joining)::

            [A]
            [B]

        Examples
        ========

        >>> from sympy import SparseMatrix, Matrix, ones
        >>> A = SparseMatrix(ones(3))
        >>> A
        Matrix([
        [1, 1, 1],
        [1, 1, 1],
        [1, 1, 1]])
        >>> B = SparseMatrix.eye(3)
        >>> B
        Matrix([
        [1, 0, 0],
        [0, 1, 0],
        [0, 0, 1]])
        >>> C = A.col_join(B); C
        Matrix([
        [1, 1, 1],
        [1, 1, 1],
        [1, 1, 1],
        [1, 0, 0],
        [0, 1, 0],
        [0, 0, 1]])
        >>> C == A.col_join(Matrix(B))
        True

        Joining along columns is the same as appending rows at the end
        of the matrix:

        >>> C == A.row_insert(A.rows, Matrix(B))
        True
        i    i   (   R   R   R^   t   col_joinR   R®   R   R   R°   R   R   Ry   (	   R!   R1   t   At   BR[   t   bR#   R$   R'   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyRÚ   µ  s&    (!	
"c         C  s†   x t  |  j ƒ D]n } |  j j | | f t j ƒ } | | | ƒ } | r_ | |  j | | f <q | r |  j j | | f ƒ q q Wd S(   s§  In-place operation on col j using two-arg functor whose args are
        interpreted as (self[i, j], i) for i in range(self.rows).

        Examples
        ========

        >>> from sympy.matrices import SparseMatrix
        >>> M = SparseMatrix.eye(3)*2
        >>> M[1, 0] = -1
        >>> M.col_op(1, lambda v, i: v + 2*M[i, 0]); M
        Matrix([
        [ 2, 4, 0],
        [-1, 0, 0],
        [ 0, 0, 2]])
        N(   R   R   R   R8   R	   R9   R³   (   R!   R$   R´   R#   R'   R¶   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   col_opó  s    c   	      C  sö   | | k r | | } } n  |  j  ƒ  } g  } x— | D] \ } } } | | k r| |  j j | | f ƒ | j | | f ƒ q5 | | k r´ |  j j | | f ƒ | |  j | | f <q5 | | k r5 Pq5 q5 Wx' | D] \ } } | |  j | | f <qÏ Wd S(   s  Swap, in place, columns i and j.

        Examples
        ========

        >>> from sympy.matrices import SparseMatrix
        >>> S = SparseMatrix.eye(3); S[2, 1] = 2
        >>> S.col_swap(1, 0); S
        Matrix([
        [0, 1, 0],
        [1, 0, 0],
        [2, 0, 1]])
        N(   RÀ   R   R³   RC   (	   R!   R#   R$   R   t   tempt   iit   jjR'   R[   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   col_swap  s    c         C  s5   t  | ƒ s t d ƒ ‚ n  |  j | t | ƒ ƒ d  S(   Ns&   `value` must be of type list or tuple.(   R   R:   t   copyin_matrixR   (   R!   R&   R%   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   copyin_list)  s    c         C  sö  |  j  | ƒ \ } } } } | j } | | | | } }	 | | |	 f k rZ t d ƒ ‚ n  t | t ƒ sÂ x†t | j ƒ D]B }
 x9 t | j ƒ D]( } | |
 | f |  |
 | | | f <q Wqy Wn0| | | | t |  ƒ k  r0xÁ t | | ƒ D]9 }
 x0 t | | ƒ D] } |  j	 j
 |
 | f d  ƒ qWqð Wnt xq |  j ƒ  D]c \ }
 } } | |
 k oc| k  n r=| | k o| k  n r=|  j	 j
 |
 | f d  ƒ q=q=WxK | j	 j ƒ  D]: \ } } | \ }
 } | |
 | f |  |
 | | | f <q´Wd  S(   NsX   The Matrix `value` doesn't have the same dimensions as the in sub-Matrix given by `key`.(   t
   key2boundsR,   R   R   R   R   R   R   R   R   R³   R.   R­   Ry   (   R!   R&   R%   t   rlot   rhit   clot   chiR,   t   drt   dcR#   R$   R'   R[   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyRã   .  s(    	-$8 c           sJ   | s i  ˆ  _  n4 ˆ  j | ƒ ‰ ‡  ‡ f d †  t ˆ  j ƒ Dƒ ˆ  _  d S(   s  Fill self with the given value.

        Notes
        =====

        Unless many values are going to be deleted (i.e. set to zero)
        this will create a matrix that is slower than a dense matrix in
        operations.

        Examples
        ========

        >>> from sympy.matrices import SparseMatrix
        >>> M = SparseMatrix.zeros(3); M
        Matrix([
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]])
        >>> M.fill(1); M
        Matrix([
        [1, 1, 1],
        [1, 1, 1],
        [1, 1, 1]])
        c           s5   i  |  ]+ } t  ˆ  j ƒ D] } ˆ | | f “ q q S(    (   R   R   (   R   R#   R$   (   R!   R'   (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pys
   <dictcomp>e  s   	N(   R   R   R   R   (   R!   R%   (    (   R!   R'   s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   fillH  s
    c         C  sª   i  } t  | |  j ƒ } xs |  j D]h \ } } | | k r= q" | | k rm |  j | | f | | d | f <q" |  j | | f | | | f <q" W| |  _ |  j d 8_ d S(   sg  Delete the given row of the matrix.

        Examples
        ========

        >>> from sympy.matrices import SparseMatrix
        >>> M = SparseMatrix([[0, 0], [0, 1]])
        >>> M
        Matrix([
        [0, 0],
        [0, 1]])
        >>> M.row_del(0)
        >>> M
        Matrix([[0, 1]])

        See Also
        ========

        col_del
        i   N(   R   R   R   (   R!   R[   RØ   R#   R$   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   row_delh  s    $!	c   	      C  sY  |  j  d k r@ |  j | j k r@ |  j | j d g  ƒ j | ƒ S|  | } } | j | j k sk t ƒ  ‚ n  | j ƒ  } t | t ƒ sd } | j } x« t	 | j ƒ D]W } xN t	 | j  ƒ D]= } | | } | rî | | j
 | | | j  f <n  | d 7} q» Wq¥ Wn@ x= | j
 j ƒ  D], \ \ } } } | | j
 | | | j  f <qW| j  | j  7_  | S(   s,  Returns B appended after A (column-wise augmenting)::

            [A B]

        Examples
        ========

        >>> from sympy import SparseMatrix, Matrix
        >>> A = SparseMatrix(((1, 0, 1), (0, 1, 0), (1, 1, 0)))
        >>> A
        Matrix([
        [1, 0, 1],
        [0, 1, 0],
        [1, 1, 0]])
        >>> B = SparseMatrix(((1, 0, 0), (0, 1, 0), (0, 0, 1)))
        >>> B
        Matrix([
        [1, 0, 0],
        [0, 1, 0],
        [0, 0, 1]])
        >>> C = A.row_join(B); C
        Matrix([
        [1, 0, 1, 1, 0, 0],
        [0, 1, 0, 0, 1, 0],
        [1, 1, 0, 0, 0, 1]])
        >>> C == A.row_join(Matrix(B))
        True

        Joining at row ends is the same as appending columns at the end
        of the matrix:

        >>> C == A.col_insert(A.cols, B)
        True
        i    i   (   R   R   R^   t   row_joinR   R®   R   R   R°   R   R   Ry   (	   R!   R1   RÛ   RÜ   R[   RÝ   R#   R$   R'   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyRî   ‰  s&    $!	
"c         C  s†   x t  |  j ƒ D]n } |  j j | | f t j ƒ } | | | ƒ } | r_ | |  j | | f <q | r |  j j | | f ƒ q q Wd S(   sç  In-place operation on row ``i`` using two-arg functor whose args are
        interpreted as ``(self[i, j], j)``.

        Examples
        ========

        >>> from sympy.matrices import SparseMatrix
        >>> M = SparseMatrix.eye(3)*2
        >>> M[0, 1] = -1
        >>> M.row_op(1, lambda v, j: v + 2*M[0, j]); M
        Matrix([
        [2, -1, 0],
        [4,  0, 0],
        [0,  0, 2]])

        See Also
        ========
        row
        zip_row_op
        col_op

        N(   R   R   R   R8   R	   R9   R³   (   R!   R#   R´   R$   R'   R¶   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   row_opÃ  s    c   	      C  sö   | | k r | | } } n  |  j  ƒ  } g  } x— | D] \ } } } | | k r| |  j j | | f ƒ | j | | f ƒ q5 | | k r´ |  j j | | f ƒ | |  j | | f <q5 | | k r5 Pq5 q5 Wx' | D] \ } } | |  j | | f <qÏ Wd S(   s  Swap, in place, columns i and j.

        Examples
        ========

        >>> from sympy.matrices import SparseMatrix
        >>> S = SparseMatrix.eye(3); S[2, 1] = 2
        >>> S.row_swap(1, 0); S
        Matrix([
        [0, 1, 0],
        [1, 0, 0],
        [0, 2, 1]])
        N(   R­   R   R³   RC   (	   R!   R#   R$   R   Rß   Rà   Rá   R'   R[   (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   row_swapâ  s    c           s#   ˆ j  | ‡  ‡ ‡ f d †  ƒ d S(   sí  In-place operation on row ``i`` using two-arg functor whose args are
        interpreted as ``(self[i, j], self[k, j])``.

        Examples
        ========

        >>> from sympy.matrices import SparseMatrix
        >>> M = SparseMatrix.eye(3)*2
        >>> M[0, 1] = -1
        >>> M.zip_row_op(1, 0, lambda v, u: v + 2*u); M
        Matrix([
        [2, -1, 0],
        [4,  0, 0],
        [0,  0, 2]])

        See Also
        ========
        row
        row_op
        col_op

        c           s   ˆ  |  ˆ ˆ | f ƒ S(   N(    (   R'   R$   (   R´   R[   R!   (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyR\     R]   N(   Rï   (   R!   R#   R[   R´   (    (   R´   R[   R!   s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt
   zip_row_op   s    N(   RÐ   RÑ   RÓ   R^   RK   Rd   R.   t   __hash__RÙ   RÚ   RÞ   Râ   Rä   Rã   Rì   Rí   Rî   Rï   Rð   Rñ   (    (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyR0   O  s    	9		#	>					 	!	:		(    t
   __future__R    R   R®   t   collectionsR   t   sympy.core.compatibilityR   R   R   R   t   sympy.core.containersR   t   sympy.core.exprR   t   sympy.core.singletonR	   t   sympy.functionsR
   t(   sympy.functions.elementary.miscellaneousR   t   sympy.utilities.iterablesR   t   commonR   t   denseR   t   matricesR   R   R   R0   (    (    (    s4   lib/python2.7/site-packages/sympy/matrices/sparse.pyt   <module>   s"   "ÿ ÿ ÿ ?