
    BpjP                         S r SSKrSSKrSSKrSSKJr  SSKJrJ	r	  SSK
JrJrJr  SS/r " S	 S
\5      r " S S\5      rS r " S S5      rSS jrS rS r " S S5      r " S S5      r\S.S jrSS jrg)a  
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

    N)_NoValue)	csc_array
csc_matrix   )FortranFormatParser	IntFormat	ExpFormathb_readhb_writec                       \ rS rSrSrg)MalformedHeader     N__name__
__module____qualname____firstlineno____static_attributes__r       W/var/www/html/pdf-tiff/venv/lib/python3.13/site-packages/scipy/io/_harwell_boeing/hb.pyr   r           r   r   c                       \ rS rSrSrg)LineOverflow$   r   Nr   r   r   r   r   r   $   r   r   r   c                 F    U R                   U R                  -  S-   US-
  -  $ )z_Return the number of bytes to read to get every full lines for the
given parsed fortran format.r   )repeatwidth)fmtnliness     r   _nbytes_fullr!   (   s%     JJ"Q&6A:66r   c                   J    \ rS rSr\SS j5       r\S 5       r S	S jrS rSr	g)
HBInfo.   Nc                    UR                  SS9nUR                  nUR                  nUR                  nUR                  u  pUR
                  nUGce  [        R                  " [        R                  " US-   SS95      n[        R                  " [        R                  " US-   SS95      nUR                  R                  [        R                  S   ;   aS  [        R                  " [        R                  " [        R                  " U5      [        R                  " S5      S9* 5      nOUR                  R                  [        R                  S   ;   a?  [        R                  " [        R                  " [        R                  " U5      SS9* 5      nO0SUR                  R                   S3n[!        U5      e[!        S	5      eUc  [        R"                  " U5      (       d  [%        S
5      eUR                  R                  [        R                  S   ;   a  SnOGUR                  R                  [        R                  S   ;   a  SnO[!        SUR                   S35      e['        USS5      nO[%        S5      eS nU" XR(                  5      nU" XR(                  5      nU" XR(                  5      nUU-   U-   nU " X#UUUUXIXUR*                  UR*                  UR*                  5      $ )ab  Create a HBInfo instance from an existing sparse matrix.

Parameters
----------
m : sparse array or 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
Fcopyr   )initialAllFloat
AllIntegerztype z not implemented yetzfmt argument not supported yet.z Complex values not supported yetintegerrealz for values not implementedunsymmetric	assembledz mxtype argument not handled yet.c                 P    XR                   -  nX R                   -  U:w  a  US-  nU$ )Nr   )r   )r   sizer    s      r   _nlines!HBInfo.from_data.<locals>._nlinesn   s+    ZZ'F

"d*!Mr   )tocscindptrindicesdatashapennzr   from_numbernpmaxdtypekind	typecodesr	   absfloat64NotImplementedError	isrealobj
ValueErrorHBMatrixTyper0   fortran_format)clsmtitlekeymxtyper   pointerr5   valuesnrowsncols
nnon_zerospointer_fmtindices_fmt
values_fmtmessagetpr1   pointer_nlinesindices_nlinesvalues_nlinestotal_nliness                         r   	from_dataHBInfo.from_data/   s]   * GGG(())wwUU
; $//w{A0NOK#//w{A0NOK||  BLL$<<&22VVBFF6NBJJqMBBD
""bll<&@@&22VVBFF6NA668
 "&,,"3"3!44HI)'22%&GHH><<'' !CDD||  BLL$>>""bll:&>>)FLL>)DEG G!"m[AF?@@	 !ll; ll;
KK8%6F5..-5&&(B(B%%	' 	'r   c                 B   UR                  5       R                  S5      n[        U5      S:  d  [        SU 35      eUSS nUSS nUR                  5       R                  S5      n[        UR	                  5       5      S:  d  [        SU 35      e[        USS 5      n[        USS 5      n[        USS	 5      n[        US	S 5      nUSS R                  5       n	U	S
:X  a  Sn	O[        U	5      n	U	S:X  d  [        S5      eUR                  5       R                  S5      n[        U5      S:  d  [        SU 35      eUSS R                  5       n
[        U
5      S:X  d  [        S5      e[        R                  U
5      nUR                  S;  a  [        SU S35      eUR                  S:X  d  [        SU S35      eUR                  S:X  d  [        S5      eUSS S:X  d  [        SU 35      e[        USS 5      n[        USS	 5      n[        US	S 5      n[        USS 5      nUS:X  d  [        SU S35      eUR                  5       R                  S5      nUR                  5       n[        U5      S:X  d  [        SU 35      eU " X4XVXxXXUS   US   US   X5      $ )zCreate 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

H   z6Expected at least 72 characters for first line, got: 
N8   z7Expected at least 56 characters for second line, got: 
      *    r   z5Only files without right hand side supported for now.F   z4Expected at least 72 character for third line, got:
   z'mxtype expected to be 3 characters long)r,   r+   z:Only real or integer matrices supported for now (detected )r-   z6Only unsymmetric matrices supported for now (detected r.   z)Only assembled matrices supported for nowz           zMalformed data for third line: zUnexpected value z! for nltvl (last entry of line 3)zExpected 3 formats, got r      )readlinestriplenrC   rstrip_expect_intupperrD   from_fortran
value_type	structurestoragesplit)rF   fidlinerH   rI   rX   rU   rV   rW   
rhs_nlinesmxtype_srJ   rM   rN   rO   nelementalscts                    r   	from_fileHBInfo.from_file   s    ||~##D)4y2~ ''+f. / /Sb	23i ||~##D)4;;=!R' ''+f. / /"49-$T"R[1$T"R[1#DBK0"R[&&(
J$Z0JQ $ % % ||~##D)4yB  &&*V- . . 8>>#8}!FGG**84$77 ..4XQ8 9 9=0 ..4XQ8 9 9~~,HIIAbzX%>tfEFFDBK(DBK( b-
!$r"+.a#K=0QR 
 ||~##D)ZZ\2w!|7t<==5%a5"Q%A	, 	,r   c                 l   Uc  Sn[        U5      S:  a  [        S5      eUc  Sn[        U5      S:  a  [        R                  " SU S3[        S	S
9  Xl        X l        X0l        X@l        XPl	        X`l
        [        5       nUR                  U5      n[        U[        5      (       d  [        SU 35      eUR                  U5      n[        U[        5      (       d  [        SU 35      eUR                  U5      n[        U[        5      (       a2  UR                   S;  a  [        SU SU 35      e["        R$                  nOL[        U[        5      (       a(  UR                   S;  a  [        SU SU 35      e[&        nO[        SU< 35      eUU l        UU l        UU l        ["        R.                  U l        ["        R.                  U l        UU l        X@l        [7        UU5      U l        XPl	        [7        UU5      U l        X`l
        [7        UU5      U l        Xl        Xl         Xl!        Xl"        Xpl#        g)z@Do not use this directly, but the class ctrs (from_* functions).NzNo Titler]   ztitle cannot be > 72 charactersz|No Key   zkey is > 8 characters (key is re   rd   )
stacklevelz,Expected int format for pointer format, got z,Expected int format for indices format, got )r,   complexz"Inconsistency between matrix type z and value type )r+   zUnsupported format for values )$ri   rC   warningswarnr   rH   rI   rX   rU   rV   rW   r   parse
isinstancer   r	   rn   r:   r@   intpointer_formatindices_formatvalues_formatint32pointer_dtypeindices_dtypevalues_dtyper!   pointer_nbytes_fullindices_nbytes_fullvalues_nbytes_fullrM   rN   rO   rv   rJ   )selfrH   rI   rX   rU   rV   rW   rJ   rM   rN   rO   pointer_format_strindices_format_strvalues_format_strright_hand_sides_nlinesrv   parserr   r   r   r   s                        r   __init__HBInfo.__init__   s:    =Eu:?>??;Cs8a<MM:3%qA&16
(,,*$&&89.)44K ./1 2 2  &89.)44K ./1 2 2 %67mY//  (;; #EfX N//<o"? @ @::Ly11  3 #EfX N//<o"? @ @ L=m=NOPP,,*XXXX(,#/#O ,#/#O *".}m"L

$&r   c                    U R                   R                  S5      U R                  R                  S5      -   /nUR                  U R                  S U R
                  S U R                  S U R                  S 35        UR                  U R                  R                  R                  S5      S U R                  S U R                  S U R                  S SS 35        U R                  R                  nU R                  R                  nU R                  R                  nUR                  UR                  S5      S UR                  S5      S UR                  S	5      S
 35        SR!                  U5      $ )z<Gives the header corresponding to this instance as a string.r]   r{   14dr_   14sr      16s   20sr\   )rH   ljustrI   appendrX   rU   rV   rW   rJ   rE   rM   rN   rO   r   r   r   join)r   headerpffmtiffmtvffmts        r   dumpHBInfo.dump  sJ   **""2&)::;**3/0C0CC/HI\I\]`Habfbtbtuxayz{3399"=cB4::cBRSWS]S]^aRbcgcrcrsvbwxyz}w~  	A##22##22""11R-ekk"oc-B5;;r?SVBWXYyy  r   )r   r   r   rV   rI   rJ   rN   rv   rO   rM   r   r   r   rU   rH   rX   r   r   r   rW   )zDefault title0NN)r   r   )
r   r   r   r   classmethodrY   rx   r   r   r   r   r   r   r#   r#   .   s?    N' N'` S, S,r 45	GR!r   r#   c                 d     [        U 5      $ ! [         a  nUc  Sn[        X-  5      UeS nAff = f)NzExpected an int, got %s)r   rC   )valuemsges      r   rk   rk   -  s:    -5z -;+C%1,-s   
 
/*/c                 V   SR                  U R                  UR                  5      U R                  5       /5      n[        R
                  " U[        SS9nSR                  U R                  UR                  5      U R                  5       /5      n[        R
                  " U[        SS9nSR                  U R                  UR                  5      U R                  5       /5      n[        R
                  " UUR                  SS9n[        XuS-
  US-
  4UR                  UR                  4S9$ )Nrb    )r<   sepr   )r7   )r   readr   rg   r:   
fromstringr   r   r   r   r   rM   rN   )contentr   
ptr_stringptr
ind_stringind
val_stringvals           r   _read_hb_datar   6  s   ',,v'A'AB"++-/ 0J
--
3 C ',,v'A'AB'')+ ,J
--
3 C ',,v'@'@A!**,. /J
--
%%30C cq5#a%(v||0LMMr   c                    U R                  SS9n S nUR                  UR                  5       5        UR                  S5        U" XR                  S-   UR                  UR
                  5        U" XR                  S-   UR                  UR                  5        U" XR                  UR                  UR                  5        g )NFr&   c                    UR                   nXCR                  -  nUS US-
  UR                  -   nUR                  US-
  UR                  45       H#  nU R                  U[	        U5      -  S-   5        M%     UR
                  UR
                  -
  nUS:  a2  U R                  XH-  [	        XR
                  U-
  S  5      -  S-   5        g g )Nr   r\   r   )python_formatr   reshapewritetupler0   )	farr    r   pyfmt
pyfmt_fullfullrownremains	            r   write_array _write_data.<locals>.write_arrayM  s     !!ZZ'
 ,FQJ#**,-<<3:: 67CGGJs+d23 8''DII%Q;GGU_b71B1C.D(EELM r   r\   r   )r3   r   r   r4   rU   r   r5   rV   r   r6   rW   r   )rG   rr   r   r   s       r   _write_datar   J  s    	UAN IIfkkmIIdOXXaZ!6!6%%'YYq[&"7"7%%'VVV11$$&r   c                      \ rS rSrSrSSSSS.rSS	S
SSS.rSSS.r\R                  5        V VVs0 s H  u  pX!_M	     snnn r	\R                  5        V VVs0 s H  u  pX!_M	     snnn r
\R                  5        V VVs0 s H  u  pX!_M	     snnn r\S 5       rSS jr\S 5       rS rSrgs  snnn f s  snnn f s  snnn f )rD   ig  zClass to hold the matrix type.RCPI)r,   r}   patternr+   SUHZ)	symmetricr-   	hermitianskewsymmetricrectangularAE)r.   	elementalc                     [        U5      S:X  d  [        S5      e U R                  US      nU R                  US      nU R                  US      nU " X#U5      $ ! [
         a  n[        SU 35      UeS nAff = f)Nrd   z:Fortran format for matrix type should be 3 characters longr   r   rf   zUnrecognized format )ri   rC   	_f2q_type_f2q_structure_f2q_storageKeyError)rF   r   rn   ro   rp   r   s         r   rm   HBMatrixType.from_fortran  s    3x1} / 0 0	Bs1v.J**3q62I&&s1v.Gzg66 	B3C59:A	Bs   >A 
A9%A44A9c                     Xl         X l        X0l        XR                  ;  a  [	        SU 35      eX R
                  ;  a  [	        SU 35      eX0R                  ;  a  [	        SU 35      eg )NzUnrecognized type zUnrecognized structure zUnrecognized storage )rn   ro   rp   	_q2f_typerC   _q2f_structure_q2f_storage)r   rn   ro   rp   s       r   r   HBMatrixType.__init__  sq    $"^^+1*>??///6ykBCC+++4WI>?? ,r   c                     U R                   U R                     U R                  U R                     -   U R                  U R
                     -   $ N)r   rn   r   ro   r   rp   r   s    r   rE   HBMatrixType.fortran_format  sE    ~~doo.""4>>23  ./ 	/r   c                 V    SU R                    SU R                   SU R                   S3$ )NzHBMatrixType(z, re   )rn   ro   rp   r   s    r   __repr__HBMatrixType.__repr__  s,    t/r$..1ADLL>QRSSr   )rp   ro   rn   N)r.   )r   r   r   r   __doc__r   r   r   itemsr   r   r   r   rm   r   propertyrE   r   r   ).0ijs   000r   rD   rD   g  s    ( 	I  N L
 #,//"34"3$!"34I'5';';'=>'=tqad'=>N%1%7%7%9:%9TQAD%9:L
B 
B
@ / /
TG 5>:s   B-B4?B;rD   c                   z    \ rS rSrSS jr\S 5       r\S 5       r\S 5       r\S 5       r	\S 5       r
S	 rS
 rSrg)HBFilei  Nc                 X    Xl         Uc  [        R                  U5      U l        gX l        g)zCreate 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)_fidr#   rx   _hb_info)r   filehb_infos      r   r   HBFile.__init__  s'     	?",,T2DM $Mr   c                 .    U R                   R                  $ r   )r   rH   r   s    r   rH   HBFile.title  s    }}"""r   c                 .    U R                   R                  $ r   )r   rI   r   s    r   rI   
HBFile.key  s    }}   r   c                 B    U R                   R                  R                  $ r   )r   rJ   rn   r   s    r   typeHBFile.type  s    }}##...r   c                 B    U R                   R                  R                  $ r   )r   rJ   ro   r   s    r   ro   HBFile.structure  s    }}##---r   c                 B    U R                   R                  R                  $ r   )r   rJ   rp   r   s    r   rp   HBFile.storage  s    }}##+++r   c                 B    [        U R                  U R                  5      $ r   )r   r   r   r   s    r   read_matrixHBFile.read_matrix  s    TYY66r   c                 B    [        XR                  U R                  5      $ r   )r   r   r   )r   rG   s     r   write_matrixHBFile.write_matrix  s    1ii77r   )r   r   r   )r   r   r   r   r   r   rH   rI   r   ro   rp   r  r  r   r   r   r   r   r     su    $& # # ! ! / / . . , ,78r   r   )spmatrixc                T   S n[        U S5      (       a	  U" U 5      nO[        U 5       nU" U5      nSSS5        U[        L aB  Sn[        R                  R                  [        5      4n[        R                  " U[        US9  SnU(       a  [        W5      $ W$ ! , (       d  f       Nm= f)a  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.
spmatrix : bool, optional (default: True)
    If ``True``, return sparse matrix. Otherwise return sparse array.

    .. deprecated:: 1.18.0
        The default value for `spmatrix` is changing to False in v1.20.
        That means the default return value will be a sparse array.
        Unless you use * instead of @, ** for matrix power, or you depend
        on 2D shapes from e.g. ``A.sum(axis=0)``, it may not matter to you.
        See :ref:`Migration from spmatrix to sparray <migration_to_sparray>`.

Returns
-------
data : csc_array or csc_matrix
    The data read from the HB file as a sparse array.

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

Examples
--------
We can read and write a harwell-boeing format file:

>>> from scipy.io import hb_read, hb_write
>>> from scipy.sparse import csr_array, eye
>>> data = csr_array(eye(3))  # create a sparse array
>>> hb_write("data.hb", data)  # write a hb file
>>> print(hb_read("data.hb", spmatrix=False))  # read a hb file
<Compressed Sparse Column sparse array of dtype 'float64'
    with 3 stored elements and shape (3, 3)>
    Coords  Values
    (0, 0)  1.0
    (1, 1)  1.0
    (2, 2)  1.0
c                 8    [        U 5      nUR                  5       $ r   )r   r  )rr   hbs     r   _get_matrixhb_read.<locals>._get_matrix  s    C[~~r   r   Na  The default value for `spmatrix` is changing to `False` in v1.20.
            That means the default return type will be a sparse array.
            Unless you use * instead of @, ** for matrix power, or you depend
            on 2D shapes from e.g. `A.sum(axis=0)` it may not matter to you.
            See the spmatrix to sparray migration guide for details.
            https://docs.scipy.org/doc/scipy/reference/sparse.migration_to_sparray.html
            )skip_file_prefixesT)hasattropenr   ospathdirname__file__r~   r   DeprecationWarningr   )path_or_open_filer
  r  r6   r   r   prefixess          r   r
   r
     s    `   &)),-#$q>D % 8 GGOOH-/c-(K$K# %$s   	B
B'c                    ^^ TR                  SS9mTc  [        R                  T5      mUU4S jn[        U S5      (       a	  U" U 5        g[	        U S5       nU" U5        SSS5        g! , (       d  f       g= f)a  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 array or matrix
    the sparse array to write
hb_info : HBInfo
    contains the meta-data for write

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

Examples
--------
We can read and write a harwell-boeing format file:

>>> from scipy.io import hb_read, hb_write
>>> from scipy.sparse import csr_array, eye
>>> data = csr_array(eye(3))  # create a sparse array
>>> hb_write("data.hb", data)  # write a hb file
>>> print(hb_read("data.hb", spmatrix=False))  # read a hb file
<Compressed Sparse Column sparse array of dtype 'float64'
    with 3 stored elements and shape (3, 3)>
    Coords  Values
    (0, 0)  1.0
    (1, 1)  1.0
    (2, 2)  1.0
Fr&   Nc                 @   > [        U T5      nUR                  T5        g r   )r   r  )rr   r  r   rG   s     r   _set_matrixhb_write.<locals>._set_matrixH  s    C!
r   r   w)r3   r#   rY   r  r  )r  rG   r   r  r   s    ``  r   r   r     si    L 	
UA""1%  '**%&#S)QN *))s   	A))
A7r   )r   r  r~   numpyr:   scipy._lib.deprecationr   scipy.sparser   r   _fortran_format_parserr   r   r	   __all__	Exceptionr   Warningr   r!   r#   rk   r   r   rD   r   r
   r   r   r   r   <module>r&     s   
( 
   + . M Mj
!	i 		7 	7|! |!~-N(&:9T 9Tx,8 ,8^ ,4 HV3r   