# Inv not working in mac?

**URL:** <https://discourse.julialang.org/t/inv-not-working-in-mac/113014>\
**Category:** General Usage\
**Created:** [April 16, 2024, 1:47pm UTC](https://discourse.julialang.org/t/inv-not-working-in-mac/113014 "2024-04-16T13:47:56Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ivanricardo](https://avatars.discourse-cdn.com/v4/letter/i/91b2a8/32.png) [@ivanricardo](https://discourse.julialang.org/u/ivanricardo)\
**Post date:** [April 16, 2024, 1:47pm UTC](https://discourse.julialang.org/t/inv-not-working-in-mac/113014/1 "2024-04-16T13:47:56Z")

</div>

Hello,

So I have just installed julia 1.10.2 on a mac (M1) and I try running

```julia
A = [1 2 3;4 5 6;7 8 9]

inv(A)

```

and I expect a singular error. However, I actually get a matrix as an output. What is weirder is that if I do the same on a Windows or a Linux machine running arch, I do indeed get an error. Is this something to do with Mac or is there a problem with how I am running my code? For reference, I installed julia 1.6.4 on mac and the same code does in fact throw an error.

Is there any intuition behind this?

---

<div class="post-metadata">

**Author:** ![dmbates](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmbates/32/44_2.png) [@dmbates](https://discourse.julialang.org/u/dmbates)\
**Post date:** [April 16, 2024, 2:12pm UTC](https://discourse.julialang.org/t/inv-not-working-in-mac/113014/2 "2024-04-16T14:12:33Z")

</div>

Determining if a matrix is singular or not is an inherently unstable computation. Different orders of operations of cut-offs on the condition number will give different results.

```julia
julia> using AppleAccelerate

julia> A = [1 2 3; 4 5 6; 7 8 9]
3×3 Matrix{Int64}:
 1 2 3
 4 5 6
 7 8 9

julia> using LinearAlgebra

julia> cond(A) # condition number
5.015946803014265e16
julia> inv(A) # an unstable approximation to the inverse
3×3 Matrix{Float64}:
  3.15252e15 -6.30504e15 3.15252e15
 -6.30504e15 1.26101e16 -6.30504e15
  3.15252e15 -6.30504e15 3.15252e15

julia> versioninfo()
Julia Version 1.10.2
Commit bd47eca2c8a (2024-03-01 10:14 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: macOS (arm64-apple-darwin22.4.0)
  CPU: 8 × Apple M1 Pro
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, apple-m1)
Threads: 6 default, 0 interactive, 3 GC (on 6 virtual cores)
Environment:
  JULIA_EDITOR = code

```

This matrix is singular in theory but not computationally singular in the tolerances used here.

---

<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:** [April 16, 2024, 2:14pm UTC](https://discourse.julialang.org/t/inv-not-working-in-mac/113014/3 "2024-04-16T14:14:40Z")

</div>

I also get a matrix on my phone.

```julia
julia> A = [1 2 3;4 5 6;7 8 9]
3×3 Matrix{Int64}:
 1 2 3
 4 5 6
 7 8 9

julia> inv(A)
3×3 Matrix{Float64}:
  3.15252e15 -6.30504e15 3.15252e15
 -6.30504e15 1.26101e16 -6.30504e15
  3.15252e15 -6.30504e15 3.15252e15

julia> versioninfo()
Julia Version 1.10.0
Commit 3120989f39b (2023-12-25 18:01 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (aarch64-linux-gnu)
  CPU: 6 × Cortex-A55
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, cortex-a55)
  Threads: 1 on 8 virtual cores

```

---

<div class="post-metadata">

**Author:** ![ivanricardo](https://avatars.discourse-cdn.com/v4/letter/i/91b2a8/32.png) [@ivanricardo](https://discourse.julialang.org/u/ivanricardo)\
**Post date:** [April 16, 2024, 2:21pm UTC](https://discourse.julialang.org/t/inv-not-working-in-mac/113014/4 "2024-04-16T14:21:24Z")

</div>

I check eps() on all devices and they yield the same value. Wouldn’t I expect a different eps() across devices if the tolerance was different? Or how could I change the default tolerance for the inv function?

---

<div class="post-metadata">

**Author:** ![dmbates](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmbates/32/44_2.png) [@dmbates](https://discourse.julialang.org/u/dmbates)\
**Post date:** [April 16, 2024, 2:32pm UTC](https://discourse.julialang.org/t/inv-not-working-in-mac/113014/5 "2024-04-16T14:32:54Z")

</div>

I believe that `eps()` is just based on the floating point representation and essentially all architectures use the IEEE Floating Point Standard and would return the same value. The differences could show up in BLAS implementations because floating point arithmetic is not associative and the order of operations can affect the result.

It is not so much the `inv` operation as the LU factorization that determines if the matrix is deemed singular

```julia
julia> lu(A)
LU{Float64, Matrix{Float64}, Vector{Int64}}
L factor:
3×3 Matrix{Float64}:
 1.0 0.0 0.0
 0.142857 1.0 0.0
 0.571429 0.5 1.0
U factor:
3×3 Matrix{Float64}:
 7.0 8.0 9.0
 0.0 0.857143 1.71429
 0.0 0.0 -1.58603e-16

```

You can see that the U matrix is essentially singular. I’m not sure where the check on singularity of the LU factorization is performed.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [April 16, 2024, 3:00pm UTC](https://discourse.julialang.org/t/inv-not-working-in-mac/113014/6 "2024-04-16T15:00:50Z")

</div>

On an x86 linux system, I see

```julia
julia> inv([1 2 3;4 5 6;7 8 9])
ERROR: LinearAlgebra.SingularException(3)
julia> inv(Number[1 2 3;4 5 6;7 8 9])
3×3 Matrix{Number}:
 -4.5036e15 9.0072e15 -4.5036e15
  9.00720e15 -1.80144e16 9.0072e15
 -4.5036e15 9.0072e15 -4.5036e15

```

The TLDR is that whether `inv` succeeds or fails depends on the implementation of the LU factorization which depends on BLAS library and the vector width of your CPU (and possibly other things).

---

<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 16, 2024, 3:42pm UTC](https://discourse.julialang.org/t/inv-not-working-in-mac/113014/7 "2024-04-16T15:42:00Z")

</div>

> [@dmbates](#):
>
> You can see that the U matrix is essentially singular.

It only throws an exception if it hits a pivot that is exactly zero.

See also [Checking for singularity of matrix - #4 by stevengj](https://discourse.julialang.org/t/checking-for-singularity-of-matrix/54746/4)

---

<div class="post-metadata">

**Author:** ![ivanricardo](https://avatars.discourse-cdn.com/v4/letter/i/91b2a8/32.png) [@ivanricardo](https://discourse.julialang.org/u/ivanricardo)\
**Post date:** [April 16, 2024, 3:53pm UTC](https://discourse.julialang.org/t/inv-not-working-in-mac/113014/8 "2024-04-16T15:53:31Z")

</div>

Yes but why is this the case across different machines? All running the same version of Julia? I think this should be fixed across different machines right?

---

<div class="post-metadata">

**Author:** ![dmbates](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmbates/32/44_2.png) [@dmbates](https://discourse.julialang.org/u/dmbates)\
**Post date:** [April 16, 2024, 4:15pm UTC](https://discourse.julialang.org/t/inv-not-working-in-mac/113014/9 "2024-04-16T16:15:59Z")

</div>

As @Oscar_Smith said, the result of a floating point calculation can vary from machine to machine depending on the order in which operations are performed. A likely culprit here is the vector width of the CPU floating point registers. I think the Apple M processors’ registers are 128 bits whereas many Intel processors are 256 bits. If the LU factorization takes advantage of the full width of the registers then different processors will evaluate the expressions in the factorization in a different order with potentially different results. Generally the differences would be negligible but for singularity it is a matter of “really very, very small in magnitude” versus zero, as @stevengj implied.

---

<div class="post-metadata">

**Author:** ![ivanricardo](https://avatars.discourse-cdn.com/v4/letter/i/91b2a8/32.png) [@ivanricardo](https://discourse.julialang.org/u/ivanricardo)\
**Post date:** [April 16, 2024, 4:21pm UTC](https://discourse.julialang.org/t/inv-not-working-in-mac/113014/10 "2024-04-16T16:21:46Z")

</div>

Okok this has actually been super helpful to learn about. Thank you @dmbates @Oscar_Smith
