The following is a seemingly simple recursion for finding a hybercube matrix.
The recursion is defined as:
I tried to put it into code but I keep running into broadcasting issues with numpy.
I tried instead of taking the size of the previous matrix, just taking powers of 2 to reduce recursive calls (for the parameter of the identity matrix).
import numpy as np
from numpy import linalg as LA
Q1 = np.array([[0, 1],
[1, 0]])
def Qn(n):
if n <= 1:
return Q1
else:
return np.array([[Qn(n-1), np.identity(int(np.exp2(n-1)))],
[np.identity(int(np.exp2(n-1))), Qn(n-1)]])
Q3= Qn(3)
eig_value, eig_vectors = LA.eig(Q3)
print(eig_value)
Q1 is the base case of my matrix. It should be very simple, yet I keep running into issues.
Traceback (most recent call last):
File "e:\Coding\Python\test.py", line 15, in <module>
Q3= Qn(3)
File "e:\Coding\Python\test.py", line 12, in Qn
return np.array([[Qn(n-1), np.identity(int(np.exp2(n-1)))],
ValueError: setting an array element with a sequence. The
requested array has an inhomogeneous shape after 2 dimensions. The
detected shape was (2, 2) + inhomogeneous part.
I get this error ^^
Use
np.blockinstead ofnp.arrayto assemble the block matrix.