L ij]dZdZgdZddlZddlmZddlmZdd l m Z m Z m Z m Z dd lmZdd lmZmZmZmZmZmZmZmZdd lmZmZmZGd deZdZdZGddee Z GddeeZ!y)zSparse DIAgonal formatzrestructuredtext en) dia_array dia_matrixisspmatrix_diaN)copy_if_needed)spmatrix)issparse_formats_spbasesparray) _data_matrix)isdense isscalarlikeisshape upcast_chargetdtype get_sum_dtype validateaxis check_shape) dia_matmat dia_matvec dia_matvecsceZdZdZddddZdZdZddZejje_ ddZ ejje _ dd Z ejje _ dd Z fd Z d Zfd ZdZdZfdZddZddZej(je_ ddZej*je_ ddZej,je_ ddZej.je_ ddZej0je_ ddZdZej4je_ xZS) _dia_basediaNmaxprintctj|||t|r|jdk(rP|r|j }|j |_|j |_t|j|_ nl|j|jk(r|r|j }n|j}|j |_|j |_t|j|_ nt|tr5t|r}t||_ tjdt!|t"|_|j%t'|j}tjd||_nY |\}} | t)d|st*}tj,tj.|d|| |_tj.|d |j%t'|| } tj0| |_t||_ n tj4|}t|t6r(|j8dk7rt)d|j8d|j;|||j}|j |_|j |_t|j|_ |+t!|} |j j=| |_|j j8d k7r t)d|j j8dk7r t)d|j jdt?|j k7r{{e#99;D II #|| )$**5 ;;$++-$ A AFF  yy )!''2 e $t}*$/ HHUHUE,JK  11TZZ1I !xx9= 5$(MD' }()DEE- " bhhtAweRV.W XDI hhtAw-1-B-B#e*-B-U,02G$&==#9DL"-e"4DK Mzz$'$(TYY!^ # > 99>>Q :; ; 99??1 T\\!2 2' (:';<"4<<014  ryy& '3t||+< <EF F =Y!5KG$W-145$ M !9$(KK=0C"EFKLM Ms0PP8 P5"P00P58 Q QQ c t|j\}}t|trdnd}|jj d}d|d|d|j d|jd|d |j d S) Nr7matrixr)r r)r.r r*r&r$nnz)r@_fmt sparse_clsds rG__repr__z_dia_base.__repr__ds|$++&3 *4 9Wx IIOOA uHZL DJJ<@hhZ1!4J4::,VW Y c|j\}}tj|jjd}||jdddfz }|dk\}|||kz}|||kz}|S)z~Returns a mask of the same shape as self.data, where mask[i,j] is True when data[i,j] corresponds to a stored element.rNr)r&r0aranger*r+)r@num_rowsnum_cols offset_indsrowmasks rG _data_maskz_dia_base._data_maskmso"ZZ(ii  23 DLL400q x  x'( rRc| td|j}tj|j|S)Nzr+) r@r^r$ri res_dtyperUrVretrYxresrow_sumsones rGrkz _dia_base.sumsfD!!$**- !ZZ( 4<??$DT!&&A&.AwwqzX%hhxqww7#$KQWWQZ ##Cy#9Cxx1 Y?H''()4C x3t||+<yyq)4<<C S((2H|||%S|99##HLLdL$; _invert_indexr`r1 result_type_dia_container) r@othersub new_offsetsself_idx other_idxself_dother_dnew_datarPs rGrwz_dia_base._add_sparses%+$$T* * >>$,, 6??S499uzz#9; ;#'99uzz#9; ;jju}}= ??; =OOK? #**""1% W [!1S5F!Fyyx!89HA&%**4&A&%**4& w 3{#3s5==7I#I!JJ}Y'?@@ ::mI&>? Xq[ !TYY . !DJJqMKO3TZZ]CAxx[!1%nnTYY ;H Xww& '499QU+; ; 'HWH,-ArrE1BB-HWH,-ArrE1BB-""Hk#:$**"MMrRcht|tst| |S|j |dS)NT)r)r.rsuper _sub_sparserw)r@r __class__s rGrz_dia_base._sub_sparses3%+7&u- -400rRc>|j|j|zSN)ryr*)r@rs rG _mul_scalarz_dia_base._mul_scalarstyy5011rRcPt|r|j|St|r|jdkDr|j |zSd|j vsd|j vsd|j vrt ||Stj|}|j \}}|j \}}t|jj d|}|jddd|fjtj|j|}|dk(r ||dd|fz}n||k7r tdtj|}||kDr||j dddfz |z} n||j dddf|zz } |dk(rd}n||k7r td||| |fz}|j#|St%|t&r|j |j k7rt ||Stj(|j |j dd\} } } t|jj d|jj d}|j| d|f|j| d|fz}|j+|| f|j S)Nrrrzinconsistent shapesT) assume_uniquereturn_indicesrv)rrrr;toarrayr&rmultiplyr0r6r`r*r=r}r5rTr+ryr.r intersect1dr~)r@r other_rows other_colsrowscolsLr*jir+rrrs rGrz_dia_base.multiplysk  ##E* * 5>zzA~||~--DJJ!tzz/Q%++5Ew'..MM%(E%*[[ "J JD$DIIOOA&-A99QU#**2>>$))U+KLDQa!e $t# !677IIaLt8T\\!T'22d:ADLLD1D88A?A4'$%:;;ad #??4( (%+u{{djj/H7#E* * NN4<<)-d D %9  "EJJ$4$4Q$7 8yy2A2&IrrM)BB""D'?$**"EErRc |}tj|jdt|jj |jj }|j jd}|j\}}t||t|j||j|j |j|j|S)Nrr#r) r0r1r&rr$charr*rr>r+ravel)r@rrpyrrbrcs rG_matmul_vectorz_dia_base._matmul_vectors  HHTZZ]+djjoo78ww||+E F IIOOA jj!1QDLL)1dllDII779aggi )rRcTtj|jd|jdftj|j|}t g|j|jj|j |j|jd|||S)Nrrr#)r0r1r&r}r*rr+)r@rrqs rG_matmul_multivectorz_dia_base._matmul_multivector,shh 1 u{{1~6^^DIIu=? 0TZZ 0$))// 04<< 0 0KKN 0$) 0+. 0 rRclt|tst| |Sd|jvsd|jvr-|j |jd|jdfSt g|j|jj|j|j|jd|jj|j|j\}}|j |jt|d|f|jd|jdfS)Nrrru) r.rr_matmul_sparser&r~rr*r+reshaper>)r@rr+r*rs rGrz_dia_base._matmul_sparse3s%+7)%0 0  ?a5;;.&& 1 u{{1~'FG G">DJJ>>#'<<>15>#(;;q>>49JJ4D4D>$)==>38**> ""DLLWr$BG#L$(JJqM5;;q>#BD DrRc|j\}}|jdk(rtj}n t |}|dkrt ||z||}d}|}nt |||z |}|}||z}|jdk7r|d|}|j j\} } ||jvro|| kDrItj| |f|j j} |j | ddd| f<| |_||j |j|k(||f<ytj|j|jjj||_t|| } tj| dz| f|j j} |j | ddd| f<|| d||f<| |_y)Nrr#rru) r&r;r0infr>r`r*r+r1r$appendtyper4) r@valuesrdrbrcvalues_nn min_index max_index data_rows data_colsr*ms rG_setdiagz_dia_base._setdiagCszz1 ;;! vvH6{H q5AE1h'AIIAq1uh'AIAI ;;! BQZF#yy 9  9$xxI 6diiooN&*iiQ  ]#  @FDIIdlla'9)<< =99T\\4<<3E3E3J3J13MNDLIy)A88Y]A.diiooFD$(IID"jyj !,2DYy(( )DIrRc*|r|jS|Srr%)r@r%s rGr-z_dia_base.todiahs 99; KrRc||dk7r td|j\}}t|j}|j }t j t |tjdddf}t j |tj||zdddfz }td||jjdz } t j|jt j|jjd| f|jjf} | ||f} |j| |f||f|S)N)rrzvSparse arrays/matrices do not support an 'axes' parameter because swapping dimensions is the only logical permutation.r#rr)r&r%) r5r&r4r+r0rTr>intcr*hstackr1r$r~) r@axesr%rUrVmax_dimr+rc pad_amountr*s rG transposez_dia_base.transposeps3  LM M"ZZ(djj/<<- IIc'l"'' 21d7 ; IIhbgg .'G2CQW1M MGDIIOOA$667 yy$))RXXtyyq/A:.N48IIOO&EFGAqDz""D'? h; &*#, ,rRc|j\}}|| ks||k\r+tjd|jjStj |j |k(\}td|}t||z|}||z }|jdk(r+tj||jjS|j|d||f}|t|z } | dkDrtj|d| fd}|S)Nrr#constant)mode) r&r0emptyr*r$nonzeror+r4r`sizer1r>pad) r@rdrridx first_collast_col result_sizeresultpaddings rGdiagonalz_dia_base.diagonalsZZ d :d88ATYY__5 5zz$,,!+,1I tax&* 88q=88Ktyy? ?3q69X#556F + Q;VVFQLzBF rRcL|jdk(r'|j|j|jS|j\}}|jj\}}t j |}||jdddfz }|dk\}|||kz}|||kz}||jdk7z}|jt|j} t j|dz| } t j|jdd|| d|dz||kr | || |dzd|j|jj| d} |jj|j} |j| | | f|j|jS) Nrr#r!rrhFr)r&r$)rL_csc_containerr&r$r*r0rTr+r3r4r1cumsumrkTr=) r@r%rUrV num_offsets offset_lenrWrXrYrCindptrindicesr*s rGtocscz_dia_base.tocscs 88q=&&tzz&D D!ZZ("&))// Zii + DLL400q x  x'( a ))TZZ)A (Q,i8!#4888+;IX+F!GqA  $*:$6F:a<= !%%-&&yu&=yy{{466"""D'6#:$**)-#5 5rRct|j\}}|jj\}}tj|}||jdddfz }|dk\}|||kz}|||kz}||jdk7z}||}tj |||j } |j|jft|j} |j| d}| j| d} |j|} |j| || ff|j|jdS)Nr)arraysr"Fr)r&r$r%) r&r*r0rTr+tilerr3r4r=r<r$) r@r%rUrVrrrWrXrYcolrCr*s rGtocooz_dia_base.tocoos2!ZZ("&))// Zii + DLL400q x  x'( a $iggk;/ =))LL?3tzz?* jjj/jjj/yy"" C: djj #  rRc|r7|j||jjf|jS|j||jf|jS)zReturns a matrix with the same sparsity structure as self, but with different data. By default the structure arrays are copied. rv)r~r+r%r&)r@r*r%s rGryz_dia_base._with_datasf &&t||((*+4::' &&t||$DJJ' rRct|}|\}}|jddd|f|_||jdkDrtj|j |jdz|jjdkr_|j dddf|jdztj |jjdk}d|j|<||_y)Nrr)rr*r&r0anyr+rTr,)r@r&rbrcrYs rGresizez_dia_base.resizesE"1IIa!e$  1 t||djjm3diiooa6HHILLD)DJJqM9IIdiiooa012DDIIdO rR)NNFr)NNN)Frg)NF)T)__name__ __module__ __qualname___formatr(rQrZr]r __doc__rerkrwrrrrrrrr-rrrrryr __classcell__)rs@rGrrs:GKGTKGZ  1$1199M oo--GO6@++%%CK-N^12/Fb D #J MM))EM,, ))11I ''//H50MM))EM 0MM))EM  ^^++FNNrRrcrtj|}tjt|||<|S)z)Helper function to invert an index array.)r0 zeros_likerTr>)rinvs rGr|r|s+ -- CyyS"CH JrRc"t|tS)aIs `x` of dia_matrix type? Parameters ---------- x object to check for being a dia matrix Returns ------- bool True if `x` is a dia matrix, False otherwise Examples -------- >>> from scipy.sparse import dia_array, dia_matrix, coo_matrix, isspmatrix_dia >>> isspmatrix_dia(dia_matrix([[5]])) True >>> isspmatrix_dia(dia_array([[5]])) False >>> isspmatrix_dia(coo_matrix([[5]])) False )r.r)rps rGrrs. a $$rRceZdZdZy)ra< Sparse array with DIAgonal storage. This can be instantiated in several ways: dia_array(D) where D is a 2-D ndarray dia_array(S) with another sparse array or matrix S (equivalent to S.todia()) dia_array((M, N), [dtype]) to construct an empty array with shape (M, N), dtype is optional, defaulting to dtype='d'. dia_array((data, offsets), shape=(M, N)) where the ``data[k,:]`` stores the diagonal entries for diagonal ``offsets[k]`` (See example below) 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 DIA format data array of the array offsets DIA format offset array of the array T Notes ----- Sparse arrays can be used in arithmetic operations: they support addition, subtraction, multiplication, division, and matrix power. Sparse arrays with DIAgonal storage do not support slicing. Examples -------- >>> import numpy as np >>> from scipy.sparse import dia_array >>> dia_array((3, 4), dtype=np.int8).toarray() array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], dtype=int8) >>> data = np.array([[1, 2, 3, 4]]).repeat(3, axis=0) >>> offsets = np.array([0, -1, 2]) >>> dia_array((data, offsets), shape=(4, 4)).toarray() array([[1, 0, 3, 0], [1, 2, 0, 4], [0, 2, 3, 0], [0, 0, 3, 4]]) >>> from scipy.sparse import dia_array >>> n = 10 >>> ex = np.ones(n) >>> data = np.array([ex, 2 * ex, ex]) >>> offsets = np.array([-1, 0, 1]) >>> dia_array((data, offsets), shape=(n, n)).toarray() array([[2., 1., 0., ..., 0., 0., 0.], [1., 2., 1., ..., 0., 0., 0.], [0., 1., 2., ..., 0., 0., 0.], ..., [0., 0., 0., ..., 2., 1., 0.], [0., 0., 0., ..., 1., 2., 1.], [0., 0., 0., ..., 0., 1., 2.]]) NrrrrrjrRrGrrHrRrceZdZdZy)raO Sparse matrix with DIAgonal storage. This can be instantiated in several ways: dia_matrix(D) where D is a 2-D ndarray dia_matrix(S) with another sparse array or matrix S (equivalent to S.todia()) dia_matrix((M, N), [dtype]) to construct an empty matrix with shape (M, N), dtype is optional, defaulting to dtype='d'. dia_matrix((data, offsets), shape=(M, N)) where the ``data[k,:]`` stores the diagonal entries for diagonal ``offsets[k]`` (See example below) 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 DIA format data array of the matrix offsets DIA format offset array of the matrix T Notes ----- Sparse matrices can be used in arithmetic operations: they support addition, subtraction, multiplication, division, and matrix power. Sparse matrices with DIAgonal storage do not support slicing. Examples -------- >>> import numpy as np >>> from scipy.sparse import dia_matrix >>> dia_matrix((3, 4), dtype=np.int8).toarray() array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], dtype=int8) >>> data = np.array([[1, 2, 3, 4]]).repeat(3, axis=0) >>> offsets = np.array([0, -1, 2]) >>> dia_matrix((data, offsets), shape=(4, 4)).toarray() array([[1, 0, 3, 0], [1, 2, 0, 4], [0, 2, 3, 0], [0, 0, 3, 4]]) >>> from scipy.sparse import dia_matrix >>> n = 10 >>> ex = np.ones(n) >>> data = np.array([ex, 2 * ex, ex]) >>> offsets = np.array([-1, 0, 1]) >>> dia_matrix((data, offsets), shape=(n, n)).toarray() array([[2., 1., 0., ..., 0., 0., 0.], [1., 2., 1., ..., 0., 0., 0.], [0., 1., 2., ..., 0., 0., 0.], ..., [0., 0., 0., ..., 2., 1., 0.], [0., 0., 0., ..., 1., 2., 1.], [0., 0., 0., ..., 0., 1., 2.]]) NrrjrRrGrr\rrRr)"r __docformat____all__numpyr0 _lib._utilr_matrixr _baser r r r _datar_sputilsrrrrrrrr _sparsetoolsrrrrr|rrrrjrRrGrsu% 7'77>=W, W,t%6I 7IXI9IrR