[SciPy-dev] Counterpart to eye --> speye

Robert Cimrman cimrman3 at ntc.zcu.cz
Tue Feb 14 07:19:53 CST 2006


Nils Wagner wrote:
> Hi all,
> 
> Is a counterpart to eye available in scipy ?
> 
> I mean something like
> 
> def speye(n):
>    return csc_matrix(eye(n))
> 

If you don't want to pass through a full matrix, you can do e.g.:

import scipy.sparse as sp
import scipy as nm

ii = nm.arange( 10 )
v = nm.ones( 10, dtype = nm.float64 )
a = sp.coo_matrix( v, (ii, ii) )

In [55]:a
Out[55]:
<10x10 sparse matrix of type '<type 'float64scalar'>'
         with 10 stored elements (space for 10)
         in COOrdinate format>

In [56]:p a
   (0, 0)        1.0
   (1, 1)        1.0
   (2, 2)        1.0
   (3, 3)        1.0
   (4, 4)        1.0
   (5, 5)        1.0
   (6, 6)        1.0
   (7, 7)        1.0
   (8, 8)        1.0
   (9, 9)        1.0

r.




More information about the Scipy-dev mailing list