U
    ZÊ¹j„1  ã                   @   sª  d dl mZmZmZ d dlmZ d dlmZ d dlm	Z	m
Z
mZ d dlZd dlZd dlZd dlZedƒZi Zded< d	ed
< ed
 ed< i Zded
< ed
 ed< ded< i Zdddged< dddged< dddged< dddged< ddd d!d"gZd#d$„ Zd>d&d'„Zd(d)„ Zd*d+„ Zg Zg ZeD ]<\ZZee Zeeeƒ�rLe eef¡ ne eef¡ �q d,d-„ Zd.d/„ Z d0d1„ Z!d2d3„ Z"d4d5„ Z#d6d7„ Z$d8d9„ Z%d:d;„ Z&d<d=„ Z'dS )?é    )ÚxÚyÚz)Úimport_module)Úskip)ÚcodegenÚmake_routineÚget_code_generatorNÚ
pyodide_jszy
program main
  include "codegen.h"
  integer :: result;
  result = 0

  %(statements)s

  call exit(result)
end program
ÚF95z�
#include "codegen.h"
#include <stdio.h>
#include <math.h>

int main() {
  int result = 0;

  %(statements)s

  return result;
}
ÚC89ÚC99z™
  if (fabs(%(call)s)>%(threshold)s) {
    printf("Numerical validation failed: %(call)s=%%e threshold=%(threshold)s\n", %(call)s);
    result = -1;
  }
zÁ
  if (abs(%(call)s)>%(threshold)s) then
    write(6,"('Numerical validation failed:')")
    write(6,"('%(call)s=',e15.5,'threshold=',e15.5)") %(call)s, %(threshold)s
    result = -1;
  end if
zcc -c codegen.c -o codegen.ozcc -c main.c -o main.oz#cc main.o codegen.o -lm -o test.exeÚccz$gfortran -c codegen.f90 -o codegen.oz6gfortran -ffree-line-length-none -c main.f90 -o main.oz%gfortran main.o codegen.o -o test.exeÚgfortranzg95 -c codegen.f90 -o codegen.oz1g95 -ffree-line-length-huge -c main.f90 -o main.oz g95 main.o codegen.o -o test.exeÚg95z!ifort -c codegen.f90 -o codegen.ozifort -c main.f90 -o main.oz"ifort main.o codegen.o -o test.exeÚifort©r   r   ©r   r   ©r   r   ©r   r   ©r   r   c              	   C   sZ   t rdS ttjdƒ�<}| D ]0}tj||dtjd�}|dkr W 5 Q R £ dS qW 5 Q R X dS )z>Run a series of commands and only return True if all ran fine.FÚwT)ÚstdoutÚshellÚstderrr   )r
   ÚopenÚosÚdevnullÚ
subprocessÚcallÚSTDOUT)ÚcommandsÚnullÚcommandÚretcode© r%   ú`/var/www/html/bwave/python/venv/lib/python3.8/site-packages/sympy/external/tests/test_codegen.pyÚtry_runw   s    
ÿr'   Tc              	   C   sR  |  ¡ }|tkst‚|tks t‚t dd¡ ¡ }|dkr@tdƒ‚t 	d| d|  ¡}t 
¡ }t |¡ |r|t||ddd	� nt|dƒ}	|	j|ddd	� g }
|D ]^\}}}}d
|d dd„ |D ƒ¡|f }|dkrät|ƒ}tt|ƒƒ}|
 t| ||dœ ¡ qž|dk�rd}n| d¡�r d}ntd| ƒ‚t|dƒ�"}| t| dd |
¡i ¡ W 5 Q R X t|ƒ}|�rxtdgƒ}nd}|dk�sœ|dk�rú|�rú|�rúdd„ }|dƒ |dƒ |dƒ |dƒ |dƒ |dƒ |d ƒ |d!ƒ t |¡ t |¡ ntd"| tjd#� t |¡ |�s2td$|d% |¡f ƒ‚|�sNtd&|d% |¡f ƒ‚d'S )(a|  A driver for the codegen tests.

       This driver assumes that a compiler ifort is present in the PATH and that
       ifort is (at least) a Fortran 90 compiler. The generated code is written in
       a temporary directory, together with a main program that validates the
       generated code. The test passes when the compilation and the validation
       run correctly.
    ZSYMPY_TEST_CLEAN_TEMPÚalways)r(   ÚsuccessÚneverzSSYMPY_TEST_CLEAN_TEMP must be one of the following: 'always', 'success' or 'never'.z_sympy_%s_testz%s_r   T)Zto_filesz%s(%s)-(%s)ú,c                 s   s   | ]}t |ƒV  qd S ©N)Ústr)Ú.0Úargr%   r%   r&   Ú	<genexpr>¬   s     zrun_test.<locals>.<genexpr>r   )r   Ú	thresholdzmain.f90ÚCzmain.cz2FIXME: filename extension unknown for language: %sr   Z
statementsÚ z
./test.exeFr)   c                 S   s   t j | ¡rt  | ¡ d S r,   )r   ÚpathÚisfileÚremove)Úfilenamer%   r%   r&   Úsafe_removeÌ   s    zrun_test.<locals>.safe_removezcodegen.f90z	codegen.cz	codegen.hz	codegen.ozmain.oztest.exezTEST NOT REMOVED: %s)Úfilez"failed to compile %s code with:
%sÚ
z"failed to execute %s code from:
%sN)ÚupperÚmain_templateÚAssertionErrorÚnumerical_test_templater   ÚgetenvÚlowerÚ
ValueErrorÚtempfileÚmkdtempÚgetcwdÚchdirr   r	   ÚwriteÚjoinÚfortranize_double_constantsr-   ÚappendÚ
startswithÚNotImplementedErrorr   r'   ÚrmdirÚprintÚsysr   )ÚlabelZroutinesÚnumerical_testsÚlanguager!   ÚfriendlyÚcleanZworkZoldworkZcode_genZtest_stringsÚfn_nameÚargsÚexpectedr1   Zcall_stringÚf_nameÚfÚcompiledZexecutedr8   r%   r%   r&   Úrun_test„   s€    

  ÿ
þ
ÿÿ 


 ÿ
 ÿrZ   c                    sL   ddl ‰ ˆ  d¡}ˆ  d¡}‡ fdd„}dd„ }| || ¡} | || ¡} | S )	z;
    Replaces every literal float with literal doubles
    r   Nz\d+(\.)?\d*[eE]-?\d+z\d+\.\d*(?!\d*d)c                    s   ˆ   dd|  d¡¡S )Nz[eE]Údr   )ÚsubÚgroup©Zmatchobj©Úrer%   r&   Úsubs_expì   s    z-fortranize_double_constants.<locals>.subs_expc                 S   s   d|   d¡ S )Nz%sd0r   )r]   r^   r%   r%   r&   Ú
subs_floatï   s    z/fortranize_double_constants.<locals>.subs_float)r`   Úcompiler\   )Zcode_stringZpattern_expZpattern_floatra   rb   r%   r_   r&   rH   ä   s    

rH   c                 C   sJ   t dtƒ}ddg}ztd|g|| |dd� W dS  tk
rD   Y dS X d S )NÚtest)rd   )ç      ð?re   çVçž¯Ò<)rd   )ç      ð¿rg   rf   Úis_feasibleF)rR   T)r   r   rZ   r=   )rQ   r!   ZroutinerP   r%   r%   r&   rh   ø   s    
þÿrh   c                   C   s   dt krtdƒ d S )Nr   z*`cc' command didn't work as expected (C89)©Úinvalid_lang_compilersr   r%   r%   r%   r&   Útest_C89_cc  s    rk   c                   C   s   dt krtdƒ d S )Nr   z*`cc' command didn't work as expected (C99)ri   r%   r%   r%   r&   Útest_C99_cc  s    rl   c                   C   s   dt krtdƒ d S )Nr   z'`ifort' command didn't work as expectedri   r%   r%   r%   r&   Útest_F95_ifort  s    rm   c                   C   s   dt krtdƒ d S )Nr   z*`gfortran' command didn't work as expectedri   r%   r%   r%   r&   Útest_F95_gfortran   s    rn   c                   C   s   dt krtdƒ d S )Nr   z%`g95' command didn't work as expectedri   r%   r%   r%   r&   Útest_F95_g95%  s    ro   c                  C   s<   ddg} dt t t fg}tD ]\}}td|| ||ƒ qd S )N)rd   )re   g      @g      @g      5@rf   )rd   )rg   g       @ç      Àrp   rf   rd   Zbasic_codegen)r   r   r   Úvalid_lang_commandsrZ   )rP   Ú	name_exprÚlangr!   r%   r%   r&   Útest_basic_codegen,  s    þrt   c                  C   s�  ddl m}  ddlm} ddlm} ddlm}m}m	} ddl
m}m} ddlm} ddlm}	m}
m}m}m}m} d	ttƒfd
|	tƒfd|
tƒfd|tƒfd|tƒfd|tƒfd|tƒfd|tƒfd|tƒfd|tƒfd|tƒfd|tƒfd|tƒfg}g }|D ]:\}}dD ]*}| | t|¡ƒ}| ||f|df¡ �q�qtD ]H\}}| d¡�rpd|tƒfd|tƒfg}ng }td|| |||ƒ �qBd S )Nr   ©ÚN)Úln)Úlog)ÚcoshÚsinhÚtanh)ÚceilingÚfloor)Úsqrt)ÚacosÚasinÚatanÚcosÚsinÚtanZ	test_fabsZ	test_acosZ	test_asinZ	test_atanZtest_cosZ	test_coshZtest_logZtest_lnZtest_sinZ	test_sinhZ	test_sqrtZtest_tanZ	test_tanh)çš™™™™™É?ç      à?çš™™™™™é?ç›+¡†›„=r2   Z
test_floorZ	test_ceilZintrinsic_math1)Úsympy.core.evalfrv   Zsympy.functionsrw   Z&sympy.functions.elementary.exponentialrx   Z%sympy.functions.elementary.hyperbolicry   rz   r{   Z#sympy.functions.elementary.integersr|   r}   Z(sympy.functions.elementary.miscellaneousr~   Ú(sympy.functions.elementary.trigonometricr   r€   r�   r‚   rƒ   r„   Úabsr   ÚsubsrI   rq   rJ   rZ   )rv   rw   rx   ry   rz   r{   r|   r}   r~   r   r€   r�   r‚   rƒ   r„   rr   rP   ÚnameÚexprÚxvalrV   rs   r!   Zname_expr_Cr%   r%   r&   Útest_intrinsic_math1_codegen6  sF     












ó
  ÿr�   c                  C   s¢   ddl m}  ddlm} d|ttƒfdtt fg}g }|D ]D\}}dD ]6\}}| | t|¡ t|¡ƒ}| |||f|df¡ qFq:tD ]\}	}
t	d|||	|
ƒ q„d S )	Nr   ru   )Úatan2Z
test_atan2Ztest_pow))r…   çÍÌÌÌÌÌô?)r†   çš™™™™™É¿)r‡   r‡   rˆ   Zintrinsic_math2)
r‰   rv   rŠ   r‘   r   r   rŒ   rI   rq   rZ   )rv   r‘   rr   rP   r�   rŽ   r�   ÚyvalrV   rs   r!   r%   r%   r&   Útest_instrinsic_math2_codegen\  s    
þr•   c                  C   sð   ddl m}  ddlm}m}m} d|tƒ|tƒ |tƒ d  	¡ fd||||||||tt t ƒƒƒƒƒƒƒƒfg}g }|D ]P\}}dD ]B\}}	}
| | 
t|¡ 
t|	¡ 
t|
¡ƒ}| |||	|
f|df¡ qˆq|tD ]\}}td	||||ƒ qÒd S )
Nr   ru   )r‚   rƒ   r„   Ztest1é   Ztest2))r…   r’   g333333Ó¿)r†   r“   g        )r‡   gÍÌÌÌÌÌ @r‡   gê-�™—q=Zcomplicated_codegen)r‰   rv   rŠ   r‚   rƒ   r„   r   r   r   ÚexpandrŒ   rI   rq   rZ   )rv   r‚   rƒ   r„   rr   rP   r�   rŽ   r�   r”   ZzvalrV   rs   r!   r%   r%   r&   Útest_complicated_codegenm  s$    ".þ     ÿr˜   )T)(Z	sympy.abcr   r   r   Zsympy.externalr   Zsympy.testing.pytestr   Zsympy.utilities.codegenr   r   r	   rN   r   rB   r   r
   r<   r>   Zcompile_commandsZcombinations_lang_compilerr'   rZ   rH   rh   rq   rj   rs   Úcompilerr!   rI   rk   rl   rm   rn   ro   rt   r�   r•   r˜   r%   r%   r%   r&   Ú<module>   sv   	ýýýýû	
`
&