o
    ohS                     @   sz  d Z ddlZddlmZ ddlmZmZmZ edZ	e	
ej edZe	
ej erLddlZejZej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$m%Z%m&Z&m'Z' dd dD \Z(Z)Z*e+de#Z,dd Z-dd Z.dd Z/dd Z0dd Z1dd Z2dd Z3d d! Z4ed"d# Z5d$d% Z6d&d' Z7d(d) Z8d*d+ Z9d,d- Z:d.d/ Z;d0d1 Z<d2d3 Z=d4d5 Z>d6d7 Z?d8d9 Z@d:d; ZAd<d= ZBd>d? ZCd@dA ZDdBdC ZEdDdE ZFdFdG ZGdHdI ZHdJdK ZIdLdM ZJdNdO ZKdPdQ ZLdRdS ZMdTdU ZNedVdW ZOdXdY ZPdZd[ ZQd\d] ZRd^d_ ZSd`da ZTdbdc ZUddde ZVdfdg ZWdhdi ZXdjdk ZYdldm ZZdS )na  
Important note on tests in this module - the Theano 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 theano_code_ and
theano_function_ functions defined below which default to using a new, empty
cache instead.
    N)import_module)raisesSKIPwarns_deprecated_sympyztheano.configdefaultstheanoc                 C   s   g | ]}t |d qS )floatX)ttscalar.0name r   x/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/sympy/printing/tests/test_theanocode.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)theano_codedim_handlingtheano_functionc                 C   s   g | ]	}t |d d qS )   syMatrixSymbolr   r   r   r   r   (       fc                 K   sF   | di  t  t| fi |W  d   S 1 sw   Y  dS )zB Wrapper for theano_code that uses a new, empty cache by default. cacheN)
setdefaultr   r   )exprkwargsr   r   r   theano_code_.   s   $r*   c                 K   sH   | di  t  t| |fi |W  d   S 1 sw   Y  dS )zF Wrapper for theano_function that uses a new, empty cache by default. r&   N)r'   r   r   )inputsoutputsr)   r   r   r   theano_function_4   s   $r-   c                  G   s>   t tt| }tjj|}tjj||\}}tj||S )z Transform SymPy expressions into Theano Computation.

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

    Returns
    =======
    theano.gof.FunctionGraph
    )	listmapr*   r   gofgraphr+   cloneFunctionGraph)exprsoutsinsr   r   r   	fgraph_of;   s   r7   c                 C   s(   t j d}|  } |j|  | S )z Simplify a Theano Computation.

    Parameters
    ==========
    fgraph : theano.gof.FunctionGraph

    Returns
    =======
    theano.gof.FunctionGraph
    fusion)r   compileget_default_mode	excludingr2   	optimizeroptimize)fgraphmoder   r   r   theano_simplifyM   s   r@   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 Theano 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 zKtheano.printing.debugprint(%s) returned empty string (%s is instance of %r))intfloatnpnumber
isinstancetupler.   typer/   theqr   printing
debugprint	TypeError)rC   rD   numeric_typesa_is_numb_is_numa_is_seqb_is_seqastrbstrargnameargvalargstrr   r   r   rM   ^   s0   

rM   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 Theano
    equivalents, as many of the other tests depend on this.
    N)rM   xtr*   r   ytr   ztr   XtXYtYZtZr   r   r   r   test_example_symbols   s   rd   c                  C   sn   t t} t| tjtjf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s5J dS )z. Test printing a Symbol to a theano variable. r   FbroadcastablesN)	r*   r   rJ   r   TensorVariabletsScalarVariablebroadcastabler   )xxxx2r   r   r   test_Symbol   s   rn   c                  C   s*   t t} t| tjsJ | jdksJ dS )z4 Test printing a MatrixSymbol to a theano variable. r   N)r*   r_   rJ   r   rh   rk   )XXr   r   r   test_MatrixSymbol   s   rp   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   re   TTFFT)TTrf   N)r   
ValueErrorr*   r_   )bcsbcr   r   r   test_MatrixSymbol_wrong_dims   s   
rw   c                  C   s8   t t} t| tj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)r*   rx   rJ   r   rh   rk   r   )fttr   r   r   test_AppliedUndef   s   rz   c                  C   s(   t t } t| }|jjtjjksJ d S N)r   r   r*   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{   )rM   r*   r"   sinr   r   r[   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   r*   r   r[   r\   r]   rM   )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)r*   r   rL   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   re   rq   r   rr   rf   N)r   rx   r*   rk   )srv   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   )re   re   re   )rq   re   re   )rs   r   r   )rr   r   r   rf   N)r   r   r*   rk   )r(   casesbc1bc2bc3r   r   r   r   test_broadcasting   s   r   c                  C   sF   t t t } t| }t|jjtjsJ t	|t
tts!J d S r{   )r_   ra   rc   r*   rJ   r|   r}   r   DotrM   r^   dotr`   rb   )r(   expr_tr   r   r   test_MatMul   s   r   c                   C   s   t ttjjjtjsJ d S r{   )rJ   r*   r_   Tr|   r}   r   
DimShuffler   r   r   r   test_Transpose   s   r   c                  C   s(   t t t } tt| jjtjsJ d S r{   )	r_   ra   rc   rJ   r*   r|   r}   r   Elemwiser(   r   r   r   test_MatAdd   s   r   c                   C   sD   t ttdd tddsJ t ttjtdds J d S )Nr      r   )rM   r*   r"   Integerr   true_divr   Halfr   r   r   r   test_Rationals  s   $ r   c                   C   s   t tddksJ d S )Nr   )r*   r"   r   r   r   r   r   test_Integers  s   r   c                  C   s    t d} tt | sJ d S )Nr   )r"   Symbolr*   	factorialr   r   r   r   test_factorial  s   
r   c                  C   sF   dd } t | ttjtttdd| ttt	t	s!J d S )Nc                 S   s   t t| S r{   )r@   r7   r   r   r   r   <lambda>  s    z!test_Derivative.<locals>.<lambda>Fevaluate)
rM   r*   r"   
Derivativer   r   r   gradr   r[   )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 theano_function() with single output. r   r      Nr-   r   r   )r%   r   r   r   test_theano_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 theano_function() with multiple outputs. r   r   r   Nr   )r%   o1o2r   r   r   test_theano_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 theano_function() vs Numpy implementation. r   r   )dimr   r   r   r       &.>)r   r   N)	r-   r   r   rH   linalgnormasarrayarangeastype)r%   rl   yyr   r   r   test_theano_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   rH   arrayr-   testingassert_allcloserJ   rL   )mr   r%   r   r   r   test_theano_function_matrix.  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   )dimsrs   re   rf   )r   r   r   r   r   r   r   test_dim_handling:  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 theano_function() to theano.function().
    r   Nr   ignorer   )r   on_unused_inputr   r   r   r    r   r   )r   r   r   )
numpyr-   r   r   r   r   r   r   r   r   )rH   r%   rl   r   zzr   r   r   test_theano_function_kwargs@  s   6.r   c               	   C   sb  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 ]e\}}}}dD ]\}t||||d}t|jtjj	j
sfJ 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 qqQqIdS )	z2 Test the "scalar" argument to theano_function(). Nr   r   r   rs   )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   rv   r   r   r   r   g  r   z:test_theano_function_scalar.<locals>.<listcomp>.<listcomp>)rH   onesrL   rk   r   ir   r   r   r   f  s    z/test_theano_function_scalar.<locals>.<listcomp>)r   r   r_   ra   r-   rJ   r   r   r9   function_moduleFunctioninput_storager.   lenziprH   rI   ndarrayndim)argsr+   r,   in_dimsout_dimsr	   r%   	in_values
out_valuesdvaluer   r   r   test_theano_function_scalarQ  s2   "	
r   c                   C   s   t tdd  dS )za
    Passing an unknown keyword argument to theano_function() should raise an
    exception.
    c                   S   s   t tgtd gddS )Nr   r   )foobar)r-   r   r   r   r   r   r     s    z0test_theano_function_bad_kwarg.<locals>.<lambda>N)r   	Exceptionr   r   r   r   test_theano_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)getattrrM   )s1s2attra1a2r   r   r   
theq_slice  s   


ztest_slice.<locals>.theq_slicer   r   )r*   slicer   r   r[   r\   )r   r   r   r   r   
test_slice  s
    "*r   c                     s  ddl m  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sBJ j	jd t|| dksPJ t fddtddD sbJ td}t||did d
|d}}}|||| }t||d|didd S )Nr   )Constantr   Tintegerr_   r   r   r   r    r   r   r&   r   c                 3   s(    | ]}j j|  |V  qd S r{   )r|   r+   equalsr   r   r`   r   r   r   	<genexpr>  s   & z#test_MatrixSlice.<locals>.<genexpr>   kr   r   )r   r   r"   r   r#   r*   ri   ScalarrK   r|   r}   idx_listr   r+   allrange)r&   r   r_   ra   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{   )rM   )r   solution)Blocktr   r   r     s    z#test_BlockMatrix.<locals>.<genexpr>)r"   r   r/   r*   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   )rH   randomrandshaper   )r   r   r   r   r   r     s     r   gh㈵>)rtol)r"   r#   Isubsdictr   r   r-   block_collapserH   r   reshaper  r   eyer   allclose)r  r   r  r  r+   output	cutinputs	cutoutputr   r%   fblockedninputsr   )r  r   r   "test_BlockMatrix_Inverse_execution  s,   
   r+  c                  C   sx   t d} t jt jfD ].}|t | t |  gt | t | gg}t|}t|tj	s0J |j
jtjks9J qd S )Ntheta)r"   r   r   ImmutableMatrixr   r   r*   rJ   r   rh   r|   r}   join_)r   
MatrixTyper_   tXr   r   r   test_DenseMatrix  s   
.r1  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   r_   r%   r   r   N)	r   r"   r   r_   r#   r  rx   r   r*   )pairsr   r   r&   str   r   r   test_cache_basic  s   r4  c               	   C   s   ddl m}  t| }z3|   tttfD ]!}t  t|}t||u s&J W d   n1 s0w   Y  qW | 	| dS | 	| w )z Test use of the global cache. r   )global_cacheN)
sympy.printing.theanocoder5  r!  clearr   r_   rx   r   r   update)r5  backupr   r3  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  t||d|u sOJ W d   n1 sYw   Y  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.
    rx   r    r   N)r"   r   r#   rx   r*   valuesr   setr/   iditemsr   r   )symbolsr&   printedr   r3  r   r   r   test_cache_types_distinct  s   
"rA  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   r*   rM   r[   r   r   r   r   test_symbols_are_created_once  s   rC  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>-  s    z%test_cache_complex.<locals>.<setcomp>N)r   r   r"   r   r   r   free_symbolsr*   r<  r   r0   r1   	ancestorsr|   rJ   r   r   r~   )r(   symbol_namesr   seenvr   r   r   test_cache_complex'  s   (rJ  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   r*   r|   r}   r   switchr[   rM   rH   nanAndOrand_or_)r(   resultr   r   r   r   test_Piecewise?  s"    rS  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{   )
rM   r*   r"   Eqr   r   r   eqr[   r\   r   r   r   r   test_RelationalsU  s
   "rV  c               	   C   s   t   tttdidtttdid} }W d    n1 sw   Y  ddlm} ddlm} ddlm} t  2 t	tt|t || 
  sKJ t	tdt | |d|d	|dd
   scJ W d    d S 1 snw   Y  d S )N
complex128r   r   )	conjugate)as_tensor_variable)complexy      ?       @r   r   r   )r   r*   r   r   $sympy.functions.elementary.complexesrX  theano.tensorrY  rZ  rM   conj)r[   r\   rX  atvcplxr   r   r   test_complexfunctions^  s   $"2"r`  c                  C   sD   t   tg dg} W d    n1 sw   Y  |  dks J d S )Ny      ?      ?)r   r-   )tfr   r   r   test_constantfunctionsi  s   rb  c                  C   sx   t d} t j}tjt| tj tjt|tj t| }tjt| |	  t|}tjt||	  dS )zP
    Test that exp(1) prints without error and evaluates close to SymPy's E
    r   N)
r"   r   ErH   r   r   rG   er*   eval)e_ae_brd  r   r   r   	test_Exp1o  s   
rh  )[__doc__loggingsympy.externalr   sympy.testing.pytestr   r   r   	getLoggertheanologgersetLevelCRITICALr   WARNINGr   rH   r	   ri   r   r   r[   r\   r]   r^   r`   rb   disabledsympyr"   sympy.core.singletonr   	sympy.abcr   r   r   r   r6  r   r   r   r_   ra   rc   r   rx   r*   r-   r7   r@   rM   rd   rn   rp   rw   rz   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+  r1  r4  r:  rA  rC  rJ  rS  rV  r`  rb  rh  r   r   r   r   <module>   s    	
0

*

		