
    (ph;L                         S r SSKrSSK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.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)	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       N/var/www/html/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cU  [        R                  " [        R                  " US-   5      5      n[        R                  " [        R                  " US-   5      5      nUR                  R                  [        R                  S   ;   a@  [        R                  " [        R                  " [        R                  " U5      5      * 5      nOUR                  R                  [        R                  S   ;   a@  [        R                  " [        R                  " [        R                  " U5      5      * 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   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>._nlinesi   s+    ZZ'F

"d*!Mr   )tocscindptrindicesdatashapennzr   from_numbernpmaxdtypekind	typecodesr   absNotImplementedError	isrealobj
ValueErrorHBMatrixTyper.   fortran_format)clsmtitlekeymxtyper   pointerr3   valuesnrowsncols
nnon_zerospointer_fmtindices_fmt
values_fmtmessagetpr/   pointer_nlinesindices_nlinesvalues_nlinestotal_nliness                         r   	from_dataHBInfo.from_data-   sD   * GGG(())wwUU
; $//wqy0ABK#//wqy0ABK||  BLL$<<&22BFF266&>4J3JK
""bll<&@@&22BFF266&>4J3JK
!&,,"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                 @   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-  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: z4Unexpected value %d for nltvl (last entry of line 3)zExpected 3 formats, got r      )readlinestriplenr@   rstrip_expect_intupperrA   from_fortran
value_type	structurestoragesplit)rC   fidlinerE   rF   rU   rR   rS   rT   
rhs_nlinesmxtype_srG   rJ   rK   rL   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S*+ , , ||~##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 TitlerZ   ztitle cannot be > 72 charactersz|No Key   zkey is > 8 characters (key is rb   ra   )
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 )$rf   r@   warningswarnr   rE   rF   rU   rR   rS   rT   r   parse
isinstancer   r   rk   r8   float64intpointer_formatindices_formatvalues_formatint32pointer_dtypeindices_dtypevalues_dtyper    pointer_nbytes_fullindices_nbytes_fullvalues_nbytes_fullrJ   rK   rL   rs   rG   )selfrE   rF   rU   rR   rS   rT   rG   rJ   rK   rL   pointer_format_strindices_format_strvalues_format_strright_hand_sides_nlinesrs   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                  SU R                  U R
                  U R                  U R                  4-  5        UR                  SU R                  R                  R                  S5      U R                  U R                  U R                  S4-  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.rZ   rx   z%14d%14d%14d%14dz%14s%14d%14d%14d%14dr\   r      z>16   z>20rY   )rE   ljustrF   appendrU   rR   rS   rT   rG   rB   rJ   rK   rL   r   r   r   join)r   headerpffmtiffmtvffmts        r   dumpHBInfo.dump  s'   **""2&)::;((($*=*=**D,>,>@@ 	A 	,{{1177;TZZzz4??A77 	8 ##22##22""11{{2BRJ 	Kyy  r   )r   r   r   rS   rF   rG   rK   rs   rL   rJ   r   r   r   rR   rE   rU   r   r   r   rT   )zDefault title0NN)r   r   )
r   r   r   r   classmethodrV   ru   r   r   r   r   r   r   r"   r"   ,   s?    K' K'Z R, R,p 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   r@   )valuemsges      r   rh   rh   ,  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$ )Nr_    )r:   sepr   )r5   )r   readr   rd   r8   
fromstringr   r   r   r   r   rJ   rK   )contentr   
ptr_stringptr
ind_stringind
val_stringvals           r   _read_hb_datar   5  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   rY   r   )python_formatr   reshapewritetupler.   )	farr   r   pyfmt
pyfmt_fullfullrownremains	            r   write_array _write_data.<locals>.write_arrayL  s     !!ZZ'
 ,FQJ#**,-<<3:: 67CGGJs+d23 8''DII%Q;GGU_b71B1C.D(EELM r   rY   r   )r1   r   r   r2   rR   r   r3   rS   r   r4   rT   r   )rD   ro   r   r   s       r   _write_datar   I  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 )rA   if  zClass to hold the matrix type.RCPI)r*   rz   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)Nra   z:Fortran format for matrix type should be 3 characters longr   r   rc   zUnrecognized format )rf   r@   	_f2q_type_f2q_structure_f2q_storageKeyError)rC   r   rk   rl   rm   r   s         r   rj   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 )rk   rl   rm   	_q2f_typer@   _q2f_structure_q2f_storage)r   rk   rl   rm   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   rk   r   rl   r   rm   r   s    r   rB   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, rb   )rk   rl   rm   r   s    r   __repr__HBMatrixType.__repr__  s,    t/r$..1ADLL>QRSSr   )rm   rl   rk   N)r,   )r   r   r   r   __doc__r   r   r   itemsr   r   r   r   rj   r   propertyrB   r   r   ).0ijs   000r   rA   rA   f  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;rA   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"   ru   _hb_info)r   filehb_infos      r   r   HBFile.__init__  s'     	?",,T2DM $Mr   c                 .    U R                   R                  $ r   )r   rE   r   s    r   rE   HBFile.title  s    }}"""r   c                 .    U R                   R                  $ r   )r   rF   r   s    r   rF   
HBFile.key  s    }}   r   c                 B    U R                   R                  R                  $ r   )r   rG   rk   r   s    r   typeHBFile.type  s    }}##...r   c                 B    U R                   R                  R                  $ r   )r   rG   rl   r   s    r   rl   HBFile.structure  s    }}##---r   c                 B    U R                   R                  R                  $ r   )r   rG   rm   r   s    r   rm   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   rD   s     r   write_matrixHBFile.write_matrix  s    1ii77r   )r   r   r   )r   r   r   r   r   r   rE   rF   r   rl   rm   r   r  r   r   r   r   r   r     su    $& # # ! ! / / . . , ,78r   r   T)spmatrixc                    S n[        U S5      (       a	  U" U 5      nO[        U 5       nU" U5      nSSS5        U(       a  [        W5      $ W$ ! , (       d  f       N"= f)an  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 ``coo_matrix``. Otherwise return ``coo_array``.

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   )ro   hbs     r   _get_matrixhb_read.<locals>._get_matrix  s    C[~~r   r   N)hasattropenr   )path_or_open_filer  r  r4   r   s        r   r	   r	     sX    R   &)),-#$q>D %$K	 %$s   	A
A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      $ [	        U S5       nU" U5      sSSS5        $ ! , (       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

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

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      $ r   )r   r  )ro   r  r   rD   s     r   _set_matrixhb_write.<locals>._set_matrix7  s    C!q!!r   r   w)r1   r"   rV   r
  r  )r  rD   r   r  r   s    ``  r   r
   r
     sl    T 	
UA""1%"  '**,--#S)Qq> *))s   A((
A6r   )r   r{   numpyr8   scipy.sparser   r   _fortran_format_parserr   r   r   __all__	Exceptionr   Warningr   r    r"   rh   r   r   rA   r   r	   r
   r   r   r   <module>r     s   
(   . M Mj
!	i 		7 	7}! }!@-N(&:9T 9Tx,8 ,8^ ,0 4n7"r   