Skip to content

Commit

Permalink
upgraded to AbstractTensors v0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
chakravala committed Apr 24, 2024
1 parent 1d2e3bb commit 1ca7fbd
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 57 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Grassmann"
uuid = "4df31cd9-4c27-5bea-88d0-e6a7146666d8"
authors = ["Michael Reed"]
version = "0.8.16"
version = "0.8.17"

[deps]
AbstractTensors = "a8e43f4a-99b7-5565-8bf1-0165161caaea"
Expand All @@ -17,7 +17,7 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
julia = "1"
Leibniz = "0.2"
DirectSum = "0.8.1"
AbstractTensors = "0.7.7"
AbstractTensors = "0.8"
ComputedFieldTypes = "1"
Requires = "1"

Expand Down
10 changes: 9 additions & 1 deletion src/composite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,7 @@ Base.rand(::AbstractRNG,::SamplerType{PseudoCouple{V,B,T} where B}) where {V,T}

# Dyadic

export operator, gradedoperator
export operator, gradedoperator, evenoperator, oddoperator

@generated function operator(t::TensorAlgebra{V},::Val{G}=Val(1)) where {V,G}
N = mdims(V)
Expand All @@ -1104,3 +1104,11 @@ gradedoperator(t::TensorAlgebra{V}) where V = Multivector{V}(Λ(V).b .⊘ Ref(t)
end
operator(fun,V,G::Int) = operator(fun,V,Val(G))
gradedoperator(fun,V) = Multivector{V}(fun.(Λ(V).b))

@pure function evenbasis(V,even=true)
N = mdims(V)
r,b = binomsum_set(N),binomial_set(N)
vcat([Λ(V).b[list(r[g]+1,r[g]+b[g])] for g evens(even ? 1 : 2,N+1)]...)
end
evenoperator(t::TensorAlgebra{V}) where V = Spinor{V}(evenbasis(V) .⊘ Ref(t))
oddoperator(t::TensorAlgebra{V}) where V = AntiSpinor{V}(evenbasis(V,false) .⊘ Ref(t))
91 changes: 39 additions & 52 deletions src/multivectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import AbstractTensors: TensorTerm, TensorGraded, TensorMixed, equal
import Leibniz: grade, antigrade, showvalue, basis, order

export TensorNested
abstract type TensorNested{V} <: Manifold{V} end
abstract type TensorNested{V,T} <: Manifold{V,T} end

for op (:(Base.:+),:(Base.:-))
@eval begin
Expand Down Expand Up @@ -59,15 +59,15 @@ export UniformScaling, I, points

## Chain{V,G,𝕂}

@computed struct Chain{V,G,𝕂} <: TensorGraded{V,G}
v::Values{binomial(mdims(V),G),𝕂}
Chain{V,G,𝕂}(v) where {V,G,𝕂} = new{DirectSum.submanifold(V),G,𝕂}(v)
@computed struct Chain{V,G,T} <: TensorGraded{V,G,T}
v::Values{binomial(mdims(V),G),T}
Chain{V,G,T}(v) where {V,G,T} = new{DirectSum.submanifold(V),G,T}(v)
end

"""
Chain{V,G,𝕂} <: TensorGraded{V,G} <: TensorAlgebra{V}
Chain{V,G,T} <: TensorGraded{V,G,T} <: TensorAlgebra{V,T}
Chain type with pseudoscalar `V::Manifold`, grade/rank `G::Int`, scalar field `𝕂::Type`.
Chain type with pseudoscalar `V::Manifold`, grade/rank `G::Int`, scalar field `T::Type`.
"""
Chain{V,G}(val::S) where {V,G,S<:AbstractVector{𝕂}} where 𝕂 = Chain{V,G,𝕂}(val)
Chain{V,G}(val::NTuple{N,T}) where {V,G,N,T} = Chain{V,G}(Values{N,T}(val))
Expand All @@ -85,6 +85,7 @@ Chain(v::Chain{V,G,𝕂}) where {V,G,𝕂} = v
DyadicProduct{V,W,G,T,N} = Chain{V,G,Chain{W,G,T,N},N}
DyadicChain{V,G,T,N} = DyadicProduct{V,V,G,T,N}

Base.Matrix(m::Chain{V,G,<:TensorGraded{W,G}}) where {V,W,G} = hcat(value.(Chain.(value(m)))...)
Base.Matrix(m::Chain{V,G,<:Chain{W,G}}) where {V,W,G} = hcat(value.(value(m))...)
DyadicChain(m::Matrix) = Chain{Submanifold(size(m)[1]),1}(m)
function Chain{V,G}(m::Matrix) where {V,G}
Expand Down Expand Up @@ -203,12 +204,12 @@ Base.ones(::Type{Chain{V,G,T,X}}) where {V,G,T<:Chain,X} = Chain{V,G,T}(ones.(nt
(a::Type{<:Chain{V,1}},b::Type{<:Chain{W,1,T}}) where {V,W,T} = Chain{V,1,Chain{W,1,T,mdims(W)},mdims(V)}

"""
ChainBundle{V,G,P} <: Manifold{V} <: TensorAlgebra{V}
ChainBundle{V,G,T,P} <: Manifold{V,T} <: TensorAlgebra{V,T}
Subsets of a bundle cross-section over a `Manifold` topology.
"""
struct ChainBundle{V,G,𝕂,Points} <: Manifold{V}
@pure ChainBundle{V,G,𝕂,P}() where {V,G,𝕂,P} = new{DirectSum.submanifold(V),G,𝕂,P}()
struct ChainBundle{V,G,T,Points} <: Manifold{V,T}
@pure ChainBundle{V,G,T,P}() where {V,G,T,P} = new{DirectSum.submanifold(V),G,T,P}()
end

const bundle_cache = (Vector{Chain{V,G,T,X}} where {V,G,T,X})[]
Expand Down Expand Up @@ -253,7 +254,6 @@ end

value(c::Vector{<:Chain}) = c
value(::ChainBundle{V,G,T,P}) where {V,G,T,P} = bundle_cache[P]::(Vector{Chain{V,G,T,binomial(mdims(V),G)}})
AbstractTensors.valuetype(::ChainBundle{V,G,T} where {V,G}) where T = T

getindex(m::ChainBundle,i::I) where I<:Integer = getindex(value(m),i)
getindex(m::ChainBundle,i) = getindex(value(m),i)
Expand All @@ -271,17 +271,17 @@ Base.display(m::ChainBundle) = (print(showbundle(m));display(value(m)))
Base.show(io::IO,m::ChainBundle) = print(io,showbundle(m),length(m))
@pure showbundle(m::ChainBundle{V,G}) where {V,G} = "$(iscell(m) ? 'C' : islocal(m) ? 'I' : 'Λ')$(DirectSum.sups[G])$V×"

## Multivector{V,𝕂}
## Multivector{V,T}

@computed struct Multivector{V,𝕂} <: TensorMixed{V}
v::Values{1<<mdims(V),𝕂}
Multivector{V,𝕂}(v) where {V,𝕂} = new{DirectSum.submanifold(V),𝕂}(v)
@computed struct Multivector{V,T} <: TensorMixed{V,T}
v::Values{1<<mdims(V),T}
Multivector{V,T}(v) where {V,T} = new{DirectSum.submanifold(V),T}(v)
end

"""
Multivector{V,𝕂} <: TensorMixed{V} <: TensorAlgebra{V}
Multivector{V,T} <: TensorMixed{V,T} <: TensorAlgebra{V,T}
Chain type with pseudoscalar `V::Manifold` and scalar field `𝕂::Type`.
Chain type with pseudoscalar `V::Manifold` and scalar field `T::Type`.
"""
Multivector{V}(v::S) where {V,S<:AbstractVector{T}} where T = Multivector{V,T}(v)
for var ((:V,:T),(:T,),())
Expand Down Expand Up @@ -320,6 +320,7 @@ Multivector(val::NTuple{N,Any}) where N = Multivector{log2sub(N)}(Values{N}(val)

DyadicMultivector{V,T,N} = Multivector{V,Multivector{V,T,N},N}

Base.Matrix(m::Multivector{V,<:TensorAlgebra{W}}) where {V,W} = hcat(value.(Multivector.(value(m)))...)
Base.Matrix(m::Multivector{V,<:Multivector{W}}) where {V,W} = hcat(value.(value(m))...)
DyadicMultivector(m::Matrix) = Multivector{log2sub(size(m)[1]),1}(m)
function Multivector{V}(m::Matrix) where V
Expand Down Expand Up @@ -457,11 +458,11 @@ getindex(m::Multivector{V},i::Submanifold{V}) where V = m[basisindex(mdims(V),UI
## AbstractSpinor{V}

"""
AbstractSpinor{V} <: TensorMixed{V} <: TensorAlgebra{V}
AbstractSpinor{V,T} <: TensorMixed{V,T} <: TensorAlgebra{V,T}
Elements of `TensorAlgebra` having non-homogenous grade being a spinor in the abstract.
"""
abstract type AbstractSpinor{V} <: TensorMixed{V} end
abstract type AbstractSpinor{V,T} <: TensorMixed{V,T} end

## Spinor{V}, PsuedoSpinor

Expand All @@ -470,9 +471,9 @@ abstract type AbstractSpinor{V} <: TensorMixed{V} end
for pinor (:Spinor,:AntiSpinor)
dpinor = Symbol(:Dyadic,pinor)
@eval begin
@computed struct $pinor{V,𝕂} <: AbstractSpinor{V}
v::Values{1<<(mdims(V)-1),𝕂}
$pinor{V,𝕂}(v::Values{N,𝕂}) where {N,V,𝕂} = new{DirectSum.submanifold(V),𝕂}(v)
@computed struct $pinor{V,T} <: AbstractSpinor{V,T}
v::Values{1<<(mdims(V)-1),T}
$pinor{V,T}(v::Values{N,T}) where {N,V,T} = new{DirectSum.submanifold(V),T}(v)
end
$pinor{V,T}(v::AbstractVector{T}) where {V,T} = $pinor{V,T}(Values{1<<(mdims(V)-1),T}(v))
$pinor{V}(v::AbstractVector{T}) where {V,T} = $pinor{V,T}(v)
Expand Down Expand Up @@ -502,6 +503,7 @@ for pinor ∈ (:Spinor,:AntiSpinor)
equal(a::Chain{V,G,T},b::$pinor{V,S}) where {V,S,G,T} = b == a
equal(a::T,b::$pinor{V,S} where S) where T<:TensorTerm{V} where V = b==a
$dpinor{V,T,N} = $pinor{V,$pinor{V,T,N},N}
Base.Matrix(m::$pinor{V,<:TensorAlgebra{W}}) where {V,W} = hcat(value.($pinor.(value(m)))...)
Base.Matrix(m::$pinor{V,<:$pinor{W}}) where {V,W} = hcat(value.(value(m))...)
$pinor(m::Matrix) = $pinor{log2sub(size(m)[1]),1}(m)
function $pinor{V}(m::Matrix) where V
Expand All @@ -513,9 +515,9 @@ for pinor ∈ (:Spinor,:AntiSpinor)
end

"""
Spinor{V,𝕂} <: AbstractSpinor{V} <: TensorAlgebra{V}
Spinor{V,T} <: AbstractSpinor{V,T} <: TensorAlgebra{V,T}
Spinor (`even` grade) type with pseudoscalar `V::Manifold` and scalar field `𝕂::Type`.
Spinor (`even` grade) type with pseudoscalar `V::Manifold` and scalar field `T::Type`.
"""
Spinor{V}(val::Submanifold{V}) where V = Spinor{V,Int}(1,val)
Spinor{V,𝕂}(v::Submanifold{V,G}) where {V,G,𝕂} = Spinor{V,𝕜}(1,v)
Expand All @@ -531,9 +533,9 @@ Spinor{V,𝕂}(v::Submanifold{V,G}) where {V,G,𝕂} = Spinor{V,𝕜}(1,v)
end

"""
AntiSpinor{V,𝕂} <: AbstractSpinor{V} <: TensorAlgebra{V}
AntiSpinor{V,T} <: AbstractSpinor{V,T} <: TensorAlgebra{V,T}
PsuedoSpinor (`odd` grade) type with pseudoscalar `V::Manifold` and scalar `𝕂::Type`.
PsuedoSpinor (`odd` grade) type with pseudoscalar `V::Manifold` and scalar `T::Type`.
"""
AntiSpinor{V}(val::Submanifold{V}) where V = AntiSpinor{V,Int}(1,val)
AntiSpinor{V,𝕂}(v::Submanifold{V,G}) where {V,G,𝕂} = AntiSpinor{V,𝕜}(1,v)
Expand Down Expand Up @@ -697,14 +699,14 @@ for T ∈ Fields
end
end

## Couple{V,B}, PseudoCouple{V,B}
## Couple{V,B,T}, PseudoCouple{V,B,T}

"""
Couple{V,B,𝕂} <: AbstractSpinor{V} <: TensorAlgebra{V}
Couple{V,B,T} <: AbstractSpinor{V,T} <: TensorAlgebra{V,T}
`Complex{𝕂}` wrapper with `V::Manifold`, basis `B::Submanifold`, scalar field of `𝕂::Type`.
`Complex{T}` wrapper with `V::Manifold`, basis `B::Submanifold`, scalar field of `T::Type`.
"""
struct Couple{V,B,T} <: AbstractSpinor{V}
struct Couple{V,B,T} <: AbstractSpinor{V,T}
v::Complex{T}
Couple{V,B}(a::T,b::T) where {V,B,T} = new{DirectSum.submanifold(V),B,T}(Complex{T}(a,b))
Couple{V,B}(v::Complex{T}) where {V,B,T} = new{DirectSum.submanifold(V),B,T}(v)
Expand All @@ -715,11 +717,11 @@ Base.abs2(z::Couple{V,B}) where {V,B} = abs2(z.v.re) + abs2(z.v.im)*abs2_inv(B)
grade(z::Couple{V,B},::Val{G}) where {V,G,B} = grade(B)==G ? z.v.im : G==0 ? z.v.re : Zero(V)

"""
PseudoCouple{V,B,𝕂} <: AbstractSpinor{V} <: TensorAlgebra{V}
PseudoCouple{V,B,T} <: AbstractSpinor{V,T} <: TensorAlgebra{V,T}
`Complex{𝕂}` wrapper with `V::Manifold`, basis `B::Submanifold`, pseudoscalar of `𝕂::Type`.
`Complex{T}` wrapper with `V::Manifold`, basis `B::Submanifold`, pseudoscalar of `T::Type`.
"""
struct PseudoCouple{V,B,T} <: AbstractSpinor{V}
struct PseudoCouple{V,B,T} <: AbstractSpinor{V,T}
v::Complex{T}
PseudoCouple{V,B}(a::T,b::T) where {V,B,T} = new{DirectSum.submanifold(V),B,T}(Complex{T}(a,b))
PseudoCouple{V,B}(v::Complex{T}) where {V,B,T} = new{DirectSum.submanifold(V),B,T}(v)
Expand Down Expand Up @@ -872,11 +874,11 @@ end
export Phasor, ∠, radius

"""
Phasor{V,B,𝕂} <: AbstractSpinor{V} <: TensorAlgebra{V}
Phasor{V,B,T} <: AbstractSpinor{V,T} <: TensorAlgebra{V,T}
`Complex{𝕂}` wrapper with `V::Manifold`, basis `B::Submanifold`, scalar field `𝕂::Type`.
`Complex{T}` wrapper with `V::Manifold`, basis `B::Submanifold`, scalar field `T::Type`.
"""
struct Phasor{V,B,T} <: AbstractSpinor{V}
struct Phasor{V,B,T} <: AbstractSpinor{V,T}
v::Complex{T}
Phasor{V,B}(r::T,iθ::T) where {V,B,T} = new{DirectSum.submanifold(V),B,T}(Complex{T}(r,iθ))
Phasor{V,B}(v::Complex{T}) where {V,B,T} = new{DirectSum.submanifold(V),B,T}(v)
Expand Down Expand Up @@ -956,7 +958,7 @@ end

export Projector, Dyadic, Proj

struct Projector{V,T,Λ} <: TensorNested{V}
struct Projector{V,T,Λ} <: TensorNested{V,T}
v::T
λ::Λ
Projector{V,T,Λ}(v::T::Λ=1) where {T<:Manifold{V},Λ} where V = new{DirectSum.submanifold(V),T,Λ}(v,λ)
Expand Down Expand Up @@ -987,7 +989,7 @@ DyadicChain{V,1,T}(P::Proj{V,T}) where {V,T<:Chain{V,1,<:Chain}} = sum(outer.(va
DyadicChain{V}(P::Proj{V,T}) where {V,T} = DyadicChain{V,1,T}(P)
DyadicChain(P::Proj{V,T}) where {V,T} = DyadicChain{V,1,T}(P)

struct Dyadic{V,X,Y} <: TensorNested{V}
struct Dyadic{V,X,Y} <: TensorNested{V,X}
x::X
y::Y
Dyadic{V,X,Y}(x::X,y::Y) where {X<:TensorGraded,Y<:TensorGraded{V}} where V = new{DirectSum.submanifold(V),X,Y}(x,y)
Expand Down Expand Up @@ -1078,21 +1080,6 @@ quatvalues(q::TensorAlgebra) = quatvalues(Spinor(even(q)))
quatvalues(q::Quaternion{V,T}) where {V,T} = Values{4,T}(q.v[1],q.v[2],-q.v[3],q.v[4])
quatvalues(q::AntiQuaternion{V,T}) where {V,T} = Values{4,T}(q.v[4],q.v[3],q.v[2],q.v[1])

@pure valuetype(::Chain{V,G,T} where {V,G}) where T = T
@pure valuetype(::Multivector{V,T} where V) where T = T
@pure valuetype(::Spinor{V,T} where V) where T = T
@pure valuetype(::AntiSpinor{V,T} where V) where T = T
@pure valuetype(::Couple{V,B,T} where {V,B}) where T = T
@pure valuetype(::PseudoCouple{V,B,T} where {V,B}) where T = T
@pure valuetype(::Phasor{V,B,T} where {V,B}) where T = T
@pure valuetype(::Type{<:Chain{V,G,T} where {V,G}}) where T = T
@pure valuetype(::Type{<:Multivector{V,T} where V}) where T = T
@pure valuetype(::Type{<:Spinor{V,T} where V}) where T = T
@pure valuetype(::Type{<:AntiSpinor{V,T} where V}) where T = T
@pure valuetype(::Type{<:Couple{V,B,T} where {V,B}}) where T = T
@pure valuetype(::Type{<:PseudoCouple{V,B,T} where {V,B}}) where T = T
@pure valuetype(::Type{<:Phasor{V,B,T} where {V,B}}) where T = T

@inline value(m::Chain,T=valuetype(m)) = T(valuetype(m),Any) ? convert(T,m.v) : m.v
@inline value(m::Multivector,T=valuetype(m)) = T(valuetype(m),Any) ? convert(T,m.v) : m.v
@inline value(m::Spinor,T=valuetype(m)) = T(valuetype(m),Any) ? convert(T,m.v) : m.v
Expand Down
4 changes: 2 additions & 2 deletions src/products.jl
Original file line number Diff line number Diff line change
Expand Up @@ -714,8 +714,8 @@ contraction(a::Proj{V,<:Chain{V,1,<:TensorNested}},b::TensorGraded{V,0}) where V
#contraction(a::Chain{W,1,<:Proj{V}},b::Chain{V,1}) where {W,V} = Chain{W,1}(value(a).⋅b)
contraction(a::Chain{W,1,<:Dyadic{V}},b::Chain{V,1}) where {W,V} = Chain{W,1}(value(a).⋅Ref(b))
contraction(a::Proj{W,<:Chain{W,1,<:TensorNested{V}}},b::Chain{V,1}) where {W,V} = a.v:b
contraction(a::Chain{W},b::Chain{V,G,<:Chain}) where {W,G,V} = Chain{V,G}(column(Ref(a).⋅value(b)))
contraction(a::Chain{W,L,<:Chain},b::Chain{V,G,<:Chain{W,L}}) where {W,L,G,V} = Chain{V,G}(column(Ref(a).⋅value(b)))
contraction(a::Chain{W},b::Chain{V,G,<:Chain}) where {W,G,V} = Chain{V,G}(value.(Ref(a).⋅value(b)))
contraction(a::Chain{W,L,<:Chain},b::Chain{V,G,<:Chain{W,L}}) where {W,L,G,V} = Chain{V,G}(value.(Ref(a).⋅value(b)))
contraction(a::Multivector{W,<:Multivector},b::Multivector{V,<:Multivector{W}}) where {W,V} = Multivector{V}(column(Ref(a).⋅value(b)))
Base.:(:)(a::Chain{V,1,<:Chain},b::Chain{V,1,<:Chain}) where V = sum(value(a).⋅value(b))
Base.:(:)(a::Chain{W,1,<:Dyadic{V}},b::Chain{V,1}) where {W,V} = sum(value(a).⋅Ref(b))
Expand Down

2 comments on commit 1ca7fbd

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

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.17 -m "<description of version>" 1ca7fbd6a9e2916b09147fe8532f8c0ad7cc3d3b
git push origin v0.8.17

Please sign in to comment.