o
    jg1                     @   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g ded< g ded< g ded< g ded< g dZdd Zd2d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&d0d1 Z'dS )3    )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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%   Y/var/www/html/zoom/venv/lib/python3.10/site-packages/sympy/external/tests/test_codegen.pytry_runw   s    

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rrt|}tt|}|
t| ||d  qO|dkrd}n|drd}ntd| t|d}|t| dd|
i  W d   n1 sw   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
| |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: %sr   
statements Nz
./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:
%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   )labelroutinesnumerical_testslanguager!   friendlycleanworkoldworkcode_gentest_stringsfn_nameargsexpectedr3   call_stringf_namefcompiledexecutedr;   r%   r%   r&   run_test   sz   









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)rU   T)r   r   rc   AssertionError)rT   r!   routinerS   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   )rS   	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 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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   rS   nameexprxvalr]   r   r!   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 ]\}}| |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   rS   r   r   r   yvalr]   r   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 ](\}}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   rS   r   r   r   r   zvalr]   r   r!   r%   r%   r&   test_complicated_codegenm  s    ". 
r   )T)(	sympy.abcr   r   r   sympy.externalr   sympy.testing.pytestr   sympy.utilities.codegenr   r   r	   rP   r   rD   r   r
   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>   sV   		
`

&