
$Oc           @   s  d  d l  Z  d  d l Z d  d l Z d  d l Z d  d l Z d  d l 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 d  Z d d  Z d   Z d d  Z d d  Z d   Z d d  Z d d  Z d d  Z d   Z d   Z d   Z d   Z  d  f  d!     YZ! d d"  Z" d d#  Z# d d$  Z$ d%   Z% d&   Z& d'   Z' d(   Z( d d)  Z) d S(*   iNc         C   sv   |  j    \ } } | j    \ } } | | k s< | | k rl t j d | | | | | f IJt j d  n  | | g S(   Ns$   %s: mismatched lengths %dx%d, %dx%d.i   (   t   dimst   syst   stderrt   exit(   t   At   Bt	   func_namet   anrt   anct   bnrt   bnc(    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   check_same_matrix_dims"   s     c         C   sp   |  j    \ } } | j    \ } } | | k r` t j d | | | | | f IJt j d  n  | | | | g S(   Ns(   %s: mismatched mul lengths %dx%d, %dx%d.i   (   R    R   R   R   (   R   R   R   R   R   R	   R
   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   check_mul_matrix_dims,   s     c         C   sM   d g | } g  } x- t  d |   D] } | j t j |   q# Wt |  S(   Ni    (   t   ranget   appendt   copyt   sackmat(   t   nrt   nct   rowt   eltst   i(    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   make_zero_matrix7   s
    c         C   s;   t  |  |   } x% t d |   D] } d | | | <q W| S(   Ni    i   (   R   R   (   t   nt   IR   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   make_identity_matrix?   s    c         C   se   t  |  |   } d } xI t d |   D]8 } x/ t d |   D] } | d 7} | | | | <q; Wq% W| S(   Ni    i   (   R   R   (   R   R   t   kR   t   j(    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   make_seq_matrixK   s    
c         C   s   t  |  |   } d } xI t d |   D]8 } x/ t d |   D] } | d 7} | | | | <q; Wq% Wx. t d |   D] } | | | | | | <qq W| S(   Ni    i   (   R   R   (   R   R   R   R   R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   make_nseq_matrixU   s    
c         C   s\   g  } |  j    \ } } t |  } x1 t d |  D]  } | j t |  | |   q4 W| S(   Ni    (   R    t   lenR   R   t   vecdot(   R   t   vt   AvR   R   R   R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   matrix_times_vectora   s    c         C   sI   t  |   } g  } x0 t d |  D] } | j |  | | |  q" W| S(   Ni    (   R   R   R   (   t   uR    R   t   wR   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   vecaddj   s
    c         C   sI   t  |   } g  } x0 t d |  D] } | j |  | | |  q" W| S(   Ni    (   R   R   R   (   R#   R    R   R$   R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   vecsubs   s
    c         C   sM   t  |   } g  } x4 t d |  D]# } | j |  | | | |  q" W| S(   Ni    (   R   R   R   (   R#   R    t   sR   R$   R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   vecsub_with_smul|   s
    !c         C   sF   d } t  |   } x- t d |  D] } | |  | | | 7} q" W| S(   Ni    (   R   R   (   R#   R    R'   R   R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyR      s
    c         C   s   t  j t |  |    S(   N(   t   matht   sqrtR   (   R#   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   vecnorm   s    c         C   s,   g  } x |  D] } | j  | |  q W| S(   N(   R   (   R#   R'   R    t   ue(    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   vecsmul   s    c         C   s   t  |  d t |    S(   Ng      ?(   R-   R+   (   R#   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   vechat   s    s   %9.6fc         C   s7   t  |   } x# t d |  D] } | |  | Gq WHd  S(   Ni    (   R   R   (   R    t   formatR   R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   print_row_vector   s    c         C   s7   t  |   } x$ t d |  D] } | |  | GHq Wd  S(   Ni    (   R   R   (   R    R/   R   R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   print_column_vector   s    c         C   s   g  } t  j  |   } | d d k r5 | d d !} n  t j d d |  } t j d d |  } | d k ro | St j d |  } x' | D] } | |  } | j |  q W| S(   Nis   
i    s   ^\s+t    s   \s+$s   \s+(   R   t   ret   subt   splitR   (   t	   orig_linet   elt_scannerR    t   linet   stringsR'   t   elt(    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   row_vector_from_string   s    t   -c         C   s   g  } | d k r t  j } n? y t | d  } Wn) t  j d | d IJt  j d  n Xd } x | d k r | j   } qf Wt | |   } | g  k r t  j d IJt  j d  n  | d k r | j   n  | S(   NR<   t   rs   Couldn't open "s   " for read.i   R2   s%   sackmat read_row_vector: empty input.(   R   t   stdint   openR   R   t   readlineR;   t   close(   R7   t	   file_nameR    t   file_handleR8   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   read_row_vector   s$    c         C   s   g  } | d k r t  j } n? y t | d  } Wn) t  j d | d IJt  j d  n Xx8 | j   } | d k r| Pn  |  |  } | j |  q` | d k r | j   n  | S(   NR<   R=   s   Couldn't open "s   " for read.i   R2   (   R   R>   R?   R   R   R@   R   RA   (   R7   RB   R    RC   R8   R:   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   read_column_vector   s"    c         C   s   d g | } d | |  <| S(   Ni    i   (    (   R   R   t   ei(    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   stdbv   s    
gHz>c         C   sP   t  |   } x7 t d |  D]& } t |  |  | k r d | g Sq Wd d g S(   Ni    i   (   R   R   t   abs(   R    t   tolR   R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   find_leader_pos  s
    c         C   s   t  |   | k  r d Sd Sd  S(   Ni   i    (   RH   (   t   xRI   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   tol_zero  s    c         C   s>   t  |   } x+ t d |  D] } t |  |  s d Sq Wd S(   Ni    i   (   R   R   RL   (   R    RI   R   R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   vector_is_zero  s
    c         C   sM   t  |   } t |  |  } t | |  } t | t |  t |   } | S(   N(   R   R   R-   t   float(   R#   t   aR   t   uat   aat   u_par(    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   projpar*  s
    c         C   s"   t  |  |  } t |  |  } | S(   N(   RS   R&   (   R#   RO   RR   t   u_perp(    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   projperp2  s    c         C   s   d } t  |   } t |  |   } t |  } | | k r d | } xX t d |  D]D } x; t d |  D]* } | | | c |  | |  | | 8<qi WqS Wn  | S(   Ng|=g       @i    (   R   R   R   R   (   R    RI   R   t   v_dot_vt   Qt   two_over_v_dot_vR   R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   householder_vector_to_Q;  s    
/c         C   s   t  |   } | d k  r9 t j | d IJt j d  n  t  |  d  } | d k  rv t j | d IJt j d  n  xA |  D]9 } t  |  | k r} t j | d IJt j d  q} q} W| | g S(   Ni   s   :  no rows.i    s   :  empty row.s   :  ragged input.(   R   R   R   R   (   R   R   R   R   R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   validate_matrixI  s    R   c           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 d   Z d   Z d   Z d   Z d   Z d   Z d d  Z d d  Z d   Z d   Z d   Z d   Z d d d  Z d   Z d   Z d d   Z d!   Z  d"   Z! d#   Z" d$   Z# d%   Z$ d&   Z% d' d(  Z& d) d*  Z' d) d+  Z( d) d,  Z) d) d-  Z* d.   Z+ d/   Z, d0   Z- d1   Z. RS(2   c         C   s   t  j |  |  _ d  S(   N(   R   t   deepcopyt   elements(   t   selfR\   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   __init__\  s    c         C   s    t  |  j  t  |  j d  g S(   Ni    (   R   R\   (   R]   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyR    `  s    c         C   s?   |  j    \ } } | | k r; t j d IJt j d  n  | S(   Ns   Non-square input.i   (   R    R   R   R   (   R]   R   R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt
   square_dimb  s
    c         C   s   t  |  j  S(   N(   R   R\   (   R]   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   num_rowsi  s    c         C   s   t  |  j d  S(   Ni    (   R   R\   (   R]   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   num_colsk  s    c         C   s[   |  j    \ } } xB t d |  D]1 } x( t d |  D] } d |  j | | <q8 Wq" Wd  S(   Ni    g        (   R    R   R\   (   R]   R   R   R   R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   fill_zero_matrixn  s    c         C   s   |  j    } xB t d |  D]1 } x( t d |  D] } d |  j | | <q2 Wq Wx( t d |  D] } d |  j | | <qa Wd  S(   Ni    g        g      ?(   R_   R   R\   (   R]   R   R   R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   fill_identity_matrixs  s    c         C   s   |  j  | S(   N(   R\   (   R]   R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   __getitem__{  s    c         C   s   | |  j  | <d  S(   N(   R\   (   R]   R   t   value(    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   __setitem__~  s    c         C   s   t  |  | d  \ } } t | |  } xS t d |  D]B } x9 t d |  D]( } |  | | | | | | | | <qM Wq7 W| S(   Ns   sackmat addi    (   R   R   R   (   R   R   R   R   t   CR   R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   __add__  s    *c         C   s   t  |  | d  \ } } t | |  } xS t d |  D]B } x9 t d |  D]( } |  | | | | | | | | <qM Wq7 W| S(   Ns   sackmat subi    (   R   R   R   (   R   R   R   R   Rg   R   R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   __sub__  s    *c   
   	   C   s   t  |  | d  \ } } } } t | |  } x t d |  D]p } xg t d |  D]V } d | | | <x? t d |  D]. }	 | | | c |  | |	 | |	 | 7<qw WqS Wq= W| S(   Ns   sackmat muli    (   R   R   R   (
   R   R   R   R   R	   R
   Rg   R   R   R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   __mul__  s    4c         C   sp   |  j    \ } } t | |  } xH t d |  D]7 } x. t d |  D] } |  | | | | | <qG Wq1 W| S(   Ni    (   R    R   R   (   R   R   R   Rg   R   R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   __neg__  s    c         C   sv   t  |  | d  |  j   \ } } xM t d |  D]< } x3 t d |  D]" } | j | | |  j | | <qH Wq2 Wd  S(   Nt   copy_elements_fromi    (   R   R    R   R\   (   R]   t   otherR   R   R   R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyRl     s
    c         C   sv   t  |  | d  |  j   \ } } xM t d |  D]< } x3 t d |  D]" } |  j | | | j | | <qH Wq2 Wd  S(   Nt   copy_elements_toi    (   R   R    R   R\   (   R]   Rm   R   R   R   R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyRn     s
    c         C   sv   |  j    \ } } t | |  } xN t d |  D]= } x4 t d |  D]# } | |  j | | | | | <qG Wq1 W| S(   Ni    (   R    R   R   R\   (   R]   R'   R   R   Rg   R   R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   smul  s    %c         C   s`   |  j    \ } } | d k s* | d k rQ t j d | | f IJt j d  n  |  j d d S(   Ni   s;   sackmat to_scalar: input dimensions must be 1x1; got %dx%d.i    (   R    R   R   R   R\   (   R]   R   R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt	   to_scalar  s    
c         C   sr   |  j    \ } } t | |  } xJ t d |  D]9 } x0 t d |  D] } |  j | | | | | <qG Wq1 W| S(   Ni    (   R    R   R   R\   (   R]   R   R   Rg   R   R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt	   transpose  s    !c         C   s   |  j    } xo t d |  D]^ } xU t d |  D]D } |  j | | } |  j | | |  j | | <| |  j | | <q2 Wq Wd  S(   Ni    (   R_   R   R\   (   R]   R   R   R   t   temp(    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   transpose_in_place  s    c         C   s   |  j    \ } } d } xu t d |  D]d } t |  j | d  } x6 t d |  D]% } | d t |  j | |  7} qU W| | d 7} q( W| S(   NR2   i    i   t    s   
(   R    R   t   strR\   (   R]   R   R   t
   mat_stringR   t
   row_stringR   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   __str__  s    #s   %9.6fc         C   s   d | GH|  j  |  Hd  S(   Ns   %s:(   t   printf(   R]   t   nameR/   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   printp  s    	c         C   s   |  j    \ } } t |  j d d t  r x t d |  D]M } xC t d |  D]2 } | |  j | | j G| |  j | | j GqR WHq< WnH xE t d |  D]4 } x* t d |  D] } | |  j | | Gq WHq Wd  S(   Ni    (   R    t
   isinstanceR\   t   complexR   t   realt   imag(   R]   R/   R   R   R   R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyRy     s    c         C   sN   |  j    \ } } g  } x/ t | |  D] } | j |  j | |  q( W| S(   N(   R    R   R   R\   (   R]   t   colidxt	   start_rowR   R   t   submatrix_columnt   src(    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   get_submatrix_column  s
    c         C   sU   |  j    \ } } d } x6 t | |  D]% } | | |  j | | <| d 7} q( Wd  S(   Ni    i   (   R    R   R\   (   R]   R   R   t   columnR   R   R   t   dst(    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   put_submatrix_column  s    c         C   sL  |  j    \ } } | j    \ } } | | | } }	 t | |	  }
 x t d |  D] } | } x} t d |	  D]l } | | } d } xE t d |  D]4 } | | } | | j | | |  j | | 7} q W| |
 | | <qp WqT Wxa t d |  D]P } | | } x= t d |	  D], } | | } |
 j | | |  j | | <qWq Wd  S(   Ni    g        (   R    R   R   R\   (   R]   R4   t   sub_start_rowt   self_start_colt   self_nrt   self_nct   sub_nrt   sub_nct   tmp_nrt   tmp_nct   tmpt   tmpit   subit   tmpjt   selfjRe   t   subkt   selfkt   selfi(    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   premultiply_by_submatrix-  s$    

(

c         C   s@  |  j    \ } } | j    \ } } | | } } t | |  }	 x t d |  D] }
 |
 } xy t d |  D]h } | } d } xE t d |  D]4 } | | } | |  j | | | j | | 7} q W| |	 |
 | <ql WqP Wx] t d |  D]L }
 |
 } x= t d |  D], } | | } |	 j |
 | |  j | | <qWq Wd  S(   Ni    g        (   R    R   R   R\   (   R]   R4   t   sub_start_colR   R   R   R   R   R   R   R   R   R   t   subjRe   R   R   R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt#   postmultiply_by_submatrix_transposeR  s$    
(
gh㈵>g        c         C   s   |  j    \ } } | | } |  j | |  } t j t | |   } | d d k r` | } n  d g | }	 | |	 d <t | |	  }
 t |
  } |  j | | |  t | t	  s | j | | d  n  d  S(   Ni    (
   R    R   R)   R*   R   R&   RY   R   R|   RN   (   R]   t   submxidxRI   t   arg_QR   R   t   heightR#   t   v0R    t   axisRW   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt    householder_UT_pass_on_submatrixt  s    


c         C   sX   |  j    \ } } | } | | k r- | } n  x$ t d |  D] } |  j |  q= Wd  S(   Ni    (   R    R   R   (   R]   R   R   t   num_HHR   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   householder_UT  s    	c         C   s   d } |  j    \ } } | } | | k r3 | } n  | j   |  j |  x* t d |  D] } | j | | |  qZ W| j   d  S(   Ngh㈵>i    (   R    Rc   Rn   R   R   Rs   (   R]   RW   t   RRI   R   R   R   R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt	   QR_decomp  s    	
c         C   s  |  j    } | } | | d } | d k  rV t j d | | | f IJt j d  n  |  j | | d  } t j t | |   } | d d k r | } n  d g | } | | d <t | |  }	 t	 |	  }
 |
 j
 d  |  j |
 | d |  |  j |
 | d  d  S(   Ni   sN   upper_hessenberg_pass_on_submatrix:  column index %d out of bounds in %d x %d.i    RW   (   R_   R   R   R   R   R)   R*   R   R&   RY   R{   R   R   (   R]   R   RI   R   R   R   R#   R   R    R   RW   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt"   upper_hessenberg_pass_on_submatrix  s&    

c         C   s.   |  j    } x t d | d  D] } q  Wd  S(   Ni   i   (   R_   R   (   R]   R   R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   upper_hessenberg  s    c         C   s\   |  j    } t |  j  } | j   d } x* t d |  D] } | | | | 9} q; W| S(   Ng      ?i    (   R_   R   R\   R   R   (   R]   R   R   t   rvR   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   det  s    
c         C   sE   |  j    } d } x, t d |  D] } | |  j | | 7} q" W| S(   Ng        i    (   R_   R   R\   (   R]   R   R   R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   trace2  s
    c         C   s   |  j    } | d @r7 t j d | IJt j d  n  | d k re |  j d d |  j d d d Sd } d } xp t d |  D]_ } |  j d |  } |  j d | d k r | | |  j d | | j   } n  | d } q W| S(   Ni   s7   sackmat pfaffian: input dimension must be even; got %d.i   i    g      ?g        i(   R_   R   R   R   R\   R   t   pfaffian_hatt   pfaffian(   R]   t   Nt   signt   sumR   t   Ahat(    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyR   ?  s    

"&c   	      C   s   |  j    } t | d | d  } d } x t d |  D] } | | k r9 | | k r9 d } xR t d |  D]A } | | k rm | | k rm |  | | | | | <| d 7} qm qm W| d 7} q9 q9 W| S(   Ni   i    i   (   R_   R   R   (	   R]   R   R   R   R   t   dit   sit   djt   sj(    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyR   S  s    c         C   s   |  j    } t |  j  } d g | } x/ t d |  D] } | j | | | j | <q8 Wx) t d |  D] } d | | | | <qj W| S(   Ni    i   (   R_   R   R\   R   (   R]   R   t   AIt   ZR   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt	   augment_Ib  s    gư>c   
      C   s  |  j    } | | } |  j   } | j   x t d |  D] } | | | } | d k ry t j d IJt j d  n/ t |  | k  r t j d IJt j d  n  x1 t d |  D]  } | | | | | | | <q Wq< W| d } x | d k ru| d } xb | | k rg| | | }	 x7 t d |  D]& } | | | c | | | |	 8<q0W| d 8} qW| d 8} q Wx2 t d |  D]! } | j | | | !| j | <qW| S(   Ni    s	   Singular.i   s   Nearly singular.i   (	   R_   R   R   R   R   R   R   RH   R\   (
   R]   RI   R   t   twonR   R   t   dR   t   i2t   mul(    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   invo  s4    

"	

$gHz>c         C   s  |  j    \ } } d } d } x| | k  r| | k  r| } d } x | r | | k  r t |  j | |  | k r | | k r |  j | } |  j | |  j | <| |  j | <n  d } qH | d 7} qH W| s | d 7} q! n  |  j | | }	 t |	  | k rd |	 }
 x. t d |  D] } |  j | | c |
 9<qW|  j | | }	 x t | d |  D]p } |  j | | } |  j | } |  j | } x< t d |  D]+ } | | |	 | | | |  j | | <qWqeWn  | d 7} | d 7} q! Wd  S(   Ni    i   g      ?(   R    RH   R\   R   (   R]   RI   R   R   t   top_rowt   left_colt	   pivot_rowt   pivot_successfulRr   t   top_row_leadR   R   t   cur_rowt   current_row_leadt   crt   tr(    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   row_reduce_below  s@    	

0
c         C   s  |  j    \ } } |  j |  x t d |  D] } x t | d |  D] } t |  j | |  \ } } | su Pn  |  j | | } |  j | | }	 t |	  | k  r qI n  t |	  t |  }
 x= t d |  D], } |  j | | c |  j | | |
 8<q WqI Wq/ Wd  S(   Ni    i   (   R    R   R   RJ   R\   RH   RN   (   R]   RI   R   R   R   t   row2t   foundt   row2_leader_post   row2_leader_valt   row_clear_valR   R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   row_echelon_form  s    2c         C   s)   t  |  j  } | j |  | j |  S(   N(   R   R\   R   t   rank_rr(   R]   RI   t   Arr(    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   rank  s    c         C   s   |  j    \ } } d } xq t d |  D]` } d } x> t d |  D]- } t |  j | |  | k rD d } PqD qD W| s( | d 7} q( q( W| S(   Ni    i   (   R    R   RH   R\   (   R]   RI   R   R   R   R   t   row_is_zeroR   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyR     s    c         C   s  |  j    \ } } t |  j  } | j   | j   } | | } | d k rW d d g St | |  } d } d g | } d g | }	 x@ t d |  D]/ }
 t | |
  \ } } | r d | | <q q Wx8 t d |  D]' }
 | |
 r |
 |	 | <| d 7} q q Wx t d |  D] }
 d | |
 |	 |
 <x t d |  D]x } t | | |	 |
  r`q<n  t | |  \ } } | st	 j
 d IJt	 j d  n  | | |	 |
 | |
 | <q<WqW|  j | |  d | g S(   Ni    i   s!   Coding error in get_kernel_basis!(   R    R   R\   R   R   R   R   RJ   RL   R   R   R   t   check_kernel_basis(   R]   R   R   t   rrR   t   dimkert   kerbast   nfreet
   free_flagst   free_indicesR   R   t   dep_posR   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   kernel_basis  s>    




#c         C   s   x t  d |  D] } | | } t |  |  } t |  s t j d IJd GHH|  j d  d G| GH| j d  d t |  d GHt |  t j d  q q Wd  S(	   Ni    s   Coding error in kernel basis.t   Matrixs   dimker =t   Basiss   Product at row t   :i   (	   R   R"   RM   R   R   R{   Ru   R0   R   (   R]   R   R   R   R    R!   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyR   M  s    
 	
c         C   sN   |  j    \ } } g  } x/ t d |  D] } | j |  j | |  q( W| S(   Ni    (   R    R   R   R\   (   R]   R   R   R   R    R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt
   get_column]  s
    c         C   sE   |  j    \ } } x, t d |  D] } | | |  j | | <q" Wd  S(   Ni    (   R    R   R\   (   R]   R   R    R   R   R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt
   put_columne  s    (/   t   __name__t
   __module__R^   R    R_   R`   Ra   Rb   Rc   Rd   Rf   Rh   Ri   Rj   Rk   Rl   Rn   Ro   Rp   Rq   Rs   Rx   R{   Ry   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   (    (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyR   Z  sZ   												
					
			
		.	%	")	0	.						41		I		c         C   s   g  } d } | d k r$ t  j } n? y t | d  } Wn) t  j d | d IJt  j d  n Xxd | j   } | d k r Pn  t | |   } | g  k r | d k r Pq qf | j |  | d 7} qf | d k r | j   n  t	 | d  t
 |  S(	   Ni    R<   R=   s   Couldn't open "s   " for read.i   R2   t   read_matrix(   R   R>   R?   R   R   R@   R;   R   RA   RZ   R   (   R7   RB   R   R`   RC   R8   R    (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyR     s.    c         C   s   |  j  |  d  S(   N(   Ry   (   R   R/   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   print_matrix  s    c         C   s  |  j    \ } } | d k r* t j } n? y t | d  } Wn) t j d | d IJt j d  n Xt |  j d d t  r!xt	 d |  D] } xq t	 d |  D]` } | d k r | j
 d  n  | j
 | |  j | | j  | j
 | |  j | | j  q W| j
 d  q Wny xv t	 d |  D]e } xO t	 d |  D]> } | d k ri| j
 d  n  | j
 | |  j | |  qGW| j
 d  q1W| d k r| j   n  d  S(	   NR<   R$   s   Couldn't open "s   " for write.i   i    Rt   s   
(   R    R   t   stdoutR?   R   R   R|   R\   R}   R   t   writeR~   R   RA   (   R   RB   R/   R   R   RC   R   R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   write_matrix  s0    # c   
      C   s   t  |  |  } t | d  } | j |  | d  } t j d |  } | j   d } xP t d |   D]? } x6 t d |  D]% }	 | | | j | |	 <| d 7} q} Wqg W| j   | S(   Nt   rbi   t   fi    i   (   R   R?   t   readt   arrayt   byteswapR   R\   RA   (
   R   R   RB   R   t   fpt   bytesR   R   R   R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   read_float_matrix_binary  s    

c   
      C   s   t  |  |  } t | d  } | j |  | d  } t j d |  } | j   d } xa t d |   D]P } xG t d |  D]6 }	 t | | | | d  | j | |	 <| d 7} q} Wqg W| j   | S(   NR   i   R   i    i   i   (	   R   R?   R   R   R   R   R}   R\   RA   (
   R   R   RB   R   R   R   R   R   R   R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   read_fcomplex_matrix_binary  s    
&
c         C   s   |  j    \ } } t | d  } t j d  } xI t d |  D]8 } x/ t d |  D] } | j |  j | |  qV Wq@ W| j   | j |  | j   d  S(   Nt   wbR   i    (	   R    R?   R   R   R   R\   R   R   RA   (   R   RB   R   R   R   R   R   R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   write_float_matrix_binary  s     
c         C   s   |  j    \ } } t | d  } t j d  } xg t d |  D]V } xM t d |  D]< } | j |  j | | j  | j |  j | | j  qV Wq@ W| j   | j	 |  | j
   d  S(   NR   R   i    (   R    R?   R   R   R   R\   R~   R   R   R   RA   (   R   RB   R   R   R   R   R   R   (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   write_fcomplex_matrix_binary	  s    #
c         C   s  |  j    \ } } t |  j  } x t d |  D] } | | } x t d |  D] } | | } t | |  }	 t | |  }
 t |
  | k  r t j d I| Id IJt j d  n  |	 |
 } t	 | | |  } qQ W| | | <q1 Wx t d |  D] } | | } t | |  } | | k  rLt j d I| Id IJt j d  n  d t
 j |  } t | |  } | | | <q W| S(   Ni    s   Row s7    of Q is zero (or near-zero) in sackmat_m.gram_schmidt.i   g      ?(   R    R   R\   R   R   RH   R   R   R   R(   R)   R*   R-   (   R   RI   R   R   RW   R   t   qjR   t   qkt   numert   denomt   quott   dott
   norm_recip(    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   gram_schmidt!  s0    



(*   R   R   R3   R)   R   t   typesR   R   R   R   R   R   R"   R%   R&   R(   R   R+   R-   R.   R0   R1   R;   RD   RE   RG   RJ   RL   RM   RS   RU   RY   RZ   R   R   R   R   R   R   R   R   R   (    (    (    sJ   /homepages/25/d321765456/htdocs/pub_http_internet/python/test/sackmat_m.pyt   <module>   sX   	
				
																						   0"4				