o
    GZh61                     @   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reeef qeeef qd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%   P/var/www/auris/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rd}n|drd}ntd| t|t| dd|
i  t|}|rt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>   s    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 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expectedr1   Zcall_stringf_namecompiledZexecutedr8   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&   rG      s   

rG   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)rc   )      ?rd   V瞯<)rc   )      rf   re   is_feasibleF)rR   T)r   r   rY   AssertionError)rQ   r!   ZroutinerP   r%   r%   r&   rg      s   
rg   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     rl   c                   C   ri   )Nr   z*`cc' command didn't work as expected (C99)rj   r%   r%   r%   r&   test_C99_cc  rm   rn   c                   C   ri   )Nr   z'`ifort' command didn't work as expectedrj   r%   r%   r%   r&   test_F95_ifort  rm   ro   c                   C   ri   )Nr   z*`gfortran' command didn't work as expectedrj   r%   r%   r%   r&   test_F95_gfortran  rm   rp   c                   C   ri   )Nr   z%`g95' command didn't work as expectedrj   r%   r%   r%   r&   test_F95_g95!  rm   rq   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       @      rr   re   rc   Zbasic_codegen)r   r   r   valid_lang_commandsrY   )rP   	name_exprlangr!   r%   r%   r&   test_basic_codegen(  s   rv   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 qqtD ]"\}}|drd|tfd|tfg}ng }td|| ||| qd 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.evalfrx   Zsympy.functionsry   Z&sympy.functions.elementary.exponentialrz   Z%sympy.functions.elementary.hyperbolicr{   r|   r}   Z#sympy.functions.elementary.integersr~   r   Z(sympy.functions.elementary.miscellaneousr   (sympy.functions.elementary.trigonometricr   r   r   r   r   r   absr   subsrH   rs   rI   rY   )rx   ry   rz   r{   r|   r}   r~   r   r   r   r   r   r   r   r   rt   rP   nameexprxvalrV   ru   r!   Z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   rw   )atan2Z
test_atan2Ztest_pow))r   ?)r   皙ɿ)r   r   r   Zintrinsic_math2)
r   rx   r   r   r   r   r   rH   rs   rY   )rx   r   rt   rP   r   r   r   yvalrV   ru   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   rw   )r   r   r   Ztest1   Ztest2))r   r   g333333ӿ)r   r   g        )r   g @r   g-q=Zcomplicated_codegen)r   rx   r   r   r   r   r   r   r   expandr   rH   rs   rY   )rx   r   r   r   rt   rP   r   r   r   r   ZzvalrV   ru   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	   rN   r   rA   r   pathlibr
   r<   r=   Zcompile_commandsZcombinations_lang_compilerr'   rY   rG   rg   rs   rk   ru   compilerr!   rH   rl   rn   ro   rp   rq   rv   r   r   r   r%   r%   r%   r&   <module>   sT   	
_

&