o
    8Vas                     @   sl   d dl mZ d dlmZ d dlmZmZmZmZ d dl	m
Z
 G dd deZedd ZG d	d
 d
eZdS )    )Iterable)singledispatch)ExprSMulsympify)global_parametersc                   @   s@   e Zd ZdZdZdd Zdd Zdd Zed	d
 Z	dd Z
dS )TensorProductz,
    Generic class for tensor products.
    Fc                 O   s  ddl m}m}m} ddlm}m} ddlm} dd |D }|	dt
j}	|	s3tj| g|R  }
|
S g }g }tj}|D ]"}t|t||frN||| q<t||frZ|| q<||9 }q<|||  }t|dkrm|S |dkrw|g| }n|}tj| g|R i |}
||
S )	Nr   )	NDimArraytensorproductArray)
MatrixBase
MatrixExpr)flattenc                 S   s   g | ]}t |qS  )r   ).0argr   r   8/usr/lib/python3/dist-packages/sympy/tensor/functions.py
<listcomp>   s    z)TensorProduct.__new__.<locals>.<listcomp>evaluate   )Zsympy.tensor.arrayr
   r   r   sympyr   r   Zsympy.strategiesr   getr   r   r   __new__r   ZOne
isinstancer   appendlen)clsargskwargsr
   r   r   r   r   r   r   objZarraysotherZscalarr   ZcoeffZnewargsr   r   r   r      s2   
zTensorProduct.__new__c                 C   s
   t | jS N)r   shapeselfr   r   r   rank/   s   
zTensorProduct.rankc                    s    ddl m   fdd| jD S )Nr   r   c                    s&   g | ]}t |d r|jn |jqS )r#   )hasattrr#   r   ir'   r   r   r   4   s   & z2TensorProduct._get_args_shapes.<locals>.<listcomp>)r   r   r   r$   r   r'   r   _get_args_shapes2   s   zTensorProduct._get_args_shapesc                 C   s   |   }t|dS )Nr   )r+   sum)r%   Z
shape_listr   r   r   r#   6   s   
zTensorProduct.shapec                    s,   t   t fddt| j|  D S )Nc                 3   s0    | ]\}}| t fd d|D V  qdS )c                 3   s    | ]}t  V  qd S r"   )nextr)   indexr   r   	<genexpr>>   s    z6TensorProduct.__getitem__.<locals>.<genexpr>.<genexpr>N)__getitem__tuple)r   r   Zshpr.   r   r   r0   =   s
    
z,TensorProduct.__getitem__.<locals>.<genexpr>)iterr   Zfromiterzipr   r+   )r%   r/   r   r.   r   r1   ;   s   zTensorProduct.__getitem__N)__name__
__module____qualname____doc__Z	is_numberr   r&   r+   propertyr#   r1   r   r   r   r   r	      s    !
r	   c                 C   s   t | dr| jS td|  )a  
    Return the shape of the *expr* as a tuple. *expr* should represent
    suitable object such as matrix or array.

    Parameters
    ==========

    expr : SymPy object having ``MatrixKind`` or ``ArrayKind``.

    Raises
    ======

    NoShapeError : Raised when object with wrong kind is passed.

    Examples
    ========

    This function returns the shape of any object representing matrix or array.

    >>> from sympy import shape, Array, Matrix, Integral
    >>> from sympy.abc import x
    >>> A = Array([1, 2])
    >>> shape(A)
    (2,)
    >>> shape(Integral(A, x))
    (2,)
    >>> M = Matrix([1, 2])
    >>> shape(M)
    (2, 1)
    >>> shape(Integral(M, x))
    (2, 1)

    You can support new type by dispatching.

    >>> from sympy import Expr
    >>> class NewExpr(Expr):
    ...     pass
    >>> @shape.register(NewExpr)
    ... def _(expr):
    ...     return shape(expr.args[0])
    >>> shape(NewExpr(M))
    (2, 1)

    If unsuitable expression is passed, ``NoShapeError()`` will be raised.

    >>> shape(Integral(x, x))
    Traceback (most recent call last):
      ...
    sympy.tensor.functions.NoShapeError: shape() called on non-array object: Integral(x, x)

    Notes
    =====

    Array-like classes (such as ``Matrix`` or ``NDimArray``) has ``shape``
    property which returns its shape, but it cannot be used for non-array
    classes containing array. This function returns the shape of any
    registered object representing array.

    r#   zA%s does not have shape, or its type is not registered to shape().)r(   r#   NoShapeError)exprr   r   r   r#   C   s
   
=r#   c                   @   s   e Zd ZdZdS )r:   an  
    Raised when ``shape()`` is called on non-array object.

    This error can be imported from ``sympy.tensor.functions``.

    Examples
    ========

    >>> from sympy import shape
    >>> from sympy.abc import x
    >>> shape(x)
    Traceback (most recent call last):
      ...
    sympy.tensor.functions.NoShapeError: shape() called on non-array object: x
    N)r5   r6   r7   r8   r   r   r   r   r:      s    r:   N)Zcollections.abcr   	functoolsr   r   r   r   r   r   Zsympy.core.parametersr   r	   r#   	Exceptionr:   r   r   r   r   <module>   s    ;
B