BryanW commited on
Commit
daf10aa
·
verified ·
1 Parent(s): a794b88

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/__pycache__/__init__.cpython-312.pyc +0 -0
  2. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/__pycache__/abstract_nodes.cpython-312.pyc +0 -0
  3. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/__pycache__/algorithms.cpython-312.pyc +0 -0
  4. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/__pycache__/approximations.cpython-312.pyc +0 -0
  5. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/__pycache__/ast.cpython-312.pyc +0 -0
  6. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/__pycache__/cfunctions.cpython-312.pyc +0 -0
  7. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/__pycache__/cnodes.cpython-312.pyc +0 -0
  8. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/__pycache__/cutils.cpython-312.pyc +0 -0
  9. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/__pycache__/cxxnodes.cpython-312.pyc +0 -0
  10. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/__pycache__/fnodes.cpython-312.pyc +0 -0
  11. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/__pycache__/futils.cpython-312.pyc +0 -0
  12. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/__pycache__/matrix_nodes.cpython-312.pyc +0 -0
  13. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/__pycache__/numpy_nodes.cpython-312.pyc +0 -0
  14. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/__pycache__/pynodes.cpython-312.pyc +0 -0
  15. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/__pycache__/pyutils.cpython-312.pyc +0 -0
  16. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/__pycache__/rewriting.cpython-312.pyc +0 -0
  17. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/__pycache__/scipy_nodes.cpython-312.pyc +0 -0
  18. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/__init__.py +0 -0
  19. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/__pycache__/__init__.cpython-312.pyc +0 -0
  20. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/__pycache__/test_abstract_nodes.cpython-312.pyc +0 -0
  21. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/__pycache__/test_algorithms.cpython-312.pyc +0 -0
  22. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/__pycache__/test_applications.cpython-312.pyc +0 -0
  23. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/__pycache__/test_approximations.cpython-312.pyc +0 -0
  24. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/__pycache__/test_ast.cpython-312.pyc +0 -0
  25. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/__pycache__/test_cfunctions.cpython-312.pyc +0 -0
  26. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/__pycache__/test_cnodes.cpython-312.pyc +0 -0
  27. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/__pycache__/test_cxxnodes.cpython-312.pyc +0 -0
  28. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/__pycache__/test_fnodes.cpython-312.pyc +0 -0
  29. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/__pycache__/test_matrix_nodes.cpython-312.pyc +0 -0
  30. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/__pycache__/test_numpy_nodes.cpython-312.pyc +0 -0
  31. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/__pycache__/test_pynodes.cpython-312.pyc +0 -0
  32. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/__pycache__/test_pyutils.cpython-312.pyc +0 -0
  33. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/__pycache__/test_rewriting.cpython-312.pyc +0 -0
  34. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/__pycache__/test_scipy_nodes.cpython-312.pyc +0 -0
  35. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/test_abstract_nodes.py +14 -0
  36. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/test_algorithms.py +180 -0
  37. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/test_applications.py +58 -0
  38. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/test_approximations.py +53 -0
  39. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/test_ast.py +661 -0
  40. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/test_cfunctions.py +186 -0
  41. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/test_cnodes.py +112 -0
  42. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/test_cxxnodes.py +14 -0
  43. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/test_fnodes.py +213 -0
  44. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/test_matrix_nodes.py +50 -0
  45. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/test_numpy_nodes.py +69 -0
  46. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/test_pynodes.py +13 -0
  47. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/test_pyutils.py +7 -0
  48. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/test_rewriting.py +479 -0
  49. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/test_scipy_nodes.py +44 -0
  50. Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/core/__pycache__/__init__.cpython-312.pyc +0 -0
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (1.16 kB). View file
 
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/__pycache__/abstract_nodes.cpython-312.pyc ADDED
Binary file (1.16 kB). View file
 
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/__pycache__/algorithms.cpython-312.pyc ADDED
Binary file (8.66 kB). View file
 
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/__pycache__/approximations.cpython-312.pyc ADDED
Binary file (9.05 kB). View file
 
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/__pycache__/ast.cpython-312.pyc ADDED
Binary file (74 kB). View file
 
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/__pycache__/cfunctions.cpython-312.pyc ADDED
Binary file (19.1 kB). View file
 
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/__pycache__/cnodes.cpython-312.pyc ADDED
Binary file (5.64 kB). View file
 
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/__pycache__/cutils.cpython-312.pyc ADDED
Binary file (827 Bytes). View file
 
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/__pycache__/cxxnodes.cpython-312.pyc ADDED
Binary file (812 Bytes). View file
 
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/__pycache__/fnodes.cpython-312.pyc ADDED
Binary file (26.2 kB). View file
 
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/__pycache__/futils.cpython-312.pyc ADDED
Binary file (2.53 kB). View file
 
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/__pycache__/matrix_nodes.cpython-312.pyc ADDED
Binary file (3.13 kB). View file
 
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/__pycache__/numpy_nodes.cpython-312.pyc ADDED
Binary file (7.95 kB). View file
 
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/__pycache__/pynodes.cpython-312.pyc ADDED
Binary file (758 Bytes). View file
 
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/__pycache__/pyutils.cpython-312.pyc ADDED
Binary file (1.5 kB). View file
 
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/__pycache__/rewriting.cpython-312.pyc ADDED
Binary file (18.4 kB). View file
 
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/__pycache__/scipy_nodes.cpython-312.pyc ADDED
Binary file (4.45 kB). View file
 
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/__init__.py ADDED
File without changes
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (225 Bytes). View file
 
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/__pycache__/test_abstract_nodes.cpython-312.pyc ADDED
Binary file (1.32 kB). View file
 
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/__pycache__/test_algorithms.cpython-312.pyc ADDED
Binary file (10.9 kB). View file
 
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/__pycache__/test_applications.cpython-312.pyc ADDED
Binary file (3.58 kB). View file
 
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/__pycache__/test_approximations.cpython-312.pyc ADDED
Binary file (3.55 kB). View file
 
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/__pycache__/test_ast.cpython-312.pyc ADDED
Binary file (48.4 kB). View file
 
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/__pycache__/test_cfunctions.cpython-312.pyc ADDED
Binary file (12.6 kB). View file
 
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/__pycache__/test_cnodes.cpython-312.pyc ADDED
Binary file (6.73 kB). View file
 
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/__pycache__/test_cxxnodes.cpython-312.pyc ADDED
Binary file (860 Bytes). View file
 
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/__pycache__/test_fnodes.cpython-312.pyc ADDED
Binary file (11 kB). View file
 
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/__pycache__/test_matrix_nodes.cpython-312.pyc ADDED
Binary file (4.16 kB). View file
 
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/__pycache__/test_numpy_nodes.cpython-312.pyc ADDED
Binary file (4.74 kB). View file
 
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/__pycache__/test_pynodes.cpython-312.pyc ADDED
Binary file (1.28 kB). View file
 
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/__pycache__/test_pyutils.cpython-312.pyc ADDED
Binary file (715 Bytes). View file
 
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/__pycache__/test_rewriting.cpython-312.pyc ADDED
Binary file (29.7 kB). View file
 
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/__pycache__/test_scipy_nodes.cpython-312.pyc ADDED
Binary file (3.5 kB). View file
 
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/test_abstract_nodes.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core.symbol import symbols
2
+ from sympy.codegen.abstract_nodes import List
3
+
4
+
5
+ def test_List():
6
+ l = List(2, 3, 4)
7
+ assert l == List(2, 3, 4)
8
+ assert str(l) == "[2, 3, 4]"
9
+ x, y, z = symbols('x y z')
10
+ l = List(x**2,y**3,z**4)
11
+ # contrary to python's built-in list, we can call e.g. "replace" on List.
12
+ m = l.replace(lambda arg: arg.is_Pow and arg.exp>2, lambda p: p.base-p.exp)
13
+ assert m == [x**2, y-3, z-4]
14
+ hash(m)
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/test_algorithms.py ADDED
@@ -0,0 +1,180 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tempfile
2
+ from sympy import log, Min, Max, sqrt
3
+ from sympy.core.numbers import Float
4
+ from sympy.core.symbol import Symbol, symbols
5
+ from sympy.functions.elementary.trigonometric import cos
6
+ from sympy.codegen.ast import Assignment, Raise, RuntimeError_, QuotedString
7
+ from sympy.codegen.algorithms import newtons_method, newtons_method_function
8
+ from sympy.codegen.cfunctions import expm1
9
+ from sympy.codegen.fnodes import bind_C
10
+ from sympy.codegen.futils import render_as_module as f_module
11
+ from sympy.codegen.pyutils import render_as_module as py_module
12
+ from sympy.external import import_module
13
+ from sympy.printing.codeprinter import ccode
14
+ from sympy.utilities._compilation import compile_link_import_strings, has_c, has_fortran
15
+ from sympy.utilities._compilation.util import may_xfail
16
+ from sympy.testing.pytest import skip, raises, skip_under_pyodide
17
+
18
+ cython = import_module('cython')
19
+ wurlitzer = import_module('wurlitzer')
20
+
21
+ def test_newtons_method():
22
+ x, dx, atol = symbols('x dx atol')
23
+ expr = cos(x) - x**3
24
+ algo = newtons_method(expr, x, atol, dx)
25
+ assert algo.has(Assignment(dx, -expr/expr.diff(x)))
26
+
27
+
28
+ @may_xfail
29
+ def test_newtons_method_function__ccode():
30
+ x = Symbol('x', real=True)
31
+ expr = cos(x) - x**3
32
+ func = newtons_method_function(expr, x)
33
+
34
+ if not cython:
35
+ skip("cython not installed.")
36
+ if not has_c():
37
+ skip("No C compiler found.")
38
+
39
+ compile_kw = {"std": 'c99'}
40
+ with tempfile.TemporaryDirectory() as folder:
41
+ mod, info = compile_link_import_strings([
42
+ ('newton.c', ('#include <math.h>\n'
43
+ '#include <stdio.h>\n') + ccode(func)),
44
+ ('_newton.pyx', ("#cython: language_level={}\n".format("3") +
45
+ "cdef extern double newton(double)\n"
46
+ "def py_newton(x):\n"
47
+ " return newton(x)\n"))
48
+ ], build_dir=folder, compile_kwargs=compile_kw)
49
+ assert abs(mod.py_newton(0.5) - 0.865474033102) < 1e-12
50
+
51
+
52
+ @may_xfail
53
+ def test_newtons_method_function__fcode():
54
+ x = Symbol('x', real=True)
55
+ expr = cos(x) - x**3
56
+ func = newtons_method_function(expr, x, attrs=[bind_C(name='newton')])
57
+
58
+ if not cython:
59
+ skip("cython not installed.")
60
+ if not has_fortran():
61
+ skip("No Fortran compiler found.")
62
+
63
+ f_mod = f_module([func], 'mod_newton')
64
+ with tempfile.TemporaryDirectory() as folder:
65
+ mod, info = compile_link_import_strings([
66
+ ('newton.f90', f_mod),
67
+ ('_newton.pyx', ("#cython: language_level={}\n".format("3") +
68
+ "cdef extern double newton(double*)\n"
69
+ "def py_newton(double x):\n"
70
+ " return newton(&x)\n"))
71
+ ], build_dir=folder)
72
+ assert abs(mod.py_newton(0.5) - 0.865474033102) < 1e-12
73
+
74
+
75
+ def test_newtons_method_function__pycode():
76
+ x = Symbol('x', real=True)
77
+ expr = cos(x) - x**3
78
+ func = newtons_method_function(expr, x)
79
+ py_mod = py_module(func)
80
+ namespace = {}
81
+ exec(py_mod, namespace, namespace)
82
+ res = eval('newton(0.5)', namespace)
83
+ assert abs(res - 0.865474033102) < 1e-12
84
+
85
+
86
+ @may_xfail
87
+ @skip_under_pyodide("Emscripten does not support process spawning")
88
+ def test_newtons_method_function__ccode_parameters():
89
+ args = x, A, k, p = symbols('x A k p')
90
+ expr = A*cos(k*x) - p*x**3
91
+ raises(ValueError, lambda: newtons_method_function(expr, x))
92
+ use_wurlitzer = wurlitzer
93
+
94
+ func = newtons_method_function(expr, x, args, debug=use_wurlitzer)
95
+
96
+ if not has_c():
97
+ skip("No C compiler found.")
98
+ if not cython:
99
+ skip("cython not installed.")
100
+
101
+ compile_kw = {"std": 'c99'}
102
+ with tempfile.TemporaryDirectory() as folder:
103
+ mod, info = compile_link_import_strings([
104
+ ('newton_par.c', ('#include <math.h>\n'
105
+ '#include <stdio.h>\n') + ccode(func)),
106
+ ('_newton_par.pyx', ("#cython: language_level={}\n".format("3") +
107
+ "cdef extern double newton(double, double, double, double)\n"
108
+ "def py_newton(x, A=1, k=1, p=1):\n"
109
+ " return newton(x, A, k, p)\n"))
110
+ ], compile_kwargs=compile_kw, build_dir=folder)
111
+
112
+ if use_wurlitzer:
113
+ with wurlitzer.pipes() as (out, err):
114
+ result = mod.py_newton(0.5)
115
+ else:
116
+ result = mod.py_newton(0.5)
117
+
118
+ assert abs(result - 0.865474033102) < 1e-12
119
+
120
+ if not use_wurlitzer:
121
+ skip("C-level output only tested when package 'wurlitzer' is available.")
122
+
123
+ out, err = out.read(), err.read()
124
+ assert err == ''
125
+ assert out == """\
126
+ x= 0.5
127
+ x= 1.1121 d_x= 0.61214
128
+ x= 0.90967 d_x= -0.20247
129
+ x= 0.86726 d_x= -0.042409
130
+ x= 0.86548 d_x= -0.0017867
131
+ x= 0.86547 d_x= -3.1022e-06
132
+ x= 0.86547 d_x= -9.3421e-12
133
+ x= 0.86547 d_x= 3.6902e-17
134
+ """ # try to run tests with LC_ALL=C if this assertion fails
135
+
136
+
137
+ def test_newtons_method_function__rtol_cse_nan():
138
+ a, b, c, N_geo, N_tot = symbols('a b c N_geo N_tot', real=True, nonnegative=True)
139
+ i = Symbol('i', integer=True, nonnegative=True)
140
+ N_ari = N_tot - N_geo - 1
141
+ delta_ari = (c-b)/N_ari
142
+ ln_delta_geo = log(b) + log(-expm1((log(a)-log(b))/N_geo))
143
+ eqb_log = ln_delta_geo - log(delta_ari)
144
+
145
+ def _clamp(low, expr, high):
146
+ return Min(Max(low, expr), high)
147
+
148
+ meth_kw = {
149
+ 'clamped_newton': {'delta_fn': lambda e, x: _clamp(
150
+ (sqrt(a*x)-x)*0.99,
151
+ -e/e.diff(x),
152
+ (sqrt(c*x)-x)*0.99
153
+ )},
154
+ 'halley': {'delta_fn': lambda e, x: (-2*(e*e.diff(x))/(2*e.diff(x)**2 - e*e.diff(x, 2)))},
155
+ 'halley_alt': {'delta_fn': lambda e, x: (-e/e.diff(x)/(1-e/e.diff(x)*e.diff(x,2)/2/e.diff(x)))},
156
+ }
157
+ args = eqb_log, b
158
+ for use_cse in [False, True]:
159
+ kwargs = {
160
+ 'params': (b, a, c, N_geo, N_tot), 'itermax': 60, 'debug': True, 'cse': use_cse,
161
+ 'counter': i, 'atol': 1e-100, 'rtol': 2e-16, 'bounds': (a,c),
162
+ 'handle_nan': Raise(RuntimeError_(QuotedString("encountered NaN.")))
163
+ }
164
+ func = {k: newtons_method_function(*args, func_name=f"{k}_b", **dict(kwargs, **kw)) for k, kw in meth_kw.items()}
165
+ py_mod = {k: py_module(v) for k, v in func.items()}
166
+ namespace = {}
167
+ root_find_b = {}
168
+ for k, v in py_mod.items():
169
+ ns = namespace[k] = {}
170
+ exec(v, ns, ns)
171
+ root_find_b[k] = ns[f'{k}_b']
172
+ ref = Float('13.2261515064168768938151923226496')
173
+ reftol = {'clamped_newton': 2e-16, 'halley': 2e-16, 'halley_alt': 3e-16}
174
+ guess = 4.0
175
+ for meth, func in root_find_b.items():
176
+ result = func(guess, 1e-2, 1e2, 50, 100)
177
+ req = ref*reftol[meth]
178
+ if use_cse:
179
+ req *= 2
180
+ assert abs(result - ref) < req
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/test_applications.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # This file contains tests that exercise multiple AST nodes
2
+
3
+ import tempfile
4
+
5
+ from sympy.external import import_module
6
+ from sympy.printing.codeprinter import ccode
7
+ from sympy.utilities._compilation import compile_link_import_strings, has_c
8
+ from sympy.utilities._compilation.util import may_xfail
9
+ from sympy.testing.pytest import skip, skip_under_pyodide
10
+ from sympy.codegen.ast import (
11
+ FunctionDefinition, FunctionPrototype, Variable, Pointer, real, Assignment,
12
+ integer, CodeBlock, While
13
+ )
14
+ from sympy.codegen.cnodes import void, PreIncrement
15
+ from sympy.codegen.cutils import render_as_source_file
16
+
17
+ cython = import_module('cython')
18
+ np = import_module('numpy')
19
+
20
+ def _mk_func1():
21
+ declars = n, inp, out = Variable('n', integer), Pointer('inp', real), Pointer('out', real)
22
+ i = Variable('i', integer)
23
+ whl = While(i<n, [Assignment(out[i], inp[i]), PreIncrement(i)])
24
+ body = CodeBlock(i.as_Declaration(value=0), whl)
25
+ return FunctionDefinition(void, 'our_test_function', declars, body)
26
+
27
+
28
+ def _render_compile_import(funcdef, build_dir):
29
+ code_str = render_as_source_file(funcdef, settings={"contract": False})
30
+ declar = ccode(FunctionPrototype.from_FunctionDefinition(funcdef))
31
+ return compile_link_import_strings([
32
+ ('our_test_func.c', code_str),
33
+ ('_our_test_func.pyx', ("#cython: language_level={}\n".format("3") +
34
+ "cdef extern {declar}\n"
35
+ "def _{fname}({typ}[:] inp, {typ}[:] out):\n"
36
+ " {fname}(inp.size, &inp[0], &out[0])").format(
37
+ declar=declar, fname=funcdef.name, typ='double'
38
+ ))
39
+ ], build_dir=build_dir)
40
+
41
+
42
+ @may_xfail
43
+ @skip_under_pyodide("Emscripten does not support process spawning")
44
+ def test_copying_function():
45
+ if not np:
46
+ skip("numpy not installed.")
47
+ if not has_c():
48
+ skip("No C compiler found.")
49
+ if not cython:
50
+ skip("Cython not found.")
51
+
52
+ info = None
53
+ with tempfile.TemporaryDirectory() as folder:
54
+ mod, info = _render_compile_import(_mk_func1(), build_dir=folder)
55
+ inp = np.arange(10.0)
56
+ out = np.empty_like(inp)
57
+ mod._our_test_function(inp, out)
58
+ assert np.allclose(inp, out)
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/test_approximations.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ from sympy.core.symbol import symbols
3
+ from sympy.functions.elementary.exponential import exp
4
+ from sympy.codegen.rewriting import optimize
5
+ from sympy.codegen.approximations import SumApprox, SeriesApprox
6
+
7
+
8
+ def test_SumApprox_trivial():
9
+ x = symbols('x')
10
+ expr1 = 1 + x
11
+ sum_approx = SumApprox(bounds={x: (-1e-20, 1e-20)}, reltol=1e-16)
12
+ apx1 = optimize(expr1, [sum_approx])
13
+ assert apx1 - 1 == 0
14
+
15
+
16
+ def test_SumApprox_monotone_terms():
17
+ x, y, z = symbols('x y z')
18
+ expr1 = exp(z)*(x**2 + y**2 + 1)
19
+ bnds1 = {x: (0, 1e-3), y: (100, 1000)}
20
+ sum_approx_m2 = SumApprox(bounds=bnds1, reltol=1e-2)
21
+ sum_approx_m5 = SumApprox(bounds=bnds1, reltol=1e-5)
22
+ sum_approx_m11 = SumApprox(bounds=bnds1, reltol=1e-11)
23
+ assert (optimize(expr1, [sum_approx_m2])/exp(z) - (y**2)).simplify() == 0
24
+ assert (optimize(expr1, [sum_approx_m5])/exp(z) - (y**2 + 1)).simplify() == 0
25
+ assert (optimize(expr1, [sum_approx_m11])/exp(z) - (y**2 + 1 + x**2)).simplify() == 0
26
+
27
+
28
+ def test_SeriesApprox_trivial():
29
+ x, z = symbols('x z')
30
+ for factor in [1, exp(z)]:
31
+ x = symbols('x')
32
+ expr1 = exp(x)*factor
33
+ bnds1 = {x: (-1, 1)}
34
+ series_approx_50 = SeriesApprox(bounds=bnds1, reltol=0.50)
35
+ series_approx_10 = SeriesApprox(bounds=bnds1, reltol=0.10)
36
+ series_approx_05 = SeriesApprox(bounds=bnds1, reltol=0.05)
37
+ c = (bnds1[x][1] + bnds1[x][0])/2 # 0.0
38
+ f0 = math.exp(c) # 1.0
39
+
40
+ ref_50 = f0 + x + x**2/2
41
+ ref_10 = f0 + x + x**2/2 + x**3/6
42
+ ref_05 = f0 + x + x**2/2 + x**3/6 + x**4/24
43
+
44
+ res_50 = optimize(expr1, [series_approx_50])
45
+ res_10 = optimize(expr1, [series_approx_10])
46
+ res_05 = optimize(expr1, [series_approx_05])
47
+
48
+ assert (res_50/factor - ref_50).simplify() == 0
49
+ assert (res_10/factor - ref_10).simplify() == 0
50
+ assert (res_05/factor - ref_05).simplify() == 0
51
+
52
+ max_ord3 = SeriesApprox(bounds=bnds1, reltol=0.05, max_order=3)
53
+ assert optimize(expr1, [max_ord3]) == expr1
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/test_ast.py ADDED
@@ -0,0 +1,661 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ from sympy.core.containers import Tuple
3
+ from sympy.core.numbers import nan, oo, Float, Integer
4
+ from sympy.core.relational import Lt
5
+ from sympy.core.symbol import symbols, Symbol
6
+ from sympy.functions.elementary.trigonometric import sin
7
+ from sympy.matrices.dense import Matrix
8
+ from sympy.matrices.expressions.matexpr import MatrixSymbol
9
+ from sympy.sets.fancysets import Range
10
+ from sympy.tensor.indexed import Idx, IndexedBase
11
+ from sympy.testing.pytest import raises
12
+
13
+
14
+ from sympy.codegen.ast import (
15
+ Assignment, Attribute, aug_assign, CodeBlock, For, Type, Variable, Pointer, Declaration,
16
+ AddAugmentedAssignment, SubAugmentedAssignment, MulAugmentedAssignment,
17
+ DivAugmentedAssignment, ModAugmentedAssignment, value_const, pointer_const,
18
+ integer, real, complex_, int8, uint8, float16 as f16, float32 as f32,
19
+ float64 as f64, float80 as f80, float128 as f128, complex64 as c64, complex128 as c128,
20
+ While, Scope, String, Print, QuotedString, FunctionPrototype, FunctionDefinition, Return,
21
+ FunctionCall, untyped, IntBaseType, intc, Node, none, NoneToken, Token, Comment
22
+ )
23
+
24
+ x, y, z, t, x0, x1, x2, a, b = symbols("x, y, z, t, x0, x1, x2, a, b")
25
+ n = symbols("n", integer=True)
26
+ A = MatrixSymbol('A', 3, 1)
27
+ mat = Matrix([1, 2, 3])
28
+ B = IndexedBase('B')
29
+ i = Idx("i", n)
30
+ A22 = MatrixSymbol('A22',2,2)
31
+ B22 = MatrixSymbol('B22',2,2)
32
+
33
+
34
+ def test_Assignment():
35
+ # Here we just do things to show they don't error
36
+ Assignment(x, y)
37
+ Assignment(x, 0)
38
+ Assignment(A, mat)
39
+ Assignment(A[1,0], 0)
40
+ Assignment(A[1,0], x)
41
+ Assignment(B[i], x)
42
+ Assignment(B[i], 0)
43
+ a = Assignment(x, y)
44
+ assert a.func(*a.args) == a
45
+ assert a.op == ':='
46
+ # Here we test things to show that they error
47
+ # Matrix to scalar
48
+ raises(ValueError, lambda: Assignment(B[i], A))
49
+ raises(ValueError, lambda: Assignment(B[i], mat))
50
+ raises(ValueError, lambda: Assignment(x, mat))
51
+ raises(ValueError, lambda: Assignment(x, A))
52
+ raises(ValueError, lambda: Assignment(A[1,0], mat))
53
+ # Scalar to matrix
54
+ raises(ValueError, lambda: Assignment(A, x))
55
+ raises(ValueError, lambda: Assignment(A, 0))
56
+ # Non-atomic lhs
57
+ raises(TypeError, lambda: Assignment(mat, A))
58
+ raises(TypeError, lambda: Assignment(0, x))
59
+ raises(TypeError, lambda: Assignment(x*x, 1))
60
+ raises(TypeError, lambda: Assignment(A + A, mat))
61
+ raises(TypeError, lambda: Assignment(B, 0))
62
+
63
+
64
+ def test_AugAssign():
65
+ # Here we just do things to show they don't error
66
+ aug_assign(x, '+', y)
67
+ aug_assign(x, '+', 0)
68
+ aug_assign(A, '+', mat)
69
+ aug_assign(A[1, 0], '+', 0)
70
+ aug_assign(A[1, 0], '+', x)
71
+ aug_assign(B[i], '+', x)
72
+ aug_assign(B[i], '+', 0)
73
+
74
+ # Check creation via aug_assign vs constructor
75
+ for binop, cls in [
76
+ ('+', AddAugmentedAssignment),
77
+ ('-', SubAugmentedAssignment),
78
+ ('*', MulAugmentedAssignment),
79
+ ('/', DivAugmentedAssignment),
80
+ ('%', ModAugmentedAssignment),
81
+ ]:
82
+ a = aug_assign(x, binop, y)
83
+ b = cls(x, y)
84
+ assert a.func(*a.args) == a == b
85
+ assert a.binop == binop
86
+ assert a.op == binop + '='
87
+
88
+ # Here we test things to show that they error
89
+ # Matrix to scalar
90
+ raises(ValueError, lambda: aug_assign(B[i], '+', A))
91
+ raises(ValueError, lambda: aug_assign(B[i], '+', mat))
92
+ raises(ValueError, lambda: aug_assign(x, '+', mat))
93
+ raises(ValueError, lambda: aug_assign(x, '+', A))
94
+ raises(ValueError, lambda: aug_assign(A[1, 0], '+', mat))
95
+ # Scalar to matrix
96
+ raises(ValueError, lambda: aug_assign(A, '+', x))
97
+ raises(ValueError, lambda: aug_assign(A, '+', 0))
98
+ # Non-atomic lhs
99
+ raises(TypeError, lambda: aug_assign(mat, '+', A))
100
+ raises(TypeError, lambda: aug_assign(0, '+', x))
101
+ raises(TypeError, lambda: aug_assign(x * x, '+', 1))
102
+ raises(TypeError, lambda: aug_assign(A + A, '+', mat))
103
+ raises(TypeError, lambda: aug_assign(B, '+', 0))
104
+
105
+
106
+ def test_Assignment_printing():
107
+ assignment_classes = [
108
+ Assignment,
109
+ AddAugmentedAssignment,
110
+ SubAugmentedAssignment,
111
+ MulAugmentedAssignment,
112
+ DivAugmentedAssignment,
113
+ ModAugmentedAssignment,
114
+ ]
115
+ pairs = [
116
+ (x, 2 * y + 2),
117
+ (B[i], x),
118
+ (A22, B22),
119
+ (A[0, 0], x),
120
+ ]
121
+
122
+ for cls in assignment_classes:
123
+ for lhs, rhs in pairs:
124
+ a = cls(lhs, rhs)
125
+ assert repr(a) == '%s(%s, %s)' % (cls.__name__, repr(lhs), repr(rhs))
126
+
127
+
128
+ def test_CodeBlock():
129
+ c = CodeBlock(Assignment(x, 1), Assignment(y, x + 1))
130
+ assert c.func(*c.args) == c
131
+
132
+ assert c.left_hand_sides == Tuple(x, y)
133
+ assert c.right_hand_sides == Tuple(1, x + 1)
134
+
135
+ def test_CodeBlock_topological_sort():
136
+ assignments = [
137
+ Assignment(x, y + z),
138
+ Assignment(z, 1),
139
+ Assignment(t, x),
140
+ Assignment(y, 2),
141
+ ]
142
+
143
+ ordered_assignments = [
144
+ # Note that the unrelated z=1 and y=2 are kept in that order
145
+ Assignment(z, 1),
146
+ Assignment(y, 2),
147
+ Assignment(x, y + z),
148
+ Assignment(t, x),
149
+ ]
150
+ c1 = CodeBlock.topological_sort(assignments)
151
+ assert c1 == CodeBlock(*ordered_assignments)
152
+
153
+ # Cycle
154
+ invalid_assignments = [
155
+ Assignment(x, y + z),
156
+ Assignment(z, 1),
157
+ Assignment(y, x),
158
+ Assignment(y, 2),
159
+ ]
160
+
161
+ raises(ValueError, lambda: CodeBlock.topological_sort(invalid_assignments))
162
+
163
+ # Free symbols
164
+ free_assignments = [
165
+ Assignment(x, y + z),
166
+ Assignment(z, a * b),
167
+ Assignment(t, x),
168
+ Assignment(y, b + 3),
169
+ ]
170
+
171
+ free_assignments_ordered = [
172
+ Assignment(z, a * b),
173
+ Assignment(y, b + 3),
174
+ Assignment(x, y + z),
175
+ Assignment(t, x),
176
+ ]
177
+
178
+ c2 = CodeBlock.topological_sort(free_assignments)
179
+ assert c2 == CodeBlock(*free_assignments_ordered)
180
+
181
+ def test_CodeBlock_free_symbols():
182
+ c1 = CodeBlock(
183
+ Assignment(x, y + z),
184
+ Assignment(z, 1),
185
+ Assignment(t, x),
186
+ Assignment(y, 2),
187
+ )
188
+ assert c1.free_symbols == set()
189
+
190
+ c2 = CodeBlock(
191
+ Assignment(x, y + z),
192
+ Assignment(z, a * b),
193
+ Assignment(t, x),
194
+ Assignment(y, b + 3),
195
+ )
196
+ assert c2.free_symbols == {a, b}
197
+
198
+ def test_CodeBlock_cse():
199
+ c1 = CodeBlock(
200
+ Assignment(y, 1),
201
+ Assignment(x, sin(y)),
202
+ Assignment(z, sin(y)),
203
+ Assignment(t, x*z),
204
+ )
205
+ assert c1.cse() == CodeBlock(
206
+ Assignment(y, 1),
207
+ Assignment(x0, sin(y)),
208
+ Assignment(x, x0),
209
+ Assignment(z, x0),
210
+ Assignment(t, x*z),
211
+ )
212
+
213
+ # Multiple assignments to same symbol not supported
214
+ raises(NotImplementedError, lambda: CodeBlock(
215
+ Assignment(x, 1),
216
+ Assignment(y, 1), Assignment(y, 2)
217
+ ).cse())
218
+
219
+ # Check auto-generated symbols do not collide with existing ones
220
+ c2 = CodeBlock(
221
+ Assignment(x0, sin(y) + 1),
222
+ Assignment(x1, 2 * sin(y)),
223
+ Assignment(z, x * y),
224
+ )
225
+ assert c2.cse() == CodeBlock(
226
+ Assignment(x2, sin(y)),
227
+ Assignment(x0, x2 + 1),
228
+ Assignment(x1, 2 * x2),
229
+ Assignment(z, x * y),
230
+ )
231
+
232
+
233
+ def test_CodeBlock_cse__issue_14118():
234
+ # see https://github.com/sympy/sympy/issues/14118
235
+ c = CodeBlock(
236
+ Assignment(A22, Matrix([[x, sin(y)],[3, 4]])),
237
+ Assignment(B22, Matrix([[sin(y), 2*sin(y)], [sin(y)**2, 7]]))
238
+ )
239
+ assert c.cse() == CodeBlock(
240
+ Assignment(x0, sin(y)),
241
+ Assignment(A22, Matrix([[x, x0],[3, 4]])),
242
+ Assignment(B22, Matrix([[x0, 2*x0], [x0**2, 7]]))
243
+ )
244
+
245
+ def test_For():
246
+ f = For(n, Range(0, 3), (Assignment(A[n, 0], x + n), aug_assign(x, '+', y)))
247
+ f = For(n, (1, 2, 3, 4, 5), (Assignment(A[n, 0], x + n),))
248
+ assert f.func(*f.args) == f
249
+ raises(TypeError, lambda: For(n, x, (x + y,)))
250
+
251
+
252
+ def test_none():
253
+ assert none.is_Atom
254
+ assert none == none
255
+ class Foo(Token):
256
+ pass
257
+ foo = Foo()
258
+ assert foo != none
259
+ assert none == None
260
+ assert none == NoneToken()
261
+ assert none.func(*none.args) == none
262
+
263
+
264
+ def test_String():
265
+ st = String('foobar')
266
+ assert st.is_Atom
267
+ assert st == String('foobar')
268
+ assert st.text == 'foobar'
269
+ assert st.func(**st.kwargs()) == st
270
+ assert st.func(*st.args) == st
271
+
272
+
273
+ class Signifier(String):
274
+ pass
275
+
276
+ si = Signifier('foobar')
277
+ assert si != st
278
+ assert si.text == st.text
279
+ s = String('foo')
280
+ assert str(s) == 'foo'
281
+ assert repr(s) == "String('foo')"
282
+
283
+ def test_Comment():
284
+ c = Comment('foobar')
285
+ assert c.text == 'foobar'
286
+ assert str(c) == 'foobar'
287
+
288
+ def test_Node():
289
+ n = Node()
290
+ assert n == Node()
291
+ assert n.func(*n.args) == n
292
+
293
+
294
+ def test_Type():
295
+ t = Type('MyType')
296
+ assert len(t.args) == 1
297
+ assert t.name == String('MyType')
298
+ assert str(t) == 'MyType'
299
+ assert repr(t) == "Type(String('MyType'))"
300
+ assert Type(t) == t
301
+ assert t.func(*t.args) == t
302
+ t1 = Type('t1')
303
+ t2 = Type('t2')
304
+ assert t1 != t2
305
+ assert t1 == t1 and t2 == t2
306
+ t1b = Type('t1')
307
+ assert t1 == t1b
308
+ assert t2 != t1b
309
+
310
+
311
+ def test_Type__from_expr():
312
+ assert Type.from_expr(i) == integer
313
+ u = symbols('u', real=True)
314
+ assert Type.from_expr(u) == real
315
+ assert Type.from_expr(n) == integer
316
+ assert Type.from_expr(3) == integer
317
+ assert Type.from_expr(3.0) == real
318
+ assert Type.from_expr(3+1j) == complex_
319
+ raises(ValueError, lambda: Type.from_expr(sum))
320
+
321
+
322
+ def test_Type__cast_check__integers():
323
+ # Rounding
324
+ raises(ValueError, lambda: integer.cast_check(3.5))
325
+ assert integer.cast_check('3') == 3
326
+ assert integer.cast_check(Float('3.0000000000000000000')) == 3
327
+ assert integer.cast_check(Float('3.0000000000000000001')) == 3 # unintuitive maybe?
328
+
329
+ # Range
330
+ assert int8.cast_check(127.0) == 127
331
+ raises(ValueError, lambda: int8.cast_check(128))
332
+ assert int8.cast_check(-128) == -128
333
+ raises(ValueError, lambda: int8.cast_check(-129))
334
+
335
+ assert uint8.cast_check(0) == 0
336
+ assert uint8.cast_check(128) == 128
337
+ raises(ValueError, lambda: uint8.cast_check(256.0))
338
+ raises(ValueError, lambda: uint8.cast_check(-1))
339
+
340
+ def test_Attribute():
341
+ noexcept = Attribute('noexcept')
342
+ assert noexcept == Attribute('noexcept')
343
+ alignas16 = Attribute('alignas', [16])
344
+ alignas32 = Attribute('alignas', [32])
345
+ assert alignas16 != alignas32
346
+ assert alignas16.func(*alignas16.args) == alignas16
347
+
348
+
349
+ def test_Variable():
350
+ v = Variable(x, type=real)
351
+ assert v == Variable(v)
352
+ assert v == Variable('x', type=real)
353
+ assert v.symbol == x
354
+ assert v.type == real
355
+ assert value_const not in v.attrs
356
+ assert v.func(*v.args) == v
357
+ assert str(v) == 'Variable(x, type=real)'
358
+
359
+ w = Variable(y, f32, attrs={value_const})
360
+ assert w.symbol == y
361
+ assert w.type == f32
362
+ assert value_const in w.attrs
363
+ assert w.func(*w.args) == w
364
+
365
+ v_n = Variable(n, type=Type.from_expr(n))
366
+ assert v_n.type == integer
367
+ assert v_n.func(*v_n.args) == v_n
368
+ v_i = Variable(i, type=Type.from_expr(n))
369
+ assert v_i.type == integer
370
+ assert v_i != v_n
371
+
372
+ a_i = Variable.deduced(i)
373
+ assert a_i.type == integer
374
+ assert Variable.deduced(Symbol('x', real=True)).type == real
375
+ assert a_i.func(*a_i.args) == a_i
376
+
377
+ v_n2 = Variable.deduced(n, value=3.5, cast_check=False)
378
+ assert v_n2.func(*v_n2.args) == v_n2
379
+ assert abs(v_n2.value - 3.5) < 1e-15
380
+ raises(ValueError, lambda: Variable.deduced(n, value=3.5, cast_check=True))
381
+
382
+ v_n3 = Variable.deduced(n)
383
+ assert v_n3.type == integer
384
+ assert str(v_n3) == 'Variable(n, type=integer)'
385
+ assert Variable.deduced(z, value=3).type == integer
386
+ assert Variable.deduced(z, value=3.0).type == real
387
+ assert Variable.deduced(z, value=3.0+1j).type == complex_
388
+
389
+
390
+ def test_Pointer():
391
+ p = Pointer(x)
392
+ assert p.symbol == x
393
+ assert p.type == untyped
394
+ assert value_const not in p.attrs
395
+ assert pointer_const not in p.attrs
396
+ assert p.func(*p.args) == p
397
+
398
+ u = symbols('u', real=True)
399
+ pu = Pointer(u, type=Type.from_expr(u), attrs={value_const, pointer_const})
400
+ assert pu.symbol is u
401
+ assert pu.type == real
402
+ assert value_const in pu.attrs
403
+ assert pointer_const in pu.attrs
404
+ assert pu.func(*pu.args) == pu
405
+
406
+ i = symbols('i', integer=True)
407
+ deref = pu[i]
408
+ assert deref.indices == (i,)
409
+
410
+
411
+ def test_Declaration():
412
+ u = symbols('u', real=True)
413
+ vu = Variable(u, type=Type.from_expr(u))
414
+ assert Declaration(vu).variable.type == real
415
+ vn = Variable(n, type=Type.from_expr(n))
416
+ assert Declaration(vn).variable.type == integer
417
+
418
+ # PR 19107, does not allow comparison between expressions and Basic
419
+ # lt = StrictLessThan(vu, vn)
420
+ # assert isinstance(lt, StrictLessThan)
421
+
422
+ vuc = Variable(u, Type.from_expr(u), value=3.0, attrs={value_const})
423
+ assert value_const in vuc.attrs
424
+ assert pointer_const not in vuc.attrs
425
+ decl = Declaration(vuc)
426
+ assert decl.variable == vuc
427
+ assert isinstance(decl.variable.value, Float)
428
+ assert decl.variable.value == 3.0
429
+ assert decl.func(*decl.args) == decl
430
+ assert vuc.as_Declaration() == decl
431
+ assert vuc.as_Declaration(value=None, attrs=None) == Declaration(vu)
432
+
433
+ vy = Variable(y, type=integer, value=3)
434
+ decl2 = Declaration(vy)
435
+ assert decl2.variable == vy
436
+ assert decl2.variable.value == Integer(3)
437
+
438
+ vi = Variable(i, type=Type.from_expr(i), value=3.0)
439
+ decl3 = Declaration(vi)
440
+ assert decl3.variable.type == integer
441
+ assert decl3.variable.value == 3.0
442
+
443
+ raises(ValueError, lambda: Declaration(vi, 42))
444
+
445
+
446
+ def test_IntBaseType():
447
+ assert intc.name == String('intc')
448
+ assert intc.args == (intc.name,)
449
+ assert str(IntBaseType('a').name) == 'a'
450
+
451
+
452
+ def test_FloatType():
453
+ assert f16.dig == 3
454
+ assert f32.dig == 6
455
+ assert f64.dig == 15
456
+ assert f80.dig == 18
457
+ assert f128.dig == 33
458
+
459
+ assert f16.decimal_dig == 5
460
+ assert f32.decimal_dig == 9
461
+ assert f64.decimal_dig == 17
462
+ assert f80.decimal_dig == 21
463
+ assert f128.decimal_dig == 36
464
+
465
+ assert f16.max_exponent == 16
466
+ assert f32.max_exponent == 128
467
+ assert f64.max_exponent == 1024
468
+ assert f80.max_exponent == 16384
469
+ assert f128.max_exponent == 16384
470
+
471
+ assert f16.min_exponent == -13
472
+ assert f32.min_exponent == -125
473
+ assert f64.min_exponent == -1021
474
+ assert f80.min_exponent == -16381
475
+ assert f128.min_exponent == -16381
476
+
477
+ assert abs(f16.eps / Float('0.00097656', precision=16) - 1) < 0.1*10**-f16.dig
478
+ assert abs(f32.eps / Float('1.1920929e-07', precision=32) - 1) < 0.1*10**-f32.dig
479
+ assert abs(f64.eps / Float('2.2204460492503131e-16', precision=64) - 1) < 0.1*10**-f64.dig
480
+ assert abs(f80.eps / Float('1.08420217248550443401e-19', precision=80) - 1) < 0.1*10**-f80.dig
481
+ assert abs(f128.eps / Float(' 1.92592994438723585305597794258492732e-34', precision=128) - 1) < 0.1*10**-f128.dig
482
+
483
+ assert abs(f16.max / Float('65504', precision=16) - 1) < .1*10**-f16.dig
484
+ assert abs(f32.max / Float('3.40282347e+38', precision=32) - 1) < 0.1*10**-f32.dig
485
+ assert abs(f64.max / Float('1.79769313486231571e+308', precision=64) - 1) < 0.1*10**-f64.dig # cf. np.finfo(np.float64).max
486
+ assert abs(f80.max / Float('1.18973149535723176502e+4932', precision=80) - 1) < 0.1*10**-f80.dig
487
+ assert abs(f128.max / Float('1.18973149535723176508575932662800702e+4932', precision=128) - 1) < 0.1*10**-f128.dig
488
+
489
+ # cf. np.finfo(np.float32).tiny
490
+ assert abs(f16.tiny / Float('6.1035e-05', precision=16) - 1) < 0.1*10**-f16.dig
491
+ assert abs(f32.tiny / Float('1.17549435e-38', precision=32) - 1) < 0.1*10**-f32.dig
492
+ assert abs(f64.tiny / Float('2.22507385850720138e-308', precision=64) - 1) < 0.1*10**-f64.dig
493
+ assert abs(f80.tiny / Float('3.36210314311209350626e-4932', precision=80) - 1) < 0.1*10**-f80.dig
494
+ assert abs(f128.tiny / Float('3.3621031431120935062626778173217526e-4932', precision=128) - 1) < 0.1*10**-f128.dig
495
+
496
+ assert f64.cast_check(0.5) == Float(0.5, 17)
497
+ assert abs(f64.cast_check(3.7) - 3.7) < 3e-17
498
+ assert isinstance(f64.cast_check(3), (Float, float))
499
+
500
+ assert f64.cast_nocheck(oo) == float('inf')
501
+ assert f64.cast_nocheck(-oo) == float('-inf')
502
+ assert f64.cast_nocheck(float(oo)) == float('inf')
503
+ assert f64.cast_nocheck(float(-oo)) == float('-inf')
504
+ assert math.isnan(f64.cast_nocheck(nan))
505
+
506
+ assert f32 != f64
507
+ assert f64 == f64.func(*f64.args)
508
+
509
+
510
+ def test_Type__cast_check__floating_point():
511
+ raises(ValueError, lambda: f32.cast_check(123.45678949))
512
+ raises(ValueError, lambda: f32.cast_check(12.345678949))
513
+ raises(ValueError, lambda: f32.cast_check(1.2345678949))
514
+ raises(ValueError, lambda: f32.cast_check(.12345678949))
515
+ assert abs(123.456789049 - f32.cast_check(123.456789049) - 4.9e-8) < 1e-8
516
+ assert abs(0.12345678904 - f32.cast_check(0.12345678904) - 4e-11) < 1e-11
517
+
518
+ dcm21 = Float('0.123456789012345670499') # 21 decimals
519
+ assert abs(dcm21 - f64.cast_check(dcm21) - 4.99e-19) < 1e-19
520
+
521
+ f80.cast_check(Float('0.12345678901234567890103', precision=88))
522
+ raises(ValueError, lambda: f80.cast_check(Float('0.12345678901234567890149', precision=88)))
523
+
524
+ v10 = 12345.67894
525
+ raises(ValueError, lambda: f32.cast_check(v10))
526
+ assert abs(Float(str(v10), precision=64+8) - f64.cast_check(v10)) < v10*1e-16
527
+
528
+ assert abs(f32.cast_check(2147483647) - 2147483650) < 1
529
+
530
+
531
+ def test_Type__cast_check__complex_floating_point():
532
+ val9_11 = 123.456789049 + 0.123456789049j
533
+ raises(ValueError, lambda: c64.cast_check(.12345678949 + .12345678949j))
534
+ assert abs(val9_11 - c64.cast_check(val9_11) - 4.9e-8) < 1e-8
535
+
536
+ dcm21 = Float('0.123456789012345670499') + 1e-20j # 21 decimals
537
+ assert abs(dcm21 - c128.cast_check(dcm21) - 4.99e-19) < 1e-19
538
+ v19 = Float('0.1234567890123456749') + 1j*Float('0.1234567890123456749')
539
+ raises(ValueError, lambda: c128.cast_check(v19))
540
+
541
+
542
+ def test_While():
543
+ xpp = AddAugmentedAssignment(x, 1)
544
+ whl1 = While(x < 2, [xpp])
545
+ assert whl1.condition.args[0] == x
546
+ assert whl1.condition.args[1] == 2
547
+ assert whl1.condition == Lt(x, 2, evaluate=False)
548
+ assert whl1.body.args == (xpp,)
549
+ assert whl1.func(*whl1.args) == whl1
550
+
551
+ cblk = CodeBlock(AddAugmentedAssignment(x, 1))
552
+ whl2 = While(x < 2, cblk)
553
+ assert whl1 == whl2
554
+ assert whl1 != While(x < 3, [xpp])
555
+
556
+
557
+ def test_Scope():
558
+ assign = Assignment(x, y)
559
+ incr = AddAugmentedAssignment(x, 1)
560
+ scp = Scope([assign, incr])
561
+ cblk = CodeBlock(assign, incr)
562
+ assert scp.body == cblk
563
+ assert scp == Scope(cblk)
564
+ assert scp != Scope([incr, assign])
565
+ assert scp.func(*scp.args) == scp
566
+
567
+
568
+ def test_Print():
569
+ fmt = "%d %.3f"
570
+ ps = Print([n, x], fmt)
571
+ assert str(ps.format_string) == fmt
572
+ assert ps.print_args == Tuple(n, x)
573
+ assert ps.args == (Tuple(n, x), QuotedString(fmt), none)
574
+ assert ps == Print((n, x), fmt)
575
+ assert ps != Print([x, n], fmt)
576
+ assert ps.func(*ps.args) == ps
577
+
578
+ ps2 = Print([n, x])
579
+ assert ps2 == Print([n, x])
580
+ assert ps2 != ps
581
+ assert ps2.format_string == None
582
+
583
+
584
+ def test_FunctionPrototype_and_FunctionDefinition():
585
+ vx = Variable(x, type=real)
586
+ vn = Variable(n, type=integer)
587
+ fp1 = FunctionPrototype(real, 'power', [vx, vn])
588
+ assert fp1.return_type == real
589
+ assert fp1.name == String('power')
590
+ assert fp1.parameters == Tuple(vx, vn)
591
+ assert fp1 == FunctionPrototype(real, 'power', [vx, vn])
592
+ assert fp1 != FunctionPrototype(real, 'power', [vn, vx])
593
+ assert fp1.func(*fp1.args) == fp1
594
+
595
+
596
+ body = [Assignment(x, x**n), Return(x)]
597
+ fd1 = FunctionDefinition(real, 'power', [vx, vn], body)
598
+ assert fd1.return_type == real
599
+ assert str(fd1.name) == 'power'
600
+ assert fd1.parameters == Tuple(vx, vn)
601
+ assert fd1.body == CodeBlock(*body)
602
+ assert fd1 == FunctionDefinition(real, 'power', [vx, vn], body)
603
+ assert fd1 != FunctionDefinition(real, 'power', [vx, vn], body[::-1])
604
+ assert fd1.func(*fd1.args) == fd1
605
+
606
+ fp2 = FunctionPrototype.from_FunctionDefinition(fd1)
607
+ assert fp2 == fp1
608
+
609
+ fd2 = FunctionDefinition.from_FunctionPrototype(fp1, body)
610
+ assert fd2 == fd1
611
+
612
+
613
+ def test_Return():
614
+ rs = Return(x)
615
+ assert rs.args == (x,)
616
+ assert rs == Return(x)
617
+ assert rs != Return(y)
618
+ assert rs.func(*rs.args) == rs
619
+
620
+
621
+ def test_FunctionCall():
622
+ fc = FunctionCall('power', (x, 3))
623
+ assert fc.function_args[0] == x
624
+ assert fc.function_args[1] == 3
625
+ assert len(fc.function_args) == 2
626
+ assert isinstance(fc.function_args[1], Integer)
627
+ assert fc == FunctionCall('power', (x, 3))
628
+ assert fc != FunctionCall('power', (3, x))
629
+ assert fc != FunctionCall('Power', (x, 3))
630
+ assert fc.func(*fc.args) == fc
631
+
632
+ fc2 = FunctionCall('fma', [2, 3, 4])
633
+ assert len(fc2.function_args) == 3
634
+ assert fc2.function_args[0] == 2
635
+ assert fc2.function_args[1] == 3
636
+ assert fc2.function_args[2] == 4
637
+ assert str(fc2) in ( # not sure if QuotedString is a better default...
638
+ 'FunctionCall(fma, function_args=(2, 3, 4))',
639
+ 'FunctionCall("fma", function_args=(2, 3, 4))',
640
+ )
641
+
642
+ def test_ast_replace():
643
+ x = Variable('x', real)
644
+ y = Variable('y', real)
645
+ n = Variable('n', integer)
646
+
647
+ pwer = FunctionDefinition(real, 'pwer', [x, n], [pow(x.symbol, n.symbol)])
648
+ pname = pwer.name
649
+ pcall = FunctionCall('pwer', [y, 3])
650
+
651
+ tree1 = CodeBlock(pwer, pcall)
652
+ assert str(tree1.args[0].name) == 'pwer'
653
+ assert str(tree1.args[1].name) == 'pwer'
654
+ for a, b in zip(tree1, [pwer, pcall]):
655
+ assert a == b
656
+
657
+ tree2 = tree1.replace(pname, String('power'))
658
+ assert str(tree1.args[0].name) == 'pwer'
659
+ assert str(tree1.args[1].name) == 'pwer'
660
+ assert str(tree2.args[0].name) == 'power'
661
+ assert str(tree2.args[1].name) == 'power'
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/test_cfunctions.py ADDED
@@ -0,0 +1,186 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core.numbers import (Rational, pi)
2
+ from sympy.core.singleton import S
3
+ from sympy.core.symbol import (Symbol, symbols)
4
+ from sympy.functions.elementary.exponential import (exp, log)
5
+ from sympy.codegen.cfunctions import (
6
+ expm1, log1p, exp2, log2, fma, log10, Sqrt, Cbrt, hypot, isnan, isinf
7
+ )
8
+ from sympy.core.function import expand_log
9
+
10
+
11
+ def test_expm1():
12
+ # Eval
13
+ assert expm1(0) == 0
14
+
15
+ x = Symbol('x', real=True)
16
+
17
+ # Expand and rewrite
18
+ assert expm1(x).expand(func=True) - exp(x) == -1
19
+ assert expm1(x).rewrite('tractable') - exp(x) == -1
20
+ assert expm1(x).rewrite('exp') - exp(x) == -1
21
+
22
+ # Precision
23
+ assert not ((exp(1e-10).evalf() - 1) - 1e-10 - 5e-21) < 1e-22 # for comparison
24
+ assert abs(expm1(1e-10).evalf() - 1e-10 - 5e-21) < 1e-22
25
+
26
+ # Properties
27
+ assert expm1(x).is_real
28
+ assert expm1(x).is_finite
29
+
30
+ # Diff
31
+ assert expm1(42*x).diff(x) - 42*exp(42*x) == 0
32
+ assert expm1(42*x).diff(x) - expm1(42*x).expand(func=True).diff(x) == 0
33
+
34
+
35
+ def test_log1p():
36
+ # Eval
37
+ assert log1p(0) == 0
38
+ d = S(10)
39
+ assert expand_log(log1p(d**-1000) - log(d**1000 + 1) + log(d**1000)) == 0
40
+
41
+ x = Symbol('x', real=True)
42
+
43
+ # Expand and rewrite
44
+ assert log1p(x).expand(func=True) - log(x + 1) == 0
45
+ assert log1p(x).rewrite('tractable') - log(x + 1) == 0
46
+ assert log1p(x).rewrite('log') - log(x + 1) == 0
47
+
48
+ # Precision
49
+ assert not abs(log(1e-99 + 1).evalf() - 1e-99) < 1e-100 # for comparison
50
+ assert abs(expand_log(log1p(1e-99)).evalf() - 1e-99) < 1e-100
51
+
52
+ # Properties
53
+ assert log1p(-2**Rational(-1, 2)).is_real
54
+
55
+ assert not log1p(-1).is_finite
56
+ assert log1p(pi).is_finite
57
+
58
+ assert not log1p(x).is_positive
59
+ assert log1p(Symbol('y', positive=True)).is_positive
60
+
61
+ assert not log1p(x).is_zero
62
+ assert log1p(Symbol('z', zero=True)).is_zero
63
+
64
+ assert not log1p(x).is_nonnegative
65
+ assert log1p(Symbol('o', nonnegative=True)).is_nonnegative
66
+
67
+ # Diff
68
+ assert log1p(42*x).diff(x) - 42/(42*x + 1) == 0
69
+ assert log1p(42*x).diff(x) - log1p(42*x).expand(func=True).diff(x) == 0
70
+
71
+
72
+ def test_exp2():
73
+ # Eval
74
+ assert exp2(2) == 4
75
+
76
+ x = Symbol('x', real=True)
77
+
78
+ # Expand
79
+ assert exp2(x).expand(func=True) - 2**x == 0
80
+
81
+ # Diff
82
+ assert exp2(42*x).diff(x) - 42*exp2(42*x)*log(2) == 0
83
+ assert exp2(42*x).diff(x) - exp2(42*x).diff(x) == 0
84
+
85
+
86
+ def test_log2():
87
+ # Eval
88
+ assert log2(8) == 3
89
+ assert log2(pi) != log(pi)/log(2) # log2 should *save* (CPU) instructions
90
+
91
+ x = Symbol('x', real=True)
92
+ assert log2(x) != log(x)/log(2)
93
+ assert log2(2**x) == x
94
+
95
+ # Expand
96
+ assert log2(x).expand(func=True) - log(x)/log(2) == 0
97
+
98
+ # Diff
99
+ assert log2(42*x).diff() - 1/(log(2)*x) == 0
100
+ assert log2(42*x).diff() - log2(42*x).expand(func=True).diff(x) == 0
101
+
102
+
103
+ def test_fma():
104
+ x, y, z = symbols('x y z')
105
+
106
+ # Expand
107
+ assert fma(x, y, z).expand(func=True) - x*y - z == 0
108
+
109
+ expr = fma(17*x, 42*y, 101*z)
110
+
111
+ # Diff
112
+ assert expr.diff(x) - expr.expand(func=True).diff(x) == 0
113
+ assert expr.diff(y) - expr.expand(func=True).diff(y) == 0
114
+ assert expr.diff(z) - expr.expand(func=True).diff(z) == 0
115
+
116
+ assert expr.diff(x) - 17*42*y == 0
117
+ assert expr.diff(y) - 17*42*x == 0
118
+ assert expr.diff(z) - 101 == 0
119
+
120
+
121
+ def test_log10():
122
+ x = Symbol('x')
123
+
124
+ # Expand
125
+ assert log10(x).expand(func=True) - log(x)/log(10) == 0
126
+
127
+ # Diff
128
+ assert log10(42*x).diff(x) - 1/(log(10)*x) == 0
129
+ assert log10(42*x).diff(x) - log10(42*x).expand(func=True).diff(x) == 0
130
+
131
+
132
+ def test_Cbrt():
133
+ x = Symbol('x')
134
+
135
+ # Expand
136
+ assert Cbrt(x).expand(func=True) - x**Rational(1, 3) == 0
137
+
138
+ # Diff
139
+ assert Cbrt(42*x).diff(x) - 42*(42*x)**(Rational(1, 3) - 1)/3 == 0
140
+ assert Cbrt(42*x).diff(x) - Cbrt(42*x).expand(func=True).diff(x) == 0
141
+
142
+
143
+ def test_Sqrt():
144
+ x = Symbol('x')
145
+
146
+ # Expand
147
+ assert Sqrt(x).expand(func=True) - x**S.Half == 0
148
+
149
+ # Diff
150
+ assert Sqrt(42*x).diff(x) - 42*(42*x)**(S.Half - 1)/2 == 0
151
+ assert Sqrt(42*x).diff(x) - Sqrt(42*x).expand(func=True).diff(x) == 0
152
+
153
+
154
+ def test_hypot():
155
+ x, y = symbols('x y')
156
+
157
+ # Expand
158
+ assert hypot(x, y).expand(func=True) - (x**2 + y**2)**S.Half == 0
159
+
160
+ # Diff
161
+ assert hypot(17*x, 42*y).diff(x).expand(func=True) - hypot(17*x, 42*y).expand(func=True).diff(x) == 0
162
+ assert hypot(17*x, 42*y).diff(y).expand(func=True) - hypot(17*x, 42*y).expand(func=True).diff(y) == 0
163
+
164
+ assert hypot(17*x, 42*y).diff(x).expand(func=True) - 2*17*17*x*((17*x)**2 + (42*y)**2)**Rational(-1, 2)/2 == 0
165
+ assert hypot(17*x, 42*y).diff(y).expand(func=True) - 2*42*42*y*((17*x)**2 + (42*y)**2)**Rational(-1, 2)/2 == 0
166
+
167
+
168
+ def test_isnan_isinf():
169
+ x = Symbol('x')
170
+
171
+ # isinf
172
+ assert isinf(+S.Infinity) == True
173
+ assert isinf(-S.Infinity) == True
174
+ assert isinf(S.Pi) == False
175
+ isinfx = isinf(x)
176
+ assert isinfx not in (False, True)
177
+ assert isinfx.func is isinf
178
+ assert isinfx.args == (x,)
179
+
180
+ # isnan
181
+ assert isnan(S.NaN) == True
182
+ assert isnan(S.Pi) == False
183
+ isnanx = isnan(x)
184
+ assert isnanx not in (False, True)
185
+ assert isnanx.func is isnan
186
+ assert isnanx.args == (x,)
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/test_cnodes.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core.symbol import symbols
2
+ from sympy.printing.codeprinter import ccode
3
+ from sympy.codegen.ast import Declaration, Variable, float64, int64, String, CodeBlock
4
+ from sympy.codegen.cnodes import (
5
+ alignof, CommaOperator, goto, Label, PreDecrement, PostDecrement, PreIncrement, PostIncrement,
6
+ sizeof, union, struct
7
+ )
8
+
9
+ x, y = symbols('x y')
10
+
11
+
12
+ def test_alignof():
13
+ ax = alignof(x)
14
+ assert ccode(ax) == 'alignof(x)'
15
+ assert ax.func(*ax.args) == ax
16
+
17
+
18
+ def test_CommaOperator():
19
+ expr = CommaOperator(PreIncrement(x), 2*x)
20
+ assert ccode(expr) == '(++(x), 2*x)'
21
+ assert expr.func(*expr.args) == expr
22
+
23
+
24
+ def test_goto_Label():
25
+ s = 'early_exit'
26
+ g = goto(s)
27
+ assert g.func(*g.args) == g
28
+ assert g != goto('foobar')
29
+ assert ccode(g) == 'goto early_exit'
30
+
31
+ l1 = Label(s)
32
+ assert ccode(l1) == 'early_exit:'
33
+ assert l1 == Label('early_exit')
34
+ assert l1 != Label('foobar')
35
+
36
+ body = [PreIncrement(x)]
37
+ l2 = Label(s, body)
38
+ assert l2.name == String("early_exit")
39
+ assert l2.body == CodeBlock(PreIncrement(x))
40
+ assert ccode(l2) == ("early_exit:\n"
41
+ "++(x);")
42
+
43
+ body = [PreIncrement(x), PreDecrement(y)]
44
+ l2 = Label(s, body)
45
+ assert l2.name == String("early_exit")
46
+ assert l2.body == CodeBlock(PreIncrement(x), PreDecrement(y))
47
+ assert ccode(l2) == ("early_exit:\n"
48
+ "{\n ++(x);\n --(y);\n}")
49
+
50
+
51
+ def test_PreDecrement():
52
+ p = PreDecrement(x)
53
+ assert p.func(*p.args) == p
54
+ assert ccode(p) == '--(x)'
55
+
56
+
57
+ def test_PostDecrement():
58
+ p = PostDecrement(x)
59
+ assert p.func(*p.args) == p
60
+ assert ccode(p) == '(x)--'
61
+
62
+
63
+ def test_PreIncrement():
64
+ p = PreIncrement(x)
65
+ assert p.func(*p.args) == p
66
+ assert ccode(p) == '++(x)'
67
+
68
+
69
+ def test_PostIncrement():
70
+ p = PostIncrement(x)
71
+ assert p.func(*p.args) == p
72
+ assert ccode(p) == '(x)++'
73
+
74
+
75
+ def test_sizeof():
76
+ typename = 'unsigned int'
77
+ sz = sizeof(typename)
78
+ assert ccode(sz) == 'sizeof(%s)' % typename
79
+ assert sz.func(*sz.args) == sz
80
+ assert not sz.is_Atom
81
+ assert sz.atoms() == {String('unsigned int'), String('sizeof')}
82
+
83
+
84
+ def test_struct():
85
+ vx, vy = Variable(x, type=float64), Variable(y, type=float64)
86
+ s = struct('vec2', [vx, vy])
87
+ assert s.func(*s.args) == s
88
+ assert s == struct('vec2', (vx, vy))
89
+ assert s != struct('vec2', (vy, vx))
90
+ assert str(s.name) == 'vec2'
91
+ assert len(s.declarations) == 2
92
+ assert all(isinstance(arg, Declaration) for arg in s.declarations)
93
+ assert ccode(s) == (
94
+ "struct vec2 {\n"
95
+ " double x;\n"
96
+ " double y;\n"
97
+ "}")
98
+
99
+
100
+ def test_union():
101
+ vx, vy = Variable(x, type=float64), Variable(y, type=int64)
102
+ u = union('dualuse', [vx, vy])
103
+ assert u.func(*u.args) == u
104
+ assert u == union('dualuse', (vx, vy))
105
+ assert str(u.name) == 'dualuse'
106
+ assert len(u.declarations) == 2
107
+ assert all(isinstance(arg, Declaration) for arg in u.declarations)
108
+ assert ccode(u) == (
109
+ "union dualuse {\n"
110
+ " double x;\n"
111
+ " int64_t y;\n"
112
+ "}")
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/test_cxxnodes.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core.symbol import Symbol
2
+ from sympy.codegen.ast import Type
3
+ from sympy.codegen.cxxnodes import using
4
+ from sympy.printing.codeprinter import cxxcode
5
+
6
+ x = Symbol('x')
7
+
8
+ def test_using():
9
+ v = Type('std::vector')
10
+ u1 = using(v)
11
+ assert cxxcode(u1) == 'using std::vector'
12
+
13
+ u2 = using(v, 'vec')
14
+ assert cxxcode(u2) == 'using vec = std::vector'
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/test_fnodes.py ADDED
@@ -0,0 +1,213 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import tempfile
3
+ from sympy.core.symbol import (Symbol, symbols)
4
+ from sympy.codegen.ast import (
5
+ Assignment, Print, Declaration, FunctionDefinition, Return, real,
6
+ FunctionCall, Variable, Element, integer
7
+ )
8
+ from sympy.codegen.fnodes import (
9
+ allocatable, ArrayConstructor, isign, dsign, cmplx, kind, literal_dp,
10
+ Program, Module, use, Subroutine, dimension, assumed_extent, ImpliedDoLoop,
11
+ intent_out, size, Do, SubroutineCall, sum_, array, bind_C
12
+ )
13
+ from sympy.codegen.futils import render_as_module
14
+ from sympy.core.expr import unchanged
15
+ from sympy.external import import_module
16
+ from sympy.printing.codeprinter import fcode
17
+ from sympy.utilities._compilation import has_fortran, compile_run_strings, compile_link_import_strings
18
+ from sympy.utilities._compilation.util import may_xfail
19
+ from sympy.testing.pytest import skip, XFAIL
20
+
21
+ cython = import_module('cython')
22
+ np = import_module('numpy')
23
+
24
+
25
+ def test_size():
26
+ x = Symbol('x', real=True)
27
+ sx = size(x)
28
+ assert fcode(sx, source_format='free') == 'size(x)'
29
+
30
+
31
+ @may_xfail
32
+ def test_size_assumed_shape():
33
+ if not has_fortran():
34
+ skip("No fortran compiler found.")
35
+ a = Symbol('a', real=True)
36
+ body = [Return((sum_(a**2)/size(a))**.5)]
37
+ arr = array(a, dim=[':'], intent='in')
38
+ fd = FunctionDefinition(real, 'rms', [arr], body)
39
+ render_as_module([fd], 'mod_rms')
40
+
41
+ (stdout, stderr), info = compile_run_strings([
42
+ ('rms.f90', render_as_module([fd], 'mod_rms')),
43
+ ('main.f90', (
44
+ 'program myprog\n'
45
+ 'use mod_rms, only: rms\n'
46
+ 'real*8, dimension(4), parameter :: x = [4, 2, 2, 2]\n'
47
+ 'print "(f7.5)", dsqrt(7d0) - rms(x)\n'
48
+ 'end program\n'
49
+ ))
50
+ ], clean=True)
51
+ assert '0.00000' in stdout
52
+ assert stderr == ''
53
+ assert info['exit_status'] == os.EX_OK
54
+
55
+
56
+ @XFAIL # https://github.com/sympy/sympy/issues/20265
57
+ @may_xfail
58
+ def test_ImpliedDoLoop():
59
+ if not has_fortran():
60
+ skip("No fortran compiler found.")
61
+
62
+ a, i = symbols('a i', integer=True)
63
+ idl = ImpliedDoLoop(i**3, i, -3, 3, 2)
64
+ ac = ArrayConstructor([-28, idl, 28])
65
+ a = array(a, dim=[':'], attrs=[allocatable])
66
+ prog = Program('idlprog', [
67
+ a.as_Declaration(),
68
+ Assignment(a, ac),
69
+ Print([a])
70
+ ])
71
+ fsrc = fcode(prog, standard=2003, source_format='free')
72
+ (stdout, stderr), info = compile_run_strings([('main.f90', fsrc)], clean=True)
73
+ for numstr in '-28 -27 -1 1 27 28'.split():
74
+ assert numstr in stdout
75
+ assert stderr == ''
76
+ assert info['exit_status'] == os.EX_OK
77
+
78
+
79
+ @may_xfail
80
+ def test_Program():
81
+ x = Symbol('x', real=True)
82
+ vx = Variable.deduced(x, 42)
83
+ decl = Declaration(vx)
84
+ prnt = Print([x, x+1])
85
+ prog = Program('foo', [decl, prnt])
86
+ if not has_fortran():
87
+ skip("No fortran compiler found.")
88
+
89
+ (stdout, stderr), info = compile_run_strings([('main.f90', fcode(prog, standard=90))], clean=True)
90
+ assert '42' in stdout
91
+ assert '43' in stdout
92
+ assert stderr == ''
93
+ assert info['exit_status'] == os.EX_OK
94
+
95
+
96
+ @may_xfail
97
+ def test_Module():
98
+ x = Symbol('x', real=True)
99
+ v_x = Variable.deduced(x)
100
+ sq = FunctionDefinition(real, 'sqr', [v_x], [Return(x**2)])
101
+ mod_sq = Module('mod_sq', [], [sq])
102
+ sq_call = FunctionCall('sqr', [42.])
103
+ prg_sq = Program('foobar', [
104
+ use('mod_sq', only=['sqr']),
105
+ Print(['"Square of 42 = "', sq_call])
106
+ ])
107
+ if not has_fortran():
108
+ skip("No fortran compiler found.")
109
+ (stdout, stderr), info = compile_run_strings([
110
+ ('mod_sq.f90', fcode(mod_sq, standard=90)),
111
+ ('main.f90', fcode(prg_sq, standard=90))
112
+ ], clean=True)
113
+ assert '42' in stdout
114
+ assert str(42**2) in stdout
115
+ assert stderr == ''
116
+
117
+
118
+ @XFAIL # https://github.com/sympy/sympy/issues/20265
119
+ @may_xfail
120
+ def test_Subroutine():
121
+ # Code to generate the subroutine in the example from
122
+ # http://www.fortran90.org/src/best-practices.html#arrays
123
+ r = Symbol('r', real=True)
124
+ i = Symbol('i', integer=True)
125
+ v_r = Variable.deduced(r, attrs=(dimension(assumed_extent), intent_out))
126
+ v_i = Variable.deduced(i)
127
+ v_n = Variable('n', integer)
128
+ do_loop = Do([
129
+ Assignment(Element(r, [i]), literal_dp(1)/i**2)
130
+ ], i, 1, v_n)
131
+ sub = Subroutine("f", [v_r], [
132
+ Declaration(v_n),
133
+ Declaration(v_i),
134
+ Assignment(v_n, size(r)),
135
+ do_loop
136
+ ])
137
+ x = Symbol('x', real=True)
138
+ v_x3 = Variable.deduced(x, attrs=[dimension(3)])
139
+ mod = Module('mymod', definitions=[sub])
140
+ prog = Program('foo', [
141
+ use(mod, only=[sub]),
142
+ Declaration(v_x3),
143
+ SubroutineCall(sub, [v_x3]),
144
+ Print([sum_(v_x3), v_x3])
145
+ ])
146
+
147
+ if not has_fortran():
148
+ skip("No fortran compiler found.")
149
+
150
+ (stdout, stderr), info = compile_run_strings([
151
+ ('a.f90', fcode(mod, standard=90)),
152
+ ('b.f90', fcode(prog, standard=90))
153
+ ], clean=True)
154
+ ref = [1.0/i**2 for i in range(1, 4)]
155
+ assert str(sum(ref))[:-3] in stdout
156
+ for _ in ref:
157
+ assert str(_)[:-3] in stdout
158
+ assert stderr == ''
159
+
160
+
161
+ def test_isign():
162
+ x = Symbol('x', integer=True)
163
+ assert unchanged(isign, 1, x)
164
+ assert fcode(isign(1, x), standard=95, source_format='free') == 'isign(1, x)'
165
+
166
+
167
+ def test_dsign():
168
+ x = Symbol('x')
169
+ assert unchanged(dsign, 1, x)
170
+ assert fcode(dsign(literal_dp(1), x), standard=95, source_format='free') == 'dsign(1d0, x)'
171
+
172
+
173
+ def test_cmplx():
174
+ x = Symbol('x')
175
+ assert unchanged(cmplx, 1, x)
176
+
177
+
178
+ def test_kind():
179
+ x = Symbol('x')
180
+ assert unchanged(kind, x)
181
+
182
+
183
+ def test_literal_dp():
184
+ assert fcode(literal_dp(0), source_format='free') == '0d0'
185
+
186
+
187
+ @may_xfail
188
+ def test_bind_C():
189
+ if not has_fortran():
190
+ skip("No fortran compiler found.")
191
+ if not cython:
192
+ skip("Cython not found.")
193
+ if not np:
194
+ skip("NumPy not found.")
195
+
196
+ a = Symbol('a', real=True)
197
+ s = Symbol('s', integer=True)
198
+ body = [Return((sum_(a**2)/s)**.5)]
199
+ arr = array(a, dim=[s], intent='in')
200
+ fd = FunctionDefinition(real, 'rms', [arr, s], body, attrs=[bind_C('rms')])
201
+ f_mod = render_as_module([fd], 'mod_rms')
202
+
203
+ with tempfile.TemporaryDirectory() as folder:
204
+ mod, info = compile_link_import_strings([
205
+ ('rms.f90', f_mod),
206
+ ('_rms.pyx', (
207
+ "#cython: language_level={}\n".format("3") +
208
+ "cdef extern double rms(double*, int*)\n"
209
+ "def py_rms(double[::1] x):\n"
210
+ " cdef int s = x.size\n"
211
+ " return rms(&x[0], &s)\n"))
212
+ ], build_dir=folder)
213
+ assert abs(mod.py_rms(np.array([2., 4., 2., 2.])) - 7**0.5) < 1e-14
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/test_matrix_nodes.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core.symbol import symbols
2
+ from sympy.core.function import Function
3
+ from sympy.matrices.dense import Matrix
4
+ from sympy.matrices.dense import zeros
5
+ from sympy.simplify.simplify import simplify
6
+ from sympy.codegen.matrix_nodes import MatrixSolve
7
+ from sympy.utilities.lambdify import lambdify
8
+ from sympy.printing.numpy import NumPyPrinter
9
+ from sympy.testing.pytest import skip
10
+ from sympy.external import import_module
11
+
12
+
13
+ def test_matrix_solve_issue_24862():
14
+ A = Matrix(3, 3, symbols('a:9'))
15
+ b = Matrix(3, 1, symbols('b:3'))
16
+ hash(MatrixSolve(A, b))
17
+
18
+
19
+ def test_matrix_solve_derivative_exact():
20
+ q = symbols('q')
21
+ a11, a12, a21, a22, b1, b2 = (
22
+ f(q) for f in symbols('a11 a12 a21 a22 b1 b2', cls=Function))
23
+ A = Matrix([[a11, a12], [a21, a22]])
24
+ b = Matrix([b1, b2])
25
+ x_lu = A.LUsolve(b)
26
+ dxdq_lu = A.LUsolve(b.diff(q) - A.diff(q) * A.LUsolve(b))
27
+ assert simplify(x_lu.diff(q) - dxdq_lu) == zeros(2, 1)
28
+ # dxdq_ms is the MatrixSolve equivalent of dxdq_lu
29
+ dxdq_ms = MatrixSolve(A, b.diff(q) - A.diff(q) * MatrixSolve(A, b))
30
+ assert MatrixSolve(A, b).diff(q) == dxdq_ms
31
+
32
+
33
+ def test_matrix_solve_derivative_numpy():
34
+ np = import_module('numpy')
35
+ if not np:
36
+ skip("numpy not installed.")
37
+ q = symbols('q')
38
+ a11, a12, a21, a22, b1, b2 = (
39
+ f(q) for f in symbols('a11 a12 a21 a22 b1 b2', cls=Function))
40
+ A = Matrix([[a11, a12], [a21, a22]])
41
+ b = Matrix([b1, b2])
42
+ dx_lu = A.LUsolve(b).diff(q)
43
+ subs = {a11.diff(q): 0.2, a12.diff(q): 0.3, a21.diff(q): 0.1,
44
+ a22.diff(q): 0.5, b1.diff(q): 0.4, b2.diff(q): 0.9,
45
+ a11: 1.3, a12: 0.5, a21: 1.2, a22: 4, b1: 6.2, b2: 3.5}
46
+ p, p_vals = zip(*subs.items())
47
+ dx_sm = MatrixSolve(A, b).diff(q)
48
+ np.testing.assert_allclose(
49
+ lambdify(p, dx_sm, printer=NumPyPrinter)(*p_vals),
50
+ lambdify(p, dx_lu, printer=NumPyPrinter)(*p_vals))
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/test_numpy_nodes.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from itertools import product
2
+ from sympy.core.singleton import S
3
+ from sympy.core.symbol import symbols
4
+ from sympy.functions.elementary.exponential import (exp, log)
5
+ from sympy.functions.elementary.miscellaneous import Max, Min
6
+ from sympy.printing.repr import srepr
7
+ from sympy.codegen.numpy_nodes import logaddexp, logaddexp2, minimum, maximum, amax, amin
8
+ from sympy.testing.pytest import raises
9
+
10
+ x, y, z = symbols('x y z')
11
+
12
+ def test_logaddexp():
13
+ lae_xy = logaddexp(x, y)
14
+ ref_xy = log(exp(x) + exp(y))
15
+ for wrt, deriv_order in product([x, y, z], range(3)):
16
+ assert (
17
+ lae_xy.diff(wrt, deriv_order) -
18
+ ref_xy.diff(wrt, deriv_order)
19
+ ).rewrite(log).simplify() == 0
20
+
21
+ one_third_e = 1*exp(1)/3
22
+ two_thirds_e = 2*exp(1)/3
23
+ logThirdE = log(one_third_e)
24
+ logTwoThirdsE = log(two_thirds_e)
25
+ lae_sum_to_e = logaddexp(logThirdE, logTwoThirdsE)
26
+ assert lae_sum_to_e.rewrite(log) == 1
27
+ assert lae_sum_to_e.simplify() == 1
28
+ was = logaddexp(2, 3)
29
+ assert srepr(was) == srepr(was.simplify()) # cannot simplify with 2, 3
30
+
31
+
32
+ def test_logaddexp2():
33
+ lae2_xy = logaddexp2(x, y)
34
+ ref2_xy = log(2**x + 2**y)/log(2)
35
+ for wrt, deriv_order in product([x, y, z], range(3)):
36
+ assert (
37
+ lae2_xy.diff(wrt, deriv_order) -
38
+ ref2_xy.diff(wrt, deriv_order)
39
+ ).rewrite(log).cancel() == 0
40
+
41
+ def lb(x):
42
+ return log(x)/log(2)
43
+
44
+ two_thirds = S.One*2/3
45
+ four_thirds = 2*two_thirds
46
+ lbTwoThirds = lb(two_thirds)
47
+ lbFourThirds = lb(four_thirds)
48
+ lae2_sum_to_2 = logaddexp2(lbTwoThirds, lbFourThirds)
49
+ assert lae2_sum_to_2.rewrite(log) == 1
50
+ assert lae2_sum_to_2.simplify() == 1
51
+ was = logaddexp2(x, y)
52
+ assert srepr(was) == srepr(was.simplify()) # cannot simplify with x, y
53
+
54
+
55
+ def test_minimum_maximum():
56
+ for MM, mm in zip([Min, Max], [minimum, maximum]):
57
+ ref = MM(x, y, z)
58
+ m = mm(x, y, z)
59
+ assert m != ref
60
+ assert m.rewrite(MM) == ref
61
+
62
+
63
+ def test_amin_amax():
64
+ for am in [amin, amax]:
65
+ assert am(x).array == x
66
+ assert am(x).axis == None
67
+ assert am(x, axis=3).axis == 3
68
+ with raises(ValueError):
69
+ am(x, y, z)
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/test_pynodes.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core.symbol import symbols
2
+ from sympy.codegen.pynodes import List
3
+
4
+
5
+ def test_List():
6
+ l = List(2, 3, 4)
7
+ assert l == List(2, 3, 4)
8
+ assert str(l) == "[2, 3, 4]"
9
+ x, y, z = symbols('x y z')
10
+ l = List(x**2,y**3,z**4)
11
+ # contrary to python's built-in list, we can call e.g. "replace" on List.
12
+ m = l.replace(lambda arg: arg.is_Pow and arg.exp>2, lambda p: p.base-p.exp)
13
+ assert m == [x**2, y-3, z-4]
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/test_pyutils.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ from sympy.codegen.ast import Print
2
+ from sympy.codegen.pyutils import render_as_module
3
+
4
+ def test_standard():
5
+ ast = Print('x y'.split(), r"coordinate: %12.5g %12.5g\n")
6
+ assert render_as_module(ast, standard='python3') == \
7
+ '\n\nprint("coordinate: %12.5g %12.5g\\n" % (x, y), end="")'
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/test_rewriting.py ADDED
@@ -0,0 +1,479 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tempfile
2
+ from sympy.core.numbers import pi, Rational
3
+ from sympy.core.power import Pow
4
+ from sympy.core.singleton import S
5
+ from sympy.core.symbol import Symbol
6
+ from sympy.functions.elementary.complexes import Abs
7
+ from sympy.functions.elementary.exponential import (exp, log)
8
+ from sympy.functions.elementary.trigonometric import (cos, sin, sinc)
9
+ from sympy.matrices.expressions.matexpr import MatrixSymbol
10
+ from sympy.assumptions import assuming, Q
11
+ from sympy.external import import_module
12
+ from sympy.printing.codeprinter import ccode
13
+ from sympy.codegen.matrix_nodes import MatrixSolve
14
+ from sympy.codegen.cfunctions import log2, exp2, expm1, log1p
15
+ from sympy.codegen.numpy_nodes import logaddexp, logaddexp2
16
+ from sympy.codegen.scipy_nodes import cosm1, powm1
17
+ from sympy.codegen.rewriting import (
18
+ optimize, cosm1_opt, log2_opt, exp2_opt, expm1_opt, log1p_opt, powm1_opt, optims_c99,
19
+ create_expand_pow_optimization, matinv_opt, logaddexp_opt, logaddexp2_opt,
20
+ optims_numpy, optims_scipy, sinc_opts, FuncMinusOneOptim
21
+ )
22
+ from sympy.testing.pytest import XFAIL, skip
23
+ from sympy.utilities import lambdify
24
+ from sympy.utilities._compilation import compile_link_import_strings, has_c
25
+ from sympy.utilities._compilation.util import may_xfail
26
+
27
+ cython = import_module('cython')
28
+ numpy = import_module('numpy')
29
+ scipy = import_module('scipy')
30
+
31
+
32
+ def test_log2_opt():
33
+ x = Symbol('x')
34
+ expr1 = 7*log(3*x + 5)/(log(2))
35
+ opt1 = optimize(expr1, [log2_opt])
36
+ assert opt1 == 7*log2(3*x + 5)
37
+ assert opt1.rewrite(log) == expr1
38
+
39
+ expr2 = 3*log(5*x + 7)/(13*log(2))
40
+ opt2 = optimize(expr2, [log2_opt])
41
+ assert opt2 == 3*log2(5*x + 7)/13
42
+ assert opt2.rewrite(log) == expr2
43
+
44
+ expr3 = log(x)/log(2)
45
+ opt3 = optimize(expr3, [log2_opt])
46
+ assert opt3 == log2(x)
47
+ assert opt3.rewrite(log) == expr3
48
+
49
+ expr4 = log(x)/log(2) + log(x+1)
50
+ opt4 = optimize(expr4, [log2_opt])
51
+ assert opt4 == log2(x) + log(2)*log2(x+1)
52
+ assert opt4.rewrite(log) == expr4
53
+
54
+ expr5 = log(17)
55
+ opt5 = optimize(expr5, [log2_opt])
56
+ assert opt5 == expr5
57
+
58
+ expr6 = log(x + 3)/log(2)
59
+ opt6 = optimize(expr6, [log2_opt])
60
+ assert str(opt6) == 'log2(x + 3)'
61
+ assert opt6.rewrite(log) == expr6
62
+
63
+
64
+ def test_exp2_opt():
65
+ x = Symbol('x')
66
+ expr1 = 1 + 2**x
67
+ opt1 = optimize(expr1, [exp2_opt])
68
+ assert opt1 == 1 + exp2(x)
69
+ assert opt1.rewrite(Pow) == expr1
70
+
71
+ expr2 = 1 + 3**x
72
+ assert expr2 == optimize(expr2, [exp2_opt])
73
+
74
+
75
+ def test_expm1_opt():
76
+ x = Symbol('x')
77
+
78
+ expr1 = exp(x) - 1
79
+ opt1 = optimize(expr1, [expm1_opt])
80
+ assert expm1(x) - opt1 == 0
81
+ assert opt1.rewrite(exp) == expr1
82
+
83
+ expr2 = 3*exp(x) - 3
84
+ opt2 = optimize(expr2, [expm1_opt])
85
+ assert 3*expm1(x) == opt2
86
+ assert opt2.rewrite(exp) == expr2
87
+
88
+ expr3 = 3*exp(x) - 5
89
+ opt3 = optimize(expr3, [expm1_opt])
90
+ assert 3*expm1(x) - 2 == opt3
91
+ assert opt3.rewrite(exp) == expr3
92
+ expm1_opt_non_opportunistic = FuncMinusOneOptim(exp, expm1, opportunistic=False)
93
+ assert expr3 == optimize(expr3, [expm1_opt_non_opportunistic])
94
+ assert opt1 == optimize(expr1, [expm1_opt_non_opportunistic])
95
+ assert opt2 == optimize(expr2, [expm1_opt_non_opportunistic])
96
+
97
+ expr4 = 3*exp(x) + log(x) - 3
98
+ opt4 = optimize(expr4, [expm1_opt])
99
+ assert 3*expm1(x) + log(x) == opt4
100
+ assert opt4.rewrite(exp) == expr4
101
+
102
+ expr5 = 3*exp(2*x) - 3
103
+ opt5 = optimize(expr5, [expm1_opt])
104
+ assert 3*expm1(2*x) == opt5
105
+ assert opt5.rewrite(exp) == expr5
106
+
107
+ expr6 = (2*exp(x) + 1)/(exp(x) + 1) + 1
108
+ opt6 = optimize(expr6, [expm1_opt])
109
+ assert opt6.count_ops() <= expr6.count_ops()
110
+
111
+ def ev(e):
112
+ return e.subs(x, 3).evalf()
113
+ assert abs(ev(expr6) - ev(opt6)) < 1e-15
114
+
115
+ y = Symbol('y')
116
+ expr7 = (2*exp(x) - 1)/(1 - exp(y)) - 1/(1-exp(y))
117
+ opt7 = optimize(expr7, [expm1_opt])
118
+ assert -2*expm1(x)/expm1(y) == opt7
119
+ assert (opt7.rewrite(exp) - expr7).factor() == 0
120
+
121
+ expr8 = (1+exp(x))**2 - 4
122
+ opt8 = optimize(expr8, [expm1_opt])
123
+ tgt8a = (exp(x) + 3)*expm1(x)
124
+ tgt8b = 2*expm1(x) + expm1(2*x)
125
+ # Both tgt8a & tgt8b seem to give full precision (~16 digits for double)
126
+ # for x=1e-7 (compare with expr8 which only achieves ~8 significant digits).
127
+ # If we can show that either tgt8a or tgt8b is preferable, we can
128
+ # change this test to ensure the preferable version is returned.
129
+ assert (tgt8a - tgt8b).rewrite(exp).factor() == 0
130
+ assert opt8 in (tgt8a, tgt8b)
131
+ assert (opt8.rewrite(exp) - expr8).factor() == 0
132
+
133
+ expr9 = sin(expr8)
134
+ opt9 = optimize(expr9, [expm1_opt])
135
+ tgt9a = sin(tgt8a)
136
+ tgt9b = sin(tgt8b)
137
+ assert opt9 in (tgt9a, tgt9b)
138
+ assert (opt9.rewrite(exp) - expr9.rewrite(exp)).factor().is_zero
139
+
140
+
141
+ def test_expm1_two_exp_terms():
142
+ x, y = map(Symbol, 'x y'.split())
143
+ expr1 = exp(x) + exp(y) - 2
144
+ opt1 = optimize(expr1, [expm1_opt])
145
+ assert opt1 == expm1(x) + expm1(y)
146
+
147
+
148
+ def test_cosm1_opt():
149
+ x = Symbol('x')
150
+
151
+ expr1 = cos(x) - 1
152
+ opt1 = optimize(expr1, [cosm1_opt])
153
+ assert cosm1(x) - opt1 == 0
154
+ assert opt1.rewrite(cos) == expr1
155
+
156
+ expr2 = 3*cos(x) - 3
157
+ opt2 = optimize(expr2, [cosm1_opt])
158
+ assert 3*cosm1(x) == opt2
159
+ assert opt2.rewrite(cos) == expr2
160
+
161
+ expr3 = 3*cos(x) - 5
162
+ opt3 = optimize(expr3, [cosm1_opt])
163
+ assert 3*cosm1(x) - 2 == opt3
164
+ assert opt3.rewrite(cos) == expr3
165
+ cosm1_opt_non_opportunistic = FuncMinusOneOptim(cos, cosm1, opportunistic=False)
166
+ assert expr3 == optimize(expr3, [cosm1_opt_non_opportunistic])
167
+ assert opt1 == optimize(expr1, [cosm1_opt_non_opportunistic])
168
+ assert opt2 == optimize(expr2, [cosm1_opt_non_opportunistic])
169
+
170
+ expr4 = 3*cos(x) + log(x) - 3
171
+ opt4 = optimize(expr4, [cosm1_opt])
172
+ assert 3*cosm1(x) + log(x) == opt4
173
+ assert opt4.rewrite(cos) == expr4
174
+
175
+ expr5 = 3*cos(2*x) - 3
176
+ opt5 = optimize(expr5, [cosm1_opt])
177
+ assert 3*cosm1(2*x) == opt5
178
+ assert opt5.rewrite(cos) == expr5
179
+
180
+ expr6 = 2 - 2*cos(x)
181
+ opt6 = optimize(expr6, [cosm1_opt])
182
+ assert -2*cosm1(x) == opt6
183
+ assert opt6.rewrite(cos) == expr6
184
+
185
+
186
+ def test_cosm1_two_cos_terms():
187
+ x, y = map(Symbol, 'x y'.split())
188
+ expr1 = cos(x) + cos(y) - 2
189
+ opt1 = optimize(expr1, [cosm1_opt])
190
+ assert opt1 == cosm1(x) + cosm1(y)
191
+
192
+
193
+ def test_expm1_cosm1_mixed():
194
+ x = Symbol('x')
195
+ expr1 = exp(x) + cos(x) - 2
196
+ opt1 = optimize(expr1, [expm1_opt, cosm1_opt])
197
+ assert opt1 == cosm1(x) + expm1(x)
198
+
199
+
200
+ def _check_num_lambdify(expr, opt, val_subs, approx_ref, lambdify_kw=None, poorness=1e10):
201
+ """ poorness=1e10 signifies that `expr` loses precision of at least ten decimal digits. """
202
+ num_ref = expr.subs(val_subs).evalf()
203
+ eps = numpy.finfo(numpy.float64).eps
204
+ assert abs(num_ref - approx_ref) < approx_ref*eps
205
+ f1 = lambdify(list(val_subs.keys()), opt, **(lambdify_kw or {}))
206
+ args_float = tuple(map(float, val_subs.values()))
207
+ num_err1 = abs(f1(*args_float) - approx_ref)
208
+ assert num_err1 < abs(num_ref*eps)
209
+ f2 = lambdify(list(val_subs.keys()), expr, **(lambdify_kw or {}))
210
+ num_err2 = abs(f2(*args_float) - approx_ref)
211
+ assert num_err2 > abs(num_ref*eps*poorness) # this only ensures that the *test* works as intended
212
+
213
+
214
+ def test_cosm1_apart():
215
+ x = Symbol('x')
216
+
217
+ expr1 = 1/cos(x) - 1
218
+ opt1 = optimize(expr1, [cosm1_opt])
219
+ assert opt1 == -cosm1(x)/cos(x)
220
+ if scipy:
221
+ _check_num_lambdify(expr1, opt1, {x: S(10)**-30}, 5e-61, lambdify_kw={"modules": 'scipy'})
222
+
223
+ expr2 = 2/cos(x) - 2
224
+ opt2 = optimize(expr2, optims_scipy)
225
+ assert opt2 == -2*cosm1(x)/cos(x)
226
+ if scipy:
227
+ _check_num_lambdify(expr2, opt2, {x: S(10)**-30}, 1e-60, lambdify_kw={"modules": 'scipy'})
228
+
229
+ expr3 = pi/cos(3*x) - pi
230
+ opt3 = optimize(expr3, [cosm1_opt])
231
+ assert opt3 == -pi*cosm1(3*x)/cos(3*x)
232
+ if scipy:
233
+ _check_num_lambdify(expr3, opt3, {x: S(10)**-30/3}, float(5e-61*pi), lambdify_kw={"modules": 'scipy'})
234
+
235
+
236
+ def test_powm1():
237
+ args = x, y = map(Symbol, "xy")
238
+
239
+ expr1 = x**y - 1
240
+ opt1 = optimize(expr1, [powm1_opt])
241
+ assert opt1 == powm1(x, y)
242
+ for arg in args:
243
+ assert expr1.diff(arg) == opt1.diff(arg)
244
+ if scipy and tuple(map(int, scipy.version.version.split('.')[:3])) >= (1, 10, 0):
245
+ subs1_a = {x: Rational(*(1.0+1e-13).as_integer_ratio()), y: pi}
246
+ ref1_f64_a = 3.139081648208105e-13
247
+ _check_num_lambdify(expr1, opt1, subs1_a, ref1_f64_a, lambdify_kw={"modules": 'scipy'}, poorness=10**11)
248
+
249
+ subs1_b = {x: pi, y: Rational(*(1e-10).as_integer_ratio())}
250
+ ref1_f64_b = 1.1447298859149205e-10
251
+ _check_num_lambdify(expr1, opt1, subs1_b, ref1_f64_b, lambdify_kw={"modules": 'scipy'}, poorness=10**9)
252
+
253
+
254
+ def test_log1p_opt():
255
+ x = Symbol('x')
256
+ expr1 = log(x + 1)
257
+ opt1 = optimize(expr1, [log1p_opt])
258
+ assert log1p(x) - opt1 == 0
259
+ assert opt1.rewrite(log) == expr1
260
+
261
+ expr2 = log(3*x + 3)
262
+ opt2 = optimize(expr2, [log1p_opt])
263
+ assert log1p(x) + log(3) == opt2
264
+ assert (opt2.rewrite(log) - expr2).simplify() == 0
265
+
266
+ expr3 = log(2*x + 1)
267
+ opt3 = optimize(expr3, [log1p_opt])
268
+ assert log1p(2*x) - opt3 == 0
269
+ assert opt3.rewrite(log) == expr3
270
+
271
+ expr4 = log(x+3)
272
+ opt4 = optimize(expr4, [log1p_opt])
273
+ assert str(opt4) == 'log(x + 3)'
274
+
275
+
276
+ def test_optims_c99():
277
+ x = Symbol('x')
278
+
279
+ expr1 = 2**x + log(x)/log(2) + log(x + 1) + exp(x) - 1
280
+ opt1 = optimize(expr1, optims_c99).simplify()
281
+ assert opt1 == exp2(x) + log2(x) + log1p(x) + expm1(x)
282
+ assert opt1.rewrite(exp).rewrite(log).rewrite(Pow) == expr1
283
+
284
+ expr2 = log(x)/log(2) + log(x + 1)
285
+ opt2 = optimize(expr2, optims_c99)
286
+ assert opt2 == log2(x) + log1p(x)
287
+ assert opt2.rewrite(log) == expr2
288
+
289
+ expr3 = log(x)/log(2) + log(17*x + 17)
290
+ opt3 = optimize(expr3, optims_c99)
291
+ delta3 = opt3 - (log2(x) + log(17) + log1p(x))
292
+ assert delta3 == 0
293
+ assert (opt3.rewrite(log) - expr3).simplify() == 0
294
+
295
+ expr4 = 2**x + 3*log(5*x + 7)/(13*log(2)) + 11*exp(x) - 11 + log(17*x + 17)
296
+ opt4 = optimize(expr4, optims_c99).simplify()
297
+ delta4 = opt4 - (exp2(x) + 3*log2(5*x + 7)/13 + 11*expm1(x) + log(17) + log1p(x))
298
+ assert delta4 == 0
299
+ assert (opt4.rewrite(exp).rewrite(log).rewrite(Pow) - expr4).simplify() == 0
300
+
301
+ expr5 = 3*exp(2*x) - 3
302
+ opt5 = optimize(expr5, optims_c99)
303
+ delta5 = opt5 - 3*expm1(2*x)
304
+ assert delta5 == 0
305
+ assert opt5.rewrite(exp) == expr5
306
+
307
+ expr6 = exp(2*x) - 3
308
+ opt6 = optimize(expr6, optims_c99)
309
+ assert opt6 in (expm1(2*x) - 2, expr6) # expm1(2*x) - 2 is not better or worse
310
+
311
+ expr7 = log(3*x + 3)
312
+ opt7 = optimize(expr7, optims_c99)
313
+ delta7 = opt7 - (log(3) + log1p(x))
314
+ assert delta7 == 0
315
+ assert (opt7.rewrite(log) - expr7).simplify() == 0
316
+
317
+ expr8 = log(2*x + 3)
318
+ opt8 = optimize(expr8, optims_c99)
319
+ assert opt8 == expr8
320
+
321
+
322
+ def test_create_expand_pow_optimization():
323
+ cc = lambda x: ccode(
324
+ optimize(x, [create_expand_pow_optimization(4)]))
325
+ x = Symbol('x')
326
+ assert cc(x**4) == 'x*x*x*x'
327
+ assert cc(x**4 + x**2) == 'x*x + x*x*x*x'
328
+ assert cc(x**5 + x**4) == 'pow(x, 5) + x*x*x*x'
329
+ assert cc(sin(x)**4) == 'pow(sin(x), 4)'
330
+ # gh issue 15335
331
+ assert cc(x**(-4)) == '1.0/(x*x*x*x)'
332
+ assert cc(x**(-5)) == 'pow(x, -5)'
333
+ assert cc(-x**4) == '-(x*x*x*x)'
334
+ assert cc(x**4 - x**2) == '-(x*x) + x*x*x*x'
335
+ i = Symbol('i', integer=True)
336
+ assert cc(x**i - x**2) == 'pow(x, i) - (x*x)'
337
+ y = Symbol('y', real=True)
338
+ assert cc(Abs(exp(y**4))) == "exp(y*y*y*y)"
339
+
340
+ # gh issue 20753
341
+ cc2 = lambda x: ccode(optimize(x, [create_expand_pow_optimization(
342
+ 4, base_req=lambda b: b.is_Function)]))
343
+ assert cc2(x**3 + sin(x)**3) == "pow(x, 3) + sin(x)*sin(x)*sin(x)"
344
+
345
+
346
+ def test_matsolve():
347
+ n = Symbol('n', integer=True)
348
+ A = MatrixSymbol('A', n, n)
349
+ x = MatrixSymbol('x', n, 1)
350
+
351
+ with assuming(Q.fullrank(A)):
352
+ assert optimize(A**(-1) * x, [matinv_opt]) == MatrixSolve(A, x)
353
+ assert optimize(A**(-1) * x + x, [matinv_opt]) == MatrixSolve(A, x) + x
354
+
355
+
356
+ def test_logaddexp_opt():
357
+ x, y = map(Symbol, 'x y'.split())
358
+ expr1 = log(exp(x) + exp(y))
359
+ opt1 = optimize(expr1, [logaddexp_opt])
360
+ assert logaddexp(x, y) - opt1 == 0
361
+ assert logaddexp(y, x) - opt1 == 0
362
+ assert opt1.rewrite(log) == expr1
363
+
364
+
365
+ def test_logaddexp2_opt():
366
+ x, y = map(Symbol, 'x y'.split())
367
+ expr1 = log(2**x + 2**y)/log(2)
368
+ opt1 = optimize(expr1, [logaddexp2_opt])
369
+ assert logaddexp2(x, y) - opt1 == 0
370
+ assert logaddexp2(y, x) - opt1 == 0
371
+ assert opt1.rewrite(log) == expr1
372
+
373
+
374
+ def test_sinc_opts():
375
+ def check(d):
376
+ for k, v in d.items():
377
+ assert optimize(k, sinc_opts) == v
378
+
379
+ x = Symbol('x')
380
+ check({
381
+ sin(x)/x : sinc(x),
382
+ sin(2*x)/(2*x) : sinc(2*x),
383
+ sin(3*x)/x : 3*sinc(3*x),
384
+ x*sin(x) : x*sin(x)
385
+ })
386
+
387
+ y = Symbol('y')
388
+ check({
389
+ sin(x*y)/(x*y) : sinc(x*y),
390
+ y*sin(x/y)/x : sinc(x/y),
391
+ sin(sin(x))/sin(x) : sinc(sin(x)),
392
+ sin(3*sin(x))/sin(x) : 3*sinc(3*sin(x)),
393
+ sin(x)/y : sin(x)/y
394
+ })
395
+
396
+
397
+ def test_optims_numpy():
398
+ def check(d):
399
+ for k, v in d.items():
400
+ assert optimize(k, optims_numpy) == v
401
+
402
+ x = Symbol('x')
403
+ check({
404
+ sin(2*x)/(2*x) + exp(2*x) - 1: sinc(2*x) + expm1(2*x),
405
+ log(x+3)/log(2) + log(x**2 + 1): log1p(x**2) + log2(x+3)
406
+ })
407
+
408
+
409
+ @XFAIL # room for improvement, ideally this test case should pass.
410
+ def test_optims_numpy_TODO():
411
+ def check(d):
412
+ for k, v in d.items():
413
+ assert optimize(k, optims_numpy) == v
414
+
415
+ x, y = map(Symbol, 'x y'.split())
416
+ check({
417
+ log(x*y)*sin(x*y)*log(x*y+1)/(log(2)*x*y): log2(x*y)*sinc(x*y)*log1p(x*y),
418
+ exp(x*sin(y)/y) - 1: expm1(x*sinc(y))
419
+ })
420
+
421
+
422
+ @may_xfail
423
+ def test_compiled_ccode_with_rewriting():
424
+ if not cython:
425
+ skip("cython not installed.")
426
+ if not has_c():
427
+ skip("No C compiler found.")
428
+
429
+ x = Symbol('x')
430
+ about_two = 2**(58/S(117))*3**(97/S(117))*5**(4/S(39))*7**(92/S(117))/S(30)*pi
431
+ # about_two: 1.999999999999581826
432
+ unchanged = 2*exp(x) - about_two
433
+ xval = S(10)**-11
434
+ ref = unchanged.subs(x, xval).n(19) # 2.0418173913673213e-11
435
+
436
+ rewritten = optimize(2*exp(x) - about_two, [expm1_opt])
437
+
438
+ # Unfortunately, we need to call ``.n()`` on our expressions before we hand them
439
+ # to ``ccode``, and we need to request a large number of significant digits.
440
+ # In this test, results converged for double precision when the following number
441
+ # of significant digits were chosen:
442
+ NUMBER_OF_DIGITS = 25 # TODO: this should ideally be automatically handled.
443
+
444
+ func_c = '''
445
+ #include <math.h>
446
+
447
+ double func_unchanged(double x) {
448
+ return %(unchanged)s;
449
+ }
450
+ double func_rewritten(double x) {
451
+ return %(rewritten)s;
452
+ }
453
+ ''' % {"unchanged": ccode(unchanged.n(NUMBER_OF_DIGITS)),
454
+ "rewritten": ccode(rewritten.n(NUMBER_OF_DIGITS))}
455
+
456
+ func_pyx = '''
457
+ #cython: language_level=3
458
+ cdef extern double func_unchanged(double)
459
+ cdef extern double func_rewritten(double)
460
+ def py_unchanged(x):
461
+ return func_unchanged(x)
462
+ def py_rewritten(x):
463
+ return func_rewritten(x)
464
+ '''
465
+ with tempfile.TemporaryDirectory() as folder:
466
+ mod, info = compile_link_import_strings(
467
+ [('func.c', func_c), ('_func.pyx', func_pyx)],
468
+ build_dir=folder, compile_kwargs={"std": 'c99'}
469
+ )
470
+ err_rewritten = abs(mod.py_rewritten(1e-11) - ref)
471
+ err_unchanged = abs(mod.py_unchanged(1e-11) - ref)
472
+ assert 1e-27 < err_rewritten < 1e-25 # highly accurate.
473
+ assert 1e-19 < err_unchanged < 1e-16 # quite poor.
474
+
475
+ # Tolerances used above were determined as follows:
476
+ # >>> no_opt = unchanged.subs(x, xval.evalf()).evalf()
477
+ # >>> with_opt = rewritten.n(25).subs(x, 1e-11).evalf()
478
+ # >>> with_opt - ref, no_opt - ref
479
+ # (1.1536301877952077e-26, 1.6547074214222335e-18)
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/codegen/tests/test_scipy_nodes.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from itertools import product
2
+ from sympy.core.power import Pow
3
+ from sympy.core.symbol import symbols
4
+ from sympy.functions.elementary.exponential import exp, log
5
+ from sympy.functions.elementary.trigonometric import cos
6
+ from sympy.core.numbers import pi
7
+ from sympy.codegen.scipy_nodes import cosm1, powm1
8
+
9
+ x, y, z = symbols('x y z')
10
+
11
+
12
+ def test_cosm1():
13
+ cm1_xy = cosm1(x*y)
14
+ ref_xy = cos(x*y) - 1
15
+ for wrt, deriv_order in product([x, y, z], range(3)):
16
+ assert (
17
+ cm1_xy.diff(wrt, deriv_order) -
18
+ ref_xy.diff(wrt, deriv_order)
19
+ ).rewrite(cos).simplify() == 0
20
+
21
+ expr_minus2 = cosm1(pi)
22
+ assert expr_minus2.rewrite(cos) == -2
23
+ assert cosm1(3.14).simplify() == cosm1(3.14) # cannot simplify with 3.14
24
+ assert cosm1(pi/2).simplify() == -1
25
+ assert (1/cos(x) - 1 + cosm1(x)/cos(x)).simplify() == 0
26
+
27
+
28
+ def test_powm1():
29
+ cases = {
30
+ powm1(x, y): x**y - 1,
31
+ powm1(x*y, z): (x*y)**z - 1,
32
+ powm1(x, y*z): x**(y*z)-1,
33
+ powm1(x*y*z, x*y*z): (x*y*z)**(x*y*z)-1
34
+ }
35
+ for pm1_e, ref_e in cases.items():
36
+ for wrt, deriv_order in product([x, y, z], range(3)):
37
+ der = pm1_e.diff(wrt, deriv_order)
38
+ ref = ref_e.diff(wrt, deriv_order)
39
+ delta = (der - ref).rewrite(Pow)
40
+ assert delta.simplify() == 0
41
+
42
+ eulers_constant_m1 = powm1(x, 1/log(x))
43
+ assert eulers_constant_m1.rewrite(Pow) == exp(1) - 1
44
+ assert eulers_constant_m1.simplify() == exp(1) - 1
Prism/LLaDA/LLaDA_Prism/.venv/lib/python3.12/site-packages/sympy/core/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (3.44 kB). View file