|
45 | 45 | Return the type of vector that stores the data of a tensor. |
46 | 46 | """ storagetype |
47 | 47 |
|
48 | | -similarstoragetype(TT::Type{<:AbstractTensorMap}) = similarstoragetype(TT, scalartype(TT)) |
| 48 | +# storage type determination and promotion - hooks for specializing |
| 49 | +# the default implementation tries to leverarge inference and `similar` |
| 50 | +@doc """ |
| 51 | + similarstoragetype(t, [T = scalartype(t)]) -> Type{<:DenseVector{T}} |
| 52 | + similarstoragetype(TT, [T = scalartype(t)]) -> Type{<:DenseVector{T}} |
| 53 | + similarstoragetype(A, [T = scalartype(t)]) -> Type{<:DenseVector{T}} |
| 54 | + similarstoragetype(D, [T = scalartype(t)]) -> Type{<:DenseVector{T}} |
49 | 55 |
|
50 | | -function similarstoragetype(TT::Type{<:AbstractTensorMap}, ::Type{T}) where {T} |
51 | | - return Core.Compiler.return_type(similar, Tuple{storagetype(TT), Type{T}}) |
52 | | -end |
| 56 | + similarstoragetype(T::Type{<:Number}) -> Vector{T} |
| 57 | +
|
| 58 | +For a given tensor `t`, tensor type `TT <: AbstractTensorMap`, array type `A <: AbstractArray`, |
| 59 | +or sector dictionary type `D <: AbstractDict{<:Sector, <:AbstractMatrix}`, compute an appropriate |
| 60 | +storage type for tensors. Optionally, a different scalar type `T` can be supplied as well. |
| 61 | +
|
| 62 | +This function determines the type of newly allocated `TensorMap`s throughout TensorKit.jl. |
| 63 | +It does so by leveraging type inference and calls to `Base.similar` for automatically determining |
| 64 | +appropriate storage types. Additionally this registers the default storage type when only a type |
| 65 | +`T <: Number` is provided, which is `Vector{T}`. |
| 66 | +""" similarstoragetype |
| 67 | + |
| 68 | +# implement in type domain and fill in default value |
| 69 | +@inline similarstoragetype(t, ::Type{T} = scalartype(t)) where {T <: Number} = |
| 70 | + similarstoragetype(typeof(t), T) |
| 71 | + |
| 72 | +# implement on tensors |
| 73 | +similarstoragetype(::Type{TT}, ::Type{T}) where {TT <: AbstractTensorMap, T <: Number} = |
| 74 | + similarstoragetype(storagetype(TT), T) |
| 75 | + |
| 76 | +# implement on arrays |
| 77 | +similarstoragetype(::Type{A}, ::Type{T}) where {T <: Number, A <: DenseVector{T}} = A |
| 78 | +Base.@assume_effects :foldable similarstoragetype(::Type{A}, ::Type{T}) where {A <: AbstractArray, T <: Number} = |
| 79 | + Core.Compiler.return_type(similar, Tuple{A, Type{T}, Int}) |
| 80 | + |
| 81 | +# implement on sectordicts - intercept scalartype defaults! |
| 82 | +similarstoragetype(d::D, ::Type{T} = scalartype(valtype(d))) where {D <: AbstractDict{<:Sector}, T <: Number} = |
| 83 | + similarstoragetype(typeof(d), T) |
| 84 | +similarstoragetype(::Type{D}, ::Type{T} = scalartype(valtype(D))) where {D <: AbstractDict{<:Sector}, T <: Number} = |
| 85 | + similarstoragetype(valtype(D), T) |
| 86 | + |
| 87 | +# default storage type for numbers |
| 88 | +similarstoragetype(::Type{T}) where {T <: Number} = Vector{T} |
| 89 | + |
| 90 | +# avoid infinite recursion |
| 91 | +similarstoragetype(X::Type, Y::Type) = |
| 92 | + throw(ArgumentError("Cannot determine a storagetype for tensor / array type `$X` and/or scalar type `$Y`")) |
53 | 93 |
|
54 | 94 | # tensor characteristics: space and index information |
55 | 95 | #----------------------------------------------------- |
|
175 | 215 | InnerProductStyle(t::AbstractTensorMap) = InnerProductStyle(typeof(t)) |
176 | 216 | storagetype(t::AbstractTensorMap) = storagetype(typeof(t)) |
177 | 217 | blocktype(t::AbstractTensorMap) = blocktype(typeof(t)) |
178 | | -similarstoragetype(t::AbstractTensorMap, T = scalartype(t)) = similarstoragetype(typeof(t), T) |
179 | 218 |
|
180 | 219 | numout(t::AbstractTensorMap) = numout(typeof(t)) |
181 | 220 | numin(t::AbstractTensorMap) = numin(typeof(t)) |
|
0 commit comments