Skip to content

Commit

Permalink
revised grade selection, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
chakravala committed Apr 22, 2024
1 parent 6f7f7c3 commit 1d2e3bb
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 68 deletions.
2 changes: 1 addition & 1 deletion 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.15"
version = "0.8.16"

[deps]
AbstractTensors = "a8e43f4a-99b7-5565-8bf1-0165161caaea"
Expand Down
116 changes: 51 additions & 65 deletions src/algebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1257,17 +1257,16 @@ for (op,product) ∈ ((:∧,:exteradd),(:*,:geomadd),
@inbounds for i 1:bn[g]
if S<:Chain
@inbounds val = :(@inbounds b.v[$(bs[g]+i)])
for j 1:bn[G+1]
A,B = swapper(ib[j],ia[i],swap)
@inbounds for j 1:bn[G+1]
@inbounds A,B = swapper(ib[j],ia[i],swap)
X,Y = swapper(:(@inbounds a[$j]),val,swap)
@inbounds $preproduct!(V,out,A,B,derive_pre(V,A,B,X,Y,MUL))
$preproduct!(V,out,A,B,derive_pre(V,A,B,X,Y,MUL))
end
else
U = UInt(basis(a))
A,B = swapper(U,ia[i],swap)
@inbounds A,B = swapper(UInt(basis(a)),ia[i],swap)
if S<:Single
X,Y = swapper(:(a.v),:(@inbounds b.v[$(bs[g]+i)]),swap)
@inbounds $preproduct!(V,out,A,B,derive_pre(V,A,B,X,Y,MUL))
$preproduct!(V,out,A,B,derive_pre(V,A,B,X,Y,MUL))
else
@inbounds $preproduct!(V,out,A,B,derive_pre(V,A,B,:(@inbounds b.v[$(bs[g]+i)]),false))
end
Expand All @@ -1288,19 +1287,19 @@ for (op,product) ∈ ((:∧,:exteradd),(:*,:geomadd),
A,B = $(swap ? :(@inbounds (ia[i],ib[j])) : :(@inbounds (ib[j],ia[i])))
X,Y = $(swap ? :((val,@inbounds a[j])) : :((@inbounds a[j],val)))
dm = derive_mul(V,A,B,X,Y,$MUL)
if @inbounds $$product!(V,out,A,B,dm)&μ
if $$product!(V,out,A,B,dm)&μ
$(insert_expr((:out,);mv=:out)...)
@inbounds $$product!(V,out,A,B,dm)
$$product!(V,out,A,B,dm)
end
end end
else quote
A,B = $(swap ? :((@inbounds ia[i],$(UInt(basis(a))))) : :(($(UInt(basis(a))),@inbounds ia[i])))
$(if S<:Single; quote
X,Y=$(swap ? :((b.v[bs[g]+1],a.v)) : :((a.v,@inbounds b.v[rs[g]+1])))
dm = derive_mul(V,A,B,X,Y,$MUL)
if @inbounds $$product!(V,out,A,B,dm)&μ
if $$product!(V,out,A,B,dm)&μ
$(insert_expr((:out,);mv=:out)...)
@inbounds $$product!(V,out,A,B,dm)
$$product!(V,out,A,B,dm)
end end
else
:(if @inbounds $$product!(V,out,A,B,derive_mul(V,A,B,b.v[rs[g]+i],false))&μ
Expand Down Expand Up @@ -1338,19 +1337,16 @@ for input ∈ (:Spinor,:AntiSpinor)
@inbounds for i 1:bn[g]
@inbounds val = par ? :(@inbounds -b.v[$(bs[g]+i)]) : :(@inbounds b.v[$(bs[g]+i)])
if S<:Chain
for j 1:bn[G+1]
A,B = swapper(ib[j],ia[i],true)
X,Y = swapper(:(@inbounds a[$j]),val,true)
@inbounds $preproduct!(V,out,A,B,derive_pre(V,A,B,X,Y,MUL))
@inbounds for j 1:bn[G+1]
@inbounds A,B = ia[i],ib[j]
$preproduct!(V,out,A,B,derive_pre(V,A,B,val,:(@inbounds a[$j]),MUL))
end
else
U = UInt(basis(a))
A,B = swapper(U,ia[i],true)
@inbounds A,B = ia[i],UInt(basis(a))
if S<:Single
X,Y = swapper(:(a.v),val,true)
@inbounds $preproduct!(V,out,A,B,derive_pre(V,A,B,X,Y,MUL))
$preproduct!(V,out,A,B,derive_pre(V,A,B,val,:(a.v),MUL))
else
@inbounds $preproduct!(V,out,A,B,derive_pre(V,A,B,val,false))
$preproduct!(V,out,A,B,derive_pre(V,A,B,val,false))
end
end
end
Expand All @@ -1364,11 +1360,11 @@ for input ∈ (:Spinor,:AntiSpinor)
!isnull(val) && for g2 $(inspin ? :(evens(1,N+1)) : :(evens(2,N+1)))
io = indexbasis(N,g2-1)
par = swap ? parityclifford(g2-1) : false
for j 1:bn[g2]
@inbounds for j 1:bn[g2]
val2 = :(b.v[$(bs[g2]+j)])
A,B = swapper(io[j],ia[i],true)
X,Y = swapper(par ? :(@inbounds -$val2) : :(@inbounds $val2),val,true)
$preproduct2!(V,out2,A,B,derive_pre(V,A,B,X,Y,MUL))
@inbounds A,B = ia[i],io[j]
Y = par ? :(@inbounds -$val2) : :(@inbounds $val2)
$preproduct2!(V,out2,A,B,derive_pre(V,A,B,val,Y,MUL))
end
end
end
Expand Down Expand Up @@ -1427,47 +1423,41 @@ for input ∈ (:Chain,)
VECS = isodd(G) ? VEC : string(VEC)*"s"
if mdims(V)<cache_limit
$(insert_expr((:N,:t,:ib,:bn,))...)
il = indexbasis(N,L)
bs = (iseven(L) ? spinsum_set : antisum_set)(N)
out = svecs(N,Any)(zeros(svecs(N,t)))
par = parityclifford(L)
if Q <: Chain
ia = indexbasis(N,L)
@inbounds for i 1:bn[L+1]
@inbounds val = (swap ? false : par) ? :(@inbounds -b.v[$i]) : :(@inbounds b.v[$i])
if S<:Chain
for j 1:bn[G+1]
A,B = swapper(ib[j],ia[i],true)
X,Y = swapper(:(@inbounds a[$j]),val,true)
@inbounds $preproduct!(V,out,A,B,derive_pre(V,A,B,X,Y,MUL))
@inbounds for j 1:bn[G+1]
@inbounds A,B = il[i],ib[j]
$preproduct!(V,out,A,B,derive_pre(V,A,B,val,:(@inbounds a[$j]),MUL))
end
else
U = UInt(basis(a))
A,B = swapper(U,ia[i],true)
@inbounds A,B = il[i],UInt(basis(a))
if S<:Single
X,Y = swapper(:(a.v),val,true)
@inbounds $preproduct!(V,out,A,B,derive_pre(V,A,B,X,Y,MUL))
$preproduct!(V,out,A,B,derive_pre(V,A,B,val,:(a.v),MUL))
else
@inbounds $preproduct!(V,out,A,B,derive_pre(V,A,B,val,false))
$preproduct!(V,out,A,B,derive_pre(V,A,B,val,false))
end
end
end
else
U2 = UInt(basis(b))
@inbounds val = (swap ? false : par) ? :(@inbounds -value(b)) : :(@inbounds value(b))
A = UInt(basis(b))
val = (swap ? false : par) ? :(-value(b)) : :(value(b))
if S<:Chain
for j 1:bn[G+1]
A,B = swapper(ib[j],U2,true)
X,Y = swapper(:(@inbounds a[$j]),val,true)
@inbounds $preproduct!(V,out,A,B,derive_pre(V,A,B,X,Y,MUL))
@inbounds for j 1:bn[G+1]
@inbounds B = ib[j]
$preproduct!(V,out,A,B,derive_pre(V,A,B,val,:(@inbounds a[$j]),MUL))
end
else
U = UInt(basis(a))
A,B = swapper(U,U2,true)
B = UInt(basis(a))
if S<:Single
X,Y = swapper(:(a.v),val,true)
@inbounds $preproduct!(V,out,A,B,derive_pre(V,A,B,X,Y,MUL))
$preproduct!(V,out,A,B,derive_pre(V,A,B,val,:(a.v),MUL))
else
@inbounds $preproduct!(V,out,A,B,derive_pre(V,A,B,val,false))
$preproduct!(V,out,A,B,derive_pre(V,A,B,val,false))
end
end
end
Expand All @@ -1478,19 +1468,18 @@ for input ∈ (:Chain,)
@inbounds for i 1:bn[g]
@inbounds val = out[bs2[g]+i]
!isnull(val) && if Q<:Chain
for j 1:bn[L+1]
A,B = swapper(ib[j],ia[i],true)
X,Y = swapper((swap ? par : false) ? :(@inbounds -b[$j]) : :(@inbounds b[$j]),val,true)
@inbounds $preproduct2!(V,out2,A,B,derive_pre(V,A,B,X,Y,MUL))
@inbounds for j 1:bn[L+1]
@inbounds A,B = ia[i],il[j]
Y = (swap ? par : false) ? :(@inbounds -b[$j]) : :(@inbounds b[$j])
$preproduct2!(V,out2,A,B,derive_pre(V,A,B,val,Y,MUL))
end
else
U = UInt(basis(b))
A,B = swapper(U,ia[i],true)
@inbounds A,B = ia[i],UInt(basis(b))
if Q<:Single
X,Y = swapper((swap ? par : false) ? :(-value(b)) : :(value(b)),val,true)
@inbounds $preproduct2!(V,out2,A,B,derive_pre(V,A,B,X,Y,MUL))
Y = swapper((swap ? par : false) ? :(-value(b)) : :(value(b)),val,true)
$preproduct2!(V,out2,A,B,derive_pre(V,A,B,val,Y,MUL))
else
@inbounds $preproduct2!(V,out2,A,B,derive_pre(V,A,B,val,false))
$preproduct2!(V,out2,A,B,derive_pre(V,A,B,val,false))
end
end
end
Expand Down Expand Up @@ -1522,21 +1511,18 @@ for input ∈ (:Couple,:PseudoCouple)
if N<cache_limit
$(insert_expr((:t,:ib,:bn,))...)
out = svecs(N,Any)(zeros(svecs(N,t)))
for (U2,val) ((UInt(BB),(swap ? false : parityclifford(grade(BB))) ? :(-value(imaginary(b))) : :(value(imaginary(b)))),(indexbasis(N,$pg)[1],(swap ? false : parityclifford($pg)) ? :(-value($$calar(b))) : :(value($$calar(b)))))
for (A,val) ((UInt(BB),(swap ? false : parityclifford(grade(BB))) ? :(-value(imaginary(b))) : :(value(imaginary(b)))),(indexbasis(N,$pg)[1],(swap ? false : parityclifford($pg)) ? :(-value($$calar(b))) : :(value($$calar(b)))))
if S<:Chain
for j 1:bn[G+1]
A,B = swapper(ib[j],U2,true)
X,Y = swapper(:(@inbounds a[$j]),val,true)
@inbounds $preproduct!(V,out,A,B,derive_pre(V,A,B,X,Y,MUL))
@inbounds for j 1:bn[G+1]
@inbounds B = ib[j]
$preproduct!(V,out,A,B,derive_pre(V,A,B,val,:(@inbounds a[$j]),MUL))
end
else
U = UInt(basis(a))
A,B = swapper(U,U2,true)
B = UInt(basis(a))
if S<:Single
X,Y = swapper(:(a.v),val,true)
@inbounds $preproduct!(V,out,A,B,derive_pre(V,A,B,X,Y,MUL))
$preproduct!(V,out,A,B,derive_pre(V,A,B,val,:(a.v),MUL))
else
@inbounds $preproduct!(V,out,A,B,derive_pre(V,A,B,val,false))
$preproduct!(V,out,A,B,derive_pre(V,A,B,val,false))
end
end
end
Expand All @@ -1547,8 +1533,8 @@ for input ∈ (:Couple,:PseudoCouple)
@inbounds for i 1:bn[g]
@inbounds val = out[bs2[g]+i]
!isnull(val) && for (B,val2) ((UInt(BB),(swap ? parityclifford(grade(BB)) : false) ? :(-value(imaginary(b))) : :(value(imaginary(b)))),(indexbasis(N,$pg)[1],(swap ? parityclifford($pg) : false) ? :(-value($$calar(b))) : :(value($$calar(b)))))
A = ia[i] #A,B = swapper(U,ia[i],true)
@inbounds $preproduct2!(V,out2,A,B,derive_pre(V,A,B,val,val2,MUL))
@inbounds A = ia[i]
$preproduct2!(V,out2,A,B,derive_pre(V,A,B,val,val2,MUL))
end
end
end
Expand Down
22 changes: 22 additions & 0 deletions src/composite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1082,3 +1082,25 @@ Base.rand(::AbstractRNG,::SamplerType{PseudoCouple{V}}) where V = rand(PseudoCou
Base.rand(::AbstractRNG,::SamplerType{PseudoCouple{V,B}}) where {V,B} = PseudoCouple{V,B}(rand(Complex{Float64}))
Base.rand(::AbstractRNG,::SamplerType{PseudoCouple{V,B,T}}) where {V,B,T} = PseudoCouple{V,B}(rand(Complex{T}))
Base.rand(::AbstractRNG,::SamplerType{PseudoCouple{V,B,T} where B}) where {V,T} = rand(PseudoCouple{V,Submanifold{V}(UInt(rand(0:(1<<mdims(V)-1)-1))),T})

# Dyadic

export operator, gradedoperator

@generated function operator(t::TensorAlgebra{V},::Val{G}=Val(1)) where {V,G}
N = mdims(V)
r,b = binomsum(N,G),binomial(N,G)
bas = Λ(V).b[list(r+1,r+b)]
:(Chain{V,G}($bas .⊘ Ref(t)))
end
operator(t::TensorAlgebra,G::Int) = operator(t,Val(G))
gradedoperator(t::TensorAlgebra{V}) where V = Multivector{V}(Λ(V).b .⊘ Ref(t))

@generated function operator(fun,V,::Val{G}=Val(1)) where G
N = mdims(V)
r,b = binomsum(N,G),binomial(N,G)
bas = Λ(V()).b[list(r+1,r+b)]
:(Chain{V,G}(fun.($bas)))
end
operator(fun,V,G::Int) = operator(fun,V,Val(G))
gradedoperator(fun,V) = Multivector{V}(fun.(Λ(V).b))
36 changes: 34 additions & 2 deletions src/multivectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ 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,<: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}
N = size(m)[2]
Chain{V,G,Chain{N≠mdims(V) ? Submanifold(N) : V,G}}(m)
end
Chain{V,G,Chain{W,G}}(m::Matrix) where {V,W,G} = Chain{V,G}(Chain{W,G}.(getindex.(Ref(m),:,list(1,size(m)[2]))))

export Chain, DyadicProduct, DyadicChain
getindex(m::Chain,i::Int) = m.v[i]
getindex(m::Chain,i::UnitRange{Int}) = m.v[i]
Expand Down Expand Up @@ -310,6 +318,16 @@ Multivector(val::NTuple{N,T}) where {N,T} = Multivector{log2sub(N)}(Values{N,T}(
Multivector(val::NTuple{N,Any}) where N = Multivector{log2sub(N)}(Values{N}(val))
@inline (::Type{T})(x...) where {T<:Multivector} = T(x)

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

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
N = size(m)[2]
Multivector{V,Multivector{Int(log2(N))≠mdims(V) ? log2sub(N) : V}}(m)
end
Multivector{V,Chain{W}}(m::Matrix) where {V,W} = Multivector{V}(Multivector{W}.(getindex.(Ref(m),:,list(1,size(m)[2]))))

function grade_src_chain(N,G,r=binomsum(N,G),is=isempty,T=Int)
:(Chain{V,$G,T}($(grade_src(N,G,r,is,T))))
end
Expand All @@ -326,7 +344,7 @@ end
for fun (:grade_src,:grade_src_chain)
nex = Symbol(fun,:_next)
@eval function $nex(N,G,r=binomsum,is=isempty,T=Int)
Expr(:elseif,:(G==$(N-G)),($fun(N,N-G,r(N,G),is,T),G-10 ? $nex(N,G-1,r,is,T) : nothing)...)
Expr(:elseif,:(G==$(N-G)),($fun(N,N-G,r(N,N-G),is,T),G-10 ? $nex(N,G-1,r,is,T) : nothing)...)
end
end

Expand Down Expand Up @@ -450,6 +468,7 @@ abstract type AbstractSpinor{V} <: TensorMixed{V} end
@pure log2sub2(N) = log2sub(2N)

for pinor (:Spinor,:AntiSpinor)
dpinor = Symbol(:Dyadic,pinor)
@eval begin
@computed struct $pinor{V,𝕂} <: AbstractSpinor{V}
v::Values{1<<(mdims(V)-1),𝕂}
Expand Down Expand Up @@ -482,6 +501,14 @@ for pinor ∈ (:Spinor,:AntiSpinor)
equal(a::Multivector{V,T},b::$pinor{V,S}) where {V,T,S} = equal(a,Multivector(b))
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,<:$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
N = size(m)[2]
$pinor{V,$pinor{Int(log2(N))≠mdims(V) ? log2sub(N) : V}}(m)
end
$pinor{V,$pinor{W}}(m::Matrix) where {V,W} = $pinor{V}($pinor{W}.(getindex.(Ref(m),:,list(1,size(m)[2]))))
end
end

Expand Down Expand Up @@ -724,6 +751,11 @@ Spinor{V,𝕂}(z::PseudoCouple{V,B,𝕂}) where {V,B,𝕂} = Spinor{V}(imaginary
AntiSpinor{V}(val::PseudoCouple{V,B,𝕂}) where {V,B,𝕂} = AntiSpinor{V,𝕂}(val)
AntiSpinor{V,𝕂}(z::PseudoCouple{V,B,𝕂}) where {V,B,𝕂} = AntiSpinor{V}(imaginary(z),volume(z))

(t::Couple{V,B})(G::Int) where {V,B} = grade(B) == G ? imaginary(t) : iszero(G) ? scalar(t) : Zero(V)
(t::PseudoCouple{V,B})(G::Int) where {V,B} = grade(B) == G ? imaginary(t) : G==mdims(V) ? volume(t) : Zero(V)
(t::Couple{V,B})(::Val{G}) where {V,B,G} = grade(B) == G ? imaginary(t) : iszero(G) ? scalar(t) : Zero(V)
(t::PseudoCouple{V,B})(::Val{G}) where {V,B,G} = grade(B) == G ? imaginary(t) : G==mdims(V) ? volume(t) : Zero(V)

@pure function Base.getproperty(a::Couple{V,B,T},v::Symbol) where {V,B,T}
return if v == :v
getfield(a,:v)
Expand Down Expand Up @@ -984,7 +1016,7 @@ import AbstractTensors: antiabs, antiabs2, geomabs, unit, unitize, unitnorm
import AbstractTensors: value, valuetype, scalar, isscalar, involute, even, odd
import AbstractTensors: vector, isvector, bivector, isbivector, volume, isvolume,
import LinearAlgebra: rank, norm
export gdims, betti, χ
export gdims, betti, χ, unit
export basis, grade, pseudograde, antigrade, hasinf, hasorigin, scalar, norm, unitnorm
export valuetype, scalar, isscalar, vector, isvector, indices, imaginary, unitize, geomabs
export bivector, isbivector, trivector, istrivector, volume, isvolume, antiabs, antiabs2
Expand Down

2 comments on commit 1d2e3bb

@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 updated: JuliaRegistries/General/105384

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.16 -m "<description of version>" 1d2e3bb6620863d0d0bc62c1583ced5d389dd392
git push origin v0.8.16

Please sign in to comment.