# Wrong rank of this simple matrix

**URL:** <https://discourse.julialang.org/t/wrong-rank-of-this-simple-matrix/122477>\
**Category:** General Usage\
**Tags:** question, package, linearalgebra, matrices\
**Created:** [November 11, 2024, 2:30am UTC](https://discourse.julialang.org/t/wrong-rank-of-this-simple-matrix/122477 "2024-11-11T02:30:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![singularity](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@singularity](https://discourse.julialang.org/u/singularity)\
**Post date:** [November 11, 2024, 2:30am UTC](https://discourse.julialang.org/t/wrong-rank-of-this-simple-matrix/122477/1 "2024-11-11T02:30:02Z")

</div>

Hi. I am using Julia 1.10.4. I am using the LinearAlgebra package to compute rank, nullspace, et cetera for matrices. My code is unexpectedly throwing some errors and it turns out it seems to be calculating matrix rank incorrectly. For instance, the 2x2 matrix below clearly has linearly dependent columns with one of the eigenvalues being 0. However, the LinearAlgebra package outputs a rank of 2 and nullspace with an empty array. These types of matrices, by definition, have at most rank N-1 where N is the size of the matrix due to the row-sum = 0 constraint. So I am not sure what is going on.

`[-0.3545449435825117 0.3545449435825117; 19.667090686581037 -19.667090686581037]`

 ![Screenshot from 2024-11-10 19-27-31](https://global.discourse-cdn.com/julialang/original/3X/d/9/d975e6311a0e2556652847b4871eec9951c3ec48.png)

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [November 11, 2024, 2:52am UTC](https://discourse.julialang.org/t/wrong-rank-of-this-simple-matrix/122477/2 "2024-11-11T02:52:14Z")

</div>

The quick response: It is probably because of numerical accuracy and floating point representation inaccuracy.  
Consider:

```julia
julia> A = [-0.3545449435825117 0.3545449435825117; 19.667090686581037 -19.667090686581037]
2×2 Matrix{Float64}:
 -0.354545 0.354545
 19.6671 -19.6671

julia> rank(A)
2

julia> svd(A)
SVD{Float64, Float64, Matrix{Float64}, Vector{Float64}}
U factor:
2×2 Matrix{Float64}:
 -0.0180244 0.999838
  0.999838 0.0180244
singular values:
2-element Vector{Float64}:
 27.817985483899523
  1.2560739669470206e-14
Vt factor:
2×2 Matrix{Float64}:
  0.707107 -0.707107
 -0.707107 -0.707107

```

The second singular value is very small but not zero.

And using the `rtol` parameter:

```julia
julia> rank(A; rtol=0.0001)
1

```

and

```julia
julia> nullspace(A, atol=0.001)
2×1 Matrix{Float64}:
 -0.7071067811865472
 -0.7071067811865478

```

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [November 11, 2024, 9:16am UTC](https://discourse.julialang.org/t/wrong-rank-of-this-simple-matrix/122477/3 "2024-11-11T09:16:11Z")

</div>

This is the kind of problem the `Note` in the docstring for `rank` warns about:

```julia
  │ Note
  │
  │ Numerical rank can be a sensitive and imprecise characterization of ill-conditioned matrices
  │ with singular values that are close to the threshold tolerance max(atol, rtol*σ₁). In such
  │ cases, slight perturbations to the singular-value computation or to the matrix can change the
  │ result of rank by pushing one or more singular values across the threshold. These variations can
  │ even occur due to changes in floating-point errors between different Julia versions,
  │ architectures, compilers, or operating systems.

```
