Skip to content

Commit

Permalink
improved ∇, ∂, subcomplex graphs, and symbolic compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
chakravala committed Oct 18, 2019
1 parent 5a2f3c3 commit 9f14a5a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 19 deletions.
55 changes: 40 additions & 15 deletions src/Grassmann.jl
Original file line number Diff line number Diff line change
Expand Up @@ -415,11 +415,9 @@ export ∇, Δ, ∂, d, ↑, ↓

generate_products(:(Leibniz.Operator),:svec)

@pure function (W::Signature{N})(d::Leibniz.Derivation{T,O}) where {N,T,O}
O < 1 && (return SChain{Int,W,1}(ones(Int,grade(W))))
C = mixedmode(W)<0
V = diffvars(W)0 ? W : tangent(W,O,C ? Int(ndims(W)/2) : ndims(W))
G,D = grade(V),diffvars(V)==1
@pure function (V::Signature{N})(d::Leibniz.Derivation{T,O}) where {N,T,O}
(O<1||diffvars(V)==0) && (return SChain{Int,V,1}(ones(Int,ndims(V))))
G,D,C = grade(V),diffvars(V)==1,mixedmode(V)<0
G2 = (C ? Int(G/2) : G)-1
= sum([getbasis(V,1<<(D ? G : k+G))*getbasis(V,1<<k) for k 0:G2])
isone(O) && (return ∇)
Expand All @@ -443,6 +441,16 @@ function ↑(ω::T) where T<:TensorAlgebra{V} where V
(hasinf(PV) ? G.v∞ : G.v∅)*(ω2-1)*iω2 + 2*iω2*ω
end
end
function (ω,b)
ω2 = ω^2
iω2 = inv(ω2+1)
2*iω2*ω + (ω2-1)*iω2*b
end
function (ω,p,m)
ω2 = ω^2
iω2 = inv(ω2+1)
2*iω2*ω + (ω2-1)*iω2*p + (ω2+1)*iω2*m
end

function ::T) where T<:TensorAlgebra{V} where V
PV = (V)
Expand All @@ -454,10 +462,12 @@ function ↓(ω::T) where T<:TensorAlgebra{V} where V
((ωb)*b)/(1-bω)
end
end
(ω,b) = ((bω)*b)/(1-ωb)
(ω,∞,∅) = (m=∅;inv(m)*(mω)/(-ω∞))

## skeleton / subcomplex

export skeleton, 𝒫, collapse, subcomplex, chain
export skeleton, 𝒫, collapse, subcomplex, chain, path

absym(t) = abs(t)
absym(t::Basis) = t
Expand All @@ -468,18 +478,21 @@ absym(t::MultiVector{T,V}) where {T,V} = MultiVector{T,V}(absym.(value(t)))

collapse(a,b) = aabsym((b))

function chain(t::T) where T<:TensorTerm{V} where V
N,B = ndims(V),bits(basis(t))
function chain(t::S,::Val{T}=Val{true}()) where S<:TensorTerm{V} where {V,T}
N,B,v = ndims(V),bits(basis(t)),value(t)
C = symmetricmask(V,B,B)[1]
G = count_ones(C)
G < 2 && (return t)
out,ind = zeros(mvec(N,2,Int)), indices(C,N)
setblade!(out,G==2 ? 1 : -1,bit2int(indexbits(N,[ind[1],ind[end]])),Dimension{N}())
if T || G == 2
setblade!(out,G==2 ? v : -v,bit2int(indexbits(N,[ind[1],ind[end]])),Dimension{N}())
end
for k 2:G
setblade!(out,1,bit2int(indexbits(N,ind[[k-1,k]])),Dimension{N}())
setblade!(out,v,bit2int(indexbits(N,ind[[k-1,k]])),Dimension{N}())
end
return MChain{Int,V,2}(out)
end
path(t) = chain(t,Val{false}())

𝒫(t::T) where T<:TensorAlgebra = skeleton(t,Val{false}())
skeleton(x::S,v=Val{true}()) where S<:TensorAlgebra = absym(x) + subcomplex(absym((x)),v)
Expand Down Expand Up @@ -537,13 +550,10 @@ function __init__()
@require LightGraphs="093fc24a-ae57-5d10-9952-331d41423f4d" begin
function LightGraphs.SimpleDiGraph(x::T,g=LightGraphs.SimpleDiGraph(grade(V))) where T<:TensorTerm{V} where V
ind = (signbit(value(x)) ? reverse : identity)(indices(basis(x)))
grade(x) == 2 ? LightGraphs.add_edge!(g,ind...) : graph((x),g)
grade(x) == 2 ? LightGraphs.add_edge!(g,ind...) : LightGraphs.SimpleDiGraph((x),g)
return g
end
function LightGraphs.SimpleDiGraph(x::S,g=LightGraphs.SimpleDiGraph(grade(V))) where {S<:TensorMixed{T,V} where T} where V
graph((x),g)
end
function graph(x::S,g=LightGraphs.SimpleDiGraph(grade(V))) where {S<:TensorMixed{T,V} where T} where V
N,G = ndims(V),grade(x)
ib = indexbasis(N,G)
for k 1:binomial(N,G)
Expand All @@ -554,7 +564,7 @@ function __init__()
end
return g
end
function graph(x::MultiVector{T,V} where T,g=LightGraphs.SimpleDiGraph(grade(V))) where V
function LightGraphs.SimpleDiGraph(x::MultiVector{T,V} where T,g=LightGraphs.SimpleDiGraph(grade(V))) where V
N = ndims(V)
for i 2:N
R = binomsum(N,i)
Expand All @@ -569,6 +579,21 @@ function __init__()
return g
end
end
#@require GraphPlot="a2cc645c-3eea-5389-862e-a155d0052231"
@require Compose="a81c6b42-2e10-5240-aca2-a61377ecd94b" begin
import LightGraphs, GraphPlot, Cairo
viewer = Base.Process(`$(haskey(ENV,"VIEWER") ? ENV["VIEWER"] : "xdg-open") simplex.pdf`,Ptr{Nothing}())
function Compose.draw(img,x::T,l=layout=GraphPlot.circular_layout) where T<:TensorAlgebra
Compose.draw(img,GraphPlot.gplot(LightGraphs.SimpleDiGraph(x),layout=l,nodelabel=collect(1:grade(vectorspace(x)))))
end
function graph(x,n="simplex.pdf",l=GraphPlot.circular_layout)
cmd = `$(haskey(ENV,"VIEWER") ? ENV["VIEWER"] : "xdg-open") $n`
global viewer
viewer.cmd == cmd && kill(viewer)
Compose.draw(Compose.PDF(n,16Compose.cm,16Compose.cm),x,l)
viewer = run(cmd,(devnull,stdout,stderr),wait=false)
end
end
@require GeometryTypes="4d00f742-c7ba-57c2-abde-4428a4b178cb" begin
Base.convert(::Type{GeometryTypes.Point},t::T) where T<:TensorTerm{V} where V = GeometryTypes.Point(value(SChain{valuetype(t),V}(vector(t))))
Base.convert(::Type{GeometryTypes.Point},t::T) where T<:TensorTerm{V,0} where V = GeometryTypes.Point(zeros(valuetype(t),ndims(V))...)
Expand Down
24 changes: 20 additions & 4 deletions src/algebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1601,14 +1601,25 @@ for (nv,d) ∈ ((:inv,:/),(:inv_rat,://))
end
throw(error("inv($m) is undefined"))
end
function $nv(m::MultiVector{Any,V}) where V
rm = ~m
d = rm*m
fd = $Sym.:∑([$Sym.:∏(a,a) for a value(d)]...)
sd = scalar(d)
$Sym.:∏(value(sd),value(sd)) == fd && (return $d(rm,sd))
for k 1:ndims(V)
@inbounds $Sym.:∑([$Sym.:∏(a,a) for a value(d[k])]...) == fd && (return $d(rm,d(k)))
end
throw(error("inv($m) is undefined"))
end
end
for Blade MSB
@eval begin
function $nv(b::$Blade{V,G,B,T}) where {V,G,B,T}
$Blade{V,G,B}($d(parityreverse(grade(V,B)) ? -one(T) : one(T),value(abs2_inv(B))*value(b)))
$Blade{V,G,B}($d(parityreverse(grade(V,B)) ? -one(T) : one(T),value(abs2_inv(B)*value(b))))
end
function $nv(b::$Blade{V,G,B,Any}) where {V,G,B}
$Blade{V,G,B}($d(parityreverse(grade(V,B)) ? -1 : 1,value(abs2_inv(B))*value(b)))
$Blade{V,G,B}($Sym.$d(parityreverse(grade(V,B)) ? -1 : 1,value($Sym.:∏(abs2_inv(B),value(b)))))
end
end
end
Expand All @@ -1623,10 +1634,15 @@ for (nv,d) ∈ ((:inv,:/),(:inv_rat,://))
end
end

function generate_inverses(Sym,T)
function generate_inverses(Mod,T)
for (nv,d,ds) ((:inv,:/,:($Sym.:/)),(:inv_rat,://,:($Sym.://)))
for Term (:TensorTerm,MSC...,:MultiVector,:MultiGrade)
@eval $d(a::S,b::T) where {S<:$Term,T<:$Sym.$T} = a*$ds(1,b)
@eval $d(a::S,b::T) where {S<:$Term,T<:$Mod.$T} = a*$ds(1,b)
end
for Blade MSB
@eval function $nv(b::$Blade{V,G,B,$Mod.$T}) where {V,G,B}
$Blade{V,G,B}($Mod.$d(parityreverse(grade(V,B)) ? -1 : 1,value($Sym.:∏(abs2_inv(B),value(b)))))
end
end
end
end
Expand Down

0 comments on commit 9f14a5a

Please sign in to comment.