Spaces:
Sleeping
Sleeping
File size: 65,637 Bytes
6a86ad5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 |
from sympy import (
latex, exp, symbols, I, pi, sin, cos, tan, log, sqrt,
re, im, arg, frac, Sum, S, Abs, lambdify,
Function, dsolve, Eq, floor, Tuple
)
from sympy.external import import_module
from sympy.plotting.series import (
LineOver1DRangeSeries, Parametric2DLineSeries, Parametric3DLineSeries,
SurfaceOver2DRangeSeries, ContourSeries, ParametricSurfaceSeries,
ImplicitSeries, _set_discretization_points, List2DSeries
)
from sympy.testing.pytest import raises, warns, XFAIL, skip, ignore_warnings
np = import_module('numpy')
def test_adaptive():
# verify that adaptive-related keywords produces the expected results
if not np:
skip("numpy not installed.")
x, y = symbols("x, y")
s1 = LineOver1DRangeSeries(sin(x), (x, -10, 10), "", adaptive=True,
depth=2)
x1, _ = s1.get_data()
s2 = LineOver1DRangeSeries(sin(x), (x, -10, 10), "", adaptive=True,
depth=5)
x2, _ = s2.get_data()
s3 = LineOver1DRangeSeries(sin(x), (x, -10, 10), "", adaptive=True)
x3, _ = s3.get_data()
assert len(x1) < len(x2) < len(x3)
s1 = Parametric2DLineSeries(cos(x), sin(x), (x, 0, 2*pi),
adaptive=True, depth=2)
x1, _, _, = s1.get_data()
s2 = Parametric2DLineSeries(cos(x), sin(x), (x, 0, 2*pi),
adaptive=True, depth=5)
x2, _, _ = s2.get_data()
s3 = Parametric2DLineSeries(cos(x), sin(x), (x, 0, 2*pi),
adaptive=True)
x3, _, _ = s3.get_data()
assert len(x1) < len(x2) < len(x3)
def test_detect_poles():
if not np:
skip("numpy not installed.")
x, u = symbols("x, u")
s1 = LineOver1DRangeSeries(tan(x), (x, -pi, pi),
adaptive=False, n=1000, detect_poles=False)
xx1, yy1 = s1.get_data()
s2 = LineOver1DRangeSeries(tan(x), (x, -pi, pi),
adaptive=False, n=1000, detect_poles=True, eps=0.01)
xx2, yy2 = s2.get_data()
# eps is too small: doesn't detect any poles
s3 = LineOver1DRangeSeries(tan(x), (x, -pi, pi),
adaptive=False, n=1000, detect_poles=True, eps=1e-06)
xx3, yy3 = s3.get_data()
s4 = LineOver1DRangeSeries(tan(x), (x, -pi, pi),
adaptive=False, n=1000, detect_poles="symbolic")
xx4, yy4 = s4.get_data()
assert np.allclose(xx1, xx2) and np.allclose(xx1, xx3) and np.allclose(xx1, xx4)
assert not np.any(np.isnan(yy1))
assert not np.any(np.isnan(yy3))
assert np.any(np.isnan(yy2))
assert np.any(np.isnan(yy4))
assert len(s2.poles_locations) == len(s3.poles_locations) == 0
assert len(s4.poles_locations) == 2
assert np.allclose(np.abs(s4.poles_locations), np.pi / 2)
with warns(
UserWarning,
match="NumPy is unable to evaluate with complex numbers some of",
test_stacklevel=False,
):
s1 = LineOver1DRangeSeries(frac(x), (x, -10, 10),
adaptive=False, n=1000, detect_poles=False)
s2 = LineOver1DRangeSeries(frac(x), (x, -10, 10),
adaptive=False, n=1000, detect_poles=True, eps=0.05)
s3 = LineOver1DRangeSeries(frac(x), (x, -10, 10),
adaptive=False, n=1000, detect_poles="symbolic")
xx1, yy1 = s1.get_data()
xx2, yy2 = s2.get_data()
xx3, yy3 = s3.get_data()
assert np.allclose(xx1, xx2) and np.allclose(xx1, xx3)
assert not np.any(np.isnan(yy1))
assert np.any(np.isnan(yy2)) and np.any(np.isnan(yy2))
assert not np.allclose(yy1, yy2, equal_nan=True)
# The poles below are actually step discontinuities.
assert len(s3.poles_locations) == 21
s1 = LineOver1DRangeSeries(tan(u * x), (x, -pi, pi), params={u: 1},
adaptive=False, n=1000, detect_poles=False)
xx1, yy1 = s1.get_data()
s2 = LineOver1DRangeSeries(tan(u * x), (x, -pi, pi), params={u: 1},
adaptive=False, n=1000, detect_poles=True, eps=0.01)
xx2, yy2 = s2.get_data()
# eps is too small: doesn't detect any poles
s3 = LineOver1DRangeSeries(tan(u * x), (x, -pi, pi), params={u: 1},
adaptive=False, n=1000, detect_poles=True, eps=1e-06)
xx3, yy3 = s3.get_data()
s4 = LineOver1DRangeSeries(tan(u * x), (x, -pi, pi), params={u: 1},
adaptive=False, n=1000, detect_poles="symbolic")
xx4, yy4 = s4.get_data()
assert np.allclose(xx1, xx2) and np.allclose(xx1, xx3) and np.allclose(xx1, xx4)
assert not np.any(np.isnan(yy1))
assert not np.any(np.isnan(yy3))
assert np.any(np.isnan(yy2))
assert np.any(np.isnan(yy4))
assert len(s2.poles_locations) == len(s3.poles_locations) == 0
assert len(s4.poles_locations) == 2
assert np.allclose(np.abs(s4.poles_locations), np.pi / 2)
with warns(
UserWarning,
match="NumPy is unable to evaluate with complex numbers some of",
test_stacklevel=False,
):
u, v = symbols("u, v", real=True)
n = S(1) / 3
f = (u + I * v)**n
r, i = re(f), im(f)
s1 = Parametric2DLineSeries(r.subs(u, -2), i.subs(u, -2), (v, -2, 2),
adaptive=False, n=1000, detect_poles=False)
s2 = Parametric2DLineSeries(r.subs(u, -2), i.subs(u, -2), (v, -2, 2),
adaptive=False, n=1000, detect_poles=True)
with ignore_warnings(RuntimeWarning):
xx1, yy1, pp1 = s1.get_data()
assert not np.isnan(yy1).any()
xx2, yy2, pp2 = s2.get_data()
assert np.isnan(yy2).any()
with warns(
UserWarning,
match="NumPy is unable to evaluate with complex numbers some of",
test_stacklevel=False,
):
f = (x * u + x * I * v)**n
r, i = re(f), im(f)
s1 = Parametric2DLineSeries(r.subs(u, -2), i.subs(u, -2),
(v, -2, 2), params={x: 1},
adaptive=False, n1=1000, detect_poles=False)
s2 = Parametric2DLineSeries(r.subs(u, -2), i.subs(u, -2),
(v, -2, 2), params={x: 1},
adaptive=False, n1=1000, detect_poles=True)
with ignore_warnings(RuntimeWarning):
xx1, yy1, pp1 = s1.get_data()
assert not np.isnan(yy1).any()
xx2, yy2, pp2 = s2.get_data()
assert np.isnan(yy2).any()
def test_number_discretization_points():
# verify that the different ways to set the number of discretization
# points are consistent with each other.
if not np:
skip("numpy not installed.")
x, y, z = symbols("x:z")
for pt in [LineOver1DRangeSeries, Parametric2DLineSeries,
Parametric3DLineSeries]:
kw1 = _set_discretization_points({"n": 10}, pt)
kw2 = _set_discretization_points({"n": [10, 20, 30]}, pt)
kw3 = _set_discretization_points({"n1": 10}, pt)
assert all(("n1" in kw) and kw["n1"] == 10 for kw in [kw1, kw2, kw3])
for pt in [SurfaceOver2DRangeSeries, ContourSeries, ParametricSurfaceSeries,
ImplicitSeries]:
kw1 = _set_discretization_points({"n": 10}, pt)
kw2 = _set_discretization_points({"n": [10, 20, 30]}, pt)
kw3 = _set_discretization_points({"n1": 10, "n2": 20}, pt)
assert kw1["n1"] == kw1["n2"] == 10
assert all((kw["n1"] == 10) and (kw["n2"] == 20) for kw in [kw2, kw3])
# verify that line-related series can deal with large float number of
# discretization points
LineOver1DRangeSeries(cos(x), (x, -5, 5), adaptive=False, n=1e04).get_data()
def test_list2dseries():
if not np:
skip("numpy not installed.")
xx = np.linspace(-3, 3, 10)
yy1 = np.cos(xx)
yy2 = np.linspace(-3, 3, 20)
# same number of elements: everything is fine
s = List2DSeries(xx, yy1)
assert not s.is_parametric
# different number of elements: error
raises(ValueError, lambda: List2DSeries(xx, yy2))
# no color func: returns only x, y components and s in not parametric
s = List2DSeries(xx, yy1)
xxs, yys = s.get_data()
assert np.allclose(xx, xxs)
assert np.allclose(yy1, yys)
assert not s.is_parametric
def test_interactive_vs_noninteractive():
# verify that if a *Series class receives a `params` dictionary, it sets
# is_interactive=True
x, y, z, u, v = symbols("x, y, z, u, v")
s = LineOver1DRangeSeries(cos(x), (x, -5, 5))
assert not s.is_interactive
s = LineOver1DRangeSeries(u * cos(x), (x, -5, 5), params={u: 1})
assert s.is_interactive
s = Parametric2DLineSeries(cos(x), sin(x), (x, -5, 5))
assert not s.is_interactive
s = Parametric2DLineSeries(u * cos(x), u * sin(x), (x, -5, 5),
params={u: 1})
assert s.is_interactive
s = Parametric3DLineSeries(cos(x), sin(x), x, (x, -5, 5))
assert not s.is_interactive
s = Parametric3DLineSeries(u * cos(x), u * sin(x), x, (x, -5, 5),
params={u: 1})
assert s.is_interactive
s = SurfaceOver2DRangeSeries(cos(x * y), (x, -5, 5), (y, -5, 5))
assert not s.is_interactive
s = SurfaceOver2DRangeSeries(u * cos(x * y), (x, -5, 5), (y, -5, 5),
params={u: 1})
assert s.is_interactive
s = ContourSeries(cos(x * y), (x, -5, 5), (y, -5, 5))
assert not s.is_interactive
s = ContourSeries(u * cos(x * y), (x, -5, 5), (y, -5, 5),
params={u: 1})
assert s.is_interactive
s = ParametricSurfaceSeries(u * cos(v), v * sin(u), u + v,
(u, -5, 5), (v, -5, 5))
assert not s.is_interactive
s = ParametricSurfaceSeries(u * cos(v * x), v * sin(u), u + v,
(u, -5, 5), (v, -5, 5), params={x: 1})
assert s.is_interactive
def test_lin_log_scale():
# Verify that data series create the correct spacing in the data.
if not np:
skip("numpy not installed.")
x, y, z = symbols("x, y, z")
s = LineOver1DRangeSeries(x, (x, 1, 10), adaptive=False, n=50,
xscale="linear")
xx, _ = s.get_data()
assert np.isclose(xx[1] - xx[0], xx[-1] - xx[-2])
s = LineOver1DRangeSeries(x, (x, 1, 10), adaptive=False, n=50,
xscale="log")
xx, _ = s.get_data()
assert not np.isclose(xx[1] - xx[0], xx[-1] - xx[-2])
s = Parametric2DLineSeries(
cos(x), sin(x), (x, pi / 2, 1.5 * pi), adaptive=False, n=50,
xscale="linear")
_, _, param = s.get_data()
assert np.isclose(param[1] - param[0], param[-1] - param[-2])
s = Parametric2DLineSeries(
cos(x), sin(x), (x, pi / 2, 1.5 * pi), adaptive=False, n=50,
xscale="log")
_, _, param = s.get_data()
assert not np.isclose(param[1] - param[0], param[-1] - param[-2])
s = Parametric3DLineSeries(
cos(x), sin(x), x, (x, pi / 2, 1.5 * pi), adaptive=False, n=50,
xscale="linear")
_, _, _, param = s.get_data()
assert np.isclose(param[1] - param[0], param[-1] - param[-2])
s = Parametric3DLineSeries(
cos(x), sin(x), x, (x, pi / 2, 1.5 * pi), adaptive=False, n=50,
xscale="log")
_, _, _, param = s.get_data()
assert not np.isclose(param[1] - param[0], param[-1] - param[-2])
s = SurfaceOver2DRangeSeries(
cos(x ** 2 + y ** 2), (x, 1, 5), (y, 1, 5), n=10,
xscale="linear", yscale="linear")
xx, yy, _ = s.get_data()
assert np.isclose(xx[0, 1] - xx[0, 0], xx[0, -1] - xx[0, -2])
assert np.isclose(yy[1, 0] - yy[0, 0], yy[-1, 0] - yy[-2, 0])
s = SurfaceOver2DRangeSeries(
cos(x ** 2 + y ** 2), (x, 1, 5), (y, 1, 5), n=10,
xscale="log", yscale="log")
xx, yy, _ = s.get_data()
assert not np.isclose(xx[0, 1] - xx[0, 0], xx[0, -1] - xx[0, -2])
assert not np.isclose(yy[1, 0] - yy[0, 0], yy[-1, 0] - yy[-2, 0])
s = ImplicitSeries(
cos(x ** 2 + y ** 2) > 0, (x, 1, 5), (y, 1, 5),
n1=10, n2=10, xscale="linear", yscale="linear", adaptive=False)
xx, yy, _, _ = s.get_data()
assert np.isclose(xx[0, 1] - xx[0, 0], xx[0, -1] - xx[0, -2])
assert np.isclose(yy[1, 0] - yy[0, 0], yy[-1, 0] - yy[-2, 0])
s = ImplicitSeries(
cos(x ** 2 + y ** 2) > 0, (x, 1, 5), (y, 1, 5),
n=10, xscale="log", yscale="log", adaptive=False)
xx, yy, _, _ = s.get_data()
assert not np.isclose(xx[0, 1] - xx[0, 0], xx[0, -1] - xx[0, -2])
assert not np.isclose(yy[1, 0] - yy[0, 0], yy[-1, 0] - yy[-2, 0])
def test_rendering_kw():
# verify that each series exposes the `rendering_kw` attribute
if not np:
skip("numpy not installed.")
u, v, x, y, z = symbols("u, v, x:z")
s = List2DSeries([1, 2, 3], [4, 5, 6])
assert isinstance(s.rendering_kw, dict)
s = LineOver1DRangeSeries(1, (x, -5, 5))
assert isinstance(s.rendering_kw, dict)
s = Parametric2DLineSeries(sin(x), cos(x), (x, 0, pi))
assert isinstance(s.rendering_kw, dict)
s = Parametric3DLineSeries(cos(x), sin(x), x, (x, 0, 2 * pi))
assert isinstance(s.rendering_kw, dict)
s = SurfaceOver2DRangeSeries(x + y, (x, -2, 2), (y, -3, 3))
assert isinstance(s.rendering_kw, dict)
s = ContourSeries(x + y, (x, -2, 2), (y, -3, 3))
assert isinstance(s.rendering_kw, dict)
s = ParametricSurfaceSeries(1, x, y, (x, 0, 1), (y, 0, 1))
assert isinstance(s.rendering_kw, dict)
def test_data_shape():
# Verify that the series produces the correct data shape when the input
# expression is a number.
if not np:
skip("numpy not installed.")
u, x, y, z = symbols("u, x:z")
# scalar expression: it should return a numpy ones array
s = LineOver1DRangeSeries(1, (x, -5, 5))
xx, yy = s.get_data()
assert len(xx) == len(yy)
assert np.all(yy == 1)
s = LineOver1DRangeSeries(1, (x, -5, 5), adaptive=False, n=10)
xx, yy = s.get_data()
assert len(xx) == len(yy) == 10
assert np.all(yy == 1)
s = Parametric2DLineSeries(sin(x), 1, (x, 0, pi))
xx, yy, param = s.get_data()
assert (len(xx) == len(yy)) and (len(xx) == len(param))
assert np.all(yy == 1)
s = Parametric2DLineSeries(1, sin(x), (x, 0, pi))
xx, yy, param = s.get_data()
assert (len(xx) == len(yy)) and (len(xx) == len(param))
assert np.all(xx == 1)
s = Parametric2DLineSeries(sin(x), 1, (x, 0, pi), adaptive=False)
xx, yy, param = s.get_data()
assert (len(xx) == len(yy)) and (len(xx) == len(param))
assert np.all(yy == 1)
s = Parametric2DLineSeries(1, sin(x), (x, 0, pi), adaptive=False)
xx, yy, param = s.get_data()
assert (len(xx) == len(yy)) and (len(xx) == len(param))
assert np.all(xx == 1)
s = Parametric3DLineSeries(cos(x), sin(x), 1, (x, 0, 2 * pi))
xx, yy, zz, param = s.get_data()
assert (len(xx) == len(yy)) and (len(xx) == len(zz)) and (len(xx) == len(param))
assert np.all(zz == 1)
s = Parametric3DLineSeries(cos(x), 1, x, (x, 0, 2 * pi))
xx, yy, zz, param = s.get_data()
assert (len(xx) == len(yy)) and (len(xx) == len(zz)) and (len(xx) == len(param))
assert np.all(yy == 1)
s = Parametric3DLineSeries(1, sin(x), x, (x, 0, 2 * pi))
xx, yy, zz, param = s.get_data()
assert (len(xx) == len(yy)) and (len(xx) == len(zz)) and (len(xx) == len(param))
assert np.all(xx == 1)
s = SurfaceOver2DRangeSeries(1, (x, -2, 2), (y, -3, 3))
xx, yy, zz = s.get_data()
assert (xx.shape == yy.shape) and (xx.shape == zz.shape)
assert np.all(zz == 1)
s = ParametricSurfaceSeries(1, x, y, (x, 0, 1), (y, 0, 1))
xx, yy, zz, uu, vv = s.get_data()
assert xx.shape == yy.shape == zz.shape == uu.shape == vv.shape
assert np.all(xx == 1)
s = ParametricSurfaceSeries(1, 1, y, (x, 0, 1), (y, 0, 1))
xx, yy, zz, uu, vv = s.get_data()
assert xx.shape == yy.shape == zz.shape == uu.shape == vv.shape
assert np.all(yy == 1)
s = ParametricSurfaceSeries(x, 1, 1, (x, 0, 1), (y, 0, 1))
xx, yy, zz, uu, vv = s.get_data()
assert xx.shape == yy.shape == zz.shape == uu.shape == vv.shape
assert np.all(zz == 1)
def test_only_integers():
if not np:
skip("numpy not installed.")
x, y, u, v = symbols("x, y, u, v")
s = LineOver1DRangeSeries(sin(x), (x, -5.5, 4.5), "",
adaptive=False, only_integers=True)
xx, _ = s.get_data()
assert len(xx) == 10
assert xx[0] == -5 and xx[-1] == 4
s = Parametric2DLineSeries(cos(x), sin(x), (x, 0, 2 * pi), "",
adaptive=False, only_integers=True)
_, _, p = s.get_data()
assert len(p) == 7
assert p[0] == 0 and p[-1] == 6
s = Parametric3DLineSeries(cos(x), sin(x), x, (x, 0, 2 * pi), "",
adaptive=False, only_integers=True)
_, _, _, p = s.get_data()
assert len(p) == 7
assert p[0] == 0 and p[-1] == 6
s = SurfaceOver2DRangeSeries(cos(x**2 + y**2), (x, -5.5, 5.5),
(y, -3.5, 3.5), "",
adaptive=False, only_integers=True)
xx, yy, _ = s.get_data()
assert xx.shape == yy.shape == (7, 11)
assert np.allclose(xx[:, 0] - (-5) * np.ones(7), 0)
assert np.allclose(xx[0, :] - np.linspace(-5, 5, 11), 0)
assert np.allclose(yy[:, 0] - np.linspace(-3, 3, 7), 0)
assert np.allclose(yy[0, :] - (-3) * np.ones(11), 0)
r = 2 + sin(7 * u + 5 * v)
expr = (
r * cos(u) * sin(v),
r * sin(u) * sin(v),
r * cos(v)
)
s = ParametricSurfaceSeries(*expr, (u, 0, 2 * pi), (v, 0, pi), "",
adaptive=False, only_integers=True)
xx, yy, zz, uu, vv = s.get_data()
assert xx.shape == yy.shape == zz.shape == uu.shape == vv.shape == (4, 7)
# only_integers also works with scalar expressions
s = LineOver1DRangeSeries(1, (x, -5.5, 4.5), "",
adaptive=False, only_integers=True)
xx, _ = s.get_data()
assert len(xx) == 10
assert xx[0] == -5 and xx[-1] == 4
s = Parametric2DLineSeries(cos(x), 1, (x, 0, 2 * pi), "",
adaptive=False, only_integers=True)
_, _, p = s.get_data()
assert len(p) == 7
assert p[0] == 0 and p[-1] == 6
s = SurfaceOver2DRangeSeries(1, (x, -5.5, 5.5), (y, -3.5, 3.5), "",
adaptive=False, only_integers=True)
xx, yy, _ = s.get_data()
assert xx.shape == yy.shape == (7, 11)
assert np.allclose(xx[:, 0] - (-5) * np.ones(7), 0)
assert np.allclose(xx[0, :] - np.linspace(-5, 5, 11), 0)
assert np.allclose(yy[:, 0] - np.linspace(-3, 3, 7), 0)
assert np.allclose(yy[0, :] - (-3) * np.ones(11), 0)
r = 2 + sin(7 * u + 5 * v)
expr = (
r * cos(u) * sin(v),
1,
r * cos(v)
)
s = ParametricSurfaceSeries(*expr, (u, 0, 2 * pi), (v, 0, pi), "",
adaptive=False, only_integers=True)
xx, yy, zz, uu, vv = s.get_data()
assert xx.shape == yy.shape == zz.shape == uu.shape == vv.shape == (4, 7)
def test_is_point_is_filled():
# verify that `is_point` and `is_filled` are attributes and that they
# they receive the correct values
if not np:
skip("numpy not installed.")
x, u = symbols("x, u")
s = LineOver1DRangeSeries(cos(x), (x, -5, 5), "",
is_point=False, is_filled=True)
assert (not s.is_point) and s.is_filled
s = LineOver1DRangeSeries(cos(x), (x, -5, 5), "",
is_point=True, is_filled=False)
assert s.is_point and (not s.is_filled)
s = List2DSeries([0, 1, 2], [3, 4, 5],
is_point=False, is_filled=True)
assert (not s.is_point) and s.is_filled
s = List2DSeries([0, 1, 2], [3, 4, 5],
is_point=True, is_filled=False)
assert s.is_point and (not s.is_filled)
s = Parametric2DLineSeries(cos(x), sin(x), (x, -5, 5),
is_point=False, is_filled=True)
assert (not s.is_point) and s.is_filled
s = Parametric2DLineSeries(cos(x), sin(x), (x, -5, 5),
is_point=True, is_filled=False)
assert s.is_point and (not s.is_filled)
s = Parametric3DLineSeries(cos(x), sin(x), x, (x, -5, 5),
is_point=False, is_filled=True)
assert (not s.is_point) and s.is_filled
s = Parametric3DLineSeries(cos(x), sin(x), x, (x, -5, 5),
is_point=True, is_filled=False)
assert s.is_point and (not s.is_filled)
def test_is_filled_2d():
# verify that the is_filled attribute is exposed by the following series
x, y = symbols("x, y")
expr = cos(x**2 + y**2)
ranges = (x, -2, 2), (y, -2, 2)
s = ContourSeries(expr, *ranges)
assert s.is_filled
s = ContourSeries(expr, *ranges, is_filled=True)
assert s.is_filled
s = ContourSeries(expr, *ranges, is_filled=False)
assert not s.is_filled
def test_steps():
if not np:
skip("numpy not installed.")
x, u = symbols("x, u")
def do_test(s1, s2):
if (not s1.is_parametric) and s1.is_2Dline:
xx1, _ = s1.get_data()
xx2, _ = s2.get_data()
elif s1.is_parametric and s1.is_2Dline:
xx1, _, _ = s1.get_data()
xx2, _, _ = s2.get_data()
elif (not s1.is_parametric) and s1.is_3Dline:
xx1, _, _ = s1.get_data()
xx2, _, _ = s2.get_data()
else:
xx1, _, _, _ = s1.get_data()
xx2, _, _, _ = s2.get_data()
assert len(xx1) != len(xx2)
s1 = LineOver1DRangeSeries(cos(x), (x, -5, 5), "",
adaptive=False, n=40, steps=False)
s2 = LineOver1DRangeSeries(cos(x), (x, -5, 5), "",
adaptive=False, n=40, steps=True)
do_test(s1, s2)
s1 = List2DSeries([0, 1, 2], [3, 4, 5], steps=False)
s2 = List2DSeries([0, 1, 2], [3, 4, 5], steps=True)
do_test(s1, s2)
s1 = Parametric2DLineSeries(cos(x), sin(x), (x, -5, 5),
adaptive=False, n=40, steps=False)
s2 = Parametric2DLineSeries(cos(x), sin(x), (x, -5, 5),
adaptive=False, n=40, steps=True)
do_test(s1, s2)
s1 = Parametric3DLineSeries(cos(x), sin(x), x, (x, -5, 5),
adaptive=False, n=40, steps=False)
s2 = Parametric3DLineSeries(cos(x), sin(x), x, (x, -5, 5),
adaptive=False, n=40, steps=True)
do_test(s1, s2)
def test_interactive_data():
# verify that InteractiveSeries produces the same numerical data as their
# corresponding non-interactive series.
if not np:
skip("numpy not installed.")
u, x, y, z = symbols("u, x:z")
def do_test(data1, data2):
assert len(data1) == len(data2)
for d1, d2 in zip(data1, data2):
assert np.allclose(d1, d2)
s1 = LineOver1DRangeSeries(u * cos(x), (x, -5, 5), params={u: 1}, n=50)
s2 = LineOver1DRangeSeries(cos(x), (x, -5, 5), adaptive=False, n=50)
do_test(s1.get_data(), s2.get_data())
s1 = Parametric2DLineSeries(
u * cos(x), u * sin(x), (x, -5, 5), params={u: 1}, n=50)
s2 = Parametric2DLineSeries(cos(x), sin(x), (x, -5, 5),
adaptive=False, n=50)
do_test(s1.get_data(), s2.get_data())
s1 = Parametric3DLineSeries(
u * cos(x), u * sin(x), u * x, (x, -5, 5),
params={u: 1}, n=50)
s2 = Parametric3DLineSeries(cos(x), sin(x), x, (x, -5, 5),
adaptive=False, n=50)
do_test(s1.get_data(), s2.get_data())
s1 = SurfaceOver2DRangeSeries(
u * cos(x ** 2 + y ** 2), (x, -3, 3), (y, -3, 3),
params={u: 1}, n1=50, n2=50,)
s2 = SurfaceOver2DRangeSeries(
cos(x ** 2 + y ** 2), (x, -3, 3), (y, -3, 3),
adaptive=False, n1=50, n2=50)
do_test(s1.get_data(), s2.get_data())
s1 = ParametricSurfaceSeries(
u * cos(x + y), sin(x + y), x - y, (x, -3, 3), (y, -3, 3),
params={u: 1}, n1=50, n2=50,)
s2 = ParametricSurfaceSeries(
cos(x + y), sin(x + y), x - y, (x, -3, 3), (y, -3, 3),
adaptive=False, n1=50, n2=50,)
do_test(s1.get_data(), s2.get_data())
# real part of a complex function evaluated over a real line with numpy
expr = re((z ** 2 + 1) / (z ** 2 - 1))
s1 = LineOver1DRangeSeries(u * expr, (z, -3, 3), adaptive=False, n=50,
modules=None, params={u: 1})
s2 = LineOver1DRangeSeries(expr, (z, -3, 3), adaptive=False, n=50,
modules=None)
do_test(s1.get_data(), s2.get_data())
# real part of a complex function evaluated over a real line with mpmath
expr = re((z ** 2 + 1) / (z ** 2 - 1))
s1 = LineOver1DRangeSeries(u * expr, (z, -3, 3), n=50, modules="mpmath",
params={u: 1})
s2 = LineOver1DRangeSeries(expr, (z, -3, 3),
adaptive=False, n=50, modules="mpmath")
do_test(s1.get_data(), s2.get_data())
def test_list2dseries_interactive():
if not np:
skip("numpy not installed.")
x, y, u = symbols("x, y, u")
s = List2DSeries([1, 2, 3], [1, 2, 3])
assert not s.is_interactive
# symbolic expressions as coordinates, but no ``params``
raises(ValueError, lambda: List2DSeries([cos(x)], [sin(x)]))
# too few parameters
raises(ValueError,
lambda: List2DSeries([cos(x), y], [sin(x), 2], params={u: 1}))
s = List2DSeries([cos(x)], [sin(x)], params={x: 1})
assert s.is_interactive
s = List2DSeries([x, 2, 3, 4], [4, 3, 2, x], params={x: 3})
xx, yy = s.get_data()
assert np.allclose(xx, [3, 2, 3, 4])
assert np.allclose(yy, [4, 3, 2, 3])
assert not s.is_parametric
# numeric lists + params is present -> interactive series and
# lists are converted to Tuple.
s = List2DSeries([1, 2, 3], [1, 2, 3], params={x: 1})
assert s.is_interactive
assert isinstance(s.list_x, Tuple)
assert isinstance(s.list_y, Tuple)
def test_mpmath():
# test that the argument of complex functions evaluated with mpmath
# might be different than the one computed with Numpy (different
# behaviour at branch cuts)
if not np:
skip("numpy not installed.")
z, u = symbols("z, u")
s1 = LineOver1DRangeSeries(im(sqrt(-z)), (z, 1e-03, 5),
adaptive=True, modules=None, force_real_eval=True)
s2 = LineOver1DRangeSeries(im(sqrt(-z)), (z, 1e-03, 5),
adaptive=True, modules="mpmath", force_real_eval=True)
xx1, yy1 = s1.get_data()
xx2, yy2 = s2.get_data()
assert np.all(yy1 < 0)
assert np.all(yy2 > 0)
s1 = LineOver1DRangeSeries(im(sqrt(-z)), (z, -5, 5),
adaptive=False, n=20, modules=None, force_real_eval=True)
s2 = LineOver1DRangeSeries(im(sqrt(-z)), (z, -5, 5),
adaptive=False, n=20, modules="mpmath", force_real_eval=True)
xx1, yy1 = s1.get_data()
xx2, yy2 = s2.get_data()
assert np.allclose(xx1, xx2)
assert not np.allclose(yy1, yy2)
def test_str():
u, x, y, z = symbols("u, x:z")
s = LineOver1DRangeSeries(cos(x), (x, -4, 3))
assert str(s) == "cartesian line: cos(x) for x over (-4.0, 3.0)"
d = {"return": "real"}
s = LineOver1DRangeSeries(cos(x), (x, -4, 3), **d)
assert str(s) == "cartesian line: re(cos(x)) for x over (-4.0, 3.0)"
d = {"return": "imag"}
s = LineOver1DRangeSeries(cos(x), (x, -4, 3), **d)
assert str(s) == "cartesian line: im(cos(x)) for x over (-4.0, 3.0)"
d = {"return": "abs"}
s = LineOver1DRangeSeries(cos(x), (x, -4, 3), **d)
assert str(s) == "cartesian line: abs(cos(x)) for x over (-4.0, 3.0)"
d = {"return": "arg"}
s = LineOver1DRangeSeries(cos(x), (x, -4, 3), **d)
assert str(s) == "cartesian line: arg(cos(x)) for x over (-4.0, 3.0)"
s = LineOver1DRangeSeries(cos(u * x), (x, -4, 3), params={u: 1})
assert str(s) == "interactive cartesian line: cos(u*x) for x over (-4.0, 3.0) and parameters (u,)"
s = LineOver1DRangeSeries(cos(u * x), (x, -u, 3*y), params={u: 1, y: 1})
assert str(s) == "interactive cartesian line: cos(u*x) for x over (-u, 3*y) and parameters (u, y)"
s = Parametric2DLineSeries(cos(x), sin(x), (x, -4, 3))
assert str(s) == "parametric cartesian line: (cos(x), sin(x)) for x over (-4.0, 3.0)"
s = Parametric2DLineSeries(cos(u * x), sin(x), (x, -4, 3), params={u: 1})
assert str(s) == "interactive parametric cartesian line: (cos(u*x), sin(x)) for x over (-4.0, 3.0) and parameters (u,)"
s = Parametric2DLineSeries(cos(u * x), sin(x), (x, -u, 3*y), params={u: 1, y:1})
assert str(s) == "interactive parametric cartesian line: (cos(u*x), sin(x)) for x over (-u, 3*y) and parameters (u, y)"
s = Parametric3DLineSeries(cos(x), sin(x), x, (x, -4, 3))
assert str(s) == "3D parametric cartesian line: (cos(x), sin(x), x) for x over (-4.0, 3.0)"
s = Parametric3DLineSeries(cos(u*x), sin(x), x, (x, -4, 3), params={u: 1})
assert str(s) == "interactive 3D parametric cartesian line: (cos(u*x), sin(x), x) for x over (-4.0, 3.0) and parameters (u,)"
s = Parametric3DLineSeries(cos(u*x), sin(x), x, (x, -u, 3*y), params={u: 1, y: 1})
assert str(s) == "interactive 3D parametric cartesian line: (cos(u*x), sin(x), x) for x over (-u, 3*y) and parameters (u, y)"
s = SurfaceOver2DRangeSeries(cos(x * y), (x, -4, 3), (y, -2, 5))
assert str(s) == "cartesian surface: cos(x*y) for x over (-4.0, 3.0) and y over (-2.0, 5.0)"
s = SurfaceOver2DRangeSeries(cos(u * x * y), (x, -4, 3), (y, -2, 5), params={u: 1})
assert str(s) == "interactive cartesian surface: cos(u*x*y) for x over (-4.0, 3.0) and y over (-2.0, 5.0) and parameters (u,)"
s = SurfaceOver2DRangeSeries(cos(u * x * y), (x, -4*u, 3), (y, -2, 5*u), params={u: 1})
assert str(s) == "interactive cartesian surface: cos(u*x*y) for x over (-4*u, 3.0) and y over (-2.0, 5*u) and parameters (u,)"
s = ContourSeries(cos(x * y), (x, -4, 3), (y, -2, 5))
assert str(s) == "contour: cos(x*y) for x over (-4.0, 3.0) and y over (-2.0, 5.0)"
s = ContourSeries(cos(u * x * y), (x, -4, 3), (y, -2, 5), params={u: 1})
assert str(s) == "interactive contour: cos(u*x*y) for x over (-4.0, 3.0) and y over (-2.0, 5.0) and parameters (u,)"
s = ParametricSurfaceSeries(cos(x * y), sin(x * y), x * y,
(x, -4, 3), (y, -2, 5))
assert str(s) == "parametric cartesian surface: (cos(x*y), sin(x*y), x*y) for x over (-4.0, 3.0) and y over (-2.0, 5.0)"
s = ParametricSurfaceSeries(cos(u * x * y), sin(x * y), x * y,
(x, -4, 3), (y, -2, 5), params={u: 1})
assert str(s) == "interactive parametric cartesian surface: (cos(u*x*y), sin(x*y), x*y) for x over (-4.0, 3.0) and y over (-2.0, 5.0) and parameters (u,)"
s = ImplicitSeries(x < y, (x, -5, 4), (y, -3, 2))
assert str(s) == "Implicit expression: x < y for x over (-5.0, 4.0) and y over (-3.0, 2.0)"
def test_use_cm():
# verify that the `use_cm` attribute is implemented.
if not np:
skip("numpy not installed.")
u, x, y, z = symbols("u, x:z")
s = List2DSeries([1, 2, 3, 4], [5, 6, 7, 8], use_cm=True)
assert s.use_cm
s = List2DSeries([1, 2, 3, 4], [5, 6, 7, 8], use_cm=False)
assert not s.use_cm
s = Parametric2DLineSeries(cos(x), sin(x), (x, -4, 3), use_cm=True)
assert s.use_cm
s = Parametric2DLineSeries(cos(x), sin(x), (x, -4, 3), use_cm=False)
assert not s.use_cm
s = Parametric3DLineSeries(cos(x), sin(x), x, (x, -4, 3),
use_cm=True)
assert s.use_cm
s = Parametric3DLineSeries(cos(x), sin(x), x, (x, -4, 3),
use_cm=False)
assert not s.use_cm
s = SurfaceOver2DRangeSeries(cos(x * y), (x, -4, 3), (y, -2, 5),
use_cm=True)
assert s.use_cm
s = SurfaceOver2DRangeSeries(cos(x * y), (x, -4, 3), (y, -2, 5),
use_cm=False)
assert not s.use_cm
s = ParametricSurfaceSeries(cos(x * y), sin(x * y), x * y,
(x, -4, 3), (y, -2, 5), use_cm=True)
assert s.use_cm
s = ParametricSurfaceSeries(cos(x * y), sin(x * y), x * y,
(x, -4, 3), (y, -2, 5), use_cm=False)
assert not s.use_cm
def test_surface_use_cm():
# verify that SurfaceOver2DRangeSeries and ParametricSurfaceSeries get
# the same value for use_cm
x, y, u, v = symbols("x, y, u, v")
# they read the same value from default settings
s1 = SurfaceOver2DRangeSeries(cos(x**2 + y**2), (x, -2, 2), (y, -2, 2))
s2 = ParametricSurfaceSeries(u * cos(v), u * sin(v), u,
(u, 0, 1), (v, 0 , 2*pi))
assert s1.use_cm == s2.use_cm
# they get the same value
s1 = SurfaceOver2DRangeSeries(cos(x**2 + y**2), (x, -2, 2), (y, -2, 2),
use_cm=False)
s2 = ParametricSurfaceSeries(u * cos(v), u * sin(v), u,
(u, 0, 1), (v, 0 , 2*pi), use_cm=False)
assert s1.use_cm == s2.use_cm
# they get the same value
s1 = SurfaceOver2DRangeSeries(cos(x**2 + y**2), (x, -2, 2), (y, -2, 2),
use_cm=True)
s2 = ParametricSurfaceSeries(u * cos(v), u * sin(v), u,
(u, 0, 1), (v, 0 , 2*pi), use_cm=True)
assert s1.use_cm == s2.use_cm
def test_sums():
# test that data series are able to deal with sums
if not np:
skip("numpy not installed.")
x, y, u = symbols("x, y, u")
def do_test(data1, data2):
assert len(data1) == len(data2)
for d1, d2 in zip(data1, data2):
assert np.allclose(d1, d2)
s = LineOver1DRangeSeries(Sum(1 / x ** y, (x, 1, 1000)), (y, 2, 10),
adaptive=False, only_integers=True)
xx, yy = s.get_data()
s1 = LineOver1DRangeSeries(Sum(1 / x, (x, 1, y)), (y, 2, 10),
adaptive=False, only_integers=True)
xx1, yy1 = s1.get_data()
s2 = LineOver1DRangeSeries(Sum(u / x, (x, 1, y)), (y, 2, 10),
params={u: 1}, only_integers=True)
xx2, yy2 = s2.get_data()
xx1 = xx1.astype(float)
xx2 = xx2.astype(float)
do_test([xx1, yy1], [xx2, yy2])
s = LineOver1DRangeSeries(Sum(1 / x, (x, 1, y)), (y, 2, 10),
adaptive=True)
with warns(
UserWarning,
match="The evaluation with NumPy/SciPy failed",
test_stacklevel=False,
):
raises(TypeError, lambda: s.get_data())
def test_apply_transforms():
# verify that transformation functions get applied to the output
# of data series
if not np:
skip("numpy not installed.")
x, y, z, u, v = symbols("x:z, u, v")
s1 = LineOver1DRangeSeries(cos(x), (x, -2*pi, 2*pi), adaptive=False, n=10)
s2 = LineOver1DRangeSeries(cos(x), (x, -2*pi, 2*pi), adaptive=False, n=10,
tx=np.rad2deg)
s3 = LineOver1DRangeSeries(cos(x), (x, -2*pi, 2*pi), adaptive=False, n=10,
ty=np.rad2deg)
s4 = LineOver1DRangeSeries(cos(x), (x, -2*pi, 2*pi), adaptive=False, n=10,
tx=np.rad2deg, ty=np.rad2deg)
x1, y1 = s1.get_data()
x2, y2 = s2.get_data()
x3, y3 = s3.get_data()
x4, y4 = s4.get_data()
assert np.isclose(x1[0], -2*np.pi) and np.isclose(x1[-1], 2*np.pi)
assert (y1.min() < -0.9) and (y1.max() > 0.9)
assert np.isclose(x2[0], -360) and np.isclose(x2[-1], 360)
assert (y2.min() < -0.9) and (y2.max() > 0.9)
assert np.isclose(x3[0], -2*np.pi) and np.isclose(x3[-1], 2*np.pi)
assert (y3.min() < -52) and (y3.max() > 52)
assert np.isclose(x4[0], -360) and np.isclose(x4[-1], 360)
assert (y4.min() < -52) and (y4.max() > 52)
xx = np.linspace(-2*np.pi, 2*np.pi, 10)
yy = np.cos(xx)
s1 = List2DSeries(xx, yy)
s2 = List2DSeries(xx, yy, tx=np.rad2deg, ty=np.rad2deg)
x1, y1 = s1.get_data()
x2, y2 = s2.get_data()
assert np.isclose(x1[0], -2*np.pi) and np.isclose(x1[-1], 2*np.pi)
assert (y1.min() < -0.9) and (y1.max() > 0.9)
assert np.isclose(x2[0], -360) and np.isclose(x2[-1], 360)
assert (y2.min() < -52) and (y2.max() > 52)
s1 = Parametric2DLineSeries(
sin(x), cos(x), (x, -pi, pi), adaptive=False, n=10)
s2 = Parametric2DLineSeries(
sin(x), cos(x), (x, -pi, pi), adaptive=False, n=10,
tx=np.rad2deg, ty=np.rad2deg, tp=np.rad2deg)
x1, y1, a1 = s1.get_data()
x2, y2, a2 = s2.get_data()
assert np.allclose(x1, np.deg2rad(x2))
assert np.allclose(y1, np.deg2rad(y2))
assert np.allclose(a1, np.deg2rad(a2))
s1 = Parametric3DLineSeries(
sin(x), cos(x), x, (x, -pi, pi), adaptive=False, n=10)
s2 = Parametric3DLineSeries(
sin(x), cos(x), x, (x, -pi, pi), adaptive=False, n=10, tp=np.rad2deg)
x1, y1, z1, a1 = s1.get_data()
x2, y2, z2, a2 = s2.get_data()
assert np.allclose(x1, x2)
assert np.allclose(y1, y2)
assert np.allclose(z1, z2)
assert np.allclose(a1, np.deg2rad(a2))
s1 = SurfaceOver2DRangeSeries(
cos(x**2 + y**2), (x, -2*pi, 2*pi), (y, -2*pi, 2*pi),
adaptive=False, n1=10, n2=10)
s2 = SurfaceOver2DRangeSeries(
cos(x**2 + y**2), (x, -2*pi, 2*pi), (y, -2*pi, 2*pi),
adaptive=False, n1=10, n2=10,
tx=np.rad2deg, ty=lambda x: 2*x, tz=lambda x: 3*x)
x1, y1, z1 = s1.get_data()
x2, y2, z2 = s2.get_data()
assert np.allclose(x1, np.deg2rad(x2))
assert np.allclose(y1, y2 / 2)
assert np.allclose(z1, z2 / 3)
s1 = ParametricSurfaceSeries(
u + v, u - v, u * v, (u, 0, 2*pi), (v, 0, pi),
adaptive=False, n1=10, n2=10)
s2 = ParametricSurfaceSeries(
u + v, u - v, u * v, (u, 0, 2*pi), (v, 0, pi),
adaptive=False, n1=10, n2=10,
tx=np.rad2deg, ty=lambda x: 2*x, tz=lambda x: 3*x)
x1, y1, z1, u1, v1 = s1.get_data()
x2, y2, z2, u2, v2 = s2.get_data()
assert np.allclose(x1, np.deg2rad(x2))
assert np.allclose(y1, y2 / 2)
assert np.allclose(z1, z2 / 3)
assert np.allclose(u1, u2)
assert np.allclose(v1, v2)
def test_series_labels():
# verify that series return the correct label, depending on the plot
# type and input arguments. If the user set custom label on a data series,
# it should returned un-modified.
if not np:
skip("numpy not installed.")
x, y, z, u, v = symbols("x, y, z, u, v")
wrapper = "$%s$"
expr = cos(x)
s1 = LineOver1DRangeSeries(expr, (x, -2, 2), None)
s2 = LineOver1DRangeSeries(expr, (x, -2, 2), "test")
assert s1.get_label(False) == str(expr)
assert s1.get_label(True) == wrapper % latex(expr)
assert s2.get_label(False) == "test"
assert s2.get_label(True) == "test"
s1 = List2DSeries([0, 1, 2, 3], [0, 1, 2, 3], "test")
assert s1.get_label(False) == "test"
assert s1.get_label(True) == "test"
expr = (cos(x), sin(x))
s1 = Parametric2DLineSeries(*expr, (x, -2, 2), None, use_cm=True)
s2 = Parametric2DLineSeries(*expr, (x, -2, 2), "test", use_cm=True)
s3 = Parametric2DLineSeries(*expr, (x, -2, 2), None, use_cm=False)
s4 = Parametric2DLineSeries(*expr, (x, -2, 2), "test", use_cm=False)
assert s1.get_label(False) == "x"
assert s1.get_label(True) == wrapper % "x"
assert s2.get_label(False) == "test"
assert s2.get_label(True) == "test"
assert s3.get_label(False) == str(expr)
assert s3.get_label(True) == wrapper % latex(expr)
assert s4.get_label(False) == "test"
assert s4.get_label(True) == "test"
expr = (cos(x), sin(x), x)
s1 = Parametric3DLineSeries(*expr, (x, -2, 2), None, use_cm=True)
s2 = Parametric3DLineSeries(*expr, (x, -2, 2), "test", use_cm=True)
s3 = Parametric3DLineSeries(*expr, (x, -2, 2), None, use_cm=False)
s4 = Parametric3DLineSeries(*expr, (x, -2, 2), "test", use_cm=False)
assert s1.get_label(False) == "x"
assert s1.get_label(True) == wrapper % "x"
assert s2.get_label(False) == "test"
assert s2.get_label(True) == "test"
assert s3.get_label(False) == str(expr)
assert s3.get_label(True) == wrapper % latex(expr)
assert s4.get_label(False) == "test"
assert s4.get_label(True) == "test"
expr = cos(x**2 + y**2)
s1 = SurfaceOver2DRangeSeries(expr, (x, -2, 2), (y, -2, 2), None)
s2 = SurfaceOver2DRangeSeries(expr, (x, -2, 2), (y, -2, 2), "test")
assert s1.get_label(False) == str(expr)
assert s1.get_label(True) == wrapper % latex(expr)
assert s2.get_label(False) == "test"
assert s2.get_label(True) == "test"
expr = (cos(x - y), sin(x + y), x - y)
s1 = ParametricSurfaceSeries(*expr, (x, -2, 2), (y, -2, 2), None)
s2 = ParametricSurfaceSeries(*expr, (x, -2, 2), (y, -2, 2), "test")
assert s1.get_label(False) == str(expr)
assert s1.get_label(True) == wrapper % latex(expr)
assert s2.get_label(False) == "test"
assert s2.get_label(True) == "test"
expr = Eq(cos(x - y), 0)
s1 = ImplicitSeries(expr, (x, -10, 10), (y, -10, 10), None)
s2 = ImplicitSeries(expr, (x, -10, 10), (y, -10, 10), "test")
assert s1.get_label(False) == str(expr)
assert s1.get_label(True) == wrapper % latex(expr)
assert s2.get_label(False) == "test"
assert s2.get_label(True) == "test"
def test_is_polar_2d_parametric():
# verify that Parametric2DLineSeries isable to apply polar discretization,
# which is used when polar_plot is executed with polar_axis=True
if not np:
skip("numpy not installed.")
t, u = symbols("t u")
# NOTE: a sufficiently big n must be provided, or else tests
# are going to fail
# No colormap
f = sin(4 * t)
s1 = Parametric2DLineSeries(f * cos(t), f * sin(t), (t, 0, 2*pi),
adaptive=False, n=10, is_polar=False, use_cm=False)
x1, y1, p1 = s1.get_data()
s2 = Parametric2DLineSeries(f * cos(t), f * sin(t), (t, 0, 2*pi),
adaptive=False, n=10, is_polar=True, use_cm=False)
th, r, p2 = s2.get_data()
assert (not np.allclose(x1, th)) and (not np.allclose(y1, r))
assert np.allclose(p1, p2)
# With colormap
s3 = Parametric2DLineSeries(f * cos(t), f * sin(t), (t, 0, 2*pi),
adaptive=False, n=10, is_polar=False, color_func=lambda t: 2*t)
x3, y3, p3 = s3.get_data()
s4 = Parametric2DLineSeries(f * cos(t), f * sin(t), (t, 0, 2*pi),
adaptive=False, n=10, is_polar=True, color_func=lambda t: 2*t)
th4, r4, p4 = s4.get_data()
assert np.allclose(p3, p4) and (not np.allclose(p1, p3))
assert np.allclose(x3, x1) and np.allclose(y3, y1)
assert np.allclose(th4, th) and np.allclose(r4, r)
def test_is_polar_3d():
# verify that SurfaceOver2DRangeSeries is able to apply
# polar discretization
if not np:
skip("numpy not installed.")
x, y, t = symbols("x, y, t")
expr = (x**2 - 1)**2
s1 = SurfaceOver2DRangeSeries(expr, (x, 0, 1.5), (y, 0, 2 * pi),
n=10, adaptive=False, is_polar=False)
s2 = SurfaceOver2DRangeSeries(expr, (x, 0, 1.5), (y, 0, 2 * pi),
n=10, adaptive=False, is_polar=True)
x1, y1, z1 = s1.get_data()
x2, y2, z2 = s2.get_data()
x22, y22 = x1 * np.cos(y1), x1 * np.sin(y1)
assert np.allclose(x2, x22)
assert np.allclose(y2, y22)
def test_color_func():
# verify that eval_color_func produces the expected results in order to
# maintain back compatibility with the old sympy.plotting module
if not np:
skip("numpy not installed.")
x, y, z, u, v = symbols("x, y, z, u, v")
# color func: returns x, y, color and s is parametric
xx = np.linspace(-3, 3, 10)
yy1 = np.cos(xx)
s = List2DSeries(xx, yy1, color_func=lambda x, y: 2 * x, use_cm=True)
xxs, yys, col = s.get_data()
assert np.allclose(xx, xxs)
assert np.allclose(yy1, yys)
assert np.allclose(2 * xx, col)
assert s.is_parametric
s = List2DSeries(xx, yy1, color_func=lambda x, y: 2 * x, use_cm=False)
assert len(s.get_data()) == 2
assert not s.is_parametric
s = Parametric2DLineSeries(cos(x), sin(x), (x, 0, 2*pi),
adaptive=False, n=10, color_func=lambda t: t)
xx, yy, col = s.get_data()
assert (not np.allclose(xx, col)) and (not np.allclose(yy, col))
s = Parametric2DLineSeries(cos(x), sin(x), (x, 0, 2*pi),
adaptive=False, n=10, color_func=lambda x, y: x * y)
xx, yy, col = s.get_data()
assert np.allclose(col, xx * yy)
s = Parametric2DLineSeries(cos(x), sin(x), (x, 0, 2*pi),
adaptive=False, n=10, color_func=lambda x, y, t: x * y * t)
xx, yy, col = s.get_data()
assert np.allclose(col, xx * yy * np.linspace(0, 2*np.pi, 10))
s = Parametric3DLineSeries(cos(x), sin(x), x, (x, 0, 2*pi),
adaptive=False, n=10, color_func=lambda t: t)
xx, yy, zz, col = s.get_data()
assert (not np.allclose(xx, col)) and (not np.allclose(yy, col))
s = Parametric3DLineSeries(cos(x), sin(x), x, (x, 0, 2*pi),
adaptive=False, n=10, color_func=lambda x, y, z: x * y * z)
xx, yy, zz, col = s.get_data()
assert np.allclose(col, xx * yy * zz)
s = Parametric3DLineSeries(cos(x), sin(x), x, (x, 0, 2*pi),
adaptive=False, n=10, color_func=lambda x, y, z, t: x * y * z * t)
xx, yy, zz, col = s.get_data()
assert np.allclose(col, xx * yy * zz * np.linspace(0, 2*np.pi, 10))
s = SurfaceOver2DRangeSeries(cos(x**2 + y**2), (x, -2, 2), (y, -2, 2),
adaptive=False, n1=10, n2=10, color_func=lambda x: x)
xx, yy, zz = s.get_data()
col = s.eval_color_func(xx, yy, zz)
assert np.allclose(xx, col)
s = SurfaceOver2DRangeSeries(cos(x**2 + y**2), (x, -2, 2), (y, -2, 2),
adaptive=False, n1=10, n2=10, color_func=lambda x, y: x * y)
xx, yy, zz = s.get_data()
col = s.eval_color_func(xx, yy, zz)
assert np.allclose(xx * yy, col)
s = SurfaceOver2DRangeSeries(cos(x**2 + y**2), (x, -2, 2), (y, -2, 2),
adaptive=False, n1=10, n2=10, color_func=lambda x, y, z: x * y * z)
xx, yy, zz = s.get_data()
col = s.eval_color_func(xx, yy, zz)
assert np.allclose(xx * yy * zz, col)
s = ParametricSurfaceSeries(1, x, y, (x, 0, 1), (y, 0, 1), adaptive=False,
n1=10, n2=10, color_func=lambda u:u)
xx, yy, zz, uu, vv = s.get_data()
col = s.eval_color_func(xx, yy, zz, uu, vv)
assert np.allclose(uu, col)
s = ParametricSurfaceSeries(1, x, y, (x, 0, 1), (y, 0, 1), adaptive=False,
n1=10, n2=10, color_func=lambda u, v: u * v)
xx, yy, zz, uu, vv = s.get_data()
col = s.eval_color_func(xx, yy, zz, uu, vv)
assert np.allclose(uu * vv, col)
s = ParametricSurfaceSeries(1, x, y, (x, 0, 1), (y, 0, 1), adaptive=False,
n1=10, n2=10, color_func=lambda x, y, z: x * y * z)
xx, yy, zz, uu, vv = s.get_data()
col = s.eval_color_func(xx, yy, zz, uu, vv)
assert np.allclose(xx * yy * zz, col)
s = ParametricSurfaceSeries(1, x, y, (x, 0, 1), (y, 0, 1), adaptive=False,
n1=10, n2=10, color_func=lambda x, y, z, u, v: x * y * z * u * v)
xx, yy, zz, uu, vv = s.get_data()
col = s.eval_color_func(xx, yy, zz, uu, vv)
assert np.allclose(xx * yy * zz * uu * vv, col)
# Interactive Series
s = List2DSeries([0, 1, 2, x], [x, 2, 3, 4],
color_func=lambda x, y: 2 * x, params={x: 1}, use_cm=True)
xx, yy, col = s.get_data()
assert np.allclose(xx, [0, 1, 2, 1])
assert np.allclose(yy, [1, 2, 3, 4])
assert np.allclose(2 * xx, col)
assert s.is_parametric and s.use_cm
s = List2DSeries([0, 1, 2, x], [x, 2, 3, 4],
color_func=lambda x, y: 2 * x, params={x: 1}, use_cm=False)
assert len(s.get_data()) == 2
assert not s.is_parametric
def test_color_func_scalar_val():
# verify that eval_color_func returns a numpy array even when color_func
# evaluates to a scalar value
if not np:
skip("numpy not installed.")
x, y = symbols("x, y")
s = Parametric2DLineSeries(cos(x), sin(x), (x, 0, 2*pi),
adaptive=False, n=10, color_func=lambda t: 1)
xx, yy, col = s.get_data()
assert np.allclose(col, np.ones(xx.shape))
s = Parametric3DLineSeries(cos(x), sin(x), x, (x, 0, 2*pi),
adaptive=False, n=10, color_func=lambda t: 1)
xx, yy, zz, col = s.get_data()
assert np.allclose(col, np.ones(xx.shape))
s = SurfaceOver2DRangeSeries(cos(x**2 + y**2), (x, -2, 2), (y, -2, 2),
adaptive=False, n1=10, n2=10, color_func=lambda x: 1)
xx, yy, zz = s.get_data()
assert np.allclose(s.eval_color_func(xx), np.ones(xx.shape))
s = ParametricSurfaceSeries(1, x, y, (x, 0, 1), (y, 0, 1), adaptive=False,
n1=10, n2=10, color_func=lambda u: 1)
xx, yy, zz, uu, vv = s.get_data()
col = s.eval_color_func(xx, yy, zz, uu, vv)
assert np.allclose(col, np.ones(xx.shape))
def test_color_func_expression():
# verify that color_func is able to deal with instances of Expr: they will
# be lambdified with the same signature used for the main expression.
if not np:
skip("numpy not installed.")
x, y = symbols("x, y")
s1 = Parametric2DLineSeries(cos(x), sin(x), (x, 0, 2*pi),
color_func=sin(x), adaptive=False, n=10, use_cm=True)
s2 = Parametric2DLineSeries(cos(x), sin(x), (x, 0, 2*pi),
color_func=lambda x: np.cos(x), adaptive=False, n=10, use_cm=True)
# the following statement should not raise errors
d1 = s1.get_data()
assert callable(s1.color_func)
d2 = s2.get_data()
assert not np.allclose(d1[-1], d2[-1])
s = SurfaceOver2DRangeSeries(cos(x**2 + y**2), (x, -pi, pi), (y, -pi, pi),
color_func=sin(x**2 + y**2), adaptive=False, n1=5, n2=5)
# the following statement should not raise errors
s.get_data()
assert callable(s.color_func)
xx = [1, 2, 3, 4, 5]
yy = [1, 2, 3, 4, 5]
raises(TypeError,
lambda : List2DSeries(xx, yy, use_cm=True, color_func=sin(x)))
def test_line_surface_color():
# verify the back-compatibility with the old sympy.plotting module.
# By setting line_color or surface_color to be a callable, it will set
# the color_func attribute.
x, y, z = symbols("x, y, z")
s = LineOver1DRangeSeries(sin(x), (x, -5, 5), adaptive=False, n=10,
line_color=lambda x: x)
assert (s.line_color is None) and callable(s.color_func)
s = Parametric2DLineSeries(cos(x), sin(x), (x, 0, 2*pi),
adaptive=False, n=10, line_color=lambda t: t)
assert (s.line_color is None) and callable(s.color_func)
s = SurfaceOver2DRangeSeries(cos(x**2 + y**2), (x, -2, 2), (y, -2, 2),
n1=10, n2=10, surface_color=lambda x: x)
assert (s.surface_color is None) and callable(s.color_func)
def test_complex_adaptive_false():
# verify that series with adaptive=False is evaluated with discretized
# ranges of type complex.
if not np:
skip("numpy not installed.")
x, y, u = symbols("x y u")
def do_test(data1, data2):
assert len(data1) == len(data2)
for d1, d2 in zip(data1, data2):
assert np.allclose(d1, d2)
expr1 = sqrt(x) * exp(-x**2)
expr2 = sqrt(u * x) * exp(-x**2)
s1 = LineOver1DRangeSeries(im(expr1), (x, -5, 5), adaptive=False, n=10)
s2 = LineOver1DRangeSeries(im(expr2), (x, -5, 5),
adaptive=False, n=10, params={u: 1})
data1 = s1.get_data()
data2 = s2.get_data()
do_test(data1, data2)
assert (not np.allclose(data1[1], 0)) and (not np.allclose(data2[1], 0))
s1 = Parametric2DLineSeries(re(expr1), im(expr1), (x, -pi, pi),
adaptive=False, n=10)
s2 = Parametric2DLineSeries(re(expr2), im(expr2), (x, -pi, pi),
adaptive=False, n=10, params={u: 1})
data1 = s1.get_data()
data2 = s2.get_data()
do_test(data1, data2)
assert (not np.allclose(data1[1], 0)) and (not np.allclose(data2[1], 0))
s1 = SurfaceOver2DRangeSeries(im(expr1), (x, -5, 5), (y, -10, 10),
adaptive=False, n1=30, n2=3)
s2 = SurfaceOver2DRangeSeries(im(expr2), (x, -5, 5), (y, -10, 10),
adaptive=False, n1=30, n2=3, params={u: 1})
data1 = s1.get_data()
data2 = s2.get_data()
do_test(data1, data2)
assert (not np.allclose(data1[1], 0)) and (not np.allclose(data2[1], 0))
def test_expr_is_lambda_function():
# verify that when a numpy function is provided, the series will be able
# to evaluate it. Also, label should be empty in order to prevent some
# backend from crashing.
if not np:
skip("numpy not installed.")
f = lambda x: np.cos(x)
s1 = LineOver1DRangeSeries(f, ("x", -5, 5), adaptive=True, depth=3)
s1.get_data()
s2 = LineOver1DRangeSeries(f, ("x", -5, 5), adaptive=False, n=10)
s2.get_data()
assert s1.label == s2.label == ""
fx = lambda x: np.cos(x)
fy = lambda x: np.sin(x)
s1 = Parametric2DLineSeries(fx, fy, ("x", 0, 2*pi),
adaptive=True, adaptive_goal=0.1)
s1.get_data()
s2 = Parametric2DLineSeries(fx, fy, ("x", 0, 2*pi),
adaptive=False, n=10)
s2.get_data()
assert s1.label == s2.label == ""
fz = lambda x: x
s1 = Parametric3DLineSeries(fx, fy, fz, ("x", 0, 2*pi),
adaptive=True, adaptive_goal=0.1)
s1.get_data()
s2 = Parametric3DLineSeries(fx, fy, fz, ("x", 0, 2*pi),
adaptive=False, n=10)
s2.get_data()
assert s1.label == s2.label == ""
f = lambda x, y: np.cos(x**2 + y**2)
s1 = SurfaceOver2DRangeSeries(f, ("a", -2, 2), ("b", -3, 3),
adaptive=False, n1=10, n2=10)
s1.get_data()
s2 = ContourSeries(f, ("a", -2, 2), ("b", -3, 3),
adaptive=False, n1=10, n2=10)
s2.get_data()
assert s1.label == s2.label == ""
fx = lambda u, v: np.cos(u + v)
fy = lambda u, v: np.sin(u - v)
fz = lambda u, v: u * v
s1 = ParametricSurfaceSeries(fx, fy, fz, ("u", 0, pi), ("v", 0, 2*pi),
adaptive=False, n1=10, n2=10)
s1.get_data()
assert s1.label == ""
raises(TypeError, lambda: List2DSeries(lambda t: t, lambda t: t))
raises(TypeError, lambda : ImplicitSeries(lambda t: np.sin(t),
("x", -5, 5), ("y", -6, 6)))
def test_show_in_legend_lines():
# verify that lines series correctly set the show_in_legend attribute
x, u = symbols("x, u")
s = LineOver1DRangeSeries(cos(x), (x, -2, 2), "test", show_in_legend=True)
assert s.show_in_legend
s = LineOver1DRangeSeries(cos(x), (x, -2, 2), "test", show_in_legend=False)
assert not s.show_in_legend
s = Parametric2DLineSeries(cos(x), sin(x), (x, 0, 1), "test",
show_in_legend=True)
assert s.show_in_legend
s = Parametric2DLineSeries(cos(x), sin(x), (x, 0, 1), "test",
show_in_legend=False)
assert not s.show_in_legend
s = Parametric3DLineSeries(cos(x), sin(x), x, (x, 0, 1), "test",
show_in_legend=True)
assert s.show_in_legend
s = Parametric3DLineSeries(cos(x), sin(x), x, (x, 0, 1), "test",
show_in_legend=False)
assert not s.show_in_legend
@XFAIL
def test_particular_case_1_with_adaptive_true():
# Verify that symbolic expressions and numerical lambda functions are
# evaluated with the same algorithm.
if not np:
skip("numpy not installed.")
# NOTE: xfail because sympy's adaptive algorithm is not deterministic
def do_test(a, b):
with warns(
RuntimeWarning,
match="invalid value encountered in scalar power",
test_stacklevel=False,
):
d1 = a.get_data()
d2 = b.get_data()
for t, v in zip(d1, d2):
assert np.allclose(t, v)
n = symbols("n")
a = S(2) / 3
epsilon = 0.01
xn = (n**3 + n**2)**(S(1)/3) - (n**3 - n**2)**(S(1)/3)
expr = Abs(xn - a) - epsilon
math_func = lambdify([n], expr)
s1 = LineOver1DRangeSeries(expr, (n, -10, 10), "",
adaptive=True, depth=3)
s2 = LineOver1DRangeSeries(math_func, ("n", -10, 10), "",
adaptive=True, depth=3)
do_test(s1, s2)
def test_particular_case_1_with_adaptive_false():
# Verify that symbolic expressions and numerical lambda functions are
# evaluated with the same algorithm. In particular, uniform evaluation
# is going to use np.vectorize, which correctly evaluates the following
# mathematical function.
if not np:
skip("numpy not installed.")
def do_test(a, b):
d1 = a.get_data()
d2 = b.get_data()
for t, v in zip(d1, d2):
assert np.allclose(t, v)
n = symbols("n")
a = S(2) / 3
epsilon = 0.01
xn = (n**3 + n**2)**(S(1)/3) - (n**3 - n**2)**(S(1)/3)
expr = Abs(xn - a) - epsilon
math_func = lambdify([n], expr)
s3 = LineOver1DRangeSeries(expr, (n, -10, 10), "",
adaptive=False, n=10)
s4 = LineOver1DRangeSeries(math_func, ("n", -10, 10), "",
adaptive=False, n=10)
do_test(s3, s4)
def test_complex_params_number_eval():
# The main expression contains terms like sqrt(xi - 1), with
# parameter (0 <= xi <= 1).
# There shouldn't be any NaN values on the output.
if not np:
skip("numpy not installed.")
xi, wn, x0, v0, t = symbols("xi, omega_n, x0, v0, t")
x = Function("x")(t)
eq = x.diff(t, 2) + 2 * xi * wn * x.diff(t) + wn**2 * x
sol = dsolve(eq, x, ics={x.subs(t, 0): x0, x.diff(t).subs(t, 0): v0})
params = {
wn: 0.5,
xi: 0.25,
x0: 0.45,
v0: 0.0
}
s = LineOver1DRangeSeries(sol.rhs, (t, 0, 100), adaptive=False, n=5,
params=params)
x, y = s.get_data()
assert not np.isnan(x).any()
assert not np.isnan(y).any()
# Fourier Series of a sawtooth wave
# The main expression contains a Sum with a symbolic upper range.
# The lambdified code looks like:
# sum(blablabla for for n in range(1, m+1))
# But range requires integer numbers, whereas per above example, the series
# casts parameters to complex. Verify that the series is able to detect
# upper bounds in summations and cast it to int in order to get successfull
# evaluation
x, T, n, m = symbols("x, T, n, m")
fs = S(1) / 2 - (1 / pi) * Sum(sin(2 * n * pi * x / T) / n, (n, 1, m))
params = {
T: 4.5,
m: 5
}
s = LineOver1DRangeSeries(fs, (x, 0, 10), adaptive=False, n=5,
params=params)
x, y = s.get_data()
assert not np.isnan(x).any()
assert not np.isnan(y).any()
def test_complex_range_line_plot_1():
# verify that univariate functions are evaluated with a complex
# data range (with zero imaginary part). There shouln't be any
# NaN value in the output.
if not np:
skip("numpy not installed.")
x, u = symbols("x, u")
expr1 = im(sqrt(x) * exp(-x**2))
expr2 = im(sqrt(u * x) * exp(-x**2))
s1 = LineOver1DRangeSeries(expr1, (x, -10, 10), adaptive=True,
adaptive_goal=0.1)
s2 = LineOver1DRangeSeries(expr1, (x, -10, 10), adaptive=False, n=30)
s3 = LineOver1DRangeSeries(expr2, (x, -10, 10), adaptive=False, n=30,
params={u: 1})
with ignore_warnings(RuntimeWarning):
data1 = s1.get_data()
data2 = s2.get_data()
data3 = s3.get_data()
assert not np.isnan(data1[1]).any()
assert not np.isnan(data2[1]).any()
assert not np.isnan(data3[1]).any()
assert np.allclose(data2[0], data3[0]) and np.allclose(data2[1], data3[1])
@XFAIL
def test_complex_range_line_plot_2():
# verify that univariate functions are evaluated with a complex
# data range (with non-zero imaginary part). There shouln't be any
# NaN value in the output.
if not np:
skip("numpy not installed.")
# NOTE: xfail because sympy's adaptive algorithm is unable to deal with
# complex number.
x, u = symbols("x, u")
# adaptive and uniform meshing should produce the same data.
# because of the adaptive nature, just compare the first and last points
# of both series.
s1 = LineOver1DRangeSeries(abs(sqrt(x)), (x, -5-2j, 5-2j), adaptive=True)
s2 = LineOver1DRangeSeries(abs(sqrt(x)), (x, -5-2j, 5-2j), adaptive=False,
n=10)
with warns(
RuntimeWarning,
match="invalid value encountered in sqrt",
test_stacklevel=False,
):
d1 = s1.get_data()
d2 = s2.get_data()
xx1 = [d1[0][0], d1[0][-1]]
xx2 = [d2[0][0], d2[0][-1]]
yy1 = [d1[1][0], d1[1][-1]]
yy2 = [d2[1][0], d2[1][-1]]
assert np.allclose(xx1, xx2)
assert np.allclose(yy1, yy2)
def test_force_real_eval():
# verify that force_real_eval=True produces inconsistent results when
# compared with evaluation of complex domain.
if not np:
skip("numpy not installed.")
x = symbols("x")
expr = im(sqrt(x) * exp(-x**2))
s1 = LineOver1DRangeSeries(expr, (x, -10, 10), adaptive=False, n=10,
force_real_eval=False)
s2 = LineOver1DRangeSeries(expr, (x, -10, 10), adaptive=False, n=10,
force_real_eval=True)
d1 = s1.get_data()
with ignore_warnings(RuntimeWarning):
d2 = s2.get_data()
assert not np.allclose(d1[1], 0)
assert np.allclose(d2[1], 0)
def test_contour_series_show_clabels():
# verify that a contour series has the abiliy to set the visibility of
# labels to contour lines
x, y = symbols("x, y")
s = ContourSeries(cos(x*y), (x, -2, 2), (y, -2, 2))
assert s.show_clabels
s = ContourSeries(cos(x*y), (x, -2, 2), (y, -2, 2), clabels=True)
assert s.show_clabels
s = ContourSeries(cos(x*y), (x, -2, 2), (y, -2, 2), clabels=False)
assert not s.show_clabels
def test_LineOver1DRangeSeries_complex_range():
# verify that LineOver1DRangeSeries can accept a complex range
# if the imaginary part of the start and end values are the same
x = symbols("x")
LineOver1DRangeSeries(sqrt(x), (x, -10, 10))
LineOver1DRangeSeries(sqrt(x), (x, -10-2j, 10-2j))
raises(ValueError,
lambda : LineOver1DRangeSeries(sqrt(x), (x, -10-2j, 10+2j)))
def test_symbolic_plotting_ranges():
# verify that data series can use symbolic plotting ranges
if not np:
skip("numpy not installed.")
x, y, z, a, b = symbols("x, y, z, a, b")
def do_test(s1, s2, new_params):
d1 = s1.get_data()
d2 = s2.get_data()
for u, v in zip(d1, d2):
assert np.allclose(u, v)
s2.params = new_params
d2 = s2.get_data()
for u, v in zip(d1, d2):
assert not np.allclose(u, v)
s1 = LineOver1DRangeSeries(sin(x), (x, 0, 1), adaptive=False, n=10)
s2 = LineOver1DRangeSeries(sin(x), (x, a, b), params={a: 0, b: 1},
adaptive=False, n=10)
do_test(s1, s2, {a: 0.5, b: 1.5})
# missing a parameter
raises(ValueError,
lambda : LineOver1DRangeSeries(sin(x), (x, a, b), params={a: 1}, n=10))
s1 = Parametric2DLineSeries(cos(x), sin(x), (x, 0, 1), adaptive=False, n=10)
s2 = Parametric2DLineSeries(cos(x), sin(x), (x, a, b), params={a: 0, b: 1},
adaptive=False, n=10)
do_test(s1, s2, {a: 0.5, b: 1.5})
# missing a parameter
raises(ValueError,
lambda : Parametric2DLineSeries(cos(x), sin(x), (x, a, b),
params={a: 0}, adaptive=False, n=10))
s1 = Parametric3DLineSeries(cos(x), sin(x), x, (x, 0, 1),
adaptive=False, n=10)
s2 = Parametric3DLineSeries(cos(x), sin(x), x, (x, a, b),
params={a: 0, b: 1}, adaptive=False, n=10)
do_test(s1, s2, {a: 0.5, b: 1.5})
# missing a parameter
raises(ValueError,
lambda : Parametric3DLineSeries(cos(x), sin(x), x, (x, a, b),
params={a: 0}, adaptive=False, n=10))
s1 = SurfaceOver2DRangeSeries(cos(x**2 + y**2), (x, -pi, pi), (y, -pi, pi),
adaptive=False, n1=5, n2=5)
s2 = SurfaceOver2DRangeSeries(cos(x**2 + y**2), (x, -pi * a, pi * a),
(y, -pi * b, pi * b), params={a: 1, b: 1},
adaptive=False, n1=5, n2=5)
do_test(s1, s2, {a: 0.5, b: 1.5})
# missing a parameter
raises(ValueError,
lambda : SurfaceOver2DRangeSeries(cos(x**2 + y**2),
(x, -pi * a, pi * a), (y, -pi * b, pi * b), params={a: 1},
adaptive=False, n1=5, n2=5))
# one range symbol is included into another range's minimum or maximum val
raises(ValueError,
lambda : SurfaceOver2DRangeSeries(cos(x**2 + y**2),
(x, -pi * a + y, pi * a), (y, -pi * b, pi * b), params={a: 1},
adaptive=False, n1=5, n2=5))
s1 = ParametricSurfaceSeries(
cos(x - y), sin(x + y), x - y, (x, -2, 2), (y, -2, 2), n1=5, n2=5)
s2 = ParametricSurfaceSeries(
cos(x - y), sin(x + y), x - y, (x, -2 * a, 2), (y, -2, 2 * b),
params={a: 1, b: 1}, n1=5, n2=5)
do_test(s1, s2, {a: 0.5, b: 1.5})
# missing a parameter
raises(ValueError,
lambda : ParametricSurfaceSeries(
cos(x - y), sin(x + y), x - y, (x, -2 * a, 2), (y, -2, 2 * b),
params={a: 1}, n1=5, n2=5))
def test_exclude_points():
# verify that exclude works as expected
if not np:
skip("numpy not installed.")
x = symbols("x")
expr = (floor(x) + S.Half) / (1 - (x - S.Half)**2)
with warns(
UserWarning,
match="NumPy is unable to evaluate with complex numbers some",
test_stacklevel=False,
):
s = LineOver1DRangeSeries(expr, (x, -3.5, 3.5), adaptive=False, n=100,
exclude=list(range(-3, 4)))
xx, yy = s.get_data()
assert not np.isnan(xx).any()
assert np.count_nonzero(np.isnan(yy)) == 7
assert len(xx) > 100
e1 = log(floor(x)) * cos(x)
e2 = log(floor(x)) * sin(x)
with warns(
UserWarning,
match="NumPy is unable to evaluate with complex numbers some",
test_stacklevel=False,
):
s = Parametric2DLineSeries(e1, e2, (x, 1, 12), adaptive=False, n=100,
exclude=list(range(1, 13)))
xx, yy, pp = s.get_data()
assert not np.isnan(pp).any()
assert np.count_nonzero(np.isnan(xx)) == 11
assert np.count_nonzero(np.isnan(yy)) == 11
assert len(xx) > 100
def test_unwrap():
# verify that unwrap works as expected
if not np:
skip("numpy not installed.")
x, y = symbols("x, y")
expr = 1 / (x**3 + 2*x**2 + x)
expr = arg(expr.subs(x, I*y*2*pi))
s1 = LineOver1DRangeSeries(expr, (y, 1e-05, 1e05), xscale="log",
adaptive=False, n=10, unwrap=False)
s2 = LineOver1DRangeSeries(expr, (y, 1e-05, 1e05), xscale="log",
adaptive=False, n=10, unwrap=True)
s3 = LineOver1DRangeSeries(expr, (y, 1e-05, 1e05), xscale="log",
adaptive=False, n=10, unwrap={"period": 4})
x1, y1 = s1.get_data()
x2, y2 = s2.get_data()
x3, y3 = s3.get_data()
assert np.allclose(x1, x2)
# there must not be nan values in the results of these evaluations
assert all(not np.isnan(t).any() for t in [y1, y2, y3])
assert not np.allclose(y1, y2)
assert not np.allclose(y1, y3)
assert not np.allclose(y2, y3)
|