ó
î&]\c           @` s,  d  Z  d d l m Z m Z m Z d d l Z d d l Z d d l m	 Z	 d d l
 m Z m Z m Z d d d d	 d
 d g Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d „  Z d	 e f d „  ƒ  YZ d d „ Z d „  Z d „  Z d e f d „  ƒ  YZ d
 e f d „  ƒ  YZ d „  Z d d „ Z d S(   s  
Implementation of Harwell-Boeing read/write.

At the moment not the full Harwell-Boeing format is supported. Supported
features are:

    - assembled, non-symmetric, real matrices
    - integer for pointer/indices
    - exponential format for float values, and int format

i    (   t   divisiont   print_functiont   absolute_importN(   t
   csc_matrix(   t   FortranFormatParsert	   IntFormatt	   ExpFormatt   MalformedHeadert   hb_readt   hb_writet   HBInfot   HBFilet   HBMatrixTypec           B` s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s9   lib/python2.7/site-packages/scipy/io/harwell_boeing/hb.pyR   "   s   t   LineOverflowc           B` s   e  Z RS(    (   R   R   (    (    (    s9   lib/python2.7/site-packages/scipy/io/harwell_boeing/hb.pyR   &   s   c         C` s   |  j  |  j d | d S(   sc   Return the number of bytes to read to get every full lines for the
    given parsed fortran format.i   (   t   repeatt   width(   t   fmtt   nlines(    (    s9   lib/python2.7/site-packages/scipy/io/harwell_boeing/hb.pyt   _nbytes_full*   s    c           B` sJ   e  Z e d  d d d d „ ƒ Z e d „  ƒ Z d d d „ Z d „  Z RS(   s   Default titlet   0c         C` sP  | j  d t ƒ } | j } | j } | j } | j \ }	 }
 | j } | d k rt j	 t
 j | d ƒ ƒ } t j	 t
 j | d ƒ ƒ } | j j t
 j d k rÇ t j	 t
 j t
 j | ƒ ƒ ƒ } q*| j j t
 j d k rt j	 t
 j t
 j | ƒ ƒ ƒ } q*t d | j j ƒ ‚ n t d ƒ ‚ | d k rÀt
 j | ƒ sTt d ƒ ‚ n  | j j t
 j d k rvd } n5 | j j t
 j d k r˜d	 } n t d
 | j ƒ ‚ t | d d ƒ } n t d ƒ ‚ d „  } | | | j ƒ } | | | j ƒ } | | | j ƒ } | | | } |  | | | | | | | |	 |
 | | j | j | j ƒ S(   sÙ  Create a HBInfo instance from an existing sparse matrix.

        Parameters
        ----------
        m : sparse matrix
            the HBInfo instance will derive its parameters from m
        title : str
            Title to put in the HB header
        key : str
            Key
        mxtype : HBMatrixType
            type of the input matrix
        fmt : dict
            not implemented

        Returns
        -------
        hb_info : HBInfo instance
        t   copyi   t   AllFloatt
   AllIntegers   type %s not implemented yets   fmt argument not supported yet.s    Complex values not supported yett   integert   reals"   type %s for values not implementedt   unsymmetrict	   assembleds    mxtype argument not handled yet.c         S` s1   | |  j  } | |  j  | k r- | d 7} n  | S(   Ni   (   R   (   R   t   sizeR   (    (    s9   lib/python2.7/site-packages/scipy/io/harwell_boeing/hb.pyt   _nlinesl   s    N(   t   tocsct   Falset   indptrt   indicest   datat   shapet   nnzt   NoneR   t   from_numbert   npt   maxt   dtypet   kindt	   typecodesR   t   abst   NotImplementedErrort	   isrealobjt
   ValueErrorR   R   t   fortran_format(   t   clst   mt   titlet   keyt   mxtypeR   t   pointerR"   t   valuest   nrowst   ncolst
   nnon_zerost   pointer_fmtt   indices_fmtt
   values_fmtt   tpR   t   pointer_nlinest   indices_nlinest   values_nlinest   total_nlines(    (    s9   lib/python2.7/site-packages/scipy/io/harwell_boeing/hb.pyt	   from_data1   sH    				%%				c         C` s  | j  ƒ  j d ƒ } t | ƒ d k s: t d | ƒ ‚ n  | d  } | d } | j  ƒ  j d ƒ } t | j ƒ  ƒ d k sŽ t d | ƒ ‚ n  t | d  ƒ } t | d d !ƒ } t | d d !ƒ } t | d d !ƒ } | d d !j ƒ  }	 |	 d	 k rÿ d
 }	 n t |	 ƒ }	 |	 d
 k s&t d ƒ ‚ n  | j  ƒ  j d ƒ } t | ƒ d k s`t d | ƒ ‚ n  | d  j ƒ  }
 t |
 ƒ d k s‘t d ƒ ‚ n  t j |
 ƒ } | j	 d k rÂt d | ƒ ‚ n  | j
 d k sät d | ƒ ‚ n  | j d k st d ƒ ‚ n  | d d !d k s(t d | ƒ ‚ n  t | d d !ƒ } t | d d !ƒ } t | d d !ƒ } t | d d !ƒ } | d
 k s“t d | ƒ ‚ n  | j  ƒ  j d ƒ } | j ƒ  } t | ƒ d k sÙt d | ƒ ‚ n  |  | | | | | | | | | | | d
 | d | d |	 | ƒ S(    s6  Create a HBInfo instance from a file object containing a matrix in the
        HB format.

        Parameters
        ----------
        fid : file-like matrix
            File or file-like object containing a matrix in the HB format.

        Returns
        -------
        hb_info : HBInfo instance
        s   
iH   s8   Expected at least 72 characters for first line, got: 
%si8   s9   Expected at least 56 characters for second line, got: 
%si   i   i*   t    i    s5   Only files without right hand side supported for now.iF   s6   Expected at least 72 character for third line, got:
%si   s'   mxtype expected to be 3 characters longR   R   s=   Only real or integer matrices supported for now (detected %s)R   s9   Only unsymmetric matrices supported for now (detected %s)R   s)   Only assembled matrices supported for nowt    i   s!   Malformed data for third line: %ss4   Unexpected value %d for nltvl (last entry of line 3)s   Expected 3 formats, got %si   i   (   R   R   s              (   t   readlinet   stript   lenR0   t   rstript   _expect_intt   upperR   t   from_fortrant
   value_typet	   structuret   storaget   split(   R2   t   fidt   lineR4   R5   RC   R@   RA   RB   t
   rhs_nlinest   mxtype_sR6   R9   R:   R;   t   nelementalst   ct(    (    s9   lib/python2.7/site-packages/scipy/io/harwell_boeing/hb.pyt	   from_file~   sl    

		i    c         C` sx  | |  _  | |  _ | d k r' d } n  t | ƒ d k rH t d ƒ ‚ n  | d k r] d } n  t | ƒ d k r† t j d | t ƒ n  | |  _ | |  _	 | |  _
 | |  _ t ƒ  } | j | ƒ } t | t ƒ sä t d | ƒ ‚ n  | j | ƒ } t | t ƒ st d | ƒ ‚ n  | j | ƒ } t | t ƒ rg| j d k r[t d | | f ƒ ‚ n  t j } nS t | t ƒ r§| j d k ržt d | | f ƒ ‚ n  t } n t d | f ƒ ‚ | |  _ | |  _ | |  _ t j |  _ t j |  _ | |  _ | |  _	 t | | ƒ |  _ | |  _
 t | | ƒ |  _ | |  _ t | | ƒ |  _ | |  _  |	 |  _! |
 |  _" | |  _# | |  _$ d S(   s@   Do not use this directly, but the class ctrs (from_* functions).s   No TitleiH   s   title cannot be > 72 characterss   |No Keyi   s!   key is > 8 characters (key is %s)s.   Expected int format for pointer format, got %ss.   Expected int format for indices format, got %sR   t   complexs6   Inconsistency between matrix type %s and value type %sR   s    Unsupported format for values %rN(   R   RY   (   R   (%   R4   R5   R&   RI   R0   t   warningst   warnR   RC   R@   RA   RB   R   t   parset
   isinstanceR   R   RN   R(   t   float64t   intt   pointer_formatt   indices_formatt   values_formatt   int32t   pointer_dtypet   indices_dtypet   values_dtypeR   t   pointer_nbytes_fullt   indices_nbytes_fullt   values_nbytes_fullR9   R:   R;   RV   R6   (   t   selfR4   R5   RC   R@   RA   RB   R6   R9   R:   R;   t   pointer_format_strt   indices_format_strt   values_format_strt   right_hand_sides_nlinesRV   t   parserR`   Ra   Rb   Rf   (    (    s9   lib/python2.7/site-packages/scipy/io/harwell_boeing/hb.pyt   __init__Ó   sh    																					c         C` sì   |  j  j d ƒ |  j j d ƒ g } | j d |  j |  j |  j |  j f ƒ | j d |  j j	 j d ƒ |  j
 |  j |  j d f ƒ |  j j	 } |  j j	 } |  j j	 } | j d | j d ƒ | j d ƒ | j d	 ƒ f ƒ d
 j | ƒ S(   s<   Gives the header corresponding to this instance as a string.iH   i   s   %14d%14d%14d%14ds   %14s%14d%14d%14d%14di   i    s   %16s%16s%20si   i   s   
(   R4   t   ljustR5   t   appendRC   R@   RA   RB   R6   R1   R9   R:   R;   R`   Ra   Rb   t   join(   Rj   t   headert   pffmtt   iffmtt   vffmt(    (    s9   lib/python2.7/site-packages/scipy/io/harwell_boeing/hb.pyt   dump  s    %			,N(   R   R   t   classmethodR&   RD   RX   Rp   Rx   (    (    (    s9   lib/python2.7/site-packages/scipy/io/harwell_boeing/hb.pyR
   0   s
   LYDc         C` sK   y t  |  ƒ SWn6 t k
 rF | d  k r3 d } n  t | |  ƒ ‚ n Xd  S(   Ns   Expected an int, got %s(   R_   R0   R&   (   t   valuet   msg(    (    s9   lib/python2.7/site-packages/scipy/io/harwell_boeing/hb.pyRK   .  s    	c   	      C` s  d j  |  j | j ƒ |  j ƒ  g ƒ } t j | d t d d ƒ} d j  |  j | j ƒ |  j ƒ  g ƒ } t j | d t d d ƒ} d j  |  j | j ƒ |  j ƒ  g ƒ } t j | d | j	 d d ƒ} y1 t
 | | d | d f d | j | j f ƒSWn t k
 r} | ‚ n Xd  S(   NRE   R*   t   sepRF   i   R$   (   Rs   t   readRg   RG   R(   t
   fromstringR_   Rh   Ri   Rf   R   R9   R:   R0   (	   t   contentRt   t
   ptr_stringt   ptrt
   ind_stringt   indt
   val_stringt   valt   e(    (    s9   lib/python2.7/site-packages/scipy/io/harwell_boeing/hb.pyt   _read_hb_data7  s"    c         C` s›   |  j  d t ƒ }  d „  } | j | j ƒ  ƒ | j d ƒ | | |  j d | j | j ƒ | | |  j d | j | j	 ƒ | | |  j
 | j | j ƒ d  S(   NR   c   	      S` sº   | j  } | | j } | | d | j  } x? | j | d | j f ƒ D]! } |  j | t | ƒ d ƒ qH W| j | j } | d k r¶ |  j | | t | | j | ƒ d ƒ n  d  S(   Ni   s   
i    (   t   python_formatR   t   reshapet   writet   tupleR   (	   t   ft   arR   R   t   pyfmtt
   pyfmt_fullt   fullt   rowt   nremain(    (    s9   lib/python2.7/site-packages/scipy/io/harwell_boeing/hb.pyt   write_arrayR  s    	#s   
i   (   R   R    RŠ   Rx   R!   R@   R`   R"   RA   Ra   R#   RB   Rb   (   R3   RR   Rt   R“   (    (    s9   lib/python2.7/site-packages/scipy/io/harwell_boeing/hb.pyt   _write_dataO  s    	

c           B` s3  e  Z d  Z i d d 6d d 6d d 6d d 6Z i d	 d
 6d d 6d d 6d d 6d d 6Z i d d 6d d 6Z e g  e j ƒ  D] \ Z Z	 e	 e f ^ q{ ƒ Z
 e g  e j ƒ  D] \ Z Z	 e	 e f ^ q¬ ƒ Z e g  e j ƒ  D] \ Z Z	 e	 e f ^ qÝ ƒ Z e d „  ƒ Z d d „ Z e d „  ƒ Z d „  Z RS(   s   Class to hold the matrix type.t   RR   t   CRY   t   Pt   patternt   IR   t   St	   symmetrict   UR   t   Ht	   hermitiant   Zt   skewsymmetrict   rectangulart   AR   t   Et	   elementalc         C` s   t  | ƒ d k s! t d ƒ ‚ n  yG |  j | d } |  j | d } |  j | d } |  | | | ƒ SWn! t k
 r‹ t d | ƒ ‚ n Xd  S(   Ni   s:   Fortran format for matrix type should be 3 characters longi    i   i   s   Unrecognized format %s(   RI   R0   t	   _f2q_typet   _f2q_structuret   _f2q_storaget   KeyError(   R2   R   RN   RO   RP   (    (    s9   lib/python2.7/site-packages/scipy/io/harwell_boeing/hb.pyRM   …  s    c         C` s…   | |  _  | |  _ | |  _ | |  j k r= t d | ƒ ‚ n  | |  j k r_ t d | ƒ ‚ n  | |  j k r t d | ƒ ‚ n  d  S(   Ns   Unrecognized type %ss   Unrecognized structure %ss   Unrecognized storage %s(   RN   RO   RP   t	   _q2f_typeR0   t   _q2f_structuret   _q2f_storage(   Rj   RN   RO   RP   (    (    s9   lib/python2.7/site-packages/scipy/io/harwell_boeing/hb.pyRp   ’  s    			c         C` s*   |  j  |  j |  j |  j |  j |  j S(   N(   R©   RN   Rª   RO   R«   RP   (   Rj   (    (    s9   lib/python2.7/site-packages/scipy/io/harwell_boeing/hb.pyR1   ž  s    c         C` s   d |  j  |  j |  j f S(   Ns   HBMatrixType(%s, %s, %s)(   RN   RO   RP   (   Rj   (    (    s9   lib/python2.7/site-packages/scipy/io/harwell_boeing/hb.pyt   __repr__¤  s    (   R   R   t   __doc__R©   Rª   R«   t   dictt   itemst   it   jR¥   R¦   R§   Ry   RM   Rp   t   propertyR1   R¬   (    (    (    s9   lib/python2.7/site-packages/scipy/io/harwell_boeing/hb.pyR   l  s,   


111c           B` sq   e  Z d d  „ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z	 d „  Z
 d „  Z RS(	   c         C` s7   | |  _  | d k r* t j | ƒ |  _ n	 | |  _ d S(   s  Create a HBFile instance.

        Parameters
        ----------
        file : file-object
            StringIO work as well
        hb_info : HBInfo, optional
            Should be given as an argument for writing, in which case the file
            should be writable.
        N(   t   _fidR&   R
   RX   t   _hb_info(   Rj   t   filet   hb_info(    (    s9   lib/python2.7/site-packages/scipy/io/harwell_boeing/hb.pyRp   ª  s    	c         C` s
   |  j  j S(   N(   R´   R4   (   Rj   (    (    s9   lib/python2.7/site-packages/scipy/io/harwell_boeing/hb.pyR4   ½  s    c         C` s
   |  j  j S(   N(   R´   R5   (   Rj   (    (    s9   lib/python2.7/site-packages/scipy/io/harwell_boeing/hb.pyR5   Á  s    c         C` s   |  j  j j S(   N(   R´   R6   RN   (   Rj   (    (    s9   lib/python2.7/site-packages/scipy/io/harwell_boeing/hb.pyt   typeÅ  s    c         C` s   |  j  j j S(   N(   R´   R6   RO   (   Rj   (    (    s9   lib/python2.7/site-packages/scipy/io/harwell_boeing/hb.pyRO   É  s    c         C` s   |  j  j j S(   N(   R´   R6   RP   (   Rj   (    (    s9   lib/python2.7/site-packages/scipy/io/harwell_boeing/hb.pyRP   Í  s    c         C` s   t  |  j |  j ƒ S(   N(   R‡   R³   R´   (   Rj   (    (    s9   lib/python2.7/site-packages/scipy/io/harwell_boeing/hb.pyt   read_matrixÑ  s    c         C` s   t  | |  j |  j ƒ S(   N(   R”   R³   R´   (   Rj   R3   (    (    s9   lib/python2.7/site-packages/scipy/io/harwell_boeing/hb.pyt   write_matrixÔ  s    N(   R   R   R&   Rp   R²   R4   R5   R·   RO   RP   R¸   R¹   (    (    (    s9   lib/python2.7/site-packages/scipy/io/harwell_boeing/hb.pyR   ©  s   	c         C` sE   d „  } t  |  d ƒ r" | |  ƒ St |  ƒ  } | | ƒ SWd QXd S(   sS  Read HB-format file.

    Parameters
    ----------
    path_or_open_file : path-like or file-like
        If a file-like object, it is used as-is. Otherwise it is opened
        before reading.

    Returns
    -------
    data : scipy.sparse.csc_matrix instance
        The data read from the HB file as a sparse matrix.

    Notes
    -----
    At the moment not the full Harwell-Boeing format is supported. Supported
    features are:

        - assembled, non-symmetric, real matrices
        - integer for pointer/indices
        - exponential format for float values, and int format

    c         S` s   t  |  ƒ } | j ƒ  S(   N(   R   R¸   (   RR   t   hb(    (    s9   lib/python2.7/site-packages/scipy/io/harwell_boeing/hb.pyt   _get_matrixð  s    R}   N(   t   hasattrt   open(   t   path_or_open_fileR»   RŒ   (    (    s9   lib/python2.7/site-packages/scipy/io/harwell_boeing/hb.pyR   Ø  s
    	
c         ` s   ˆ j  d t ƒ ‰ ˆ  d k r0 t j ˆ ƒ ‰  n  ‡  ‡ f d †  } t |  d ƒ r[ | |  ƒ St |  d ƒ  } | | ƒ SWd QXd S(   sm  Write HB-format file.

    Parameters
    ----------
    path_or_open_file : path-like or file-like
        If a file-like object, it is used as-is. Otherwise it is opened
        before writing.
    m : sparse-matrix
        the sparse matrix to write
    hb_info : HBInfo
        contains the meta-data for write

    Returns
    -------
    None

    Notes
    -----
    At the moment not the full Harwell-Boeing format is supported. Supported
    features are:

        - assembled, non-symmetric, real matrices
        - integer for pointer/indices
        - exponential format for float values, and int format

    R   c         ` s   t  |  ˆ  ƒ } | j ˆ ƒ S(   N(   R   R¹   (   RR   Rº   (   R¶   R3   (    s9   lib/python2.7/site-packages/scipy/io/harwell_boeing/hb.pyt   _set_matrix  s    RŠ   t   wN(   R   R    R&   R
   RD   R¼   R½   (   R¾   R3   R¶   R¿   RŒ   (    (   R¶   R3   s9   lib/python2.7/site-packages/scipy/io/harwell_boeing/hb.pyR	   û  s    
(   R­   t
   __future__R    R   R   RZ   t   numpyR(   t   scipy.sparseR   t.   scipy.io.harwell_boeing._fortran_format_parserR   R   R   t   __all__t	   ExceptionR   t   WarningR   R   t   objectR
   R&   RK   R‡   R”   R   R   R   R	   (    (    (    s9   lib/python2.7/site-packages/scipy/io/harwell_boeing/hb.pyt   <module>   s$   		þ			=/	#