L ixdZdZgdZddlmZddlZddlmZddl m Z dd l m Z m Z dd lmZdd lmZmZmZmZdd lmZmZmZmZmZmZdd lmZddlmZmZm Z m!Z!m"Z"m#Z#m$Z$Gddee Z%dZ&Gdde%eZ'Gdde e%Z(y)z"Compressed Block Sparse Row formatzrestructuredtext en) bsr_array bsr_matrixisspmatrix_bsr)warnN)copy_if_needed)spmatrix) _data_matrix _minmax_mixin) _cs_matrix)issparse_formats_spbasesparray)isshapegetdtypegetdata to_nativeupcast check_shape) _sparsetools) bsr_matvec bsr_matvecscsr_matmat_maxnnz bsr_matmat bsr_transposebsr_sort_indices bsr_tocsrc\eZdZdZ ddddZd dZedefdZd!dZ e jje _ d!d Z e jje _ d Z d"d Ze jje_ d Zd ZdZdZdZdZd#dZd$dZe j,je_ d$dZe j.je_ d dZd%dZe j2je_ d#dZe j4je_ dZdZdZdZd%dZd dZ d$dZ!y)& _bsr_basebsrNmaxprintc tj|||t|r|j|jk(r|r|j }n|j |}|j |j|j|jf\|_|_|_|_ nt|trt|rt||_ |j\}}|d}n$t|std|t|}t!j"d|zt%|t&|_|\} } || zdk7s|| zdk7r td|j)t+|| z|| z| |  } t!j"d| |_t!j"|| zd z| |_nt-|d k(ro|j/||| } | j |} | j | j| j| jf\|_|_|_|_ n t-|dk(r;|\}}}d }| t+|}|t+|t+|}|j)||f|d} |st0}t!j2||| |_t!j2||| |_t5||||_|jj6dk7r"td|jj|)t|std|t||jjd dk7rtd|d|jjd dtd t!j8|}t|t<r(|j6d k7rtd|j6d|j/|| j |}|j |j|j|jf\|_|_|_|_ |t||_ ni|j] t-|j d z }|jj+d z}|j>\} } t|| z|| zf|_ |j| tdt||_ |6|jjAt%||jd|_|jCdy#t:$r}td|jd|d}~wwxYw#t:$r}td|d}~wwxYw)Nr" blocksize)rrzinvalid blocksize=r)defaultrz#shape must be multiple of blocksizemaxvaldtyper)r,shapeT)r*check_contents)copyr,z*BSR data must be 3-dimensional, got shape=zmismatching blocksize=z vs z(unrecognized bsr_array constructor usagezunrecognized form for z_matrix constructorzBSR arrays don't support zD input. Use 2Dz!unable to infer matrix dimensionszneed to infer shapeFr1) full_check)"r __init__r formatr1tobsrindptrindicesdata_shape isinstancetuplerrr. ValueErrornpzerosrfloat_get_index_dtypemaxlen_coo_containerrarrayrndimasarray Exceptionrr&astype check_format)selfarg1r.r,r1r&r#MNRC idx_dtypecoor!r9r8r7r*es W/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/scipy/sparse/_bsr.pyr4z_bsr_base.__init__sdD8< D>{{dkk)dyy{zzIz6 T\\499dkkA =DKty$+U #t})$/ jj!$ %I"9-(+=i[)IJJ %i 0IHHTI%5xu7UV !Ea>Q&$DTYY__DUV("9-(+=i[)IJJY'499??12+>>(4YK"499??12#6"79 !!KLL Mzz$'$(TYY!^ #= 0z8index pointer values must form a non-decreasing sequencer+N)r.r&r7r,kindrnamer8rFr=r9rCprunennzrBminr>diffrArGr)rKr3rMrNrOrPrQs rTrJz_bsr_base.check_formatsjj!nn! ;;   ! !S ( 7 8I8I8N8N7OqQ  <<   " "c ) 89K9K9P9P8QQRS  <<   !T[[%5%5%:@A A 99>>Q 12 2  1q (&s4;;'7&8 adQhZqQ  KKNa @A A  TYY /IJ J KKOc$,,/ /DE E xx!|<<##%A-$8A$T\\%5%5%7$8;<<##%)$%GHH774;;'++-1$&BCC--t||T[[.IJI**T[[ BDK::dll)DDL!$)),DI# returnc4|jjddS)zBlock size of the matrix.rN)r9r.)rKs rTr&z_bsr_base.blocksizesyyqr""r`cv| td|j\}}t|jd|z|zS)Nz6_getnnz over an axis is not implemented for BSR formatrY)NotImplementedErrorr&intr7)rKaxisrOrPs rT_getnnzz_bsr_base._getnnzsD  %'78 8~~14;;r?#a'!++r`cb| tdtj|jS)Nz:count_nonzero over axis is not implemented for BSR format.)rdr> count_nonzero _deduped_data)rKrfs rTriz_bsr_base.count_nonzeros4  %L  2 2 455r`c t|j\}}t|trdnd}dj d|j D}d|d|d|j d|jd |d |jd S) NrEmatrixxc32K|]}t|ywN)str).0rms rT z%_bsr_base.__repr__..s4SV4s) rr5r;rjoinr&r,r]r.)rK_fmt sparse_clsbs rT__repr__z_bsr_base.__repr__s$++&3 *4 9Wx HH4T^^4 4uHZL DJJ<@hhZ;A3l4::,VW Y r`c |j\}}|| ks||k\r+tjd|jjS|j \}}tj t|t|dz|t|dz t|j}tj|||z||z|||j|jtj|j| |SNrr+)r.r>emptyr9r,r&r?r^rBrr bsr_diagonalr7r8ravel)rKkrowscolsrOrPys rTdiagonalz_bsr_base.diagonalsZZ d :d88ATYY__5 5~~1 HHSAq )4#a)+;<!$**- /!!!TQY 1a"&++t||"$((499"5q :r`ctrord)rKkeys rT __getitem__z_bsr_base.__getitem__!!r`ctror)rKrvals rT __setitem__z_bsr_base.__setitem__ rr`cD|jdj|SNFr2)tocoo _add_dense)rKothers rTrz_bsr_base._add_denseszzuz%0077r`c P|j\}}|j\}}tj|jdt |j |j }t ||z||z|||j|j|jj|| |Sr|) r.r&r>r?rr,rr7r8r9r)rKrrMrNrOrPresults rT_matmul_vectorz_bsr_base._matmul_vectorsjj!nn!$**Q-vdjj%++/NO1a4Aq! KKtyy'8 6  r`c |j\}}|j\}}|jd}tj||ft |j |j }t ||z||z||||j|j|jj|j|j |S)Nrr+) r&r.r>r?rr,rr7r8r9r)rKrrOrPrMrNn_vecsrs rT_matmul_multivectorz_bsr_base._matmul_multivectorsnn!jj!Q1V*F4::ekk,JKAqD!Q$1 T\\499??+< v||~ / r`c|j\}}|j\}}|j\}}|jdk(r|jd}nd}|jdk(r|dk(r|j||fd}n|j||f}|j |j |j |j |j f} t||z||z|j j| |j j| |j j| |j j| } |j |j |j |j |j f| } tj|j j| } tj| | } tj||z| zt|j|j} t| ||z||z||||j j| |j j| tj|j|j j| |j j| tj|j| | | | j!d ||} |j#| | | f||f||f S) Nr!rcsrF)r&r1r%r)r+rY)r.r&)r.r&r5r6rAr7r8rrIr>r}rr,rrr9reshape_bsr_container)rKrrMK1K2rNrOnrPrQbnnzr7r8r9s rT_matmul_sparsez_bsr_base._matmul_sparse,st 2 Ann! <<5 "AA <<5 Q!VKK1Q%eK!%!4!4Y!?!&!4!4Y!?!&!5!5i!@ B ))4;; +0<<+H15*7 $++++9=((4y1xx!Dtzz%++(FG4Aq!tQ1;;%%i0<<&&y188DII&<<&&y1=='' 288EJJ' ||Bq#"" 7F #Aq6aV#  r`c|d|jfvr |jj|S|r|jS|S)a=Convert this array/matrix into Block Sparse Row Format. With copy=False, the data/indices may be shared between this array/matrix and the resultant bsr_array/bsr_matrix. If blocksize=(R, C) is provided, it will be used for determining block size of the bsr_array/bsr_matrix. Nr%)r&tocsrr6r1)rKr&r1s rTr6z_bsr_base.tobsrdsC T4>>2 2::<%% %: : 99; Kr`c f|j\}}|j\}}|j}|j|j|j ft ||}tj|dz|}tj||} tj|t|j} t||z||z|||jj|d|j j|d|j|| | |j| | |f|jS)Nr)rr+Fr2r.)r.r&r]rAr7r8rBr>r}rr,rrIr9_csr_container) rKr1rMrNrOrPr]rQr7r8r9s rTrz_bsr_base.tocsrts zz1~~1hh))4;; *E14S!*> !a%y1((3i0xx6$**#56!q&q&Q++$$YU$;,,%%ie%<)) ""D'6#:$**"MMr`cF|jdj|Sr)rtocsc)rKr1s rTrz_bsr_base.tocscs!zzuz%+++66r`c|j\}}|j\}}tj|j}|j j tj tjj kDrD|jtj}tj||k7r td|}|jt||}|tj||z|zj|} | j||zjd||} | tj tj||jddd|fz } | jd} ||j"zj|dj||zjd||} | tj tj|||dfz } | jd} |j$jd} |r| j'} |j)| | | ff|jS) zConvert this array/matrix to COOrdinate format. When copy=False the data array will be shared between this array/matrix and the resultant coo_array/coo_matrix. zMatrix too big to convertr)r+rYrFr2r)r.r&r>r_r7r,itemsizeintprIanyr=rArBarangerepeatrtiler8r9r1rD) rKr1rMrNrOrP indptr_diffindptr_diff_limitedrQrowcolr9s rTrz_bsr_base.tocoosjj!nn!ggdkk*    % %(9(B(B B"-"4"4RWW"= vv)[89 !<==-K))Q); 299QT33;;KHjj1o%%b1- rwwryy)4<r}r7r8r9rr) rKaxesr1rOrPrMrNNBLKr7r8r9s rT transposez_bsr_base.transposesQ  LM M~~1zz1xx!A# 88q=&&1v!Q-1ZZd'D D!Q$($++*;*;<((4t||'9'9:xxq! DIIOO<adAqD!Qkk4<<1Bgtzz| 5""D'6#:*+Qd#< D >? ?IIet$ ||ET* r`cB|j||j}tt|j|z|jz}|j\}}t |j t |j z}|j|j|j|j|jf|} tj|jj| } tj|| } gd} || vr,tj||z|ztj} n;tj||z|zt|j|j} ||jd|z|jd|z|||jj!| |jj!| |j |jj!| |jj!| tj"|j | | | | d}| d|} | d||z|z} ||d z kr | j%} | j%} | j'd||} |j| | | f|j S) z5Apply the binary operation fn to two sparse matrices.r%r)r+)_ne__lt__gt__le__ge_rrrYNr-r) __class__r&getattrrr5rCr9rAr7r8r>r}r.bool_rr,rIrr1r)rKropin_shape out_shapefnrOrPmax_bnnzrQr7r8bool_opsr9 actual_bnnzs rT_binoptz_bsr_base._binopt1s# u?\4;;#3dkk#A Bnn!tyy>C O3))4;; +0<<+H19*; $++++9=((895; >88AaCL9D88AaCLtzz%++0NOD 4::a=! TZZ]A-q! ;;  i ( <<  y ) 99 <<  y ) ==   * 88EJJ     Rj ,;'$QqS_% ! #llnG99;D||Bq#~~tWf5TZZ~HHr`c:|r[|j||jj|jjf|j|j S|j||j|jf|j|j S)zReturns a matrix with the same sparsity structure as self, but with different data. By default the structure arrays (i.e. .indptr and .indices) are copied. )r.r,)rr8r1r7r.r,)rKr9r1s rT _with_dataz_bsr_base._with_databs >>4 (9(9(;DKK>4 T[["A)-$**"F Fr`c0tj|||Sro)r _broadcast_to)rKr.r1s rTrz_bsr_base._broadcast_tous$$T5$77r`)NNFN)Tror')NF)F)NN)"__name__ __module__ __qualname___formatr4rJpropertyr<r&rgr__doc__rirzrrrrrrrr6rrrrrrrrr\rrrr`rTr r sGG:?s,-1s,j?-H#5##,oo--GO6$1199M  ''//H ""8  2 p N*MM))EM7MM))EM$ LDoo--GO<2 ))11I ()B '+*.Ib F&8r`r c"t|tS)aIs `x` of a bsr_matrix type? Parameters ---------- x object to check for being a bsr matrix Returns ------- bool True if `x` is a bsr matrix, False otherwise Examples -------- >>> from scipy.sparse import bsr_array, bsr_matrix, csr_matrix, isspmatrix_bsr >>> isspmatrix_bsr(bsr_matrix([[5]])) True >>> isspmatrix_bsr(bsr_array([[5]])) False >>> isspmatrix_bsr(csr_matrix([[5]])) False )r;r)rms rTrrys. a $$r`ceZdZdZy)ra Block Sparse Row format sparse array. This can be instantiated in several ways: bsr_array(D, [blocksize=(R,C)]) where D is a 2-D ndarray. bsr_array(S, [blocksize=(R,C)]) with another sparse array or matrix S (equivalent to S.tobsr()) bsr_array((M, N), [blocksize=(R,C), dtype]) to construct an empty sparse array with shape (M, N) dtype is optional, defaulting to dtype='d'. bsr_array((data, ij), [blocksize=(R,C), shape=(M, N)]) where ``data`` and ``ij`` satisfy ``a[ij[0, k], ij[1, k]] = data[k]`` bsr_array((data, indices, indptr), [shape=(M, N)]) is the standard BSR representation where the block column indices for row i are stored in ``indices[indptr[i]:indptr[i+1]]`` and their corresponding block values are stored in ``data[ indptr[i]: indptr[i+1] ]``. If the shape parameter is not supplied, the array dimensions are inferred from the index arrays. Attributes ---------- dtype : dtype Data type of the array shape : 2-tuple Shape of the array ndim : int Number of dimensions (this is always 2) nnz size data BSR format data array of the array indices BSR format index array of the array indptr BSR format index pointer array of the array blocksize Block size has_sorted_indices : bool Whether indices are sorted has_canonical_format : bool T Notes ----- Sparse arrays can be used in arithmetic operations: they support addition, subtraction, multiplication, division, and matrix power. **Summary of BSR format** The Block Sparse Row (BSR) format is very similar to the Compressed Sparse Row (CSR) format. BSR is appropriate for sparse matrices with dense sub matrices like the last example below. Such sparse block matrices often arise in vector-valued finite element discretizations. In such cases, BSR is considerably more efficient than CSR and CSC for many sparse arithmetic operations. **Blocksize** The blocksize (R,C) must evenly divide the shape of the sparse array (M,N). That is, R and C must satisfy the relationship ``M % R = 0`` and ``N % C = 0``. If no blocksize is specified, a simple heuristic is applied to determine an appropriate blocksize. **Canonical Format** In canonical format, there are no duplicate blocks and indices are sorted per row. **Limitations** Block Sparse Row format sparse arrays do not support slicing. Examples -------- >>> import numpy as np >>> from scipy.sparse import bsr_array >>> bsr_array((3, 4), dtype=np.int8).toarray() array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], dtype=int8) >>> row = np.array([0, 0, 1, 2, 2, 2]) >>> col = np.array([0, 2, 2, 0, 1, 2]) >>> data = np.array([1, 2, 3 ,4, 5, 6]) >>> bsr_array((data, (row, col)), shape=(3, 3)).toarray() array([[1, 0, 2], [0, 0, 3], [4, 5, 6]]) >>> indptr = np.array([0, 2, 3, 6]) >>> indices = np.array([0, 2, 2, 0, 1, 2]) >>> data = np.array([1, 2, 3, 4, 5, 6]).repeat(4).reshape(6, 2, 2) >>> bsr_array((data,indices,indptr), shape=(6, 6)).toarray() array([[1, 1, 0, 0, 2, 2], [1, 1, 0, 0, 2, 2], [0, 0, 0, 0, 3, 3], [0, 0, 0, 0, 3, 3], [4, 4, 5, 5, 6, 6], [4, 4, 5, 5, 6, 6]]) Nrrrrrr`rTrrkr`rceZdZdZy)ra Block Sparse Row format sparse matrix. This can be instantiated in several ways: bsr_matrix(D, [blocksize=(R,C)]) where D is a 2-D ndarray. bsr_matrix(S, [blocksize=(R,C)]) with another sparse array or matrix S (equivalent to S.tobsr()) bsr_matrix((M, N), [blocksize=(R,C), dtype]) to construct an empty sparse matrix with shape (M, N) dtype is optional, defaulting to dtype='d'. bsr_matrix((data, ij), [blocksize=(R,C), shape=(M, N)]) where ``data`` and ``ij`` satisfy ``a[ij[0, k], ij[1, k]] = data[k]`` bsr_matrix((data, indices, indptr), [shape=(M, N)]) is the standard BSR representation where the block column indices for row i are stored in ``indices[indptr[i]:indptr[i+1]]`` and their corresponding block values are stored in ``data[ indptr[i]: indptr[i+1] ]``. If the shape parameter is not supplied, the matrix dimensions are inferred from the index arrays. Attributes ---------- dtype : dtype Data type of the matrix shape : 2-tuple Shape of the matrix ndim : int Number of dimensions (this is always 2) nnz size data BSR format data array of the matrix indices BSR format index array of the matrix indptr BSR format index pointer array of the matrix blocksize Block size has_sorted_indices : bool Whether indices are sorted has_canonical_format : bool T Notes ----- Sparse matrices can be used in arithmetic operations: they support addition, subtraction, multiplication, division, and matrix power. **Summary of BSR format** The Block Sparse Row (BSR) format is very similar to the Compressed Sparse Row (CSR) format. BSR is appropriate for sparse matrices with dense sub matrices like the last example below. Such sparse block matrices often arise in vector-valued finite element discretizations. In such cases, BSR is considerably more efficient than CSR and CSC for many sparse arithmetic operations. **Blocksize** The blocksize (R,C) must evenly divide the shape of the sparse matrix (M,N). That is, R and C must satisfy the relationship ``M % R = 0`` and ``N % C = 0``. If no blocksize is specified, a simple heuristic is applied to determine an appropriate blocksize. **Canonical Format** In canonical format, there are no duplicate blocks and indices are sorted per row. **Limitations** Block Sparse Row format sparse matrices do not support slicing. Examples -------- >>> import numpy as np >>> from scipy.sparse import bsr_matrix >>> bsr_matrix((3, 4), dtype=np.int8).toarray() array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], dtype=int8) >>> row = np.array([0, 0, 1, 2, 2, 2]) >>> col = np.array([0, 2, 2, 0, 1, 2]) >>> data = np.array([1, 2, 3 ,4, 5, 6]) >>> bsr_matrix((data, (row, col)), shape=(3, 3)).toarray() array([[1, 0, 2], [0, 0, 3], [4, 5, 6]]) >>> indptr = np.array([0, 2, 3, 6]) >>> indices = np.array([0, 2, 2, 0, 1, 2]) >>> data = np.array([1, 2, 3, 4, 5, 6]).repeat(4).reshape(6, 2, 2) >>> bsr_matrix((data,indices,indptr), shape=(6, 6)).toarray() array([[1, 1, 0, 0, 2, 2], [1, 1, 0, 0, 2, 2], [0, 0, 0, 0, 3, 3], [0, 0, 0, 0, 3, 3], [4, 4, 5, 5, 6, 6], [4, 4, 5, 5, 6, 6]]) Nrrr`rTrrrr`r))r __docformat____all__warningsrnumpyr>scipy._lib._utilr_matrixr _datar r _compressedr _baser rrr_sputilsrrrrrrrrrrrrrrr rrrrr`rTrs}(% 7+.#77$$&&& ^ 8 M^ 8B%6l 7l^l9lr`