Tuesday, 15 January 2013

python - Scipy Sparse: Unexpected Identity Behavior -


The following is my attempt to create a sparse matrix in which c is in the form of its diagonal I know I also have alternative methods for this, but I do not know why the following code is not working as expected:

  NP import as SPP SPRCS sparse C = NP is as. Archange (0,5)> & Gt; & Gt; NP Resident (5) * C array ([[0., 0., 0., 0., 0.], [0., 1., 0., 0., 0.], [0., 0, 2., 0., 0.], [0., 0., 0., 3., 0.], [0., 0., 0., 0., 4.]])> Gt; & Gt; & Gt; Sparse.identity (5) * c array ([0., 1., 2., 3., 4.]) #Optimized output: & lt; 5x5 sparse matrix type '& lt; Type 'numpy.float64' & gt; With 5 stored elements (1 diagonal) in the Diagonal format & gt; # And (Spurs.Findiness (5) * C) .Todense () == NP  sparse.identity (5) in the expression (5) * c  

expression * C , the multiplication operator of the sparse matrix is ​​used, which is the algebraic matrix multiplied (i.e. the time vector of the matrix gives a vector.)

You get a sparse with a given diagonal Diagonal matrix can be used:

 in  [18]: SPP import from sparse [19]: in C = np.arange (5) [20]: d = sparse.diags (C, 0) in [21]: d out [21]: 5x5 space matrix R '& lt; Type 'numpy.float64' & gt; 'With 5 stored elements (1 diagonal) in the Diagonal format & gt; In [22]: DA out [22]: array ([[0., 0., 0., 0., 0.], [0., 1., 0., 0., 0.], [0] 0., 0., 0., 4.]] 

Code>

No comments:

Post a Comment