
    sg1                        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 Zd$dZd Zd Zg Zg ZeD ]:  \  ZZee   Z eee      rej=                  eef       (ej=                  eef       < d Zd Z d Z!d Z"d Z#d  Z$d! Z%d" Z&d# Z'y)%    )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                     t         ryt        t        j                  d      5 }| D ]8  }t	        j
                  ||dt        j                        }|dk7  s0 ddd       y 	 ddd       y# 1 sw Y   yxY w)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retcodes       T/var/www/html/venv/lib/python3.12/site-packages/sympy/external/tests/test_codegen.pytry_runr(   w   sr    	bjj#	 $ 	G oogd$%,,.G!| 	  s   2A*A*A**A3c                    |j                         }|t        v sJ |t        v sJ t        j                  dd      j                         }|dvrt        d      t        j                  d|z  d| z        }t        j                         }t        j                  |       |rt        ||dd	       n t        |d      }	|	j                  |dd	       g }
|D ]j  \  }}}}|d
dj                  d |D              d|d}|dk(  rt        |      }t        t!        |            }|
j#                  t        |   ||dz         l |dk(  rd}n"|j%                  d      rd}nt'        d|z        t)        |d      5 }|j                  t        |   ddj                  |
      iz         ddd       t+        |      }|rt+        dg      }nd}|dk(  s	|dk(  rr|rp|rnd } |d        |d        |d        |d         |d        |d        |d!        |d"       t        j                  |       t        j,                  |       n3t/        d#|z  t0        j2                  $       t        j                  |       |sJ d%|d&d'j                  |             |sJ d(|d)d'j                  |             y# 1 sw Y   xY w)*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_files(,c              3   2   K   | ]  }t        |        y wN)str).0args     r'   	<genexpr>zrun_test.<locals>.<genexpr>   s     73c#h7s   z)-()r   )r!   	thresholdzmain.f90Czmain.cz2FIXME: filename extension unknown for language: %sr   
statements Nz
./test.exeFr,   c                 n    t         j                  j                  |       rt        j                  |        y y r2   )r   pathisfileremove)filenames    r'   safe_removezrun_test.<locals>.safe_remove   s#    ww~~h'		(# (    zcodegen.f90z	codegen.cz	codegen.hz	codegen.ozmain.oztest.exezTEST NOT REMOVED: %s)filezfailed to compile z code with:

zfailed to execute z code from:
)uppermain_templatenumerical_test_templater   getenvlower
ValueErrortempfilemkdtempgetcwdchdirr   r
   writejoinfortranize_double_constantsr3   append
startswithNotImplementedErrorr   r(   rmdirprintsysr   )labelroutinesnumerical_testslanguager#   friendlycleanworkoldworkcode_gentest_stringsfn_nameargsexpectedr8   call_stringf_namefcompiledexecutedrA   s                        r'   run_testrj      s    ~~H}$$$.... II-x8>>@E22noo ,x7GDiikGHHTN  	(I=%h	:xT: L.= 	*xSXX7$77Cu5kBK3C	NCI3H="A
 
 		 5			S	!!@8KM 	M 
fc	 Ma	(#|RWW\5J&KK	MM
 x H L>* Ui/H	$ 	M"K K K JHHJ

$t+#**=
  '$))H%' '8 '$))H%' '8GM Ms   6-J33J=c                     ddl j                  d      }j                  d      }fd}d }|j                  ||       } |j                  ||       } | S )z;
    Replaces every literal float with literal doubles
    r   Nz\d+(\.)?\d*[eE]-?\d+z\d+\.\d*(?!\d*d)c                 H    j                  dd| j                  d            S )Nz[eE]dr   )subgroup)matchobjres    r'   subs_expz-fortranize_double_constants.<locals>.subs_exp   s    vvfc8>>!#455rB   c                 *    d| j                  d      z  S )Nz%sd0r   )ro   )rp   s    r'   
subs_floatz/fortranize_double_constants.<locals>.subs_float   s    q)))rB   )rq   compilern   )code_stringpattern_exppattern_floatrr   rt   rq   s        @r'   rQ   rQ      sZ     **45KJJ23M6* //(K8K##J<KrB   c                 p    t        dt              }ddg}	 t        d|g|| |d       y# t        $ r Y yw xY w)Ntest)rz   )      ?r{   V瞯<)rz   )      r}   r|   is_feasibleF)r\   T)r	   r   rj   AssertionError)r[   r#   routinerZ   s       r'   r~   r~      sL    61%G%&O	?Hh	! s   ) 	55c                  ,    dt         v rt        d       y y )Nr   z*`cc' command didn't work as expected (C89)invalid_lang_compilersr    rB   r'   test_C89_ccr         ..9: /rB   c                  ,    dt         v rt        d       y y )Nr   z*`cc' command didn't work as expected (C99)r   r   rB   r'   test_C99_ccr     r   rB   c                  ,    dt         v rt        d       y y )Nr   z'`ifort' command didn't work as expectedr   r   rB   r'   test_F95_ifortr     s    1167 2rB   c                  ,    dt         v rt        d       y y )Nr   z*`gfortran' command didn't work as expectedr   r   rB   r'   test_F95_gfortranr      s    449: 5rB   c                  ,    dt         v rt        d       y y )Nr   z%`g95' command didn't work as expectedr   r   rB   r'   test_F95_g95r   %  s    //45 0rB   c                  t    ddg} dt         t        z   t        z  fg}t        D ]  \  }}t	        d|| ||        y )N)rz   )r{   g      @g      @g      5@r|   )rz   )r}   g       @      r   r|   rz   basic_codegen)r   r   r   valid_lang_commandsrj   )rZ   	name_exprlangr#   s       r'   test_basic_codegenr   ,  sK    .0O 1q5!)$%I- Nh)_dHMNrB   c                     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 ]4  } | |j1                  t.        |            }|j3                  ||f|df       6 @ t4        D ]G  \  }}|j7                  d      rd |t.              fd |t.              fg}ng }t9        d||z   |||       I y )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)皙?      ?皙?+=r9   
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   subsrR   r   rS   rj   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rZ   nameexprxvalrd   r   r#   name_expr_Cs                           r'   test_intrinsic_math1_codegenr   6  sv   "":HHD=ZZ	c!f	d1g	d1g	d1g	SV	d1g	SV	BqE	SV	d1g	d1g	SV	d1gI O E
d! 	ED1d+,H""D4'8U#CD	EE . 2h??3(%(3k71:5NOKK"I$; $	22rB   c                  T   ddl m}  ddlm} d |t        t
              fdt        t
        z  fg}g }|D ]V  \  }}dD ]L  \  }} | |j                  t        |      j                  t
        |            }|j                  |||f|df       N X t        D ]  \  }	}
t        d|||	|
        y )	Nr   r   )atan2
test_atan2test_pow))r   ?)r   皙ɿ)r   r   r   intrinsic_math2)
r   r   r   r   r   r   r   rR   r   rj   )r   r   r   rZ   r   r   r   yvalrd   r   r#   s              r'   test_instrinsic_math2_codegenr   \  s    ">	uQ{#	QTI O J
d= 	JJD$1d+00D9:H""D4,%#HI	JJ . Ph"IhOPrB   c                  B   ddl m}  ddlm}m}m} d |t               |t              z    |t              z   dz  j                         fd | | | | | | | |t        t        z   t        z                                                   fg}g }|D ]l  \  }}dD ]b  \  }}	}
 | |j                  t        |      j                  t        |	      j                  t        |
            }|j                  |||	|
f|df       d n t        D ]  \  }}t        d	||||        y )
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   rR   r   rj   )r   r   r   r   r   rZ   r   r   r   r   zvalrd   r   r#   s                 r'   test_complicated_codegenr   m  s    "HH	CFSVOc!f,q088:;	#c#c#c#c!a%!)n*=&>"?@ABCDI O P
d S 	PD$1d+00D9>>q$GHH""D4t*<h#NO	PP . Oh!9otX	OOrB   )T)(	sympy.abcr   r   r   sympy.externalr   sympy.testing.pytestr   sympy.utilities.codegenr   r	   r
   rW   r   rK   r    r   rF   rG   compile_commandscombinations_lang_compilerr(   rj   rQ   r~   r   r   r   compilerr#   rR   r   r   r   r   r   r   r   r   r   r   rB   r'   <module>r      s{  0   ( % M M 
 	   <(
 
e e  %U+e   "   "9!?  "              
]'@(   0 8ND()H4"""D(#34%%tX&678;
;
8
;
6N#2LP"OrB   