ó
Ë½÷Xc           @` 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 l 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	   ExpFormat(   t   string_typest   MalformedHeadert   hb_readt   hb_writet   HBInfot   HBFilet   HBMatrixTypec           B` s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/io/harwell_boeing/hb.pyR   $   s   t   LineOverflowc           B` s   e  Z RS(    (   R   R   (    (    (    s9   /tmp/pip-build-7oUkmx/scipy/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   /tmp/pip-build-7oUkmx/scipy/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` s>  | 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 | ƒ sBt d ƒ ‚ n  | j
 j t j d k rd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
        i   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   /tmp/pip-build-7oUkmx/scipy/scipy/io/harwell_boeing/hb.pyt   _nlinesl   s    N(   t   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   /tmp/pip-build-7oUkmx/scipy/scipy/io/harwell_boeing/hb.pyt	   from_data3   sF    				%%				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(    s4  Create a HBInfo instance from a file object containg 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   (   s   reals   integers              (   t   readlinet   stript   lenR.   t   rstript   _expect_intt   upperR   t   from_fortrant
   value_typet	   structuret   storaget   split(   R0   t   fidt   lineR2   R3   RA   R>   R?   R@   t
   rhs_nlinest   mxtype_sR4   R7   R8   R9   t   nelementalst   ct(    (    s9   /tmp/pip-build-7oUkmx/scipy/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(   s   reals   complex(   s   integer(%   R2   R3   R$   RG   R.   t   warningst   warnR   RA   R>   R?   R@   R   t   parset
   isinstanceR   R   RL   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_fullR7   R8   R9   RT   R4   (   t   selfR2   R3   RA   R>   R?   R@   R4   R7   R8   R9   t   pointer_format_strt   indices_format_strt   values_format_strt   right_hand_sides_nlinesRT   t   parserR^   R_   R`   Rd   (    (    s9   /tmp/pip-build-7oUkmx/scipy/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   
(   R2   t   ljustR3   t   appendRA   R>   R?   R@   R4   R/   R7   R8   R9   R^   R_   R`   t   join(   Rh   t   headert   pffmtt   iffmtt   vffmt(    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/io/harwell_boeing/hb.pyt   dump  s    %			,N(   R   R   t   classmethodR$   RB   RV   Rn   Rv   (    (    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/io/harwell_boeing/hb.pyR   2   s
   J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]   R.   R$   (   t   valuet   msg(    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/io/harwell_boeing/hb.pyRI   .  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(   NRC   R(   t   sepRD   i   R"   (   Rq   t   readRe   RE   R&   t
   fromstringR]   Rf   Rg   Rd   R   R7   R8   R.   (	   t   contentRr   t
   ptr_stringt   ptrt
   ind_stringt   indt
   val_stringt   valt   e(    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/io/harwell_boeing/hb.pyt   _read_hb_data7  s"    c         C` s‰   d „  } | j  | j ƒ  ƒ | j  d ƒ | | |  j d | j | j ƒ | | |  j d | j | j ƒ | | |  j | j	 | j
 ƒ d  S(   Nc   	      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   /tmp/pip-build-7oUkmx/scipy/scipy/io/harwell_boeing/hb.pyt   write_arrayP  s    	#s   
i   (   Rˆ   Rv   R   R>   R^   R    R?   R_   R!   R@   R`   (   R1   RP   Rr   R‘   (    (    s9   /tmp/pip-build-7oUkmx/scipy/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   CRW   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(   RG   R.   t	   _f2q_typet   _f2q_structuret   _f2q_storaget   KeyError(   R0   R   RL   RM   RN   (    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/io/harwell_boeing/hb.pyRK   ƒ  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(   RL   RM   RN   t	   _q2f_typeR.   t   _q2f_structuret   _q2f_storage(   Rh   RL   RM   RN   (    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/io/harwell_boeing/hb.pyRn     s    			c         C` s*   |  j  |  j |  j |  j |  j |  j S(   N(   R§   RL   R¨   RM   R©   RN   (   Rh   (    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/io/harwell_boeing/hb.pyR/   œ  s    c         C` s   d |  j  |  j |  j f S(   Ns   HBMatrixType(%s, %s, %s)(   RL   RM   RN   (   Rh   (    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/io/harwell_boeing/hb.pyt   __repr__¢  s    (   R   R   t   __doc__R§   R¨   R©   t   dictt   itemst   it   jR£   R¤   R¥   Rw   RK   Rn   t   propertyR/   Rª   (    (    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/io/harwell_boeing/hb.pyR   j  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   RV   t   _hb_info(   Rh   t   filet   hb_info(    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/io/harwell_boeing/hb.pyRn   ¨  s    	c         C` s
   |  j  j S(   N(   R²   R2   (   Rh   (    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/io/harwell_boeing/hb.pyR2   »  s    c         C` s
   |  j  j S(   N(   R²   R3   (   Rh   (    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/io/harwell_boeing/hb.pyR3   ¿  s    c         C` s   |  j  j j S(   N(   R²   R4   RL   (   Rh   (    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/io/harwell_boeing/hb.pyt   typeÃ  s    c         C` s   |  j  j j S(   N(   R²   R4   RM   (   Rh   (    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/io/harwell_boeing/hb.pyRM   Ç  s    c         C` s   |  j  j j S(   N(   R²   R4   RN   (   Rh   (    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/io/harwell_boeing/hb.pyRN   Ë  s    c         C` s   t  |  j |  j ƒ S(   N(   R…   R±   R²   (   Rh   (    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/io/harwell_boeing/hb.pyt   read_matrixÏ  s    c         C` s   t  | |  j |  j ƒ S(   N(   R’   R±   R²   (   Rh   R1   (    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/io/harwell_boeing/hb.pyt   write_matrixÒ  s    N(   R   R   R$   Rn   R°   R2   R3   Rµ   RM   RN   R¶   R·   (    (    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/io/harwell_boeing/hb.pyR   §  s   	c         C` sQ   d „  } t  |  t ƒ rC t |  ƒ } z | | ƒ SWd | j ƒ  Xn
 | |  ƒ Sd S(   sf  Read HB-format file.

    Parameters
    ----------
    file : str-like or file-like
        If a string-like object, file is the name of the file to read. If a
        file-like object, the data are read from it.

    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¶   (   RP   t   hb(    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/io/harwell_boeing/hb.pyt   _get_matrixî  s    N(   R[   R   t   opent   close(   R³   R¹   RP   (    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/io/harwell_boeing/hb.pyR	   Ö  s    	c         ` s{   ˆ  d k r t j ˆ ƒ ‰  n  ‡  ‡ f d †  } t |  t ƒ rm t |  d ƒ } z | | ƒ SWd | j ƒ  Xn
 | |  ƒ Sd S(   s€  Write HB-format file.

    Parameters
    ----------
    file : str-like or file-like
        if a string-like object, file is the name of the file to read. If a
        file-like object, the data are read from it.
    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

    c         ` s   t  |  ˆ  ƒ } | j ˆ ƒ S(   N(   R   R·   (   RP   R¸   (   R´   R1   (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/io/harwell_boeing/hb.pyt   _set_matrix  s    t   wN(   R$   R   RB   R[   R   Rº   R»   (   R³   R1   R´   R¼   RP   (    (   R´   R1   s9   /tmp/pip-build-7oUkmx/scipy/scipy/io/harwell_boeing/hb.pyR
   ü  s    (    R«   t
   __future__R    R   R   RX   t   numpyR&   t   scipy.sparseR   t.   scipy.io.harwell_boeing._fortran_format_parserR   R   R   t   scipy._lib.sixR   t   __all__t	   ExceptionR   t   WarningR   R   t   objectR   R$   RI   R…   R’   R   R   R	   R
   (    (    (    s9   /tmp/pip-build-7oUkmx/scipy/scipy/io/harwell_boeing/hb.pyt   <module>   s&   		ü			=/	&