# Computing Inverse of a stack of matrices

**URL:** https://discourse.julialang.org/t/computing-inverse-of-a-stack-of-matrices/45580
**Category:** General Usage
**Tags:** performance, linearalgebra
**Created:** [August 26, 2020, 4:23pm UTC](https://discourse.julialang.org/t/computing-inverse-of-a-stack-of-matrices/45580 "2020-08-26T16:23:17Z")
**Posts on this page:** 1
**Showing post:** 17

<div class="post-metadata">

### Author: ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)
#### Post date: [August 29, 2020, 1:09am UTC](https://discourse.julialang.org/t/computing-inverse-of-a-stack-of-matrices/45580/17 "2020-08-29T01:09:59Z")

</div>

An alternative to `StaticArrays` is my [Grassmann.jl](https://github.com/chakravala/Grassmann.jl) package, which can do multi-linear algebra without ever defining higher order array interfaces. Newly released v0.6 deprecated `StaticArrays`

```Julia
using Grassmann, StaticArrays
A = rand(SMatrix{3,3,Float64},1000,4);
B = Chain{ℝ3,1,Chain{ℝ3,1}}.(A);

```

The tests show that `Chain{V,1,Chain{V,1}}` is a bit slower at `inv` than `SMatrix`, yet it is actually many times faster at computing determinants as the `det` timing test shows:

```Julia
julia> @btime inv.($A);
  128.939 μs (4002 allocations: 593.83 KiB)

julia> @btime inv.($B);
  181.841 μs (4002 allocations: 906.33 KiB)

julia> @btime det.($A);
  92.142 μs (4002 allocations: 93.83 KiB)

julia> @btime det.($B);
  14.172 μs (2 allocations: 31.33 KiB)

```

As you can see, `Grassmann` algebra provides a foundation (entirely independent of `StaticArrays`) for doing multi-linear algebra. It also is faster for some other methods like `\` linear solve method.

> [@Grassmann.jl A\\b 3x faster than Julia's StaticArrays.jl](https://discourse.julialang.org/t/grassmann-jl-a-b-3x-faster-than-julias-staticarrays-jl/41451/35):
>
> This was because of a special explicit case for dimensions 1 and 2 and 3, which are now accounted for in Grassmann.jl also. Also, support has been added for Moore-Penrose inverses for underdetermined and overdetermined linear systems. For underdetermined cases, the exterior product algorithm works ~20x faster than the SMatrix algorithm, and it is numerically stable. For overdetermined equations, the method used is based on the traditional normal equations, and this is prone to more numerical i…

Instead of using a traditional `StaticArray` interface, the `Grassmann` foundation is used. This is represented with a different memory layout, which can be used in interesting ways.

I’m sure there are many more optimizations possible on `Grassmann` in the future.

---

_[View the full topic](https://discourse.julialang.org/t/computing-inverse-of-a-stack-of-matrices/45580)._
