|
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}} |
| 55 | +
|
| 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 |
| 69 | +similarstoragetype(t) = similarstoragetype(typeof(t)) |
| 70 | +similarstoragetype(t, ::Type{T}) where {T <: Number} = similarstoragetype(typeof(t), T) |
| 71 | + |
| 72 | +# avoid infinite recursion |
| 73 | +similarstoragetype(X::Type) = |
| 74 | + throw(ArgumentError("Cannot determine a storagetype for tensor / array type `$X`")) |
| 75 | +similarstoragetype(X::Type, ::Type{T}) where {T <: Number} = |
| 76 | + throw(ArgumentError("Cannot determine a storagetype for tensor / array type `$X` and/or scalar type `$T`")) |
| 77 | + |
| 78 | +# implement on tensors |
| 79 | +similarstoragetype(::Type{TT}) where {TT <: AbstractTensorMap} = similarstoragetype(storagetype(TT)) |
| 80 | +similarstoragetype(::Type{TT}, ::Type{T}) where {TT <: AbstractTensorMap, T <: Number} = |
| 81 | + similarstoragetype(storagetype(TT), T) |
| 82 | + |
| 83 | +# implement on arrays |
| 84 | +similarstoragetype(::Type{A}) where {A <: DenseVector{<:Number}} = A |
| 85 | +Base.@assume_effects :foldable similarstoragetype(::Type{A}) where {A <: AbstractArray{<:Number}} = |
| 86 | + Core.Compiler.return_type(similar, Tuple{A, Int}) |
| 87 | +Base.@assume_effects :foldable similarstoragetype(::Type{A}, ::Type{T}) where {A <: AbstractArray, T <: Number} = |
| 88 | + Core.Compiler.return_type(similar, Tuple{A, Type{T}, Int}) |
| 89 | + |
| 90 | +# implement on sectordicts |
| 91 | +similarstoragetype(::Type{D}) where {D <: AbstractDict{<:Sector, <:AbstractMatrix}} = |
| 92 | + similarstoragetype(valtype(D)) |
| 93 | +similarstoragetype(::Type{D}, ::Type{T}) where {D <: AbstractDict{<:Sector, <:AbstractMatrix}, T <: Number} = |
| 94 | + similarstoragetype(valtype(D), T) |
| 95 | + |
| 96 | +# default storage type for numbers |
| 97 | +similarstoragetype(::Type{T}) where {T <: Number} = Vector{T} |
49 | 98 |
|
50 | | -function similarstoragetype(TT::Type{<:AbstractTensorMap}, ::Type{T}) where {T} |
51 | | - return Core.Compiler.return_type(similar, Tuple{storagetype(TT), Type{T}}) |
52 | | -end |
53 | 99 |
|
54 | 100 | # tensor characteristics: space and index information |
55 | 101 | #----------------------------------------------------- |
|
175 | 221 | InnerProductStyle(t::AbstractTensorMap) = InnerProductStyle(typeof(t)) |
176 | 222 | storagetype(t::AbstractTensorMap) = storagetype(typeof(t)) |
177 | 223 | blocktype(t::AbstractTensorMap) = blocktype(typeof(t)) |
178 | | -similarstoragetype(t::AbstractTensorMap, T = scalartype(t)) = similarstoragetype(typeof(t), T) |
179 | 224 |
|
180 | 225 | numout(t::AbstractTensorMap) = numout(typeof(t)) |
181 | 226 | numin(t::AbstractTensorMap) = numin(typeof(t)) |
@@ -503,17 +548,17 @@ end |
503 | 548 |
|
504 | 549 | # 3 arguments |
505 | 550 | Base.similar(t::AbstractTensorMap, codomain::TensorSpace, domain::TensorSpace) = |
506 | | - similar(t, similarstoragetype(t), codomain ← domain) |
| 551 | + similar(t, similarstoragetype(t, scalartype(t)), codomain ← domain) |
507 | 552 | Base.similar(t::AbstractTensorMap, ::Type{T}, codomain::TensorSpace) where {T} = |
508 | 553 | similar(t, T, codomain ← one(codomain)) |
509 | 554 |
|
510 | 555 | # 2 arguments |
511 | 556 | Base.similar(t::AbstractTensorMap, codomain::TensorSpace) = |
512 | | - similar(t, similarstoragetype(t), codomain ← one(codomain)) |
513 | | -Base.similar(t::AbstractTensorMap, V::TensorMapSpace) = similar(t, similarstoragetype(t), V) |
| 557 | + similar(t, codomain ← one(codomain)) |
| 558 | +Base.similar(t::AbstractTensorMap, V::TensorMapSpace) = similar(t, scalartype(t), V) |
514 | 559 | Base.similar(t::AbstractTensorMap, ::Type{T}) where {T} = similar(t, T, space(t)) |
515 | 560 | # 1 argument |
516 | | -Base.similar(t::AbstractTensorMap) = similar(t, similarstoragetype(t), space(t)) |
| 561 | +Base.similar(t::AbstractTensorMap) = similar(t, scalartype(t), space(t)) |
517 | 562 |
|
518 | 563 | # generic implementation for AbstractTensorMap -> returns `TensorMap` |
519 | 564 | function Base.similar(t::AbstractTensorMap, ::Type{TorA}, V::TensorMapSpace) where {TorA} |
|
524 | 569 |
|
525 | 570 | # implementation in type-domain |
526 | 571 | function Base.similar(::Type{TT}, V::TensorMapSpace) where {TT <: AbstractTensorMap} |
527 | | - TT′ = tensormaptype(spacetype(V), numout(V), numin(V), similarstoragetype(TT)) |
| 572 | + TT′ = tensormaptype(spacetype(V), numout(V), numin(V), similarstoragetype(TT, scalartype(TT))) |
528 | 573 | return TT′(undef, V) |
529 | 574 | end |
530 | 575 | Base.similar(::Type{TT}, cod::TensorSpace, dom::TensorSpace) where {TT <: AbstractTensorMap} = |
|
0 commit comments