chakravala / Grassmann.jl

⟨Grassmann-Clifford-Hodge⟩ multilinear differential geometric algebra

Home Page:https://grassmann.crucialflow.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Inverse not defined for MBlade?

eschnett opened this issue · comments

I'm trying to calculate the inverse of a blade with Grassmann v0.1.3:

julia> basis"3"
(⟨+++⟩, v, v₁, v₂, v₃, v₁₂, v₁₃, v₂₃, v₁₂₃)
julia> inv(v1+v2)
ERROR: MethodError: no method matching inv(::MBlade{Int64,⟨+++⟩,1,3})

Is this function missing, or am I looking in the wrong place?

Currently, the implementation of inv is only for single Basis and SValue elements. It can be extended to MBlade elements but not for mixed-grade MultiVector elements, I believe.

A naive implementation would be the following formula

julia> using Grassmann; basis"3"
(⟨+++⟩, v, v₁, v₂, v₃, v₁₂, v₁₃, v₂₃, v₁₂₃)

julia> Base.inv(a::TensorAlgebra) = (A=~a; A/(Aa))

julia> inv(v1+v2)
0.0 + 0.5v₁ + 0.5v₂

However, a more optimized method could also be defined, similar to the other operations.

The MBlade should now be invertible with the inv method on master branch, which will be on v0.1.4

The inv method has now been fully generalized to arbitrary MultiVector elements, provided that the it is defined for the given input value.

julia> 1/(0v+2v1)
0.0 + 0.5v₁

julia> @btime inv(0v+2v1)
  5.677 μs (80 allocations: 4.81 KiB)
0.0 + 0.5v₁

julia> @btime inv(2v1)
  62.522 ns (4 allocations: 64 bytes)
0.5v₁

julia> @btime inv(0v2+2v1)
  136.415 ns (6 allocations: 128 bytes)
0.5v₁ + 0.0v₂ + 0.0v₃

However, it is much slower for an arbitrary MultiVector input at the moment, due to type instability.

This may be improved in the future with the MultiGrade type, when it is further implemented.