|
| 1 | +using SparseArrays: SparseMatrixCSC, findnz, nnz |
| 2 | +using SparseArraysBase: SparseMatrixDOK, eachstoredindex, isstored, sparsezeros, |
| 3 | + storedlength |
| 4 | +using TensorAlgebra: contract, matricize |
| 5 | +using Test: @testset, @test |
| 6 | + |
| 7 | +@testset "TensorAlgebraExt (eltype = $elt)" for elt in (Float32, ComplexF64) |
| 8 | + a = sparsezeros(elt, (2, 2, 2)) |
| 9 | + a[1, 1, 1] = 1 |
| 10 | + a[2, 1, 2] = 2 |
| 11 | + |
| 12 | + # matricize |
| 13 | + m = matricize(a, (1, 3), (2,)) |
| 14 | + @test m isa SparseMatrixCSC{elt} |
| 15 | + @test nnz(m) == 2 |
| 16 | + @test isstored(m, 1, 1) |
| 17 | + @test m[1, 1] ≡ elt(1) |
| 18 | + @test isstored(m, 4, 1) |
| 19 | + @test m[4, 1] ≡ elt(2) |
| 20 | + @test issetequal(eachstoredindex(m), [CartesianIndex(1, 1), CartesianIndex(4, 1)]) |
| 21 | + for I in setdiff(CartesianIndices(m), [CartesianIndex(1, 1), CartesianIndex(4, 1)]) |
| 22 | + @test m[I] ≡ zero(elt) |
| 23 | + end |
| 24 | + |
| 25 | + # contract |
| 26 | + b, l = contract(a, ("i", "j", "k"), a, ("j", "k", "l")) |
| 27 | + @test b isa SparseMatrixDOK{elt} |
| 28 | + @test storedlength(b) == 1 |
| 29 | + @test only(eachstoredindex(b)) == CartesianIndex(1, 1) |
| 30 | + @test b[1, 1] ≡ elt(1) |
| 31 | +end |
0 commit comments