Skip to content

Commit

Permalink
revised some tensor contraction
Browse files Browse the repository at this point in the history
  • Loading branch information
chakravala committed Jun 24, 2024
1 parent 1416e12 commit 2493c4d
Showing 1 changed file with 53 additions and 5 deletions.
58 changes: 53 additions & 5 deletions src/forms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ end

export TensorNested
abstract type TensorNested{V,T} <: Manifold{V,T} end
@pure Manifold(::T) where T<:TensorNested{V} where V = V
@pure Manifold(::Type{T}) where T<:TensorNested{V} where V = V

transpose_row(t::Values{N,<:Chain{V}},i,W=V) where {N,V} = Chain{W,1}(getindex.(t,i))
transpose_row(t::FixedVector{N,<:Chain{V}},i,W=V) where {N,V} = Chain{W,1}(getindex.(t,i))
Expand Down Expand Up @@ -409,6 +411,8 @@ struct DiagonalOperator{V,T<:TensorAlgebra{V}} <: TensorNested{V,T}
DiagonalOperator(t::T) where {V,T<:TensorAlgebra{V}} = new{V,T}(t)
end

(T::DiagonalOperator{V})(x::TensorAlgebra{V}) where V = contraction(T,x)

value(t::DiagonalOperator) = t.v
matrix(m::DiagonalOperator) = matrix(TensorOperator(m))
getindex(t::DiagonalOperator,i::Int,j::Int) = ij ? zero(valuetype(value(t))) : value(t)[i]
Expand Down Expand Up @@ -452,6 +456,7 @@ struct TensorOperator{V,W,T<:TensorAlgebra{V,<:TensorAlgebra{W}}} <: TensorNeste
end

Endomorphism{V,T<:TensorAlgebra{V,<:TensorAlgebra{V}}} = TensorOperator{V,V,T}
(T::TensorOperator{V})(x::TensorAlgebra{V}) where V = contraction(T,x)

value(t::TensorOperator) = t.v
matrix(m::TensorOperator) = matrix(value(m))
Expand Down Expand Up @@ -534,6 +539,7 @@ end
:(Outermorphism{V}($(Expr(:tuple,[:(compound(t,Val($g))) for g list(1,mdims(V))]...))))
end

(T::Outermorphism{V})(x::TensorAlgebra{V}) where V = contraction(T,x)
Outermorphism(t::Simplex) = outermorphism(t)
Outermorphism(t::Endomorphism{V,<:Simplex}) where V = outermorphism(value(t))
outermorphism(t::Endomorphism{V,<:Simplex}) where V = outermorphism(value(t))
Expand Down Expand Up @@ -597,6 +603,13 @@ outer(a::Leibniz.Derivation,b::Chain{V,1}) where V= outer(V(a),b)
outer(a::Chain{W},b::Leibniz.Derivation{T,1}) where {W,T} = outer(a,W(b))
outer(a::Chain{W},b::Chain{V,1}) where {W,V} = Chain{V,1}(a.*value(b))

contraction_metric(a::TensorNested,b::TensorNested,g) = contraction(a,b)
contraction_metric(a::TensorNested,b::TensorAlgebra,g) = contraction(a,b)
contraction_metric(a::TensorAlgebra,b::TensorNested,g) = contraction(a,b)
wedgedot_metric(a::TensorNested,b::TensorNested,g) = ab
wedgedot_metric(a::TensorNested,b::TensorAlgebra,g) = ab
wedgedot_metric(a::TensorAlgebra,b::TensorNested,g) = ab

contraction(a::Proj,b::TensorGraded) = a.v(a.λ*(a.vb))
contraction(a::Dyadic,b::TensorGraded) = a.x(a.yb)
contraction(a::TensorGraded,b::Dyadic) = (ab.x)b.y
Expand Down Expand Up @@ -624,7 +637,7 @@ contraction(a::Submanifold{W},b::Chain{V,G,<:Chain}) where {W,G,V} = Chain{V,G}(
contraction(a::Single{W},b::Chain{V,G,<:Chain}) where {W,G,V} = Chain{V,G}(column(Ref(a).⋅value(b)))
contraction(x::Chain{V,G,<:Chain},y::Single{V,G}) where {V,G} = value(y)*x[bladeindex(mdims(V),UInt(basis(y)))]
contraction(x::Chain{V,G,<:Chain},y::Submanifold{V,G}) where {V,G} = x[bladeindex(mdims(V),UInt(y))]
contraction(a::Chain{V,L,<:Chain{V,G},N},b::Chain{V,G,<:Chain{V},M}) where {V,G,L,N,M} = Chain{V,G}(contraction.(Ref(a),value(b)))
#contraction(a::Chain{V,L,<:Chain{V,G},N},b::Chain{V,G,<:Chain{V},M}) where {V,G,L,N,M} = Chain{V,G}(contraction.(Ref(a),value(b)))
contraction(x::Chain{V,L,<:Chain{V,G},N},y::Chain{V,G,<:Chain{V,L},N}) where {L,N,V,G} = Chain{V,G}(contraction.(Ref(x),value(y)))
contraction(x::Chain{W,L,<:Chain{V,G},N},y::Chain{V,G,T,N}) where {W,L,N,V,G,T} = Chain{V,G}(matmul(value(x),value(y)))
contraction(x::Chain{W,L,<:Multivector{V},N},y::Chain{V,G,T,N}) where {W,L,N,V,G,T} = Multivector{V}(matmul(value(x),value(y)))
Expand All @@ -634,16 +647,22 @@ contraction(x::Multivector{W,<:Multivector{V},N},y::Multivector{V,T,N}) where {W
Expr(:call,:Values,[Expr(:call,:+,:(@inbounds y[$i]*value(x[$i]))) for i list(1,N)]...)
end
@inline @generated function matmul(x::Values{N,<:Chain{V,G}},y::Values{N}) where {N,V,G}
Expr(:call,:Values,[Expr(:call,:+,[:(@inbounds y[$i]*x[$i][$j]) for i list(1,N)]...) for j list(1,binomial(mdims(V),G))]...)
Expr(:call,:Values,[Expr(:call,:+,[:(@inbounds x[$i][$j]*y[$i]) for i list(1,N)]...) for j list(1,binomial(mdims(V),G))]...)
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))]...)
Expr(:call,:Values,[Expr(:call,:+,[:(@inbounds value(x[$i])[$j]*y[$i]) 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))]...)
Expr(:call,:Values,[Expr(:call,:+,[:(@inbounds value(x[$i])[$j]*y[$i]) 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))]...)
Expr(:call,:Values,[Expr(:call,:+,[:(@inbounds value(x[$i])[$j]*y[$i]) for i list(1,N)]...) for j list(1,1<<(mdims(V)-1))]...)
end
@inline @generated function matwedge(x::Values{N,<:Chain{V,G}},y::Values{N}) where {N,V,G}
Expr(:call,:Values,[Expr(:call,:+,[:(@inbounds x[$i][$j]y[$i]) for i list(1,N)]...) for j list(1,binomial(mdims(V),G))]...)
end
@inline @generated function matvee(x::Values{N,<:Chain{V,G}},y::Values{N}) where {N,V,G}
Expr(:call,:Values,[Expr(:call,:+,[:(@inbounds x[$i][$j]y[$i]) for i list(1,N)]...) for j list(1,binomial(mdims(V),G))]...)
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)))
Expand Down Expand Up @@ -682,6 +701,21 @@ minus(a::TensorNested,b::TensorNested) = a+(-b)
@inline (a::Chain{V,G,<:Chain{V,G}} where {V,G},b::TensorNested) = contraction(a,b)
@inline (a::TensorNested,b::Chain{V,G,<:Chain{V,G}} where {V,G}) = contraction(a,b)

for (op,po) ((:,:.∧),(:,:.∨))
@eval begin
$op(a::DiagonalOperator{V,<:Chain{V,G}},b::Chain{V,G}) where {V,G} = Chain{V,G}($po(value(value(a)),value(b)))
$op(a::DiagonalOperator{V,<:Chain{V,G}},b::DiagonalOperator{V,<:Chain{V,G}}) where {V,G} = DiagonalOperator(Chain{V,G}($po(value(value(a)),value(value(b)))))
#$op(a::Endomorphism{W,<:TensorGraded},b::Endomorphism{V,<:Chain{V,G}}) where {W,V,G} = TensorOperator(Chain{V,G}($po(Ref(value(a)),value(value(b)))))
$op(a::Endomorphism{W,<:TensorGraded},b::Endomorphism{V,<:Chain{V,G}}) where {W,V,G} = TensorOperator(Chain{V,G}($po(Ref(a),value(value(b)))))
#$op(x::Chain{W,L,<:Chain{V,G},N},y::Chain{V,G,<:Chain{X,F} where {X,F},N}) where {W,L,N,V,G} = Chain{V,G}($po(Ref(x),value(y)))
end
end
(a::Endomorphism{W,<:Chain},b::Chain{V,G}) where {W,V,G} = Chain{V,G}(matwedge(value(value(a)),value(b)))
(a::Endomorphism{W,<:Chain},b::Chain{V,G}) where {W,V,G} = Chain{V,G}(matvee(value(value(a)),value(b)))
for op (:complementright,:complementleft,:complementrighthodge,:complementlefthodge,:metric,:cometric)
@eval $op(a::Endomorphism{V,<:Chain}) where V = map($op,a)
end

contraction(a::DiagonalOperator{V,<:Chain{V,G}},b::Chain{V,G}) where {V,G} = Chain{V,G}(value(value(a)).*value(b))
contraction(a::Chain{V,G},b::DiagonalOperator{V,<:Chain{V,G}}) where {V,G} = Chain{V,G}(value(a).*value(value(b)))
contraction(a::DiagonalOperator{V,<:Chain{V,G}},b::DiagonalOperator{V,<:Chain{V,G}}) where {V,G} = DiagonalOperator(Chain{V,G}(value(value(a)).*value(value(b))))
Expand Down Expand Up @@ -710,6 +744,14 @@ end
Expr(:call,:(Multivector{V}),Expr(:call,:Values,:(@inbounds value(b)[1]),[:(value(contraction((@inbounds a.v[$g]),b(Val($g))))...) for g list(1,mdims(V))]...))
end

scalarcheck(x) = isscalar(x) ? value(scalar(x)) : x
for (op,args) ((:contraction,()),(:contraction_metric,(:g,)))
@eval @generated function $op(x::Endomorphism{V,<:Chain{V,G,<:Chain{V,G,<:TensorGraded{W,L}}}},y::TensorGraded{W,L},$(args...)) where {W,L,V,G}
Expr(:call,:TensorOperator,Expr(:call,:(Chain{V,G}),[Expr(:call,:(Chain{V,G}),
[:(@inbounds scalarcheck($$op(x[$i][$j],y,$($args...)))) for i list(1,gdims(mdims(V),G))]...) for j list(1,gdims(mdims(V),G))]...))
end
end

for op (:plus,:minus,:+,:-)
@eval @generated function $op(a::Outermorphism{V},b::Outermorphism{V}) where V
Expr(:call,:(Outermorphism{V}),Expr(:tuple,[:($$op(value(a)[$g],value(b)[$g])) for g list(1,mdims(V))]...))
Expand Down Expand Up @@ -749,6 +791,12 @@ for F ∈ Fields
end
end

Base.map(fn, x::DiagonalOperator) = DiagonalOperator(map(fn,value(x)))
Base.map(fn, x::TensorOperator{V,W,<:Chain{V,G}}) where {V,W,G} = TensorOperator(Chain{V,G}(map.(fn,value(value(x)))))
Base.map(fn, x::TensorOperator{V,W,<:Spinor}) where {V,W} = TensorOperator(Spinor{V}(map.(fn,value(value(x)))))
Base.map(fn, x::TensorOperator{V,W,<:AntiSpinor}) where {V,W} = TensorOperator(AntiSpinor{V}(map.(fn,value(value(x)))))
Base.map(fn, x::TensorOperator{V,W,<:Multivector}) where {V,W} = TensorOperator(Multivector{V}(map.(fn,value(value(x)))))

# dyadic identity element

for op (:(Base.:+),:(Base.:-))
Expand Down

2 comments on commit 2493c4d

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

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.21 -m "<description of version>" 2493c4dafc113727cb56ff902686f82fb0cc0af2
git push origin v0.8.21

Please sign in to comment.