1 Introduction

The similarity between square matrices is an important equivalence relationship in linear algebra. In complex field, not all square matrices can be diagonalized. But every square matrix is similar to a Jordan normal form, which is also called Jordan matrix, through a similarity transform known as the Jordan transform. Jordan matrix is an “almost diagonal” upper triangular matrix (or lower triangular matrix in some convention). Its diagonal elements are the eigenvalues of the matrix, and the form includes diagonal matrix as a special case. As long as the Jordan canonical form of a matrix is known, the linear-algebraic nature of the matrix can be seen at a glance. Jordan matrix plays an important role not only in algebraic theory, but also in physics and in engineering control [1, 2] etc.

For a set of linear-differential equations of constant coefficients, the Jordan canonical form can be employed in analyzing the properties of the solutions [3]. In further development, Frobenius first proposed one hundred years ago that a square matrix can be decomposed into the product of two symmetric matrices \(A_{n,n} = S_1S_2\) with one of the two being nonsingular [4]. The proposal was rediscovered by Taussky and Zassenhaus in 1959 [5]. Such a significant matrix factorization can be proved within the framework of Jordan transform [6,7,8]. More recently, Kwon, Ao and Thouless proved in stochastic system near fixed point that the strict Lyapunov function can be obtained with the help of Jordan transform [9]. The same can be extended to the work of Chen et al. since 2020 [10, 11].

Chen’s work explores the rich phenomena of nonlinear quasi limit cycles near fixed points and make connections to similar properties found in the field of artificial deep neural networks [12, 13]. The computational analysis requires the solution of continuous Lyapunov equation. It is highly desirable that we find another method that can be fully controlled and does not rely on the command of lyap in e.g. Matlab\(^{\copyright }\) when performing numerical simulations. Such a task may be accomplished by having an efficient algorithm of Jordan transform to improve the computational efficiency. The ability to overcome the limitation is of great significance in the relevant field of stochastic dynamical analysis.

There have been many studies on getting the Jordan canonical form. For example, Chi and Ye [14] studied the Jordan form by the singular value decomposition (SVD). But when the difference between the individual matrix elements becomes large, the work fails to obtain satisfying accuracy. For another example, Yu and Cheng [15] provided an algorithm for computing the Jordan canonical form using elementary transforms. The pressing problem is that on popular platforms one can not obtain the Jordan normal matrix and/or the transform matrix efficiently. In the case of Matlab\(^{\copyright }\), the Jordan matrix J and the transform matrix P is given by \([P, J] = {\normalsize {\textsf {jordan}}}(A)\). But the computation is actually carried out by symbolic manipulations hence is limited to very small dimensions. For an highly anisotropic n-square matrix A with \(n>12\), the command could simply fail to produce a result. Even on a block-like matrix, it can barely handle a size of \(n>50\). Such a capability is useless for practical applications. In this paper, we present a new algorithm to compute the Jordan transform. The success is demonstrated on e.g. the Matlab\(^{\copyright }\) platform.

2 Computational Logics

In complex field, an n-square matrix A has t distinct eigenvalues \(\{\lambda _1, \lambda _2, \ldots , \lambda _t\}\), corresponding to \(s\ge t\) linearly independent eigenvectors. Let the algebraic multiplicity of eigenvalue \(\lambda _i\) (\(i\in [1,t]\)) be \(m_i\) in the Jordan form, with the geometric multiplicity \(\rho _i\) that corresponds to the order of the eigenspace for the ith eigenvalue. In other words, eigenvalue \(\lambda _i\) has \(\rho _i\) linearly independent eigenvector(s). We have \(\sum _{i=1}^t m_i = n\), \(m_i \ge \rho _i\), and \(\sum _{i=1}^t \rho _i = s\le n\). When \(s = n\), matrix A is diagonalizable. Namely, there exists an \(n^{\text{th}}\)-order invertible matrix M such that \(M^{-1}AM\) becomes a diagonal matrix in which the diagonal elements are the eigenvalues of matrix A. But when \(s < n\), matrix A has multiple eigenvalue(s) \(\lambda _i\), whose \(m_i > \rho _i\). In this case, the n-square matrix A is not diagonalizable. There is an \(n\)th-order invertible matrix P satisfying

$$\begin{aligned}{} & {} J = P^{-1}AP = \text{diag} (J_1(\lambda _1), J_2(\lambda _2), \ldots , J_t(\lambda _t)),\end{aligned}$$
(1)
$$\begin{aligned}{} & {} J_i(\lambda _i) = \text{diag} (J_{i1}(\lambda _i), J_{i2}(\lambda _i), \ldots , J_{i\rho _i}(\lambda _i)). \end{aligned}$$
(2)

The matrix J is the Jordan normal form, which is similar to the matrix A. In the above, the square matrix \(J_{ij} (\lambda _i)\) (\(j = 1, 2, \ldots , \rho _i\)) is a Jordan block, which can be written as:

$$\begin{aligned} J_{ij}(\lambda _i) = \left( \begin{array}{ccccc} \lambda _i &{} 1 &{} &{} &{} 0 \\ &{} \lambda _i &{} 1 &{} &{} \\ &{} &{} \ddots &{} \ddots &{} \\ &{} &{} &{} \ddots &{} 1 \\ 0 &{} &{} &{} &{} \lambda _i \\ \end{array} \right) _{k_{j}\times k_{j}}, \end{aligned}$$
(3)

where \(J_{ij}(\lambda _i)\) is a \(k_{j}\)th-order square matrix, and \(\sum _{j = 1} ^{\rho _i} \text{dim} (J_{ij}(\lambda _{i})) = \sum _{j=1} ^{\rho _i} k_{j} = m_i\).

In order to obtain the Jordan normal form of n-square matrix A, we first need to find the transform matrix \(P = \{P_1, P_2,\ldots , P_t\}\), where \(P_i = \{P_{i1}, P_{i2},\ldots , P_{i\rho _i}\}\), and \(P_{ij}\) is a column vector array. In a single Jordan block \(J_{ij} (\lambda _i)\), the corresponding eigenvectors \(P_{ij} = \{u_1, u_2,\ldots , u_{k_{j}}\}\) satisfy:

$$\begin{aligned} Au_1&= \lambda _iu_1 \\ Au_2&= \lambda _iu_2+u_1 \\ \cdots \\ Au_k&= \lambda _iu_{k} + u_{k-1}, \end{aligned}$$
(4)

where we have dropped the subscript j on k for simplicity. Introducing a matrix \(N_{i} = A-\lambda _iI\), Eq. (4) can be rewritten as:

$$\begin{aligned} Nu_1 = 0, \, Nu_2 = u_1, \,\ldots , \, Nu_{k} = u_{k-1}, \end{aligned}$$
(5)

where we have dropped the subscript i on \(N_{i}\) for the same reason. The relationship in Eq. (4) or (5) is called Jordan chain. Where \(u_1\) is the eigenvector and \(\{u_2,\ldots , u_{k_j}\}\) are generalized eigenvectors of matrix A, which are respectively in the null-space of matrix N [i.e. \(\text{kernel}(N)\)] and the generalized eigenspace [i.e. \(\text{kernel}(N^{m_i})\)]. According to Eq. (5), \(N^{k} u_{k} = 0\) (and \(1 < k \le m_i\)), so that \(N^{m_i} u_{k} = 0\). Therefore, the generalized eigenvector \(u_k\) can be obtained from a basis of \(N^{m_i}\) with zero eigenvalue and then the eigenvector \(u_1\) can be deduced inversely. Eventually the reversible matrix P can be found and the Jordan transform of square matrix A is realized.

3 Implementation of Jordan Transform

In accordance with the computational logics presented in Sect. 2, a flow chat of the algorithm proposed for Jordan transform is shown in Fig. 1. For a given n-square matrix A, the specific steps are as follows.

Fig. 1
figure 1

The workflow of the algorithm for Jordan transform

(1) Compute the eigenvalues \(\Lambda = \{\lambda _i\}\) and eigenvectors \(V = \{v_i\}\) of A

(2) Record the eigenvalues and the corresponding algebraic multiplicity \(\{\lambda _i, m_i\}\) \((i =1, 2,\ldots , t);\)

(3) Judgment: If all the algebraic multiplicities equal 1, that is \(\forall\) i, \(m_i \equiv 1\), the matrix A is diagonalizable. In this case, \(P = V\) and \(J = \Lambda\) is a diagonal matrix;

(4) Judgment: Else for each \(m_{i} > 1\), compute the geometric multiplicity of the eigenvalue. If the algebraic multiplicities equal to their geometric multiplicities for all of them, that is \(\forall\) \(m_i >1\), \(m_i \equiv \rho _i\), A is also diagonalizable. We too have \(P = V\) and the diagonal \(J = \Lambda ;\)

(5) Failing on the conditions set in steps (3) and (4), A is not diagonalizable. Single out those eigenvalues with \(m_i = \rho _i\), each of their “Jordan blocks” reduces to an \(m_i\)th-order diagonal square matrix with the element \(\lambda _i\). In this case, \(P_i = \{v_i\}\) (multiple columns);

(6) When \(m_i > \rho _i\), there are 2 or more Jordan blocks corresponding to \(\lambda _i\). Let \(N = A-\lambda _iI\), \(N_p = N^{m_i}\), and record the corresponding null-space \(S = \text{kernel}(N)\), \(G = \text{kernel}(N_p);\)

(7) The column vector arrays S and G recorded in step (6) are respectively the space composed of eigenvectors and that of the generalized eigenvectors. From the two we can obtain the column vector array R that is not in S: \(R = \{ r_k \mid r_k \in G, r_k \notin S \}\), \(1 \le k \le R_c\), where \(R_c\) is the number of columns in R and the columns in array R are mutually orthogonal;

(8) Construct an array of columns for each column in the column vector array R: Let \(p_k = [r_k]\), compute \(Nr_k\) and add the result to \(p_k\), i.e. \(p_k = [Nr_k, p_k]\) (in Matlab\(^{\copyright }\) language). Repeat the procedure with \(r_k = Nr_k\) until \(Nr_k = 0\) (maximum \(m_i\) iterations);

(9) When \(R_c \ge 2\), it is necessary to check the validity of the column vector array \(p_k\). The leftist column in \(p_k\) must be in the space of S so that \(Np_k(:,1) = 0\) (in Matlab\(^{\copyright }\) language, and \(p_k(:,1)\) refers to the leftist column or the first column in the vector array \(p_k\)). Otherwise it is removed. If the dimension of the array of two first columns \(\text{dim}([p_k(:,1), p_{k'}(:,1)]) = 1\), the two vector groups \(p_k\) and \(p_{k'}\) are equivalent, corresponding to the same Jordan block. In this case, the longer array is kept: \(k = \{\text{max}\{\text{dim}(p_k), \text{dim}(p_{k'})\}\}_{(k,k')}\), \(P_{ik} = p_k\). When \(\text{dim}([p_k(:,1), p_{k'}(:,1)]) > 1\), both are retained;

(10) The (generalized) eigenvector group \(P_{ik}\) corresponding to each Jordan block can be so obtained through step (9). But it is necessary to further check if some eigenvectors in S are left out. If so add them to the array of \(p_i\): \(p_i = \{p_i \in S, p_i \notin \{P_{ik}\} \}\);

(11) After steps (5)–(10), the generalized eigenvector group for the ith eigenvalue is

$$\begin{aligned} P_i = \left\{ \begin{array}{ll} \{v_i\}, &{}\quad m_i = \rho _i\\ {}[p_i, P_{ik}], &{}\quad m_i > \rho _i \end{array}; \right. \end{aligned}$$
(6)

(12) Output the transform matrix P and the Jordan normal form J of n-square matrix A as

$$\begin{aligned} P = \left\{ \begin{array}{ll} V, &{}\quad m_i \equiv \rho _i\\ \{P_i\}, &{}\quad \text{otherwise} \end{array} \right. , \quad J = P^{-1}AP. \end{aligned}$$
(7)

4 Results

The effectiveness of the new routine \(([P, J] = {\normalsize {\textsf {newjordan}}}(A))\) proposed in this paper may be verified by comparing it with \([P_1, J_1] = {\normalsize {\textsf {jordan}}}(A)\), the Jordan transform routine in Matlab\(^{\copyright }\). Due to the ordering of eigenvalues, the sequence of the resulting Jordan blocks may be different. The check the difference, it is sufficient to define a pseudo error \(\varepsilon = \Vert \sum _i^{n}\sum _j^{n}\Vert a_{ij}\Vert - \sum _i^{n}\sum _j^{n}\Vert b_{ij}\Vert \Vert\), where \(a_{ij}\) and \(b_{ij}\) are the matrix elements of \(J_1\) and J respectively.

Take the 6 low-order square matrices \(A_i\) (\(i = 1,2,\ldots , 6\)) as examples 1 to example 6,

$$\begin{aligned}&A_1 = \left( \begin{array}{ccc} 2 &{} -1 &{} -1 \\ 1 &{} -1 &{} -1 \\ -1 &{} 1 &{} 1 \\ \end{array} \right) ,\quad A_2 = \left( \begin{array}{ccc} 1 &{} -3 &{} -2 \\ -1 &{} 1 &{} -1 \\ 2 &{} 4 &{} 5 \\ \end{array} \right) ,\quad A_3 = \left( \begin{array}{ccc} -1 &{} -2 &{} 6 \\ -1 &{} 0 &{} 3 \\ -1 &{} -1 &{} 4 \\ \end{array} \right) , \\ &\quad A_4 = \left( \begin{array}{cccc} 5 &{} 4 &{} 2 &{} 1 \\ 0 &{} 1 &{} -1 &{} -1 \\ -1 &{} -1 &{} 3 &{} 0 \\ 1 &{} 1 &{} -1 &{} 2\\ \end{array} \right) ,\quad A_5 = \left( \begin{array}{cccc} 12 &{} 32 &{} 66 &{} 116 \\ -25 &{} -76 &{} -164 &{} -294 \\ 21 &{} 66 &{} 143 &{} 256 \\ -6 &{} -19 &{} -41 &{} -73\\ \end{array} \right) , \\ &\quad A_6 = \left( \begin{array}{ccccc} 1 &{} 3 &{} 6 &{} 7 &{} 5 \\ 1 &{} 2 &{} 3 &{} 4 &{} 11 \\ 5 &{} 3 &{} 7 &{} 9 &{} 8 \\ 8 &{} 10 &{} 11 &{} 13 &{} 19\\ 1 &{} 12 &{} 23 &{} 45 &{} 22\\ \end{array} \right) . \end{aligned}$$
(8)
$$\begin{aligned} A_9^{\prime } = \left( \begin{array}{ccccc} 4 &{} 6 &{} 9 &{} 5 &{} 8 \\ 7 &{} 2 &{} 10 &{} 2 &{} 10 \\ 0 &{} 5 &{} 7 &{} 4 &{} 10 \\ 9 &{} 5 &{} 4 &{} 7 &{} 5 \\ 8 &{} 10 &{} 9 &{} 6 &{} 10 \\ \end{array} \right) ,\quad A_{10}^{\prime } = \left( \begin{array}{cccccccccc} 1 &{} 0 &{} 2 &{} 4 &{} 7 &{} 5 &{} 7 &{} 8 &{} 6 &{} 6 \\ 1 &{} 8 &{} 3 &{} 9 &{} 4 &{} 0 &{} 4 &{} 9 &{} 9 &{} 3 \\ 3 &{} 1 &{} 9 &{} 8 &{} 7 &{} 9 &{} 8 &{} 10 &{} 2 &{} 8 \\ 6 &{} 5 &{} 5 &{} 5 &{} 10 &{} 6 &{} 4 &{} 5 &{} 7 &{} 10 \\ 5 &{} 3 &{} 4 &{} 8 &{} 5 &{} 0 &{} 4 &{} 2 &{} 6 &{} 10 \\ 9 &{} 4 &{} 2 &{} 9 &{} 5 &{} 0 &{} 0 &{} 2 &{} 4 &{} 1 \\ 5 &{} 7 &{} 10 &{} 4 &{} 3 &{} 2 &{} 0 &{} 5 &{} 9 &{} 2 \\ 4 &{} 2 &{} 4 &{} 3 &{} 1 &{} 5 &{} 1 &{} 8 &{} 4 &{} 0 \\ 5 &{} 3 &{} 8 &{} 6 &{} 2 &{} 1 &{} 6 &{} 3 &{} 2 &{} 6 \\ 7 &{} 2 &{} 6 &{} 9 &{} 1 &{} 0 &{} 2 &{} 5 &{} 6 &{} 1 \\ \end{array} \right) . \end{aligned}$$
(9)

Then we combine them to get Example 7 and Example 8 as \(A_7 = \text{diag}(A_1, A_2, A_3, A_4, A_5, A_6)\) and \(A_8 = \text{diag}(A_7, A_7)\). Finally, make three random rows \(A_{9, 10, 11}^{\prime } = \text{round}(\text{rand}(n)*10)\) with order \(n = 5, 10, 1000\), and let \(A_9 = \text{diag}(A_9^{\prime }, A_1)\), \(A_{10} = \text{diag}(A_{10}^{\prime }, A_1)\) and \(A_{11} = \text{diag}(A_{11}^{\prime }, A_1)\) be examples 9~11 (We will not show \(A _{11}^{\prime }\) in this paper as \(n=1000\) is too large).

On a Matlab\(^{\copyright }\) platform, the computation times for the two routines jordan and newjordan and the errors between them are shown in Table 1 (not recorded if the times exceeded 10 min). They are averaged over five calculations for each sample.

Table 1 The results of commands of jordan.m and newjordan.m on Matlab\(^{\copyright }\)

5 Discussions

We conclude this work with exemplary discussions relevant to real applications. Consider a set of stochastic differential equation

$$\begin{aligned} \dot{x}&= {} f(x) + \zeta (x,t) \\ &\approx {} F\,x + \zeta (t), \end{aligned}$$
(10)

where x is a multidimensional vector and \(\zeta\) is a random noise. According to the structural analysis of stochastic dynamics near a fixed point published in PNAS [9], the equations can be decomposed into

$$\begin{aligned} \dot{x} = -(D+Q)\,U\,x + \zeta (t). \end{aligned}$$
(11)

In the above, the matrix D is symmetric and semi-positive definite, the matrix Q is antisymmetric, and U is symmetric. On a stable fixed point and setting D to the diffusion matrix by \(\zeta (t)\), U becomes a semi-positive matrix for the thermodynamic potential, a crucial functional for various physical analyses.

Based on the symmetry and antisymmetry of the matrices, one can obtain

$$\begin{aligned} FQ + QF^{T} = FD - DF^{T}. \end{aligned}$$
(12)

This is a type of continuous Lyapunov equation. For real symmetric F, Eq. (12) can be rewritten as

$$\begin{aligned} (\lambda _{\alpha } + \lambda _{\beta }) {\tilde{Q}}_{\alpha \beta } = (\lambda _{\alpha } - \lambda _{\beta }) {\tilde{D}}_{\alpha \beta }. \end{aligned}$$
(13)

Here \(\lambda _{\alpha , \beta }\) are the eigenvalues of F. \({\tilde{Q}}_{\alpha \beta }\) are \({\tilde{D}}_{\alpha \beta }\) are the matrix elements of Q and D under the representation of F.

But for general cases it is necessary to consider the Jordan transform of the F,

$$\begin{aligned} LFL^{-1} = \Lambda , \end{aligned}$$
(14)

where the matrix \(L^{-1}\) is the transform matrix computed in this paper and \(\Lambda\) is the corresponding Jordan matrix. Similarly, in the representation of matrix F, \({\tilde{Q}} = LQL^{T}, {\tilde{D}} = LDL^{T}\), the upper triangular matrix elements (\(\alpha < \beta\)) of matrix \({\tilde{Q}}\) can be expressed as:

$$\begin{aligned} {\tilde{Q}}_{\alpha \beta }= & {} \frac{\lambda _{\alpha } - \lambda _{\beta }^{*}}{\lambda _{\alpha } + \lambda _{\beta }^{*}} {\tilde{D}}_{\alpha \beta } + 2\lambda _{\beta }^{*} \sum _{\mu \ge 1} \frac{(-1)^{\mu - 1}}{(\lambda _{\alpha } + \lambda _{\beta }^{*})^{\mu +1}} {\tilde{D}}_{\alpha +\mu , \beta } \\{} & {} -2\lambda _{\alpha } \sum _{v \ge 1} \frac{(-1)^{v - 1}}{(\lambda _{\alpha } + \lambda _{\beta }^{*})^{v+1}} {\tilde{D}}_{\alpha , \beta +v} \\{} & {} +2\sum _{\mu , v \ge 1} \frac{(-1)^{\mu +v + 1}(\mu +v-1)!}{\mu ! v! (\lambda _{\alpha } + \lambda _{\beta }^{*})^{\mu +v+1}}(\mu \lambda _{\beta }^{*} - v \lambda _{\alpha }) {\tilde{D}}_{\alpha +\mu , \beta +v}. \end{aligned}$$
(15)

Complete details can be found in [9].

To summarize, this paper discusses how to effectively calculate the Jordan canonical form and the transform matrix of nth-order square matrix A on popular platforms like Matlab\(^{\copyright }\). The eigenvalues, eigenvectors and generalized eigenvectors of an n-square A provides new insights into the Jordan transform. On a very general consideration, the new algorithm proposed realizes Jordan transform without the limitation by the order of the matrix. It quickly gives the Jordan normal form J and the corresponding transform matrix P. When dealing with practical problems, such as the high-dimensional complex, nonlinear systems presented in [10, 11, 16, 17], the method may play a crucial role and improve the computational efficiency.