P4PL4
P4PL4

Reputation: 13

Creating a matrix such that each element is a quaternion in python

I am trying to create a matrix of random self-dual quaternions (4d numbers) in python. Each of these quaternions can be represented as a complex 2x2 matrix generated as follows:

X=np.random.normal(loc=0, scale=1, size=1)+np.random.normal(loc=0, scale=1, size=1)*1j
Y=np.random.normal(loc=0, scale=1, size=1)+np.random.normal(loc=0, scale=1, size=1)*1j
quaternion=[[X][Y],[-Y.conj()][X.conj()]]

I would like to somehow run a loop creating n^2 of these random matrices, and then make each of them an element of an n x n matrix (Which will, of course, be 2n x 2n as each entry is a 2x2 matrix). I tried appending them to a list and reshaping it to a matrix, but to no avail. I hope I have articulated this clearly, any help would be greatly appreciated.

Upvotes: 0

Views: 232

Answers (1)

Hammad Ahmed
Hammad Ahmed

Reputation: 885

n = 10
shape = [n,n,2,2]
real = np.random.normal(loc= 0, scale= 1, size= [n,n,2])
imgn = np.random.normal(loc= 0, scale= 1, size= [n,n,2]) * 1j

q1 = real + imgn                     # q1 = [[X], [Y]] where X and Y are imaginary numbers

q2 = q1[..., ::-1]                   # q2 = [[Y], [X]]
q2 = q2 * np.array([-1, 1])          # q2 = [[-Y], [X]]
q2 = q2.conj()                       # q2 = [[-Y.conj], [X.conj]]

dual = np.empty(shape, dtype= complex)
dual[:, :, 0, :] = q1
dual[:, :, 1, :] = q2

quaternions = dual
quaternions.shape

>>> (10, 10, 2, 2)

okay, so im familiar with quaternions and from your question i assume you're representing a quaternion with 1 x 2 array of two complex numbers. and with the 2 x 2 matrix it's dual?

Upvotes: 1

Related Questions