
    Pi                     P   d dl Z d dlZd dlmZ d dlmZmZ d dlmZ	 e j
        ddd            Zddded	e j        d
ede	j        deeee	j        f                  f
dZddded	e j        d
ede	j        def
dZe j        j        ddded	e j        d
ede	j        ddf
d            ZdS )    N)defaultdict)MappingOptionalTcsec          
         t           j        j                            | |          \  } }t           j        j                            |           }t          |          t          |          k    r/t          dt          |           dt          |                     	 t           j        	                    d t          ||          D             d |                                D             z   |          }nK# t           j        j        j        t           j        j        j        t           j        j        j        f$ r Y d S w xY wt#          t$                    }|D ]}|                                D ]}t)          |t           j        j        j                  ra|j                            d          }||d                                      t3          d |d	d          D                       |j        f           i }	|                                D ]j\  }
}t7          j        d
 |D             d          d	z   }t7          j        |d          }|D ]
\  }}|||<   |j        dk    rt?          |          }||	|
<   k|	S )N	Expected  tensors, got c                 T    g | ]%\  }}t           j                            ||          &S  einxexprEquation.0r   tensor_shapes      a/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/einx/op/solve.py
<listcomp>z_solve.<locals>.<listcomp>   s>       &D, 	""466      c                     g | ]L\  }}t           j                            |t          j        |          d t          j        f         dd          MS .N)depth1depth2r   r   r   npasarraynewaxisr   kvs      r   r   z_solve.<locals>.<listcomp>   sY       Aq 	""1bjmmCO&DTZ^"__  r   r   .r   c              3   4   K   | ]}t          |          V  d S )N)int)r   ts     r   	<genexpr>z_solve.<locals>.<genexpr>*   s(      /K/K1A/K/K/K/K/K/Kr      c                     g | ]\  }}|S r   r   )r   coordvalues      r   r   z_solve.<locals>.<listcomp>.   s    666<5%666r   )axisint32)dtyper   ) r   oputil!_clean_description_and_parametersr   stage1
parse_argslen
ValueErrorsolvezipitemsstage2SolveDepthExceptionSolveExpansionExceptionstage3SolveValueExceptionr   listall
isinstanceAxisnamesplitappendtupler*   r   amaxzerosshaper$   )descriptionr   tensor_shapes
parametersexprsvaluesrootr   tokensvalues2rA   xsrG   r*   r)   r!   s                   r   _solverQ      s   "glLLZ K I''44E
5zzS''''SSZZSSs=?Q?QSSTTT	 *-e]*C*C   &,,..  	    

 

 		,	0	,   
 tt F Z ZHHJJ 	Z 	ZD$	 0 566 Z--vay!((%/K/Kqrr
/K/K/K*K*KTZ)XYYY	Z
 GLLNN  b662666Q???!Cg... 	 	HE1E%LL;"JJENs   AC6 6AD>=D>FrH   tensorsr   rJ   returnc                :    t          | gd |D             R d|i|S )a  Solve for the axis values of the given expressions and tensors.

    Args:
        description: Description string for the tensors in einx notation.
        tensors: Input tensors or tensor factories matching the description string.
        cse: Whether to apply common subexpression elimination to the expressions.
            Defaults to False.
        **parameters: Additional parameters that specify values for single axes, e.g. ``a=4``.

    Returns:
        A mapping from axis name to axis value, or ``None`` if no solution was found.

    Examples:
        >>> x = np.zeros((10, 5))
        >>> einx.solve("a b", x)
        {'a': 10, 'b': 5}
    c                 L    g | ]!}t           j                            |          "S r   r   tracer	get_shaper   tensors     r   r   zsolve.<locals>.<listcomp>N   s(    KKKt{,,V44KKKr   r   )rQ   rH   r   rR   rJ   s       r   r5   r5   9   sI    ( KK7KKK  QTXb  r   c                *    t          | g|R d|i|duS )a  Check whether the given expressions and tensors match.

    Args:
        description: Description string for the tensors in einx notation.
        tensors: Input tensors or tensor factories matching the description string.
        cse: Whether to apply common subexpression elimination to the expressions.
            Defaults to False.
        **parameters: Additional parameters that specify values for single axes, e.g. ``a=4``.

    Returns:
        True if the expressions and tensors match, False otherwise.

    Examples:
        >>> x = np.zeros((10, 5))
        >>> einx.matches("a b", x)
        True
        >>> einx.matches("a b c", x)
        False
    r   N)r5   r[   s       r   matchesr]   R   s,    , >w>>>C>:>>dJJr   c                   t           j        j                            | |          \  } }t           j        j                            |           }t          |          t          |          k    r/t          dt          |           dt          |                     d |D             }t           j        	                    d t          ||          D             d |                                D             z   |           dS )a  Check whether the given expressions and tensors match and raise an exception if they don't.

    Args:
        description: Description string for the tensors in einx notation.
        tensors: Input tensors or tensor factories matching the description string.
        cse: Whether to apply common subexpression elimination to the expressions.
            Defaults to False.
        **parameters: Additional parameters that specify values for single axes, e.g. ``a=4``.
    r	   r
   c                 L    g | ]!}t           j                            |          "S r   rV   rY   s     r   r   zcheck.<locals>.<listcomp>   s(    IIIvT[**622IIIr   c                 T    g | ]%\  }}t           j                            ||          &S r   r   r   s      r   r   zcheck.<locals>.<listcomp>   s/    ddd4FD,		D,	/	/dddr   c                     g | ]L\  }}t           j                            |t          j        |          d t          j        f         dd          MS r   r   r   s      r   r   zcheck.<locals>.<listcomp>   sY     
 
 
1 Iq"*Q--RZ"@VZ[[
 
 
r   r   N)r   r.   r/   r0   r   r1   r2   r3   r4   r5   r6   r7   )rH   r   rR   rJ   rK   rI   s         r   checkrb   k   s    #glLLZ K I''44E
5zzS\\!!MSZZMMs7||MMNNNIIIIIMIOOdd#eUbJcJcddd
 
"((**
 
 
	

       r   )r   numpyr   collectionsr   typingr   r   numpy.typingnpt	lru_cacherQ   strTensorbool	ArrayLiker5   r]   traceback_utilfilterrb   r   r   r   <module>ro      s        # # # # # # $ $ $ $ $ $ $ $       ,0 - - - - -b :?   $26NQmgc3=()*   4 :>K K KK $K26KMP]K	K K K K2 9=   $26MP]	     r   