
    Qi!                         d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZmZmZ d d	lmZ  G d
 de          Z G d dee          Z G d dee          ZdS )    N)List)Basic)Tuple)S)_sympify)MutableNDimArray)	NDimArrayImmutableNDimArray	ArrayKind)flattenc                   p    e Zd ZU ee         ed<   d Zedefd            Z	d Z
ed             Zd Zd Zd	S )
DenseNDimArray_arrayc                     t          |i |S NImmutableDenseNDimArray)selfargskwargss      {/var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/sympy/tensor/array/dense_ndim_array.py__new__zDenseNDimArray.__new__   s    &7777    returnc                 4    t          j        | j                  S r   )r   _unionr   r   s    r   kindzDenseNDimArray.kind   s    ,,,r   c                                                     }||S                                t          t                    rst	          d D                       rZ                               \  }} fd|D             }fdt          |          D             } t                     ||          S                                 j	                 S )aL  
        Allows to get items from N-dim array.

        Examples
        ========

        >>> from sympy import MutableDenseNDimArray
        >>> a = MutableDenseNDimArray([0, 1, 2, 3], (2, 2))
        >>> a
        [[0, 1], [2, 3]]
        >>> a[0, 0]
        0
        >>> a[1, 1]
        3
        >>> a[0]
        [0, 1]
        >>> a[1]
        [2, 3]


        Symbolic index:

        >>> from sympy.abc import i, j
        >>> a[i, j]
        [[0, 1], [2, 3]][i, j]

        Replace `i` and `j` to get element `(1, 1)`:

        >>> a[i, j].subs({i: 1, j: 1})
        3

        Nc              3   @   K   | ]}t          |t                    V  d S r   
isinstanceslice.0is     r   	<genexpr>z-DenseNDimArray.__getitem__.<locals>.<genexpr>?   ,      +P+PQJq%,@,@+P+P+P+P+P+Pr   c                 P    g | ]"}j                             |                   #S  )r   _parse_index)r%   r&   r   s     r   
<listcomp>z.DenseNDimArray.__getitem__.<locals>.<listcomp>A   s.    III1T[!2!21!5!56IIIr   c                 j    g | ]/\  }}t          |         t                     t          |          0S r*   )r"   r#   len)r%   r&   elindexs      r   r,   z.DenseNDimArray.__getitem__.<locals>.<listcomp>B   s8    ```%!RJuUVxY^D_D_`c"gg```r   )
_check_symbolic_index_check_index_for_getitemr"   tupleany _get_slice_data_for_array_access	enumeratetyper+   r   )r   r0   syindex
sl_factorseindicesarraynshapes   ``     r   __getitem__zDenseNDimArray.__getitem__   s    B ,,U33N--e44eU## 	&+P+P%+P+P+P(P(P 	&#'#H#H#O#O JIIIIIIIE````9Z+@+@```F4::eV,,,%%e,,E;u%%r   c                 z    t          j        d |t          j                  }|                     dg|z  f|          S )Nc                     | |z  S r   r*   xys     r   <lambda>z&DenseNDimArray.zeros.<locals>.<lambda>J   s
    AaC r   r   )	functoolsreducer   One_new)clsshapelist_lengths      r   zeroszDenseNDimArray.zerosH   s9    &'7'7FFxx![*E222r   c                     ddl m} |                                 dk    rt          d           || j        d         | j        d         | j                  S )a  
        Converts MutableDenseNDimArray to Matrix. Can convert only 2-dim array, else will raise error.

        Examples
        ========

        >>> from sympy import MutableDenseNDimArray
        >>> a = MutableDenseNDimArray([1 for i in range(9)], (3, 3))
        >>> b = a.tomatrix()
        >>> b
        Matrix([
        [1, 1, 1],
        [1, 1, 1],
        [1, 1, 1]])

        r   )Matrix   zDimensions must be of size of 2   )sympy.matricesrM   rank
ValueErrorrI   r   )r   rM   s     r   tomatrixzDenseNDimArray.tomatrixM   sZ    " 	*)))))99;;!>???vdjmTZ]DK@@@r   c                     t          j        d |          }|| j        k    r't          d| j        t	          |          |fz             t          |           | j        |          S )a  
        Returns MutableDenseNDimArray instance with new shape. Elements number
        must be        suitable to new shape. The only argument of method sets
        new shape.

        Examples
        ========

        >>> from sympy import MutableDenseNDimArray
        >>> a = MutableDenseNDimArray([1, 2, 3, 4, 5, 6], (2, 3))
        >>> a.shape
        (2, 3)
        >>> a
        [[1, 2, 3], [4, 5, 6]]
        >>> b = a.reshape(3, 2)
        >>> b.shape
        (3, 2)
        >>> b
        [[1, 2], [3, 4], [5, 6]]

        c                     | |z  S r   r*   r@   s     r   rC   z(DenseNDimArray.reshape.<locals>.<lambda>{   s
    ac r   z2Expecting reshape size to %d but got prod(%s) = %d)rD   rE   
_loop_sizerR   strr7   r   )r   newshapenew_total_sizes      r   reshapezDenseNDimArray.reshapee   su    , #)//8DDT_,,QXU@ @ A A A tDzz$+x000r   N)__name__
__module____qualname__r   r   __annotations__r   propertyr   r   r=   classmethodrK   rS   rZ   r*   r   r   r   r      s         K8 8 8 -i - - - X-.& .& .&` 3 3 [3A A A01 1 1 1 1r   r   c                   >    e Zd ZddZed             Zd Zd Zd ZdS )r   Nc                       | j         ||fi |S r   rG   rH   iterablerI   r   s       r   r   zImmutableDenseNDimArray.__new__       sx%226222r   c                 |    | j         ||fi |\  }}t          t          t          |           }|                     ||           t          |          }t          | }t          j        | ||fi |}||_        t          |          |_
        t          |          |_        t          j        d |d          |_        |S )Nc                     | |z  S r   r*   r@   s     r   rC   z.ImmutableDenseNDimArray._new.<locals>.<lambda>   
    qs r   rO   )_handle_ndarray_creation_inputsr   mapr   _check_special_boundsr   r   r   _shapelistr   r.   _rankrD   rE   rV   rH   re   rI   r   	flat_listr   s         r   rG   zImmutableDenseNDimArray._new   s    >3>xYYRXYYys8U++,!!)U333I&&	9%	}S)U==f==9ooZZ
#*??E1EEr   c                      t          d          )Nzimmutable N-dim array)	TypeError)r   r0   values      r   __setitem__z#ImmutableDenseNDimArray.__setitem__   s    /000r   c                      t          |           S r   )MutableDenseNDimArrayr   s    r   
as_mutablez"ImmutableDenseNDimArray.as_mutable   s    $T***r   c                 8    ddl m} |                     |          S )Nr   )simplify)sympy.simplify.simplifyrz   	applyfunc)r   r   rz   s      r   _eval_simplifyz&ImmutableDenseNDimArray._eval_simplify   s&    444444~~h'''r   r   )	r[   r\   r]   r   r`   rG   ru   rx   r}   r*   r   r   r   r      sp        3 3 3 3   [1 1 1+ + +( ( ( ( (r   r   c                   N    e Zd ZddZed             Zd Zd Zed             Z	dS )rw   Nc                       | j         ||fi |S r   rc   rd   s       r   r   zMutableDenseNDimArray.__new__   rf   r   c                 2    | j         ||fi |\  }}t          |          }t                              |           }||_        t          |          |_        t          |          |_        |rt          j
        d |          nt          |          |_        |S )Nc                     | |z  S r   r*   r@   s     r   rC   z,MutableDenseNDimArray._new.<locals>.<lambda>   ri   r   )rj   r   objectr   rm   rn   r   r.   ro   rD   rE   rV   rp   s         r   rG   zMutableDenseNDimArray._new   s    >3>xYYRXYYyI&&	~~c""9ooZZ
FK_)*??EBBBQTU^Q_Q_r   c                    t          |t                    rwt          d |D                       r^|                     ||          \  }}}|D ]?}d t	          ||          D             }||         | j        |                     |          <   @dS |                     |          }|                     |           t          |          }|| j        |<   dS )a  Allows to set items to MutableDenseNDimArray.

        Examples
        ========

        >>> from sympy import MutableDenseNDimArray
        >>> a = MutableDenseNDimArray.zeros(2,  2)
        >>> a[0,0] = 1
        >>> a[1,1] = 1
        >>> a
        [[1, 0], [0, 1]]

        c              3   @   K   | ]}t          |t                    V  d S r   r!   r$   s     r   r'   z4MutableDenseNDimArray.__setitem__.<locals>.<genexpr>   r(   r   c                 "    g | ]\  }}|||z
  S r   r*   )r%   indjs      r   r,   z5MutableDenseNDimArray.__setitem__.<locals>.<listcomp>   s!    XXXvsA!-37---r   N)	r"   r3   r4   $_get_slice_data_for_array_assignmentzipr   r+   _setter_iterable_checkr   )r   r0   rt   r:   slice_offsetsr&   other_is          r   ru   z!MutableDenseNDimArray.__setitem__   s     eU## 		'+P+P%+P+P+P(P(P 		'-1-V-VW\^c-d-d*E8] C CXXQ1F1FXXX49'ND--a0011C C %%e,,E''...UOOE!&DKr   c                      t          |           S r   r   r   s    r   as_immutablez"MutableDenseNDimArray.as_immutable   s    &t,,,r   c                 $    d | j         D             S )Nc                 &    h | ]}|j         D ]}|S r*   )free_symbols)r%   r   r&   s      r   	<setcomp>z5MutableDenseNDimArray.free_symbols.<locals>.<setcomp>   s'    ???a??1????r   )r   r   s    r   r   z"MutableDenseNDimArray.free_symbols   s    ??4;????r   )NN)
r[   r\   r]   r   r`   rG   ru   r   r_   r   r*   r   r   rw   rw      s        3 3 3 3   [' ' '2- - - @ @ X@ @ @r   rw   )rD   typingr   sympy.core.basicr   sympy.core.containersr   sympy.core.singletonr   sympy.core.sympifyr   %sympy.tensor.array.mutable_ndim_arrayr   sympy.tensor.array.ndim_arrayr	   r
   r   sympy.utilities.iterablesr   r   r   rw   r*   r   r   <module>r      sN             " " " " " " ' ' ' ' ' ' " " " " " " ' ' ' ' ' ' B B B B B B R R R R R R R R R R - - - - - -t1 t1 t1 t1 t1Y t1 t1 t1n( ( ( ( (n.@ ( ( (8.@ .@ .@ .@ .@N,< .@ .@ .@ .@ .@r   