Skip to content
Open
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
7 changes: 3 additions & 4 deletions NDTensors/src/diag/tensoralgebra/outer.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
function outer!(
R::DenseTensor{<:Number, NR}, T1::DiagTensor{<:Number, N1}, T2::DiagTensor{<:Number, N2}
) where {NR, N1, N2}
for i1 in 1:diaglength(T1), i2 in 1:diaglength(T2)
indsR = CartesianIndex{NR}(ntuple(r -> r N1 ? i1 : i2, Val(NR)))
R[indsR] = getdiagindex(T1, i1) * getdiagindex(T2, i2)
end
t1 = T1.storage.data
t2 = T2.storage.data
array(R) .= t1 .* t2'
Copy link
Member

@mtfishman mtfishman Nov 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like there are a few issues with this proposal:

  1. t2' will complex conjugate the elements, while outer[!] isn't supposed to conjugate the elements. Maybe transpose could be used as an alternative.
  2. t1 * transpose(t2) when t1 and t2 are vectors will output a matrix whose length this the product of the lengths of the diagonals of T1 and T2, I don't understand how the shapes of this operation work out since R in general is a tensor whose length is the product of the total lengths of T1 and T2 (not just the lengths of their diagonals). I think maybe you want to do something like t1 * transpose(t2) and reshape it into a vector (or relatedly, something like kron(t1, t2), which has an in-place counterpart kron!) and use that to write into the diagonal values of R.

It looks like you'll need to investigate this more to make sure it really does the correct operation.

return R
end

Expand Down
5 changes: 5 additions & 0 deletions NDTensors/test/test_diag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ end

## Test dot on GPU
@test dot(t, A) ≈ dot(dev(array(t)), array(A)) rtol = sqrt(eps(elt))

NDTensors.outer!(A, t,t);
for i in NDTensors.cpu(A)
@test i == one(elt)
end
end
nothing
end
Loading