Skip to content

Commit

Permalink
improved differential mixed symmetry dispatch #31
Browse files Browse the repository at this point in the history
  • Loading branch information
chakravala committed Sep 9, 2019
1 parent 10d9966 commit 225bb79
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 28 deletions.
48 changes: 34 additions & 14 deletions src/algebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import Base: +, -, *, ^, /, //, inv, <, >, <<, >>, >>>
import AbstractLattices: , , dist
import AbstractTensors: , , , , , , , , rem, div, contraction
import DirectSum: diffcheck, tangent, hasinforigin, hasorigininf
import DirectSum: diffcheck, diffmode, tangent, hasinforigin, hasorigininf
export tangent

const Field = Real
Expand Down Expand Up @@ -50,7 +50,7 @@ function declare_mutating_operations(M,F,set_val,SUB,MUL)
if v 0 && !diffcheck(V,a,b)
A,B,Q,Z = symmetricmask(V,a,b)
val = (typeof(V)<:Signature || count_ones(A&B)==0) ? (parity(A,B,V) ? $SUB(v) : v) : $MUL(parityinner(A,B,V),v)
diffvars(V)0 && !iszero(Z) && (val *= Basis{V}(Z))
diffvars(V)0 && !iszero(Z) && (val *= Basis{V}(Z); count_ones(Q)+order(val)>diffmode(V) && (return m))
$s(m,val,(AB)|Q,Dimension{N}())
end
return m
Expand All @@ -60,7 +60,7 @@ function declare_mutating_operations(M,F,set_val,SUB,MUL)
A,B,Q,Z = symmetricmask(V,a,b)
pcc,bas,cc = (hasinf(V) && hasorigin(V)) ? conformal(A,B,V) : (false,AB,false)
val = (typeof(V)<:Signature || count_ones(A&B)==0) ? (parity(A,B,V)pcc ? $SUB(v) : v) : $MUL(parityinner(A,B,V),pcc ? $SUB(v) : v)
diffvars(V)0 && !iszero(Z) && (val *= Basis{V}(Z))
diffvars(V)0 && !iszero(Z) && (val *= Basis{V}(Z); count_ones(Q)+order(val)>diffmode(V) && (return m))
$s(m,val,bas|Q,Dimension{N}())
cc && $s(m,hasinforigin(V,A,B) ? $SUB(val) : val,(conformalmask(V)bas)|Q,Dimension{N}())
end
Expand All @@ -78,7 +78,11 @@ function declare_mutating_operations(M,F,set_val,SUB,MUL)
if val 0
g,C,t,Z = $uct(A,B,V)
v = val
diffvars(V)0 && !iszero(Z) && (v *= Basis{V}(Z))
if diffvars(V)0 && !iszero(Z)
_,_,Q,_ = symmetricmask(V,A,B)
v *= Basis{V}(Z)
count_ones(Q)+order(v)>diffmode(V) && (return m)
end
t && $s(m,typeof(V) <: Signature ? g ? $SUB(v) : v : $MUL(g,v),C,Dimension{N}())
end
return m
Expand Down Expand Up @@ -200,8 +204,14 @@ end

for Blade MSB
@eval begin
*(a::$Blade{V},b::Basis{V}) where V = SBlade{V}(a.v,basis(a)*b)
*(a::Basis{V},b::$Blade{V}) where V = SBlade{V}(b.v,a*basis(b))
function *(a::$Blade{V},b::Basis{V}) where V
bas = basis(a)*b
order(a.v)+order(bas)>diffmode(V) ? zero(V) : SBlade{V}(a.v,bas)
end
function *(a::Basis{V},b::$Blade{V}) where V
bas = a*basis(b)
order(b.v)+order(bas)>diffmode(V) ? zero(V) : SBlade{V}(b.v,bas)
end
end
end

Expand Down Expand Up @@ -251,7 +261,7 @@ function ∧(a::X,b::Y) where {X<:TensorTerm{V},Y<:TensorTerm{V}} where V
A,B,Q,Z = symmetricmask(V,ba,bb)
((count_ones(A&B)>0) || diffcheck(V,ba,bb)) && (return g_zero(V))
v = value(a)*value(b)
diffvars(V)0 && !iszero(Z) && (v = SBlade{V}(Basis{V}(Z),v))
diffvars(V)0 && !iszero(Z) && (v = SBlade{V}(Basis{V}(Z),v); count_ones(Q)+order(v)>diffmode(V) && (return zero(V)))
return SBlade{V}(parity(x,y) ? -v : v,Basis{V}((AB)|Q))
end

Expand Down Expand Up @@ -284,7 +294,11 @@ function ∨(a::X,b::Y) where {X<:TensorTerm{V},Y<:TensorTerm{V}} where V
p,C,t,Z = regressive(bits(basis(a)),bits(basis(b)),V)
!t && (return g_zero(V))
v = value(a)*value(b)
diffvars(V)0 && !iszero(Z) && (v = SBlade{V}(Basis{V}(Z),v))
if diffvars(V)0 && !iszero(Z)
_,_,Q,_ = symmetricmask(V,bits(basis(a)),bits(basis(b)))
v = SBlade{V}(Basis{V}(Z),v)
count_ones(Q)+order(v)>diffmode(V) && (return zero(V))
end
return SBlade{V}(p ? -v : v,Basis{V}(C))
end

Expand Down Expand Up @@ -326,7 +340,11 @@ function contraction(a::X,b::Y) where {X<:TensorTerm{V},Y<:TensorTerm{V}} where
g,C,t,Z = interior(bits(basis(a)),bits(basis(b)),V)
!t && (return g_zero(V))
v = value(a)*value(b)
diffvars(V)0 && !iszero(Z) && (v = SBlade{V}(Basis{V}(Z),v))
if diffvars(V)0 && !iszero(Z)
_,_,Q,_ = symmetricmask(V,bits(basis(a)),bits(basis(b)))
v = SBlade{V}(Basis{V}(Z),v)
count_ones(Q)+order(v)>diffmode(V) && (return zero(V))
end
return SBlade{V}(typeof(V) <: Signature ? (g ? -v : v) : g*v,Basis{V}(C))
end

Expand Down Expand Up @@ -371,7 +389,11 @@ function cross(a::X,b::Y) where {X<:TensorTerm{V},Y<:TensorTerm{V}} where V
p,C,t,Z = crossprod(bits(basis(a)),bits(basis(b)),V)
!t && (return zero(V))
v = value(a)*value(b)
diffvars(V)0 && !iszero(Z) && (v = SBlade{V}(Basis{V}(Z),v))
if diffvars(V)0 && !iszero(Z)
_,_,Q,_ = symmetricmask(V,bits(basis(a)),bits(basis(b)))
v = SBlade{V}(Basis{V}(Z),v)
count_ones(Q)+order(v)>diffmode(V) && (return zero(V))
end
return SBlade{V}(p ? -v : v,Basis{V}(C))
end

Expand Down Expand Up @@ -491,10 +513,8 @@ function generate_product_algebra(Field=Field,VEC=:mvec,MUL=:*,ADD=:+,SUB=:-,CON
end
end
for (A,B) [(A,B) for A MSB, B MSB]
@eval begin
function *(a::$A{V,G,A,T} where {V,G,A},b::$B{W,L,B,S} where {W,L,B}) where {T<:$Field,S<:$Field}
SBlade($MUL(a.v,b.v),basis(a)*basis(b))
end
@eval function *(a::$A{V,G,A,T} where {V,G,A},b::$B{W,L,B,S} where {W,L,B}) where {T<:$Field,S<:$Field}
SBlade($MUL(a.v,b.v),basis(a)*basis(b))
end
end
for Chain MSC
Expand Down
44 changes: 30 additions & 14 deletions src/multivectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,26 @@ for Blade ∈ MSB
export $Blade
@pure $Blade(b::Basis{V,G}) where {V,G} = $Blade{V}(b)
@pure $Blade{V}(b::Basis{V,G}) where {V,G} = $Blade{V,G,b,Int}(1)
$Blade(v,b::TensorTerm{V}) where V = $Blade{V}(v,b)
$Blade{V}(v,b::SBlade{V,G}) where {V,G} = $Blade{V,G,basis(b)}(v*b.v)
$Blade{V}(v,b::MBlade{V,G}) where {V,G} = $Blade{V,G,basis(b)}(v*b.v)
$Blade{V}(v::T,b::Basis{V,G}) where {V,G,T} = $Blade{V,G,b,T}(v)
$Blade{V,G}(v::T,b::Basis{V,G}) where {V,G,T} = $Blade{V,G,b,T}(v)
$Blade{V,G}(v,b::SBlade{V,G}) where {V,G} = $Blade{V,G,basis(b)}(v*b.v)
$Blade{V,G}(v,b::MBlade{V,G}) where {V,G} = $Blade{V,G,basis(b)}(v*b.v)
$Blade{V,G,B}(v::T) where {V,G,B,T} = $Blade{V,G,B,T}(v)
$Blade{V}(v::T) where {V,T} = $Blade{V,0,Basis{V}(),T}(v)
$Blade{V,G,B}(v::T) where {V,G,B,T} = $Blade{V,G,B,T}(v)
$Blade(v,b::TensorTerm{V}) where V = $Blade{V}(v,b)
$Blade{V}(v::T,b::Basis{V,G}) where {V,G,T} = $Blade{V,G}(v,b)
function $Blade{V,G}(v::T,b::Basis{V,G}) where {V,G,T}
order(v)+order(b)>diffmode(V) ? zero(V) : $Blade{V,G,b,T}(v)
end
function $Blade{V,G,B}(b::T) where T<:TensorTerm{V} where {V,G,B}
order(B)+order(b)>diffmode(V) ? zero(V) : $Blade{V,G,B,Any}(b)
end
function show(io::IO,m::$Blade)
T = valuetype(m)
print(io,((!(T<:TensorMixed))&&Tparany ? [m.v] : ['(',m.v,')'])...,basis(m))
par = (!(T<:TensorMixed)) && Tparany
!par && (par = typeof(value(m)) <: TensorTerm)
print(io,(par ? [m.v] : ['(',m.v,')'])...,basis(m))
end
end
for Other MSB, VG ((:V,),(:V,:G))
@eval function $Blade{$(VG...)}(v,b::$Other{V,G}) where {V,G}
order(v)+order(b)>diffmode(V) ? zero(V) : $Blade{V,G,basis(b)}(v*b.v)
end
end
end
Expand Down Expand Up @@ -158,12 +166,15 @@ for (Chain,vector,Blade) ∈ ((MSC[1],:MVector,MSB[1]),(MSC[2],:SVector,MSB[2]))
end
@inbounds DirectSum.printindices(io,V,ib[1])
for k 2:length(ib)
@inbounds tmv = typeof(m.v[k])
@inbounds mvs = m.v[k]
tmv = typeof(mvs)
if T == Any && (tmv parsym || tmv <: TensorAlgebra)
@inbounds (!(tmv<:TensorMixed))&&tmvparval ? print(io," + ",m.v[k]) : print(io," + (",m.v[k],")")
par = (!(tmv<:TensorMixed)) && tmvparval
!par && (par = typeof(mvs) <: TensorTerm)
par ? print(io," + ",mvs) : print(io," + (",mvs,")")
else
@inbounds sbm = signbit(m.v[k])
@inbounds print(io,sbm ? " - " : " + ",sbm ? abs(m.v[k]) : m.v[k])
sbm = signbit(mvs)
print(io,sbm ? " - " : " + ",sbm ? abs(mvs) : mvs)
end
@inbounds DirectSum.printindices(io,V,ib[k])
end
Expand Down Expand Up @@ -299,7 +310,9 @@ function show(io::IO, m::MultiVector{T,V}) where {T,V}
@inbounds if mvs 0
tmv = typeof(mvs)
if T == Any && (tmv parsym || tmv <: TensorAlgebra)
(!(tmv<:TensorMixed))&&tmvparval ? print(io," + ",mvs) : print(io," + (",mvs,")")
par = (!(tmv<:TensorMixed)) && tmvparval
!par && (par = typeof(mvs) <: TensorTerm)
par ? print(io," + ",mvs) : print(io," + (",mvs,")")
else
sba = signbit(mvs)
print(io,sba ? " - " : " + ",sba ? abs(mvs) : mvs)
Expand Down Expand Up @@ -473,6 +486,9 @@ valuetype(t::MultiGrade) = promote_type(valuetype.(terms(t))...)
@pure grade(m::Union{MChain{T,V,G},SChain{T,V,G}} where {T,V}) where G = G
@pure grade(m::SparseChain{V,G} where V) where G = G
@pure grade(m::Real) = 0
@pure order(m::Basis{V,G,B} where G) where {V,B} = count_ones(symmetricmask(V,B,B)[4])
@pure order(m::Union{MBlade,SBlade}) = order(basis(m))+order(value(m))
@pure order(m) = 0
@pure bits(m::T) where T<:TensorTerm = bits(basis(m))

@pure isinf(e::Basis{V}) where V = hasinf(e) && count_ones(bits(e)) == 1
Expand Down

0 comments on commit 225bb79

Please sign in to comment.