U
    9%e1                     @   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/Darija-Ai-API/env/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rt|}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|rd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|||	|
 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   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 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   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   		
`
&