Skip to content
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,6 +1,6 @@
name = "ITensorBase"
uuid = "4795dd04-0d67-49bb-8f44-b89c448a1dc7"
version = "0.12.1"
version = "0.12.2"
authors = ["ITensor developers <support@itensor.org> and contributors"]

[workspace]
Expand Down
6 changes: 4 additions & 2 deletions src/ITensorBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ module ITensorBase
export AbstractNamedTensor, NamedTensor, AbstractITensor, ITensor, Index,
NamedUnitRange, aligndims, aligneddims, apply, codomainnames, commonind, commoninds,
dimnames, dimnametype, domainnames, hascommoninds, id, inds, mapinds, named, nameddims,
noncommoninds, noprime, operator, prime, replaceinds, sim, similar_operator, state,
trycommonind, trynoncommonind, uniqueind, uniqueinds, unioninds, uniquename
noncommonind, noncommoninds, noprime, operator, prime, replaceinds, sim,
similar_operator, state,
trycommonind, trynoncommonind, tryuniqueind, uniqueind, uniqueinds, unioninds,
uniquename
if VERSION >= v"1.11.0-DEV.469"
eval(
Meta.parse(
Expand Down
56 changes: 52 additions & 4 deletions src/abstractnamedtensor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ commonind(a::AbstractNamedTensor, b::AbstractNamedTensor) = only(commoninds(a, b
uniqueind(a::AbstractNamedTensor, b::AbstractNamedTensor)

The single index of `a` that does not appear by name in `b`. Errors unless there is exactly
one such index. Use [`trynoncommonind`](@ref) to get `nothing` instead of an error.
one such index. Use [`tryuniqueind`](@ref) to get `nothing` instead of an error.

# Examples

Expand All @@ -755,6 +755,28 @@ See also [`uniqueinds`](@ref), [`commonind`](@ref).
"""
uniqueind(a::AbstractNamedTensor, b::AbstractNamedTensor) = only(uniqueinds(a, b))

"""
noncommonind(a::AbstractNamedTensor, b::AbstractNamedTensor)

The single index not shared by name between `a` and `b` (the symmetric difference). Errors
unless there is exactly one such index. Use [`trynoncommonind`](@ref) to get `nothing` instead
of an error.

# Examples

```jldoctest
julia> i, j, k = Index.((2, 3, 2));

julia> a, b = randn(i, j), randn(i, j, k);

julia> noncommonind(a, b) == k
true
```

See also [`noncommoninds`](@ref), [`uniqueind`](@ref).
"""
noncommonind(a::AbstractNamedTensor, b::AbstractNamedTensor) = only(noncommoninds(a, b))

"""
trycommonind(a::AbstractNamedTensor, b::AbstractNamedTensor)

Expand All @@ -781,7 +803,7 @@ function trycommonind(a::AbstractNamedTensor, b::AbstractNamedTensor)
end

"""
trynoncommonind(a::AbstractNamedTensor, b::AbstractNamedTensor)
tryuniqueind(a::AbstractNamedTensor, b::AbstractNamedTensor)

The single index of `a` that does not appear by name in `b`, or `nothing` if there is no such
index or more than one. The non-erroring counterpart of [`uniqueind`](@ref).
Expand All @@ -793,15 +815,41 @@ julia> i, j, k = Index.((2, 3, 2));

julia> a, b = randn(i, j), randn(j, k);

julia> trynoncommonind(a, b) == i
julia> tryuniqueind(a, b) == i
true
```
"""
function trynoncommonind(a::AbstractNamedTensor, b::AbstractNamedTensor)
function tryuniqueind(a::AbstractNamedTensor, b::AbstractNamedTensor)
us = uniqueinds(a, b)
return length(us) == 1 ? only(us) : nothing
end

"""
trynoncommonind(a::AbstractNamedTensor, b::AbstractNamedTensor)

The single index not shared by name between `a` and `b` (the symmetric difference), or
`nothing` if there is no such index or more than one. The non-erroring counterpart of
[`noncommonind`](@ref).

# Examples

```jldoctest
julia> i, j, k = Index.((2, 3, 2));

julia> a, b = randn(i, j), randn(i, j, k);

julia> trynoncommonind(a, b) == k
true

julia> isnothing(trynoncommonind(randn(i, j), randn(j, k)))
true
```
"""
function trynoncommonind(a::AbstractNamedTensor, b::AbstractNamedTensor)
ncs = noncommoninds(a, b)
return length(ncs) == 1 ? only(ncs) : nothing
end

# `Base.isempty(a::AbstractArray)` is defined as `length(a) == 0`,
# which involves comparing a named integer to an unnamed integer
# which isn't well defined.
Expand Down
22 changes: 17 additions & 5 deletions test/test_basics.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using ITensorBase: ITensorBase, AbstractNamedTensor, ITensor, Index, IndexName, NamedTensor,
commonind, commoninds, dimnametype, gettag, hascommoninds, hastag, id, inds, mapinds,
name, named, noncommoninds, noprime, plev, prime, replaceinds, setplev, settag, sim,
tags, trycommonind, trynoncommonind, unioninds, uniqueind, uniqueinds, uniquename,
unname, unnamed, unsettag, uuid
name, named, noncommonind, noncommoninds, noprime, plev, prime, replaceinds, setplev,
settag, sim, tags, trycommonind, trynoncommonind, tryuniqueind, unioninds, uniqueind,
uniqueinds, uniquename, unname, unnamed, unsettag, uuid
using Test: @test, @test_broken, @test_throws, @testset
using UUIDs: UUID

Expand Down Expand Up @@ -250,7 +250,12 @@ using UUIDs: UUID
@test name(commonind(a, b)) == name(j)
@test name(uniqueind(a, b)) == name(i)
@test name(trycommonind(a, b)) == name(j)
@test name(trynoncommonind(a, b)) == name(i)
@test name(tryuniqueind(a, b)) == name(i)

# The symmetric-difference single: the one index not shared across both tensors.
e = randn(elt, i, j, k)
@test name(noncommonind(a, e)) == name(k)
@test name(trynoncommonind(a, e)) == name(k)

# No shared index.
c = randn(elt, k)
Expand All @@ -265,6 +270,13 @@ using UUIDs: UUID
@test isnothing(trycommonind(a, d))
@test_throws ArgumentError commonind(a, c)
@test_throws ArgumentError uniqueind(a, c)
@test isnothing(trynoncommonind(a, c))
@test isnothing(tryuniqueind(a, c))

# `noncommonind` errors / `trynoncommonind` is `nothing` unless there is exactly one
# non-shared index: two here (symdiff is `[i, k]`), zero for identical index sets.
@test_throws ArgumentError noncommonind(a, b)
@test isnothing(trynoncommonind(a, b))
@test_throws ArgumentError noncommonind(a, d)
@test isnothing(trynoncommonind(a, d))
end
end
5 changes: 3 additions & 2 deletions test/test_exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ using Test: @test, @testset
:Index, :NamedUnitRange,
:aligndims, :aligneddims, :apply, :codomainnames, :commonind, :commoninds,
:dimnames, :dimnametype, :domainnames, :hascommoninds, :id,
:inds, :mapinds, :named, :nameddims, :noncommoninds, :noprime, :operator,
:inds, :mapinds, :named, :nameddims, :noncommonind, :noncommoninds, :noprime,
:operator,
:prime,
:replaceinds, :sim, :similar_operator, :state, :trycommonind, :trynoncommonind,
:uniqueind, :uniqueinds, :unioninds, :uniquename,
:tryuniqueind, :uniqueind, :uniqueinds, :unioninds, :uniquename,
]
publics = [
:IndexName, :name, :nametype, :replacedimnames, :setname, :space, :unnamed,
Expand Down
Loading