4
$\begingroup$

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)

$\endgroup$
0

1 Answer 1

4
$\begingroup$

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/

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.