TRIQS / triqs

a Toolbox for Research on Interacting Quantum Systems
https://triqs.github.io
GNU General Public License v3.0
139 stars 72 forks source link

Gf class matrix multiplication and right multiplication priority #864

Closed GingrasO closed 1 year ago

GingrasO commented 1 year ago

Dear all,

Following numpy's convention, the matrix multiplication operator should be @, while * performs elementwise multiplication. Thus I have added the set of operation corresponding to matrix multiplications to the Gf class. I simply changed the names from mul to matmul and created the mul functions such that they call the matmul functions. In other words, the Gf objects works the same, but now one can also perform the @ multiplication if used to numpy. However, it is not possible to do elementwise multiplication.

Moreover, I noticed the the __rmul__ function of the Gf class was never called when the left operator was a numpy array. That is because the numpy array has a __mul__ function that is instead called and leads to errors. To prioritize the Gf functions, I have added a __array_priority__ value to the Gf class, which makes it call its __rmul__ before numpy's __mul__.

Wentzell commented 1 year ago

Thank you @GingrasO for these improvements!