diff --git a/docs/src/changelog.md b/docs/src/changelog.md index a87247d0..9376087f 100644 --- a/docs/src/changelog.md +++ b/docs/src/changelog.md @@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- In tensor index notation, `r[;]` now denotes a tensor with no indices, just like `r[]` ([#325](https://github.com/QuantumKitHub/TensorOperations.jl/issues/325)). + ### Performance ## [5.8.4](https://github.com/QuantumKitHub/TensorOperations.jl/compare/v5.8.3...v5.8.4) - 2026-11-16 diff --git a/src/indexnotation/analyzers.jl b/src/indexnotation/analyzers.jl index 2d2cb5d2..a5507c95 100644 --- a/src/indexnotation/analyzers.jl +++ b/src/indexnotation/analyzers.jl @@ -10,6 +10,8 @@ function decomposetensor(ex) else return ex.args[1], ex.args[2:end], Any[] end + elseif isemptysplittensor(ex) + return ex.args[1], Any[], Any[] elseif isexpr(ex, :typed_vcat) if isexpr(ex.args[2], [:row, :tuple]) leftind = ex.args[2].args diff --git a/src/indexnotation/preprocessors.jl b/src/indexnotation/preprocessors.jl index b1ca158c..588dae22 100644 --- a/src/indexnotation/preprocessors.jl +++ b/src/indexnotation/preprocessors.jl @@ -4,6 +4,8 @@ function replaceindices((@nospecialize f), ex) return Expr(:block, map(x -> replaceindices(f, x), ex.args)...) elseif isexpr(ex, :macrocall) && ex.args[1] == Symbol("@notensor") return ex + elseif isemptysplittensor(ex) + return ex elseif istensor(ex) if ex.head == :ref || ex.head == :typed_hcat if length(ex.args) == 1 diff --git a/src/indexnotation/verifiers.jl b/src/indexnotation/verifiers.jl index 948c9445..2d5e68f6 100644 --- a/src/indexnotation/verifiers.jl +++ b/src/indexnotation/verifiers.jl @@ -18,6 +18,9 @@ function isindex(ex) end end +# Julia parses A[;] as a typed concatenation with dimension 1 and no elements. +isemptysplittensor(ex) = isexpr(ex, :typed_ncat, 2) && ex.args[2] === 1 + # test for a simple tensor object indexed by valid indices """ istensor(ex) @@ -42,6 +45,8 @@ function istensor(ex::Expr) else return all(isindex, ex.args[2:end]) end + elseif isemptysplittensor(ex) + return true elseif ex.head == :typed_vcat && length(ex.args) == 3 if isexpr(ex.args[2], [:row, :tuple]) all(isindex, ex.args[2].args) || return false @@ -120,7 +125,7 @@ function isscalarexpr(ex) return true elseif isexpr(ex, :call) && ex.args[1] == :tensorscalar return istensorexpr(ex.args[2]) - elseif isexpr(ex, (:ref, :typed_vcat, :typed_hcat)) + elseif isexpr(ex, (:ref, :typed_vcat, :typed_hcat)) || isemptysplittensor(ex) return false elseif isexpr(ex, :call) # || isdefinition(ex) || isassignment(ex) return all(isscalarexpr, ex.args[2:end]) diff --git a/test/auxiliary.jl b/test/auxiliary.jl index e102ca03..29d06e3b 100644 --- a/test/auxiliary.jl +++ b/test/auxiliary.jl @@ -57,6 +57,11 @@ @test !isindex(:("x")) @test !isindex(:(5.1)) + @test istensor(:(a[;])) + @test !isscalarexpr(:(a[;])) + @test decomposetensor(:(a[;])) == decomposetensor(:(a[])) + @test !istensor(:(a[;;])) + @test !istensor(:(a[i;; j])) @test istensor(:(a[1, 2, 3])) @test decomposetensor(:(a[1, 2, 3])) == (:a, Any[1, 2, 3], Any[]) @test istensor(:(a[5][a b c])) diff --git a/test/tensor.jl b/test/tensor.jl index 11ed2473..34d855cf 100644 --- a/test/tensor.jl +++ b/test/tensor.jl @@ -2,7 +2,19 @@ #----------------------------------------- backendlist = (BaseCopy(), BaseView(), StridedNative(), StridedBLAS()) +@testset "empty left and right indices on input" begin + r = fill(5.0) + @tensor s[] := 2 * r[;] + @test s == 2 * r +end + @testset "with backend $b" for b in backendlist + @testset "empty left and right indices" begin + A = [1.0 2.0; 3.0 4.0] + @tensor backend = b r[;] := A[1; 1] + @test r == fill(tr(A)) + end + @testset "tensorcontract 1" begin A = randn(Float64, (3, 5, 4, 6)) p = (4, 1, 3, 2)