Jutho / TensorOperations.jl

Julia package for tensor contractions and related operations
https://jutho.github.io/TensorOperations.jl/stable/
Other
438 stars 55 forks source link

Sums are not commutative #136

Closed ebelnikola closed 1 year ago

ebelnikola commented 1 year ago

Hi!

I am not sure if this is a bug, or if it is supposed to work this way, but I noticed the following. Here is a code which was supposed to do a special sort of symmetrisation of matrix A, depending on F:

` A=[0.2 0.44 0.3 0.1]

F=[1 0 0 1];

@tensor begin A[i1,i2]=A[i2,a]*F[a,i1]+A[i1,i2]; end; `

The problem is that what the code does is that it first assigns A[i1,i2]=A[i2,a]F[a,i1] and then adds A to itself. Thus, instead of a symmetric matrix (in the F=Id case), I have got A=2A^t. What is especially confusing, is that

' @tensor begin A[i1,i2]=A[i1,i2]+A[i2,a]*F[a,i1]; end; '

works fine, so the sum is not commutative.

lkdvos commented 1 year ago

Hi!

After some checking, I guess this bug is similar to how things like mul! from LinearAlgebra require that the output matrix must not be aliased with any of the input matrices. As the macro attempts to allocate the least amount possible, under the hood, A = B + C actually gets rewritten to A += B followed by A += C. While toying around with this, i even stumbled upon the following:

julia> A = [1 2; 3 4];
julia> @tensor A[a,b] = A[b,a]
2×2 Matrix{Int64}:
 1  2
 2  4

Thanks for pointing out this bug, I will add an error in the new versions to catch the case where the two tensors point to the same object, but @tensor will probably still be badly behaved for similar statements using views etc...

As an aside, concretely for the example you have, I think the following is better and avoids the problems:

@tensor A[i1,i2] += A[i2,a] * F[a, i1]
ebelnikola commented 1 year ago

Thanks!