
    g/                         d dl Z d dlZd dlmZmZ d dlmZ d dlmZm	Z	 d dl
mZ d dlmZ d dlmZ  G d d	e          Zd
 Z G d de          ZdS )    N)_sympifysympify)Expr)BasicTuple)ImmutableDenseNDimArray)Symbol)Integerc                      e Zd ZdZd Zed             Zed             Zed             Zed             Z	ed             Z
ed             Zed	             Zd
 Zd Zed             Zed             Zed             Zd Zd Zd Zd Zd ZdS )ArrayComprehensiona  
    Generate a list comprehension.

    Explanation
    ===========

    If there is a symbolic dimension, for example, say [i for i in range(1, N)] where
    N is a Symbol, then the expression will not be expanded to an array. Otherwise,
    calling the doit() function will launch the expansion.

    Examples
    ========

    >>> from sympy.tensor.array import ArrayComprehension
    >>> from sympy import symbols
    >>> i, j, k = symbols('i j k')
    >>> a = ArrayComprehension(10*i + j, (i, 1, 4), (j, 1, 3))
    >>> a
    ArrayComprehension(10*i + j, (i, 1, 4), (j, 1, 3))
    >>> a.doit()
    [[11, 12, 13], [21, 22, 23], [31, 32, 33], [41, 42, 43]]
    >>> b = ArrayComprehension(10*i + j, (i, 1, 4), (j, 1, k))
    >>> b.doit()
    ArrayComprehension(10*i + j, (i, 1, 4), (j, 1, k))
    c                    t          d |D                       rt          d          t          |          g}|                    |                     ||                     t          j        | g|R i |}|j        dd          |_        | 	                    |j                  |_
        t          |j
                  |_        |                     |j
                  |_        |S )Nc              3   @   K   | ]}t          |          d k    pdV  dS    Nlen.0ls     b/var/www/html/ai-engine/env/lib/python3.11/site-packages/sympy/tensor/array/array_comprehension.py	<genexpr>z-ArrayComprehension.__new__.<locals>.<genexpr>%   1      44qs1vv{"d444444    KArrayComprehension requires values lower and upper bound for the expression   )any
ValueErrorr   extend_check_limits_validityr   __new___args_limits_calculate_shape_from_limits_shaper   _rank_calculate_loop_size
_loop_sizeclsfunctionsymbolsassumptionsarglistobjs         r   r    zArrayComprehension.__new__$   s    44G44444 	5 4 5 5 58$$%s11(GDDEEEmC9'999[99im55ckBB

OO	11#*==
r   c                     | j         d         S )aA  The function applied across limits.

        Examples
        ========

        >>> from sympy.tensor.array import ArrayComprehension
        >>> from sympy import symbols
        >>> i, j = symbols('i j')
        >>> a = ArrayComprehension(10*i + j, (i, 1, 4), (j, 1, 3))
        >>> a.function
        10*i + j
        r   )r!   selfs    r   r*   zArrayComprehension.function1   s     z!}r   c                     | j         S )au  
        The list of limits that will be applied while expanding the array.

        Examples
        ========

        >>> from sympy.tensor.array import ArrayComprehension
        >>> from sympy import symbols
        >>> i, j = symbols('i j')
        >>> a = ArrayComprehension(10*i + j, (i, 1, 4), (j, 1, 3))
        >>> a.limits
        ((i, 1, 4), (j, 1, 3))
        r"   r0   s    r   limitszArrayComprehension.limitsA   s     |r   c                     | j         j        }| j        D ]O\  }}}|                    |           |j                            |j                  }|                    |          }P|S )a)  
        The set of the free_symbols in the array.
        Variables appeared in the bounds are supposed to be excluded
        from the free symbol set.

        Examples
        ========

        >>> from sympy.tensor.array import ArrayComprehension
        >>> from sympy import symbols
        >>> i, j, k = symbols('i j k')
        >>> a = ArrayComprehension(10*i + j, (i, 1, 4), (j, 1, 3))
        >>> a.free_symbols
        set()
        >>> b = ArrayComprehension(10*i + j, (i, 1, 4), (j, 1, k+3))
        >>> b.free_symbols
        {k}
        )r*   free_symbolsr"   discardunion)r1   expr_free_symvarinfsupcurr_free_symss         r   r6   zArrayComprehension.free_symbolsR   so    ( 2!\ 	@ 	@MCc!!#&&& -33C4DEEN)//??MMr   c                 $    d | j         D             S )aL  The tuples of the variables in the limits.

        Examples
        ========

        >>> from sympy.tensor.array import ArrayComprehension
        >>> from sympy import symbols
        >>> i, j, k = symbols('i j k')
        >>> a = ArrayComprehension(10*i + j, (i, 1, 4), (j, 1, 3))
        >>> a.variables
        [i, j]
        c                     g | ]
}|d          S )r    r   s     r   
<listcomp>z0ArrayComprehension.variables.<locals>.<listcomp>{   s    +++!+++r   r3   r0   s    r   	variableszArrayComprehension.variablesm   s     ,+dl++++r   c                 $    d | j         D             S )zThe list of dummy variables.

        Note
        ====

        Note that all variables are dummy variables since a limit without
        lower bound or upper bound is not accepted.
        c                 D    g | ]}t          |          d k    |d         S )r   r   r   r   s     r   rA   z4ArrayComprehension.bound_symbols.<locals>.<listcomp>   s'    :::c!ffkk!kkkr   r3   r0   s    r   bound_symbolsz ArrayComprehension.bound_symbols}   s     ;:dl::::r   c                     | j         S )aE  
        The shape of the expanded array, which may have symbols.

        Note
        ====

        Both the lower and the upper bounds are included while
        calculating the shape.

        Examples
        ========

        >>> from sympy.tensor.array import ArrayComprehension
        >>> from sympy import symbols
        >>> i, j, k = symbols('i j k')
        >>> a = ArrayComprehension(10*i + j, (i, 1, 4), (j, 1, 3))
        >>> a.shape
        (4, 3)
        >>> b = ArrayComprehension(10*i + j, (i, 1, 4), (j, 1, k+3))
        >>> b.shape
        (4, k + 3)
        )r$   r0   s    r   shapezArrayComprehension.shape   s    0 {r   c                 x    | j         D ]1\  }}}t          ||                              t                    r dS 2dS )a  
        Test if the array is shape-numeric which means there is no symbolic
        dimension.

        Examples
        ========

        >>> from sympy.tensor.array import ArrayComprehension
        >>> from sympy import symbols
        >>> i, j, k = symbols('i j k')
        >>> a = ArrayComprehension(10*i + j, (i, 1, 4), (j, 1, 3))
        >>> a.is_shape_numeric
        True
        >>> b = ArrayComprehension(10*i + j, (i, 1, 4), (j, 1, k+3))
        >>> b.is_shape_numeric
        False
        FT)r"   r   atomsr	   )r1   _r;   r<   s       r   is_shape_numericz#ArrayComprehension.is_shape_numeric   sJ    &  < 	 	KAsCS#$$V,, uutr   c                     | j         S )a9  The rank of the expanded array.

        Examples
        ========

        >>> from sympy.tensor.array import ArrayComprehension
        >>> from sympy import symbols
        >>> i, j, k = symbols('i j k')
        >>> a = ArrayComprehension(10*i + j, (i, 1, 4), (j, 1, 3))
        >>> a.rank()
        2
        )r%   r0   s    r   rankzArrayComprehension.rank   s     zr   c                 F    | j         j        rt          d          | j         S )a  
        The length of the expanded array which means the number
        of elements in the array.

        Raises
        ======

        ValueError : When the length of the array is symbolic

        Examples
        ========

        >>> from sympy.tensor.array import ArrayComprehension
        >>> from sympy import symbols
        >>> i, j = symbols('i j')
        >>> a = ArrayComprehension(10*i + j, (i, 1, 4), (j, 1, 3))
        >>> len(a)
        12
        z Symbolic length is not supported)r'   r6   r   r0   s    r   __len__zArrayComprehension.__len__   s)    ( ?' 	A?@@@r   c                    g }|D ]\  }}}t          |          }t          |          }t          |t                    r
t          | }nt          |          }|                    t          |||                     t          d ||fD                       rt          d          ||k    dk    rt          d          ||j        v s	||j        v rt          d          |S )Nc              3      K   | ]P}t          |t                     p5|                    t          t                    |                                k    V  Qd S N)
isinstancer   rI   r	   r
   )r   is     r   r   z<ArrayComprehension._check_limits_validity.<locals>.<genexpr>   sk       U UDE #1d+++U0H0HAGGII0U U U U U U Ur   zABounds should be an Expression(combination of Integer and Symbol)Tz-Lower bound should be inferior to upper boundz)Variable should not be part of its bounds)	r   rS   listr   appendr   	TypeErrorr   r6   )r)   r*   r4   
new_limitsr:   r;   r<   s          r   r   z)ArrayComprehension._check_limits_validity   s    
# 	N 	NMCc3--C3--C #t$$ $SksmmeCc22333 U UJMsU U U U U e cdddc	d"" !PQQQc&&&#1A*A*A !LMMM +Br   c                 4    t          d |D                       S )Nc                 &    g | ]\  }}}||z
  d z   S r   r@   )r   rJ   r;   r<   s       r   rA   zCArrayComprehension._calculate_shape_from_limits.<locals>.<listcomp>   s&    >>>3cCi!m>>>r   )tuple)r)   r4   s     r   r#   z/ArrayComprehension._calculate_shape_from_limits   s    >>v>>>???r   c                 &    |sdS d}|D ]}||z  }|S )Nr   r   r@   )r)   rG   	loop_sizer   s       r   r&   z'ArrayComprehension._calculate_loop_size   s4     	1	 	& 	&A!AIIr   c                 <    | j         s| S |                                 S rR   )rK   _expand_array)r1   hintss     r   doitzArrayComprehension.doit  s$    $ 	K!!###r   c                     g }t          j        d | j        D              D ]*}|                    |                     |                     +t          || j                  S )Nc                 <    g | ]\  }}}t          ||d z             S r[   )range)r   r:   r;   r<   s       r   rA   z4ArrayComprehension._expand_array.<locals>.<listcomp>  s<     *9 *9 *9,9Cc +0SU*;*; *9 *9 *9r   )	itertoolsproductr"   rV   _get_elementr   rG   )r1   resvaluess      r   r`   z ArrayComprehension._expand_array  ss    ' *9 *9+/<*9 *9 *9 : 	2 	2F JJt((001111&sDJ777r   c                 v    | j         }t          | j        |          D ]\  }}|                    ||          }|S rR   )r*   ziprB   subs)r1   rj   tempr:   vals        r   rh   zArrayComprehension._get_element  sB    }DNF33 	' 	'HC99S#&&DDr   c                 z    | j         r&|                                                                 S t          d          )a  Transform the expanded array to a list.

        Raises
        ======

        ValueError : When there is a symbolic dimension

        Examples
        ========

        >>> from sympy.tensor.array import ArrayComprehension
        >>> from sympy import symbols
        >>> i, j = symbols('i j')
        >>> a = ArrayComprehension(10*i + j, (i, 1, 4), (j, 1, 3))
        >>> a.tolist()
        [[11, 12, 13], [21, 22, 23], [31, 32, 33], [41, 42, 43]]
        z-A symbolic array cannot be expanded to a list)rK   r`   tolistr   r0   s    r   rq   zArrayComprehension.tolist  s<    $   	1%%''..000HIIIr   c                     ddl m} | j        st          d          | j        dk    rt          d           ||                                                                           S )aE  Transform the expanded array to a matrix.

        Raises
        ======

        ValueError : When there is a symbolic dimension
        ValueError : When the rank of the expanded array is not equal to 2

        Examples
        ========

        >>> from sympy.tensor.array import ArrayComprehension
        >>> from sympy import symbols
        >>> i, j = symbols('i j')
        >>> a = ArrayComprehension(10*i + j, (i, 1, 4), (j, 1, 3))
        >>> a.tomatrix()
        Matrix([
        [11, 12, 13],
        [21, 22, 23],
        [31, 32, 33],
        [41, 42, 43]])
        r   )Matrixz/A symbolic array cannot be expanded to a matrix   zDimensions must be of size of 2)sympy.matricesrs   rK   r   r%   r`   tomatrix)r1   rs   s     r   rv   zArrayComprehension.tomatrix3  ss    . 	*)))))$ 	PNOOO:??>???vd((**3355666r   N)__name__
__module____qualname____doc__r    propertyr*   r4   r6   rB   rE   rG   rK   rM   rO   classmethodr   r#   r&   rb   r`   rh   rq   rv   r@   r   r   r   r   
   s        2     X   X    X4 , , X, 	; 	; X	;   X2   X.    0   [, @ @ [@   [$ $ $8 8 8  J J J.7 7 7 7 7r   r   c                 b    d }t          | t          |                    o| j        |j        k    S )Nc                      dS )Nr   r@   r@   r   r   <lambda>zisLambda.<locals>.<lambda>U  s    Q r   )rS   typerw   )vLAMBDAs     r   isLambdar   T  s-    YFaf&&H1:+HHr   c                   4    e Zd ZdZd Zed             Zd ZdS )ArrayComprehensionMapa[  
    A subclass of ArrayComprehension dedicated to map external function lambda.

    Notes
    =====

    Only the lambda function is considered.
    At most one argument in lambda function is accepted in order to avoid ambiguity
    in value assignment.

    Examples
    ========

    >>> from sympy.tensor.array import ArrayComprehensionMap
    >>> from sympy import symbols
    >>> i, j, k = symbols('i j k')
    >>> a = ArrayComprehensionMap(lambda: 1, (i, 1, 4))
    >>> a.doit()
    [1, 1, 1, 1]
    >>> b = ArrayComprehensionMap(lambda a: a+1, (j, 1, 4))
    >>> b.doit()
    [2, 3, 4, 5]

    c                    t          d |D                       rt          d          t          |          st          d          |                     ||          }t	          j        | g|R i |}|j        |_        |                     |j                  |_	        t          |j	                  |_        |                     |j	                  |_        ||_        |S )Nc              3   @   K   | ]}t          |          d k    pdV  dS r   r   r   s     r   r   z0ArrayComprehensionMap.__new__.<locals>.<genexpr>r  r   r   r   zData type not supported)r   r   r   r   r   r    r!   r"   r#   r$   r   r%   r&   r'   _lambdar(   s         r   r    zArrayComprehensionMap.__new__q  s    44G44444 	5 4 5 5 5 !! 	86777,,Xw??mC9'999[99i55ckBB

OO	11#*==
r   c                 2      G  fddt                     }|S )Nc                       e Zd Z fdZdS )%ArrayComprehensionMap.func.<locals>._c                 .    t          j        g|R i |S rR   )r   r   )r)   argskwargsr1   s      r   r    z-ArrayComprehensionMap.func.<locals>._.__new__  s#    ,T\KDKKKFKKKr   N)rw   rx   ry   r    r0   s   r   rJ   r     s5        L L L L L L Lr   rJ   )r   )r1   rJ   s   ` r   funczArrayComprehensionMap.func  sI    	L 	L 	L 	L 	L 	L 	L% 	L 	L 	L r   c                     | j         }| j         j        j        dk    r |            }n4| j         j        j        dk    r |t          j        d |                    }|S )Nr   r   c                     | |z  S rR   r@   )abs     r   r   z4ArrayComprehensionMap._get_element.<locals>.<lambda>  s
    ac r   )r   __code__co_argcount	functoolsreduce)r1   rj   rn   s      r   rh   z"ArrayComprehensionMap._get_element  sa    |< ,11466DD\".!334	()9)96BBCCDr   N)rw   rx   ry   rz   r    r{   r   rh   r@   r   r   r   r   X  sW         0  "   X    r   r   )r   rf   sympy.core.sympifyr   r   sympy.core.exprr   
sympy.corer   r   sympy.tensor.arrayr   sympy.core.symbolr	   sympy.core.numbersr
   r   r   r   r@   r   r   <module>r      s           0 0 0 0 0 0 0 0             # # # # # # # # 6 6 6 6 6 6 $ $ $ $ $ $ & & & & & &G7 G7 G7 G7 G7 G7 G7 G7T
I I I7 7 7 7 7. 7 7 7 7 7r   