a
    <b0                     @   s  d dl mZmZ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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d0d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  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.d/ Z$dS )1    )xyz)skip)codegenmake_routineget_code_generatorNzy
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   s<   t tjd}| D ]&}tj||dtjd}|dkr dS qdS )z>Run a series of commands and only return True if all ran fine.wT)stdoutshellstderrr   F)openosdevnull
subprocesscallSTDOUT)commandsnullcommandretcode r#   q/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/sympy/external/tests/test_codegen.pytry_runt   s    
r%   Tc                 C   sh  |  }|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|d,}|t| dd|
i  W d   n1 sj0    Y  t|}|rt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
| |sHJ d%|d&|f |sdJ 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: %sr   Z
statements Nz
./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:
%s)uppermain_template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expectedr0   Zcall_stringf_namefZcompiledZexecutedr7   r#   r#   r$   run_test   sx    



$ 





rW   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   )rZ   r[   r#   r#   r$   
subs_float   s    z/fortranize_double_constants.<locals>.subs_float)r]   compilerY   )Zcode_stringZpattern_expZpattern_floatr^   r_   r#   r\   r$   rF      s    

rF   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)ra   )      ?rb   V瞯<)ra   )      rd   rc   is_feasibleF)rP   T)r   r   rW   AssertionError)rO   r   ZroutinerN   r#   r#   r$   re      s    
re   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    ri   c                   C   s   dt v rtd d S )Nr   z*`cc' command didn't work as expected (C99)rg   r#   r#   r#   r$   test_C99_cc  s    rj   c                   C   s   dt v rtd d S )Nr   z'`ifort' command didn't work as expectedrg   r#   r#   r#   r$   test_F95_ifort  s    rk   c                   C   s   dt v rtd d S )Nr   z*`gfortran' command didn't work as expectedrg   r#   r#   r#   r$   test_F95_gfortran  s    rl   c                   C   s   dt v rtd d S )Nr   z%`g95' command didn't work as expectedrg   r#   r#   r#   r$   test_F95_g95   s    rm   c                  C   s<   ddg} dt t t fg}tD ]\}}td|| || qd S )N)ra   )rb   g      @g      @g      5@rc   )ra   )rd   g       @      rn   rc   ra   Zbasic_codegen)r   r   r   valid_lang_commandsrW   )rN   	name_exprlangr   r#   r#   r$   test_basic_codegen'  s    rr   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)皙?      ?皙?+=r1   Z
test_floorZ	test_ceilZintrinsic_math1)sympy.core.evalfrt   Zsympy.functionsru   Z&sympy.functions.elementary.exponentialrv   Z%sympy.functions.elementary.hyperbolicrw   rx   ry   Z#sympy.functions.elementary.integersrz   r{   Z(sympy.functions.elementary.miscellaneousr|   (sympy.functions.elementary.trigonometricr}   r~   r   r   r   r   absr   subsrG   ro   rH   rW   )rt   ru   rv   rw   rx   ry   rz   r{   r|   r}   r~   r   r   r   r   rp   rN   nameexprxvalrT   rq   r   Zname_expr_Cr#   r#   r$   test_intrinsic_math1_codegen1  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   rs   )atan2Z
test_atan2Ztest_pow))r   ?)r   皙ɿ)r   r   r   Zintrinsic_math2)
r   rt   r   r   r   r   r   rG   ro   rW   )rt   r   rp   rN   r   r   r   yvalrT   rq   r   r#   r#   r$   test_instrinsic_math2_codegenW  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   rs   )r   r   r   Ztest1   Ztest2))r   r   g333333ӿ)r   r   g        )r   g @r   g-q=Zcomplicated_codegen)r   rt   r   r   r   r   r   r   r   expandr   rG   ro   rW   )rt   r   r   r   rp   rN   r   r   r   r   ZzvalrT   rq   r   r#   r#   r$   test_complicated_codegenh  s    ". 
r   )T)%Z	sympy.abcr   r   r   Zsympy.testing.pytestr   Zsympy.utilities.codegenr   r   r   rL   r   r@   r   r;   r<   Zcompile_commandsZcombinations_lang_compilerr%   rW   rF   re   ro   rh   rq   compilerr   rG   ri   rj   rk   rl   rm   rr   r   r   r   r#   r#   r#   r$   <module>   sP   		
`
&