[Numpy-discussion] eye(N,dtype='S10')

Ryan Krauss ryanlists at gmail.com
Wed Apr 5 19:50:07 EDT 2006


I am trying to create a function that can return a matrix that is
either made up of complex numbers or strings depending on the input. 
I have created a symbolic string class to help me with that and it
works well.  One clumsy part is that in several cases I want to create
an identity matrix and just replace a couple of elements.  I currently
have to do this in two steps:

In [27]: mymat=numpy.eye(4,dtype='f')

In [28]: mymat.astype('S10')
Out[28]:
array([[1.0, 0.0, 0.0, 0.0],
       [0.0, 1.0, 0.0, 0.0],
       [0.0, 0.0, 1.0, 0.0],
       [0.0, 0.0, 0.0, 1.0]], dtype=(string,10))

I create a floating point matrix in the string case rather than a
complex matrix so I don't have to parse the +0.0j stuff.

But what I would really like is to be able to just be able to create
either a complex matrix or a string matrix at the beginning.  But
trying

numpy.eye(4,dtype='S10')

produces

array([[True, False, False, False],
       [False, True, False, False],
       [False, False, True, False],
       [False, False, False, True]], dtype=(string,10))

rather than

array([[1.0, 0.0, 0.0, 0.0],
       [0.0, 1.0, 0.0, 0.0],
       [0.0, 0.0, 1.0, 0.0],
       [0.0, 0.0, 0.0, 1.0]], dtype=(string,10))

I need 1's and 0's rather than True and False because when I am done,
I put the string representation into an input script to Maxima and
Maxima wouldn't handle the True and False values well.

Is there a way to directly create an identitiy string matrix with '1'
and '0'  instead of True and False?

Thanks,

Ryan




More information about the NumPy-Discussion mailing list