a
    kh61                     @   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 n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   sp   t rdS ttjdH}| D ]2}tj||dtjd}|dkr W d   dS qW d   n1 sb0    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%   O/var/www/auris/lib/python3.9/site-packages/sympy/external/tests/test_codegen.pytry_runt   s    
2r'   Tc                 C   s@  |  }|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rt|}tt|}|
t| ||d  q|dkrd}n|dr d}ntd| t|t| dd|
i  t|}|rftdg}nd}|dks|dkr|r|rd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.
    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>       zrun_test.<locals>.<genexpr>r   )r   	thresholdzmain.f90Czmain.cz2FIXME: filename extension unknown for language: %sZ
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numerical_test_templater   getenvlower
ValueErrortempfilemkdtempgetcwdchdirr   r	   writejoinfortranize_double_constantsr-   append
startswithNotImplementedErrorr
   
write_textr'   rmdirprintsysr   )labelZroutinesnumerical_testslanguager!   friendlycleanZworkZoldworkZcode_genZtest_stringsfn_nameargsexpectedr2   Zcall_stringf_nameZcompiledZexecutedr9   r%   r%   r&   run_test   sv    



 





rY   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_floatr`   ra   r%   r^   r&   rH      s    

rH   c                 C   sH   t dt}ddg}ztd|g|| |dd W dS  tyB   Y dS 0 d S )Ntest)rc   )      ?rd   V瞯<)rc   )      rf   re   is_feasibleF)rS   T)r   r   rY   AssertionError)rR   r!   ZroutinerQ   r%   r%   r&   rg      s    
rg   c                   C   s   dt v 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 v 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 v 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 v 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 v 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)rc   )rd   g      @g      @g      5@re   )rc   )rf   g       @      rp   re   rc   Zbasic_codegen)r   r   r   valid_lang_commandsrY   )rQ   	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)皙?      ?皙?+=r3   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   rY   )rv   rw   rx   ry   rz   r{   r|   r}   r~   r   r   r   r   r   r   rr   rQ   nameexprxvalrW   rs   r!   Zname_expr_Cr%   r%   r&   test_intrinsic_math1_codegen2  sB     













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|||	|
 qd 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   rY   )rv   r   rr   rQ   r   r   r   yvalrW   rs   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 ]P\}}dD ]B\}}	}
| |
t|
t|	
t|
}||||	|
f|df qq|tD ]\}}td	|||| qd 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   rY   )rv   r   r   r   rr   rQ   r   r   r   r   ZzvalrW   rs   r!   r%   r%   r&   test_complicated_codegeni  s    ". 
r   )T)(Z	sympy.abcr   r   r   Zsympy.testing.pytestr   r   Zsympy.utilities.codegenr   r   r	   rO   r   rB   r   pathlibr
   r=   r>   Zcompile_commandsZcombinations_lang_compilerr'   rY   rH   rg   rq   rj   rs   compilerr!   rI   rk   rl   rm   rn   ro   rt   r   r   r   r%   r%   r%   r&   <module>   sR   	
_
&