Is there anyway to randomly generate an invertible matrix $Q \in \{-1, +1\}^{n \times n}$?

61 views Asked by At

I am trying to randomly generate symmetric matrices $A \in \R{n \times n}$ with $-1, +1$ eigenvectors for some numerical experiments.

I want to use the eigendecomposition $A = Q \Lambda Q^{-1}$, but this requires being able to randomly generate $Q \in {-1, +1}^{n \times n}$ such that $Q$ is invertible and $A$ is symmetric.

Is there any painless way of doing this in either python or MATLAB?

I tried randomly sampling $n$ random $-1, +1 $n$-dimensional vectors to form the columns of $Q$, without any success. Basically $Q$ is never invertible when constructed in this way.

I also tried simply taking Q to be the all-ones matrix and randomly flipping the signs of elements, but I am not getting a symmetric matrix after computing $A = Q \Lambda Q^{-1}$.

1

There are 1 answers

0
Frank Yellin On

I'm slightly confused by your question. I created symmetric NxN (N=30) grids containing only 1 and -1, and pretty much all of them are invertible.

>>> N = 30
>>> grid = np.mgrid[:N, :N]
>>> in_top = grid[0] <= grid[1]


>>> # random NxN array of 1s and -1s
>>> x = np.random.choice([-1, 1], size=(N, N))
>>> # create a symmetric matrix from the top half.
>>> z = np.choose(in_top, [x, x.T])
>>> assert np.all(z == z.T)
>>> np.linalg.det(z)
201062451511295.84