o
    ohR                     @   s  d Z ddlZddlmZ ddlmZmZ ddlmZ e	dZ
e
ej edZe
ej ersddlZejZddlmZ dd	l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 dD \Z Z!Z"dd dD \Z#Z$Z%ndZ&ddl'Z(ddl)m*Z* ddl+m,Z,m-Z-m.Z.m/Z/ ddlm0Z0m1Z1m2Z2 dd dD \Z3Z4Z5e(6de/Z7dd Z8dd Z9dd Z:dd  Z;d!d" Z<d#d$ Z=d%d& Z>d'd( Z?ed)d* Z@d+d, ZAd-d. ZBd/d0 ZCd1d2 ZDd3d4 ZEd5d6 ZFd7d8 ZGd9d: ZHd;d< ZId=d> ZJd?d@ ZKdAdB ZLdCdD ZMdEdF ZNdGdH ZOdIdJ ZPdKdL ZQdMdN ZRdOdP ZSdQdR ZTdSdT ZUdUdV ZVdWdX ZWdYdZ ZXd[d\ ZYed]d^ ZZd_d` Z[dadb Z\dcdd Z]dedf Z^dgdh Z_didj Z`dkdl Zadmdn Zbdodp Zcdqdr ZddS )sa  
Important note on tests in this module - the Aesara printing functions use a
global cache by default, which means that tests using it will modify global
state and thus not be independent from each other. Instead of using the "cache"
keyword argument each time, this module uses the aesara_code_ and
aesara_function_ functions defined below which default to using a new, empty
cache instead.
    N)import_module)raisesSKIP)ignore_warningszaesara.configdefaultsaesara)
ScalarType)Variable)TensorVariable)Elemwise
DimShuffle)Dot)true_dividec                 C   s   g | ]}t |d qS )floatX)aetscalar.0name r   x/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/sympy/printing/tests/test_aesaracode.py
<listcomp>#       r   xyzc                 C   s   g | ]
}t jd d|dqS )r   FFr   )r   tensorr   nr   r   r   r   $   s    XYZT)S)xyzt)aesara_codedim_handlingaesara_functionc                 C   s   g | ]	}t |d d qS )   syMatrixSymbolr   r   r   r   r   2       fc                 K   s   | di  t| fi |S )zB Wrapper for aesara_code that uses a new, empty cache by default. cache)
setdefaultr$   )exprkwargsr   r   r   aesara_code_8   s   r1   c                 K   s   | di  t| |fi |S )zF Wrapper for aesara_function that uses a new, empty cache by default. r-   )r.   r&   )inputsoutputsr0   r   r   r   aesara_function_=   s   r4   c                  G   sD   t tt| }t tjj|}tjj||\}}tjj	||S )z Transform SymPy expressions into Aesara Computation.

    Parameters
    ==========
    exprs
        SymPy expressions

    Returns
    =======
    aesara.graph.fg.FunctionGraph
    )
listmapr1   r   graphbasicgraph_inputsclonefgFunctionGraph)exprsoutsinsr   r   r   	fgraph_ofC   s   r@   c                 C   s(   t j d}|  } |j|  | S )z Simplify a Aesara Computation.

    Parameters
    ==========
    fgraph : aesara.graph.fg.FunctionGraph

    Returns
    =======
    aesara.graph.fg.FunctionGraph
    fusion)r   compileget_default_mode	excludingr:   	optimizerrewrite)fgraphmoder   r   r   aesara_simplifyU   s   rI   c                 C   s   t ttjf}t| |}t||}|s|r|r|sdS | |kS t| ttf}t|ttf}|s0|rL|r<|r<t| t|kr>dS ttt	| ttt	|kS t
jj| dd}t
jj|dd}d| |fd||ffD ]\}	}
}|dkrztd|	|	t|
f qf||kS )a   Test two Aesara objects for equality.

    Also accepts numeric types and lists/tuples of supported types.

    Note - debugprint() has a bug where it will accept numeric types but does
    not respect the "file" argument and in this case and instead prints the number
    to stdout and returns an empty string. This can lead to tests passing where
    they should fail because any two numbers will always compare as equal. To
    prevent this we treat numbers as a separate case.
    Fstr)fileab zKaesara.printing.debugprint(%s) returned empty string (%s is instance of %r))intfloatnpnumber
isinstancetupler5   typer6   theqr   printing
debugprint	TypeError)rL   rM   numeric_typesa_is_numb_is_numa_is_seqb_is_seqastrbstrargnameargvalargstrr   r   r   rV   f   s0   

rV   c                   C   sp   t ttts	J t tttsJ t tttsJ t ttt	s$J t t
tts-J t ttts6J dS )z
    Check that the example symbols in this module print to their Aesara
    equivalents, as many of the other tests depend on this.
    N)rV   xtr1   r    ytr!   ztr"   XtXYtYZtZr   r   r   r   test_example_symbols   s   rm   c                  C   sf   t t} t| tsJ | jdksJ | jtjksJ t ttdid}|jdks)J |jtjks1J dS )z. Test printing a Symbol to a aesara variable. r   FbroadcastablesN)r1   r    rS   r   broadcastabler   )xxxx2r   r   r   test_Symbol   s   rt   c                  C   s(   t t} t| tsJ | jdksJ dS )z4 Test printing a MatrixSymbol to a aesara variable. r   N)r1   rh   rS   r	   rq   )XXr   r   r   test_MatrixSymbol   s   rv   c               	   C   sN   g d} | D ]}t t ttt|id W d   n1 sw   Y  qdS )z/ Test MatrixSymbol with invalid broadcastable. )r   rn   TTFFT)TTro   N)r   
ValueErrorr1   rh   )bcsbcr   r   r   test_MatrixSymbol_wrong_dims   s   
r}   c                  C   s6   t t} t| tsJ | jdksJ | jdksJ dS )zG Test printing AppliedUndef instance, which works similarly to Symbol. r   f_tN)r1   r~   rS   r	   rq   r   )fttr   r   r   test_AppliedUndef   s   r   c                  C   s(   t t } t| }|jjtjjksJ d S N)r    r!   r1   owneropr   r   addr/   compr   r   r   test_add   s   r   c                   C   s@   t tttttsJ t tttttsJ d S r   )rV   r1   r)   sinr    r   rd   tanr   r   r   r   	test_trig   s   "r   c                  C   sf   t td t t t dt  } t| }tt	d tt
 tdt  }t||s1J dS )z; Test printing a complex expression with multiple symbols.    N)r)   expr    cosr!   logr"   r1   r   rd   re   rf   rV   )r/   r   expectedr   r   r   	test_many   s   &&r   c                  C   s   dD ]} t tt| idjj| ksJ qt ttdidjjdv s!J t td tdidjjdks1J t tt tdtdidjjdksCJ dS )	zA Test specifying specific data types through the dtype argument. )float32float64int8int16int32int64dtypesr   )r   r      r   r   N)r1   r    rU   dtyper!   r   r   r   r   
test_dtype   s
    (r   c                  C   s6   t tfD ]} dD ]}t| | |idj|ksJ qqdS )zG Test the "broadcastables" argument when printing symbol-like objects. )r   rn   rw   r   rx   ro   N)r    r~   r1   rq   )sr|   r   r   r   test_broadcastables   s
   r   c                  C   s`   t t } g dg dg dg dg dg}|D ]\}}}t| t |t|id}|j|ks-J qdS )zG Test "broadcastable" attribute after applying element-wise binary op. )r   r   r   )rn   rn   rn   )rw   rn   rn   )ry   r   r   )rx   r   r   ro   N)r    r!   r1   rq   )r/   casesbc1bc2bc3r   r   r   r   test_broadcasting   s   r   c                  C   sD   t t t } t| }t|jjtsJ t|t	
t
ts J d S r   )rh   rj   rl   r1   rS   r   r   r   rV   rg   dotri   rk   )r/   expr_tr   r   r   test_MatMul   s   r   c                   C   s   t ttjjjtsJ d S r   )rS   r1   rh   Tr   r   r   r   r   r   r   test_Transpose  s   r   c                  C   s&   t t t } tt| jjtsJ d S r   )rh   rj   rl   rS   r1   r   r   r
   r/   r   r   r   test_MatAdd  s   r   c                   C   s@   t ttdd tddsJ t ttjtddsJ d S )Nr      r   )rV   r1   r)   Integerr   r   Halfr   r   r   r   test_Rationals  s   "r   c                   C   s   t tddksJ d S )Nr   )r1   r)   r   r   r   r   r   test_Integers  s   r   c                  C   s    t d} tt | sJ d S )Nr   )r)   Symbolr1   	factorialr   r   r   r   test_factorial  s   
r   c               	   C   sp   t t* dd } t| ttjtttdd| t	t
tts&J W d    d S 1 s1w   Y  d S )Nc                 S   s   t t| S r   )rI   r@   r   r   r   r   <lambda>  s    z!test_Derivative.<locals>.<lambda>Fevaluate)r   UserWarningrV   r1   r)   
Derivativer   r    r   gradr   rd   )simpr   r   r   test_Derivative  s   

"r   c                  C   s*   t ttgtt g} | dddksJ dS )z, Test aesara_function() with single output. r   r      Nr4   r    r!   )r,   r   r   r   test_aesara_function_simple  s   r   c                  C   sD   t ttgtt tt g} | dd\}}|dksJ |dks J dS )z/ Test aesara_function() with multiple outputs. r   r   r   Nr   )r,   o1o2r   r   r   test_aesara_function_multi#  s   r   c                  C   s   t ttgtt gdtdtdid} tj| ddgddgtddg dk s)J t ttgtt gtdtdidd	} tdd}dtdd }tj| ||dtd  dk s`J d
S )z1 Test aesara_function() vs Numpy implementation. r   r   )dimr   r   r   r'      &.>)r   r   N)	r4   r    r!   rQ   linalgnormasarrayarangeastype)r,   rr   yyr   r   r   test_aesara_function_numpy*  s   
0,r   c                  C   s   t ttgttt t gg} tddgddgg}ttttg| g}tj	|ddd| ttttg| gdd}tj	|ddd| ttttg| | g}t
|dddtg s[J tj	|dddd | tj	|dddd | d S )	N      ?       @g      @g      @T)r   r   r   )r)   Matrixr    r!   r"   rQ   arrayr4   testingassert_allcloserS   rU   )mr   r,   r   r   r   test_aesara_function_matrix7  s   r   c                   C   sd   t tgddtdiksJ t ttgtdtdidtdtdiks!J t tgtdidtdiks0J d S )	Nr   )r   r   r   )dimsry   rn   ro   )r%   r    r!   r   r   r   r   test_dim_handlingC  s
   
"r   c                  C   s   ddl } ttttgtt gddtdtdtdid}| j|ddgdd	gddg| d	d
g dk s4J ttttgtt gtdtdtdiddd}| d	d}d| d	d }d| d	d }| j||||d| d  dk szJ dS )zU
    Test passing additional kwargs from aesara_function() to aesara.function().
    r   Nr   ignorer   )r   on_unused_inputr   r   r   r'   r   r   )r   r   r   )
numpyr4   r    r!   r"   r   r   r   r   r   )rQ   r,   rr   r   zzr   r   r   test_aesara_function_kwargsI  s   6.r   c               	   C   sh  ddl m}  ttgtt gddgfttgtt gddgfttgtt gtdtdidgfttgtt tt gdddgfttttgtt tt gdddgfg}|D ]b\}}}}dD ]Y}t||||d}t|j| siJ dd	 |jj	D }|| }	t|	t
s~|	g}	t|t|	ksJ t||	D ]"\}
}|r|
dkrt|tjsJ qt|tjsJ |j|
ksJ qqWqOdS )
z2 Test the "scalar" argument to aesara_function(). r   )FunctionNr   r   ry   )r   r   c                 S   s$   g | ]}t d d |jjD qS )c                 S   s   g | ]}|rd ndqS )r   r   r   )r   r|   r   r   r   r   q  r   z:test_aesara_function_scalar.<locals>.<listcomp>.<listcomp>)rQ   onesrU   rq   r   ir   r   r   r   p  s    z/test_aesara_function_scalar.<locals>.<listcomp>)aesara.compile.function.typesr   r    r!   rh   rj   r4   rS   r&   input_storager5   lenziprQ   rR   ndarrayndim)r   argsr2   r3   in_dimsout_dimsr   r,   	in_values
out_valuesdvaluer   r   r   test_aesara_function_scalarZ  s4   "	
r   c                   C   s   t tdd  dS )za
    Passing an unknown keyword argument to aesara_function() should raise an
    exception.
    c                   S   s   t tgtd gddS )Nr   r   )foobar)r4   r    r   r   r   r   r     s    z0test_aesara_function_bad_kwarg.<locals>.<lambda>N)r   	Exceptionr   r   r   r   test_aesara_function_bad_kwarg  s   r   c                  C   s   t tdddtdddksJ dd } tdtdi}| t ttt|dttts+J | t tdtd|dtdtds>J d S )Nr   r   r   c                 S   sZ   dD ](}t | |}t ||}|d u s|d u r"|d u s!|d u s! dS qt||s* dS qdS )N)startstopstepFT)getattrrV   )s1s2attra1a2r   r   r   
theq_slice  s   


ztest_slice.<locals>.theq_slicer   r   )r1   slicer    r!   rd   re   )r   r   r   r   r   
test_slice  s
    "*r   c            	         s  i } t jddd}t d||}|ddddd	d
f }t|| d td}t jjjt	|||t	|||fks;J  jj
d t|| dksIJ t fddtddD sYJ t d}t||did d|d}}}|||| }t||d|did d S )Nr   Tintegerrh   r   r   r   r'   r   r   r-   r   r   c                 3   s"    | ]} j j| j|kV  qd S r   )r   r2   datar   ri   r   r   	<genexpr>  s     z#test_MatrixSlice.<locals>.<genexpr>   kr   r   )r)   r   r*   r1   r   rT   r   r   idx_listr   r2   allrange)	r-   r   rh   rj   r   r  r   r   r   r   r  r   test_MatrixSlice  s   * 
r  c            
   
      s   t jdddfdddD \} }}}tt| |||f\}}}}t | |g||gg}t| tdtd||td||tdtd||td||g}	t fd	d
|	D s_J d S )Nr   Tr   c                    s   g | ]	}t |  qS r   r(   r   r   r   r   r     r+   z$test_BlockMatrix.<locals>.<listcomp>ABCDr   r   c                 3   s    | ]}t  |V  qd S r   )rV   )r   solution)Blocktr   r   r    s    z#test_BlockMatrix.<locals>.<genexpr>)r)   r   r6   r1   BlockMatrixr   joinany)
ABCDAtBtCtDtBlock	solutionsr   )r  r   r   test_BlockMatrix  s     r  c                     s`  d\} }dt d|| }t d||}||f}|j| }||d |d f| d | d fg||d |d f|d |d fgi  fdd|D }|tt||}tt|gt| }t||g|i d}	t|t |g|i d}
fd	d|D }t	
||  |jt	|g}|d
  t	|jd 7  < t	j|	| |
| ddsJ d S )N)r   r'   r   r  r  r   c                    s"   g | ]}t j|g | R  qS r   )r)   blockcutr   )cutsizesr   r   r     s   " z6test_BlockMatrix_Inverse_execution.<locals>.<listcomp>)r   r-   c                    s    g | ]}t jj|j  qS r   )rQ   randomrandshaper   )r   r    r   r   r   r     s     r   gh㈵>)rtol)r)   r*   Isubsdictr   r   r4   block_collapserQ   r   reshaper  r   eyer   allclose)r  r   r  r  r2   output	cutinputs	cutoutputr   r,   fblockedninputsr   )r  r   r   "test_BlockMatrix_Inverse_execution  s,   
   r,  c                  C   s   ddl m}  td}tjtjfD ]-}|t|t| gt|t|gg}t|}t	|t
s5J t	|jj| s>J qd S )Nr   )Jointheta)aesara.tensor.basicr-  r)   r   r   ImmutableMatrixr   r   r1   rS   r	   r   r   )r-  r#   
MatrixTyperh   tXr   r   r   test_DenseMatrix  s   
.r3  c                  C   s   t tdfttjdgtjR  fttdtdfg} | D ]*\}}i }t||d}t||d|u s6J t|i d|us@J t||d|u sJJ q dS )zH Test single symbol-like objects are cached when printed by themselves. r    rh   r,   r#   r   N)	r    r)   r   rh   r*   r  r~   r   r1   )pairsr   r   r-   str   r   r   test_cache_basic  s   r6  c               	   C   sb   ddl m}  t| }z |   tttfD ]}t|}t||u s"J qW | | dS | | w )z Test use of the global cache. r   )global_cacheN)	sympy.printing.aesaracoder7  r"  clearr    rh   r~   r$   update)r7  backupr   r5  r   r   r   test_global_cache  s   r<  c                  C   s   t dt dddtg} i }i }| D ]}t||d}|| vs#J |||< qtttt	| t| ks9J |
 D ]\}}t||d|u sKJ q=dS )z
    Test that symbol-like objects of different types (Symbol, MatrixSymbol,
    AppliedUndef) are distinguished by the cache even if they have the same
    name.
    r~   r'   r   N)r)   r   r*   r~   r1   valuesr   setr6   iditemsr$   )symbolsr-   printedr   r5  r   r   r   test_cache_types_distinct  s   
"rC  c                  C   sD   t jttdd} t| }t|tt sJ t|ttt r J dS )zf
    Test that a symbol is cached and reused when it appears in an expression
    more than once.
    Fr   N)r)   Addr    r1   rV   rd   r   r   r   r   test_symbols_are_created_once$  s   rE  c                  C   s   t d ttt  ttt t    } dd | jD }t| }t }t	j
j|gD ]#}|jdu rNt|t	j
jjsN|j|v sAJ |j|vsHJ ||j q+||ksUJ dS )zf
    Test caching on a complicated expression with multiple symbols appearing
    multiple times.
    r   c                 S   s   h | ]}|j qS r   r   )r   r   r   r   r   	<setcomp>5  s    z%test_cache_complex.<locals>.<setcomp>N)r    r!   r)   r   r   r"   free_symbolsr1   r>  r   r7   r8   	ancestorsr   rS   Constantr   r   )r/   symbol_namesr   seenvr   r   r   test_cache_complex/  s   (rM  c                  C   s*  t dtdk fttdk fd} t| }|jjtjksJ ttdk dttdk td}t	||s3J t ttdk f} t| }ttdk tt
j}t	||sQJ t dt tdktdk ftt tdktdk f} t| }tttdktdk dtttdktdk tt
j}t	||sJ d S )Nr   r   )r   Tr   )r)   	Piecewiser    r1   r   r   r   switchrd   rV   rQ   nanAndOrand_or_)r/   resultr   r   r   r   test_PiecewiseG  s"    rV  c                   C   s   t ttttttt	sJ t tttktt	ksJ t tttk tt	k s+J t tttktt	ks8J t tttktt	ksEJ d S r   )
rV   r1   r)   Eqr    r!   r   eqrd   re   r   r   r   r   test_Relationals]  s
   "rY  c                  C   s   t dtdi} tt | dtt| d}}ddlm} ddlm} ddlm} ttt|t  | d||	  s8J ttdt  ||d|d	|dd
   sPJ d S )N
complex128r   r   )	conjugate)as_tensor_variable)complexy      ?       @r   r   r   )
r    r!   r$   $sympy.functions.elementary.complexesr[  aesara.tensorr\  r]  rV   conj)r   rd   re   r[  atvcplxr   r   r   test_complexfunctionsf  s   &4rc  c                  C   s   t g dg} |  dksJ d S )Ny      ?      ?)r&   )tfr   r   r   test_constantfunctionsp  s   re  )e__doc__loggingsympy.externalr   sympy.testing.pytestr   r   sympy.utilities.exceptionsr   	getLoggeraesaraloggersetLevelCRITICALr   WARNINGr   rQ   r   r   aesara.scalar.basicr   aesara.graph.basicr   aesara.tensor.varr	   aesara.tensor.elemwiser
   r   aesara.tensor.mathr   r8  r   rd   re   rf   rg   ri   rk   disabledsympyr)   sympy.core.singletonr   	sympy.abcr    r!   r"   r#   r$   r%   r&   rh   rj   rl   r   r~   r1   r4   r@   rI   rV   rm   rt   rv   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r,  r3  r6  r<  rC  rE  rM  rV  rY  rc  re  r   r   r   r   <module>   s    	
0

+

	
