Skip to content

Commit

Permalink
refined some syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
chakravala committed Jan 25, 2025
1 parent 45d04f9 commit f5a784f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 83 deletions.
8 changes: 0 additions & 8 deletions src/Grassmann.jl
Original file line number Diff line number Diff line change
Expand Up @@ -562,14 +562,6 @@ function __init__()
Chain(m::StaticArrays.SMatrix{N,N}) where N = Chain{Submanifold(N),1}(m)
Chain{V,G}(m::StaticArrays.SMatrix{N,N}) where {V,G,N} = Chain{V,G}(Chain{V,G}.(getindex.(Ref(m),:,StaticArrays.SVector{N}(1:N))))
Chain{V,G,<:Chain{W,G}}(m::StaticArrays.SMatrix{M,N}) where {V,W,G,M,N} = Chain{V,G}(Chain{W,G}.(getindex.(Ref(m),:,StaticArrays.SVector{N}(1:N))))
#Base.log(A::Chain{V,G,<:Chain{V,G}}) where {V,G} = Chain{V,G}(log(StaticArrays.SMatrix(A)))
LinearAlgebra.eigvals(A::Chain{V,G,<:Chain{V,G}}) where {V,G} = Chain(Values{binomial(mdims(V),G)}(LinearAlgebra.eigvals(StaticArrays.SMatrix(A))))
LinearAlgebra.eigvecs(A::Chain{V,G,<:Chain{V,G}}) where {V,G} = Chain(Chain.(Values{binomial(mdims(A),G)}.(getindex.(Ref(LinearAlgebra.eigvecs(StaticArrays.SMatrix(A))),:,list(1,binomial(mdims(A),G))))))
function LinearAlgebra.eigen(A::Chain{V,G,<:Chain{V,G}}) where {V,G}
E,N = eigen(StaticArrays.SMatrix(A)),binomial(mdims(V),G)
e = Chain(Chain.(Values{N}.(getindex.(Ref(E.vectors),:,list(1,N)))))
Proj(e,Chain(Values{N}(E.values)))
end
end
@require Meshes = "eacbb407-ea5a-433e-ab97-5258b1ca43fa" begin
Meshes.Point(t::Values) = Meshes.Point(Tuple(t.v))
Expand Down
89 changes: 24 additions & 65 deletions src/composite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ export exph, log_fast, logh_fast, pseudoexp, pseudolog, pseudometric, pseudodot,
export pseudoabs, pseudoabs2, pseudosqrt, pseudocbrt, pseudoinv, pseudoscalar
export pseudocos, pseudosin, pseudotan, pseudocosh, pseudosinh, pseudotanh
export coabs, coabs2, cosqrt, cocbrt, coinv, coscalar, coexp, colog, cometric, codot, @co
export cocos, cosin, cotan, cocosh, cosinh, cotanh

export vandermonde, volumes, detsimplex, submesh
export cocos, cosin, cotan, cocosh, cosinh, cotanh, vandermonde, volumes, submesh

## exponential & logarithm function

Expand Down Expand Up @@ -740,8 +738,8 @@ end
else
x,y,xy = Grassmann.Cramer(N-1,1)
mid = [:(signscalar(($(x[i])(v-x1)$(y[end-i]))/d)) for i list(1,N-2)]
out = Values(:(signscalar((v∧∧(vectors(t,v)))/d)),mid...,:(signscalar(($(x[end])(v-x1))/d)))
return Expr(:block,:(T=vectors(t)),:((x1,y1)=@inbounds (t[1],T[end])),xy...,
out = Values(:(signscalar((v∧∧(affineframe(t,v)))/d)),mid...,:(signscalar(($(x[end])(v-x1))/d)))
return Expr(:block,:(T=value(affineframe(t))),:((x1,y1)=@inbounds (t[1],T[end])),xy...,
:($(x[end])=$(x[end-1])T[end-1];d=$(x[end])T[end]),ands(out))
end
end
Expand Down Expand Up @@ -836,19 +834,17 @@ function vandermondeinterp(x,y,V,grid) # grid=384
return coef,xp,yp # coefficients, interpolation
end

@generated function vectors(t,c=columns(t))
v = Expr(:tuple,[:(M.(p[c[$i]]-A)) for i list(2,mdims(t))]...)
quote
p = points(t)
M,A = (Manifold(p)),p[c[1]]
Chain{M,1}.($(Expr(:.,:Values,v)))
end
end
@pure list(a::Int,b::Int) = Values{max(0,b-a+1),Int}(a:b...)
@pure evens(a::Int,b::Int) = Values{((b-a)÷2)+1,Int}(a:2:b...)
vectors(x::Values{N,<:Chain{V}},y=x[1]) where {N,V} = (V).(x[list(2,N)].-y)
vectors(x::Chain{V,1},y=x[1]) where V = vectors(value(x),y)
#point(x,y=x[1]) = y∧∧(vectors(x))
affineframe(x::Values{1,<:Chain{V,G,T,N}},y=nothing) where {V,G,T,N} = Values{0,Chain{↓(V),G,T,N-1}}()
@generated function affineframe(x::Values{N,<:Chain{V}},y=x[1]) where {N,V}
M = :(V($(list(2,N)...)))
:(TensorOperator(Chain{$M,1}((V).(x[$(list(2,N))].-y))))
end
affineframe(x::Chain{V,1},y=x[1]) where V = affineframe(value(x),y)
export affineframe
const vectors = affineframe
#point(x,y=x[1]) = y∧∧(affineframe(x))

signscalar(x::Submanifold{V,0} where V) = true
signscalar(x::Single{V,0} where V) = !signbit(value(x))
Expand All @@ -858,56 +854,37 @@ signscalar(x::Chain{V,0} where V) = !signbit(@inbounds x[1])
signscalar(x::Multivector) = isscalar(x) && !signbit(value(scalar(x)))
ands(x,i=length(x)-1) = i 0 ? Expr(:&&,x[end-i],ands(x,i-1)) : x[end-i]

function Base.findfirst(P,t::Vector{<:Chain{V,1,<:Chain}} where V)
function Base.findfirst(P,t::AbstractVector{<:Chain{V,1,<:Chain}} where V)
for i 1:length(t)
@inbounds P t[i] && (return i)
end
return 0
end
function Base.findfirst(P,t::ChainBundle)
p = points(t)
for i 1:length(t)
P p[t[i]] && (return i)
end
return 0
end
function Base.findlast(P,t::Vector{<:Chain{V,1,<:Chain}} where V)
function Base.findlast(P,t::AbstractVector{<:Chain{V,1,<:Chain}} where V)
for i length(t):-1:1
@inbounds P t[i] && (return i)
end
return 0
end
function Base.findlast(P,t::ChainBundle)
p = points(t)
for i length(t):-1:1
P p[t[i]] && (return i)
end
return 0
end
Base.findall(P,t) = findall(P .∈ getindex.(points(t),value(t)))
Base.findall(P,t::AbstractVector{<:Chain{V,1,<:Chain}} where V) = findall(P .∈ t)

edgelength(e) = (v=points(e)[value(e)]; Real(abs(v[2]-v[1])))
volumes(m,dets) = Real.(abs.(dets))
volumes(m) = mdims(Manifold(m))2 ? Real.(abs.(detsimplex(m))) : edgelength.(value(m))
detsimplex(m::Vector{<:Chain{V}}) where V = det(m)/factorial(mdims(V)-1)
detsimplex(m::ChainBundle) = detsimplex(value(m))
mean(m::T) where T<:AbstractVector{<:Chain} = sum(m)/length(m)
mean(m::T) where T<:Values = sum(m)/length(m)
mean(m::AbstractVector) = sum(m)/length(m)
mean(m::Values{N}) where N = sum(m)/N
mean(m::Chain{V,1,<:Chain} where V) = mean(value(m))
barycenter(m::Values{N,<:Chain}) where N = (s=sum(m);@inbounds s/s[1])
barycenter(m::Vector{<:Chain}) = (s=sum(m);@inbounds s/s[1])
barycenter(m::AbstractVector{<:Chain}) = (s=sum(m);@inbounds s/s[1])
barycenter(m::Chain{V,1,<:Chain} where V) = barycenter(value(m))
curl(m::FixedVector{N,<:Chain{V}} where N) where V = curl(Chain{V,1}(m))
curl(m::Values{N,<:Chain{V}} where N) where V = curl(Chain{V,1}(m))
curl(m::T) where T<:TensorAlgebra = Manifold(m)(∇)×m
LinearAlgebra.det(t::Chain{V,1,<:Chain} where V) = !(t)
LinearAlgebra.det(m::Vector{<:Chain{V}}) where V = .!(m)
LinearAlgebra.det(m::ChainBundle) = .!(m)
(m::ChainBundle) = (value(m))
function (m::Vector{<:Chain{V}}) where V
function (m::DenseVector{<:Chain{V}}) where V
p = points(m); pm = p[m]
if mdims(p)>mdims(V)
.∧(vectors.(pm))
.∧(affineframe.(pm))
else
Chain{↓(Manifold(V)),mdims(V)-1}.(value.(.∧(pm)))
end
Expand Down Expand Up @@ -953,31 +930,13 @@ function refinemesh!(::R,p::ChainBundle{W},e,t,η,_=nothing) where {W,R<:Abstrac
end
end

const array_cache = (Array{T,2} where T)[]
array(m::Vector{<:Chain}) = [m[i][j] for i1:length(m),jlist(1,mdims(Manifold(m)))]
function array(m::ChainBundle{V,G,T,B} where {V,G,T}) where B
for k length(array_cache):B
push!(array_cache,Array{Any,2}(undef,0,0))
end
isempty(array_cache[B]) && (array_cache[B] = array(value(m)))
return array_cache[B]
end
function array!(m::ChainBundle{V,G,T,B} where {V,G,T}) where B
length(array_cache) B && (array_cache[B] = Array{Any,2}(undef,0,0))
end
array(m::ChainBundle) = array(value(m))
array!(m::ChainBundle) = nothing

const submesh_cache = (Array{T,2} where T)[]
submesh(m) = [m[i][j] for i1:length(m),jlist(2,mdims(Manifold(m)))]
function submesh(m::ChainBundle{V,G,T,B} where {V,G,T}) where B
for k length(submesh_cache):B
push!(submesh_cache,Array{Any,2}(undef,0,0))
end
isempty(submesh_cache[B]) && (submesh_cache[B] = submesh(value(m)))
return submesh_cache[B]
end
function submesh!(m::ChainBundle{V,G,T,B} where {V,G,T}) where B
length(submesh_cache) B && (submesh_cache[B] = Array{Any,2}(undef,0,0))
end
submesh(m::ChainBundle) = submesh(value(m))
submesh!(m::ChainBundle) = nothing

for op (:div,:rem,:mod,:mod1,:fld,:fld1,:cld,:ldexp)
@eval begin
Expand Down
45 changes: 37 additions & 8 deletions src/forms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -527,10 +527,17 @@ struct TensorOperator{V,W,T<:TensorAlgebra{V,<:TensorAlgebra{W}}} <: TensorNeste
TensorOperator{V,W}(t::T) where {V,W,T<:TensorAlgebra{V,<:TensorAlgebra{W}}} = new{V,W,T}(t)
TensorOperator{V}(t::T) where {V,W,T<:TensorAlgebra{V,<:TensorAlgebra{W}}} = new{V,W,T}(t)
TensorOperator(t::T) where {V,W,T<:TensorAlgebra{V,<:TensorAlgebra{W}}} = new{V,W,T}(t)
TensorOperator(t::T) where {V,W,G,T<:Chain{V,G,<:TensorAlgebra{W},1}} = new{V,W,T}(t)
end

const Endomorphism{V,T<:TensorAlgebra{V,<:TensorAlgebra{V}}} = TensorOperator{V,V,T}
Endomorphism(t::TensorAlgebra{V,<:TensorAlgebra{V}}) where V = TensorOperator{V,V}(t)
for op (:TensorOperator,:Endomorphism)
@eval $op(t::Vararg{Chain{V,G,T,N},N}) where {V,G,T,N} = $op(Chain{V,G}(t...))
for typ (:Spinor,:CoSpinor,:Multivector)
@eval $op(t::Vararg{$typ{V,T,N},N}) where {V,T,N} = $op($typ{V}(t...))
end
end

(T::TensorOperator{V})(x::TensorAlgebra{V}) where V = contraction(T,x)
function (T::TensorOperator{V,W})(x::TensorAlgebra{V},y::TensorAlgebra{W}) where {V,W}
Expand All @@ -550,6 +557,7 @@ 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]
getindex(t::TensorOperator,i::Int) = value(t.v)[i]
Base.lastindex(t::TensorOperator) = lastindex(value(t))
Base.transpose(t::TensorOperator) = TensorOperator(transpose(value(t)))
scalar(m::Endomorphism) = tr(m)/length(value(m))
LinearAlgebra.tr(m::Endomorphism) = tr(value(m))
Expand Down Expand Up @@ -582,17 +590,18 @@ TensorOperator(m::AbstractMatrix) = TensorOperator{Submanifold.(size(m))...}(m)
TensorOperator{V,W}(m::AbstractMatrix) where {V,W} = TensorOperator(Chain{V}(Chain{W,1}.(getindex.(Ref(m),:,list(1,mdims(V))))))

SpectralOperator(t::Endomorphism) = eigen(t)
@generated function DiagonalOperator(t::Endomorphism{V,<:Chain{V,G}}) where {V,G}
Expr(:call,:DiagonalOperator,Expr(:call,:(Chain{V,G}),[:(t[$i,$i]) for i list(1,binomial(mdims(V),G))]...))
DiagonalOperator(t::Endomorphism) = DiagonalOperator(LinearAlgebra.diag(t))
@generated function LinearAlgebra.diag(t::Endomorphism{V,<:Chain{V,G}}) where {V,G}
Expr(:call,:(Chain{V,G}),[:(t[$i,$i]) for i list(1,binomial(mdims(V),G))]...)
end
@generated function DiagonalOperator(t::Endomorphism{V,<:Spinor{V}}) where V
Expr(:call,:DiagonalOperator,Expr(:call,:(Spinor{V}),[:(t[$i,$i]) for i list(1,binomial(mdims(V),G))]...))
@generated function LinearAlgebra.diag(t::Endomorphism{V,<:Spinor{V}}) where V
Expr(:call,:(Spinor{V}),[:(t[$i,$i]) for i list(1,binomial(mdims(V),G))]...)
end
@generated function DiagonalOperator(t::Endomorphism{V,<:AntiSpinor{V}}) where V
Expr(:call,:DiagonalOperator,Expr(:call,:(AntiSpinor{V}),[:(t[$i,$i]) for i list(1,binomial(mdims(V),G))]...))
@generated function LinearAlgebra.diag(t::Endomorphism{V,<:AntiSpinor{V}}) where V
Expr(:call,:(AntiSpinor{V}),[:(t[$i,$i]) for i list(1,binomial(mdims(V),G))]...)
end
@generated function DiagonalOperator(t::Endomorphism{V,<:Multivector{V}}) where V
Expr(:call,:DiagonalOperator,Expr(:call,:(Multivector{V}),[:(t[$i,$i]) for i list(1,1<<mdims(V))]...))
@generated function LinearAlgebra.diag(t::Endomorphism{V,<:Multivector{V}}) where V
Expr(:call,:(Multivector{V}),[:(t[$i,$i]) for i list(1,1<<mdims(V))]...)
end

_axes(t::TensorOperator) = (Base.OneTo(length(t.v)),Base.OneTo(length(t.v)))
Expand Down Expand Up @@ -1494,3 +1503,23 @@ isinduced(::Type{<:InducedMetric}) = true
@inline Base.log(t::Real,g::InducedMetric) = Base.log(t)
@inline Base.log(t::Complex,g::InducedMetric) = Base.log(t)

# other

affineframe(x::TensorOperator,y=x[1]) = affineframe(value(value(x)),y)
mean(m::TensorOperator) = mean(value(value(m)))
barycenter(m::TensorOperator) = barycenter(value(value(m)))
curl(m::TensorAlgebra) = Manifold(m)(∇)×m
Base.findall(P,t::AbstractVector{<:TensorOperator}) = findall(P .∈ t)
function Base.findfirst(P,t::AbstractVector{<:TensorOperator})
for i 1:length(t)
@inbounds P value(t[i]) && (return i)
end
return 0
end
function Base.findlast(P,t::AbstractVector{<:TensorOperator})
for i length(t):-1:1
@inbounds P value(t[i]) && (return i)
end
return 0
end

6 changes: 4 additions & 2 deletions src/multivectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,15 @@ end

@pure Manifold(::ChainBundle{V}) where V = V
@pure Manifold(::Type{<:ChainBundle{V}}) where V = V
@pure Manifold(::Vector{<:Chain{V}}) where V = V
@pure Manifold(::AbstractVector{<:Chain{V}}) where V = V
@pure Manifold(::Type{<:AbstractVector{<:Chain{V}}}) where V = V
@pure LinearAlgebra.rank(M::ChainBundle{V,G} where V) where G = G
@pure grade(::ChainBundle{V}) where V = grade(V)
@pure antigrade(::ChainBundle{V}) where V = antigrade(V)
@pure AbstractTensors.mdims(::ChainBundle{V}) where V = mdims(V)
@pure AbstractTensors.mdims(::Type{T}) where T<:ChainBundle{V} where V = mdims(V)
@pure AbstractTensors.mdims(::Vector{<:Chain{V}}) where V = mdims(V)
@pure AbstractTensors.mdims(::AbstractVector{<:Chain{V}}) where V = mdims(V)
@pure AbstractTensors.mdims(::Type{<:AbstractVector{<:Chain{V}}}) where V = mdims(V)
@pure Base.parent(::ChainBundle{V}) where V = isbundle(V) ? parent(V) : V
@pure Base.parent(::Vector{<:Chain{V}}) where V = isbundle(V) ? parent(V) : V
@pure DirectSum.supermanifold(m::ChainBundle{V}) where V = V
Expand Down

2 comments on commit f5a784f

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

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.29 -m "<description of version>" f5a784f0a90551f3178338a4560e9ef2fde66dac
git push origin v0.8.29

Please sign in to comment.