Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ITensors] Generalize diag_itensor constructors to AbstractVector #1510

Merged
merged 2 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ITensors"
uuid = "9136182c-28ba-11e9-034c-db9fb085ebd5"
authors = ["Matthew Fishman <[email protected]>", "Miles Stoudenmire <[email protected]>"]
version = "0.6.14"
version = "0.6.15"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
36 changes: 19 additions & 17 deletions src/itensor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -305,14 +305,14 @@ end

using NDTensors.TypeParameterAccessors: set_eltype, type_parameters, specify_type_parameters
"""
ITensor([ElT::Type, ]A::Array, inds)
ITensor([ElT::Type, ]A::Array, inds::Index...)
ITensor([ElT::Type, ]A::AbstractArray, inds)
ITensor([ElT::Type, ]A::AbstractArray, inds::Index...)

itensor([ElT::Type, ]A::Array, inds)
itensor([ElT::Type, ]A::Array, inds::Index...)
itensor([ElT::Type, ]A::AbstractArray, inds)
itensor([ElT::Type, ]A::AbstractArray, inds::Index...)

Construct an ITensor from an Array `A` and indices `inds`.
The ITensor will be a view of the Array data if possible (if
Construct an ITensor from an AbstractArray `A` and indices `inds`.
The ITensor will be a view of the AbstractArray data if possible (if
no conversion to a different element type is necessary).

If specified, the ITensor will have element type `ElT`.
Expand Down Expand Up @@ -389,13 +389,13 @@ function ITensor(eltype::Type{<:Number}, A::AbstractArray{<:Number}, is...; kwar
end

# For now, it's not well defined to construct an ITensor without indices
# from a non-zero dimensional Array
# from a non-zero dimensional AbstractArray
function ITensor(
as::AliasStyle, eltype::Type{<:Number}, A::AbstractArray{<:Number}; kwargs...
)
if length(A) > 1
error(
"Trying to create an ITensor without any indices from Array $A of dimensions $(size(A)). Cannot construct an ITensor from an Array with more than one element without any indices.",
"Trying to create an ITensor without any indices from $(typeof(A)) $A of dimensions $(size(A)). Cannot construct an ITensor from an $(typeof(A)) with more than one element without any indices.",
)
end
return ITensor(eltype, A[]; kwargs...)
Expand Down Expand Up @@ -448,8 +448,8 @@ diag_itensor(is::Indices) = diag_itensor(Float64, is)
diag_itensor(is...) = diag_itensor(indices(is...))

"""
diag_itensor([ElT::Type, ]v::Vector, inds...)
diagitensor([ElT::Type, ]v::Vector, inds...)
diag_itensor([ElT::Type, ]v::AbstractVector, inds...)
diagitensor([ElT::Type, ]v::AbstractVector, inds...)

Make a sparse ITensor with non-zero elements only along the diagonal.
In general, the diagonal elements will be those stored in `v` and
Expand All @@ -467,29 +467,31 @@ The version `diagitensor` might output an ITensor whose storage data
is an alias of the input vector data in order to minimize operations.
"""
function diag_itensor(
as::AliasStyle, eltype::Type{<:Number}, v::Vector{<:Number}, is::Indices
as::AliasStyle, eltype::Type{<:Number}, v::AbstractVector{<:Number}, is::Indices
)
length(v) ≠ mindim(is) && error(
"Length of vector for diagonal must equal minimum of the dimension of the input indices",
)
data = Vector{eltype}(as, v)
data = set_eltype(typeof(v), eltype)(as, v)
return itensor(Diag(data), is)
end

function diag_itensor(as::AliasStyle, eltype::Type{<:Number}, v::Vector{<:Number}, is...)
function diag_itensor(
as::AliasStyle, eltype::Type{<:Number}, v::AbstractVector{<:Number}, is...
)
return diag_itensor(as, eltype, v, indices(is...))
end

function diag_itensor(as::AliasStyle, v::Vector, is...)
function diag_itensor(as::AliasStyle, v::AbstractVector, is...)
return diag_itensor(as, eltype(v), v, is...)
end

function diag_itensor(as::AliasStyle, v::Vector{<:RealOrComplex{Int}}, is...)
function diag_itensor(as::AliasStyle, v::AbstractVector{<:RealOrComplex{Int}}, is...)
return diag_itensor(AllowAlias(), float(eltype(v)), v, is...)
end

diag_itensor(v::Vector{<:Number}, is...) = diag_itensor(NeverAlias(), v, is...)
function diag_itensor(eltype::Type{<:Number}, v::Vector{<:Number}, is...)
diag_itensor(v::AbstractVector{<:Number}, is...) = diag_itensor(NeverAlias(), v, is...)
function diag_itensor(eltype::Type{<:Number}, v::AbstractVector{<:Number}, is...)
return diag_itensor(NeverAlias(), eltype, v, is...)
end

Expand Down
Loading