I want to generate a random 4x4 matrix with real entries, one or some of whose eigenvalues is the pure imaginary number I
or some other imaginary number.
NOTICE The real part of one or some eigenvalues of the random real matrix is zero.
I can generate a random matrix with integer entries.
Clear["Global`*"];
While[mA = RandomInteger[{-5, 5}, {4, 4}];
! MemberQ[Eigenvalues[mA], I]];
MatrixForm[mA]
Eigensystem[mA]
$\left(\begin{array}{cccc}2 & -2 & 4 & 0 \\ -2 & -3 & -2 & -1 \\ -4 & 1 & -3 & 2 \\ 3 & 2 & 4 & 1\end{array}\right)$
{{-3, I, -I, 0}, {{4, -12, -11, 14}, {4, -2, -3 + I, 4}, {4, -2, -3 - I, 4}, {17, -9, -13, 19}}}
But this code doesn't generate real random matrices, and I don't know if there are any errors in the code. Or is there a better way?
Clear["Global`*"];
While[mA = RandomReal[{-5, 5}, {4, 4}];
! MemberQ[Eigenvalues[mA], N[I]]];
MatrixForm[mA]
Eigensystem[mA]
Some reference questions in this site.
Generate random matrix with specific eigenvalues
Update
Now I think there is an answer to this question.
Thanks for @Daniel Huber's answer. The following code is expanded according to Daniel Huber's answer. Thanks to @Nasser's answers and comments and @Yarchik's comments, the following code is to generate a matrix from pure imaginary eigenvalues according to their comments, instead of generating random matrices and then detecting whether the eigenvalues are pure imaginary.
Take the 10x10 real matrix as an example, where there are four pure imaginary eigenvalues and the other six are real eigenvalues. Since the eigenvalue of anti-symmetric matrix is pure imaginary or zero, a random anti-symmetric matrix mantisimA
is generated first (there are some related problems in this station, i.e. Random real antisymmetric matrix ).
Clear["Global`*"];
RandomMatrix[rank_, m_] :=
Sum[TensorProduct @@ RandomReal[{-1, 1}, {2, m}], {i, rank}];
MatrixForm[mA = RandomMatrix[4, 4]];
MatrixForm[mantisimA = Transpose[mA - Transpose[mA]]]
$\left(\begin{array}{cccc}0 . & 0.881925 & 0.223423 & 0.329019 \\ -0.881925 & 0 . & -0.666968 & -0.746005 \\ -0.223423 & 0.666968 & 0 . & 0.136676 \\ -0.329019 & 0.746005 & -0.136676 & 0 .\end{array}\right)$
AntisymmetricMatrixQ[mantisimA]
True
Eigenvalues[mantisimA] // Chop
{0. + 1.39773 I, 0. - 1.39773 I, 0. + 0.0484833 I, 0. - 0.0484833 I}
Then generate a random diagonal matrix mB
:
MatrixForm[mB = DiagonalMatrix[RandomReal[{-1, 1}, 6]]]
$\left(\begin{array}{cccccc}0.413869 & 0 . & 0 . & 0 . & 0 . & 0 . \\ 0 . & 0.962549 & 0 . & 0 . & 0 . & 0 . \\ 0 . & 0 . & -0.408453 & 0 . & 0 . & 0 . \\ 0 . & 0 . & 0 . & 0.278016 & 0 . & 0 . \\ 0 . & 0 . & 0 . & 0 . & 0.237626 & 0 . \\ 0 . & 0 . & 0 . & 0 . & 0 . & -0.904459\end{array}\right)$
A block diagonal matrix mAB
is generated from mantisimA
and mB
:
mAB = {{mantisimA, 0}, {0, mB}};
MatrixForm[mAB = ArrayFlatten[mAB]]
$\left(\begin{array}{ccccc}0 . & 0.881925 & 0.223423 & 0.329019 & 0 \\ -0.881925 & 0 . & -0.666968 & -0.746005 & 0 \\ -0.223423 & 0.666968 & 0 . & 0.136676 & 0 \\ -0.329019 & 0.746005 & -0.136676 & 0 . & 0 \\ 0 & 0 & 0 & 0 & 0.413869 \\ 0 & 0 & 0 & 0 & 0 . \\ 0 & 0 & 0 & 0 & 0 . \\ 0 & 0 & 0 & 0 & 0 . \\ 0 & 0 & 0 & 0 & 0 . \\ 0 & 0 & 0 & 0 & 0 .\end{array}\right.$$\left.\begin{array}{ccccc}0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 \\ 0 . & 0 . & 0 . & 0 . & 0 . \\ 0.962549 & 0 . & 0 . & 0 . & 0 . \\ 0 . & 0.408453 & 0 . & 0 . & 0 . \\ 0 . & 0 . & 0.278016 & 0 . & 0 . \\ 0 . & 0 . & 0 . & 0.237626 & 0 . \\ 0 . & 0 . & 0 . & 0 . & -0.904459\end{array}\right)$
For mAB
, we use a random matrix mC
to do the similarity transformation to get the final matrix mat
we want. Because the similarity transformation does not change the eigenvalues of the matrix, so mat
looks like a random matrix. It can be seen that mat
has 4 random pure imaginary number eigenvalues and 6 random real number eigenvalues, which meet our requirements:
MatrixForm[mC = RandomMatrix[10, 10]];
MatrixForm[mat = Inverse[mC] . mAB . mC]
Eigenvalues[mat] // Chop
$\left(\begin{array}{cccccccccc}16.3385 & -41.5472 & -4.33312 & 20.4892 & 4.12538 & -6.1673 & 7.77213 & -38.2829 & 38.9472 & 23.9866 \\ 5.84092 & -14.6843 & -2.5163 & 5.77717 & 1.61527 & -1.99138 & 2.08355 & -11.9304 & 13.7347 & 9.85066 \\ 10.2421 & -27.6697 & -5.07184 & 10.6219 & 2.53251 & -7.94874 & 3.00113 & -20.4135 & 24.6155 & 20.2253 \\ -10.5361 & 24.7431 & 5.9475 & -5.92907 & -2.08304 & 7.68168 & -1.11551 & 14.7715 & -22.0036 & -16.9828 \\ 2.75025 & -7.24597 & -1.2144 & 3.09571 & 0.81912 & -2.37392 & 1.08187 & -5.75566 & 6.51882 & 4.31431 \\ -4.4096 & 11.1205 & 2.44641 & -3.8146 & -1.23057 & 2.28264 & -1.6066 & 8.30484 & -10.4105 & -6.96863 \\ 23.2956 & -59.9963 & -8.20137 & 27.3573 & 5.98617 & -10.5259 & 10.7755 & -52.3885 & 56.2111 & 35.4369 \\ -0.406186 & -1.81916 & 2.08004 & 4.85612 & 0.0409096 & -0.143971 & 1.48435 & -5.43918 & 1.31029 & 1.88427 \\ -4.71134 & 10.3438 & 0.595214 & -5.04138 & -0.942769 & 1.01936 & -2.01276 & 9.93673 & -9.89259 & -4.04313 \\ 7.18626 & -18.7654 & -2.7216 & 8.60548 & 2.05074 & -2.56859 & 3.59416 & -16.7025 & 17.8306 & 11.3804\end{array}\right)$
{0. + 1.39773 I, 0. - 1.39773 I, 0.962549, -0.904459, 0.413869, -0.408453, 0.278016, 0.237626, 0. + 0.0484833 I, 0. - 0.0484833 I}