[Numpy-discussion] fixing diag() for matrices
Alan G Isaac
aisaac at american.edu
Fri Jul 28 18:25:39 CDT 2006
The following are from pyGAUSS.
fwiw,
Alan Isaac
#diag: diagonal of matrix as column vector (2D only!)
def diag(x):
if not isinstance(x,numpy.matrix):
x = numpy.asanyarray(x)
assert(len(x.shape)==2), "For 2-d arrays only."
return x.diagonal(offset=0,axis1=-2,axis2=-1).reshape((-1,1))
else:
return x.diagonal().T
#diagrv: insert v as diagonal of matrix x (2D only!)
def diagrv(x,v,copy=True):
x = numpy.matrix( x, copy=copy )
stride = 1 + x.shape[1]
x.flat[ slice(0,x.size,stride) ] = v
return x
More information about the Numpy-discussion
mailing list