
    \h/                         S SK r S SKrS SKJrJr  S SKJr  S SKJrJ	r	  S SK
Jr  S SKJr  S SKJr   " S S	\5      rS
 r " S S\5      rg)    N)_sympifysympify)Expr)BasicTuple)ImmutableDenseNDimArray)Symbol)Integerc                       \ rS rSrSrS r\S 5       r\S 5       r\S 5       r	\S 5       r
\S 5       r\S	 5       r\S
 5       rS rS r\S 5       r\S 5       r\S 5       rS rS rS rS rS rSrg)ArrayComprehension
   a  
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                    [        S U 5       5      (       a  [        S5      e[        U5      /nUR                  U R	                  X5      5        [
        R                  " U /UQ70 UD6nUR                  SS  Ul        U R                  UR                  5      Ul
        [        UR                  5      Ul        U R                  UR                  5      Ul        U$ )Nc              3   P   #    U  H  n[        U5      S :g  =(       d    Sv   M     g7f   Nlen.0ls     ^/var/www/auris/envauris/lib/python3.13/site-packages/sympy/tensor/array/array_comprehension.py	<genexpr>-ArrayComprehension.__new__.<locals>.<genexpr>%         4Gqs1v{"d"G   $&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"   ArrayComprehension.__new__$   s    4G444 4 5 58$%s11(DEmmC9'9[9iim55ckkB


O	11#**=
    c                      U R                   S   $ )zThe 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,   ArrayComprehension.function1   s     zz!}r2   c                     U R                   $ )a%  
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$   r4   s    r   limitsArrayComprehension.limitsA   s     ||r2   c                     U R                   R                  nU R                   HM  u  p#nUR                  U5        UR                  R	                  UR                  5      nUR	                  U5      nMO     U$ )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)r5   expr_free_symvarinfsupcurr_free_symss         r   r<   ArrayComprehension.free_symbolsR   sg    ( 22!\\MCc!!#& --33C4D4DEN)//?M * r2   c                 J    U R                    Vs/ s H  oS   PM	     sn$ s  snf )a  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]
r   r8   r5   r   s     r   	variablesArrayComprehension.variablesm   s"     #ll+l!l+++s    c                 n    U R                    Vs/ s H  n[        U5      S:w  d  M  US   PM     sn$ s  snf )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.
r   r   )r$   r   rF   s     r   bound_symbols ArrayComprehension.bound_symbols}   s0     #ll:lc!fk!l:::s   2	2c                     U R                   $ )a  
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&   r4   s    r   shapeArrayComprehension.shape   s    0 {{r2   c                 z    U R                    H+  u  pn[        X#5      R                  [        5      (       d  M+    g   g)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	   )r5   _rA   rB   s       r   is_shape_numeric#ArrayComprehension.is_shape_numeric   s3    &  <<KACS$$V,, ( r2   c                     U R                   $ )zThe 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'   r4   s    r   rankArrayComprehension.rank   s     zzr2   c                 f    U R                   R                  (       a  [        S5      eU R                   $ )ad  
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)   r<   r   r4   s    r   __len__ArrayComprehension.__len__   s'    ( ??''?@@r2   c                    / nU H  u  pEn[        U5      n[        U5      n[        U[        5      (       a	  [        U6 nO[        U5      nUR	                  [        XEU5      5        [        S XV4 5       5      (       a  [        S5      eXV:  S:X  a  [        S5      eXER                  ;   d  XFR                  ;   d  M  [        S5      e   U$ )Nc              3      #    U  HL  n[        U[        5      (       + =(       d+    UR                  [        [        5      UR                  5       :g  v   MN     g 7fN)
isinstancer   rP   r	   r
   )r   is     r   r   <ArrayComprehension._check_limits_validity.<locals>.<genexpr>   s@      UISA #1d++U0HAGGI0UUISs   AA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   r]   listr   appendr   	TypeErrorr   r<   )r+   r,   r9   
new_limitsr@   rA   rB   s          r   r!   )ArrayComprehension._check_limits_validity   s     
#MCc3-C3-C #t$$SksmeCc23 UJMU U U cdd	d" !PQQ&&&#1A1A*A !LMM! $" r2   c           
      `    [        U VVVs/ s H  u  p#oDU-
  S-   PM     snnn5      $ s  snnnf Nr   )tuple)r+   r9   rQ   rA   rB   s        r   r%   /ArrayComprehension._calculate_shape_from_limits   s)    v>vCi!mv>??>s   )c                 4    U(       d  gSnU H  nX#-  nM	     U$ )Nr   r    )r+   rM   	loop_sizer   s       r   r(   'ArrayComprehension._calculate_loop_size   s&    	A!I  r2   c                 H    U R                   (       d  U $ U R                  5       $ r\   )rR   _expand_array)r5   hintss     r   doitArrayComprehension.doit  s    $$K!!##r2   c                    / n[         R                  " U R                   VVVs/ s H  u  p#n[        X4S-   5      PM     snnn6  H#  nUR	                  U R                  U5      5        M%     [        XR                  5      $ s  snnnf rf   )	itertoolsproductr$   rangera   _get_elementr   rM   )r5   resr@   rA   rB   valuess         r   rn    ArrayComprehension._expand_array  sz    ''+/<<*9+7 -:Cc +0U*;+7*9 :F JJt((01:
 'sJJ77*9s   B c                 ~    U R                   n[        U R                  U5       H  u  p4UR                  X45      nM     U$ r\   )r,   ziprG   subs)r5   rx   tempr@   vals        r   rv   ArrayComprehension._get_element  s5    }}DNNF3HC99S&D 4r2   c                 v    U R                   (       a  U R                  5       R                  5       $ [        S5      e)am  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)rR   rn   tolistr   r4   s    r   r   ArrayComprehension.tolist  s1    $   %%'..00HIIr2   c                     SSK Jn  U R                  (       d  [        S5      eU R                  S:w  a  [        S5      eU" U R                  5       R                  5       5      $ )a  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.matricesr   rR   r   r'   rn   tomatrix)r5   r   s     r   r   ArrayComprehension.tomatrix3  sP    . 	*$$NOO::?>??d((*33566r2   rj   N)__name__
__module____qualname____firstlineno____doc__r"   propertyr,   r9   r<   rG   rJ   rM   rR   rU   rX   classmethodr!   r%   r(   rp   rn   rv   r   r   __static_attributes__rj   r2   r   r   r   
   s    2       4 , , 	; 	;  2  .0  , @ @  $8J.7r2   r   c                 r    S n[        U [        U5      5      =(       a    U R                  UR                  :H  $ )Nc                      g)Nr   rj   rj   r2   r   <lambda>isLambda.<locals>.<lambda>U  s    Qr2   )r]   typer   )vLAMBDAs     r   isLambdar   T  s*    Faf&H1::+HHr2   c                   4    \ rS rSrSrS r\S 5       rS rSr	g)ArrayComprehensionMapiX  a  
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                    [        S U 5       5      (       a  [        S5      e[        U5      (       d  [        S5      eU R                  X5      n[        R
                  " U /UQ70 UD6nUR                  Ul        U R                  UR                  5      Ul	        [        UR                  5      Ul        U R                  UR                  5      Ul        Xl        U$ )Nc              3   P   #    U  H  n[        U5      S :g  =(       d    Sv   M     g7fr   r   r   s     r   r   0ArrayComprehensionMap.__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"   ArrayComprehensionMap.__new__q  s    4G444 4 5 5 !!677,,X?mmC9'9[9ii55ckkB


O	11#**=
r2   c                 ,   ^   " U 4S jS[         5      nU$ )Nc                   "   > \ rS rSrU 4S jrSrg)%ArrayComprehensionMap.func.<locals>._i  c                 6   > [        TR                  /UQ70 UD6$ r\   )r   r   )r+   argskwargsr5   s      r   r"   -ArrayComprehensionMap.func.<locals>._.__new__  s    ,T\\KDKFKKr2   rj   N)r   r   r   r   r"   r   r4   s   r   rQ   r     s    L Lr2   rQ   )r   )r5   rQ   s   ` r   funcArrayComprehensionMap.func  s    	L% 	L r2   c                     U R                   nU R                   R                  R                  S:X  a	  U" 5       nU$ U R                   R                  R                  S:X  a  U" [        R                  " S U5      5      nU$ )Nr   r   c                 
    X-  $ r\   rj   )abs     r   r   4ArrayComprehensionMap._get_element.<locals>.<lambda>  s    acr2   )r   __code__co_argcount	functoolsreduce)r5   rx   r}   s      r   rv   "ArrayComprehensionMap._get_element  sh    ||<<  ,,16D  \\""..!3	(()96BCDr2   rj   N)
r   r   r   r   r   r"   r   r   rv   r   rj   r2   r   r   r   X  s%    0"  r2   r   )r   rs   sympy.core.sympifyr   r   sympy.core.exprr   
sympy.corer   r   sympy.tensor.arrayr   sympy.core.symbolr	   sympy.core.numbersr
   r   r   r   rj   r2   r   <module>r      s<     0   # 6 $ &G7 G7T
I7. 7r2   