Skip to content

Commit

Permalink
implemented dispatch of UniformScaling as universal pseudoscalar
Browse files Browse the repository at this point in the history
  • Loading branch information
chakravala committed Feb 26, 2019
1 parent f4e4f78 commit 9536ca1
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,11 @@ v₂₃
```
Effort of computation depends on the sparsity. Then it is possible to assign the **quaternion** generators `i,j,k` with
```Julia
julia> i,j,k = complementright.((-Λ(3).v1,-Λ(3).v2,-Λ(3).v3))
(-1v₂₃, 1v₁₃, -1v₁₂)
julia> i,j,k = hyperplanes(ℝ^3)
3-element Array{SValue{⟨+++⟩,2,B,Int64} where B,1}:
-1v₂₃
1v₁₃
-1v₁₂

julia> @btime i^2, j^2, k^2, i*j*k
158.925 ns (5 allocations: 112 bytes)
Expand Down
6 changes: 6 additions & 0 deletions src/Grassmann.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ include("forms.jl")
include("symbolic.jl")
include("generators.jl")

## fundamentals

export hyperplanes

hyperplanes(V::VectorSpace{N}) where N = map(n->I*getbasis(V,1<<n),0:N-1)

abstract type SubAlgebra{V} <: TensorAlgebra{V} end

adjoint(G::A) where A<:SubAlgebra{V} where V = Λ(dual(V))
Expand Down
33 changes: 31 additions & 2 deletions src/algebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,19 @@ end
#*(a::Basis{V},b::MultiGrade{V}) where V = MultiGrade{V}(b.v,a*basis(b))
#*(a::MultiGrade{V},b::MultiGrade{V}) where V = MultiGrade{V}(a.v*b.v,basis(a)*basis(b))

for Value MSV
@eval begin
*(a::UniformScaling,b::$Value{V}) where V = V(a)*b
*(a::$Value{V},b::UniformScaling) where V = a*V(b)
end
end
for Blade MSB
@eval begin
*(a::UniformScaling,b::$Blade{T,V} where T) where V = V(a)*b
*(a::$Blade{T,V} where T,b::UniformScaling) where V = a*V(b)
end
end

## exterior product

export ,
Expand All @@ -159,7 +172,10 @@ function ∧(a::X,b::Y) where {X<:TensorTerm{V},Y<:TensorTerm{V}} where V
return SValue{V}(parity(x,y) ? -v : v,Basis{V}(AB))
end

(a::X,b::Y) where {X<:TensorAlgebra,Y<:TensorAlgebra} = interop(,a,b)
@inline (a::X,b::Y) where {X<:TensorAlgebra,Y<:TensorAlgebra} = interop(,a,b)
@inline (a::TensorAlgebra{V},b::UniformScaling{T}) where {V,T<:Field} = aV(b)
@inline (a::UniformScaling{T},b::TensorAlgebra{V}) where {V,T<:Field} = V(a)b


@inline exterior_product!(V::VectorSpace,out,α,β,γ) = (count_ones&β)==0) && joinaddmulti!(V,out,α,β,γ)

Expand Down Expand Up @@ -215,6 +231,9 @@ for r ∈ (:reverse,:involve,:conj)
end
end

reverse(a::UniformScaling{Bool}) = UniformScaling(!a.λ)
reverse(a::UniformScaling{T}) where T<:Field = UniformScaling(-a.λ)

## inner product: a ∨ ⋆(b)

import LinearAlgebra: dot,
Expand Down Expand Up @@ -256,7 +275,10 @@ function ∨(a::X,b::Y) where {X<:TensorTerm{V},Y<:TensorTerm{V}} where V
return SValue{V}(p ? -v : v,Basis{V}(C))
end

(a::X,b::Y) where {X<:TensorAlgebra,Y<:TensorAlgebra} = interop(,a,b)
@inline (a::X,b::Y) where {X<:TensorAlgebra,Y<:TensorAlgebra} = interop(,a,b)
@inline (a::TensorAlgebra{V},b::UniformScaling{T}) where {V,T<:Field} = aV(b)
@inline (a::UniformScaling{T},b::TensorAlgebra{V}) where {V,T<:Field} = V(a)b


@pure function regressive_calc(N,M,S,A,B)
α,β = complement(N,A),complement(N,B)
Expand All @@ -270,6 +292,13 @@ end
end
end

## cross product

import LinearAlgebra: cross
export ×

cross(a::TensorAlgebra{V},b::TensorAlgebra{V}) where V = I*(ab)

### parity cache

@eval begin
Expand Down
8 changes: 8 additions & 0 deletions src/multivectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ abstract type TensorMixed{T,V} <: TensorAlgebra{V} end

import DirectSum: shift_indices, printindex, printindices, VTI

## pseudoscalar

using LinearAlgebra
import LinearAlgebra: I
export UniformScaling, I

## MultiBasis{N}

struct Basis{V,G,B} <: TensorTerm{V,G}
Expand Down Expand Up @@ -382,6 +388,8 @@ end

## conversions

@inline (V::VectorSpace)(s::UniformScaling{T}) where T = SValue{V}(T<:Bool ? (s.λ ? -(one(T)) : one(T)) : s.λ,getbasis(V,(one(Bits)<<ndims(V))-1))

@pure function (W::VectorSpace)(b::Basis{V}) where V
V==W && (return b)
!(VW) && throw(error("cannot convert from $(V) to $(W)"))
Expand Down

0 comments on commit 9536ca1

Please sign in to comment.