o
    ˜…›i61  ã                   @   s”  d dl mZmZmZ d dlmZ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d dlm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g d¢ed< g d¢ed< g d¢ed< g d¢ed< g d¢Zdd„ Zd1dd„Zdd„ Zdd„ Zg Zg ZeD ]\ZZee Zeeeƒrœe eef¡ q‡e eef¡ q‡dd „ Zd!d"„ Z d#d$„ Z!d%d&„ Z"d'd(„ Z#d)d*„ Z$d+d,„ Z%d-d.„ Z&d/d0„ Z'dS )2é    )ÚxÚyÚz)ÚIS_WASMÚskip)ÚcodegenÚmake_routineÚget_code_generatorN)ÚPathzy
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Úcc)z$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Úgfortran)z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Úg95)z!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   sr   t rdS ttjdƒ%}| D ]}tj||dtjd}|dkr& W d  ƒ dS qW d  ƒ dS 1 s2w   Y  dS )z>Run a series of commands and only return True if all ran fine.FÚwT)ÚstdoutÚshellÚstderrr   N)r   ÚopenÚosÚdevnullÚ
subprocessÚcallÚSTDOUT)ÚcommandsÚnullÚcommandÚretcode© r%   úc/sda-disk/www/egybert/egybert_env/lib/python3.10/site-packages/sympy/external/tests/test_codegen.pyÚtry_runt   s    
ÿûý
ÿúr'   Tc                 C   s2  |  ¡ }|tv s
J ‚|tv sJ ‚t dd¡ ¡ }|dv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rrt|ƒ}tt|ƒƒ}|
 t| ||dœ ¡ qO|dkr†d}n| d¡rŽd}ntd| ƒ‚t|ƒ t| dd |
¡i ¡ t|ƒ}|r°t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 
|¡ |s	J d#|d$ |¡f ƒ‚|sJ 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.
    Ú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)Ú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: %sÚ
statementsÚ z
./test.exeFr*   c                 S   s   t j | ¡rt  | ¡ d S 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Únumerical_test_templater   ÚgetenvÚlowerÚ
ValueErrorÚtempfileÚmkdtempÚgetcwdÚchdirr   r	   ÚwriteÚjoinÚfortranize_double_constantsr/   ÚappendÚ
startswithÚNotImplementedErrorr
   Ú
write_textr'   ÚrmdirÚprintÚsysr   )ÚlabelÚroutinesÚnumerical_testsÚlanguager!   ÚfriendlyÚcleanÚworkÚoldworkÚcode_genÚtest_stringsÚfn_nameÚargsÚexpectedr3   Úcall_stringÚf_nameÚcompiledÚexecutedr;   r%   r%   r&   Úrun_test   sv   

ÿ
þ
ÿÿ



ÿ

ÿrc   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©Úmatchobj©Úrer%   r&   Úsubs_expè   s   z-fortranize_double_constants.<locals>.subs_expc                 S   s   d|   d¡ S )Nz%sd0r   )rf   rg   r%   r%   r&   Ú
subs_floatë   s   z/fortranize_double_constants.<locals>.subs_float)rj   Úcompilere   )Úcode_stringÚpattern_expÚpattern_floatrk   rl   r%   ri   r&   rJ   à   s   

rJ   c                 C   sD   t dtƒ}ddg}ztd|g|| |dd W dS  ty!   Y dS w )NÚtest)rq   )ç      ð?rr   çVçž¯Ò<)rq   )ç      ð¿rt   rs   Úis_feasibleF)rV   T)r   r   rc   ÚAssertionError)rU   r!   ÚroutinerT   r%   r%   r&   ru   ô   s   
þÿÿru   c                   C   ó   dt v r
tdƒ d S d S )Nr   z*`cc' command didn't work as expected (C89)©Úinvalid_lang_compilersr   r%   r%   r%   r&   Útest_C89_cc  ó   ÿr{   c                   C   rx   )Nr   z*`cc' command didn't work as expected (C99)ry   r%   r%   r%   r&   Útest_C99_cc  r|   r}   c                   C   rx   )Nr   z'`ifort' command didn't work as expectedry   r%   r%   r%   r&   Útest_F95_ifort  r|   r~   c                   C   rx   )Nr   z*`gfortran' command didn't work as expectedry   r%   r%   r%   r&   Útest_F95_gfortran  r|   r   c                   C   rx   )Nr   z%`g95' command didn't work as expectedry   r%   r%   r%   r&   Útest_F95_g95!  r|   r€   c                  C   s<   ddg} dt t t fg}tD ]\}}td|| ||ƒ qd S )N)rq   )rr   g      @g      @g      5@rs   )rq   )rt   g       @ç      Àr   rs   rq   Úbasic_codegen)r   r   r   Úvalid_lang_commandsrc   )rT   Ú	name_exprÚlangr!   r%   r%   r&   Útest_basic_codegen(  s   þÿr†   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‡qtD ]"\}}| d¡rµd|tƒfd|tƒfg}ng }td|| |||ƒ qŸd S )Nr   ©ÚN)Úln)Úlog)ÚcoshÚsinhÚtanh)ÚceilingÚfloor)Úsqrt)ÚacosÚasinÚatanÚcosÚsinÚtanÚ	test_fabsÚ	test_acosÚ	test_asinÚ	test_atanÚtest_cosÚ	test_coshÚtest_logÚtest_lnÚtest_sinÚ	test_sinhÚ	test_sqrtÚtest_tanÚ	test_tanh)çš™™™™™É?ç      à?çš™™™™™é?ç›+¡†›„=r4   Ú
test_floorÚ	test_ceilÚintrinsic_math1)Úsympy.core.evalfrˆ   Úsympy.functionsr‰   Ú&sympy.functions.elementary.exponentialrŠ   Ú%sympy.functions.elementary.hyperbolicr‹   rŒ   r   Ú#sympy.functions.elementary.integersrŽ   r   Ú(sympy.functions.elementary.miscellaneousr   Ú(sympy.functions.elementary.trigonometricr‘   r’   r“   r”   r•   r–   Úabsr   ÚsubsrK   rƒ   rL   rc   )rˆ   r‰   rŠ   r‹   rŒ   r   rŽ   r   r   r‘   r’   r“   r”   r•   r–   r„   rT   ÚnameÚexprÚxvalr^   r…   r!   Úname_expr_Cr%   r%   r&   Útest_intrinsic_math1_codegen2  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 ]\}}| | t|¡ t|¡ƒ}| |||f|df¡ q#qtD ]\}	}
t	d|||	|
ƒ qBd S )	Nr   r‡   )Úatan2Ú
test_atan2Útest_pow))r¤   çÍÌÌÌÌÌô?)r¥   çš™™™™™É¿)r¦   r¦   r§   Úintrinsic_math2)
r«   rˆ   r±   r¹   r   r   r³   rK   rƒ   rc   )rˆ   r¹   r„   rT   r´   rµ   r¶   Úyvalr^   r…   r!   r%   r%   r&   Útest_instrinsic_math2_codegenX  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 ](\}}dD ]!\}}	}
| | 
t|¡ 
t|	¡ 
t|
¡ƒ}| |||	|
f|df¡ qDq>tD ]\}}td	||||ƒ qid S )
Nr   r‡   )r”   r•   r–   Útest1é   Útest2))r¤   r¼   g333333Ó¿)r¥   r½   g        )r¦   gÍÌÌÌÌÌ @r¦   gê-™—q=Úcomplicated_codegen)r«   rˆ   r±   r”   r•   r–   r   r   r   Úexpandr³   rK   rƒ   rc   )rˆ   r”   r•   r–   r„   rT   r´   rµ   r¶   r¿   Úzvalr^   r…   r!   r%   r%   r&   Útest_complicated_codegeni  s    ".þ þ
ÿÿrÇ   )T)(Ú	sympy.abcr   r   r   Úsympy.testing.pytestr   r   Úsympy.utilities.codegenr   r   r	   rQ   r   rD   r   Úpathlibr
   r?   r@   Úcompile_commandsÚcombinations_lang_compilerr'   rc   rJ   ru   rƒ   rz   r…   Úcompilerr!   rK   r{   r}   r~   r   r€   r†   r¸   rÀ   rÇ   r%   r%   r%   r&   Ú<module>   sT   	
_

&