Skip to content

Commit

Permalink
improved higher order nested differential dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
chakravala committed Oct 6, 2019
1 parent 9968946 commit 39c3234
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
8 changes: 4 additions & 4 deletions src/Grassmann.jl
Original file line number Diff line number Diff line change
Expand Up @@ -433,10 +433,10 @@ function __init__()
@require Reduce="93e0c654-6965-5f22-aba9-9c1ae6b3c259" begin
*(a::Reduce.RExpr,b::Basis{V}) where V = SBlade{V}(a,b)
*(a::Basis{V},b::Reduce.RExpr) where V = SBlade{V}(b,a)
*(a::Reduce.RExpr,b::MultiVector{T,V}) where {T,V} = MultiVector{promote_type(T,F),V}(broadcast(Reduce.Algebra.:*,Refa,b.v))
*(a::MultiVector{T,V},b::Reduce.RExpr) where {T,V} = MultiVector{promote_type(T,F),V}(broadcast(Reduce.Algebra.:*,a.v,b))
*(a::Reduce.RExpr,b::MultiGrade{V}) where V = MultiGrade{V}(broadcast(Reduce.Algebra.:*,a,b.v))
*(a::MultiGrade{V},b::Reduce.RExpr) where V = MultiGrade{V}(broadcast(Reduce.Algebra.:*,a.v,b))
*(a::Reduce.RExpr,b::MultiVector{T,V}) where {T,V} = MultiVector{promote_type(T,F),V}(broadcast(Reduce.Algebra.:*,Ref(a),b.v))
*(a::MultiVector{T,V},b::Reduce.RExpr) where {T,V} = MultiVector{promote_type(T,F),V}(broadcast(Reduce.Algebra.:*,a.v,Ref(b)))
*(a::Reduce.RExpr,b::MultiGrade{V}) where V = MultiGrade{V}(broadcast(Reduce.Algebra.:*,Ref(a),b.v))
*(a::MultiGrade{V},b::Reduce.RExpr) where V = MultiGrade{V}(broadcast(Reduce.Algebra.:*,a.v,Ref(b)))
(a::Reduce.RExpr,b::Reduce.RExpr) = Reduce.Algebra.:*(a,b)
(a::Reduce.RExpr,b::B) where B<:TensorTerm{V,G} where {V,G} = SBlade{V,G}(a,b)
(a::A,b::Reduce.RExpr) where A<:TensorTerm{V,G} where {V,G} = SBlade{V,G}(b,a)
Expand Down
31 changes: 18 additions & 13 deletions src/algebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ end
end
end

for M (:Signature,:DiagonalForm)
@eval @pure loworder(V::$M{N,M,S,D,O}) where {N,M,S,D,O} = O0 ? $M{N,M,S,D,O-1}() : V
end
@pure loworder(::SubManifold{N,M,S}) where {N,M,S} = SubManifold{N,loworder(M),S}()

function generate_mutators(M,F,set_val,SUB,MUL)
for (op,set) ((:add,:(+=)),(:set,:(=)))
sm = Symbol(op,:multi!)
Expand All @@ -105,7 +110,7 @@ function generate_mutators(M,F,set_val,SUB,MUL)
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)
if diffvars(V)0
!iszero(Z) && (TAny ? (return true) : (val *= getbasis(V,Z)))
!iszero(Z) && (TAny ? (return true) : (val *= getbasis(loworder(V),Z)))
count_ones(Q)+order(val)>diffmode(V) && (return false)
end
$s(m,val,(AB)|Q,Dimension{N}())
Expand All @@ -118,7 +123,7 @@ function generate_mutators(M,F,set_val,SUB,MUL)
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)
if diffvars(V)0
!iszero(Z) && (TAny ? (return true) : (val *= getbasis(V,Z)))
!iszero(Z) && (TAny ? (return true) : (val *= getbasis(loworder(V),Z)))
count_ones(Q)+order(val)>diffmode(V) && (return false)
end
$s(m,val,bas|Q,Dimension{N}())
Expand All @@ -142,7 +147,7 @@ function generate_mutators(M,F,set_val,SUB,MUL)
if !iszero(Z)
TAny && (return true)
_,_,Q,_ = symmetricmask(V,A,B)
v *= getbasis(V,Z)
v *= getbasis(loworder(V),Z)
count_ones(Q)+order(v)>diffmode(V) && (return false)
end
end
Expand Down Expand Up @@ -260,7 +265,7 @@ function mul(a::Basis{V},b::Basis{V},der=derive_mul(V,bits(a),bits(b),1,true)) w
pcc,bas,cc = (hasinf(V) && hasorigin(V)) ? conformal(A,B,V) : (false,AB,false)
d = getbasis(V,bas|Q)
out = (typeof(V)<:Signature || count_ones(A&B)==0) ? (parity(a,b)pcc ? SBlade{V}(-1,d) : d) : SBlade{V}((pcc ? -1 : 1)*parityinner(A,B,V),d)
diffvars(V)0 && !iszero(Z) && (out = SBlade{V}(getbasis(V,Z),out))
diffvars(V)0 && !iszero(Z) && (out = SBlade{V}(getbasis(loworder(V),Z),out))
return cc ? (v=value(out);out+SBlade{V}(hasinforigin(V,A,B) ? -(v) : v,getbasis(V,conformalmask(V)bits(d)))) : out
end

Expand Down Expand Up @@ -315,7 +320,7 @@ export ∧, ∨, ⊗
A,B,Q,Z = symmetricmask(V,ba,bb)
((count_ones(A&B)>0) || diffcheck(V,ba,bb) || iszero(derive_mul(V,ba,bb,1,true))) && (return g_zero(V))
d = getbasis(V,(AB)|Q)
diffvars(V)0 && !iszero(Z) && (d = SBlade{V}(getbasis(V,Z),d))
diffvars(V)0 && !iszero(Z) && (d = SBlade{V}(getbasis(loworder(V),Z),d))
return parity(a,b) ? SBlade{V}(-1,d) : d
end

Expand All @@ -326,7 +331,7 @@ function ∧(a::X,b::Y) where {X<:TensorTerm{V},Y<:TensorTerm{V}} where V
((count_ones(A&B)>0) || diffcheck(V,ba,bb)) && (return g_zero(V))
v = derive_mul(V,ba,bb,value(a),value(b),*)
if diffvars(V)0 && !iszero(Z)
v=typeof(v)<:TensorMixed ? SBlade{V}(getbasis(V,Z),v) : SBlade{V}(v,getbasis(V,Z))
v=typeof(v)<:TensorMixed ? SBlade{V}(getbasis(V,Z),v) : SBlade{V}(v,getbasis(loworder(V),Z))
count_ones(Q)+order(v)>diffmode(V) && (return zero(V))
end
return SBlade{V}(parity(x,y) ? -v : v,getbasis(V,(AB)|Q))
Expand All @@ -353,7 +358,7 @@ Exterior product as defined by the anti-symmetric quotient Λ≡⊗/~
p,C,t,Z = regressive(a,b)
(!t || iszero(derive_mul(V,bits(a),bits(b),1,true))) && (return g_zero(V))
d = getbasis(V,C)
diffvars(V)0 && !iszero(Z) && (d = SBlade{V}(getbasis(V,Z),d))
diffvars(V)0 && !iszero(Z) && (d = SBlade{V}(getbasis(loworder(V),Z),d))
return p ? SBlade{V}(-1,d) : d
end

Expand All @@ -364,7 +369,7 @@ function ∨(a::X,b::Y) where {X<:TensorTerm{V},Y<:TensorTerm{V}} where V
v = derive_mul(V,ba,bb,value(a),value(b),*)
if diffvars(V)0 && !iszero(Z)
_,_,Q,_ = symmetricmask(V,bits(basis(a)),bits(basis(b)))
v=typeof(v)<:TensorMixed ? SBlade{V}(getbasis(V,Z),v) : SBlade{V}(v,getbasis(V,Z))
v=typeof(v)<:TensorMixed ? SBlade{V}(getbasis(V,Z),v) : SBlade{V}(v,getbasis(loworder(V),Z))
count_ones(Q)+order(v)>diffmode(V) && (return zero(V))
end
return SBlade{V}(p ? -v : v,getbasis(V,C))
Expand Down Expand Up @@ -400,7 +405,7 @@ Interior (right) contraction product: ω⋅η = ω∨⋆η
g,C,t,Z = interior(a,b)
(!t || iszero(derive_mul(V,bits(a),bits(b),1,true))) && (return g_zero(V))
d = getbasis(V,C)
diffvars(V)0 && !iszero(Z) && (d = SBlade{V}(getbasis(V,Z),d))
diffvars(V)0 && !iszero(Z) && (d = SBlade{V}(getbasis(loworder(V),Z),d))
return typeof(V) <: Signature ? (g ? SBlade{V}(-1,d) : d) : SBlade{V}(g,d)
end

Expand All @@ -411,7 +416,7 @@ function contraction(a::X,b::Y) where {X<:TensorTerm{V},Y<:TensorTerm{V}} where
v = derive_mul(V,ba,bb,value(a),value(b),*)
if diffvars(V)0 && !iszero(Z)
_,_,Q,_ = symmetricmask(V,bits(basis(a)),bits(basis(b)))
v=typeof(v)<:TensorMixed ? SBlade{V}(getbasis(V,Z),v) : SBlade{V}(v,getbasis(V,Z))
v=typeof(v)<:TensorMixed ? SBlade{V}(getbasis(V,Z),v) : SBlade{V}(v,getbasis(loworder(V),Z))
count_ones(Q)+order(v)>diffmode(V) && (return zero(V))
end
return SBlade{V}(typeof(V) <: Signature ? (g ? -v : v) : g*v,getbasis(V,C))
Expand Down Expand Up @@ -450,7 +455,7 @@ cross(a::TensorAlgebra{V},b::TensorAlgebra{V}) where V = ⋆(a∧b)
p,C,t,Z = crossprod(a,b)
(!t || iszero(derive_mul(V,bits(a),bits(b),1,true))) && (return zero(V))
d = getbasis(V,C)
diffvars(V)0 && !iszero(Z) && (d = SBlade{V}(getbasis(V,Z),d))
diffvars(V)0 && !iszero(Z) && (d = SBlade{V}(getbasis(loworder(V),Z),d))
return p ? SBlade{V}(-1,d) : d
end

Expand All @@ -461,7 +466,7 @@ function cross(a::X,b::Y) where {X<:TensorTerm{V},Y<:TensorTerm{V}} where V
v = derive_mul(V,ba,bb,value(a),value(b),*)
if diffvars(V)0 && !iszero(Z)
_,_,Q,_ = symmetricmask(V,bits(basis(a)),bits(basis(b)))
v=typeof(v)<:TensorMixed ? SBlade{V}(getbasis(V,Z),v) : SBlade{V}(v,getbasis(V,Z))
v=typeof(v)<:TensorMixed ? SBlade{V}(getbasis(V,Z),v) : SBlade{V}(v,getbasis(loworder(V),Z))
count_ones(Q)+order(v)>diffmode(V) && (return zero(V))
end
return SBlade{V}(p ? -v : v,getbasis(V,C))
Expand Down Expand Up @@ -877,7 +882,7 @@ function generate_products(Field=Field,VEC=:mvec,MUL=:*,ADD=:+,SUB=:-,CONJ=:conj
end
end
end
return MultiVector{t,V,2^N}(out)::MultiVector{t,V,2^N}
return MultiVector{t,V}(out)
end
function $op(a::MultiVector{T,V},b::MultiVector{S,V}) where {V,T<:$Field,S<:$Field}
$(insert_expr((:N,:t,:out,:bs,:bn,),VEC)...)
Expand Down
5 changes: 3 additions & 2 deletions src/multivectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -594,8 +594,9 @@ adjoint(b::MultiGrade{V,G}) where {V,G} = MultiGrade{dual(V),G}(adjoint.(terms(b

## conversions

@inline (V::Signature)(s::UniformScaling{T}) where T = SBlade{V}(T<:Bool ? (s.λ ? one(Int) : -(one(Int))) : s.λ,getbasis(V,(one(T)<<(ndims(V)-diffvars(V)))-1))
@inline (V::DiagonalForm)(s::UniformScaling{T}) where T = SBlade{V}(T<:Bool ? (s.λ ? one(Int) : -(one(Int))) : s.λ,getbasis(V,(one(T)<<(ndims(V)-diffvars(V)))-1))
for M (:Signature,:DiagonalForm)
@eval @inline (V::$M)(s::UniformScaling{T}) where T = SBlade{V}(T<:Bool ? (s.λ ? one(Int) : -(one(Int))) : s.λ,getbasis(V,(one(T)<<(ndims(V)-diffvars(V)))-1))
end

@pure function (W::Signature)(b::Basis{V}) where V
V==W && (return b)
Expand Down

2 comments on commit 39c3234

@chakravala
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request updated: JuliaRegistries/General/4124

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if Julia TagBot is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.1 -m "<description of version>" 39c323460aa959c2c306b6bf3db141866057cb45
git push origin v0.3.1

Please sign in to comment.