
    "`}                         d dl mZ ddlZdZdZ G d de      Z G d d	e      Zed
k(  rddl	Z	 e	j                          yy)   )xrange    N
z  c                   4   e Zd ZdZd Zd Zd(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 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d Z e eed      Z!d  Z"d! Z# e e"e#d"      Z$d# Z% e e%      Z&d$ Z'd% Z( e e(      Z)d& Z*e*Z+d' Z,y)*_matrixa#  
    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.

    The datatype of the context (mpf for mp, mpi for iv, and float for fp) is used to store the data.

    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')]])

    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']])

    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, using ``@`` (for Python >= 3.5) or ``*``. For clarity, ``@`` is
    recommended (`PEP 465 <https://www.python.org/dev/peps/pep-0465/>`), because
    the meaning of ``*`` is different in many other Python libraries such as NumPy.

        >>> A @ B # doctest:+SKIP
        matrix(
        [['8.0', '22.0'],
         ['14.0', '48.0']])
        >>> A * B # same as A @ B
        matrix(
        [['8.0', '22.0'],
         ['14.0', '48.0']])
        >>> matrix([[1, 2, 3]]) * matrix([[-6], [7], [-2]])
        matrix(
        [['2.0']])

    ..
        COMMENT: TODO: the above "doctest:+SKIP" may be removed as soon as we
        have dropped support for Python 3.4 and below.

    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                    i | _         d | _        d|v rt        j                  d       t	        |d   t
        t        f      rt	        |d   d   t
        t        f      rV|d   }t        |      | _        t        |d         | _	        t        |      D ]  \  }}t        |      D ]  \  }}|| ||f<    ! y |d   }t        |      | _        d| _	        t        |      D ]  \  }}	|	| |df<    y t	        |d   t              rSt        |      dk(  r|d   x| _        | _	        y t	        |d   t              st        d      |d   | _        |d   | _	        y t	        |d   t              rh|d   }|j                  | _        |j                  | _	        t        |j                        D ](  }t        |j                        D ]  }|||f   | ||f<    * y t        |d   d      r`| j                   j#                  |d   j%                               }|j                   | _         |j                  | _        |j                  | _	        y t        d      )N
force_typea#  The force_type argument was removed, it did not work properly anyway. If you want to force floating-point or interval computations, use the respective methods from `fp` or `mp` instead, e.g., `fp.matrix()` or `iv.matrix()`. If you want to truncate values to integer, use .apply(int) instead.r      zexpected inttolistz#could not interpret given arguments)_matrix__data_LUwarningswarn
isinstancelisttuplelen_matrix__rows_matrix__cols	enumerateint	TypeErrorr   r   hasattrctxmatrixr   )
selfargskwargsAirowjaves
             :/usr/lib/python3/dist-packages/mpmath/matrices/matrices.py__init__z_matrix.__init__  s    6!MM W X
 d1ge}-$q'!*tUm4G!!f!!A$i'l 'FAs )# '1%&QT
'' G!!f%aL #DAq!"DAJ#Q%4yA~,0G3dk!$q'3/#N33"1g"1gQ)QA//DK//DKAHH% )) )A!"1a4DAJ)) T!Wh'Q 01A//DK//DK//DKABB    c                     | j                   j                  | j                  | j                        }t	        | j                        D ].  }t	        | j                        D ]  } || ||f         |||f<    0 |S )zR
        Return a copy of self with the function `f` applied elementwise.
        )r   r   r   r   r   )r   fnewr    r"   s        r&   applyz_matrix.applyN  sp     hhoodkk4;;7$ 	(ADKK( (T!A#Y<AaC(	( 
r(   Nc                 Z   g }dg| j                   z  }t        | j                        D ]  }|j                  g        t        | j                         D ]g  }|r$ | j                  j
                  | ||f   |fi |}nt        | ||f         }|d   j                  |       t        t        |      ||         ||<   i  t        |      D ]M  \  }}t        |      D ]  \  }}	|	j                  ||         ||<    dt        j                  |      z   dz   ||<   O t        j                  |      S )Nr   [])colsrangerowsappendr   nstrstrmaxr   r   rjustcolsepjoinrowsep)
r   nr   resmaxlenr    r"   stringr!   elems
             r&   __nstr__z_matrix.__nstr__X  s%   tyytyy! 	8AJJrN499% 8*TXX]]4!9aB6BF ac^FBv&FVAY7q	8	8  n 	2FAs$S> /4F1I.A/ 6;;s++c1CF		2
 {{3r(   c                 "    | j                         S N)rA   r   s    r&   __str__z_matrix.__str__n  s    }}r(   c                 T   | j                   j                  }d}t        | j                        D ]m  }|dz  }t        | j                        D ]D  }|rt        | ||f   |      st        | ||f         }ndt        | ||f         z   dz   }||dz   z  }F |dd }|dz  }o |dd }|dz  }|S )	zd
        Create a list string from a matrix.

        If avoid_type: avoid multiple 'mpf's.
        r/   'z, Nz],
 r0   )r   mpfr   r   r   r   reprr6   )r   
avoid_typetypsr    r"   r#   s          r&   
_toliststrz_matrix._toliststrq  s     hhll$ 		AHADKK( !D1Is)CT!A#YAc$qs)n,s2AQX #2ALA		 crF	Sr(   c           
          t        | j                        D cg c]*  }t        | j                        D cg c]	  }| ||f    c}, c}}S c c}w c c}}w )z6
        Convert the matrix to a nested list.
        )r2   r   r   r   r    r"   s      r&   r   z_matrix.tolist  sB     BGt{{ASTAE$++$67qac7TT7Ts   AAAAc                     | j                   j                  r| j                         S d}|| j                  d      dz   z  }|S )Nzmatrix(
T)rL   ))r   prettyrE   rO   )r   rN   s     r&   __repr__z_matrix.__repr__  s=    88??<<>!	T___-33r(   c                 h    || j                   v r| j                   |   S | j                  j                  S )a  
        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
        )r   r   zero)r   keys     r&   __get_elementz_matrix.__get_element  s-     $++;;s##88== r(   c                 `    |r|| j                   |<   y|| j                   v r| j                   |= yy)aQ  
        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
                4. Does not reset the LU cache
        N)r   )r   rX   values      r&   __set_elementz_matrix.__set_element  s2     $DKKDKKC   r(   c           	         t        |t              st        |t              r3| j                  dk(  rd|f}n| j                  dk(  r|df}nt        d      t        |d   t              st        |d   t              rt        |d   t              r|d   j                  |d   j                  dk\  rS|d   j                  |d   j                  | j                  dz   k  r%t        |d   j                  | j                         }nt        d      |d   g}t        |d   t              r|d   j                  |d   j                  dk\  rS|d   j                  |d   j                  | j                  dz   k  r%t        |d   j                  | j                         }nt        d      |d   g}| j                  j                  t        |      t        |            }t        |      D ]=  \  }}t        |      D ]*  \  }}|j                  ||f| j                  ||f             , ? |S |d   | j                  k\  s|d   | j                  k\  rt        d      || j                   v r| j                   |   S | j                  j"                  S )z
            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]
        r
   r   insufficient indices for matrixRow index out of boundsColumn index out of boundsmatrix index out of range)r   r   slicer   r   
IndexErrorstartstopr   indicesr   r   r   r   _matrix__set_element_matrix__get_elementr   rW   )	r   rX   r3   columnsmr    xr"   ys	            r&   __getitem__z_matrix.__getitem__  sE    c3:c%#8{{a#h!Ah !BCCc!fU#z#a&'? #a&'FLL(CFLLA,=V[[(CFKK4;;q=,H!3q6>>$++#>?D$%>?? Ax #a&'FLL(CFLLA,=V[[(CFKK4;;q=,H$c!fnnT[[&ABG$%ABB q6( D	#g,7A ! E!$W- ECAaOOQqE$*<*<aU*CDEE H 1v$A$++(= !<==dkk!{{3''xx}}$r(   c           	      6   t        |t              st        |t              r3| j                  dk(  rd|f}n| j                  dk(  r|df}nt        d      t        |d   t              st        |d   t              rt        |d   t              r|d   j                  |d   j                  dk\  rS|d   j                  |d   j                  | j                  dz   k  r%t        |d   j                  | j                         }nt        d      |d   g}t        |d   t              r|d   j                  |d   j                  dk\  rS|d   j                  |d   j                  | j                  dz   k  r%t        |d   j                  | j                         }nt        d      |d   g}t        || j                  j                        rt        |      |j                  k(  rdt        |      |j                  k(  rLt        |      D ]=  \  }}t        |      D ]*  \  }}| j!                  ||f|j#                  ||f             , ? nt%        d      | j                  j'                  |      }|D ]  }|D ]  }| j!                  ||f|         nw|d   | j                  k\  s|d   | j                  k\  rt        d      | j                  j'                  |      }|r|| j(                  |<   n|| j(                  v r| j(                  |= | j*                  rd | _        y )Nr
   r   r^   r_   r`   zDimensions do not matchra   )r   r   rb   r   r   rc   rd   re   r   rf   r   r   r   r3   r1   r   rg   rh   
ValueErrorconvertr   r   )	r   rX   r[   r3   ri   r    rk   r"   rl   s	            r&   __setitem__z_matrix.__setitem__  s    c3:c%#8{{a#h!Ah !BCCc!fU#z#a&'?#a&'FLL(CFLLA,=V[[(CFKK4;;q=,H!3q6>>$++#>?D$%>?? Ax#a&'FLL(CFLLA,=V[[(CFKK4;;q=,H$c!fnnT[[&ABG$%ABB q6(%0t9

*s7|uzz/I( R!#,W#5 RCAa ..!ue6I6I1Q%6PQRR %%>?? ((/ 9A$ 9**Aa5%899 1v$A$++(= !<==HH$$U+E#(C #KK$88DHr(   c              #      K   t        | j                        D ]%  }t        | j                        D ]  }| ||f     ' y wrC   )r   r   r   rQ   s      r&   __iter__z_matrix.__iter__:  sC     $ 	 ADKK(  1Q3i 	 s   ?Ac                     t         j                  j                        rɉ j                  j                  k7  rt        d       j                  j                   j                  j                        }t         j                        D ][  t        j                        D ]A   j                  j                   fdt        j                        D              |f<   C ] |S  j                  j                   j                   j                        }t         j                        D ]+  t         j                        D ]   f   z  |f<    - |S )Nz,dimensions not compatible for multiplicationc              3   :   K   | ]  }|f   |f   f  y wrC    ).0kr    r"   otherr   s     r&   	<genexpr>z"_matrix.__mul__.<locals>.<genexpr>G  s0      .D)* 04AaCy%!*.E .D   )r   r   r   r   r   ro   r   fdotr   ry   r+   r    r"   s   `` @@r&   __mul__z_matrix.__mul__?  s*   eTXX__-{{ell* !OPP((//$++u||<CDKK( D- DA $ .D.4U\\.B.D !DC1IDD J ((//$++t{{;CDKK( 3, 3A %QT
 2C1I33 Jr(   c                 $    | j                  |      S rC   )r~   r   ry   s     r&   
__matmul__z_matrix.__matmul__R      ||E""r(   c                 z    t        || j                  j                        rt        d      | j	                  |      S )Nz&other should not be type of ctx.matrix)r   r   r   r   r~   r   s     r&   __rmul__z_matrix.__rmul__U  s/    eTXX__-DEE||E""r(   c                    t        |t              st        d      | j                  | j                  k(  st        d      |}|dk(  r%| j
                  j                  | j                        S |dk  r| }d}nd}|}d}| j                         }|dk7  r|dz  dk(  r||z  }||z  }|dz  }|dk7  r|r| j
                  j                  |      }|S )Nz$only integer exponents are supportedz*only powers of square matrices are definedr   TFr
   r   )	r   r   ro   r   r   r   eyecopyinverse)r   ry   r<   negr    rl   zs          r&   __pow__z_matrix.__pow__[  s     %%CDD{{dkk)IJJ688<<,,q5ACCIIK1f1uzE!AQA	 1f
   #Ar(   c                 0   t        || j                  j                        rJ | j                  j                  | j                  | j                        }t        | j                        D ]+  }t        | j                        D ]  }| ||f   |z  |||f<    - |S rC   )r   r   r   r   r   r   r}   s        r&   __div__z_matrix.__div__v  s    eTXX__555hhoodkk4;;7$ 	-ADKK( -!9u,AaC-	- 
r(   c                    t        || j                  j                        r| j                  |j                  k(  r| j                  |j                  k(  st        d      | j                  j                  | j                  | j                        }t        | j                        D ]0  }t        | j                        D ]  }| ||f   |||f   z   |||f<    2 |S | j                  j                  | j                  | j                        }t        | j                        D ]3  }t        | j                        D ]  }|||fxx   | ||f   |z   z  cc<    5 |S )Nz$incompatible dimensions for addition)r   r   r   r   r   ro   r   r}   s        r&   __add__z_matrix.__add__  s(   eTXX__-KK5<</DKK5<<4O !GHH((//$++t{{;CDKK( 6, 6A#AaCy51:5C!H66 J ((//$++t{{;CDKK( 2, 2A!HQqS	E 11H22 Jr(   c                 $    | j                  |      S rC   )r   r   s     r&   __radd__z_matrix.__radd__  r   r(   c                     t        || j                  j                        r=| j                  |j                  k(  r| j                  |j                  k(  st        d      | j                  |dz        S )Nz'incompatible dimensions for subtractionr.   )r   r   r   r   r   ro   r   r   s     r&   __sub__z_matrix.__sub__  sR    eTXX__-t{{ell7R26++2MFGG||ERL))r(   c                     d| z  S )zO
        +M returns a copy of M, rounded to current working precision.
        r
   rv   rD   s    r&   __pos__z_matrix.__pos__  s     d{r(   c                     d| z  S )Nr.   rv   rD   s    r&   __neg__z_matrix.__neg__  s    d{r(   c                     |  |z   S rC   rv   r   s     r&   __rsub__z_matrix.__rsub__  s    uu}r(   c                     | j                   |j                   k(  xr4 | j                  |j                  k(  xr | j                  |j                  k(  S rC   )r   r   r   r   s     r&   __eq__z_matrix.__eq__  s@    {{ell* /t{{ell/J /;;%,,.	/r(   c                     | j                   dk(  r| j                  S | j                  dk(  r| j                   S | j                   S Nr
   )r3   r1   rD   s    r&   __len__z_matrix.__len__  s6    99>99YY!^9999r(   c                     | j                   S rC   )r   rD   s    r&   	__getrowsz_matrix.__getrows      {{r(   c                 |    | j                   j                         D ]  }|d   |k\  s| j                   |=  || _        y )Nr   )r   r   r   r   r[   rX   s      r&   	__setrowsz_matrix.__setrows  >    ;;##% 	%C1vKK$	% r(   znumber of rows)docc                     | j                   S rC   )r   rD   s    r&   	__getcolsz_matrix.__getcols  r   r(   c                 |    | j                   j                         D ]  }|d   |k\  s| j                   |=  || _        y r   )r   r   r   r   s      r&   	__setcolsz_matrix.__setcols  r   r(   znumber of columnsc                     | j                   j                  | j                  | j                        }t	        | j                        D ](  }t	        | j                        D ]  }| ||f   |||f<    * |S rC   )r   r   r   r   r   )r   r+   r    r"   s       r&   	transposez_matrix.transpose  sj    hhoodkk4;;7$ 	%ADKK( %!9AaC%	% 
r(   c                 L    | j                  | j                  j                        S rC   )r,   r   conjrD   s    r&   	conjugatez_matrix.conjugate  s    zz$((--((r(   c                 >    | j                         j                         S rC   )r   r   rD   s    r&   transpose_conjz_matrix.transpose_conj  s    ~~))++r(   c                     | j                   j                  | j                  | j                        }| j                  j                         |_        |S rC   )r   r   r   r   r   r   )r   r+   s     r&   r   z_matrix.copy  s7    hhoodkk4;;7[[%%'

r(   c                     | j                   j                  | j                  d      }t        | j                        D ]  }| ||f   ||<    |S r   )r   r   r3   r2   )r   r<   rj   r    s       r&   columnz_matrix.column  sH    HHOODIIq)tyy! 	A!9AaD	r(   rC   )F)-__name__
__module____qualname____doc__r'   r,   rA   rE   rO   r   rU   rh   rg   rm   rq   rs   r~   r   r   r   r   __truediv__r   r   r   r   r   r   r   r   _matrix__getrows_matrix__setrowspropertyr3   _matrix__getcols_matrix__setcolsr1   r   Tr   r   Hr   __copy__r   rv   r(   r&   r   r   	   s   M^3Cj ,.U
!!?%BGR 
&##6 K"#*/ Iy.>?D Iy.ABD 	A), 	 A
 Hr(   r   c                   V    e Zd Zd Zd Zd Zd Zd ZddZddZ	d	 Z
d
 ZddZddZy)MatrixMethodsc                     t        dt        fi       | _        | | j                  _        | j                  | j                  _        y )Nr   )typer   r   r   rp   )r   s    r&   r'   zMatrixMethods.__init__  s/    (WJ3


 [[

r(   c                 Z     | j                   |fi |}t        |      D ]	  }d|||f<    |S )z6
        Create square identity matrix n x n.
        r
   )r   r   )r   r<   r   r   r    s        r&   r   zMatrixMethods.eye  s>     CJJq#F# 	AAacF	r(   c                      | j                   t        |      fi |}t        t        |            D ]  }||   |||f<    |S )a&  
        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   diagonalr   r   r    s        r&   diagzMatrixMethods.diag  sK     CJJs8}//H& 	!Aa[AacF	!r(   c                    t        |      dk(  r|d   x}}n0t        |      dk(  r|d   }|d   }nt        dt        |      z         | j                  ||fi |}t        |      D ]  }t        |      D ]	  }d|||f<     |S )a&  
        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']])
        r
   r   r   z*zeros expected at most 2 arguments, got %ir   r   r   r   r   r   r   rj   r<   r   r    r"   s           r&   zeroszMatrixMethods.zeros
  s     t9>GOAY!^QAQAH3t9TUUCJJq!&v& 	AAY !A#	 r(   c                    t        |      dk(  r|d   x}}n0t        |      dk(  r|d   }|d   }nt        dt        |      z         | j                  ||fi |}t        |      D ]  }t        |      D ]	  }d|||f<     |S )a#  
        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']])
        r
   r   r   z)ones expected at most 2 arguments, got %ir   r   s           r&   oneszMatrixMethods.ones$  s     t9>GOAY!^QAQAG#d)STTCJJq!&v& 	AAY !A#	 r(   Nc                     ||}| j                  ||      }t        |      D ],  }t        |      D ]  }| j                  ||z   dz   z  |||f<    . |S )z
        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.
        r
   )r   r   one)r   rj   r<   r   r    r"   s         r&   hilbertzMatrixMethods.hilbert>  si     9AJJq! 	/AAY /AEAI.!A#/	/ r(   c                     |s|} | j                   ||fi |}t        |      D ]0  }t        |      D ]   }| j                         ||z
  z  |z   |||f<   " 2 |S )aZ  
        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   rand)	r   rj   r<   minr7   r   r   r    r"   s	            r&   
randmatrixzMatrixMethods.randmatrixN  sq     ACJJq!&v& 	8AAY 8sSy1C7!A#8	8 r(   c                     ||k(  ryt        || j                        r4t        |j                        D ]  }|||f   |||f   c|||f<   |||f<    yt        |t              r||   ||   c||<   ||<   yt        d      )z(
        Swap row i with row j.
        Nzcould not interpret type)r   r   r   r1   r   r   )r   r   r    r"   rx   s        r&   swap_rowzMatrixMethods.swap_rowd  s     6a$AFF^ 0!"1Q31Q3!A#!A#04 1qtJAaD!A$677r(   c                 :   t        || j                        st        d      |j                  t	        |      k7  rt        d      |j                         }|xj                  dz  c_        t        |j                        D ]  }||   |||j                  dz
  f<    |S )zB
        Extend matrix A with column b and return result.
        z A should be a type of ctx.matrixzValue should be equal to len(b)r
   )	r   r   r   r3   r   ro   r   r1   r   )r   r   br    s       r&   extendzMatrixMethods.extendr  s     !SZZ(>??66SV>??FFH	! 	"AqTAakN	"r(   c                     	 t        |       t              t        ur j                         j                  k(  rt         fd|D              S dk(  r j                  |d      S dk(  r# j                   j                  |dd            S dkD  r* j                   j                  fd|D                    S t        d      # t        $ r  j                  |      cY S w xY w)a  
        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   @   K   | ]  }j                  |        y wrC   )absmax)rw   r    r   s     r&   rz   z%MatrixMethods.norm.<locals>.<genexpr>  s     0szz!}0s   r
   absoluter   )r   squaredc              3   :   K   | ]  }t        |      z    y wrC   )abs)rw   r    ps     r&   rz   z%MatrixMethods.norm.<locals>.<genexpr>  s     '=aA	'=r{   zp has to be >= 1)iterr   r   r   r   rp   infr7   fsumsqrtnthrootro   )r   rk   r   s   ` `r&   normzMatrixMethods.norm  s    D	!G 7#AA<0a000!V88A8**!V88CHHQAH>??U;;sxx'=1'==qAA/00  	!::a= 	!s   C C+*C+c                      j                        t        |      t        urSt        |      t        u r1dj	                  |j                               r j                  d      S  j                  |      }j                  j                  c|dk(  rt         fdt              D              S | j                  k(  rt         fdt              D              S t        d      )a  
        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')

        	frobeniusr   r
   c              3   l   K   | ]*  j                  fd t              D        d       , yw)c              3   ,   K   | ]  }|f     y wrC   rv   )rw   r    r   r"   s     r&   rz   z0MatrixMethods.mnorm.<locals>.<genexpr>.<genexpr>        ;A1Q3 ;   r
   r   Nr   r   )rw   r"   r   r   rj   s    @r&   rz   z&MatrixMethods.mnorm.<locals>.<genexpr>  (     \Qsxx ; ;axH\   04c              3   l   K   | ]*  j                  fd t              D        d       , yw)c              3   ,   K   | ]  }|f     y wrC   rv   )rw   r"   r   r    s     r&   rz   z0MatrixMethods.mnorm.<locals>.<genexpr>.<genexpr>  r   r   r
   r   Nr   )rw   r    r   r   r<   s    @r&   rz   z&MatrixMethods.mnorm.<locals>.<genexpr>  r   r   zmatrix p-norm for arbitrary p)r   r   r   r6   
startswithlowerr   rp   r3   r1   r7   r   r   NotImplementedError)r   r   r   rj   r<   s   `` @@r&   mnormzMatrixMethods.mnorm  s    N JJqM7#Aw#~+"8"8"Cxx1~%AAvvqvv16\RXYZR[\\\#''\\RXYZR[\\\%&EFFr(   rC   )Nr   r
   )r   )r
   )r   r   r   r'   r   r   r   r   r   r   r   r   r   r   rv   r(   r&   r   r     s;    )$44 ,811f2Gr(   r   __main__)libmp.backendr   r   r;   r9   objectr   r   r   doctesttestmodrv   r(   r&   <module>r      sU    "  
	\f \|~GF ~G@ zGOO r(   