[Numpy-discussion] General Array -> Into Index Array + Value Array of Nonzero Elements

Benjamin Kern benjamin@kerns...
Wed Dec 9 13:29:16 CST 2009


Hello everyone,

at the moment i like to create a numpy interface to a library for numerical optimization. This library uses a special format to include symmetric matrices, basically if you have 

A = np.array([ [1.0, 0.0, 2.0]
			 [0.0, 3.0, 0.0]
			 [2.0, 0.0, 5.0] ] )

you would have to create 2 arrays which specify the position as well as the the non-zero elements
of the lower triangular part of the matrix. So in this example you would need the following arrays to specify the matrix completely

A_pos = np.array([0, 2, 3, 5], dtype=int )
A_val = np.array([1.0, 3.0, 2.0, 5.0])

So now to my question. Is there a clever way to extract these two arrays A_pos and A_val from an arbirtrary A (where A.ndim=2) 
Another question would be if there is the possibility to do something similiar if you are using sparse matrices (from scipy.sparse).

Best
Benjamin


More information about the NumPy-Discussion mailing list