I am done

This commit is contained in:
2024-10-30 22:14:35 +01:00
parent 720dc28c09
commit 40e2a747cf
36901 changed files with 5011519 additions and 0 deletions

View File

@ -0,0 +1,77 @@
from sympy.core.symbol import symbols, Symbol
from sympy.functions import Max
from sympy.plotting.experimental_lambdify import experimental_lambdify
from sympy.plotting.intervalmath.interval_arithmetic import \
interval, intervalMembership
# Tests for exception handling in experimental_lambdify
def test_experimental_lambify():
x = Symbol('x')
f = experimental_lambdify([x], Max(x, 5))
# XXX should f be tested? If f(2) is attempted, an
# error is raised because a complex produced during wrapping of the arg
# is being compared with an int.
assert Max(2, 5) == 5
assert Max(5, 7) == 7
x = Symbol('x-3')
f = experimental_lambdify([x], x + 1)
assert f(1) == 2
def test_composite_boolean_region():
x, y = symbols('x y')
r1 = (x - 1)**2 + y**2 < 2
r2 = (x + 1)**2 + y**2 < 2
f = experimental_lambdify((x, y), r1 & r2)
a = (interval(-0.1, 0.1), interval(-0.1, 0.1))
assert f(*a) == intervalMembership(True, True)
a = (interval(-1.1, -0.9), interval(-0.1, 0.1))
assert f(*a) == intervalMembership(False, True)
a = (interval(0.9, 1.1), interval(-0.1, 0.1))
assert f(*a) == intervalMembership(False, True)
a = (interval(-0.1, 0.1), interval(1.9, 2.1))
assert f(*a) == intervalMembership(False, True)
f = experimental_lambdify((x, y), r1 | r2)
a = (interval(-0.1, 0.1), interval(-0.1, 0.1))
assert f(*a) == intervalMembership(True, True)
a = (interval(-1.1, -0.9), interval(-0.1, 0.1))
assert f(*a) == intervalMembership(True, True)
a = (interval(0.9, 1.1), interval(-0.1, 0.1))
assert f(*a) == intervalMembership(True, True)
a = (interval(-0.1, 0.1), interval(1.9, 2.1))
assert f(*a) == intervalMembership(False, True)
f = experimental_lambdify((x, y), r1 & ~r2)
a = (interval(-0.1, 0.1), interval(-0.1, 0.1))
assert f(*a) == intervalMembership(False, True)
a = (interval(-1.1, -0.9), interval(-0.1, 0.1))
assert f(*a) == intervalMembership(False, True)
a = (interval(0.9, 1.1), interval(-0.1, 0.1))
assert f(*a) == intervalMembership(True, True)
a = (interval(-0.1, 0.1), interval(1.9, 2.1))
assert f(*a) == intervalMembership(False, True)
f = experimental_lambdify((x, y), ~r1 & r2)
a = (interval(-0.1, 0.1), interval(-0.1, 0.1))
assert f(*a) == intervalMembership(False, True)
a = (interval(-1.1, -0.9), interval(-0.1, 0.1))
assert f(*a) == intervalMembership(True, True)
a = (interval(0.9, 1.1), interval(-0.1, 0.1))
assert f(*a) == intervalMembership(False, True)
a = (interval(-0.1, 0.1), interval(1.9, 2.1))
assert f(*a) == intervalMembership(False, True)
f = experimental_lambdify((x, y), ~r1 & ~r2)
a = (interval(-0.1, 0.1), interval(-0.1, 0.1))
assert f(*a) == intervalMembership(False, True)
a = (interval(-1.1, -0.9), interval(-0.1, 0.1))
assert f(*a) == intervalMembership(False, True)
a = (interval(0.9, 1.1), interval(-0.1, 0.1))
assert f(*a) == intervalMembership(False, True)
a = (interval(-0.1, 0.1), interval(1.9, 2.1))
assert f(*a) == intervalMembership(True, True)

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,146 @@
from sympy.core.numbers import (I, pi)
from sympy.core.relational import Eq
from sympy.core.symbol import (Symbol, symbols)
from sympy.functions.elementary.complexes import re
from sympy.functions.elementary.exponential import exp
from sympy.functions.elementary.trigonometric import (cos, sin, tan)
from sympy.logic.boolalg import (And, Or)
from sympy.plotting.plot_implicit import plot_implicit
from sympy.plotting.plot import unset_show
from tempfile import NamedTemporaryFile, mkdtemp
from sympy.testing.pytest import skip, warns, XFAIL
from sympy.external import import_module
from sympy.testing.tmpfiles import TmpFileManager
import os
#Set plots not to show
unset_show()
def tmp_file(dir=None, name=''):
return NamedTemporaryFile(
suffix='.png', dir=dir, delete=False).name
def plot_and_save(expr, *args, name='', dir=None, **kwargs):
p = plot_implicit(expr, *args, **kwargs)
p.save(tmp_file(dir=dir, name=name))
# Close the plot to avoid a warning from matplotlib
p._backend.close()
def plot_implicit_tests(name):
temp_dir = mkdtemp()
TmpFileManager.tmp_folder(temp_dir)
x = Symbol('x')
y = Symbol('y')
#implicit plot tests
plot_and_save(Eq(y, cos(x)), (x, -5, 5), (y, -2, 2), name=name, dir=temp_dir)
plot_and_save(Eq(y**2, x**3 - x), (x, -5, 5),
(y, -4, 4), name=name, dir=temp_dir)
plot_and_save(y > 1 / x, (x, -5, 5),
(y, -2, 2), name=name, dir=temp_dir)
plot_and_save(y < 1 / tan(x), (x, -5, 5),
(y, -2, 2), name=name, dir=temp_dir)
plot_and_save(y >= 2 * sin(x) * cos(x), (x, -5, 5),
(y, -2, 2), name=name, dir=temp_dir)
plot_and_save(y <= x**2, (x, -3, 3),
(y, -1, 5), name=name, dir=temp_dir)
#Test all input args for plot_implicit
plot_and_save(Eq(y**2, x**3 - x), dir=temp_dir)
plot_and_save(Eq(y**2, x**3 - x), adaptive=False, dir=temp_dir)
plot_and_save(Eq(y**2, x**3 - x), adaptive=False, n=500, dir=temp_dir)
plot_and_save(y > x, (x, -5, 5), dir=temp_dir)
plot_and_save(And(y > exp(x), y > x + 2), dir=temp_dir)
plot_and_save(Or(y > x, y > -x), dir=temp_dir)
plot_and_save(x**2 - 1, (x, -5, 5), dir=temp_dir)
plot_and_save(x**2 - 1, dir=temp_dir)
plot_and_save(y > x, depth=-5, dir=temp_dir)
plot_and_save(y > x, depth=5, dir=temp_dir)
plot_and_save(y > cos(x), adaptive=False, dir=temp_dir)
plot_and_save(y < cos(x), adaptive=False, dir=temp_dir)
plot_and_save(And(y > cos(x), Or(y > x, Eq(y, x))), dir=temp_dir)
plot_and_save(y - cos(pi / x), dir=temp_dir)
plot_and_save(x**2 - 1, title='An implicit plot', dir=temp_dir)
@XFAIL
def test_no_adaptive_meshing():
matplotlib = import_module('matplotlib', min_module_version='1.1.0', catch=(RuntimeError,))
if matplotlib:
try:
temp_dir = mkdtemp()
TmpFileManager.tmp_folder(temp_dir)
x = Symbol('x')
y = Symbol('y')
# Test plots which cannot be rendered using the adaptive algorithm
# This works, but it triggers a deprecation warning from sympify(). The
# code needs to be updated to detect if interval math is supported without
# relying on random AttributeErrors.
with warns(UserWarning, match="Adaptive meshing could not be applied"):
plot_and_save(Eq(y, re(cos(x) + I*sin(x))), name='test', dir=temp_dir)
finally:
TmpFileManager.cleanup()
else:
skip("Matplotlib not the default backend")
def test_line_color():
x, y = symbols('x, y')
p = plot_implicit(x**2 + y**2 - 1, line_color="green", show=False)
assert p._series[0].line_color == "green"
p = plot_implicit(x**2 + y**2 - 1, line_color='r', show=False)
assert p._series[0].line_color == "r"
def test_matplotlib():
matplotlib = import_module('matplotlib', min_module_version='1.1.0', catch=(RuntimeError,))
if matplotlib:
try:
plot_implicit_tests('test')
test_line_color()
finally:
TmpFileManager.cleanup()
else:
skip("Matplotlib not the default backend")
def test_region_and():
matplotlib = import_module('matplotlib', min_module_version='1.1.0', catch=(RuntimeError,))
if not matplotlib:
skip("Matplotlib not the default backend")
from matplotlib.testing.compare import compare_images
test_directory = os.path.dirname(os.path.abspath(__file__))
try:
temp_dir = mkdtemp()
TmpFileManager.tmp_folder(temp_dir)
x, y = symbols('x y')
r1 = (x - 1)**2 + y**2 < 2
r2 = (x + 1)**2 + y**2 < 2
test_filename = tmp_file(dir=temp_dir, name="test_region_and")
cmp_filename = os.path.join(test_directory, "test_region_and.png")
p = plot_implicit(r1 & r2, x, y)
p.save(test_filename)
compare_images(cmp_filename, test_filename, 0.005)
test_filename = tmp_file(dir=temp_dir, name="test_region_or")
cmp_filename = os.path.join(test_directory, "test_region_or.png")
p = plot_implicit(r1 | r2, x, y)
p.save(test_filename)
compare_images(cmp_filename, test_filename, 0.005)
test_filename = tmp_file(dir=temp_dir, name="test_region_not")
cmp_filename = os.path.join(test_directory, "test_region_not.png")
p = plot_implicit(~r1, x, y)
p.save(test_filename)
compare_images(cmp_filename, test_filename, 0.005)
test_filename = tmp_file(dir=temp_dir, name="test_region_xor")
cmp_filename = os.path.join(test_directory, "test_region_xor.png")
p = plot_implicit(r1 ^ r2, x, y)
p.save(test_filename)
compare_images(cmp_filename, test_filename, 0.005)
finally:
TmpFileManager.cleanup()

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 KiB

View File

@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://ccgrfp1kviy0t"
path="res://.godot/imported/test_region_and.png-c1d9a0695d86ddf4348962c5b17879ff.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://rl/Lib/site-packages/sympy/plotting/tests/test_region_and.png"
dest_files=["res://.godot/imported/test_region_and.png-c1d9a0695d86ddf4348962c5b17879ff.ctex"]
[params]
compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.8 KiB

View File

@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://bwcwpn78v6jsw"
path="res://.godot/imported/test_region_not.png-f3250ebd2c1022cb029e786ee370fd83.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://rl/Lib/site-packages/sympy/plotting/tests/test_region_not.png"
dest_files=["res://.godot/imported/test_region_not.png-f3250ebd2c1022cb029e786ee370fd83.ctex"]
[params]
compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

View File

@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://ptn7si3a6w3e"
path="res://.godot/imported/test_region_or.png-fcd19de9f7f9d11aa222486f596ec33d.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://rl/Lib/site-packages/sympy/plotting/tests/test_region_or.png"
dest_files=["res://.godot/imported/test_region_or.png-fcd19de9f7f9d11aa222486f596ec33d.ctex"]
[params]
compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

View File

@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://dp0upq1njbej6"
path="res://.godot/imported/test_region_xor.png-b5f7c27e2fb8694466c39ee95db567dc.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://rl/Lib/site-packages/sympy/plotting/tests/test_region_xor.png"
dest_files=["res://.godot/imported/test_region_xor.png-b5f7c27e2fb8694466c39ee95db567dc.ctex"]
[params]
compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,203 @@
from sympy.core.singleton import S
from sympy.core.symbol import Symbol
from sympy.functions.elementary.exponential import log
from sympy.functions.elementary.miscellaneous import sqrt
from sympy.functions.elementary.trigonometric import sin
from sympy.plotting.textplot import textplot_str
from sympy.utilities.exceptions import ignore_warnings
def test_axes_alignment():
x = Symbol('x')
lines = [
' 1 | ..',
' | ... ',
' | .. ',
' | ... ',
' | ... ',
' | .. ',
' | ... ',
' | ... ',
' | .. ',
' | ... ',
' 0 |--------------------------...--------------------------',
' | ... ',
' | .. ',
' | ... ',
' | ... ',
' | .. ',
' | ... ',
' | ... ',
' | .. ',
' | ... ',
' -1 |_______________________________________________________',
' -1 0 1'
]
assert lines == list(textplot_str(x, -1, 1))
lines = [
' 1 | ..',
' | .... ',
' | ... ',
' | ... ',
' | .... ',
' | ... ',
' | ... ',
' | .... ',
' 0 |--------------------------...--------------------------',
' | .... ',
' | ... ',
' | ... ',
' | .... ',
' | ... ',
' | ... ',
' | .... ',
' -1 |_______________________________________________________',
' -1 0 1'
]
assert lines == list(textplot_str(x, -1, 1, H=17))
def test_singularity():
x = Symbol('x')
lines = [
' 54 | . ',
' | ',
' | ',
' | ',
' | ',' | ',
' | ',
' | ',
' | ',
' | ',
' 27.5 |--.----------------------------------------------------',
' | ',
' | ',
' | ',
' | . ',
' | \\ ',
' | \\ ',
' | .. ',
' | ... ',
' | ............. ',
' 1 |_______________________________________________________',
' 0 0.5 1'
]
assert lines == list(textplot_str(1/x, 0, 1))
lines = [
' 0 | ......',
' | ........ ',
' | ........ ',
' | ...... ',
' | ..... ',
' | .... ',
' | ... ',
' | .. ',
' | ... ',
' | / ',
' -2 |-------..----------------------------------------------',
' | / ',
' | / ',
' | / ',
' | . ',
' | ',
' | . ',
' | ',
' | ',
' | ',
' -4 |_______________________________________________________',
' 0 0.5 1'
]
# RuntimeWarning: divide by zero encountered in log
with ignore_warnings(RuntimeWarning):
assert lines == list(textplot_str(log(x), 0, 1))
def test_sinc():
x = Symbol('x')
lines = [
' 1 | . . ',
' | . . ',
' | ',
' | . . ',
' | ',
' | . . ',
' | ',
' | ',
' | . . ',
' | ',
' 0.4 |-------------------------------------------------------',
' | . . ',
' | ',
' | . . ',
' | ',
' | ..... ..... ',
' | .. \\ . . / .. ',
' | / \\ / \\ ',
' |/ \\ . . / \\',
' | \\ / \\ / ',
' -0.2 |_______________________________________________________',
' -10 0 10'
]
# RuntimeWarning: invalid value encountered in double_scalars
with ignore_warnings(RuntimeWarning):
assert lines == list(textplot_str(sin(x)/x, -10, 10))
def test_imaginary():
x = Symbol('x')
lines = [
' 1 | ..',
' | .. ',
' | ... ',
' | .. ',
' | .. ',
' | .. ',
' | .. ',
' | .. ',
' | .. ',
' | / ',
' 0.5 |----------------------------------/--------------------',
' | .. ',
' | / ',
' | . ',
' | ',
' | . ',
' | . ',
' | ',
' | ',
' | ',
' 0 |_______________________________________________________',
' -1 0 1'
]
# RuntimeWarning: invalid value encountered in sqrt
with ignore_warnings(RuntimeWarning):
assert list(textplot_str(sqrt(x), -1, 1)) == lines
lines = [
' 1 | ',
' | ',
' | ',
' | ',
' | ',
' | ',
' | ',
' | ',
' | ',
' | ',
' 0 |-------------------------------------------------------',
' | ',
' | ',
' | ',
' | ',
' | ',
' | ',
' | ',
' | ',
' | ',
' -1 |_______________________________________________________',
' -1 0 1'
]
assert list(textplot_str(S.ImaginaryUnit, -1, 1)) == lines

View File

@ -0,0 +1,110 @@
from pytest import raises
from sympy import (
symbols, Expr, Tuple, Integer, cos, solveset, FiniteSet, ImageSet)
from sympy.plotting.utils import (
_create_ranges, _plot_sympify, extract_solution)
from sympy.physics.mechanics import ReferenceFrame, Vector as MechVector
from sympy.vector import CoordSys3D, Vector
def test_plot_sympify():
x, y = symbols("x, y")
# argument is already sympified
args = x + y
r = _plot_sympify(args)
assert r == args
# one argument needs to be sympified
args = (x + y, 1)
r = _plot_sympify(args)
assert isinstance(r, (list, tuple, Tuple)) and len(r) == 2
assert isinstance(r[0], Expr)
assert isinstance(r[1], Integer)
# string and dict should not be sympified
args = (x + y, (x, 0, 1), "str", 1, {1: 1, 2: 2.0})
r = _plot_sympify(args)
assert isinstance(r, (list, tuple, Tuple)) and len(r) == 5
assert isinstance(r[0], Expr)
assert isinstance(r[1], Tuple)
assert isinstance(r[2], str)
assert isinstance(r[3], Integer)
assert isinstance(r[4], dict) and isinstance(r[4][1], int) and isinstance(r[4][2], float)
# nested arguments containing strings
args = ((x + y, (y, 0, 1), "a"), (x + 1, (x, 0, 1), "$f_{1}$"))
r = _plot_sympify(args)
assert isinstance(r, (list, tuple, Tuple)) and len(r) == 2
assert isinstance(r[0], Tuple)
assert isinstance(r[0][1], Tuple)
assert isinstance(r[0][1][1], Integer)
assert isinstance(r[0][2], str)
assert isinstance(r[1], Tuple)
assert isinstance(r[1][1], Tuple)
assert isinstance(r[1][1][1], Integer)
assert isinstance(r[1][2], str)
# vectors from sympy.physics.vectors module are not sympified
# vectors from sympy.vectors are sympified
# in both cases, no error should be raised
R = ReferenceFrame("R")
v1 = 2 * R.x + R.y
C = CoordSys3D("C")
v2 = 2 * C.i + C.j
args = (v1, v2)
r = _plot_sympify(args)
assert isinstance(r, (list, tuple, Tuple)) and len(r) == 2
assert isinstance(v1, MechVector)
assert isinstance(v2, Vector)
def test_create_ranges():
x, y = symbols("x, y")
# user don't provide any range -> return a default range
r = _create_ranges({x}, [], 1)
assert isinstance(r, (list, tuple, Tuple)) and len(r) == 1
assert isinstance(r[0], (Tuple, tuple))
assert r[0] == (x, -10, 10)
r = _create_ranges({x, y}, [], 2)
assert isinstance(r, (list, tuple, Tuple)) and len(r) == 2
assert isinstance(r[0], (Tuple, tuple))
assert isinstance(r[1], (Tuple, tuple))
assert r[0] == (x, -10, 10) or (y, -10, 10)
assert r[1] == (y, -10, 10) or (x, -10, 10)
assert r[0] != r[1]
# not enough ranges provided by the user -> create default ranges
r = _create_ranges(
{x, y},
[
(x, 0, 1),
],
2,
)
assert isinstance(r, (list, tuple, Tuple)) and len(r) == 2
assert isinstance(r[0], (Tuple, tuple))
assert isinstance(r[1], (Tuple, tuple))
assert r[0] == (x, 0, 1) or (y, -10, 10)
assert r[1] == (y, -10, 10) or (x, 0, 1)
assert r[0] != r[1]
# too many free symbols
raises(ValueError, lambda: _create_ranges({x, y}, [], 1))
raises(ValueError, lambda: _create_ranges({x, y}, [(x, 0, 5), (y, 0, 1)], 1))
def test_extract_solution():
x = symbols("x")
sol = solveset(cos(10 * x))
assert sol.has(ImageSet)
res = extract_solution(sol)
assert len(res) == 20
assert isinstance(res, FiniteSet)
res = extract_solution(sol, 20)
assert len(res) == 40
assert isinstance(res, FiniteSet)