Jutho / TensorOperations.jl

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

Unexpected behavior when there is a global minus sign #133

Closed markusqh closed 1 year ago

markusqh commented 1 year ago

The following code ignores the minus sign in res2:

vec=[1,2];
mat1=rand(2,2);
mat2=rand(2,2);
@tensor begin
  res1[mu]:=-(mat1[mu,alpha]*vec[alpha])
  res2[mu]:=-mat1[mu,alpha]*vec[alpha]
  res3[mu]:=-mat1[mu,alpha]*vec[alpha]-mat2[mu,alpha]*vec[alpha]
  res4[mu]:=-(mat1[mu,alpha]*vec[alpha]+mat2[mu,alpha]*vec[alpha])
end

mat1*vec
res1
res2

-mat1*vec-mat2*vec
res3
res4

res1 and res2 differ by a minus sign. Note that res3 is correct, while res4 is not. Is this a bug?

Jutho commented 1 year ago

Dear @markusqh , thanks for this issue report. This is most definitely a bug, and it is surprising (worrying) that this has not been discovered before. We are currently working on a major overhaul of TensorOperations.jl, and will thus fix this in the new version.

Jutho commented 1 year ago

In fact, this bug is sufficiently severe that it needed to be fixed immediately. It is now fixed on master, and I will tag a patch update (3.2.5) in a minute.