5

When using numpy or tensorflow in Python, we can simply write

C = A @ B

for matrix multiplication C = np.matmul(A,B). I wonder if there is a shorthand for tensor product (Kronecker product), $C=A\otimes B$ . Now I can only do this by using np.kron(A, B) or using qutip qutip.tensor(A, B)

glS
  • 27,510
  • 7
  • 37
  • 125
Neo
  • 171
  • 5

1 Answers1

4

Python currently doesn't support an operator for Kronecker products. Note how the @ symbol works: when you write the statement A @ B, Python$^1$ checks the objects A and B for a __matmul__ method and then returns A.__matmul__(B). But there's no built-in operator that corresponds to something like a __kron__ method.

If you really want this functionality, one way might be to change how the * operator works by redefining __mul__ (e.g. see here or here) to perform a kronecker product, so that calling A * B would return np.kron(A, B). However this could end up being very confusing for yourself and others reading your code.


$^1$ as of Python 3.5, see PEP 465: https://www.python.org/dev/peps/pep-0465/

forky40
  • 7,988
  • 2
  • 12
  • 33