Skip to content

Commit

Permalink
revised TensorField implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
chakravala committed Jun 27, 2024
1 parent d0023f4 commit 708d1f6
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Additional packages such as `Adapode` build on the `TensorField` concept by gene
Many of these methods can automatically generalize to higher dimensional manifolds and are compatible with discrete differential geometry.

```
_________ __ ________
_________ __ __________
\_ ___ \_____ ________/ |______ ____ \\ /
/ \ \/\__ \\_ __ \ __\__ \ / \ \\ /
\ \____/ __ \| | \/| | / __ \| | \ \\ /
Expand Down
20 changes: 13 additions & 7 deletions src/Cartan.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Cartan
#
# https://github.com/chakravala
# https://crucialflow.com
# _________ __ ________
# _________ __ __________
# \_ ___ \_____ ________/ |______ ____ \\ /
# / \ \/\__ \\_ __ \ __\__ \ / \ \\ /
# \ \____/ __ \| | \/| | / __ \| | \ \\ /
Expand Down Expand Up @@ -338,13 +338,19 @@ struct TensorField{B,F,N,PA,GA} <: GlobalFiber{LocalTensor{B,F},N}
dom::PA
cod::Array{F,N}
met::GA
TensorField(id::Int,dom::PA,cod::Array{F,N},met::GA=Global{N}(InducedMetric())) where {N,P,F,G,PA<:AbstractArray{P,N},GA<:AbstractArray{G,N}} = new{Coordinate{P,G},F,N,PA,GA}(id,dom,cod,met)
TensorField(id::Int,dom::PA,cod::Vector{F},met::GA=Global{N}(InducedMetric())) where {F,G,PA<:ChainBundle,GA<:AbstractVector{G}} = new{Coordinate{eltype(value(points(dom))),G},F,1,PA,GA}(id,dom,cod,met)
TensorField(dom::PA,cod::Array{F,N},met::GA=Global{N}(InducedMetric())) where {N,P,F,G,PA<:AbstractArray{P,N},GA<:AbstractArray{G,N}} = new{Coordinate{P,G},F,N,PA,GA}((global grid_id+=1),dom,cod,met)
TensorField(dom::MA,cod::Array{F,N}) where {F,N,P,G,PA<:AbstractArray{P,N},GA<:AbstractArray{G,N},MA<:GridManifold{P,G,N,PA,GA}} = new{Coordinate{P,G},F,N,PA,GA}(dom.id,dom.dom,cod,dom.cod)
TensorField(dom::PA,cod::Vector{F},met::GA=Global{N}(InducedMetric())) where {F,G,PA<:ChainBundle,GA<:AbstractVector{G}} = new{Coordinate{eltype(value(points(dom))),G},F,1,PA,GA}((global grid_id+=1),dom,cod,met)
function TensorField(id::Int,dom::PA,cod::Array{F,N},met::GA=Global{N}(InducedMetric())) where {N,P,F,G,PA<:AbstractArray{P,N},GA<:AbstractArray{G,N}}
new{Coordinate{P,G},F,N,PA,GA}(id,dom,cod,met)
end
function TensorField(id::Int,dom::PA,cod::Vector{F},met::GA=Global{N}(InducedMetric())) where {F,G,PA<:ChainBundle,GA<:AbstractVector{G}}
new{Coordinate{eltype(value(points(dom))),G},F,1,PA,GA}(id,dom,cod,met)
end
end

TensorField(id::Int,dom,cod::Array,met::GlobalFiber) = TensorField(id,dom,cod,fiber(met))
TensorField(dom::GridManifold,cod::Array) = TensorField(dom.id,base(dom),cod,fiber(dom))
TensorField(dom::AbstractArray{B,N} where B,cod::Array{F,N} where F,met::AbstractArray=Global{N}(InducedMetric())) where N = TensorField((global grid_id+=1),dom,cod,fiber(met))
TensorField(dom::ChainBundle,cod::Vector,met::AbstractVector=Global{1}(InducedMetric())) = TensorField((global grid_id+=1),dom,cod,met)

#const ParametricMesh{B,F,PA<:AbstractVector{<:Chain},GA} = TensorField{B,F,1,PA,GA}
const MeshFunction{B,F<:AbstractReal,BA<:ChainBundle,GA} = TensorField{B,F,1,BA,GA}
const ElementFunction{B,F<:AbstractReal,PA<:AbstractVector,GA} = TensorField{B,F,1,PA,GA}
Expand Down Expand Up @@ -591,7 +597,7 @@ end
for fun (:-,:!,:~,:real,:imag,:conj,:deg2rad)
@eval Base.$fun(t::TensorField) = TensorField(domain(t), $fun.(codomain(t)))
end
for fun (:exp,:log,:sinh,:cosh,:abs,:sqrt,:cbrt,:cos,:sin,:tan,:cot,:sec,:csc,:asec,:acsc,:sech,:csch,:asech,:tanh,:coth,:asinh,:acosh,:atanh,:acoth,:asin,:acos,:atan,:acot,:sinc,:cosc,:abs2)
for fun (:exp,:log,:sinh,:cosh,:abs,:sqrt,:cbrt,:cos,:sin,:tan,:cot,:sec,:csc,:asec,:acsc,:sech,:csch,:asech,:tanh,:coth,:asinh,:acosh,:atanh,:acoth,:asin,:acos,:atan,:acot,:sinc,:cosc,:abs2)#:inv
@eval Base.$fun(t::TensorField) = TensorField(domain(t), $fun.(codomain(t),ref(metrictensor(t))))
end
for fun (:reverse,:involute,:clifford,:even,:odd,:scalar,:vector,:bivector,:volume,:value,:complementleft)
Expand Down
64 changes: 56 additions & 8 deletions src/diffgeo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,16 @@ isclosed(t::IntervalMap) = norm(codomain(t)[end]-codomain(t)[1]) ≈ 0

export Grid

struct Grid{N,T,A<:AbstractArray{T,N}}
struct Grid{N,T,A<:AbstractArray{T,N}}#,G}
v::A
#g::G
#Grid(v::A,g::G) where {N,T,A<:AbstractArray{T,N},G} = new{N,T,A,G}(v,g)
end

#Grid(v::A) where {N,T,A<:AbstractArray{T,N}} = Grid(v,Global{N}(InducedMetric()))
#Grid(v::GridManifold{<:Real}) = Grid(points(v))
#Grid(v::GridManifold) = Grid(points(v),fiber(metrictensor(v)))

Base.size(m::Grid) = size(m.v)

@generated function Base.getindex(g::Grid{M},j::Int,::Val{N},i::Vararg{Int}) where {M,N}
Expand Down Expand Up @@ -87,6 +93,22 @@ Grassmann.curl(t::TensorField) = ⋆d(t)
Grassmann.d(t::TensorField) = TensorField(fromany((t)Chain(t)))
Grassmann.d(t::GlobalSection) = gradient(t)
Grassmann.(t::TensorField) = TensorField(fromany(Chain(t)(t)))
Grassmann.d(t::ScalarField) = gradient(t)
#Grassmann.∂(t::ScalarField) = gradient(t)
#Grassmann.∂(t::VectorField) = TensorField(domain(t), sum.(value.(codomain(t))))
#=function Grassmann.∂(t::VectorField{G,B,<:Chain{V},N,T} where {B,N,T}) where {G,V}
n = mdims(V)
TensorField(domain(t), Real.(Chain{V,G}(ones(Values{binomial(n,G),Int})).⋅codomain(t)))
end
function Grassmann.∂(t::GradedField{G,B,<:Chain{V}} where B) where {G,V}
n = mdims(V)
TensorField(domain(t), (Chain{V,G}(ones(Values{binomial(n,G),Int})).⋅codomain(t)))
end=#

@generated function dvec(t::TensorField{B,<:Chain{V,G} where {V,G}} where B)
V = Manifold(fibertype(t)); N = mdims(V)
Expr(:.,:(Chain{$V,1}),Expr(:tuple,[:(gradient(getindex.(t,$i))) for i list(1,N)]...))
end

@generated function Grassmann.d(t::TensorField{B,<:Chain{V,G,<:Chain} where {V,G}} where B)
V = Manifold(fibertype(t)); N = mdims(V)
Expand Down Expand Up @@ -122,28 +144,28 @@ LinearAlgebra.dot(t::TensorField,::Derivation) = TensorField(fromany(Grassmann.c

function Base.:*(n::Submanifold,t::TensorField)
if istangent(n)
gradient(t,indices(n)[1])
gradient(t,Val(indices(n)[1]))
else
TensorField(domain(t), (Ref(n).*codomain(t)))
end
end
function Base.:*(t::TensorField,n::Submanifold)
if istangent(n)
gradient(t,indices(n)[1])
gradient(t,Val(indices(n)[1]))
else
TensorField(domain(t), (codomain(t).*Ref(n)))
end
end
function LinearAlgebra.dot(n::Submanifold,t::TensorField)
if istangent(n)
gradient(t,indices(n)[1])
gradient(t,Val(indices(n)[1]))
else
TensorField(domain(t), dot.(Ref(n),codomain(t)))
end
end
function LinearAlgebra.dot(t::TensorField,n::Submanifold)
if istangent(n)
gradient(t,indices(n)[1])
gradient(t,Val(indices(n)[1]))
else
TensorField(domain(t), dot.(codomain(t),Ref(n)))
end
Expand All @@ -155,15 +177,23 @@ for fun ∈ (:_slow,:_fast)
function $grad(f::IntervalMap,d::AbstractVector=$cd(basepoints(f)))
TensorField(domain(f), $cd(codomain(f),d))
end
function $grad(f::TensorField{B,F,N,<:AbstractArray} where {B,F,N},d::AbstractArray=$cd(basepoints(f)))
function $grad(f::TensorField{B,F,N,<:AbstractArray} where {B,F,N},d::AbstractArray=$cd(base(f)))
TensorField(domain(f), $cd(Grid(codomain(f)),d))
end
function $grad(f::IntervalMap,::Val{1},d::AbstractVector=$cd(basepoints(f)))
TensorField(domain(f), $cd(codomain(f),d))
end
function $grad(f::TensorField{B,F,Nf,<:RealRegion} where {B,F,Nf},n::Val{N},d::AbstractArray=$cd(basepoints(f).v[N])) where N
function $grad(f::TensorField{B,F,Nf,<:RealRegion,<:Global{Nf,<:InducedMetric}} where {B,F,Nf},n::Val{N},d::AbstractArray=$cd(basepoints(f).v[N])) where N
TensorField(domain(f), $cd(Grid(codomain(f)),n,d))
end
function $grad(f::TensorField{B,F,Nf,<:RealRegion} where {B,F,Nf},n::Val{N},d::AbstractArray=$cd(basepoints(f).v[N])) where N
l = size(basepoints(f))
dg = sqrt.(getindex.(metrictensor(f),N+1,N+1))
@threads for i l[1]; for j l[2]
dg[i,j] *= d[isone(N) ? i : j]
end end
TensorField(domain(f), $cd(Grid(codomain(f)),n,dg))
end
function $grad(f::TensorField{B,F,N,<:AbstractArray} where {B,F,N},n::Val,d::AbstractArray=$cd(basepoints(f),n))
TensorField(domain(f), $cd(Grid(codomain(f)),n,d))
end
Expand Down Expand Up @@ -232,6 +262,13 @@ for fun ∈ (:_slow,:_fast)
end end
return d
end
function $cd(f::Grid{2},n::Val{N},dt::AbstractMatrix,l::Tuple=size(f.v)) where N
d = Array{eltype(f.v),2}(undef,l...)
@threads for i 1:l[1]; for j 1:l[2]
d[i,j] = $cd(f,l[N],n,i,j)/dt[i,j]
end end
return d
end
function $cd(f::Grid{2},n::Val{N},l::Tuple=size(f.v)) where N
d = Array{eltype(f.v),2}(undef,l...)
@threads for i 1:l[1]; for j 1:l[2]
Expand Down Expand Up @@ -260,6 +297,13 @@ for fun ∈ (:_slow,:_fast)
end end end
return d
end
function $cd(f::Grid{3},n::Val{N},dt::AbstractArray,l::Tuple=size(f.v)) where N
d = Array{eltype(f.v),3}(undef,l...)
@threads for i 1:l[1]; for j 1:l[2]; for k 1:l[3]
d[i,j,k] = $cd(f,l[N],n,i,j,k)/dt[i,j,k]
end end end
return d
end
function $cd(f::Grid{3},n::Val{N},l::Tuple=size(f.v)) where N
d = Array{eltype(f.v),3}(undef,l...)
@threads for i 1:l[1]; for j 1:l[2]; for k 1:l[3]
Expand All @@ -269,9 +313,11 @@ for fun ∈ (:_slow,:_fast)
end
$cd(f::Grid{1},l::Tuple,i::Int) = $cd(f,l[1],Val(1),i)
@generated function $cd(f::Grid{N},l::Tuple,i::Vararg{Int}) where N
:(Chain($([:($$cd(f,l[$n],Val($n),i...)) for n 1:N]...)))
:(Chain($([:($$cd(f,l[$n],Val($n),i...)) for n list(1,N)]...)))
end
$cd(f::RealRegion) = ProductSpace($cd.(f.v))
$cd(f::GridManifold{P,G,N,<:RealRegion,<:Global{N,<:InducedMetric}}) where {P,G,N} = ProductSpace($cd.(base(f).v))
$cd(f::GridManifold{P,G,N,<:RealRegion}) where {P,G,N} = applymetric.($cd(base(f)),fiber(f))
function $cd(f::AbstractRange,l::Tuple=size(f))
d = Vector{eltype(f)}(undef,l[1])
@threads for i 1:l[1]
Expand All @@ -289,6 +335,8 @@ for fun ∈ (:_slow,:_fast)
end
end

applymetric(f::Chain{V,G},g::DiagonalOperator{V,<:Multivector}) where {V,G} = Chain{V,G}(value(f)./sqrt.(value(value(g)(Val(G)))))

function centraldiff_slow(f::Grid,l::Int,n::Val{N},i::Vararg{Int}) where N #l=size(f)[N]
if isone(i[N])
18f[1,n,i...]-9f[2,n,i...]+2f[3,n,i...]-11f.v[i...]
Expand Down

2 comments on commit 708d1f6

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

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.2.0 -m "<description of version>" 708d1f6ef6861acf44ae04eb1ccae85d0430335b
git push origin v0.2.0

Also, note the warning: This looks like a new registration that registers version 0.2.0.
Ideally, you should register an initial release with 0.0.1, 0.1.0 or 1.0.0 version numbers
This can be safely ignored. However, if you want to fix this you can do so. Call register() again after making the fix. This will update the Pull request.

Please sign in to comment.