Skip to content

Commit 02472f0

Browse files
committed
implemented Orthotope \& Orthogrid
1 parent e896689 commit 02472f0

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/composite.jl

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,3 +682,47 @@ Base.rand(::AbstractRNG,::SamplerType{Chain{V,G,T} where G}) where {V,T} = rand(
682682
Base.rand(::AbstractRNG,::SamplerType{MultiVector}) = rand(MultiVector{rand(Manifold)})
683683
Base.rand(::AbstractRNG,::SamplerType{MultiVector{V}}) where V = MultiVector{V}(DirectSum.orand(svec(ndims(V),Float64)))
684684
Base.rand(::AbstractRNG,::SamplerType{MultiVector{V,T}}) where {V,T} = MultiVector{V}(rand(svec(ndims(V),T)))
685+
686+
export Orthotope, Orthogrid
687+
688+
struct Orthotope{V,T}
689+
min::Chain{V,1,T}
690+
max::Chain{V,1,T}
691+
end
692+
693+
struct Orthogrid{V,T}
694+
x::Orthotope{V,T}
695+
n::Chain{V,1,Int}
696+
s::Chain{V,1,Float64}
697+
end
698+
699+
Orthogrid{V,T}(x,n) where {V,T} = Orthogrid{V,T}(x,n,Chain{V,1}(value(x.max-x.min)./(value(n)-1)))
700+
701+
Base.show(io::IO,t::Orthogrid) = println('(',t.x.min,"):(",t.s,"):(",t.x.max,')')
702+
703+
zeroinf(f) = iszero(f) ? Inf : f
704+
705+
(::Base.Colon)(min::Chain{V,1,T},max::Chain{V,1,T}) where {V,T} = Orthotope{V,T}(min,max)
706+
(::Base.Colon)(min::Chain{V,1,T},step::Chain{V,1,T},max::Chain{V,1,T}) where {V,T} = Orthogrid{V,T}(min:max,Chain{V,1}(Int.(round.(value(max-min)./zeroinf.(value(step))))+1),step)
707+
708+
Base.iterate(t::Orthogrid) = (getindex(t,1),1)
709+
Base.iterate(t::Orthogrid,state) = (s=state+1; slength(t) ? (getindex(t,s),s) : nothing)
710+
@pure Base.eltype(::Type{Orthogrid{V,T}}) where {V,T} = Chain{V,1,T,ndims(V)}
711+
@pure Base.step(t::Orthogrid) = value(t.s)
712+
@pure Base.size(t::Orthogrid) = value(t.n).data
713+
@pure Base.length(t::Orthogrid) = prod(size(t))
714+
@pure Base.lastindex(t::Orthogrid) = length(t)
715+
@pure Base.lastindex(t::Orthogrid,i::Int) = size(t)[i]
716+
@pure Base.getindex(t::Orthogrid,i::CartesianIndex) = getindex(t,i.I...)
717+
@pure Base.getindex(t::Orthogrid{V},i::Vararg{Int}) where V = Chain{V,1}(value(t.x.min)+(SVector(i)-1).*step(t))
718+
719+
Base.IndexStyle(::Orthogrid) = IndexCartesian()
720+
function Base.getindex(A::Orthogrid, I::Int)
721+
Base.@_inline_meta
722+
@inbounds getindex(A, Base._to_subscript_indices(A, I)...)
723+
end
724+
Base._to_subscript_indices(A::Orthogrid, i::Integer) = (Base.@_inline_meta; Base._unsafe_ind2sub(A, i))
725+
function Base._ind2sub(A::Orthogrid, ind)
726+
Base.@_inline_meta
727+
Base._ind2sub(axes(A), ind)
728+
end

0 commit comments

Comments
 (0)