Spaces:
Running
Running
File size: 57,796 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 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 |
"""Power series evaluation and manipulation using sparse Polynomials
Implementing a new function
---------------------------
There are a few things to be kept in mind when adding a new function here::
- The implementation should work on all possible input domains/rings.
Special cases include the ``EX`` ring and a constant term in the series
to be expanded. There can be two types of constant terms in the series:
+ A constant value or symbol.
+ A term of a multivariate series not involving the generator, with
respect to which the series is to expanded.
Strictly speaking, a generator of a ring should not be considered a
constant. However, for series expansion both the cases need similar
treatment (as the user does not care about inner details), i.e, use an
addition formula to separate the constant part and the variable part (see
rs_sin for reference).
- All the algorithms used here are primarily designed to work for Taylor
series (number of iterations in the algo equals the required order).
Hence, it becomes tricky to get the series of the right order if a
Puiseux series is input. Use rs_puiseux? in your function if your
algorithm is not designed to handle fractional powers.
Extending rs_series
-------------------
To make a function work with rs_series you need to do two things::
- Many sure it works with a constant term (as explained above).
- If the series contains constant terms, you might need to extend its ring.
You do so by adding the new terms to the rings as generators.
``PolyRing.compose`` and ``PolyRing.add_gens`` are two functions that do
so and need to be called every time you expand a series containing a
constant term.
Look at rs_sin and rs_series for further reference.
"""
from sympy.polys.domains import QQ, EX
from sympy.polys.rings import PolyElement, ring, sring
from sympy.polys.polyerrors import DomainError
from sympy.polys.monomials import (monomial_min, monomial_mul, monomial_div,
monomial_ldiv)
from mpmath.libmp.libintmath import ifac
from sympy.core import PoleError, Function, Expr
from sympy.core.numbers import Rational
from sympy.core.intfunc import igcd
from sympy.functions import sin, cos, tan, atan, exp, atanh, tanh, log, ceiling
from sympy.utilities.misc import as_int
from mpmath.libmp.libintmath import giant_steps
import math
def _invert_monoms(p1):
"""
Compute ``x**n * p1(1/x)`` for a univariate polynomial ``p1`` in ``x``.
Examples
========
>>> from sympy.polys.domains import ZZ
>>> from sympy.polys.rings import ring
>>> from sympy.polys.ring_series import _invert_monoms
>>> R, x = ring('x', ZZ)
>>> p = x**2 + 2*x + 3
>>> _invert_monoms(p)
3*x**2 + 2*x + 1
See Also
========
sympy.polys.densebasic.dup_reverse
"""
terms = list(p1.items())
terms.sort()
deg = p1.degree()
R = p1.ring
p = R.zero
cv = p1.listcoeffs()
mv = p1.listmonoms()
for mvi, cvi in zip(mv, cv):
p[(deg - mvi[0],)] = cvi
return p
def _giant_steps(target):
"""Return a list of precision steps for the Newton's method"""
res = giant_steps(2, target)
if res[0] != 2:
res = [2] + res
return res
def rs_trunc(p1, x, prec):
"""
Truncate the series in the ``x`` variable with precision ``prec``,
that is, modulo ``O(x**prec)``
Examples
========
>>> from sympy.polys.domains import QQ
>>> from sympy.polys.rings import ring
>>> from sympy.polys.ring_series import rs_trunc
>>> R, x = ring('x', QQ)
>>> p = x**10 + x**5 + x + 1
>>> rs_trunc(p, x, 12)
x**10 + x**5 + x + 1
>>> rs_trunc(p, x, 10)
x**5 + x + 1
"""
R = p1.ring
p = R.zero
i = R.gens.index(x)
for exp1 in p1:
if exp1[i] >= prec:
continue
p[exp1] = p1[exp1]
return p
def rs_is_puiseux(p, x):
"""
Test if ``p`` is Puiseux series in ``x``.
Raise an exception if it has a negative power in ``x``.
Examples
========
>>> from sympy.polys.domains import QQ
>>> from sympy.polys.rings import ring
>>> from sympy.polys.ring_series import rs_is_puiseux
>>> R, x = ring('x', QQ)
>>> p = x**QQ(2,5) + x**QQ(2,3) + x
>>> rs_is_puiseux(p, x)
True
"""
index = p.ring.gens.index(x)
for k in p:
if k[index] != int(k[index]):
return True
if k[index] < 0:
raise ValueError('The series is not regular in %s' % x)
return False
def rs_puiseux(f, p, x, prec):
"""
Return the puiseux series for `f(p, x, prec)`.
To be used when function ``f`` is implemented only for regular series.
Examples
========
>>> from sympy.polys.domains import QQ
>>> from sympy.polys.rings import ring
>>> from sympy.polys.ring_series import rs_puiseux, rs_exp
>>> R, x = ring('x', QQ)
>>> p = x**QQ(2,5) + x**QQ(2,3) + x
>>> rs_puiseux(rs_exp,p, x, 1)
1/2*x**(4/5) + x**(2/3) + x**(2/5) + 1
"""
index = p.ring.gens.index(x)
n = 1
for k in p:
power = k[index]
if isinstance(power, Rational):
num, den = power.as_numer_denom()
n = int(n*den // igcd(n, den))
elif power != int(power):
den = power.denominator
n = int(n*den // igcd(n, den))
if n != 1:
p1 = pow_xin(p, index, n)
r = f(p1, x, prec*n)
n1 = QQ(1, n)
if isinstance(r, tuple):
r = tuple([pow_xin(rx, index, n1) for rx in r])
else:
r = pow_xin(r, index, n1)
else:
r = f(p, x, prec)
return r
def rs_puiseux2(f, p, q, x, prec):
"""
Return the puiseux series for `f(p, q, x, prec)`.
To be used when function ``f`` is implemented only for regular series.
"""
index = p.ring.gens.index(x)
n = 1
for k in p:
power = k[index]
if isinstance(power, Rational):
num, den = power.as_numer_denom()
n = n*den // igcd(n, den)
elif power != int(power):
den = power.denominator
n = n*den // igcd(n, den)
if n != 1:
p1 = pow_xin(p, index, n)
r = f(p1, q, x, prec*n)
n1 = QQ(1, n)
r = pow_xin(r, index, n1)
else:
r = f(p, q, x, prec)
return r
def rs_mul(p1, p2, x, prec):
"""
Return the product of the given two series, modulo ``O(x**prec)``.
``x`` is the series variable or its position in the generators.
Examples
========
>>> from sympy.polys.domains import QQ
>>> from sympy.polys.rings import ring
>>> from sympy.polys.ring_series import rs_mul
>>> R, x = ring('x', QQ)
>>> p1 = x**2 + 2*x + 1
>>> p2 = x + 1
>>> rs_mul(p1, p2, x, 3)
3*x**2 + 3*x + 1
"""
R = p1.ring
p = R.zero
if R.__class__ != p2.ring.__class__ or R != p2.ring:
raise ValueError('p1 and p2 must have the same ring')
iv = R.gens.index(x)
if not isinstance(p2, PolyElement):
raise ValueError('p2 must be a polynomial')
if R == p2.ring:
get = p.get
items2 = list(p2.items())
items2.sort(key=lambda e: e[0][iv])
if R.ngens == 1:
for exp1, v1 in p1.items():
for exp2, v2 in items2:
exp = exp1[0] + exp2[0]
if exp < prec:
exp = (exp, )
p[exp] = get(exp, 0) + v1*v2
else:
break
else:
monomial_mul = R.monomial_mul
for exp1, v1 in p1.items():
for exp2, v2 in items2:
if exp1[iv] + exp2[iv] < prec:
exp = monomial_mul(exp1, exp2)
p[exp] = get(exp, 0) + v1*v2
else:
break
p.strip_zero()
return p
def rs_square(p1, x, prec):
"""
Square the series modulo ``O(x**prec)``
Examples
========
>>> from sympy.polys.domains import QQ
>>> from sympy.polys.rings import ring
>>> from sympy.polys.ring_series import rs_square
>>> R, x = ring('x', QQ)
>>> p = x**2 + 2*x + 1
>>> rs_square(p, x, 3)
6*x**2 + 4*x + 1
"""
R = p1.ring
p = R.zero
iv = R.gens.index(x)
get = p.get
items = list(p1.items())
items.sort(key=lambda e: e[0][iv])
monomial_mul = R.monomial_mul
for i in range(len(items)):
exp1, v1 = items[i]
for j in range(i):
exp2, v2 = items[j]
if exp1[iv] + exp2[iv] < prec:
exp = monomial_mul(exp1, exp2)
p[exp] = get(exp, 0) + v1*v2
else:
break
p = p.imul_num(2)
get = p.get
for expv, v in p1.items():
if 2*expv[iv] < prec:
e2 = monomial_mul(expv, expv)
p[e2] = get(e2, 0) + v**2
p.strip_zero()
return p
def rs_pow(p1, n, x, prec):
"""
Return ``p1**n`` modulo ``O(x**prec)``
Examples
========
>>> from sympy.polys.domains import QQ
>>> from sympy.polys.rings import ring
>>> from sympy.polys.ring_series import rs_pow
>>> R, x = ring('x', QQ)
>>> p = x + 1
>>> rs_pow(p, 4, x, 3)
6*x**2 + 4*x + 1
"""
R = p1.ring
if isinstance(n, Rational):
np = int(n.p)
nq = int(n.q)
if nq != 1:
res = rs_nth_root(p1, nq, x, prec)
if np != 1:
res = rs_pow(res, np, x, prec)
else:
res = rs_pow(p1, np, x, prec)
return res
n = as_int(n)
if n == 0:
if p1:
return R(1)
else:
raise ValueError('0**0 is undefined')
if n < 0:
p1 = rs_pow(p1, -n, x, prec)
return rs_series_inversion(p1, x, prec)
if n == 1:
return rs_trunc(p1, x, prec)
if n == 2:
return rs_square(p1, x, prec)
if n == 3:
p2 = rs_square(p1, x, prec)
return rs_mul(p1, p2, x, prec)
p = R(1)
while 1:
if n & 1:
p = rs_mul(p1, p, x, prec)
n -= 1
if not n:
break
p1 = rs_square(p1, x, prec)
n = n // 2
return p
def rs_subs(p, rules, x, prec):
"""
Substitution with truncation according to the mapping in ``rules``.
Return a series with precision ``prec`` in the generator ``x``
Note that substitutions are not done one after the other
>>> from sympy.polys.domains import QQ
>>> from sympy.polys.rings import ring
>>> from sympy.polys.ring_series import rs_subs
>>> R, x, y = ring('x, y', QQ)
>>> p = x**2 + y**2
>>> rs_subs(p, {x: x+ y, y: x+ 2*y}, x, 3)
2*x**2 + 6*x*y + 5*y**2
>>> (x + y)**2 + (x + 2*y)**2
2*x**2 + 6*x*y + 5*y**2
which differs from
>>> rs_subs(rs_subs(p, {x: x+ y}, x, 3), {y: x+ 2*y}, x, 3)
5*x**2 + 12*x*y + 8*y**2
Parameters
----------
p : :class:`~.PolyElement` Input series.
rules : ``dict`` with substitution mappings.
x : :class:`~.PolyElement` in which the series truncation is to be done.
prec : :class:`~.Integer` order of the series after truncation.
Examples
========
>>> from sympy.polys.domains import QQ
>>> from sympy.polys.rings import ring
>>> from sympy.polys.ring_series import rs_subs
>>> R, x, y = ring('x, y', QQ)
>>> rs_subs(x**2+y**2, {y: (x+y)**2}, x, 3)
6*x**2*y**2 + x**2 + 4*x*y**3 + y**4
"""
R = p.ring
ngens = R.ngens
d = R(0)
for i in range(ngens):
d[(i, 1)] = R.gens[i]
for var in rules:
d[(R.index(var), 1)] = rules[var]
p1 = R(0)
p_keys = sorted(p.keys())
for expv in p_keys:
p2 = R(1)
for i in range(ngens):
power = expv[i]
if power == 0:
continue
if (i, power) not in d:
q, r = divmod(power, 2)
if r == 0 and (i, q) in d:
d[(i, power)] = rs_square(d[(i, q)], x, prec)
elif (i, power - 1) in d:
d[(i, power)] = rs_mul(d[(i, power - 1)], d[(i, 1)],
x, prec)
else:
d[(i, power)] = rs_pow(d[(i, 1)], power, x, prec)
p2 = rs_mul(p2, d[(i, power)], x, prec)
p1 += p2*p[expv]
return p1
def _has_constant_term(p, x):
"""
Check if ``p`` has a constant term in ``x``
Examples
========
>>> from sympy.polys.domains import QQ
>>> from sympy.polys.rings import ring
>>> from sympy.polys.ring_series import _has_constant_term
>>> R, x = ring('x', QQ)
>>> p = x**2 + x + 1
>>> _has_constant_term(p, x)
True
"""
R = p.ring
iv = R.gens.index(x)
zm = R.zero_monom
a = [0]*R.ngens
a[iv] = 1
miv = tuple(a)
for expv in p:
if monomial_min(expv, miv) == zm:
return True
return False
def _get_constant_term(p, x):
"""Return constant term in p with respect to x
Note that it is not simply `p[R.zero_monom]` as there might be multiple
generators in the ring R. We want the `x`-free term which can contain other
generators.
"""
R = p.ring
i = R.gens.index(x)
zm = R.zero_monom
a = [0]*R.ngens
a[i] = 1
miv = tuple(a)
c = 0
for expv in p:
if monomial_min(expv, miv) == zm:
c += R({expv: p[expv]})
return c
def _check_series_var(p, x, name):
index = p.ring.gens.index(x)
m = min(p, key=lambda k: k[index])[index]
if m < 0:
raise PoleError("Asymptotic expansion of %s around [oo] not "
"implemented." % name)
return index, m
def _series_inversion1(p, x, prec):
"""
Univariate series inversion ``1/p`` modulo ``O(x**prec)``.
The Newton method is used.
Examples
========
>>> from sympy.polys.domains import QQ
>>> from sympy.polys.rings import ring
>>> from sympy.polys.ring_series import _series_inversion1
>>> R, x = ring('x', QQ)
>>> p = x + 1
>>> _series_inversion1(p, x, 4)
-x**3 + x**2 - x + 1
"""
if rs_is_puiseux(p, x):
return rs_puiseux(_series_inversion1, p, x, prec)
R = p.ring
zm = R.zero_monom
c = p[zm]
# giant_steps does not seem to work with PythonRational numbers with 1 as
# denominator. This makes sure such a number is converted to integer.
if prec == int(prec):
prec = int(prec)
if zm not in p:
raise ValueError("No constant term in series")
if _has_constant_term(p - c, x):
raise ValueError("p cannot contain a constant term depending on "
"parameters")
one = R(1)
if R.domain is EX:
one = 1
if c != one:
# TODO add check that it is a unit
p1 = R(1)/c
else:
p1 = R(1)
for precx in _giant_steps(prec):
t = 1 - rs_mul(p1, p, x, precx)
p1 = p1 + rs_mul(p1, t, x, precx)
return p1
def rs_series_inversion(p, x, prec):
"""
Multivariate series inversion ``1/p`` modulo ``O(x**prec)``.
Examples
========
>>> from sympy.polys.domains import QQ
>>> from sympy.polys.rings import ring
>>> from sympy.polys.ring_series import rs_series_inversion
>>> R, x, y = ring('x, y', QQ)
>>> rs_series_inversion(1 + x*y**2, x, 4)
-x**3*y**6 + x**2*y**4 - x*y**2 + 1
>>> rs_series_inversion(1 + x*y**2, y, 4)
-x*y**2 + 1
>>> rs_series_inversion(x + x**2, x, 4)
x**3 - x**2 + x - 1 + x**(-1)
"""
R = p.ring
if p == R.zero:
raise ZeroDivisionError
zm = R.zero_monom
index = R.gens.index(x)
m = min(p, key=lambda k: k[index])[index]
if m:
p = mul_xin(p, index, -m)
prec = prec + m
if zm not in p:
raise NotImplementedError("No constant term in series")
if _has_constant_term(p - p[zm], x):
raise NotImplementedError("p - p[0] must not have a constant term in "
"the series variables")
r = _series_inversion1(p, x, prec)
if m != 0:
r = mul_xin(r, index, -m)
return r
def _coefficient_t(p, t):
r"""Coefficient of `x_i**j` in p, where ``t`` = (i, j)"""
i, j = t
R = p.ring
expv1 = [0]*R.ngens
expv1[i] = j
expv1 = tuple(expv1)
p1 = R(0)
for expv in p:
if expv[i] == j:
p1[monomial_div(expv, expv1)] = p[expv]
return p1
def rs_series_reversion(p, x, n, y):
r"""
Reversion of a series.
``p`` is a series with ``O(x**n)`` of the form $p = ax + f(x)$
where $a$ is a number different from 0.
$f(x) = \sum_{k=2}^{n-1} a_kx_k$
Parameters
==========
a_k : Can depend polynomially on other variables, not indicated.
x : Variable with name x.
y : Variable with name y.
Returns
=======
Solve $p = y$, that is, given $ax + f(x) - y = 0$,
find the solution $x = r(y)$ up to $O(y^n)$.
Algorithm
=========
If $r_i$ is the solution at order $i$, then:
$ar_i + f(r_i) - y = O\left(y^{i + 1}\right)$
and if $r_{i + 1}$ is the solution at order $i + 1$, then:
$ar_{i + 1} + f(r_{i + 1}) - y = O\left(y^{i + 2}\right)$
We have, $r_{i + 1} = r_i + e$, such that,
$ae + f(r_i) = O\left(y^{i + 2}\right)$
or $e = -f(r_i)/a$
So we use the recursion relation:
$r_{i + 1} = r_i - f(r_i)/a$
with the boundary condition: $r_1 = y$
Examples
========
>>> from sympy.polys.domains import QQ
>>> from sympy.polys.rings import ring
>>> from sympy.polys.ring_series import rs_series_reversion, rs_trunc
>>> R, x, y, a, b = ring('x, y, a, b', QQ)
>>> p = x - x**2 - 2*b*x**2 + 2*a*b*x**2
>>> p1 = rs_series_reversion(p, x, 3, y); p1
-2*y**2*a*b + 2*y**2*b + y**2 + y
>>> rs_trunc(p.compose(x, p1), y, 3)
y
"""
if rs_is_puiseux(p, x):
raise NotImplementedError
R = p.ring
nx = R.gens.index(x)
y = R(y)
ny = R.gens.index(y)
if _has_constant_term(p, x):
raise ValueError("p must not contain a constant term in the series "
"variable")
a = _coefficient_t(p, (nx, 1))
zm = R.zero_monom
assert zm in a and len(a) == 1
a = a[zm]
r = y/a
for i in range(2, n):
sp = rs_subs(p, {x: r}, y, i + 1)
sp = _coefficient_t(sp, (ny, i))*y**i
r -= sp/a
return r
def rs_series_from_list(p, c, x, prec, concur=1):
"""
Return a series `sum c[n]*p**n` modulo `O(x**prec)`.
It reduces the number of multiplications by summing concurrently.
`ax = [1, p, p**2, .., p**(J - 1)]`
`s = sum(c[i]*ax[i]` for i in `range(r, (r + 1)*J))*p**((K - 1)*J)`
with `K >= (n + 1)/J`
Examples
========
>>> from sympy.polys.domains import QQ
>>> from sympy.polys.rings import ring
>>> from sympy.polys.ring_series import rs_series_from_list, rs_trunc
>>> R, x = ring('x', QQ)
>>> p = x**2 + x + 1
>>> c = [1, 2, 3]
>>> rs_series_from_list(p, c, x, 4)
6*x**3 + 11*x**2 + 8*x + 6
>>> rs_trunc(1 + 2*p + 3*p**2, x, 4)
6*x**3 + 11*x**2 + 8*x + 6
>>> pc = R.from_list(list(reversed(c)))
>>> rs_trunc(pc.compose(x, p), x, 4)
6*x**3 + 11*x**2 + 8*x + 6
"""
# TODO: Add this when it is documented in Sphinx
"""
See Also
========
sympy.polys.rings.PolyRing.compose
"""
R = p.ring
n = len(c)
if not concur:
q = R(1)
s = c[0]*q
for i in range(1, n):
q = rs_mul(q, p, x, prec)
s += c[i]*q
return s
J = int(math.sqrt(n) + 1)
K, r = divmod(n, J)
if r:
K += 1
ax = [R(1)]
q = R(1)
if len(p) < 20:
for i in range(1, J):
q = rs_mul(q, p, x, prec)
ax.append(q)
else:
for i in range(1, J):
if i % 2 == 0:
q = rs_square(ax[i//2], x, prec)
else:
q = rs_mul(q, p, x, prec)
ax.append(q)
# optimize using rs_square
pj = rs_mul(ax[-1], p, x, prec)
b = R(1)
s = R(0)
for k in range(K - 1):
r = J*k
s1 = c[r]
for j in range(1, J):
s1 += c[r + j]*ax[j]
s1 = rs_mul(s1, b, x, prec)
s += s1
b = rs_mul(b, pj, x, prec)
if not b:
break
k = K - 1
r = J*k
if r < n:
s1 = c[r]*R(1)
for j in range(1, J):
if r + j >= n:
break
s1 += c[r + j]*ax[j]
s1 = rs_mul(s1, b, x, prec)
s += s1
return s
def rs_diff(p, x):
"""
Return partial derivative of ``p`` with respect to ``x``.
Parameters
==========
x : :class:`~.PolyElement` with respect to which ``p`` is differentiated.
Examples
========
>>> from sympy.polys.domains import QQ
>>> from sympy.polys.rings import ring
>>> from sympy.polys.ring_series import rs_diff
>>> R, x, y = ring('x, y', QQ)
>>> p = x + x**2*y**3
>>> rs_diff(p, x)
2*x*y**3 + 1
"""
R = p.ring
n = R.gens.index(x)
p1 = R.zero
mn = [0]*R.ngens
mn[n] = 1
mn = tuple(mn)
for expv in p:
if expv[n]:
e = monomial_ldiv(expv, mn)
p1[e] = R.domain_new(p[expv]*expv[n])
return p1
def rs_integrate(p, x):
"""
Integrate ``p`` with respect to ``x``.
Parameters
==========
x : :class:`~.PolyElement` with respect to which ``p`` is integrated.
Examples
========
>>> from sympy.polys.domains import QQ
>>> from sympy.polys.rings import ring
>>> from sympy.polys.ring_series import rs_integrate
>>> R, x, y = ring('x, y', QQ)
>>> p = x + x**2*y**3
>>> rs_integrate(p, x)
1/3*x**3*y**3 + 1/2*x**2
"""
R = p.ring
p1 = R.zero
n = R.gens.index(x)
mn = [0]*R.ngens
mn[n] = 1
mn = tuple(mn)
for expv in p:
e = monomial_mul(expv, mn)
p1[e] = R.domain_new(p[expv]/(expv[n] + 1))
return p1
def rs_fun(p, f, *args):
r"""
Function of a multivariate series computed by substitution.
The case with f method name is used to compute `rs\_tan` and `rs\_nth\_root`
of a multivariate series:
`rs\_fun(p, tan, iv, prec)`
tan series is first computed for a dummy variable _x,
i.e, `rs\_tan(\_x, iv, prec)`. Then we substitute _x with p to get the
desired series
Parameters
==========
p : :class:`~.PolyElement` The multivariate series to be expanded.
f : `ring\_series` function to be applied on `p`.
args[-2] : :class:`~.PolyElement` with respect to which, the series is to be expanded.
args[-1] : Required order of the expanded series.
Examples
========
>>> from sympy.polys.domains import QQ
>>> from sympy.polys.rings import ring
>>> from sympy.polys.ring_series import rs_fun, _tan1
>>> R, x, y = ring('x, y', QQ)
>>> p = x + x*y + x**2*y + x**3*y**2
>>> rs_fun(p, _tan1, x, 4)
1/3*x**3*y**3 + 2*x**3*y**2 + x**3*y + 1/3*x**3 + x**2*y + x*y + x
"""
_R = p.ring
R1, _x = ring('_x', _R.domain)
h = int(args[-1])
args1 = args[:-2] + (_x, h)
zm = _R.zero_monom
# separate the constant term of the series
# compute the univariate series f(_x, .., 'x', sum(nv))
if zm in p:
x1 = _x + p[zm]
p1 = p - p[zm]
else:
x1 = _x
p1 = p
if isinstance(f, str):
q = getattr(x1, f)(*args1)
else:
q = f(x1, *args1)
a = sorted(q.items())
c = [0]*h
for x in a:
c[x[0][0]] = x[1]
p1 = rs_series_from_list(p1, c, args[-2], args[-1])
return p1
def mul_xin(p, i, n):
r"""
Return `p*x_i**n`.
`x\_i` is the ith variable in ``p``.
"""
R = p.ring
q = R(0)
for k, v in p.items():
k1 = list(k)
k1[i] += n
q[tuple(k1)] = v
return q
def pow_xin(p, i, n):
"""
>>> from sympy.polys.domains import QQ
>>> from sympy.polys.rings import ring
>>> from sympy.polys.ring_series import pow_xin
>>> R, x, y = ring('x, y', QQ)
>>> p = x**QQ(2,5) + x + x**QQ(2,3)
>>> index = p.ring.gens.index(x)
>>> pow_xin(p, index, 15)
x**15 + x**10 + x**6
"""
R = p.ring
q = R(0)
for k, v in p.items():
k1 = list(k)
k1[i] *= n
q[tuple(k1)] = v
return q
def _nth_root1(p, n, x, prec):
"""
Univariate series expansion of the nth root of ``p``.
The Newton method is used.
"""
if rs_is_puiseux(p, x):
return rs_puiseux2(_nth_root1, p, n, x, prec)
R = p.ring
zm = R.zero_monom
if zm not in p:
raise NotImplementedError('No constant term in series')
n = as_int(n)
assert p[zm] == 1
p1 = R(1)
if p == 1:
return p
if n == 0:
return R(1)
if n == 1:
return p
if n < 0:
n = -n
sign = 1
else:
sign = 0
for precx in _giant_steps(prec):
tmp = rs_pow(p1, n + 1, x, precx)
tmp = rs_mul(tmp, p, x, precx)
p1 += p1/n - tmp/n
if sign:
return p1
else:
return _series_inversion1(p1, x, prec)
def rs_nth_root(p, n, x, prec):
"""
Multivariate series expansion of the nth root of ``p``.
Parameters
==========
p : Expr
The polynomial to computer the root of.
n : integer
The order of the root to be computed.
x : :class:`~.PolyElement`
prec : integer
Order of the expanded series.
Notes
=====
The result of this function is dependent on the ring over which the
polynomial has been defined. If the answer involves a root of a constant,
make sure that the polynomial is over a real field. It cannot yet handle
roots of symbols.
Examples
========
>>> from sympy.polys.domains import QQ, RR
>>> from sympy.polys.rings import ring
>>> from sympy.polys.ring_series import rs_nth_root
>>> R, x, y = ring('x, y', QQ)
>>> rs_nth_root(1 + x + x*y, -3, x, 3)
2/9*x**2*y**2 + 4/9*x**2*y + 2/9*x**2 - 1/3*x*y - 1/3*x + 1
>>> R, x, y = ring('x, y', RR)
>>> rs_nth_root(3 + x + x*y, 3, x, 2)
0.160249952256379*x*y + 0.160249952256379*x + 1.44224957030741
"""
if n == 0:
if p == 0:
raise ValueError('0**0 expression')
else:
return p.ring(1)
if n == 1:
return rs_trunc(p, x, prec)
R = p.ring
index = R.gens.index(x)
m = min(p, key=lambda k: k[index])[index]
p = mul_xin(p, index, -m)
prec -= m
if _has_constant_term(p - 1, x):
zm = R.zero_monom
c = p[zm]
if R.domain is EX:
c_expr = c.as_expr()
const = c_expr**QQ(1, n)
elif isinstance(c, PolyElement):
try:
c_expr = c.as_expr()
const = R(c_expr**(QQ(1, n)))
except ValueError:
raise DomainError("The given series cannot be expanded in "
"this domain.")
else:
try: # RealElement doesn't support
const = R(c**Rational(1, n)) # exponentiation with mpq object
except ValueError: # as exponent
raise DomainError("The given series cannot be expanded in "
"this domain.")
res = rs_nth_root(p/c, n, x, prec)*const
else:
res = _nth_root1(p, n, x, prec)
if m:
m = QQ(m, n)
res = mul_xin(res, index, m)
return res
def rs_log(p, x, prec):
"""
The Logarithm of ``p`` modulo ``O(x**prec)``.
Notes
=====
Truncation of ``integral dx p**-1*d p/dx`` is used.
Examples
========
>>> from sympy.polys.domains import QQ
>>> from sympy.polys.rings import ring
>>> from sympy.polys.ring_series import rs_log
>>> R, x = ring('x', QQ)
>>> rs_log(1 + x, x, 8)
1/7*x**7 - 1/6*x**6 + 1/5*x**5 - 1/4*x**4 + 1/3*x**3 - 1/2*x**2 + x
>>> rs_log(x**QQ(3, 2) + 1, x, 5)
1/3*x**(9/2) - 1/2*x**3 + x**(3/2)
"""
if rs_is_puiseux(p, x):
return rs_puiseux(rs_log, p, x, prec)
R = p.ring
if p == 1:
return R.zero
c = _get_constant_term(p, x)
if c:
const = 0
if c == 1:
pass
else:
c_expr = c.as_expr()
if R.domain is EX:
const = log(c_expr)
elif isinstance(c, PolyElement):
try:
const = R(log(c_expr))
except ValueError:
R = R.add_gens([log(c_expr)])
p = p.set_ring(R)
x = x.set_ring(R)
c = c.set_ring(R)
const = R(log(c_expr))
else:
try:
const = R(log(c))
except ValueError:
raise DomainError("The given series cannot be expanded in "
"this domain.")
dlog = p.diff(x)
dlog = rs_mul(dlog, _series_inversion1(p, x, prec), x, prec - 1)
return rs_integrate(dlog, x) + const
else:
raise NotImplementedError
def rs_LambertW(p, x, prec):
"""
Calculate the series expansion of the principal branch of the Lambert W
function.
Examples
========
>>> from sympy.polys.domains import QQ
>>> from sympy.polys.rings import ring
>>> from sympy.polys.ring_series import rs_LambertW
>>> R, x, y = ring('x, y', QQ)
>>> rs_LambertW(x + x*y, x, 3)
-x**2*y**2 - 2*x**2*y - x**2 + x*y + x
See Also
========
LambertW
"""
if rs_is_puiseux(p, x):
return rs_puiseux(rs_LambertW, p, x, prec)
R = p.ring
p1 = R(0)
if _has_constant_term(p, x):
raise NotImplementedError("Polynomial must not have constant term in "
"the series variables")
if x in R.gens:
for precx in _giant_steps(prec):
e = rs_exp(p1, x, precx)
p2 = rs_mul(e, p1, x, precx) - p
p3 = rs_mul(e, p1 + 1, x, precx)
p3 = rs_series_inversion(p3, x, precx)
tmp = rs_mul(p2, p3, x, precx)
p1 -= tmp
return p1
else:
raise NotImplementedError
def _exp1(p, x, prec):
r"""Helper function for `rs\_exp`. """
R = p.ring
p1 = R(1)
for precx in _giant_steps(prec):
pt = p - rs_log(p1, x, precx)
tmp = rs_mul(pt, p1, x, precx)
p1 += tmp
return p1
def rs_exp(p, x, prec):
"""
Exponentiation of a series modulo ``O(x**prec)``
Examples
========
>>> from sympy.polys.domains import QQ
>>> from sympy.polys.rings import ring
>>> from sympy.polys.ring_series import rs_exp
>>> R, x = ring('x', QQ)
>>> rs_exp(x**2, x, 7)
1/6*x**6 + 1/2*x**4 + x**2 + 1
"""
if rs_is_puiseux(p, x):
return rs_puiseux(rs_exp, p, x, prec)
R = p.ring
c = _get_constant_term(p, x)
if c:
if R.domain is EX:
c_expr = c.as_expr()
const = exp(c_expr)
elif isinstance(c, PolyElement):
try:
c_expr = c.as_expr()
const = R(exp(c_expr))
except ValueError:
R = R.add_gens([exp(c_expr)])
p = p.set_ring(R)
x = x.set_ring(R)
c = c.set_ring(R)
const = R(exp(c_expr))
else:
try:
const = R(exp(c))
except ValueError:
raise DomainError("The given series cannot be expanded in "
"this domain.")
p1 = p - c
# Makes use of SymPy functions to evaluate the values of the cos/sin
# of the constant term.
return const*rs_exp(p1, x, prec)
if len(p) > 20:
return _exp1(p, x, prec)
one = R(1)
n = 1
c = []
for k in range(prec):
c.append(one/n)
k += 1
n *= k
r = rs_series_from_list(p, c, x, prec)
return r
def _atan(p, iv, prec):
"""
Expansion using formula.
Faster on very small and univariate series.
"""
R = p.ring
mo = R(-1)
c = [-mo]
p2 = rs_square(p, iv, prec)
for k in range(1, prec):
c.append(mo**k/(2*k + 1))
s = rs_series_from_list(p2, c, iv, prec)
s = rs_mul(s, p, iv, prec)
return s
def rs_atan(p, x, prec):
"""
The arctangent of a series
Return the series expansion of the atan of ``p``, about 0.
Examples
========
>>> from sympy.polys.domains import QQ
>>> from sympy.polys.rings import ring
>>> from sympy.polys.ring_series import rs_atan
>>> R, x, y = ring('x, y', QQ)
>>> rs_atan(x + x*y, x, 4)
-1/3*x**3*y**3 - x**3*y**2 - x**3*y - 1/3*x**3 + x*y + x
See Also
========
atan
"""
if rs_is_puiseux(p, x):
return rs_puiseux(rs_atan, p, x, prec)
R = p.ring
const = 0
if _has_constant_term(p, x):
zm = R.zero_monom
c = p[zm]
if R.domain is EX:
c_expr = c.as_expr()
const = atan(c_expr)
elif isinstance(c, PolyElement):
try:
c_expr = c.as_expr()
const = R(atan(c_expr))
except ValueError:
raise DomainError("The given series cannot be expanded in "
"this domain.")
else:
try:
const = R(atan(c))
except ValueError:
raise DomainError("The given series cannot be expanded in "
"this domain.")
# Instead of using a closed form formula, we differentiate atan(p) to get
# `1/(1+p**2) * dp`, whose series expansion is much easier to calculate.
# Finally we integrate to get back atan
dp = p.diff(x)
p1 = rs_square(p, x, prec) + R(1)
p1 = rs_series_inversion(p1, x, prec - 1)
p1 = rs_mul(dp, p1, x, prec - 1)
return rs_integrate(p1, x) + const
def rs_asin(p, x, prec):
"""
Arcsine of a series
Return the series expansion of the asin of ``p``, about 0.
Examples
========
>>> from sympy.polys.domains import QQ
>>> from sympy.polys.rings import ring
>>> from sympy.polys.ring_series import rs_asin
>>> R, x, y = ring('x, y', QQ)
>>> rs_asin(x, x, 8)
5/112*x**7 + 3/40*x**5 + 1/6*x**3 + x
See Also
========
asin
"""
if rs_is_puiseux(p, x):
return rs_puiseux(rs_asin, p, x, prec)
if _has_constant_term(p, x):
raise NotImplementedError("Polynomial must not have constant term in "
"series variables")
R = p.ring
if x in R.gens:
# get a good value
if len(p) > 20:
dp = rs_diff(p, x)
p1 = 1 - rs_square(p, x, prec - 1)
p1 = rs_nth_root(p1, -2, x, prec - 1)
p1 = rs_mul(dp, p1, x, prec - 1)
return rs_integrate(p1, x)
one = R(1)
c = [0, one, 0]
for k in range(3, prec, 2):
c.append((k - 2)**2*c[-2]/(k*(k - 1)))
c.append(0)
return rs_series_from_list(p, c, x, prec)
else:
raise NotImplementedError
def _tan1(p, x, prec):
r"""
Helper function of :func:`rs_tan`.
Return the series expansion of tan of a univariate series using Newton's
method. It takes advantage of the fact that series expansion of atan is
easier than that of tan.
Consider `f(x) = y - \arctan(x)`
Let r be a root of f(x) found using Newton's method.
Then `f(r) = 0`
Or `y = \arctan(x)` where `x = \tan(y)` as required.
"""
R = p.ring
p1 = R(0)
for precx in _giant_steps(prec):
tmp = p - rs_atan(p1, x, precx)
tmp = rs_mul(tmp, 1 + rs_square(p1, x, precx), x, precx)
p1 += tmp
return p1
def rs_tan(p, x, prec):
"""
Tangent of a series.
Return the series expansion of the tan of ``p``, about 0.
Examples
========
>>> from sympy.polys.domains import QQ
>>> from sympy.polys.rings import ring
>>> from sympy.polys.ring_series import rs_tan
>>> R, x, y = ring('x, y', QQ)
>>> rs_tan(x + x*y, x, 4)
1/3*x**3*y**3 + x**3*y**2 + x**3*y + 1/3*x**3 + x*y + x
See Also
========
_tan1, tan
"""
if rs_is_puiseux(p, x):
r = rs_puiseux(rs_tan, p, x, prec)
return r
R = p.ring
const = 0
c = _get_constant_term(p, x)
if c:
if R.domain is EX:
c_expr = c.as_expr()
const = tan(c_expr)
elif isinstance(c, PolyElement):
try:
c_expr = c.as_expr()
const = R(tan(c_expr))
except ValueError:
R = R.add_gens([tan(c_expr, )])
p = p.set_ring(R)
x = x.set_ring(R)
c = c.set_ring(R)
const = R(tan(c_expr))
else:
try:
const = R(tan(c))
except ValueError:
raise DomainError("The given series cannot be expanded in "
"this domain.")
p1 = p - c
# Makes use of SymPy functions to evaluate the values of the cos/sin
# of the constant term.
t2 = rs_tan(p1, x, prec)
t = rs_series_inversion(1 - const*t2, x, prec)
return rs_mul(const + t2, t, x, prec)
if R.ngens == 1:
return _tan1(p, x, prec)
else:
return rs_fun(p, rs_tan, x, prec)
def rs_cot(p, x, prec):
"""
Cotangent of a series
Return the series expansion of the cot of ``p``, about 0.
Examples
========
>>> from sympy.polys.domains import QQ
>>> from sympy.polys.rings import ring
>>> from sympy.polys.ring_series import rs_cot
>>> R, x, y = ring('x, y', QQ)
>>> rs_cot(x, x, 6)
-2/945*x**5 - 1/45*x**3 - 1/3*x + x**(-1)
See Also
========
cot
"""
# It can not handle series like `p = x + x*y` where the coefficient of the
# linear term in the series variable is symbolic.
if rs_is_puiseux(p, x):
r = rs_puiseux(rs_cot, p, x, prec)
return r
i, m = _check_series_var(p, x, 'cot')
prec1 = prec + 2*m
c, s = rs_cos_sin(p, x, prec1)
s = mul_xin(s, i, -m)
s = rs_series_inversion(s, x, prec1)
res = rs_mul(c, s, x, prec1)
res = mul_xin(res, i, -m)
res = rs_trunc(res, x, prec)
return res
def rs_sin(p, x, prec):
"""
Sine of a series
Return the series expansion of the sin of ``p``, about 0.
Examples
========
>>> from sympy.polys.domains import QQ
>>> from sympy.polys.rings import ring
>>> from sympy.polys.ring_series import rs_sin
>>> R, x, y = ring('x, y', QQ)
>>> rs_sin(x + x*y, x, 4)
-1/6*x**3*y**3 - 1/2*x**3*y**2 - 1/2*x**3*y - 1/6*x**3 + x*y + x
>>> rs_sin(x**QQ(3, 2) + x*y**QQ(7, 5), x, 4)
-1/2*x**(7/2)*y**(14/5) - 1/6*x**3*y**(21/5) + x**(3/2) + x*y**(7/5)
See Also
========
sin
"""
if rs_is_puiseux(p, x):
return rs_puiseux(rs_sin, p, x, prec)
R = x.ring
if not p:
return R(0)
c = _get_constant_term(p, x)
if c:
if R.domain is EX:
c_expr = c.as_expr()
t1, t2 = sin(c_expr), cos(c_expr)
elif isinstance(c, PolyElement):
try:
c_expr = c.as_expr()
t1, t2 = R(sin(c_expr)), R(cos(c_expr))
except ValueError:
R = R.add_gens([sin(c_expr), cos(c_expr)])
p = p.set_ring(R)
x = x.set_ring(R)
c = c.set_ring(R)
t1, t2 = R(sin(c_expr)), R(cos(c_expr))
else:
try:
t1, t2 = R(sin(c)), R(cos(c))
except ValueError:
raise DomainError("The given series cannot be expanded in "
"this domain.")
p1 = p - c
# Makes use of SymPy cos, sin functions to evaluate the values of the
# cos/sin of the constant term.
return rs_sin(p1, x, prec)*t2 + rs_cos(p1, x, prec)*t1
# Series is calculated in terms of tan as its evaluation is fast.
if len(p) > 20 and R.ngens == 1:
t = rs_tan(p/2, x, prec)
t2 = rs_square(t, x, prec)
p1 = rs_series_inversion(1 + t2, x, prec)
return rs_mul(p1, 2*t, x, prec)
one = R(1)
n = 1
c = [0]
for k in range(2, prec + 2, 2):
c.append(one/n)
c.append(0)
n *= -k*(k + 1)
return rs_series_from_list(p, c, x, prec)
def rs_cos(p, x, prec):
"""
Cosine of a series
Return the series expansion of the cos of ``p``, about 0.
Examples
========
>>> from sympy.polys.domains import QQ
>>> from sympy.polys.rings import ring
>>> from sympy.polys.ring_series import rs_cos
>>> R, x, y = ring('x, y', QQ)
>>> rs_cos(x + x*y, x, 4)
-1/2*x**2*y**2 - x**2*y - 1/2*x**2 + 1
>>> rs_cos(x + x*y, x, 4)/x**QQ(7, 5)
-1/2*x**(3/5)*y**2 - x**(3/5)*y - 1/2*x**(3/5) + x**(-7/5)
See Also
========
cos
"""
if rs_is_puiseux(p, x):
return rs_puiseux(rs_cos, p, x, prec)
R = p.ring
c = _get_constant_term(p, x)
if c:
if R.domain is EX:
c_expr = c.as_expr()
_, _ = sin(c_expr), cos(c_expr)
elif isinstance(c, PolyElement):
try:
c_expr = c.as_expr()
_, _ = R(sin(c_expr)), R(cos(c_expr))
except ValueError:
R = R.add_gens([sin(c_expr), cos(c_expr)])
p = p.set_ring(R)
x = x.set_ring(R)
c = c.set_ring(R)
else:
try:
_, _ = R(sin(c)), R(cos(c))
except ValueError:
raise DomainError("The given series cannot be expanded in "
"this domain.")
p1 = p - c
# Makes use of SymPy cos, sin functions to evaluate the values of the
# cos/sin of the constant term.
p_cos = rs_cos(p1, x, prec)
p_sin = rs_sin(p1, x, prec)
R = R.compose(p_cos.ring).compose(p_sin.ring)
p_cos.set_ring(R)
p_sin.set_ring(R)
t1, t2 = R(sin(c_expr)), R(cos(c_expr))
return p_cos*t2 - p_sin*t1
# Series is calculated in terms of tan as its evaluation is fast.
if len(p) > 20 and R.ngens == 1:
t = rs_tan(p/2, x, prec)
t2 = rs_square(t, x, prec)
p1 = rs_series_inversion(1+t2, x, prec)
return rs_mul(p1, 1 - t2, x, prec)
one = R(1)
n = 1
c = []
for k in range(2, prec + 2, 2):
c.append(one/n)
c.append(0)
n *= -k*(k - 1)
return rs_series_from_list(p, c, x, prec)
def rs_cos_sin(p, x, prec):
r"""
Return the tuple ``(rs_cos(p, x, prec)`, `rs_sin(p, x, prec))``.
Is faster than calling rs_cos and rs_sin separately
"""
if rs_is_puiseux(p, x):
return rs_puiseux(rs_cos_sin, p, x, prec)
t = rs_tan(p/2, x, prec)
t2 = rs_square(t, x, prec)
p1 = rs_series_inversion(1 + t2, x, prec)
return (rs_mul(p1, 1 - t2, x, prec), rs_mul(p1, 2*t, x, prec))
def _atanh(p, x, prec):
"""
Expansion using formula
Faster for very small and univariate series
"""
R = p.ring
one = R(1)
c = [one]
p2 = rs_square(p, x, prec)
for k in range(1, prec):
c.append(one/(2*k + 1))
s = rs_series_from_list(p2, c, x, prec)
s = rs_mul(s, p, x, prec)
return s
def rs_atanh(p, x, prec):
"""
Hyperbolic arctangent of a series
Return the series expansion of the atanh of ``p``, about 0.
Examples
========
>>> from sympy.polys.domains import QQ
>>> from sympy.polys.rings import ring
>>> from sympy.polys.ring_series import rs_atanh
>>> R, x, y = ring('x, y', QQ)
>>> rs_atanh(x + x*y, x, 4)
1/3*x**3*y**3 + x**3*y**2 + x**3*y + 1/3*x**3 + x*y + x
See Also
========
atanh
"""
if rs_is_puiseux(p, x):
return rs_puiseux(rs_atanh, p, x, prec)
R = p.ring
const = 0
if _has_constant_term(p, x):
zm = R.zero_monom
c = p[zm]
if R.domain is EX:
c_expr = c.as_expr()
const = atanh(c_expr)
elif isinstance(c, PolyElement):
try:
c_expr = c.as_expr()
const = R(atanh(c_expr))
except ValueError:
raise DomainError("The given series cannot be expanded in "
"this domain.")
else:
try:
const = R(atanh(c))
except ValueError:
raise DomainError("The given series cannot be expanded in "
"this domain.")
# Instead of using a closed form formula, we differentiate atanh(p) to get
# `1/(1-p**2) * dp`, whose series expansion is much easier to calculate.
# Finally we integrate to get back atanh
dp = rs_diff(p, x)
p1 = - rs_square(p, x, prec) + 1
p1 = rs_series_inversion(p1, x, prec - 1)
p1 = rs_mul(dp, p1, x, prec - 1)
return rs_integrate(p1, x) + const
def rs_sinh(p, x, prec):
"""
Hyperbolic sine of a series
Return the series expansion of the sinh of ``p``, about 0.
Examples
========
>>> from sympy.polys.domains import QQ
>>> from sympy.polys.rings import ring
>>> from sympy.polys.ring_series import rs_sinh
>>> R, x, y = ring('x, y', QQ)
>>> rs_sinh(x + x*y, x, 4)
1/6*x**3*y**3 + 1/2*x**3*y**2 + 1/2*x**3*y + 1/6*x**3 + x*y + x
See Also
========
sinh
"""
if rs_is_puiseux(p, x):
return rs_puiseux(rs_sinh, p, x, prec)
t = rs_exp(p, x, prec)
t1 = rs_series_inversion(t, x, prec)
return (t - t1)/2
def rs_cosh(p, x, prec):
"""
Hyperbolic cosine of a series
Return the series expansion of the cosh of ``p``, about 0.
Examples
========
>>> from sympy.polys.domains import QQ
>>> from sympy.polys.rings import ring
>>> from sympy.polys.ring_series import rs_cosh
>>> R, x, y = ring('x, y', QQ)
>>> rs_cosh(x + x*y, x, 4)
1/2*x**2*y**2 + x**2*y + 1/2*x**2 + 1
See Also
========
cosh
"""
if rs_is_puiseux(p, x):
return rs_puiseux(rs_cosh, p, x, prec)
t = rs_exp(p, x, prec)
t1 = rs_series_inversion(t, x, prec)
return (t + t1)/2
def _tanh(p, x, prec):
r"""
Helper function of :func:`rs_tanh`
Return the series expansion of tanh of a univariate series using Newton's
method. It takes advantage of the fact that series expansion of atanh is
easier than that of tanh.
See Also
========
_tanh
"""
R = p.ring
p1 = R(0)
for precx in _giant_steps(prec):
tmp = p - rs_atanh(p1, x, precx)
tmp = rs_mul(tmp, 1 - rs_square(p1, x, prec), x, precx)
p1 += tmp
return p1
def rs_tanh(p, x, prec):
"""
Hyperbolic tangent of a series
Return the series expansion of the tanh of ``p``, about 0.
Examples
========
>>> from sympy.polys.domains import QQ
>>> from sympy.polys.rings import ring
>>> from sympy.polys.ring_series import rs_tanh
>>> R, x, y = ring('x, y', QQ)
>>> rs_tanh(x + x*y, x, 4)
-1/3*x**3*y**3 - x**3*y**2 - x**3*y - 1/3*x**3 + x*y + x
See Also
========
tanh
"""
if rs_is_puiseux(p, x):
return rs_puiseux(rs_tanh, p, x, prec)
R = p.ring
const = 0
if _has_constant_term(p, x):
zm = R.zero_monom
c = p[zm]
if R.domain is EX:
c_expr = c.as_expr()
const = tanh(c_expr)
elif isinstance(c, PolyElement):
try:
c_expr = c.as_expr()
const = R(tanh(c_expr))
except ValueError:
raise DomainError("The given series cannot be expanded in "
"this domain.")
else:
try:
const = R(tanh(c))
except ValueError:
raise DomainError("The given series cannot be expanded in "
"this domain.")
p1 = p - c
t1 = rs_tanh(p1, x, prec)
t = rs_series_inversion(1 + const*t1, x, prec)
return rs_mul(const + t1, t, x, prec)
if R.ngens == 1:
return _tanh(p, x, prec)
else:
return rs_fun(p, _tanh, x, prec)
def rs_newton(p, x, prec):
"""
Compute the truncated Newton sum of the polynomial ``p``
Examples
========
>>> from sympy.polys.domains import QQ
>>> from sympy.polys.rings import ring
>>> from sympy.polys.ring_series import rs_newton
>>> R, x = ring('x', QQ)
>>> p = x**2 - 2
>>> rs_newton(p, x, 5)
8*x**4 + 4*x**2 + 2
"""
deg = p.degree()
p1 = _invert_monoms(p)
p2 = rs_series_inversion(p1, x, prec)
p3 = rs_mul(p1.diff(x), p2, x, prec)
res = deg - p3*x
return res
def rs_hadamard_exp(p1, inverse=False):
"""
Return ``sum f_i/i!*x**i`` from ``sum f_i*x**i``,
where ``x`` is the first variable.
If ``invers=True`` return ``sum f_i*i!*x**i``
Examples
========
>>> from sympy.polys.domains import QQ
>>> from sympy.polys.rings import ring
>>> from sympy.polys.ring_series import rs_hadamard_exp
>>> R, x = ring('x', QQ)
>>> p = 1 + x + x**2 + x**3
>>> rs_hadamard_exp(p)
1/6*x**3 + 1/2*x**2 + x + 1
"""
R = p1.ring
if R.domain != QQ:
raise NotImplementedError
p = R.zero
if not inverse:
for exp1, v1 in p1.items():
p[exp1] = v1/int(ifac(exp1[0]))
else:
for exp1, v1 in p1.items():
p[exp1] = v1*int(ifac(exp1[0]))
return p
def rs_compose_add(p1, p2):
"""
compute the composed sum ``prod(p2(x - beta) for beta root of p1)``
Examples
========
>>> from sympy.polys.domains import QQ
>>> from sympy.polys.rings import ring
>>> from sympy.polys.ring_series import rs_compose_add
>>> R, x = ring('x', QQ)
>>> f = x**2 - 2
>>> g = x**2 - 3
>>> rs_compose_add(f, g)
x**4 - 10*x**2 + 1
References
==========
.. [1] A. Bostan, P. Flajolet, B. Salvy and E. Schost
"Fast Computation with Two Algebraic Numbers",
(2002) Research Report 4579, Institut
National de Recherche en Informatique et en Automatique
"""
R = p1.ring
x = R.gens[0]
prec = p1.degree()*p2.degree() + 1
np1 = rs_newton(p1, x, prec)
np1e = rs_hadamard_exp(np1)
np2 = rs_newton(p2, x, prec)
np2e = rs_hadamard_exp(np2)
np3e = rs_mul(np1e, np2e, x, prec)
np3 = rs_hadamard_exp(np3e, True)
np3a = (np3[(0,)] - np3)/x
q = rs_integrate(np3a, x)
q = rs_exp(q, x, prec)
q = _invert_monoms(q)
q = q.primitive()[1]
dp = p1.degree()*p2.degree() - q.degree()
# `dp` is the multiplicity of the zeroes of the resultant;
# these zeroes are missed in this computation so they are put here.
# if p1 and p2 are monic irreducible polynomials,
# there are zeroes in the resultant
# if and only if p1 = p2 ; in fact in that case p1 and p2 have a
# root in common, so gcd(p1, p2) != 1; being p1 and p2 irreducible
# this means p1 = p2
if dp:
q = q*x**dp
return q
_convert_func = {
'sin': 'rs_sin',
'cos': 'rs_cos',
'exp': 'rs_exp',
'tan': 'rs_tan',
'log': 'rs_log'
}
def rs_min_pow(expr, series_rs, a):
"""Find the minimum power of `a` in the series expansion of expr"""
series = 0
n = 2
while series == 0:
series = _rs_series(expr, series_rs, a, n)
n *= 2
R = series.ring
a = R(a)
i = R.gens.index(a)
return min(series, key=lambda t: t[i])[i]
def _rs_series(expr, series_rs, a, prec):
# TODO Use _parallel_dict_from_expr instead of sring as sring is
# inefficient. For details, read the todo in sring.
args = expr.args
R = series_rs.ring
# expr does not contain any function to be expanded
if not any(arg.has(Function) for arg in args) and not expr.is_Function:
return series_rs
if not expr.has(a):
return series_rs
elif expr.is_Function:
arg = args[0]
if len(args) > 1:
raise NotImplementedError
R1, series = sring(arg, domain=QQ, expand=False, series=True)
series_inner = _rs_series(arg, series, a, prec)
# Why do we need to compose these three rings?
#
# We want to use a simple domain (like ``QQ`` or ``RR``) but they don't
# support symbolic coefficients. We need a ring that for example lets
# us have `sin(1)` and `cos(1)` as coefficients if we are expanding
# `sin(x + 1)`. The ``EX`` domain allows all symbolic coefficients, but
# that makes it very complex and hence slow.
#
# To solve this problem, we add only those symbolic elements as
# generators to our ring, that we need. Here, series_inner might
# involve terms like `sin(4)`, `exp(a)`, etc, which are not there in
# R1 or R. Hence, we compose these three rings to create one that has
# the generators of all three.
R = R.compose(R1).compose(series_inner.ring)
series_inner = series_inner.set_ring(R)
series = eval(_convert_func[str(expr.func)])(series_inner,
R(a), prec)
return series
elif expr.is_Mul:
n = len(args)
for arg in args: # XXX Looks redundant
if not arg.is_Number:
R1, _ = sring(arg, expand=False, series=True)
R = R.compose(R1)
min_pows = list(map(rs_min_pow, args, [R(arg) for arg in args],
[a]*len(args)))
sum_pows = sum(min_pows)
series = R(1)
for i in range(n):
_series = _rs_series(args[i], R(args[i]), a, prec - sum_pows +
min_pows[i])
R = R.compose(_series.ring)
_series = _series.set_ring(R)
series = series.set_ring(R)
series *= _series
series = rs_trunc(series, R(a), prec)
return series
elif expr.is_Add:
n = len(args)
series = R(0)
for i in range(n):
_series = _rs_series(args[i], R(args[i]), a, prec)
R = R.compose(_series.ring)
_series = _series.set_ring(R)
series = series.set_ring(R)
series += _series
return series
elif expr.is_Pow:
R1, _ = sring(expr.base, domain=QQ, expand=False, series=True)
R = R.compose(R1)
series_inner = _rs_series(expr.base, R(expr.base), a, prec)
return rs_pow(series_inner, expr.exp, series_inner.ring(a), prec)
# The `is_constant` method is buggy hence we check it at the end.
# See issue #9786 for details.
elif isinstance(expr, Expr) and expr.is_constant():
return sring(expr, domain=QQ, expand=False, series=True)[1]
else:
raise NotImplementedError
def rs_series(expr, a, prec):
"""Return the series expansion of an expression about 0.
Parameters
==========
expr : :class:`Expr`
a : :class:`Symbol` with respect to which expr is to be expanded
prec : order of the series expansion
Currently supports multivariate Taylor series expansion. This is much
faster that SymPy's series method as it uses sparse polynomial operations.
It automatically creates the simplest ring required to represent the series
expansion through repeated calls to sring.
Examples
========
>>> from sympy.polys.ring_series import rs_series
>>> from sympy import sin, cos, exp, tan, symbols, QQ
>>> a, b, c = symbols('a, b, c')
>>> rs_series(sin(a) + exp(a), a, 5)
1/24*a**4 + 1/2*a**2 + 2*a + 1
>>> series = rs_series(tan(a + b)*cos(a + c), a, 2)
>>> series.as_expr()
-a*sin(c)*tan(b) + a*cos(c)*tan(b)**2 + a*cos(c) + cos(c)*tan(b)
>>> series = rs_series(exp(a**QQ(1,3) + a**QQ(2, 5)), a, 1)
>>> series.as_expr()
a**(11/15) + a**(4/5)/2 + a**(2/5) + a**(2/3)/2 + a**(1/3) + 1
"""
R, series = sring(expr, domain=QQ, expand=False, series=True)
if a not in R.symbols:
R = R.add_gens([a, ])
series = series.set_ring(R)
series = _rs_series(expr, series, a, prec)
R = series.ring
gen = R(a)
prec_got = series.degree(gen) + 1
if prec_got >= prec:
return rs_trunc(series, gen, prec)
else:
# increase the requested number of terms to get the desired
# number keep increasing (up to 9) until the received order
# is different than the original order and then predict how
# many additional terms are needed
for more in range(1, 9):
p1 = _rs_series(expr, series, a, prec=prec + more)
gen = gen.set_ring(p1.ring)
new_prec = p1.degree(gen) + 1
if new_prec != prec_got:
prec_do = ceiling(prec + (prec - prec_got)*more/(new_prec -
prec_got))
p1 = _rs_series(expr, series, a, prec=prec_do)
while p1.degree(gen) + 1 < prec:
p1 = _rs_series(expr, series, a, prec=prec_do)
gen = gen.set_ring(p1.ring)
prec_do *= 2
break
else:
break
else:
raise ValueError('Could not calculate %s terms for %s'
% (str(prec), expr))
return rs_trunc(p1, gen, prec)
|