# Base.nullspace for general matrices

**URL:** https://discourse.julialang.org/t/base-nullspace-for-general-matrices/112356
**Category:** General Usage
**Created:** [March 31, 2024, 3:39pm UTC](https://discourse.julialang.org/t/base-nullspace-for-general-matrices/112356 "2024-03-31T15:39:58Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![azoviktor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/azoviktor/32/206405_2.png) [@azoviktor](https://discourse.julialang.org/u/azoviktor)
#### Post date: [March 31, 2024, 3:39pm UTC](https://discourse.julialang.org/t/base-nullspace-for-general-matrices/112356/1 "2024-03-31T15:39:58Z")

</div>

Let’s say I would like to use the `nullspace` method from julia Base module for a matrix of type `AbstractMatrix{MyNumberType}`. What interface should I implement in order to make it work? Does such an interface technically exist (since it is not documented)?

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [March 31, 2024, 4:43pm UTC](https://discourse.julialang.org/t/base-nullspace-for-general-matrices/112356/2 "2024-03-31T16:43:28Z")

</div>

Looks like satisfying the asbtract array interface is sufficient. I’m not sure what kind of number you are dealing with though.

```julia-repl
julia> struct Foo{T} <: AbstractMatrix{T} end

julia> Base.getindex(::Foo{T}, _...) where T = zero(T)

julia> Base.size(::Foo) = (3,3)

julia> using LinearAlgebra

julia> nullspace(Foo{Int}())
3×3 Matrix{Float64}:
 1.0 0.0 0.0
 0.0 1.0 0.0
 0.0 0.0 1.0

```

[https://docs.julialang.org/en/v1/manual/interfaces/#man-interface-array](https://docs.julialang.org/en/v1/manual/interfaces/#man-interface-array)

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [March 31, 2024, 4:50pm UTC](https://discourse.julialang.org/t/base-nullspace-for-general-matrices/112356/3 "2024-03-31T16:50:46Z")

</div>

> [@azoviktor](#):
>
> Let’s say I would like to use the `nullspace` method from julia Base module for a matrix of type `AbstractMatrix{MyNumberType}`. What interface should I implement in order to make it work? Does such an interface technically exist (since it is not documented)?

`nullspace` uses the SVD, so you need a package like GenericLinearAlgebra.jl that supports SVD of your number type.

---

<div class="post-metadata">

### Author: ![Jean\_Michel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jean_michel/32/8282_2.png) [@Jean\_Michel](https://discourse.julialang.org/u/Jean_Michel)
#### Post date: [April 1, 2024, 10:33am UTC](https://discourse.julialang.org/t/base-nullspace-for-general-matrices/112356/4 "2024-04-01T10:33:54Z")

</div>

`GenericLinearAlgebra` despite its name is not really generic. It assumes your numbers are some kind of floating point objects. For example define a 51x51 Hilbert matrix

```julia
julia> m=[BigInt(1)//(n+m) for n in 1:51, m in 1:51];

julia> size(GenericLinearAlgebra.nullspace(m))
(51, 1)

```

while the matrix is invertible. I have written a few routines for really generic linear algebra (valid over any field, and some routines valid over any ring when applicable, similar to `det_bareiss`) which are in the package `GenLinearAlgebra`. These includes `nullspace`. I do not claim to have written the best possible algorithms, and I do not know if there has been other similar efforts. Note that the algorithms valid over any field are often not good ones for floating-point numbers.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [April 1, 2024, 12:39pm UTC](https://discourse.julialang.org/t/base-nullspace-for-general-matrices/112356/5 "2024-04-01T12:39:29Z")

</div>

> [@Jean\_Michel](#):
>
> `GenericLinearAlgebra` despite its name is not really generic. It assumes your numbers are some kind of floating point objects.

The algorithms for exact arithmetic are _completely_ different from the algorithms for inexact arithmetic in many cases; GenericLinearAlgebra is geared towards the latter.

In inexact arithmetic, `nullspace` uses an SVD (which is inherently inexact due to the Abel–Ruffini theorem), with a cutoff for the singular values. In exact arithmetic, you’d compute a nullspace basis using elimination.

---

<div class="post-metadata">

### Author: ![Jean\_Michel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jean_michel/32/8282_2.png) [@Jean\_Michel](https://discourse.julialang.org/u/Jean_Michel)
#### Post date: [April 1, 2024, 1:24pm UTC](https://discourse.julialang.org/t/base-nullspace-for-general-matrices/112356/6 "2024-04-01T13:24:34Z")

</div>

Yes, that was my point. I do not know what is the best vocabulary to talk about this dichotomy:  
exact/inexact, exact/approximate, complete field/general field ? You could possibly also do approximate computation with p-adic numbers.
