Skip to content

Commit

Permalink
improved robustness of algebra
Browse files Browse the repository at this point in the history
  • Loading branch information
chakravala committed May 21, 2024
1 parent 5a4c30a commit 2523f25
Show file tree
Hide file tree
Showing 9 changed files with 579 additions and 127 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
[compat]
julia = "1"
Leibniz = "0.2"
DirectSum = "0.8.1"
DirectSum = "0.8.12"
AbstractTensors = "0.8"
ComputedFieldTypes = "1"
Requires = "1"
Expand Down
4 changes: 2 additions & 2 deletions src/Grassmann.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ import AbstractTensors: Values, Variables, FixedVector, clifford, hodge, wedge,
export , ℝ, @V_str, @S_str, @D_str, Manifold, Submanifold, Signature, DiagonalForm, value
export @basis, @basis_str, @dualbasis, @dualbasis_str, @mixedbasis, @mixedbasis_str, Λ
export ℝ0, ℝ1, ℝ2, ℝ3, ℝ4, ℝ5, ℝ6, ℝ7, ℝ8, ℝ9, mdims, tangent, metric, antimetric
export hodge, wedge, vee, complement, dot, antidot, istangent
export hodge, wedge, vee, complement, dot, antidot, istangent, Values

import Base: @pure, ==, isapprox
import Base: print, show, getindex, setindex!, promote_rule, convert, adjoint
import DirectSum: V0, , generate, basis, getalgebra, getbasis, dual, Zero, One, Zero, One
import Leibniz: hasinf, hasorigin, dyadmode, value, pre, vsn, metric, mdims, gdims
import Leibniz: bit2int, indexbits, indices, diffvars, diffmask
import Leibniz: symmetricmask, indexstring, indexsymbol, combo, digits_fast
import DirectSum: antimetric
import DirectSum: antimetric, signbool

import Leibniz: hasconformal, hasinf2origin, hasorigin2inf
import AbstractTensors: valuetype, scalar, isscalar, trivector, istrivector, , complement
Expand Down
73 changes: 50 additions & 23 deletions src/algebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,33 @@ Geometric algebraic product: ω⊖η = (-1)ᵖdet(ω∩η)⊗(Λ(ω⊖η)∪L(ω
*(a::X,b::Y,c::Z...) where {X<:TensorAlgebra,Y<:TensorAlgebra,Z<:TensorAlgebra} = *(a*b,c...)

@pure function mul(a::Submanifold{V},b::Submanifold{V},der=derive_mul(V,UInt(a),UInt(b),1,true)) where V
ba,bb = UInt(a),UInt(b)
(diffcheck(V,ba,bb) || iszero(der)) && (return Zero(V))
A,B,Q,Z = symmetricmask(V,ba,bb)
pcc,bas,cc = (hasinf(V) && hasorigin(V)) ? conformal(V,A,B) : (false,AB,false)
d = getbasis(V,bas|Q)
out = (typeof(V)<:Signature || count_ones(A&B)==0) ? (parity(a,b)pcc ? Single{V}(-1,d) : d) : Single{V}((pcc ? -1 : 1)*parityinner(V,A,B),d)
diffvars(V)0 && !iszero(Z) && (out = Single{V}(getbasis(loworder(V),Z),out))
return cc ? (v=value(out);out+Single{V}(hasinforigin(V,A,B) ? -(v) : v,getbasis(V,conformalmask(V)UInt(d)))) : out
if isdiag(V)
ba,bb = UInt(a),UInt(b)
(diffcheck(V,ba,bb) || iszero(der)) && (return Zero(V))
A,B,Q,Z = symmetricmask(V,ba,bb)
d = getbasis(V,(AB)|Q)
out = if typeof(V)<:Signature || count_ones(A&B)==0
(parity(a,b) ? Single{V}(-1,d) : d)
else
Single{V}(signbool(parityinner(V,A,B)),d)
end
diffvars(V)0 && !iszero(Z) && (out = Single{V}(getbasis(loworder(V),Z),out))
return out
else
out = paritygeometric(V,UInt(a),UInt(b))
isempty(out) ? Zero(V) : +(Single{V}.(out)...)
end
end

function (a::Single{V},b::Submanifold{V}) where V
v = derive_mul(V,UInt(basis(a)),UInt(b),a.v,true)
bas = mul(basis(a),b,v)
order(a.v)+order(bas)>diffmode(V) ? Zero(V) : Single{V}(v,bas)
order(a.v)+order(bas)>diffmode(V) ? Zero(V) : v*bas
end
function (a::Submanifold{V},b::Single{V}) where V
v = derive_mul(V,UInt(a),UInt(basis(b)),b.v,false)
bas = mul(a,basis(b),v)
order(b.v)+order(bas)>diffmode(V) ? Zero(V) : Single{V}(v,bas)
order(b.v)+order(bas)>diffmode(V) ? Zero(V) : v*bas
end

export , ,
Expand Down Expand Up @@ -173,24 +181,43 @@ export ⋅
Interior (right) contraction product: ω⋅η = ω∨⋆η
"""
@pure function contraction(a::Submanifold{V},b::Submanifold{V}) where V
g,C,t,Z = interior(a,b)
(!t || iszero(derive_mul(V,UInt(a),UInt(b),1,true))) && (return Zero(V))
d = getbasis(V,C)
istangent(V) && !iszero(Z) && (d = Single{V}(getbasis(loworder(V),Z),d))
return isone(g) ? d : Single{V}(g,d)
iszero(derive_mul(V,UInt(a),UInt(b),1,true)) && (return Zero(V))
if isdiag(V) || hasconformal(V)
g,C,t,Z = interior(a,b)
(!t) && (return Zero(V))
d = getbasis(V,C)
istangent(V) && !iszero(Z) && (d = Single{V}(getbasis(loworder(V),Z),d))
return isone(g) ? d : Single{V}(g,d)
else
Cg,Z = parityinterior(V,UInt(a),UInt(b),Val(true))
#d = getbasis(V,C)
#istangent(V) && !iszero(Z) && (d = Single{V}(getbasis(loworder(V),Z),d))
return isempty(Cg) ? Zero(V) : +(Single{V}.(Cg)...)
end
end

function contraction(a::X,b::Y) where {X<:TensorTerm{V},Y<:TensorTerm{V}} where V
ba,bb = UInt(basis(a)),UInt(basis(b))
g,C,t,Z = interior(V,ba,bb)
!t && (return Zero(V))
v = derive_mul(V,ba,bb,value(a),value(b),AbstractTensors.dot)
if istangent(V) && !iszero(Z)
_,_,Q,_ = symmetricmask(V,ba,bb)
v = !(typeof(v)<:TensorTerm) ? Single{V}(v,getbasis(V,Z)) : Single{V}(v,getbasis(loworder(V),Z))
count_ones(Q)+order(v)>diffmode(V) && (return Zero(V))
if isdiag(V) || hasconformal(V)
g,C,t,Z = interior(V,ba,bb)
!t && (return Zero(V))
v = derive_mul(V,ba,bb,value(a),value(b),AbstractTensors.dot)
if istangent(V) && !iszero(Z)
_,_,Q,_ = symmetricmask(V,ba,bb)
v = !(typeof(v)<:TensorTerm) ? Single{V}(v,getbasis(V,Z)) : Single{V}(v,getbasis(loworder(V),Z))
count_ones(Q)+order(v)>diffmode(V) && (return Zero(V))
end
return Single{V}(g*v,getbasis(V,C))
else
Cg,Z = parityinterior(V,ba,bb,Val(true))
v = derive_mul(V,ba,bb,value(a),value(b),AbstractTensors.dot)
if istangent(V) && !iszero(Z)
_,_,Q,_ = symmetricmask(V,ba,bb)
v = !(typeof(v)<:TensorTerm) ? Single{V}(v,getbasis(V,Z)) : Single{V}(v,getbasis(loworder(V),Z))
count_ones(Q)+order(v)>diffmode(V) && (return Zero(V))
end
return isempty(Cg) ? Zero(V) : (+(Single{V}.(Cg)...))*v
end
return Single{V}(g*v,getbasis(V,C))
end

export ,
Expand Down
4 changes: 3 additions & 1 deletion src/composite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ end
function Base.exp(t::T) where T<:TensorGraded
S,B,V = T<:Submanifold,T<:TensorTerm,Manifold(t)
if B && isnull(t)
return One(V)
vt = valuetype(t)
return Couple{V,basis(t)}(one(vt),zero(vt))
elseif isR301(V) && grade(t)==2 # && abs(t[0])<1e-9 && !options.over
u = sqrt(abs(abs2(t)[1]))
u<1e-5 && (return One(V)+t)
Expand Down Expand Up @@ -804,6 +805,7 @@ end

DirectSum.Λ(x::Chain{V,1,<:Chain{V,1}},G) where V = compound(x,G)
compound(x,G::T) where T<:Integer = compound(x,Val(G))
compound(x::Chain{V,1,<:Chain{V,1}},::Val{0}) where V = Chain{V,0}(Values(Chain{V,0}(1)))
@generated function compound(x::Chain{V,1,<:Chain{V,1}},::Val{G}) where {V,G}
Expr(:call,:(Chain{V,G}),Expr(:call,:Values,[Expr(:call,:,[:(@inbounds x[$i]) for i indices(j)]...) for j indexbasis(mdims(V),G)]...))
end
Expand Down
128 changes: 122 additions & 6 deletions src/forms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -339,12 +339,12 @@ for pinor ∈ (:Spinor,:AntiSpinor)#,:Multivector)
end
end

display_matrix(m::Chain{V,G,<:TensorGraded{W,G}}) where {V,G,W} = vcat(transpose([V,chainbasis(V,G)...]),hcat(chainbasis(W,G),matrix(m)))
display_matrix(m::TensorGraded{V,G,<:Spinor{W}}) where {V,G,W} = vcat(transpose([V,evenbasis(V)...]),hcat(evenbasis(W),matrix(m)))
display_matrix(m::TensorGraded{V,G,<:AntiSpinor{W}}) where {V,G,W} = vcat(transpose([V,oddbasis(V)...]),hcat(oddbasis(W),matrix(m)))
display_matrix(m::TensorAlgebra{V,<:Multivector{W}}) where {V,W} = vcat(transpose([V,fullbasis(V)...]),hcat(fullbasis(W),matrix(m)))
display_matrix(m::Chain{V,G,<:TensorGraded{W,G}}) where {V,G,W} = vcat(transpose([Submanifold(V),chainbasis(V,G)...]),hcat(chainbasis(W,G),matrix(m)))
display_matrix(m::TensorGraded{V,G,<:Spinor{W}}) where {V,G,W} = vcat(transpose([Submanifold(V),evenbasis(V)...]),hcat(evenbasis(W),matrix(m)))
display_matrix(m::TensorGraded{V,G,<:AntiSpinor{W}}) where {V,G,W} = vcat(transpose([Submanifold(V),oddbasis(V)...]),hcat(oddbasis(W),matrix(m)))
display_matrix(m::TensorAlgebra{V,<:Multivector{W}}) where {V,W} = vcat(transpose([Submanifold(V),fullbasis(V)...]),hcat(fullbasis(W),matrix(m)))
for (pinor,bas) ((:Spinor,:evenbasis),(:AntiSpinor,:oddbasis),(:Multivector,:fullbasis))
@eval display_matrix(m::$pinor{V,<:TensorAlgebra{W}}) where {V,W} = vcat(transpose([V,$bas(V)...]),hcat($bas(W),matrix(m)))
@eval display_matrix(m::$pinor{V,<:TensorAlgebra{W}}) where {V,W} = vcat(transpose([Submanifold(V),$bas(V)...]),hcat($bas(W),matrix(m)))
end

export Projector, Dyadic, Proj
Expand Down Expand Up @@ -401,6 +401,15 @@ Chain(P::Dyadic{V}) where V = Chain{V}(P)

export TensorOperator, Endomorphism

#=struct TensorOperator{V,W,S<:TensorAlgebra{W},T<:TensorAlgebra{V,S}} <: TensorNested{V,S}
v::T
TensorOperator{V,W}(t::T) where {V,W,S<:TensorAlgebra{W},T<:TensorAlgebra{V,S}} = new{V,W,S,T}(t)
TensorOperator{V}(t::T) where {V,W,S<:TensorAlgebra{W},T<:TensorAlgebra{V,S}} = new{V,W,S,T}(t)
TensorOperator(t::T) where {V,W,S<:TensorAlgebra{W},T<:TensorAlgebra{V,S}} = new{V,W,S,T}(t)
end
Endomorphism{V,S<:TensorAlgebra{V},T<:TensorAlgebra{V,S}} = TensorOperator{V,V,S,T}=#

struct TensorOperator{V,W,T<:TensorAlgebra{V,<:TensorAlgebra{W}}} <: TensorNested{V,T}
v::T
TensorOperator{V,W}(t::T) where {V,W,T<:TensorAlgebra{V,<:TensorAlgebra{W}}} = new{V,W,T}(t)
Expand All @@ -412,6 +421,8 @@ Endomorphism{V,T<:TensorAlgebra{V,<:TensorAlgebra{V}}} = TensorOperator{V,V,T}

value(t::TensorOperator) = t.v
matrix(m::TensorOperator) = matrix(value(m))
compound(m::TensorOperator,g) = TensorOperator(compound(value(m),g))
compound(m::TensorOperator,g::Integer) = TensorOperator(compound(value(m),g))
getindex(t::TensorOperator,i::Int,j::Int) = value(value(t.v)[j])[i]

for op (:(Base.inv),)
Expand All @@ -432,6 +443,9 @@ function show(io::IO, ::MIME"text/plain", t::TensorOperator)
if isempty(X) && get(io, :compact, false)::Bool
return show(io, X)
end
show_matrix(io, t, X)
end
function show_matrix(io::IO, t, X)
# 0) show summary before setting :compact
summary(io, t)
isempty(X) && return
Expand Down Expand Up @@ -544,6 +558,15 @@ end
@inline @generated function matmul(x::Values{N,<:Multivector{V}},y::Values{N}) where {N,V}
Expr(:call,:Values,[Expr(:call,:+,[:(@inbounds y[$i]*value(x[$i])[$j]) for i list(1,N)]...) for j list(1,1<<mdims(V))]...)
end
@inline @generated function matmul(x::Values{N,<:Spinor{V}},y::Values{N}) where {N,V}
Expr(:call,:Values,[Expr(:call,:+,[:(@inbounds y[$i]*value(x[$i])[$j]) for i list(1,N)]...) for j list(1,1<<(mdims(V)-1))]...)
end
@inline @generated function matmul(x::Values{N,<:AntiSpinor{V}},y::Values{N}) where {N,V}
Expr(:call,:Values,[Expr(:call,:+,[:(@inbounds y[$i]*value(x[$i])[$j]) for i list(1,N)]...) for j list(1,1<<(mdims(V)-1))]...)
end

contraction(x::Spinor{W,<:Spinor{V},N},y::Spinor{V,T,N}) where {W,N,V,T} = Spinor{V}(matmul(value(x),value(y)))
contraction(x::AntiSpinor{W,<:AntiSpinor{V},N},y::AntiSpinor{V,T,N}) where {W,N,V,T} = AntiSpinor{V}(matmul(value(x),value(y)))

contraction(a::Dyadic{V,<:Chain{V,1,<:Chain},<:Chain{V,1,<:Chain}} where V,b::TensorGraded) = sum(value(a.x).⊗(value(a.y).⋅b))
contraction(a::Dyadic{V,<:Chain{V,1,<:Chain}} where V,b::TensorGraded) = sum(value(a.x).⊗(a.y.⋅b))
Expand Down Expand Up @@ -632,7 +655,7 @@ end
@pure fulldyad(V) = Multivector.(fullbasis(V))
@pure fullbasis(V) = Λ(V).b

@pure chaindyad(V,G) = Chain.(chainbasis(V,G))
@pure chaindyad(V,G=1) = Chain.(chainbasis(V,G))
@pure function chainbasis(V,G=1)
N = mdims(V)
r,b = binomsum(N,G),binomial(N,G)
Expand Down Expand Up @@ -661,3 +684,96 @@ gradedoperator(fun,V) = TensorOperator(Multivector{V}(fun.(Λ(V).b)))
end
evenoperator(t::TensorAlgebra{V}) where V = TensorOperator(Spinor{V}(evenbasis(V) .⊘ Ref(t)))
oddoperator(t::TensorAlgebra{V}) where V = TensorOperator(AntiSpinor{V}(oddbasis(V) .⊘ Ref(t)))

# metric tensor

antimetrictensor(V,G) = compound(metrictensor(V),grade(V)-G)
antimetrictensor(V,::Val{G}=Val(1)) where G = compound(metrictensor(V),Val(grade(V)-G))
metrictensor(V::TensorBundle) = TensorOperator(map(Chain,value(metricdyad(V))))
metrictensor(V::Int) = TensorOperator(map(Chain,value(metricdyad(V))))
metricdyad(V::TensorBundle) = metricdyad(Submanifold(V))
metricdyad(V::Int) = metricdyad(Submanifold(V))
function metricdyad(V)
if hasconformal(V)
N = mdims(V)
TensorOperator(Chain{V}(Single{V}((UInt(2),-1)),Single{V}((UInt(1),-1)),[Single{V}((UInt(1)<<i,1)) for i list(2,N-1)]...))
else
cayley(V,1,(x,y)->value(contraction(x,y)))
end
end

metricfull(V) = TensorOperator(Multivector{V}(vcat(value.(map.(Multivector,value.(metrictensor.(V,list(0,mdims(V))))))...)))
metriceven(V) = TensorOperator(Spinor{V}(vcat(value.(map.(Spinor,value.(metrictensor.(V,evens(0,mdims(V))))))...)))
metricodd(V) = TensorOperator(AntiSpinor{V}(vcat(value.(map.(AntiSpinor,value.(metrictensor.(V,evens(1,mdims(V))))))...)))

struct MetricTensor{n,ℙ,g,Vars,Diff,Name} <: TensorBundle{n,ℙ,g,Vars,Diff,Name}
@pure MetricTensor{N,M,S,F,D,L}() where {N,M,S,F,D,L} = new{N,M,S,F,D,L}()
end

export MetricTensor, metrictensor
@pure MetricTensor{N,M,S,F,D}() where {N,M,S,F,D} = MetricTensor{N,M,S,F,D,1}()
@pure MetricTensor{N,M,S}() where {N,M,S} = MetricTensor{N,M,S,0,0}()
@pure MetricTensor{N,M}(b::Values{N,<:Tuple}) where {N,M} = MetricTensor{N,M,metricsig(M,Values.(b))}()
@pure MetricTensor{N,M}(b::Values{N,<:Values}) where {N,M} = MetricTensor{N,M,metricsig(M,b)}()
@pure MetricTensor{N,M}(b::Values{N,<:Chain}) where {N,M} = MetricTensor{N,M,metricsig(M,value.(b))}()
@pure MetricTensor{N,M}(b::Values{N,<:AbstractVector}) where {N,M} = MetricTensor{N,M,metricsig(M,Values{N}.(b))}()
MetricTensor{N,M}(b::Vector) where {N,M} = MetricTensor{N,M}(Values(b...))
@pure MetricTensor(b::Tuple) = MetricTensor(Values(b))
@pure MetricTensor(b::Values{N}) where N = MetricTensor{N,0}(b)
MetricTensor(b::Values{N,<:Real}) where N = DiagonalForm(b)
MetricTensor(b::AbstractVector{<:Real}) = DiagonalForm(b)
MetricTensor(b::AbstractVector) = MetricTensor{length(b),0}(b)
MetricTensor(b::AbstractMatrix) = MetricTensor(getindex.(Ref(b),:,1:(size(b)[1])))
MetricTensor(b::Chain{V,G,<:Chain} where {V,G}) = MetricTensor(value(b))
MetricTensor(b::Chain) = DiagonalForm(value(b))
MetricTensor(b::TensorOperator) = MetricTensor(value(b))
MetricTensor(b...) = MetricTensor(b)

@pure Manifold(::Type{T}) where T<:MetricTensor = T()

@pure metrictensor(b::Submanifold{V}) where V = isbasis(b) ? metrictensor(V) : TensorOperator(map(b,b(value(metrictensor(V)))))
@pure metrictensor(V,G) = compound(metrictensor(V),G)
@pure function metrictensor(V::MetricTensor{N,M,S} where N) where {M,S}
out = Chain{Submanifold(V)}.(metrictensor_cache[S])
TensorOperator(Chain{Submanifold(V)}(isdual(V) ? SUB(out) : out))
end
const metrictensor_cache = Values[]
@pure function metricsig(M,b::Values)
a = dyadmode(M)>0 ? SUB(b) : b
if a metrictensor_cache
findfirst(x->x==a,metrictensor_cache)
else
push!(metrictensor_cache,a)
length(metrictensor_cache)
end
end

@pure DirectSum.getalgebra(V::MetricTensor) = DirectSum.getalgebra(Submanifold(V))

for t (Any,Integer)
@eval @inline getindex(s::MetricTensor{N,M,S} where {N,M},i::T) where {S,T<:$t} = value(value(metrictensor(s))[i])
end
@inline getindex(vs::MetricTensor,i::Vector) = [getindex(vs,j) for j i]
@inline getindex(vs::MetricTensor,i::UnitRange{Int}) = [getindex(vs,j) for j i]
@inline getindex(vs::MetricTensor{N,M,S} where M,i::Colon) where {N,S} = Vector(value(metrictensor(vs)))

@pure Signature(V::MetricTensor{N,M}) where {N,M} = Signature{N,M,UInt(0)}()

# anything array-like gets summarized e.g. 10-element Array{Int64,1}
Base.summary(io::IO, a::MetricTensor) = Base.array_summary(io, a, _axes(metrictensor(a)))

show(io::IO,M::MetricTensor) = Base.show(io,Submanifold(M))
function show(io::IO, ::MIME"text/plain", M::MetricTensor)
X = display_matrix(value(metrictensor(M)))
if isempty(X) && get(io, :compact, false)::Bool
return show(io, X)
end
show_matrix(io,M,X)
end

(M::MetricTensor)(b::Int...) = Submanifold{M}(b)
(M::MetricTensor)(b::T) where T<:AbstractVector{Int} = Submanifold{M}(b)
(M::MetricTensor)(b::T) where T<:AbstractRange{Int} = Submanifold{M}(b)

import LinearAlgebra: isdiag; export isdiag
isdiag(::MetricTensor) = false
Loading

2 comments on commit 2523f25

@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/107351

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.19 -m "<description of version>" 2523f2548287733dbb60df960add153a11b1908f
git push origin v0.8.19

Please sign in to comment.