![]() System : Linux absol.cf 5.4.0-198-generic #218-Ubuntu SMP Fri Sep 27 20:18:53 UTC 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.33 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, Directory : /usr/local/lib/python3.6/dist-packages/sympy/tensor/ |
Upload File : |
from sympy import Expr, S, Mul, sympify from sympy.core.compatibility import Iterable from sympy.core.parameters import global_parameters class TensorProduct(Expr): """ Generic class for tensor products. """ is_number = False def __new__(cls, *args, **kwargs): from sympy.tensor.array import NDimArray, tensorproduct, Array from sympy import MatrixBase, MatrixExpr from sympy.strategies import flatten args = [sympify(arg) for arg in args] evaluate = kwargs.get("evaluate", global_parameters.evaluate) if not evaluate: obj = Expr.__new__(cls, *args) return obj arrays = [] other = [] scalar = S.One for arg in args: if isinstance(arg, (Iterable, MatrixBase, NDimArray)): arrays.append(Array(arg)) elif isinstance(arg, (MatrixExpr,)): other.append(arg) else: scalar *= arg coeff = scalar*tensorproduct(*arrays) if len(other) == 0: return coeff if coeff != 1: newargs = [coeff] + other else: newargs = other obj = Expr.__new__(cls, *newargs, **kwargs) return flatten(obj) def rank(self): return len(self.shape) def _get_args_shapes(self): from sympy import Array return [i.shape if hasattr(i, "shape") else Array(i).shape for i in self.args] @property def shape(self): shape_list = self._get_args_shapes() return sum(shape_list, ()) def __getitem__(self, index): index = iter(index) return Mul.fromiter( arg.__getitem__(tuple(next(index) for i in shp)) for arg, shp in zip(self.args, self._get_args_shapes()) )