ó
ÍÝn[c           @   sq   d  d l  m Z d Z d Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ e d k rm d	 d
 l Z e j	 ƒ  n  d
 S(   i   (   t   xranges   
s     t   _matrixc           B   s‚  e  Z d  Z d „  Z d „  Z e 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 e Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z e e e d d ƒZ  d „  Z! d „  Z" e e! e" d d ƒZ# d „  Z$ e e$ ƒ Z% d  „  Z& d! „  Z' e e' ƒ Z( d" „  Z) e) Z* d# „  Z+ RS($   sm  
    Numerical matrix.

    Specify the dimensions or the data as a nested list.
    Elements default to zero.
    Use a flat list to create a column vector easily.

    By default, only mpf is used to store the data. You can specify another type
    using force_type=type. It's possible to specify None.
    Make sure force_type(force_type()) is fast.

    Creating matrices
    -----------------

    Matrices in mpmath are implemented using dictionaries. Only non-zero values
    are stored, so it is cheap to represent sparse matrices.

    The most basic way to create one is to use the ``matrix`` class directly.
    You can create an empty matrix specifying the dimensions:

        >>> from mpmath import *
        >>> mp.dps = 15
        >>> matrix(2)
        matrix(
        [['0.0', '0.0'],
         ['0.0', '0.0']])
        >>> matrix(2, 3)
        matrix(
        [['0.0', '0.0', '0.0'],
         ['0.0', '0.0', '0.0']])

    Calling ``matrix`` with one dimension will create a square matrix.

    To access the dimensions of a matrix, use the ``rows`` or ``cols`` keyword:

        >>> A = matrix(3, 2)
        >>> A
        matrix(
        [['0.0', '0.0'],
         ['0.0', '0.0'],
         ['0.0', '0.0']])
        >>> A.rows
        3
        >>> A.cols
        2

    You can also change the dimension of an existing matrix. This will set the
    new elements to 0. If the new dimension is smaller than before, the
    concerning elements are discarded:

        >>> A.rows = 2
        >>> A
        matrix(
        [['0.0', '0.0'],
         ['0.0', '0.0']])

    Internally ``mpmathify`` is used every time an element is set. This
    is done using the syntax A[row,column], counting from 0:

        >>> A = matrix(2)
        >>> A[1,1] = 1 + 1j
        >>> A
        matrix(
        [['0.0', '0.0'],
         ['0.0', mpc(real='1.0', imag='1.0')]])

    You can use the keyword ``force_type`` to change the function which is
    called on every new element:

        >>> matrix(2, 5, force_type=int) # doctest: +SKIP
        matrix(
        [[0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0]])

    A more comfortable way to create a matrix lets you use nested lists:

        >>> matrix([[1, 2], [3, 4]])
        matrix(
        [['1.0', '2.0'],
         ['3.0', '4.0']])

    If you want to preserve the type of the elements you can use
    ``force_type=None``:

        >>> matrix([[1, 2.5], [1j, mpf(2)]], force_type=None)
        matrix(
        [['1.0', '2.5'],
         [mpc(real='0.0', imag='1.0'), '2.0']])

    Convenient advanced functions are available for creating various standard
    matrices, see ``zeros``, ``ones``, ``diag``, ``eye``, ``randmatrix`` and
    ``hilbert``.

    Vectors
    .......

    Vectors may also be represented by the ``matrix`` class (with rows = 1 or cols = 1).
    For vectors there are some things which make life easier. A column vector can
    be created using a flat list, a row vectors using an almost flat nested list::

        >>> matrix([1, 2, 3])
        matrix(
        [['1.0'],
         ['2.0'],
         ['3.0']])
        >>> matrix([[1, 2, 3]])
        matrix(
        [['1.0', '2.0', '3.0']])

    Optionally vectors can be accessed like lists, using only a single index::

        >>> x = matrix([1, 2, 3])
        >>> x[1]
        mpf('2.0')
        >>> x[1,0]
        mpf('2.0')

    Other
    .....

    Like you probably expected, matrices can be printed::

        >>> print randmatrix(3) # doctest:+SKIP
        [ 0.782963853573023  0.802057689719883  0.427895717335467]
        [0.0541876859348597  0.708243266653103  0.615134039977379]
        [ 0.856151514955773  0.544759264818486  0.686210904770947]

    Use ``nstr`` or ``nprint`` to specify the number of digits to print::

        >>> nprint(randmatrix(5), 3) # doctest:+SKIP
        [2.07e-1  1.66e-1  5.06e-1  1.89e-1  8.29e-1]
        [6.62e-1  6.55e-1  4.47e-1  4.82e-1  2.06e-2]
        [4.33e-1  7.75e-1  6.93e-2  2.86e-1  5.71e-1]
        [1.01e-1  2.53e-1  6.13e-1  3.32e-1  2.59e-1]
        [1.56e-1  7.27e-2  6.05e-1  6.67e-2  2.79e-1]

    As matrices are mutable, you will need to copy them sometimes::

        >>> A = matrix(2)
        >>> A
        matrix(
        [['0.0', '0.0'],
         ['0.0', '0.0']])
        >>> B = A.copy()
        >>> B[0,0] = 1
        >>> B
        matrix(
        [['1.0', '0.0'],
         ['0.0', '0.0']])
        >>> A
        matrix(
        [['0.0', '0.0'],
         ['0.0', '0.0']])

    Finally, it is possible to convert a matrix to a nested list. This is very useful,
    as most Python libraries involving matrices or arrays (namely NumPy or SymPy)
    support this format::

        >>> B.tolist()
        [[mpf('1.0'), mpf('0.0')], [mpf('0.0'), mpf('0.0')]]


    Matrix operations
    -----------------

    You can add and subtract matrices of compatible dimensions::

        >>> A = matrix([[1, 2], [3, 4]])
        >>> B = matrix([[-2, 4], [5, 9]])
        >>> A + B
        matrix(
        [['-1.0', '6.0'],
         ['8.0', '13.0']])
        >>> A - B
        matrix(
        [['3.0', '-2.0'],
         ['-2.0', '-5.0']])
        >>> A + ones(3) # doctest:+ELLIPSIS
        Traceback (most recent call last):
          ...
        ValueError: incompatible dimensions for addition

    It is possible to multiply or add matrices and scalars. In the latter case the
    operation will be done element-wise::

        >>> A * 2
        matrix(
        [['2.0', '4.0'],
         ['6.0', '8.0']])
        >>> A / 4
        matrix(
        [['0.25', '0.5'],
         ['0.75', '1.0']])
        >>> A - 1
        matrix(
        [['0.0', '1.0'],
         ['2.0', '3.0']])

    Of course you can perform matrix multiplication, if the dimensions are
    compatible::

        >>> A * B
        matrix(
        [['8.0', '22.0'],
         ['14.0', '48.0']])
        >>> matrix([[1, 2, 3]]) * matrix([[-6], [7], [-2]])
        matrix(
        [['2.0']])

    You can raise powers of square matrices::

        >>> A**2
        matrix(
        [['7.0', '10.0'],
         ['15.0', '22.0']])

    Negative powers will calculate the inverse::

        >>> A**-1
        matrix(
        [['-2.0', '1.0'],
         ['1.5', '-0.5']])
        >>> A * A**-1
        matrix(
        [['1.0', '1.0842021724855e-19'],
         ['-2.16840434497101e-19', '1.0']])

    Matrix transposition is straightforward::

        >>> A = ones(2, 3)
        >>> A
        matrix(
        [['1.0', '1.0', '1.0'],
         ['1.0', '1.0', '1.0']])
        >>> A.T
        matrix(
        [['1.0', '1.0'],
         ['1.0', '1.0'],
         ['1.0', '1.0']])

    Norms
    .....

    Sometimes you need to know how "large" a matrix or vector is. Due to their
    multidimensional nature it's not possible to compare them, but there are
    several functions to map a matrix or a vector to a positive real number, the
    so called norms.

    For vectors the p-norm is intended, usually the 1-, the 2- and the oo-norm are
    used.

        >>> x = matrix([-10, 2, 100])
        >>> norm(x, 1)
        mpf('112.0')
        >>> norm(x, 2)
        mpf('100.5186549850325')
        >>> norm(x, inf)
        mpf('100.0')

    Please note that the 2-norm is the most used one, though it is more expensive
    to calculate than the 1- or oo-norm.

    It is possible to generalize some vector norms to matrix norm::

        >>> A = matrix([[1, -1000], [100, 50]])
        >>> mnorm(A, 1)
        mpf('1050.0')
        >>> mnorm(A, inf)
        mpf('1001.0')
        >>> mnorm(A, 'F')
        mpf('1006.2310867787777')

    The last norm (the "Frobenius-norm") is an approximation for the 2-norm, which
    is hard to calculate and not available. The Frobenius-norm lacks some
    mathematical properties you might expect from a norm.
    c         O   sÂ  i  |  _  d  |  _ | j d |  j j ƒ } | s< d „  } n  t | d t t f ƒ rCt | d d t t f ƒ rñ | d } t	 | ƒ |  _
 t	 | d ƒ |  _ xŸ t | ƒ D]? \ } } x0 t | ƒ D]" \ } } | | ƒ |  | | f <qÄ Wq« Wq¾| d }	 t	 |	 ƒ |  _
 d |  _ x¨t |	 ƒ D] \ } }
 |
 |  | d f <q Wn{t | d t ƒ r¾t	 | ƒ d k r| d |  _
 |  _ q¾t | d t ƒ s¡t d ƒ ‚ n  | d |  _
 | d |  _ n t | d t ƒ r\| d j ƒ  } | j  |  _  | j
 |  _
 | j |  _ x¶ t | j
 ƒ D]@ } x7 t | j ƒ D]& } | | | | f ƒ | | | f <q+WqWnb t | d d ƒ r²|  j j | d j ƒ  ƒ } | j  |  _  | j
 |  _
 | j |  _ n t d ƒ ‚ d  S(   Nt
   force_typec         S   s   |  S(   N(    (   t   x(    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt   <lambda>&  s    i    i   s   expected intt   tolists#   could not interpret given arguments(   t   _matrix__datat   Nonet   _LUt   gett   ctxt   convertt
   isinstancet   listt   tuplet   lent   _matrix__rowst   _matrix__colst	   enumeratet   intt	   TypeErrorR   t   copyR    t   hasattrt   matrixR   (   t   selft   argst   kwargsR   t   At   it   rowt   jt   at   vt   e(    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt   __init__  sN    		
!
	+c         C   ss   |  j  j |  j |  j ƒ } xQ t |  j ƒ D]@ } x7 t |  j ƒ D]& } | |  | | f ƒ | | | f <qA Wq+ W| S(   sR   
        Return a copy of self with the function `f` applied elementwise.
        (   R
   R   R   R   R    (   R   t   ft   newR   R   (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt   applyP  s
    (c   
      K   s?  g  } d g |  j  } x­ t |  j ƒ D]œ } | j g  ƒ x† t |  j  ƒ D]u } | rz |  j j |  | | f | |  } n t |  | | f ƒ } | d j | ƒ t t | ƒ | | ƒ | | <qI Wq& Wxi t	 | ƒ D][ \ } } x1 t	 | ƒ D]# \ } }	 |	 j
 | | ƒ | | <qì Wd t j | ƒ d | | <qÓ Wt j | ƒ S(   Ni    iÿÿÿÿt   [t   ](   t   colst   ranget   rowst   appendR
   t   nstrt   strt   maxR   R   t   rjustt   colsept   joint   rowsep(
   R   t   nR   t   rest   maxlenR   R   t   stringR   t   elem(    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt   __nstr__Z  s    %%c         C   s
   |  j  ƒ  S(   N(   R8   (   R   (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt   __str__p  s    c         C   sâ   |  j  j } d } xµ t |  j ƒ D]¤ } | d 7} x} t |  j ƒ D]l } | si t |  | | f | ƒ r‚ t |  | | f ƒ } n d t |  | | f ƒ d } | | d 7} qB W| d  } | d 7} q" W| d  } | d 7} | S(   sd   
        Create a list string from a matrix.

        If avoid_type: avoid multiple 'mpf's.
        R&   t   's   , iþÿÿÿs   ],
 iýÿÿÿR'   (   R
   t   mpfR    R   R   R   t   reprR-   (   R   t
   avoid_typet   typt   sR   R   R   (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt
   _toliststrs  s    
!


c         C   sF   g  t  |  j ƒ D]2 } g  t  |  j ƒ D] } |  | | f ^ q& ^ q S(   s6   
        Convert the matrix to a nested list.
        (   R)   R   R   (   R   R   R   (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyR   Š  s    c         C   s:   |  j  j r |  j ƒ  Sd } | |  j d t ƒ d 7} | S(   Ns   matrix(
R=   t   )(   R
   t   prettyR9   R@   t   True(   R   R?   (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt   __repr__  s
    
c         C   s(   | |  j  k r |  j  | S|  j j Sd S(   s  
        Fast extraction of the i,j element from the matrix
            This function is for private use only because is unsafe:
                1. Does not check on the value of key it expects key to be a integer tuple (i,j)
                2. Does not check bounds
        N(   R   R
   t   zero(   R   t   key(    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt   __get_element—  s    c         C   s6   | r | |  j  | <n | |  j  k r2 |  j  | =n  d S(   s"  
        Fast assignment of the i,j element in the matrix
            This function is unsafe:
                1. Does not check on the value of key it expects key to be a integer tuple (i,j)
                2. Does not check bounds
                3. Does not check the value type
        N(   R   (   R   RF   t   value(    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt   __set_element£  s    c   	      C   s²  t  | t ƒ s t  | t ƒ ri |  j d k r< d | f } qi |  j d k rZ | d f } qi t d ƒ ‚ n  t  | d t ƒ s t  | d t ƒ rUt  | d t ƒ r#| d j d k sÈ | d j d k r| d j d k sõ | d j |  j d k rt	 | d j
 |  j ƒ Œ  } q0t d ƒ ‚ n | d g } t  | d t ƒ rÄ| d j d k si| d j d k rµ| d j d k s–| d j |  j d k rµt	 | d j
 |  j ƒ Œ  } qÑt d ƒ ‚ n | d g } |  j j t | ƒ t | ƒ ƒ } x\ t | ƒ D]N \ } } x? t | ƒ D]1 \ } } | j | | f |  j | | f ƒ ƒ qWqÿW| S| d |  j k s{| d |  j k rŠt d ƒ ‚ n  | |  j k r¤|  j | S|  j j Sd S(   sÅ   
            Getitem function for mp matrix class with slice index enabled
            it allows the following assingments
            scalar to a slice of the matrix
         B = A[:,2:6]
        i   i    s   insufficient indices for matrixs   Row index out of boundss   Column index out of boundss   matrix index out of rangeN(   R   R   t   sliceR   R   t
   IndexErrort   startR   t   stopR    t   indicesR
   R   R   R   t   _matrix__set_elementt   _matrix__get_elementR   RE   (	   R   RF   R*   t   columnst   mR   R   R   t   y(    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt   __getitem__±  s:    &&-&-!-&c   	      C   s`  t  | t ƒ s t  | t ƒ ri |  j d k r< d | f } qi |  j d k rZ | d f } qi t d ƒ ‚ n  t  | d t ƒ s t  | d t ƒ rÎt  | d t ƒ r#| d j d  k sÈ | d j d k r| d j d  k sõ | d j |  j d k rt	 | d j
 |  j ƒ Œ  } q0t d ƒ ‚ n | d g } t  | d t ƒ rÄ| d j d  k si| d j d k rµ| d j d  k s–| d j |  j d k rµt	 | d j
 |  j ƒ Œ  } qÑt d ƒ ‚ n | d g } t  | |  j j ƒ rt | ƒ | j k rrt | ƒ | j k rrxk t | ƒ D]N \ } } x? t | ƒ D]1 \ } } |  j | | f | j | | f ƒ ƒ q6WqWqËt d ƒ ‚ qG|  j j | ƒ } x± | D]- } x$ | D] } |  j | | f | ƒ q§WqšWny | d |  j k sô| d |  j k rt d ƒ ‚ n  |  j j | ƒ } | r+| |  j | <n | |  j k rG|  j | =n  |  j r\d  |  _ n  d  S(   Ni   i    s   insufficient indices for matrixs   Row index out of boundss   Column index out of boundss   Dimensions do not matchs   matrix index out of range(   R   R   RJ   R   R   RK   RL   R   RM   R    RN   R
   R   R   R*   R(   R   RO   RP   t
   ValueErrorR   R   R   (	   R   RF   RH   R*   RQ   R   R   R   RS   (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt   __setitem__ò  sN    &&-&-*0!&	c         c   sG   x@ t  |  j ƒ D]/ } x& t  |  j ƒ D] } |  | | f Vq& Wq Wd  S(   N(   R    R   R   (   R   R   R   (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt   __iter__;  s    c            s<  t  ˆ ˆ j j ƒ rÇ ˆ j ˆ j k r6 t d ƒ ‚ n  ˆ j j ˆ j ˆ j ƒ } xo t ˆ j ƒ D]^ ‰  xU t ˆ j ƒ D]D ‰ ˆ j j ‡  ‡ ‡ ‡ f d †  t ˆ j ƒ Dƒ ƒ | ˆ  ˆ f <qw Wqa W| Sˆ j j ˆ j ˆ j ƒ } xO t ˆ j ƒ D]> ‰  x5 t ˆ j ƒ D]$ ‰ ˆ ˆ ˆ  ˆ f | ˆ  ˆ f <qWqò W| Sd  S(   Ns,   dimensions not compatible for multiplicationc         3   s/   |  ]% } ˆ ˆ  | f ˆ | ˆ f f Vq d  S(   N(    (   t   .0t   k(   R   R   t   otherR   (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pys	   <genexpr>H  s   (   R   R
   R   R   R   RU   R    t   fdot(   R   RZ   R$   (    (   R   R   RZ   R   s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt   __mul__@  s    (&c         C   s1   t  | |  j j ƒ r$ t d ƒ ‚ n  |  j | ƒ S(   Ns&   other should not be type of ctx.matrix(   R   R
   R   R   R\   (   R   RZ   (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt   __rmul__S  s    c         C   s  t  | t ƒ s t d ƒ ‚ n  |  j |  j k s? t d ƒ ‚ n  | } | d k rd |  j j |  j ƒ S| d k  r€ | } t } n t } | } d } |  j	 ƒ  } xA | d k rá | d d k rÊ | | } n  | | } | d } q¡ W| rý |  j j
 | ƒ } n  | S(   Ns$   only integer exponents are supporteds*   only powers of square matrices are definedi    i   i   (   R   R   RU   R   R   R
   t   eyeRC   t   FalseR   t   inverse(   R   RZ   R3   t   negR   RS   t   z(    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt   __pow__Y  s,    	
c         C   s   t  | |  j j ƒ s t ‚ |  j j |  j |  j ƒ } xO t |  j ƒ D]> } x5 t |  j ƒ D]$ } |  | | f | | | | f <q] WqG W| S(   N(   R   R
   R   t   AssertionErrorR   R   R    (   R   RZ   R$   R   R   (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt   __div__t  s    &c         C   s>  t  | |  j j ƒ rÃ |  j | j k o6 |  j | j k sH t d ƒ ‚ n  |  j j |  j |  j ƒ } xY t |  j ƒ D]H } x? t |  j ƒ D]. } |  | | f | | | f | | | f <q‰ Wqs W| S|  j j |  j |  j ƒ } xU t |  j ƒ D]D } x; t |  j ƒ D]* } | | | f c |  | | f | 7<qWqî W| Sd  S(   Ns$   incompatible dimensions for addition(   R   R
   R   R   R   RU   R    (   R   RZ   R$   R   R   (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt   __add__  s    $0,c         C   s   |  j  | ƒ S(   N(   Rf   (   R   RZ   (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt   __radd__  s    c         C   sZ   t  | |  j j ƒ rI |  j | j k o6 |  j | j k rI t d ƒ ‚ n  |  j | d ƒ S(   Ns'   incompatible dimensions for subtractioniÿÿÿÿ(   R   R
   R   R   R   RU   Rf   (   R   RZ   (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt   __sub__“  s    'c         C   s   d |  S(   Niÿÿÿÿ(    (   R   (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt   __neg__™  s    c         C   s	   |  | S(   N(    (   R   RZ   (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt   __rsub__œ  s    c         C   s4   |  j  | j  k o3 |  j | j k o3 |  j | j k S(   N(   R   R   R   (   R   RZ   (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt   __eq__Ÿ  s    $c         C   s7   |  j  d k r |  j S|  j d k r, |  j  S|  j  Sd  S(   Ni   (   R*   R(   (   R   (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt   __len__£  s
    c         C   s   |  j  S(   N(   R   (   R   (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt	   __getrows«  s    c         C   sD   x4 |  j  j ƒ  D]# } | d | k r |  j  | =q q W| |  _ d  S(   Ni    (   R   R   R   (   R   RH   RF   (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt	   __setrows®  s    t   docs   number of rowsc         C   s   |  j  S(   N(   R   (   R   (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt	   __getcols¶  s    c         C   sD   x4 |  j  j ƒ  D]# } | d | k r |  j  | =q q W| |  _ d  S(   Ni   (   R   R   R   (   R   RH   RF   (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt	   __setcols¹  s    s   number of columnsc         C   sm   |  j  j |  j |  j ƒ } xK t |  j ƒ D]: } x1 t |  j ƒ D]  } |  | | f | | | f <qA Wq+ W| S(   N(   R
   R   R   R   R    (   R   R$   R   R   (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt	   transposeÁ  s
    "c         C   s   |  j  |  j j ƒ S(   N(   R%   R
   t   conj(   R   (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt	   conjugateÊ  s    c         C   s   |  j  ƒ  j ƒ  S(   N(   Rt   Rr   (   R   (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt   transpose_conjÍ  s    c         C   s1   |  j  j |  j |  j ƒ } |  j j ƒ  | _ | S(   N(   R
   R   R   R   R   R   (   R   R$   (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyR   Ò  s    c         C   sJ   |  j  j |  j d ƒ } x+ t |  j ƒ D] } |  | | f | | <q( W| S(   Ni   (   R
   R   R*   R)   (   R   R3   RR   R   (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt   columnÙ  s    (,   t   __name__t
   __module__t   __doc__R"   R%   R   R8   R9   R_   R@   R   RD   RP   RO   RT   RV   RW   R\   R]   Rc   Re   t   __truediv__Rf   Rg   Rh   Ri   Rj   Rk   Rl   t   _matrix__getrowst   _matrix__setrowst   propertyR*   t   _matrix__getcolst   _matrix__setcolsR(   Rr   t   TRt   Ru   t   HR   t   __copy__Rv   (    (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyR      sP   ÿ 	2	
						A	I																					t   MatrixMethodsc           B   s}   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d d „ Z d d d d „ Z	 d	 „  Z
 d
 „  Z d d „ Z d d „ Z RS(   c         C   s7   t  d t f i  ƒ |  _ |  |  j _ |  j |  j _ d  S(   NR   (   t   typeR   R   R
   R   (   R
   (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyR"   á  s    c         K   s=   |  j  | |  } x$ t | ƒ D] } d | | | f <q W| S(   s6   
        Create square identity matrix n x n.
        i   (   R   R    (   R
   R3   R   R   R   (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyR^   ç  s    c         K   sM   |  j  t | ƒ |  } x. t t | ƒ ƒ D] } | | | | | f <q+ W| S(   s&  
        Create square diagonal matrix using given list.

        Example:
        >>> from mpmath import diag, mp
        >>> mp.pretty = False
        >>> diag([1, 2, 3])
        matrix(
        [['1.0', '0.0', '0.0'],
         ['0.0', '2.0', '0.0'],
         ['0.0', '0.0', '3.0']])
        (   R   R   R    (   R
   t   diagonalR   R   R   (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt   diagð  s    c         O   s¹   t  | ƒ d k r# | d } } n? t  | ƒ d k rL | d } | d } n t d t  | ƒ ƒ ‚ |  j | | |  } x; t | ƒ D]- } x$ t | ƒ D] } d | | | f <q— Wq„ W| S(   s&  
        Create matrix m x n filled with zeros.
        One given dimension will create square matrix n x n.

        Example:
        >>> from mpmath import zeros, mp
        >>> mp.pretty = False
        >>> zeros(2)
        matrix(
        [['0.0', '0.0'],
         ['0.0', '0.0']])
        i   i    i   s*   zeros expected at most 2 arguments, got %i(   R   R   R   R    (   R
   R   R   RR   R3   R   R   R   (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt   zeros  s    
c         O   s¹   t  | ƒ d k r# | d } } n? t  | ƒ d k rL | d } | d } n t d t  | ƒ ƒ ‚ |  j | | |  } x; t | ƒ D]- } x$ t | ƒ D] } d | | | f <q— Wq„ W| S(   s#  
        Create matrix m x n filled with ones.
        One given dimension will create square matrix n x n.

        Example:
        >>> from mpmath import ones, mp
        >>> mp.pretty = False
        >>> ones(2)
        matrix(
        [['1.0', '1.0'],
         ['1.0', '1.0']])
        i   i    i   s)   ones expected at most 2 arguments, got %i(   R   R   R   R    (   R
   R   R   RR   R3   R   R   R   (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt   ones  s    
c         C   sx   | d k r | } n  |  j | | ƒ } xJ t | ƒ D]< } x3 t | ƒ D]% } |  j | | d | | | f <qG Wq4 W| S(   sÕ   
        Create (pseudo) hilbert matrix m x n.
        One given dimension will create hilbert matrix n x n.

        The matrix is very ill-conditioned and symmetric, positive definite if
        square.
        i   N(   R   R   R    t   one(   R
   RR   R3   R   R   R   (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt   hilbert6  s    	'i    i   c   	      K   sx   | s | } n  |  j  | | |  } xM t | ƒ D]? } x6 t | ƒ D]( } |  j ƒ  | | | | | | f <qD Wq1 W| S(   sZ  
        Create a random m x n matrix.

        All values are >= min and <max.
        n defaults to m.

        Example:
        >>> from mpmath import randmatrix
        >>> randmatrix(2) # doctest:+SKIP
        matrix(
        [['0.53491598236191806', '0.57195669543302752'],
         ['0.85589992269513615', '0.82444367501382143']])
        (   R   R    t   rand(	   R
   RR   R3   t   minR.   R   R   R   R   (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt
   randmatrixF  s    	*c         C   s³   | | k r d St  | |  j ƒ rt xŠ t | j ƒ D]; } | | | f | | | f | | | f <| | | f <q2 Wn; t  | t ƒ r£ | | | | | | <| | <n t d ƒ ‚ d S(   s(   
        Swap row i with row j.
        Ns   could not interpret type(   R   R   R    R(   R   R   (   R
   R   R   R   RY   (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt   swap_row\  s    < c         C   s™   t  | |  j ƒ s! t d ƒ ‚ n  | j t | ƒ k rE t d ƒ ‚ n  | j ƒ  } | j d 7_ x2 t | j ƒ D]! } | | | | | j d f <qp W| S(   sB   
        Extend matrix A with column b and return result.
        s    A should be a type of ctx.matrixs   Value should be equal to len(b)i   (	   R   R   R   R*   R   RU   R   R(   R    (   R
   R   t   bR   (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt   extendj  s    i   c            s  y t  | ƒ Wn t k
 r+ ˆ  j | ƒ SXt ˆ ƒ t k	 rP ˆ  j ˆ ƒ ‰ n  ˆ ˆ  j k ry t ‡  f d †  | Dƒ ƒ Sˆ d k r˜ ˆ  j | d d ƒSˆ d k rÆ ˆ  j	 ˆ  j | d d d d ƒƒ Sˆ d k rû ˆ  j
 ˆ  j ‡ f d †  | Dƒ ƒ ˆ ƒ St d ƒ ‚ d S(	   sÞ  
        Gives the entrywise `p`-norm of an iterable *x*, i.e. the vector norm
        `\left(\sum_k |x_k|^p\right)^{1/p}`, for any given `1 \le p \le \infty`.

        Special cases:

        If *x* is not iterable, this just returns ``absmax(x)``.

        ``p=1`` gives the sum of absolute values.

        ``p=2`` is the standard Euclidean vector norm.

        ``p=inf`` gives the magnitude of the largest element.

        For *x* a matrix, ``p=2`` is the Frobenius norm.
        For operator matrix norms, use :func:`~mpmath.mnorm` instead.

        You can use the string 'inf' as well as float('inf') or mpf('inf')
        to specify the infinity norm.

        **Examples**

            >>> from mpmath import *
            >>> mp.dps = 15; mp.pretty = False
            >>> x = matrix([-10, 2, 100])
            >>> norm(x, 1)
            mpf('112.0')
            >>> norm(x, 2)
            mpf('100.5186549850325')
            >>> norm(x, inf)
            mpf('100.0')

        c         3   s   |  ] } ˆ  j  | ƒ Vq d  S(   N(   t   absmax(   RX   R   (   R
   (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pys	   <genexpr>¡  s    i   t   absolutei   t   squaredc         3   s   |  ] } t  | ƒ ˆ  Vq d  S(   N(   t   abs(   RX   R   (   t   p(    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pys	   <genexpr>§  s    s   p has to be >= 1N(   t   iterR   R‘   R„   R   R   t   infR.   t   fsumt   sqrtt   nthrootRU   (   R
   R   R•   (    (   R
   R•   s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt   normx  s    "")c            sô   ˆ j  ˆ  ƒ ‰  t | ƒ t k	 rj t | ƒ t k rX d j | j ƒ  ƒ rX ˆ j ˆ  d ƒ Sˆ j | ƒ } n  ˆ  j ˆ  j	 ‰ ‰ | d k r¯ t
 ‡  ‡ ‡ f d †  t ˆ ƒ Dƒ ƒ S| ˆ j k rä t
 ‡  ‡ ‡ f d †  t ˆ ƒ Dƒ ƒ St d ƒ ‚ d S(   só  
        Gives the matrix (operator) `p`-norm of A. Currently ``p=1`` and ``p=inf``
        are supported:

        ``p=1`` gives the 1-norm (maximal column sum)

        ``p=inf`` gives the `\infty`-norm (maximal row sum).
        You can use the string 'inf' as well as float('inf') or mpf('inf')

        ``p=2`` (not implemented) for a square matrix is the usual spectral
        matrix norm, i.e. the largest singular value.

        ``p='f'`` (or 'F', 'fro', 'Frobenius, 'frobenius') gives the
        Frobenius norm, which is the elementwise 2-norm. The Frobenius norm is an
        approximation of the spectral norm and satisfies

        .. math ::

            \frac{1}{\sqrt{\mathrm{rank}(A)}} \|A\|_F \le \|A\|_2 \le \|A\|_F

        The Frobenius norm lacks some mathematical properties that might
        be expected of a norm.

        For general elementwise `p`-norms, use :func:`~mpmath.norm` instead.

        **Examples**

            >>> from mpmath import *
            >>> mp.dps = 15; mp.pretty = False
            >>> A = matrix([[1, -1000], [100, 50]])
            >>> mnorm(A, 1)
            mpf('1050.0')
            >>> mnorm(A, inf)
            mpf('1001.0')
            >>> mnorm(A, 'F')
            mpf('1006.2310867787777')

        t	   frobeniusi   i   c         3   s=   |  ]3 ‰  ˆ j  ‡ ‡  f d  †  t ˆ ƒ Dƒ d d ƒVq d S(   c         3   s   |  ] } ˆ  | ˆ f Vq d  S(   N(    (   RX   R   (   R   R   (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pys	   <genexpr>Ù  s    R’   i   N(   R˜   R    (   RX   (   R   R
   RR   (   R   s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pys	   <genexpr>Ù  s    c         3   s=   |  ]3 ‰  ˆ j  ‡ ‡  f d  †  t ˆ ƒ Dƒ d d ƒVq d S(   c         3   s   |  ] } ˆ  ˆ | f Vq d  S(   N(    (   RX   R   (   R   R   (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pys	   <genexpr>Û  s    R’   i   N(   R˜   R    (   RX   (   R   R
   R3   (   R   s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pys	   <genexpr>Û  s    s   matrix p-norm for arbitrary pN(   R   R„   R   R-   t
   startswitht   lowerR›   R   R*   R(   R.   R    R—   t   NotImplementedError(   R
   R   R•   (    (   R   R
   RR   R3   s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt   mnorm«  s    ''&&N(   Rw   Rx   R"   R^   R†   R‡   Rˆ   R   RŠ   R   RŽ   R   R›   R    (    (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyRƒ   ß  s   								3t   __main__iÿÿÿÿN(
   t   libmp.backendR    R2   R0   t   objectR   Rƒ   Rw   t   doctestt   testmod(    (    (    s7   lib/python2.7/site-packages/mpmath/matrices/matrices.pyt   <module>   s   ÿ ÿ Ùÿ 