# How to tell if a thing is invertible?

**URL:** https://discourse.julialang.org/t/how-to-tell-if-a-thing-is-invertible/16584
**Category:** General Usage
**Created:** [October 20, 2018, 11:36pm UTC](https://discourse.julialang.org/t/how-to-tell-if-a-thing-is-invertible/16584 "2018-10-20T23:36:24Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![bobcassels](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bobcassels/32/14586_2.png) [@bobcassels](https://discourse.julialang.org/u/bobcassels)
#### Post date: [October 20, 2018, 11:36pm UTC](https://discourse.julialang.org/t/how-to-tell-if-a-thing-is-invertible/16584/1 "2018-10-20T23:36:24Z")

</div>

I need an `isinvertible` function, where `isinvertible(x)` is true iff `inv(x)` is defined, and `isone(inv(x) * x)`. That’s to rule out `0`, since `inv(0)` is defined, but returns `Inf`.

In the Julia LU decomposition, There is [a test for zero](https://github.com/JuliaLang/julia/blob/master/stdlib/LinearAlgebra/src/lu.jl#L109). That is to protect the taking of `inv` a few lines later. So that `iszero` test really wants to be testing to see if the element is invertible. This matters for me, because I want to take inverses of matrices of modular integers, and often there are non-zero elements which are not invertible.

---

<div class="post-metadata">

### Author: ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)
#### Post date: [October 21, 2018, 12:46am UTC](https://discourse.julialang.org/t/how-to-tell-if-a-thing-is-invertible/16584/2 "2018-10-21T00:46:50Z")

</div>

```julia
isinvertible(x) = applicable(inv, x) && isone(inv(x) * x)

```

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [October 21, 2018, 1:04am UTC](https://discourse.julialang.org/t/how-to-tell-if-a-thing-is-invertible/16584/3 "2018-10-21T01:04:12Z")

</div>

The condition that the result be one is not necessarily true, eg in interval arithmetic.

---

<div class="post-metadata">

### Author: ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)
#### Post date: [October 21, 2018, 1:17am UTC](https://discourse.julialang.org/t/how-to-tell-if-a-thing-is-invertible/16584/4 "2018-10-21T01:17:56Z")

</div>

@bobcassels has another question outstanding: [How to test whether comparison is defined for a type?](https://discourse.julialang.org/t/how-to-test-whether-comparison-is-defined-for-a-type/16583)  
I thought that the `applicable function might help there too, but it doesn’t.

```julia
julia> applicable(<, Mod29(1) , Mod29(3))
true

```

but this fails

```julia
julia> Mod29(1) < Mod29(3)
ERROR: < not defined for Mod29

```

Because

```julia
(< )(x::T, y::T) where {T<:Real} = no_op_err("<" , T) 
# see base/promotion.jl line 426

```

That is, the `<` method applies to the arguments, but unfortunately that method forces an error.

So my suggestion to use the `applicable` function is not foolproof 🙁

---

<div class="post-metadata">

### Author: ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)
#### Post date: [October 21, 2018, 1:57am UTC](https://discourse.julialang.org/t/how-to-tell-if-a-thing-is-invertible/16584/5 "2018-10-21T01:57:29Z")

</div>

What kind of object you are expecting as input? Number? Matrices?

Sorry, just saw the end of the post, it was cropped on my tablet.

---

<div class="post-metadata">

### Author: ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)
#### Post date: [October 21, 2018, 2:00am UTC](https://discourse.julialang.org/t/how-to-tell-if-a-thing-is-invertible/16584/6 "2018-10-21T02:00:47Z")

</div>

Original question:

> [@bobcassels](#):
>
> I need an `isinvertible` function, where `isinvertible(x)` is true iff `inv(x)` is defined, and `isone(inv(x) * x)` . That’s to rule out `0` , since `inv(0)` is defined, but returns `Inf` .

---

<div class="post-metadata">

### Author: ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)
#### Post date: [October 21, 2018, 2:04am UTC](https://discourse.julialang.org/t/how-to-tell-if-a-thing-is-invertible/16584/7 "2018-10-21T02:04:39Z")

</div>

The problem you saw can be avoided by a `try ... catch` block.

---

<div class="post-metadata">

### Author: ![bobcassels](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bobcassels/32/14586_2.png) [@bobcassels](https://discourse.julialang.org/u/bobcassels)
#### Post date: [October 21, 2018, 2:14am UTC](https://discourse.julialang.org/t/how-to-tell-if-a-thing-is-invertible/16584/8 "2018-10-21T02:14:31Z")

</div>

In my case, I am interested in anything that might be an element in a matrix passed to LU decomposition. For now, my modular integers, \<:Real.

Of course I can use `try ... catch`. But that doesn’t seem “in the spirit of Julia”, at least as I understand that spirit. I want to put this in the middle of the standard library LU decomposition function.

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [October 21, 2018, 3:46am UTC](https://discourse.julialang.org/t/how-to-tell-if-a-thing-is-invertible/16584/9 "2018-10-21T03:46:45Z")

</div>

```julia
using LinearAlgebra
isinvertible(x) = issuccess(lu(x, check=false))

```

---

<div class="post-metadata">

### Author: ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)
#### Post date: [October 21, 2018, 3:58am UTC](https://discourse.julialang.org/t/how-to-tell-if-a-thing-is-invertible/16584/10 "2018-10-21T03:58:34Z")

</div>

```julia
julia> isinvertible(3)
ERROR: UndefVarError: lu not defined

```

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [October 21, 2018, 4:02am UTC](https://discourse.julialang.org/t/how-to-tell-if-a-thing-is-invertible/16584/11 "2018-10-21T04:02:25Z")

</div>

`using LinearAlgebra` first. I will edit the comment.

---

<div class="post-metadata">

### Author: ![bobcassels](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bobcassels/32/14586_2.png) [@bobcassels](https://discourse.julialang.org/u/bobcassels)
#### Post date: [October 21, 2018, 4:18am UTC](https://discourse.julialang.org/t/how-to-tell-if-a-thing-is-invertible/16584/12 "2018-10-21T04:18:09Z")

</div>

I’m not trying to check the result of lu. I want to change [the source of lu](https://github.com/JuliaLang/julia/blob/master/stdlib/LinearAlgebra/src/lu.jl#L109) to check to see if an element is invertible, and do something different if it is not.

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [October 21, 2018, 4:31am UTC](https://discourse.julialang.org/t/how-to-tell-if-a-thing-is-invertible/16584/13 "2018-10-21T04:31:13Z")

</div>

Can you give examples with inputs and desired outputs of this `isinvertible` function?

---

<div class="post-metadata">

### Author: ![bobcassels](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bobcassels/32/14586_2.png) [@bobcassels](https://discourse.julialang.org/u/bobcassels)
#### Post date: [October 21, 2018, 4:45am UTC](https://discourse.julialang.org/t/how-to-tell-if-a-thing-is-invertible/16584/14 "2018-10-21T04:45:28Z")

</div>

Imagine I have defined a type Mod26 that is integers mod 26 [where Mod26(x) is invertible iff gcd(x, 26) == 1]

```julia
isinvertible(100) # true
isinvertible(0) # false
isinvertible(-1.5) # true
isinvertible(-0.0) # false
isinvertible(Mod26(2)) # false
isinvertible(Mod26(7)) # true
isinvertible(Rational(1) + Rational(3)im) # true
isinvertible(0+0im) # false

```

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [October 21, 2018, 4:48am UTC](https://discourse.julialang.org/t/how-to-tell-if-a-thing-is-invertible/16584/15 "2018-10-21T04:48:42Z")

</div>

These should work:

```julia
isinvertible(x) = !iszero(x)
isinvertible(x::Mod26) = gcd(x, 26) == 1

```

---

<div class="post-metadata">

### Author: ![bobcassels](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bobcassels/32/14586_2.png) [@bobcassels](https://discourse.julialang.org/u/bobcassels)
#### Post date: [October 21, 2018, 4:55am UTC](https://discourse.julialang.org/t/how-to-tell-if-a-thing-is-invertible/16584/16 "2018-10-21T04:55:55Z")

</div>

Let’s pause this discussion for now. It’s not going in a useful direction.

Yes, I can define it myself. But I want to change the system’s LU decomposition, so that it works for more cases. So I want this `isinvertible` function to be part of the core, so it can be used by the standard library. I will make a pull request for the changes I want to make to Julia, and then we can discuss this in that context.

I want the standard library’s matrix inversion to work on matrices of modular numbers. It does not currently, for the reasons I’ve pointed out. I am proposing changes to allow it to work. As I said, I will propose that change, and we can discuss in that context.

---

<div class="post-metadata">

### Author: ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)
#### Post date: [October 21, 2018, 8:25am UTC](https://discourse.julialang.org/t/how-to-tell-if-a-thing-is-invertible/16584/17 "2018-10-21T08:25:07Z")

</div>

If I understand your situation correctly, it sounds like this should be solved by multiple dispatch + extension:

- Add an `isinvertible` function to Base that covers all relevant default types
- Change the LU source from `!iszero` to `isinvertible`
- Extend `isinvertible` in your code to work with your type:

```julia
import Base.isinvertible
isinvertible(x::Mod26) = gcd(x, 26) == 1

```

Btw, if you don’t want to go through the process of changing Julia itself, perhaps you could just extend `iszero(::Mod26)`? A bit of a hack, but it might work for you.

---

<div class="post-metadata">

### Author: ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)
#### Post date: [October 21, 2018, 8:35am UTC](https://discourse.julialang.org/t/how-to-tell-if-a-thing-is-invertible/16584/18 "2018-10-21T08:35:37Z")

</div>

> [@Ronis\_BR](#):
>
> The problem you saw can be avoided by a `try ... catch` block.

Please don’t suggest this 🙂 Exceptions should be reserved for exceptional circumstances, not situations like this.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [October 21, 2018, 8:54am UTC](https://discourse.julialang.org/t/how-to-tell-if-a-thing-is-invertible/16584/19 "2018-10-21T08:54:52Z")

</div>

> [@bobcassels](#):
>
> I want to change the system’s LU decomposition, so that it works for more cases. So I want this `isinvertible` function to be part of the core, so it can be used by the standard library.

Note that determining if an object in a nontrivial algebra is (numerically) invertible often has a cost that is comparable to inverting it. Checking if something is invertible without reusing the intermediate representation (eg LU for matrices, etc) may be inefficient.

It may be better to just explain your problem in detail, maybe it has a better solution. Eg if you want to generalize LU, then do that instead.
