Skip to content

Commit

Permalink
add aqua, fix ambiguities and unbound type parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Jutho committed Jan 5, 2024
1 parent b62a772 commit 9ad2804
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 45 deletions.
26 changes: 18 additions & 8 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "TensorKit"
uuid = "07d1fe3e-3e46-537d-9eac-e9e13d0d4cec"
authors = ["Jutho Haegeman"]
version = "0.12.0"
version = "0.12.1"

[deps]
HalfIntegers = "f0d1745a-41c9-11e9-1dd9-e5d34d218721"
Expand All @@ -14,21 +14,34 @@ TupleTools = "9d95972d-f1c8-5527-a6e0-b4b365fa01f6"
VectorInterface = "409d34a3-91d5-4945-b6ec-7529ddf182d8"
WignerSymbols = "9f57e263-0b3d-5e2e-b1be-24f2bb48858b"

[weakdeps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"

[extensions]
TensorKitChainRulesCoreExt = "ChainRulesCore"

[compat]
Aqua = "0.6, 0.7, 0.8"
ChainRulesCore = "1"
ChainRulesTestUtils = "1"
Combinatorics = "1"
FiniteDifferences = "0.12"
HalfIntegers = "1"
LinearAlgebra = "1"
LRUCache = "1.0.2"
PackageExtensionCompat = "1"
Random = "1"
Strided = "2"
TensorOperations = "4.1"
Test = "1"
TestExtras = "0.2"
TupleTools = "1.1"
VectorInterface = "0.4"
WignerSymbols = "1,2"
julia = "1.6"

[extensions]
TensorKitChainRulesCoreExt = "ChainRulesCore"

[extras]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
ChainRulesTestUtils = "cdddcdb0-9152-4a09-a978-84456f9df70a"
Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"
Expand All @@ -42,7 +55,4 @@ TestExtras = "5ed8adda-3752-4e41-b88a-e8b09835ee3a"
WignerSymbols = "9f57e263-0b3d-5e2e-b1be-24f2bb48858b"

[targets]
test = ["Combinatorics", "HalfIntegers", "LinearAlgebra", "Random", "TensorOperations", "Test", "TestExtras", "WignerSymbols", "ChainRulesCore", "ChainRulesTestUtils", "FiniteDifferences"]

[weakdeps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
test = ["Aqua", "Combinatorics", "HalfIntegers", "LinearAlgebra", "Random", "TensorOperations", "Test", "TestExtras", "WignerSymbols", "ChainRulesCore", "ChainRulesTestUtils", "FiniteDifferences"]
12 changes: 7 additions & 5 deletions src/auxiliary/dicts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,22 @@ struct VectorDict{K,V} <: AbstractDict{K,V}
values::Vector{V}
end
VectorDict{K,V}() where {K,V} = VectorDict{K,V}(Vector{K}(), Vector{V}())
function VectorDict{K,V}(kv) where {K,V}
VectorDict() = VectorDict{Any,Any}()

function VectorDict{K,V}(kvs) where {K,V}
keys = Vector{K}()
values = Vector{V}()
if Base.IteratorSize(kv) !== SizeUnknown()
sizehint!(keys, length(kv))
sizehint!(values, length(kv))
sizehint!(keys, length(kvs))
sizehint!(values, length(kvs))
end
for (k, v) in kv
for (k, v) in kvs
push!(keys, k)
push!(values, v)
end
return VectorDict{K,V}(keys, values)
end
VectorDict(kv::Pair{K,V}...) where {K,V} = VectorDict{K,V}(kv)
VectorDict(kv1::Pair{K,V}, kvs::Pair{K,V}...) where {K,V} = VectorDict{K,V}((kv1, kvs...))
VectorDict(g::Base.Generator) = VectorDict(g...)

Base.length(d::VectorDict) = length(d.keys)
Expand Down
6 changes: 3 additions & 3 deletions src/fusiontrees/fusiontrees.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ function FusionTree{I}(uncoupled::NTuple{N}, coupled=one(I),
_abelianinner(map(s -> convert(I, s),
(uncoupled..., dual(coupled)))))
end
function FusionTree(uncoupled::NTuple{N,I}, coupled::I=one(I),
isdual=ntuple(n -> false, N)) where {I<:Sector,N}
function FusionTree(uncoupled::Tuple{I,Vararg{I}}, coupled::I=one(I),
isdual=ntuple(n -> false, length(uncoupled))) where {I<:Sector}
FusionStyle(I) isa UniqueFusion ||
error("fusion tree requires inner lines if `FusionStyle(I) <: MultipleFusion`")
return FusionTree{I}(uncoupled, coupled, isdual,
Expand Down Expand Up @@ -230,7 +230,7 @@ end
function _abelianinner(outer::Tuple{I,I,I}) where {I<:Sector}
return first((outer...)) == one(I) ? () : throw(SectorMismatch())
end
function _abelianinner(outer::NTuple{N,I}) where {I<:Sector,N}
function _abelianinner(outer::Tuple{I,I,I,I,Vararg{I}}) where {I<:Sector}
c = first(outer[1] outer[2])
return (c, _abelianinner((c, TupleTools.tail2(outer)...))...)
end
9 changes: 6 additions & 3 deletions src/fusiontrees/iterator.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# FusionTreeIterator:
# iterate over fusion trees for fixed coupled and uncoupled sector labels
#==============================================================================#
function fusiontrees(uncoupled::NTuple{N,I}, coupled::I=one(I),
isdual::NTuple{N,Bool}=ntuple(n -> false, Val(N))) where
{N,I<:Sector}
function fusiontrees(uncoupled::NTuple{N,I}, coupled::I,
isdual::NTuple{N,Bool}) where {N,I<:Sector}
return FusionTreeIterator{I,N}(uncoupled, coupled, isdual)
end
function fusiontrees(uncoupled::Tuple{I,Vararg{I}}, coupled::I=one(I)) where {I<:Sector}
isdual = ntuple(n -> false, length(uncoupled))
return fusiontrees(uncoupled, coupled, isdual)
end

struct FusionTreeIterator{I<:Sector,N}
uncoupled::NTuple{N,I}
Expand Down
34 changes: 18 additions & 16 deletions src/sectors/product.jl
Original file line number Diff line number Diff line change
Expand Up @@ -187,27 +187,29 @@ group representations, we have `Irrep[G₁] ⊠ Irrep[G₂] == Irrep[G₁ × G

# grow types from the left using Base.tuple_type_cons
(I1::Type{Trivial}, I2::Type{Trivial}) = Trivial
(I1::Type{Trivial}, I2::Type{<:ProductSector}) = I2
(I1::Type{Trivial}, I2::Type{<:Sector}) = I2
(I1::Type{<:Sector}, I2::Type{<:Trivial}) = I1
(I1::Type{<:Sector}, I2::Type{<:Sector}) = ProductSector{Tuple{I1,I2}}

(I1::Type{<:ProductSector}, I2::Type{Trivial}) = I1
(I1::Type{<:ProductSector}, I2::Type{<:Sector}) = I1 ProductSector{Tuple{I2}}

(::Type{Trivial}, P::Type{ProductSector{T}}) where {T<:SectorTuple} = P
function (I::Type{<:Sector}, ::Type{ProductSector{T}}) where {T<:SectorTuple}
return ProductSector{Base.tuple_type_cons(I, T)}
end

function (::Type{ProductSector{Tuple{I}}},
::Type{ProductSector{T}}) where {I<:Sector,T<:SectorTuple}
return ProductSector{Base.tuple_type_cons(I, T)}
@static if VERSION >= v"1.8"
Base.@assume_effects :foldable function (I1::Type{<:ProductSector},
I2::Type{<:ProductSector})
T1 = I1.parameters[1]
T2 = I2.parameters[1]
return ProductSector{Tuple{T1.parameters...,T2.parameters...}}
end
else
Base.@pure function (I1::Type{<:ProductSector}, I2::Type{<:ProductSector})
T1 = I1.parameters[1]
T2 = I2.parameters[1]
return ProductSector{Tuple{T1.parameters...,T2.parameters...}}
end
end
(I1::Type{<:ProductSector}, I2::Type{<:Sector}) = I1 ProductSector{Tuple{I2}}

function (::Type{ProductSector{T1}},
I2::Type{ProductSector{T2}}) where {T1<:SectorTuple,T2<:SectorTuple}
return Base.tuple_type_head(T1) (ProductSector{Base.tuple_type_tail(T1)} I2)
end
(I1::Type{<:Sector}, I2::Type{Trivial}) = I1
(I1::Type{<:Sector}, I2::Type{<:ProductSector}) = ProductSector{Tuple{I1}} I2
(I1::Type{<:Sector}, I2::Type{<:Sector}) = ProductSector{Tuple{I1,I2}}

function Base.show(io::IO, P::ProductSector)
sectors = P.sectors
Expand Down
7 changes: 4 additions & 3 deletions src/spaces/gradedspace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,13 @@ end

GradedSpace{I}(args...; kwargs...) where {I<:Sector} = Vect[I](args..., kwargs...)

function GradedSpace(dims::Tuple{Vararg{Pair{I,<:Integer}}};
function GradedSpace(dims::Tuple{Pair{I,<:Integer},Vararg{Pair{I,<:Integer}}};
dual::Bool=false) where {I<:Sector}
return Vect[I](dims; dual=dual)
end
function GradedSpace(dims::Vararg{Pair{I,<:Integer}}; dual::Bool=false) where {I<:Sector}
return Vect[I](dims; dual=dual)
function GradedSpace(dim1::Pair{I,<:Integer}, rdims::Vararg{Pair{I,<:Integer}};
dual::Bool=false) where {I<:Sector}
return Vect[I]((dim1, rdims...); dual=dual)
end
function GradedSpace(dims::AbstractDict{I,<:Integer}; dual::Bool=false) where {I<:Sector}
return Vect[I](dims; dual=dual)
Expand Down
19 changes: 16 additions & 3 deletions src/spaces/productspace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,25 @@ same type are allowed.
"""
struct ProductSpace{S<:ElementarySpace,N} <: CompositeSpace{S}
spaces::NTuple{N,S}
ProductSpace{S,N}(spaces::NTuple{N,S}) where {S<:ElementarySpace,N} = new{S,N}(spaces)
end
ProductSpace(spaces::Vararg{S,N}) where {S<:ElementarySpace,N} = ProductSpace{S,N}(spaces)

function ProductSpace{S,N}(spaces::Vararg{S,N}) where {S<:ElementarySpace,N}
return ProductSpace{S,N}(spaces)
end
ProductSpace{S}(spaces) where {S<:ElementarySpace} = ProductSpace{S,length(spaces)}(spaces)

function ProductSpace{S}(spaces::Tuple{Vararg{S}}) where {S<:ElementarySpace}
return ProductSpace{S,length(spaces)}(spaces)
end
ProductSpace{S}(spaces::Vararg{S}) where {S<:ElementarySpace} = ProductSpace{S}(spaces)

function ProductSpace(spaces::Tuple{S,Vararg{S}}) where {S<:ElementarySpace}
return ProductSpace{S,length(spaces)}(spaces)
end
function ProductSpace(space1::S, rspaces::Vararg{S}) where {S<:ElementarySpace}
return ProductSpace((space1, rspaces...))
end

ProductSpace(P::ProductSpace) = P

# Corresponding methods
Expand Down Expand Up @@ -177,7 +190,7 @@ Base.hash(P::ProductSpace, h::UInt) = hash(P.spaces, h)

# Default construction from product of spaces
#---------------------------------------------
(V::Vararg{S}) where {S<:ElementarySpace} = ProductSpace(V)
(V::ElementarySpace...) = ProductSpace(V...)
(P::ProductSpace) = P
function (P1::ProductSpace{S}, P2::ProductSpace{S}) where {S<:ElementarySpace}
return ProductSpace{S}(tuple(P1.spaces..., P2.spaces...))
Expand Down
5 changes: 5 additions & 0 deletions src/tensors/braidingtensor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,11 @@ end

# Fallback cases for planarcontract!
# TODO: implement specialised cases for contracting 0, 1, 3 and 4 indices
function planarcontract!(C::AbstractTensorMap{S}, A::BraidingTensor{S}, pA::Index2Tuple,
B::BraidingTensor{S}, pB::Index2Tuple, α::Number, β::Number,
backend::Backend...) where {S}
return planarcontract!(C, copy(A), pA, copy(B), pB, α, β, backend...)
end
function planarcontract!(C::AbstractTensorMap{S}, A::BraidingTensor{S}, pA::Index2Tuple,
B::AbstractTensorMap{S}, pB::Index2Tuple, α::Number, β::Number,
backend::Backend...) where {S}
Expand Down
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,8 @@ printstyled("Finished all tests in ",
string(round((Tf - Ti) / 60; sigdigits=3)),
" minutes."; bold=true, color=Base.info_color())
println()

@testset "Aqua" verbose = true begin
using Aqua
Aqua.test_all(TensorKit)
end
14 changes: 14 additions & 0 deletions test/sectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,18 @@ println("------------------------------------")
end
end
end

@testset "Deligne product" begin
sectorlist′ = (Trivial, sectorlist...)
for I1 in sectorlist′, I2 in sectorlist′
a = first(smallset(I1))
b = first(smallset(I2))

@constinferred a b
@constinferred a b a
@constinferred a b a b
@constinferred I1 I2
@test typeof(a b) == I1 I2
end
end
end
8 changes: 4 additions & 4 deletions test/spaces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ println("------------------------------------")
@test @constinferred(oneunit(V)) == W == oneunit(typeof(V))
@test @constinferred((V, V)) ==^(2d)
@test_throws SpaceMismatch ((V, V'))
promote_except = ErrorException("promotion of types $(typeof(ℝ^d)) and " *
"$(typeof(ℂ^d)) failed to change any arguments")
@test_throws promote_except ((ℝ^d, ℂ^d))
@test_throws promote_except ((ℝ^d, ℂ^d))
# promote_except = ErrorException("promotion of types $(typeof(ℝ^d)) and " *
# "$(typeof(ℂ^d)) failed to change any arguments")
# @test_throws promote_except (⊕(ℝ^d, ℂ^d))
@test_throws MethodError ((ℝ^d, ℂ^d))
@test @constinferred((V, V)) ==^(2d)
@test @constinferred((V, oneunit(V))) ==^(d + 1)
@test @constinferred((V, V, V, V)) ==^(4d)
Expand Down

0 comments on commit 9ad2804

Please sign in to comment.