Skip to content

Commit e61dc06

Browse files
committed
improved Type{<:Manifold} dispatch
1 parent 7dcf6bd commit e61dc06

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "DirectSum"
22
uuid = "22fd7b30-a8c0-5bf2-aabe-97783860d07c"
33
authors = ["Michael Reed"]
4-
version = "0.5.10"
4+
version = "0.5.11"
55

66
[deps]
77
ComputedFieldTypes = "459fdd68-db75-56b8-8c15-d717a790f88e"

src/DirectSum.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ end
5858
@pure Signature{N,M}(b::BitArray{1},f=0,d=0) where {N,M} = Signature{N,M,bit2int(b[1:N]),f,d}()
5959
@pure Signature{N,M}(b::Array{Bool,1},f=0,d=0) where {N,M} = Signature{N,M}(convert(BitArray{1},b),f,d)
6060
@pure Signature{N,M}(s::String) where {N,M} = Signature{N,M}([k=='-' for ks])
61-
@pure Signature(n::Int,d::Int=0,o::Int=0,s::Bits=zero(Bits)) = Signature{n,doc2m(d,o),s}()
6261
@pure Signature(str::String) = Signature{length(str)}(str)
62+
@pure Signature(n::Int,d::Int=0,o::Int=0,s::Bits=zero(Bits)) = Signature{n,doc2m(d,o),s}()
6363

6464
@pure function Signature{N}(s::String) where N
6565
ms = match(r"[0-9]+",s)
@@ -169,8 +169,10 @@ end
169169
@pure SubManifold{V,N}() where {V,N} = SubManifold{V,N}(UInt(1)<<N-1)
170170
@pure SubManifold{M,N}(b::UInt) where {M,N} = SubManifold{M,N,b}()
171171
SubManifold{M,N}(b::SVector{N}) where {M,N} = SubManifold{M,N}(bit2int(indexbits(ndims(M),b)))
172+
SubManifold{M}(b::UnitRange) where M = SubManifold{M,length(b)}(SVector(b...))
172173
SubManifold{M}(b::Vector) where M = SubManifold{M,length(b)}(SVector(b...))
173174
SubManifold{M}(b::Tuple) where M = SubManifold{M,length(b)}(SVector(b...))
175+
SubManifold{M}(b::SVector) where M = SubManifold{M,length(b)}(b)
174176
SubManifold{M}(b...) where M = SubManifold{M}(b)
175177

176178
for t ((:V,),(:V,:G))
@@ -243,8 +245,14 @@ end
243245
# ==(a::SubManifold{V,G},b::SubManifold{W,G}) where {V,W,G} = interop(==,a,b)
244246

245247
for A (Signature,DiagonalForm,SubManifold)
248+
@eval @pure Manifold(::Type{T}) where T<:$A = T()
246249
for B (Signature,DiagonalForm,SubManifold)
247-
@eval @pure ==(a::$A,b::$B) = (ab) && (ab)
250+
@eval begin
251+
@pure ==(a::$A,b::$B) = (ab) && (ab)
252+
@pure ==(::Type{A},b::$B) where A<:$A = A() == b
253+
@pure ==(a::$A,::Type{B}) where B<:$B = a == B()
254+
@pure ==(::Type{A},::Type{B}) where {A<:$A,B<:$B} = A() == B()
255+
end
248256
end
249257
end
250258

src/generic.jl

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ export valuetype, value, hasinf, hasorigin, isorigin, norm, indices, tangent, is
88
(M::Signature)(b::Int...) = SubManifold{M}(b)
99
(M::DiagonalForm)(b::Int...) = SubManifold{M}(b)
1010
(M::SubManifold)(b::Int...) = SubManifold{supermanifold(M)}(b)
11+
(M::Signature)(b::T) where T<:AbstractVector{Int} = SubManifold{M}(b)
12+
(M::DiagonalForm)(b::T) where T<:AbstractVector{Int} = SubManifold{M}(b)
13+
(M::SubManifold)(b::T) where T<:AbstractVector{Int} = SubManifold{supermanifold(M)}(b)
14+
(M::Signature)(b::T) where T<:AbstractRange{Int} = SubManifold{M}(b)
15+
(M::DiagonalForm)(b::T) where T<:AbstractRange{Int} = SubManifold{M}(b)
16+
(M::SubManifold)(b::T) where T<:AbstractRange{Int} = SubManifold{supermanifold(M)}(b)
1117

1218
@pure Base.ndims(S::SubManifold{M,G}) where {G,M} = isbasis(S) ? ndims(M) : G
1319
@pure grade(V::M) where M<:Manifold{N} where N = N-(isdyadic(V) ? 2 : 1)*diffvars(V)
@@ -50,15 +56,21 @@ const mixedmode = dyadmode
5056
@inline value(::SubManifold,T=Int) = T==Any ? 1 : one(T)
5157
@inline value(m::Simplex,T::DataType=valuetype(m)) = T(valuetype(m),Any) ? convert(T,m.v) : m.v
5258
@inline value_diff(m::T) where T<:TensorTerm = (v=value(m);istensor(v) ? v : m)
53-
@pure isbasis(::SubManifold{V}) where V = typeof(V)<:SubManifold
54-
@pure isbasis(::T) where T<: TensorBundle = false
55-
@pure isbasis(::Simplex) = false
59+
60+
for T (:T,:(Type{T}))
61+
@eval begin
62+
@pure isbasis(::$T) where T<:SubManifold{V} where V = typeof(V)<:SubManifold
63+
@pure isbasis(::$T) where T<:TensorAlgebra = false
64+
@pure isbasis(::$T) where T<:TensorBundle = false
65+
@pure isbasis(::$T) where T<:Simplex = false
66+
@pure bits(b::$T) where T<:SubManifold{V,G,B} where {V,G} where B = B::UInt
67+
@pure UInt(b::$T) where T<:SubManifold{V,G,B} where {V,G} where B = B::UInt
68+
end
69+
end
70+
@pure UInt(m::T) where T<:TensorTerm = UInt(basis(m))
71+
@pure bits(m::T) where T<:TensorTerm = bits(basis(m))
5672
@pure basis(m::SubManifold) = isbasis(m) ? m : SubManifold(m)
5773
@pure basis(m::Simplex{V,G,B} where {V,G}) where B = B
58-
@pure UInt(m::T) where T<:TensorTerm = bits(basis(m))
59-
@pure bits(m::T) where T<:TensorTerm = bits(basis(m))
60-
@pure bits(b::SubManifold{V,G,B} where {V,G}) where B = B::UInt
61-
@pure bits(::Type{SubManifold{V,G,B}} where {V,G}) where B = B
6274
@pure det(s::Signature) = isodd(count_ones(metric(s))) ? -1 : 1
6375
@pure det(s::DiagonalForm) = PROD(diagonalform(s))
6476
@pure Base.abs(s::SubManifold) = isbasis(s) ? Base.sqrt(Base.abs2(s)) : sqrt(abs(det(s)))

0 commit comments

Comments
 (0)