
    g1                     
   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 ]H\  ZZee         Z eee          re                    eef           1e                    eef           Id Zd Z d Z!d Z"d  Z#d! Z$d" Z%d# Z&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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dS t          t          j        d          5 }| D ]8}t	          j        ||dt          j                  }|dk    r ddd           dS 9	 ddd           n# 1 swxY w 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retcodes       ]/var/www/html/ai-engine/env/lib/python3.11/site-packages/sympy/external/tests/test_codegen.pytry_runr(   w   s     u	bj#		 $ 	 	G ogd$%,. . .G!||        	               4s   .A-A--A14A1Tc                 r   |                                 }|t          v sJ |t          v sJ t          j        dd                                          }|dvrt          d          t          j        d|z  d| z            }t          j	                    }t          j
        |           |rt          ||dd	           n(t          |d          }	|	                    |dd	           g }
|D ]\  }}}}|d
d                    d |D                       d|d}|dk    r+t          |          }t          t!          |                    }|
                    t          |         ||dz             |dk    rd}n*|                    d          rd}nt'          d|z            t)          |d          5 }|                    t          |         dd                    |
          iz             ddd           n# 1 swxY w Y   t+          |          }|rt+          dg          }nd}|dk    s
|dk    r|r|rd } |d            |d            |d            |d             |d            |d            |d!            |d"           t          j
        |           t          j        |           n2t/          d#|z  t0          j        $           t          j
        |           |s#J d%|d&d'                    |                      |s#J d(|d)d'                    |                      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_files(,c              3   4   K   | ]}t          |          V  d S N)str).0args     r'   	<genexpr>zrun_test.<locals>.<genexpr>   s(      773c#hh777777    z)-()r   )r!   	thresholdzmain.f90Czmain.cz2FIXME: filename extension unknown for language: %sr   
statements Nz
./test.exeFr,   c                 p    t           j                            |           rt          j        |            d S d S r2   )r   pathisfileremove)filenames    r'   safe_removezrun_test.<locals>.safe_remove   s8    w~~h'' $	(#####$ $r7   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expectedr9   call_stringf_namefcompiledexecutedrB   s                        r'   run_testrj      s_    ~~H}$$$$..... I-x88>>@@E222nooo ,x7GGDikkGHTNNN  ; 	(I=====%h	::xT::: L.= 	 	*xGGSXX77$7777777Cu5kBBK3C	NNCCI3H="A
 A
 
 	 	 	 	
 5			S	!	! M!@8KM M 	M 
fc		 Ma	(#|RWW\5J5J&KK	M 	M 	MM M M M M M M M M M M M M M M
 x  H  L>** Ui//H//	$ 	$ 	$ 	M"""K   K   K   JHHJ

$t+#*====
  ' ' '$))H%%%' ' ' ' ' ' '$))H%%%' ' ' ' ' 's   39G88G<?G<c                     ddl                     d          }                    d          }fd}d }|                    ||           } |                    ||           } | S )z;
    Replaces every literal float with literal doubles
    r   Nz\d+(\.)?\d*[eE]-?\d+z\d+\.\d*(?!\d*d)c                 X                         dd|                     d                    S )Nz[eE]dr   )subgroup)matchobjres    r'   subs_expz-fortranize_double_constants.<locals>.subs_exp   s%    vvfc8>>!#4#4555r7   c                 2    d|                      d          z  S )Nz%sd0r   )ro   )rp   s    r'   
subs_floatz/fortranize_double_constants.<locals>.subs_float   s    q))))r7   )rq   compilern   )code_stringpattern_exppattern_floatrr   rt   rq   s        @r'   rQ   rQ      s     III**455KJJ233M6 6 6 6 6* * * //(K88K##J<<Kr7   c                     t          dt                    }ddg}	 t          d|g|| |d           dS # t          $ r Y dS 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~      sp    61%%G%&O	?Hh	! 	! 	! 	!t   uus   3 
A Ac                  :    dt           v rt          d           d S d S )Nr   z*`cc' command didn't work as expected (C89)invalid_lang_compilersr    r7   r'   test_C89_ccr     +    ...9::::: /.r7   c                  :    dt           v rt          d           d S d S )Nr   z*`cc' command didn't work as expected (C99)r   r   r7   r'   test_C99_ccr     r   r7   c                  :    dt           v rt          d           d S d S )Nr   z'`ifort' command didn't work as expectedr   r   r7   r'   test_F95_ifortr     s+    111677777 21r7   c                  :    dt           v rt          d           d S d S )Nr   z*`gfortran' command didn't work as expectedr   r   r7   r'   test_F95_gfortranr      s+    4449::::: 54r7   c                  :    dt           v rt          d           d S d S )Nr   z%`g95' command didn't work as expectedr   r   r7   r'   test_F95_g95r   %  s+    ///455555 0/r7   c                      ddg} dt           t          z   t          z  fg}t          D ]\  }}t	          d|| ||           d S )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   ,  s`    .0O 1q5!)$%I- N Nh)_dHMMMMN Nr7   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 ]H\  }}dD ]@} | |                    t.          |                    }|                    ||f|df           AIt4          D ]W\  }}|                    d          r%d |t.                    fd |t.                    fg}ng }t9          d||z   |||           Xd 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)皙?      ?皙?+=r:   
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  s}   """"""""""""::::::HHHHHHHHHHDDDDDDDD======ZZZZZZZZZZZZZZZZ	c!ff	dd1gg	dd1gg	dd1gg	SSVV	dd1gg	SSVV	BBqEE	SSVV	dd1gg	dd1gg	SSVV	dd1ggI O E E
d! 	E 	EDq1d++,,H""D4'8U#CDDDD	E . 2 2h??3 	(%%((3k771::5NOKKK"I$; $	2 	2 	2 	22 2r7   c                     ddl m}  ddlm} d |t          t
                    fdt          t
          z  fg}g }|D ]e\  }}dD ]]\  }} | |                    t          |                              t
          |                    }|                    |||f|df           ^ft          D ]\  }	}
t          d|||	|
           d S )	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   """""">>>>>>	uuQ{{#	QTI O J J
d= 	J 	JJD$q1d++00D99::H""D4,%#HIIII	J . P Ph"IhOOOOP Pr7   c                     ddl m}  ddlm}m}m} d |t                     |t                    z    |t                    z   dz  	                                fd | | | | | | | |t          t          z   t          z                                                                                   fg}g }|D ]\  }}dD ]x\  }}	}
 | |
                    t          |          
                    t          |	          
                    t          |
                    }|                    |||	|
f|df           yt          D ]\  }}t          d	||||           d 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   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HHHHHHHH	CCFFSSVVOcc!ff,q088::;	##cc##cc##cc##cc!a%!)nn*=*=&>&>"?"?@@AABBCCDI O P P
d S 	P 	PD$q1d++00D99>>q$GGHHH""D4t*<h#NOOOO	P . O Oh!9otX	O 	O 	O 	OO Or7   )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   r7   r'   <module>r      s  0           ( ( ( ( ( ( % % % % % % M M M M M M M M M M 



 				      ]<((
 
e e  %U+e   "   "9!?  "                          
 
 
]' ]' ]' ]'@  (     0 8 8ND()H{4"" 8""D(#34444%%tX&67777; ; ;
; ; ;
8 8 8
; ; ;
6 6 6N N N#2 #2 #2LP P P"O O O O Or7   