Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TensorProduct bug when the args are a combination of symbols and matrices #15047

Closed
saidctb opened this issue Aug 7, 2018 · 2 comments
Closed

Comments

@saidctb
Copy link

saidctb commented Aug 7, 2018

I have this simple example that doesn't work, I'm not so sure if we are allowed to combine such arguments or not, but then why does it work in a certain order and doesn't on the other.

In [37]: from sympy.physics.quantum import TensorProduct

In [38]: from sympy import Symbol, eye

In [39]: a = Symbol('a',commutative=False)

In [40]: TensorProduct(a,eye(1))
Out[40]: axMatrix([[1]])

In [41]: TensorProduct(eye(1),a)

TypeError Traceback (most recent call last)
in ()
----> 1 TensorProduct(eye(1),a)

/usr/local/lib/python3.5/dist-packages/sympy/physics/quantum/tensorproduct.py in new(cls, *args)
120 def new(cls, *args):
121 if isinstance(args[0], (Matrix, numpy_ndarray, scipy_sparse_matrix)):
--> 122 return matrix_tensor_product(*args)
123 c_part, new_args = cls.flatten(sympify(args))
124 c_part = Mul(*c_part)

/usr/local/lib/python3.5/dist-packages/sympy/physics/quantum/matrixutils.py in matrix_tensor_product(*product)
245 """Compute the matrix tensor product of sympy/numpy/scipy.sparse matrices."""
246 if isinstance(product[0], MatrixBase):
--> 247 return _sympy_tensor_product(*product)
248 elif isinstance(product[0], numpy_ndarray):
249 return _numpy_tensor_product(*product)

/usr/local/lib/python3.5/dist-packages/sympy/physics/quantum/matrixutils.py in _sympy_tensor_product(*matrices)
192 if not all(isinstance(m, MatrixBase) for m in matrices):
193 raise TypeError(
--> 194 'Sequence of Matrices expected, got: %s' % repr(matrices)
195 )
196

TypeError: Sequence of Matrices expected, got: (Matrix([[1]]), a)

In [42]:

@ManuelZierl
Copy link

it works if u use
from sympy.tensor.functions import TensorProduct

also i'm not sure what you are trying to do but
a = Symbol('a',commutative=False)
is interpreted to the best of my knowledge as a number not a matrix so:

x = Symbol('x',commutative=False)
TensorProduct(x,eye(4))

results in:
⎡x 0 0 0⎤
⎢0 x 0 0⎥
⎢0 0 x 0⎥
⎣0 0 0 x⎦

if you want x to be a Matrix as well (like i would suggest snice you are using the tesorproduct) i would recommend to use MatrixSybols

M = MatrixSymbol('M',2,2)
TensorProduct(eye(2), M)

⎡1 0 0 0⎤
⎢0 1 0 0⎥ ⊗M
⎢0 0 1 0⎥
⎣0 0 0 1⎦

@saidctb
Copy link
Author

saidctb commented Sep 13, 2018

ah thanks a lot, yes I was trying to use a matrix

@saidctb saidctb closed this as completed Sep 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants