
    (phR                        S r SSKrSSKrSSKrSSKJr  SSKrSSKr	SSK
JrJrJrJrJrJrJr  SSKJrJr  SSKJr  / SQr\R0                  S	:H  rSrSrS
rSrSrSr\S\S\S\S\S\SS/ SQSS0r \\\\\\\\\\\S.r!Sr"Sr#S
r$SSSSSS.r%\"S\#S\$S 0r&\RN                  " \RP                  5      RR                  r* " S! S"5      r+ " S# S$5      r, " S% S&\5      r-S,S' jr. " S( S)5      r/ " S* S+5      r0g)-z0Classes for read / write of matlab (TM) 4 files
    N)mul   )MatFileReader	docfillermatdims
read_dtypeconvert_dtypesarr_to_charsarr_dtype_number)squeeze_elementchars_to_strings)reduce)MatFile4ReaderMatFile4WriterSYS_LITTLE_ENDIAN
VarHeader4
VarReader4
VarWriter4	arr_to_2dmclass_infomdtypes_templatemiDOUBLEmiINT16miINT32miSINGLEmiUINT16miUINT8mxCHAR_CLASSmxFULL_CLASSmxSPARSE_CLASSnp_to_mtypesorder_codeslittle            f8f4i4i2u2u1header))moptr*   )mrowsr*   )ncolsr*   )imagfr*   )namlenr*   U1)r(   c32c24c16r)   c8r*   r+   r,   r-   S1<>zVAX D-floatzVAX G-floatCray)r   r   r$   r%   r&   doublecharsparsec                   "    \ rS rSrSrSrS rSrg)r   Y   Fc                 @    Xl         X l        X0l        X@l        XPl        g N)namedtypemclassdims
is_complex)selfrD   rE   rF   rG   rH   s         H/var/www/html/venv/lib/python3.13/site-packages/scipy/io/matlab/_mio4.py__init__VarHeader4.__init__^   s     	
	$    )rG   rE   rH   rF   rD   N)__name__
__module____qualname____firstlineno__
is_logical	is_globalrK   __static_attributes__ rM   rJ   r   r   Y   s    JI
%rM   r   c                   P    \ rS rSrSrS rS rSS jrSS jrS r	S r
S	 rS
 rSrg)r   k   z!Class to read matlab 4 variables c                     Xl         UR                  U l        UR                  U l        UR                  U l        UR                  U l        g rC   )file_reader
mat_streamdtypeschars_as_strings
squeeze_me)rI   rY   s     rJ   rK   VarReader4.__init__n   s=    &%00!(( + < <%00rM   c                 $   [        U R                  U R                  S   5      nU R                  R                  [	        US   5      5      R                  S5      nUS   S:  d	  US   S:  a  [        S5      e[        US   S5      u  p4US	;  a%  [        R                  " S
[        U    S3[        SS9  [        US5      u  pTUS:w  a  [        S5      e[        US5      u  pdUnUS   US   4nUS   S:H  n	U R                  U   n
[        UU
UUU	5      $ )z$Read and return header for variable r.   r3       r/   r     z.Mat 4 mopt wrong format, byteswapping problem?  )r   r   z!We do not support byte ordering 'z'; returned data may be corruptr%   )
stackleveld   z,O in MOPT integer should be 0, wrong format?
   r0   r1   r2   r   )r   rZ   r[   readintstrip
ValueErrordivmodwarningswarnr"   UserWarningr   )rI   datarD   MrestOPTrG   rH   rE   s              rJ   read_headerVarReader4.read_headeru   s&   $//4;;x+@A##CX$78>>wG<!tF|d2MNNft,F?MM=k!n=M N: :%!5 s#6KLLr"WtG}-']a'
A 	rM   c                 l   UR                   nU[        :X  a  U R                  U5      nOhU[        :X  a5  U R	                  U5      nU(       a  U R
                  (       a  [        U5      nO)U[        :X  a  U R                  U5      $ [        SU 35      eU(       a  U R                  (       a  [        U5      $ U$ )NNo reader for class code )rF   r   read_full_arrayr   read_char_arrayr\   r   r    read_sparse_array	TypeErrorr]   r   )rI   hdrprocessrF   arrs        rJ   array_from_headerVarReader4.array_from_header   s    \!&&s+C|#&&s+C400&s+~%))#..7x@AAt"3''
rM   c                    UR                   n[        [        UR                  [        R
                  " UR                  5      5      nU[        :  a(  [        SUR                  R                  S5       S35      eU R                  R                  U5      n[        U5      U:w  a(  [        SUR                  R                  S5       S35      e[        R                  " UR                  UUSS9nU(       a  UR                  5       nU$ )a  Mat4 read using header `hdr` dtype and dims

Parameters
----------
hdr : object
   object with attributes ``dtype``, ``dims``. dtype is assumed to be
   the correct endianness
copy : bool, optional
   copies array before return if True (default True)
   (buffer is usually read only)

Returns
-------
arr : ndarray
    of dtype given by `hdr` ``dtype`` and shape given by `hdr` ``dims``
z
Variable 'latin1zL' has byte length longer than largest possible NumPy array on this platform.z!Not enough bytes to read matrix 'z'; is this a badly-formed file? Consider listing matrices with `whosmat` and loading named matrices with `variable_names` kwarg to `loadmat`F)shaperE   bufferorder)rE   r   r   rG   npint64itemsize	_MAX_INTPri   rD   decoderZ   rf   lenndarraycopy)rI   r|   r   dt	num_bytesr   r~   s          rJ   read_sub_arrayVarReader4.read_sub_array   s    " YY3"((2;;*?@	y SXX__X67 8M NO O %%i0v;)#HHOOH-. /DEF F
 jjsxx! &"$ ((*C
rM   c                     UR                   (       a'  U R                  USS9nU R                  USS9nX#S-  -   $ U R                  U5      $ )zFull (rather than sparse) matrix getter

Read matrix (array) can be real or complex

Parameters
----------
hdr : ``VarHeader4`` instance

Returns
-------
arr : ndarray
    complex array if ``hdr.is_complex`` is True, otherwise a real
    numeric array
F)r   y              ?)rH   r   )rI   r|   resres_js       rJ   rx   VarReader4.read_full_array   sS     >>%%c%6C''%'8E"*%%""3''rM   c                 H   U R                  U5      R                  [        R                  5      nUR	                  5       R                  S5      n[        R                  " UR                  [        R                  " S5      [        R                  " U5      S9R                  5       $ )zlatin-1 text matrix (char matrix) reader

Parameters
----------
hdr : ``VarHeader4`` instance

Returns
-------
arr : ndarray
    with dtype 'U1', shape given by `hdr` ``dims``
latin-1r4   r   rE   r   )r   astyper   uint8tobytesr   r   rG   rE   arrayr   )rI   r|   r~   Ss       rJ   ry   VarReader4.read_char_array   sm     !!#&--bhh7KKM  +zz "!#!..2df	5rM   c                    U R                  U5      nUSS2SS24   n[        US   5      [        US   5      4n[        R                  " USS2S4   SS9n[        R                  " USS2S4   SS9nUS-  nUS-  nUR                  S   S	:X  a  [        R                  " USS2S
4   SS9nO*[        R                  " USS2S
4   SS9nUSS2S	4   Ul        [        R                  R                  XuU44U5      $ )a  Read and return sparse matrix type

Parameters
----------
hdr : ``VarHeader4`` instance

Returns
-------
arr : coo_array
    with dtype ``float`` and shape read from the sparse array data

Notes
-----
MATLAB 4 real sparse arrays are saved in a N+1 by 3 array format, where
N is the number of non-zero values. Column 1 values [0:N] are the
(1-based) row indices of the each non-zero value, column 2 [0:N] are the
column indices, column 3 [0:N] are the (real) values. The last values
[-1,0:2] of the rows, column indices are shape[0] and shape[1]
respectively of the output matrix. The last value for the values column
is a padding 0. mrows and ncols values from the header give the shape of
the stored matrix, here [N+1, 3]. Complex data are saved as a 4 column
matrix, where the fourth column contains the imaginary component; the
last value is again 0. Complex sparse data do *not* have the header
``imagf`` field set to True; the fact that the data are complex is only
detectable because there are 4 storage columns.
N)r   r   )r   r   r   intcrE   r   r%   r$   floatcomplex)	r   rg   r   ascontiguousarrayr   imagscipyr?   	coo_array)rI   r|   r   tmprG   IJVs           rJ   rz   VarReader4.read_sparse_array   s    6 !!#&#2#a%jCICI/  QqS7  QqS7	Q	Q99Q<1$$S1XG<A$$S1XI>A1XAF||%%qAi66rM   c                    UR                   nU[        :X  a%  [        [        [        UR
                  5      5      nGOU[        :X  a;  [        [        [        UR
                  5      5      nU R                  (       a  USS nGO:U[        :X  Ga!  UR                  nUR
                  n[        U5      S:X  a  US   S:  a	  US   S:  d  gU R                  R                  UR                  US   S-
  -  S5        [        R                  " SUU R                  R!                  UR                  5      S9nU R                  R                  UR                  US   S-
  -  S5        [        R                  " SUU R                  R!                  UR                  5      S9n[	        U5      [	        U5      4nO[#        SU 35      eU R$                  (       a#  [        U Vs/ s H  oS:w  d  M
  UPM     sn5      nU$ s  snf )	zgRead the shape of the array described by the header.
The file position after this call is unspecified.
Nr   r$   r   r   rU   r   rw   )rF   r   tuplemaprg   rG   r   r\   r    rE   r   rZ   seekr   r   r   rf   r{   r]   )	rI   r|   rF   r   r   rG   rowscolsxs	            rJ   shape_from_headerVarReader4.shape_from_header  s    \!#c388,-E|##c388,-E$$cr
~%B88DINtAw!|Q1 OO  Q!!<a@::Bb%)__%9%9"++%FHDOO  Q!!<a@::Bb%)__%9%9"++%FHD YD	*E7x@AA??e6eAv1e67E 7s   	G1"G1)r\   r[   rY   rZ   r]   NT)rN   rO   rP   rQ   __doc__rK   rt   r   r   rx   ry   rz   r   rT   rU   rM   rJ   r   r   k   s/    ,14"%N(,5$(7T rM   r   c                   b   ^  \ rS rSrSr\U 4S j5       rS rS rS r	SS jr
SS jrS	 rS
rU =r$ )r   i<  zReader for Mat4 files c                 <   > [         TU ]  " U/UQ70 UD6  SU l        g)zEInitialize matlab 4 file reader

%(matstream_arg)s
%(load_args)s
    N)superrK   _matrix_reader)rI   rZ   argskwargs	__class__s       rJ   rK   MatFile4Reader.__init__>  s$     	5d5f5"rM   c                 H   U R                   R                  S5        [        U R                   [        R                  " S5      5      nU R                   R                  S5        US:X  a  gUS:  d  US:  a  [
        =(       a    S=(       d    S$ [
        =(       a    S=(       d    S$ )Nr   r*   r:   ra   r;   )rZ   r   r   r   rE   r   )rI   r/   s     rJ   guess_byte_orderMatFile4Reader.guess_byte_orderH  su    Q$//288D>:Q19!8td{$,33 (S/C/rM   c                 b    [        [        U R                  5      U l        [	        U 5      U l        g)zPRun when beginning read of variables

Sets up readers from parameters in `self`
N)r	   r   
byte_orderr[   r   r   )rI   s    rJ   initialize_readMatFile4Reader.initialize_readT  s$    
 %%5tG(.rM   c                 P   U R                   R                  5       n[        [        UR                  [
        R                  " UR                  R                  5      5      nUR                  (       a  UR                  [        :X  d  US-  nU R                  R                  5       U-   nX4$ )a	  Read and return header, next position

Parameters
----------
None

Returns
-------
header : object
   object that can be passed to self.read_var_array, and that
   has attributes ``name`` and ``is_global``
next_position : int
   position in stream of next variable
r$   )r   rt   r   r   rG   r   r   rE   r   rH   rF   r    rZ   tell)rI   r|   remaining_bytesnext_positions       rJ   read_var_headerMatFile4Reader.read_var_header\  sy     !!--/ chh9K9K0LM>>#**">q O,,.@!!rM   c                 8    U R                   R                  X5      $ )aD  Read array, given `header`

Parameters
----------
header : header object
   object with fields defining variable header
process : {True, False}, optional
   If True, apply recursive post-processing during loading of array.

Returns
-------
arr : array
   array with post-processing applied or not according to
   `process`.
)r   r   )rI   r.   r}   s      rJ   read_var_arrayMatFile4Reader.read_var_arrays  s      ""44VEErM   c                 p   [        U[        5      (       a  U/nOUb  [        U5      nU R                  R	                  S5        U R                  5         0 nU R                  5       (       d  U R                  5       u  p4UR                  c  SOUR                  R                  S5      nUb"  XQ;  a  U R                  R	                  U5        Mv  U R                  U5      X%'   U R                  R	                  U5        Ub#  UR                  U5        [        U5      S:X  a   U$ U R                  5       (       d  M  U$ )zget variables from stream as dictionary

Parameters
----------
variable_names : None or str or sequence of str, optional
    variable name, or sequence of variable names to get from Mat file /
    file stream. If None, then get all variables in file.
r   Noner   )
isinstancestrlistrZ   r   r   end_of_streamr   rD   r   r   remover   )rI   variable_namesmdictr|   r   rD   s         rJ   get_variablesMatFile4Reader.get_variables  s    nc**,-N'!.1NQ$$&&!%!5!5!7C XX-6388??83LD)d.H$$]3--c2EKOO  /)%%d+~&!+ $$&& rM   c                    U R                   R                  S5        U R                  5         / nU R                  5       (       d  U R	                  5       u  p#UR
                  c  SOUR
                  R                  S5      nU R                  R                  U5      n[        R                  UR                  S5      nUR                  XEU45        U R                   R                  U5        U R                  5       (       d  M  U$ )zlist variables from stream r   r   r   unknown)rZ   r   r   r   r   rD   r   r   r   r   getrF   append)rI   varsr|   r   rD   r   infos          rJ   list_variablesMatFile4Reader.list_variables  s    Q$$&&!%!5!5!7C XX-6388??83LD''99#>E??3::y9DKKd+,OO  / $$&& rM   )r   r[   r   rC   )rN   rO   rP   rQ   r   r   rK   r   r   r   r   r   r   rT   __classcell__)r   s   @rJ   r   r   <  s<    !# #
0/".F$> rM   r   c                 n    [        X5      n[        U5      S:  a  [        S5      eU R                  U5      $ )a[  Make ``arr`` exactly two dimensional

If `arr` has more than 2 dimensions, raise a ValueError

Parameters
----------
arr : array
oned_as : {'row', 'column'}, optional
   Whether to reshape 1-D vectors as row vectors or column vectors.
   See documentation for ``matdims`` for more detail

Returns
-------
arr2d : array
   2-D version of the array
r$   z=Matlab 4 files cannot save arrays with more than 2 dimensions)r   r   ri   reshape)r~   oned_asrG   s      rJ   r   r     s:    " 3 D
4y1} ( ) 	);;trM   c                   N    \ rS rSrS rS rS r\\S4S jr	S r
S rS	 rS
 rSrg)r   i  c                 H    UR                   U l         UR                  U l        g rC   file_streamr   )rI   file_writers     rJ   rK   VarWriter4.__init__  s    &22"**rM   c                 T    U R                   R                  UR                  SS95        g )Nr   )r   )r   writer   )rI   r~   s     rJ   write_bytesVarWriter4.write_bytes  s!    s{{{56rM   c                 :    U R                   R                  U5        g rC   )r   r   )rI   ss     rJ   write_stringVarWriter4.write_string  s    q!rM   r   c                 >   [         R                  " S[        S   5      n[        (       + nSnUS-  US-  -   US-  -   U-   US'   US   US'   US	   US
'   XVS'   [	        U5      S	-   US'   U R                  U5        US-   n	U R                  U	R                  S5      5        g)a  Write header for given data options

Parameters
----------
name : str
    name of variable
shape : sequence
    Shape of array as it will be read in matlab
P : int, optional
    code for mat4 data type, one of ``miDOUBLE, miSINGLE, miINT32,
    miINT16, miUINT16, miUINT8``
T : int, optional
    code for mat4 matrix class, one of ``mxFULL_CLASS, mxCHAR_CLASS,
    mxSPARSE_CLASS``
imagf : int, optional
    flag indicating complex
rU   r.   r   rb   rd   re   r/   r0   r   r1   r2   r3    r   N)r   emptyr   r   r   r   r   encode)
rI   rD   r   rr   rs   r2   r.   ro   rq   rn   s
             rJ   write_headerVarWriter4.write_header  s    $ ".x89!!d(c'"b&! v  (w(wwt9q=x d{$++h/0rM   c                 4   [         R                  R                  U5      (       a  U R                  X5        g[        R
                  " U5      nUR                  nUR                  (       d   UR                  UR                  S5      5      nUR                  nU[        R                  L a  [        S5      eU[        R                  L a  [        S5      eU[        R                  [        R                  4;   a  U R!                  X5        gU R#                  X5        g)zWrite matrix `arr`, with name `name`

Parameters
----------
arr : array_like
   array to write
name : str
   name in matlab workspace
N=z!Cannot save object arrays in Mat4zCannot save void type arrays)r   r?   issparsewrite_sparser   asarrayrE   isnativer   newbyteordertypeobject_r{   voidstr_bytes_
write_charwrite_numeric)rI   r~   rD   r   dtts        rJ   r   VarWriter4.write  s     <<  %%c(jjoYY{{**R__S12Cgg"**?@@BGG^:;;RWWbii((OOC&3%rM   c                    [        XR                  5      nUR                  R                  S:H  n [        UR                  R
                  SS     nU R                  UUR                  U[        US9  U(       a7  U R                  UR                  5        U R                  UR                  5        g U R                  U5        g ! [         a3    U(       a  UR                  S5      nOUR                  S5      n[        n Nf = f)Ncr   c128r(   )rr   rs   r2   )r   r   rE   kindr!   r   KeyErrorr   r   r   r   r   r   realr   )rI   r~   rD   r2   rr   s        rJ   r
  VarWriter4.write_numeric  s    \\*		#%	SYY]]12./A 	$))( %	 	 	'
 SXX&SXX&S!  	jj(jj&A	s    C :C>=C>c                 z   UR                   R                  [        R                  :X  aC  UR                   R                  [        R                   " S5      R                  :w  a  [        U5      n[        XR                  5      nUR                  nU R                  UU[        [        S9  UR                   R                  S:X  aj  [        R                  " U5      n[        R                  " S[!        X5      US9nUR#                  5       R%                  S5      n[        R                  " USUS9nU R'                  U5        g )Nr4   rr   rs   UrU   r   r   r9   )rE   r  r   r  r   r
   r   r   r   r   r   r   r  mathprodr   r   itemr   r   )rI   r~   rD   rG   n_charsst_arrsts          rJ   r	  VarWriter4.write_char,  s    99>>RWW$););rxx~?V?V)Vs#C\\*yy	 	 	
 99>>S iioGZZb&6s&D'*,F %%i0B**4tB?CrM   c                 P   UR                  5       nUR                  R                  S:H  n[        R                  " UR
                  S-   SU-   4SS9nUR                  USS2S4'   UR                  USS2S4'   USS2SS	24==   S-  ss'   U(       a;  UR                  R                  USS2S	4'   UR                  R                  USS2S4'   OUR                  USS2S	4'   UR                  USSS	24'   U R                  UUR                  [        [        S
9  U R                  U5        g)zHSparse matrices are 2-D

See docstring for VarReader4.read_sparse_array
r  r   r%   r(   r   Nr   r   r$   r  )tocoorE   r  r   zerosnnzrowcolrn   r  r   r   r   r   r    r   )rI   r~   rD   Ar2   ijvs         rJ   r   VarWriter4.write_sparse@  s   
 IIK#hh	1U7+48UUCRCE
UUCRCE
CRC!GCAJCAJCAJggBqsFII	 	 	
 	rM   r   N)rN   rO   rP   rQ   rK   r   r   r   r   r   r   r
  r	  r   rT   rU   rM   rJ   r   r     s4    +7" +3l! 1B&:",(rM   r   c                   ,    \ rS rSrSrSS jrSS jrSrg)r   iY  z(Class for writing matlab 4 format files Nc                 4    Xl         Uc  SnX l        S U l        g )Nr"  )r   r   _matrix_writer)rI   r   r   s      rJ   rK   MatFile4Writer.__init__[  s    &?G"rM   c                     [        U 5      U l        UR                  5        H   u  p4U R                  R                  XC5        M"     g)av  Write variables in `mdict` to stream

Parameters
----------
mdict : mapping
   mapping with method ``items`` return name, contents pairs
   where ``name`` which will appeak in the matlab workspace in
   file load, and ``contents`` is something writeable to a
   matlab file, such as a NumPy array.
write_header : {None, True, False}
   If True, then write the matlab file header before writing the
   variables. If None (the default) then write the file header
   if we are at position 0 in the stream. By setting False
   here, and setting the stream position to the end of the file,
   you can append variables to a matlab file
N)r   r)  itemsr   )rI   r   r   rD   vars        rJ   put_variablesMatFile4Writer.put_variablesb  s7    ( ).ID%%c0 'rM   )r)  r   r   rC   )rN   rO   rP   rQ   r   rK   r.  rT   rU   rM   rJ   r   r   Y  s    3#1rM   r   )r"  )1r   sysrk   r  operatorr   numpyr   scipy.sparser   _miobaser   r   r   r   r	   r
   r   
_mio_utilsr   r   	functoolsr   __all__	byteorderr   r   r   r   r   r   r   r   r!   r   r   r    r"   r   iinfointpmaxr   r   r   r   r   r   r   rU   rM   rJ   <module>r<     sj   
     F F F :  MMX- 


 ddTTdT !
 	$   






  
 (&H HHRWW!!	% %$N Nbv] vr0I IX1 1rM   