# Computing the rank of a symbolic matrix in Julia

**URL:** <https://discourse.julialang.org/t/computing-the-rank-of-a-symbolic-matrix-in-julia/48651>\
**Category:** General Usage\
**Tags:** question, package\
**Created:** [October 19, 2020, 6:25pm UTC](https://discourse.julialang.org/t/computing-the-rank-of-a-symbolic-matrix-in-julia/48651 "2020-10-19T18:25:17Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![AS90](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@AS90](https://discourse.julialang.org/u/AS90)\
**Post date:** [October 22, 2020, 9:09am UTC](https://discourse.julialang.org/t/computing-the-rank-of-a-symbolic-matrix-in-julia/48651/21 "2020-10-22T09:09:39Z")

</div>

No worries: I had suspected that \mathcal{C}(s) and \mathcal{O}(s) for the PBH test matrices were misnomers, if you will, but the point was clear and of use. Grateful for the SNF survey and suggestion, but I cannot deviate from symbolic forms, because the scope is to advance eigenvalue conditions for the VAR representation of x\_{t} in y\_{t} under all possible cases. Can I please ask you to try this code in order to check how long the machine may employ to deliver the eigenvalues? Thankful.

> using SymPy, LinearAlgebra  
> A=[Sym[“a$i$j” for i in 1:3, j in 1:3] zeros(3, 1); zeros(1, 4)]; B=Sym[“b$i” for i in 1:4];  
> M=[1 0 0 0]; C=M_A; D=M_B;  
> Obs=[C; C_A; C_A^2; C_A^3]; ro=Obs.rank();  
> T=[Obs[1:3, 1:4]; 0 0 0 1];  
> Ad=inv(T)AT; Bd = inv(T)B; Cd = CT;  
> Am=Ad[1:3, 1:3]; Bm=Bd[1:3]; Cm=Cd[1:1, 1:3];  
> Fm=Am-Bm_inv(D)\*Cm; eigvals(Fm)

---

<div class="post-metadata">

**Author:** ![AS90](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@AS90](https://discourse.julialang.org/u/AS90)\
**Post date:** [October 22, 2020, 9:11am UTC](https://discourse.julialang.org/t/computing-the-rank-of-a-symbolic-matrix-in-julia/48651/22 "2020-10-22T09:11:18Z")

</div>

To j\_verzani: did you manage to run the code, by any chance? Cheers.

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [October 22, 2020, 6:05pm UTC](https://discourse.julialang.org/t/computing-the-rank-of-a-symbolic-matrix-in-julia/48651/23 "2020-10-22T18:05:00Z")

</div>

Can you reformat? There is some missing operator with

```julia
C=M A; D=M B;

```

But it likely doesn’t matter. SymPy seems to be fine with eigenvalues of arbitrary 3x3 matrices, but 4x4 are a problem.

---

<div class="post-metadata">

**Author:** ![AS90](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@AS90](https://discourse.julialang.org/u/AS90)\
**Post date:** [October 22, 2020, 6:35pm UTC](https://discourse.julialang.org/t/computing-the-rank-of-a-symbolic-matrix-in-julia/48651/24 "2020-10-22T18:35:25Z")

</div>

Sorry about that, it was supposed to show the multiplication signs. In pasting, the quotation marks were moreover transmuted into smart quotes; B was also defined inefficiently. The correct version be should today’s, which I presented to the other user; at any rate, my machine managed to return the eigenvalues after an odd hour, in line with your technical insight. Thanks for the symbolic solution, anyway.

[Eigenvalues (asymmetric)|690x201](https://discourse.julialang.org/uploads/short-url/iiMV7GPCcOxoMNWi4f2VXTdPVF2.png)

---

<div class="post-metadata">

**Author:** ![AS90](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@AS90](https://discourse.julialang.org/u/AS90)\
**Post date:** [October 22, 2020, 8:38pm UTC](https://discourse.julialang.org/t/computing-the-rank-of-a-symbolic-matrix-in-julia/48651/25 "2020-10-22T20:38:37Z")

</div>

How can I get SymPy to compute controllability matrix \mathcal{C}=[B \; \cdots \; A^{n\_{x}-1}B] through an element loop? This command returns a 1\times3 row vector, as opposed to a 3\times3 matrix.

> using SymPy, LinearAlgebra, Statistics, Compat  
> A=[Sym[“a$i$j” for i in 1:2, j in 1:2] zeros(2, 1); zeros(1, 3)]; B=Sym[“b$i” for i in 1:3];  
> M=[1 0 0]; C=M_A; D=M_B; nx=size(A, 1)  
> Con=[A^(i)\*B for i in 0:nx-1]

The element wise (dot) alternative by contrast returns the wrong matrix. Thanks.

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [October 22, 2020, 8:58pm UTC](https://discourse.julialang.org/t/computing-the-rank-of-a-symbolic-matrix-in-julia/48651/26 "2020-10-22T20:58:05Z")

</div>

As used here, SymPy is just using Julia’s AbstractArray type. So this is a linear algebra question. Maybe, `reduce(hcat, ([A^i for i in 0:(n-1)]..., B))` is what you want. (BTW, the code you had earlier seems to have an issue with your “D” matrix, so I couldn’t test.)

[Previous page](https://discourse.julialang.org/t/computing-the-rank-of-a-symbolic-matrix-in-julia/48651.md?page=1)
