Skip to content

Commit

Permalink
made signature utilities robust
Browse files Browse the repository at this point in the history
  • Loading branch information
chakravala committed May 21, 2024
1 parent d2268bb commit e20872f
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "DirectSum"
uuid = "22fd7b30-a8c0-5bf2-aabe-97783860d07c"
authors = ["Michael Reed"]
version = "0.8.11"
version = "0.8.12"

[deps]
ComputedFieldTypes = "459fdd68-db75-56b8-8c15-d717a790f88e"
Expand Down
39 changes: 30 additions & 9 deletions src/DirectSum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module DirectSum
export TensorBundle, Signature, DiagonalForm, Manifold, Submanifold, ℝ, , mdims
import Base: getindex, convert, @pure, +, *, , , , , ==
import LinearAlgebra, AbstractTensors
import LinearAlgebra: det, rank
import LinearAlgebra: det, rank, isdiag
using Leibniz, ComputedFieldTypes

## Manifold{N}
Expand All @@ -36,7 +36,7 @@ import Leibniz: printlabel, supermanifold, shift_indices, shift_indices!, printi
import Leibniz: symmetricmask, parityleft, parityright, paritylefthodge, combine
import Leibniz: parityrighthodge, parityclifford, parityconj, parityreverse, parityinvolute
import Leibniz: parityrightnull, parityleftnull, parityrightnullpre, parityleftnullpre
import Leibniz: hasconformal, parval, TensorTerm, mixed, subs, sups, vio
import Leibniz: hasconformal, parval, TensorTerm, mixed, subs, sups, vio, gdims

import Leibniz: grade, order, options, metric, polymode, dyadmode, diffmode, diffvars
import Leibniz: pseudograde, hasinf, hasorigin, norm, indices, isbasis, Bits, bits,
Expand Down Expand Up @@ -214,6 +214,7 @@ end
#@pure Submanifold{M}() where M = Submanifold{M isa Int ? Submanifold(M) : M,rank(M)}()
@pure Submanifold{V,N}() where {V,N} = Submanifold{V,N}(UInt(1)<<N-1)
@pure Submanifold{M,N}(b::UInt) where {M,N} = Submanifold{M,N,b}()
@pure Submanifold{M,N}(b::Submanifold{M,N}) where {M,N} = b
Submanifold{M,N}(b::Values{N}) where {M,N} = Submanifold{M,N}(bit2int(indexbits(mdims(M),b)))
Submanifold{M}(b::UnitRange) where M = Submanifold{M,length(b)}(Values(b...))
Submanifold{M}(b::Vector) where M = Submanifold{M,length(b)}(Values(b...))
Expand Down Expand Up @@ -243,17 +244,27 @@ for t ∈ (Any,Integer)
elseif typeof(M)<:Int
1
else
val = M[indices(S)[i]]
typeof(M)<:Signature ? (val ? -1 : 1) : val
ind = indices(S)
val = M[ind[i]]
if typeof(M)<:Signature
val ? -1 : 1
else
typeof(val)<:Values ? val[Values(ind...)] : val
end
end
end
end
@inline getindex(vs::Submanifold,i::Vector) = [getindex(vs,j) for j i]
@inline getindex(vs::Submanifold,i::UnitRange{Int}) = [getindex(vs,j) for j i]
@inline function getindex(::Submanifold{M,N,S} where N,i::Colon) where {M,S}
typeof(M)<:Int && (return ones(Int,M))
val = M[indices(S)]
typeof(M)<:Signature ? [v ? -1 : 1 for v val] : val
ind = indices(S)
val = M[ind]
if typeof(M)<:Signature
[v ? -1 : 1 for v val]
else
eltype(val) <: Values ? getindex.(val,Ref(Values(ind...))) : val
end
end

function Base.iterate(r::Submanifold, i::Int=1)
Expand Down Expand Up @@ -281,7 +292,13 @@ function Base.show(io::IO,s::Submanifold{V,NN,S}) where {V,NN,S}
hasorigin(s) && print(io,vio[2])
ind = indices(S)
for k hasinf(s)+hasorigin(s)+1+(d<0 ? abs(d) : 0):NM
print(io,k ind ? sig(M,k) : '_')
met = if k ind
metr = sig(s,findfirst(x->x==k,ind))
typeof(metr)==Bool ? metr ? '-' : '+' : metr
else
'_'
end
print(io,met)
printsep(io,M,k,NM)
end
d>0 && print(io,[((C>0)!polymode(s) ? sups : subs)[x-NM] for x ind[N+1:N+abs(d)]]...)
Expand Down Expand Up @@ -393,6 +410,7 @@ Single(v::Complex) = Single{Submanifold(0)}(v)
@pure Single{V}(b::Submanifold{V,G}) where {V,G} = Single{V,G,b,Int}(1)
Single{V}(v::T) where {V,T} = Single{V,0,Submanifold{V}(),T}(v)
Single{V}(v::S) where S<:TensorTerm where V = v
Single{V}(v::Tuple{UInt,T}) where {V,T} = @inbounds Single{V}(v[2],Submanifold{V}(v[1]))
Single{V,G,B}(v::T) where {V,G,B,T} = Single{V,G,B,T}(v)
Single(v,b::S) where S<:TensorTerm{V} where V = Single{V}(v,b)
Single{V}(v,b::S) where S<:TensorAlgebra where V = v*b
Expand Down Expand Up @@ -455,7 +473,10 @@ end

for M (:Signature,:DiagonalForm,:Submanifold)
@eval begin
@inline (V::$M)(s::LinearAlgebra.UniformScaling{T}) where T = Single{V}(T<:Bool ? (s.λ ? 1 : -1) : s.λ,getbasis(V,(one(UInt)<<(mdims(V)-diffvars(V)))-1))
@inline function (V::$M)(s::LinearAlgebra.UniformScaling{T}) where T
b = getbasis(V,(one(UInt)<<(mdims(V)-diffvars(V)))-1)
T<:Bool ? b : Single{V}(s.λ,b)
end
(W::$M)(b::Single) = Single{W}(value(b),W(basis(b)))
==(::Type{<:$M}, ::Type{Union{}}) = false
end
Expand Down Expand Up @@ -607,6 +628,6 @@ end
include("generic.jl")
include("operations.jl")
include("basis.jl")
#include("e.jl")
include("grade.jl")

end # module
12 changes: 8 additions & 4 deletions src/basis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,9 @@ end=#
const Λ0 = Λ{Submanifold(0)}()
const Λ0S = Λ{ℝ0}()

for V (:Int,:Signature,:DiagonalForm)
for V (:Int,:Signature,:DiagonalForm,:MetricTensor)
@eval const $(Symbol(:algebra_cache_,V)) = Vector{Vector{Dict{UInt,Vector{Dict{UInt,Λ}}}}}[]
@eval @pure getalgebra(V::$V) = getalgebra(Submanifold(V))
V:MetricTensor && @eval @pure getalgebra(V::$V) = getalgebra(Submanifold(V))
end
@eval begin
@pure function getalgebra(n::Int,m::Int,s,S::UInt,vs::Type,f::Int=0,d::Int=0)
Expand All @@ -239,6 +239,8 @@ end
algebra_cache_Signature
elseif vs <: DiagonalForm
algebra_cache_DiagonalForm
else
algebra_cache_MetricTensor
end
for F length(alc)+1:f1
push!(alc,Vector{Dict{UInt,Vector{Dict{UInt,Λ}}}}[])
Expand Down Expand Up @@ -379,9 +381,9 @@ for (ExtraBasis,extra) ∈ ((SparseBasis,:sparse),(ExtendedBasis,:extended))
getextra = Symbol(:get,extra)
getalg = Symbol(getextra,:_Signature)
extra_cache = Symbol(extra,:_cache)
for V (:Int,:Signature,:DiagonalForm)
for V (:Int,:Signature,:DiagonalForm,:MetricTensor)
@eval const $(Symbol(extra_cache,:_,V)) = Vector{Vector{Dict{UInt,Vector{Dict{UInt,$ExtraBasis}}}}}[]
V:Int && (@eval @pure $getextra(V::$V) = $getextra(Submanifold(V)))
V(:Int,:MetricTensor) && (@eval @pure $getextra(V::$V) = $getextra(Submanifold(V)))
end
@eval begin
@pure function $getextra(n::Int,m::Int,s,S::UInt,vs,f::Int=0,d::Int=0)
Expand All @@ -393,6 +395,8 @@ for (ExtraBasis,extra) ∈ ((SparseBasis,:sparse),(ExtendedBasis,:extended))
$(Symbol(extra_cache,:_Signature))
elseif vs <: DiagonalForm
$(Symbol(extra_cache,:_DiagonalForm))
else
$(Symbol(extra_cache,:_MetricTensor))
end
for F length(exc)+1:f1
push!(exc,Vector{Dict{UInt,Vector{Dict{UInt,$ExtraBasis}}}}[])
Expand Down
4 changes: 4 additions & 0 deletions src/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ end

export isdyadic, isdual, istangent

@pure LinearAlgebra.isdiag(V::Signature) = !hasconformal(V)
@pure LinearAlgebra.isdiag(::DiagonalForm) = true
@pure LinearAlgebra.isdiag(::Submanifold{V}) where V = isdiag(V)

@inline value(x::M,T=Int) where M<:TensorBundle = T==Any ? 1 : one(T)
@inline value(::Submanifold,T=Int) = T==Any ? 1 : one(T)
@inline value(m::Single,T::DataType=valuetype(m)) = T(valuetype(m),Any) ? convert(T,m.v) : m.v
Expand Down
42 changes: 42 additions & 0 deletions src/grade.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

export grade, gdims, Grade

struct Grade{N,G} <: Integer
@pure Grade{N,G}() where {N,G} = new{N,G}()
@pure Grade{N}(G::Int) where N = new{N,G}()
@pure Grade(N::Int,G::Int) = new{N,G}()
end

Base.show(io::IO,::Grade{N,G}) where {N,G} = print(io,"Λ$G")

Base.convert(::Type{Int},::Grade{N,G}) where {N,G} = G
Base.convert(::Type{UInt},::Grade{N,G}) where {N,G} = UInt(G)

@pure mdims(::Grade{N}) where N = N
@pure tdims(::Grade{N}) where N = 1<<N
@pure grade(::Grade{N,G}) where {N,G} = G
@pure grades(t::TensorGraded) = Values{1}(Grade(mdims(t),grade(t)))

for fun (:gdims,:combo,:binomsum,:spinsum,:antisum,:indexbasis)
@eval begin
@pure $fun(::Grade{N,G}) where {N,G} = $fun(N,G)
@pure $fun(::Grade{N,N},::Grade{N,G}) where {N,G} = $fun(N,G)
end
end
for fun (:binomial_set,:binomsum_set,:spinsum_set,:antisum_set,:indexbasis_set,:indexeven_set,:indexodd_set,:indexbasis,:indexeven,:indexodd)
@eval @pure $fun(::Grade{N,N}) where N = $fun(N)
end

Base.:+(::Grade{N,G},::Grade{N,F}) where {N,G,F} = Grade{N,G+F}()
Base.:-(::Grade{N,G},::Grade{N,F}) where {N,G,F} = Grade{N,G-F}()

Base.:+(::Grade{N,G},F::Int) where {N,G} = G+F
Base.:+(F::Int,::Grade{N,G}) where {N,G} = F+G
Base.:-(::Grade{N,G},F::Int) where {N,G} = G-F
Base.:-(F::Int,::Grade{N,G}) where {N,G} = F-G

Base.:(==)(::Grade,::Grade) = false
Base.:(==)(::Grade{N,G},::Grade{N,G}) where {N,G} = true
Base.:(==)(::Grade{N,G},F::Int) where {N,G} = G==F
Base.:(==)(F::Int,::Grade{N,G}) where {N,G} = F==G

23 changes: 18 additions & 5 deletions src/operations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ end
@inbounds (bi[1], bi[2]>M ? bi[2]-M : bi[2])
end

@pure (W::Submanifold{Q,M})(b::Zero{V}) where {Q,M,V} = Zero(W)
@pure function (W::Submanifold{Q,M})(b::Submanifold{V,G,R}) where {Q,M,V,G,R}
if isbasis(W) && !isbasis(b)
RS = R&UInt(W)
Expand Down Expand Up @@ -297,7 +298,8 @@ for side ∈ (:left,:right)
end
@pure function $pg(V::$Q,B,G=count_ones(B))
ind = indices(B&(UInt(1)<<(mdims(V)-diffvars(V))-1),mdims(V))
g,c = prod(V[ind]), hasconformal(V) && (B&UInt(3) == UInt(2))
gg,c = V[ind], hasconformal(V) && (B&UInt(3) == UInt(2))
g = isempty(gg) ? 1 : prod(signbool.(gg))
$p(0,sum(ind),G,mdims(V)-diffvars(V))c ? -(g) : g
end
end
Expand All @@ -310,7 +312,8 @@ end
for Q (:DiagonalForm,:Submanifold)
@eval begin
@pure function paritymetric(V::$Q,B,G=count_ones(B))
prod(V[indices(B&(UInt(1)<<(mdims(V)-diffvars(V))-1),mdims(V))])
g = V[indices(B&(UInt(1)<<(mdims(V)-diffvars(V))-1),mdims(V))]
isempty(g) ? 1 : prod(signbool.(g))
end
@pure function parityanti(V::$Q,B)
paritymetric(V,complement(mdims(V),B,diffvars(V),hasinf(V)+hasorigin(V)))
Expand All @@ -330,16 +333,20 @@ export complementleftanti, complementrightanti
@inline complementrightanti(t) = complementright(antimetric(t))
@inline complementleftanti(t) = complementleft(antimetric(t))

signbool(t::Bool) = t ? -1 : 1
signbool(t) = t

for side (:left,:right)
s,p = Symbol(:complement,side),Symbol(:parity,side)
h,pg,pn = Symbol(s,:hodge),Symbol(p,:hodge),Symbol(p,:null)
for (c,p) ((s,p),(h,pg))
@eval begin
@pure function $c(b::Submanifold{V,G,B}) where {V,G,B}
$(ch ? nothing : side:right ? :((!isdiag(V) && !hasconformal(V)) && (return $s(metric(b)))) : :((!isdiag(V) && !hasconformal(V)) && (return reverse(b)*V(LinearAlgebra.I))) )
d = getbasis(V,complement(mdims(V),B,diffvars(V),$(ch ? 0 : :(hasinf(V)+hasorigin(V)))))
isdyadic(V) && throw(error("Complement for mixed tensors is undefined"))
v = $(ch ? :($pn(V,B,value(d))) : :(value(d)))
typeof(V)<:Signature ? ($p(b) ? Single{V}(-v,d) : isone(v) ? d : Single{V}(v,d)) : Single{V}($p(b)*v,d)
typeof(V)<:Signature ? ($p(b) ? Single{V}(-v,d) : isone(v) ? d : Single{V}(v,d)) : Single{V}(signbool($p(b))*v,d)
end
$c(b::Single) = conj(value(b))*$c(basis(b))
end
Expand All @@ -349,15 +356,21 @@ end
@eval begin
@pure function metric(b::Submanifold{V,G,B}) where {V,G,B}
!isbasis(b) && (return metric(V))
(!isdiag(V) || hasconformal(V)) && (return complementleft(complementrighthodge(b)))
isdyadic(V) && throw(error("Complement for mixed tensors is undefined"))
hasorigin(b) && !hasinf(b) && (return Zero(V))
hasinf(b) && !hasorigin(b) && (return Zero(V))
p = paritymetric(b)
typeof(V)<:Signature ? (p ? -b : b ) : Single{V}(p,b)
typeof(p)==Bool ? (p ? -b : b ) : Single{V}(p,b)
end
metric(b::Single) = value(b)*metric(basis(b))
@pure function antimetric(b::Submanifold{V,G,B}) where {V,G,B}
(!isdiag(V) || hasconformal(V)) && (return antimetric_term(b))
isdyadic(V) && throw(error("Complement for mixed tensors is undefined"))
hasorigin(b) && !hasinf(b) && (return Zero(V))
hasinf(b) && !hasorigin(b) && (return Zero(V))
p = parityanti(b)
typeof(V)<:Signature ? (p ? -b : b ) : Single{V}(p,b)
typeof(p)==Bool ? (p ? -b : b ) : Single{V}(p,b)
end
antimetric(b::Single) = value(b)*antimetric(basis(b))
end
Expand Down

2 comments on commit e20872f

@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 created: JuliaRegistries/General/107347

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

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 the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.8.12 -m "<description of version>" e20872f465ffeecbe0b3e770f477a6196b4c892c
git push origin v0.8.12

Please sign in to comment.