Use your own types in geometric algebra expressions

Inputs

If the type you use supports indexing, e.g. Vector, it already works:

using SymbolicGA

x = rand(3)
y = rand(3)

@ga 3 x::1 ∧ y::1
Bivector{Float64, 3, 3}(-0.024542233107283262, 0.1769258733883427, 0.03123779169975869)

If your type does not support indexing, and you don't want it to, overload SymbolicGA.getcomponent(::T, [i::Int, [j::Int]]):

struct MyInputType{T}
  values::Vector{T}
end

SymbolicGA.getcomponent(x::MyInputType, i::Int) = x.values[i]

x = MyInputType(rand(3))
y = MyInputType(rand(3))

@ga 3 x::1 ∧ y::1
Bivector{Float64, 3, 3}(0.041638081949248465, 0.4541687543369966, 0.0919779956738592)

For scalars and aggregates of objects with multiple grades, you will need to overload SymbolicGA.getcomponent(::T) and SymbolicGA.getcomponent(::T, j::Int, i::Int) respectively (see SymbolicGA.getcomponent).

Outputs

If you want to reconstruct a custom type from components, either define a constructor for a single tuple argument, e.g. T(components::Tuple)

struct MyOutputType{T}
  values::Vector{T}
end

MyOutputType(x::Tuple) = MyOutputType(collect(x))

x = rand(3)
y = rand(3)

@ga 3 MyOutputType dual(x::1 ∧ y::1)
Main.MyOutputType{Float64}([0.40861688520104295, 0.02944080745378222, -0.3324921751617663])

If you don't want such constructor to be defined, you can overload SymbolicGA.construct(::Type{T}, ::Tuple) directly:

struct MyOutputType2{T}
  values::Vector{T}
end

SymbolicGA.construct(T::Type{<:MyOutputType2}, x::Tuple) = MyOutputType2(collect(x))

x = rand(3)
y = rand(3)

@ga 3 MyOutputType2 dual(x::1 ∧ y::1)
Main.MyOutputType2{Float64}([0.12942459135496687, 0.15407256977765127, -0.32721428888922555])

Integrations for Vector, Tuple and <:Real have already been defined:

@ga 3 Tuple dual(x::1 ∧ y::1)
(0.12942459135496687, 0.15407256977765127, -0.32721428888922555)
@ga 3 Vector dual(x::1 ∧ y::1)
3-element Vector{Float64}:
  0.12942459135496687
  0.15407256977765127
 -0.32721428888922555
z = rand(3)
@ga 3 Float16 dual(x::1 ∧ y::1 ∧ z::1)
Float16(-0.1356)

This page was generated using Literate.jl.