This file is indexed.

/usr/share/pyshared/sympy/simplify/simplify.py is in python-sympy 0.7.1.rc1-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
from sympy import SYMPY_DEBUG

from sympy.core import (Basic, S, C, Add, Mul, Pow, Rational, Integer,
    Derivative, Wild, Symbol, sympify, expand, expand_mul, expand_func,
    Function, Equality, Dummy, Atom, count_ops)

from sympy.core.compatibility import iterable
from sympy.core.numbers import igcd
from sympy.core.function import expand_log

from sympy.utilities import flatten
from sympy.functions import gamma, exp, sqrt, log

from sympy.simplify.cse_main import cse

from sympy.polys import (Poly, together, reduced, cancel, factor,
    ComputationFailed, terms_gcd)

from sympy.core.compatibility import reduce

import sympy.mpmath as mpmath

def fraction(expr, exact=False):
    """Returns a pair with expression's numerator and denominator.
       If the given expression is not a fraction then this function
       will return the tuple (expr, 1).

       This function will not make any attempt to simplify nested
       fractions or to do any term rewriting at all.

       If only one of the numerator/denominator pair is needed then
       use numer(expr) or denom(expr) functions respectively.

       >>> from sympy import fraction, Rational, Symbol
       >>> from sympy.abc import x, y

       >>> fraction(x/y)
       (x, y)
       >>> fraction(x)
       (x, 1)

       >>> fraction(1/y**2)
       (1, y**2)

       >>> fraction(x*y/2)
       (x*y, 2)
       >>> fraction(Rational(1, 2))
       (1, 2)

       This function will also work fine with assumptions:

       >>> k = Symbol('k', negative=True)
       >>> fraction(x * y**k)
       (x, y**(-k))

       If we know nothing about sign of some exponent and 'exact'
       flag is unset, then structure this exponent's structure will
       be analyzed and pretty fraction will be returned:

       >>> from sympy import exp
       >>> fraction(2*x**(-y))
       (2, x**y)

       >>> fraction(exp(-x))
       (1, exp(x))

       >>> fraction(exp(-x), exact=True)
       (exp(-x), 1)

    """
    expr = sympify(expr)

    numer, denom = [], []

    for term in Mul.make_args(expr):
        if term.is_Pow or term.func is exp:
            b, ex = term.as_base_exp()
            if ex.is_negative:
                if ex is S.NegativeOne:
                    denom.append(b)
                else:
                    denom.append(Pow(b, -ex))
            elif not exact and ex.is_Mul:
                n, d = term.as_numer_denom()
                numer.append(n)
                denom.append(d)
            else:
                numer.append(term)
        elif term.is_Rational:
            n, d = term.as_numer_denom()
            numer.append(n)
            denom.append(d)
        else:
            numer.append(term)

    return Mul(*numer), Mul(*denom)

def numer(expr):
    return fraction(expr)[0]

def denom(expr):
    return fraction(expr)[1]

def fraction_expand(expr):
    a, b = fraction(expr)
    return a.expand() / b.expand()

def numer_expand(expr):
    a, b = fraction(expr)
    return a.expand() / b

def denom_expand(expr):
    a, b = fraction(expr)
    return a / b.expand()

def separate(expr, deep=False, force=False):
    """A wrapper to expand(power_base=True) which separates a power
       with a base that is a Mul into a product of powers, without performing
       any other expansions, provided that assumptions about the power's base
       and exponent allow.

       deep=True (default is False) will do separations inside functions.

       force=True (default is False) will cause the expansion to ignore
       assumptions about the base and exponent. When False, the expansion will
       only happen if the base is non-negative or the exponent is an integer.

       >>> from sympy.abc import x, y, z
       >>> from sympy import separate, sin, cos, exp

       >>> (x*y)**2
       x**2*y**2

       >>> (2*x)**y
       (2*x)**y
       >>> separate(_)
       2**y*x**y

       >>> separate((x*y)**z)
       (x*y)**z
       >>> separate((x*y)**z, force=True)
       x**z*y**z
       >>> separate(sin((x*y)**z))
       sin((x*y)**z)
       >>> separate(sin((x*y)**z), deep=True, force=True)
       sin(x**z*y**z)

       >>> separate((2*sin(x))**y + (2*cos(x))**y)
       2**y*sin(x)**y + 2**y*cos(x)**y

       >>> separate((2*exp(y))**x)
       2**x*exp(x*y)

       >>> separate((2*cos(x))**y)
       2**y*cos(x)**y

       Notice that summations are left untouched. If this is not the
       desired behavior, apply 'expand' to the expression:

       >>> separate(((x+y)*z)**2)
       z**2*(x + y)**2
       >>> (((x+y)*z)**2).expand()
       x**2*z**2 + 2*x*y*z**2 + y**2*z**2

       >>> separate((2*y)**(1+z))
       2**(z + 1)*y**(z + 1)
       >>> ((2*y)**(1+z)).expand()
       2*2**z*y*y**z

    """
    return sympify(expr).expand(deep=deep, mul=False, power_exp=False,\
    power_base=True, basic=False, multinomial=False, log=False, force=force)

def collect(expr, syms, evaluate=True, exact=False):
    """
        Collect additive terms with respect to a list of symbols up
        to powers with rational exponents. By the term symbol here
        are meant arbitrary expressions, which can contain powers,
        products, sums etc. In other words symbol is a pattern
        which will be searched for in the expression's terms.

        This function will not apply any redundant expanding to the
        input expression, so user is assumed to enter expression in
        final form. This makes 'collect' more predictable as there
        is no magic behind the scenes. However it is important to
        note, that powers of products are converted to products of
        powers using 'separate' function.

        There are two possible types of output. First, if 'evaluate'
        flag is set, this function will return a single expression
        or else it will return a dictionary with separated symbols
        up to rational powers as keys and collected sub-expressions
        as values respectively.

        >>> from sympy import collect, sympify, Wild
        >>> from sympy.abc import a, b, c, x, y, z

        This function can collect symbolic coefficients in polynomial
        or rational expressions. It will manage to find all integer or
        rational powers of collection variable:

        >>> collect(a*x**2 + b*x**2 + a*x - b*x + c, x)
        c + x**2*(a + b) + x*(a - b)

        The same result can be achieved in dictionary form:

        >>> d = collect(a*x**2 + b*x**2 + a*x - b*x + c, x, evaluate=False)
        >>> d[x**2]
        a + b
        >>> d[x]
        a - b
        >>> d[sympify(1)]
        c

        You can also work with multi-variate polynomials. However
        remember that this function is greedy so it will care only
        about a single symbol at time, in specification order:

        >>> collect(x**2 + y*x**2 + x*y + y + a*y, [x, y])
        x**2*(y + 1) + x*y + y*(a + 1)

        Also more complicated expressions can be used as patterns:

        >>> from sympy import sin, log
        >>> collect(a*sin(2*x) + b*sin(2*x), sin(2*x))
        (a + b)*sin(2*x)

        >>> collect(a*x*log(x) + b*(x*log(x)), x*log(x))
        x*(a + b)*log(x)

        You can use wildcards in the pattern

        >>> w = Wild('w1')
        >>> collect(a*x**y - b*x**y, w**y)
        x**y*(a - b)

        It is also possible to work with symbolic powers, although
        it has more complicated behavior, because in this case
        power's base and symbolic part of the exponent are treated
        as a single symbol:

        >>> collect(a*x**c + b*x**c, x)
        a*x**c + b*x**c

        >>> collect(a*x**c + b*x**c, x**c)
        x**c*(a + b)

        However if you incorporate rationals to the exponents, then
        you will get well known behavior:

        >>> collect(a*x**(2*c) + b*x**(2*c), x**c)
        (a + b)*(x**2)**c

        Note also that all previously stated facts about 'collect'
        function apply to the exponential function, so you can get:

        >>> from sympy import exp
        >>> collect(a*exp(2*x) + b*exp(2*x), exp(x))
        (a + b)*exp(2*x)

        If you are interested only in collecting specific powers
        of some symbols then set 'exact' flag in arguments:

        >>> collect(a*x**7 + b*x**7, x, exact=True)
        a*x**7 + b*x**7

        >>> collect(a*x**7 + b*x**7, x**7, exact=True)
        x**7*(a + b)

        You can also apply this function to differential equations, where
        derivatives of arbitrary order can be collected.  Note that if you
        collect with respect to a function or a derivative of a function,
        all derivatives of that function will also be collected. Use
        exact=True to prevent this from happening:

        >>> from sympy import Derivative as D, collect, Function
        >>> f = Function('f') (x)

        >>> collect(a*D(f,x) + b*D(f,x), D(f,x))
        (a + b)*Derivative(f(x), x)

        >>> collect(a*D(D(f,x),x) + b*D(D(f,x),x), f)
        (a + b)*Derivative(f(x), x, x)

        >>> collect(a*D(D(f,x),x) + b*D(D(f,x),x), D(f,x), exact=True)
        a*Derivative(f(x), x, x) + b*Derivative(f(x), x, x)

        >>> collect(a*D(f,x) + b*D(f,x) + a*f + b*f, f,x)
        (a + b)*f(x) + (a + b)*Derivative(f(x), x)

        Or you can even match both derivative order and exponent at the same
        time.

        >>> collect(a*D(D(f,x),x)**2 + b*D(D(f,x),x)**2, D(f,x))
        (a + b)*Derivative(f(x), x, x)**2

        Note: arguments are expected to be in expanded form, so you might have
        to call expand() prior to calling this function.
    """
    def make_expression(terms):
        product = []

        for term, rat, sym, deriv in terms:
            if deriv is not None:
                var, order = deriv

                while order > 0:
                    term, order = Derivative(term, var), order-1

            if sym is None:
                if rat is S.One:
                    product.append(term)
                else:
                    product.append(Pow(term, rat))
            else:
                product.append(Pow(term, rat*sym))

        return Mul(*product)

    def parse_derivative(deriv):
        # scan derivatives tower in the input expression and return
        # underlying function and maximal differentiation order
        expr, sym, order = deriv.expr, deriv.variables[0], 1

        for s in deriv.variables[1:]:
            if s == sym:
                order += 1
            else:
                raise NotImplementedError('Improve MV Derivative support in collect')

        while isinstance(expr, Derivative):
            s0 = expr.variables[0]

            for s in expr.variables:
                if s != s0:
                    raise NotImplementedError('Improve MV Derivative support in collect')

            if s0 == sym:
                expr, order = expr.expr, order+len(expr.variables)
            else:
                break

        return expr, (sym, Rational(order))

    def parse_term(expr):
        """Parses expression expr and outputs tuple (sexpr, rat_expo, sym_expo, deriv)
        where:
         - sexpr is the base expression
         - rat_expo is the rational exponent that sexpr is raised to
         - sym_expo is the symbolic exponent that sexpr is raised to
         - deriv contains the derivatives the the expression

         for example, the output of x would be (x, 1, None, None)
         the output of 2**x would be (2, 1, x, None)
        """
        rat_expo, sym_expo = S.One, None
        sexpr, deriv = expr, None

        if expr.is_Pow:
            if isinstance(expr.base, Derivative):
                sexpr, deriv = parse_derivative(expr.base)
            else:
                sexpr = expr.base

            if expr.exp.is_Rational:
                rat_expo = expr.exp
            elif expr.exp.is_Mul:
                coeff, tail = expr.exp.as_coeff_mul()

                if coeff.is_Rational:
                    rat_expo, sym_expo = coeff, expr.exp._new_rawargs(*tail)
                else:
                    sym_expo = expr.exp
            else:
                sym_expo = expr.exp
        elif expr.func is C.exp:
            arg = expr.args[0]
            if arg.is_Rational:
                sexpr, rat_expo = S.Exp1, arg
            elif arg.is_Mul:
                coeff, tail = arg.as_coeff_mul()

                if coeff.is_Rational:
                    sexpr, rat_expo = C.exp(arg._new_rawargs(*tail)), coeff
        elif isinstance(expr, Derivative):
            sexpr, deriv = parse_derivative(expr)

        return sexpr, rat_expo, sym_expo, deriv

    def parse_expression(terms, pattern):
        """Parse terms searching for a pattern.
        terms is a list of tuples as returned by parse_terms;
        pattern is an expression treated as a product of factors
        """
        pattern = Mul.make_args(pattern)

        if len(terms) < len(pattern):
            # pattern is longer than  matched product
            # so no chance for positive parsing result
            return None
        else:
            pattern = [parse_term(elem) for elem in pattern]

            terms = terms[:] # need a copy
            elems, common_expo, has_deriv = [], None, False

            for elem, e_rat, e_sym, e_ord in pattern:

                if elem.is_Number:
                    # a constant is a match for everything
                    continue

                for j in range(len(terms)):
                    if terms[j] is None:
                        continue

                    term, t_rat, t_sym, t_ord = terms[j]

                    # keeping track of whether one of the terms had
                    # a derivative or not as this will require rebuilding
                    # the expression later
                    if t_ord is not None:
                        has_deriv= True

                    if (term.match(elem) is not None and \
                            (t_sym == e_sym or t_sym is not None and \
                            e_sym is not None and \
                            t_sym.match(e_sym) is not None)):
                        if exact == False:
                            # we don't have to be exact so find common exponent
                            # for both expression's term and pattern's element
                            expo = t_rat / e_rat

                            if common_expo is None:
                                # first time
                                common_expo = expo
                            else:
                                # common exponent was negotiated before so
                                # there is no chance for a pattern match unless
                                # common and current exponents are equal
                                if common_expo != expo:
                                    common_expo = 1
                        else:
                            # we ought to be exact so all fields of
                            # interest must match in every details
                            if e_rat != t_rat or e_ord != t_ord:
                                continue

                        # found common term so remove it from the expression
                        # and try to match next element in the pattern
                        elems.append(terms[j])
                        terms[j] = None

                        break

                else:
                    # pattern element not found
                    return None

            return filter(None, terms), elems, common_expo, has_deriv

    if evaluate:
        if expr.is_Mul:
            ret = 1
            for term in expr.args:
                ret *= collect(term, syms, True, exact)
            return ret
        elif expr.is_Pow:
            b = collect(expr.base, syms, True, exact)
            return Pow(b, expr.exp)

    summa = [separate(i) for i in Add.make_args(sympify(expr))]

    if hasattr(syms, '__iter__') or hasattr(syms, '__getitem__'):
        syms = [separate(s) for s in syms]
    else:
        syms = [separate(syms)]

    collected, disliked = {}, S.Zero
    for product in summa:
        terms = [parse_term(i) for i in Mul.make_args(product)]

        for symbol in syms:
            if SYMPY_DEBUG:
                print "DEBUG: parsing of expression %s with symbol %s " % (str(terms), str(symbol))

            result = parse_expression(terms, symbol)

            if SYMPY_DEBUG:
                print "DEBUG: returned %s" %  str(result)

            if result is not None:
                terms, elems, common_expo, has_deriv = result

                # when there was derivative in current pattern we
                # will need to rebuild its expression from scratch
                if not has_deriv:
                    index = 1
                    for elem in elems:
                        index *= Pow(elem[0], elem[1])
                        if elem[2] is not None:
                            index **= elem[2]
                else:
                    index = make_expression(elems)
                terms = separate(make_expression(terms))
                index = separate(index)
                if index in collected.keys():
                    collected[index] += terms
                else:
                    collected[index] = terms

                break
        else:
            # none of the patterns matched
            disliked += product

    if disliked is not S.Zero:
        collected[S.One] = disliked

    if evaluate:
        return Add(*[a*b for a, b in collected.iteritems()])
    else:
        return collected

def rcollect(expr, *vars):
    """
    Recursively collect sums in an expression.

    Example
    =======

    >>> from sympy.simplify import rcollect
    >>> from sympy.abc import x, y

    >>> expr = (x**2*y + x*y + x + y)/(x + y)

    >>> rcollect(expr, y)
    (x + y*(x**2 + x + 1))/(x + y)

    """
    if expr.is_Atom or not expr.has(*vars):
        return expr
    else:
        expr = expr.__class__(*[ rcollect(arg, *vars) for arg in expr.args ])

        if expr.is_Add:
            return collect(expr, vars)
        else:
            return expr

def separatevars(expr, symbols=[], dict=False, force=False):
    """
    Separates variables in an expression, if possible.  By
    default, it separates with respect to all symbols in an
    expression and collects constant coefficients that are
    independent of symbols.

    If dict=True then the separated terms will be returned
    in a dictionary keyed to their corresponding symbols.
    By default, all symbols in the expression will appear as
    keys; if symbols are provided, then all those symbols will
    be used as keys, and any terms in the expression containing
    other symbols or non-symbols will be returned keyed to the
    string 'coeff'.

    If force=True, then power bases will only be separated if assumptions allow.

    Note: the order of the factors is determined by Mul, so that the
    separated expressions may not necessarily be grouped together.

    Examples:
    >>> from sympy.abc import x, y, z, alpha
    >>> from sympy import separatevars, sin
    >>> separatevars((x*y)**y)
    (x*y)**y
    >>> separatevars((x*y)**y, force=True)
    x**y*y**y
    >>> separatevars(2*x**2*z*sin(y)+2*z*x**2)
    2*x**2*z*(sin(y) + 1)

    >>> separatevars(2*x+y*sin(x))
    2*x + y*sin(x)
    >>> separatevars(2*x**2*z*sin(y)+2*z*x**2, symbols=(x, y), dict=True)
    {'coeff': 2*z, x: x**2, y: sin(y) + 1}
    >>> separatevars(2*x**2*z*sin(y)+2*z*x**2, [x, y, alpha], dict=True)
    {'coeff': 2*z, alpha: 1, x: x**2, y: sin(y) + 1}

    If the expression is not really separable, or is only partially
    separable, separatevars will do the best it can to separate it.

    >>> separatevars(x+x*y-3*(x**2))
    -x*(3*x - y - 1)

    If the expression is not separable then expr is returned unchanged
    or (if dict=True) then None is returned.

    >>> eq = 2*x+y*sin(x)
    >>> separatevars(eq) == eq
    True
    >>> separatevars(2*x+y*sin(x), symbols=(x, y), dict=True) == None
    True

    """

    if dict:
        return _separatevars_dict(_separatevars(expr, force), *symbols)
    else:
        return _separatevars(expr, force)

def _separatevars(expr, force):
    # get a Pow ready for expansion
    if expr.is_Pow:
        expr = Pow(separatevars(expr.base, force=force), expr.exp)

    # First try other expansion methods
    expr = expr.expand(mul=False, multinomial=False, force=force)

    _expr = expr.expand(power_exp=False, deep=False, force=force)

    if not force:
        # factor will expand bases so we mask them off now
        pows = [p for p in _expr.atoms(Pow) if p.base.is_Mul]
        dums = [Dummy(str(i)) for i in xrange(len(pows))]
        _expr = _expr.subs(dict(zip(pows, dums)))

    _expr = factor(_expr, expand=False)

    if not force:
        # and retore them
        _expr = _expr.subs(dict(zip(dums, pows)))



    if not _expr.is_Add:
        expr = _expr

    if expr.is_Add:

        nonsepar = sympify(0)
        # Find any common coefficients to pull out
        commoncsetlist = []
        for i in expr.args:
            if i.is_Mul:
                commoncsetlist.append(set(i.args))
            else:
                commoncsetlist.append(set((i,)))
        commoncset = set(flatten(commoncsetlist))
        commonc = sympify(1)

        for i in commoncsetlist:
            commoncset = commoncset.intersection(i)
        commonc = Mul(*commoncset)

        for i in expr.args:
            coe = i.extract_multiplicatively(commonc)
            if coe == None:
                nonsepar += sympify(1)
            else:
                nonsepar += coe
        if nonsepar == 0:
            return commonc
        else:
            return commonc*nonsepar

    else:
        return expr

def _separatevars_dict(expr, *symbols):
    if symbols:
        assert all((t.is_Atom for t in symbols)), "symbols must be Atoms."

    ret = dict(((i, S.One) for i in symbols + ('coeff',)))

    for i in Mul.make_args(expr):
        expsym = i.free_symbols
        intersection = set(symbols).intersection(expsym)
        if len(intersection) > 1:
            return None
        if len(intersection) == 0:
            # There are no symbols, so it is part of the coefficient
            ret['coeff'] *= i
        else:
            ret[intersection.pop()] *= i

    return ret

def ratsimp(expr):
    """Put an expression over a common denominator, cancel and reduce.

    == Examples ==
        >>> from sympy import ratsimp
        >>> from sympy.abc import x, y
        >>> ratsimp(1/x + 1/y)
        (x + y)/(x*y)
    """

    f, g = cancel(expr).as_numer_denom()
    try:
        Q, r = reduced(f, [g], field=True, expand=False)
    except ComputationFailed:
        return f/g

    return Add(*Q) + cancel(r/g)

def trigsimp(expr, deep=False, recursive=False):
    """
    == Usage ==

    trigsimp(expr) -> reduces expression by using known trig identities

    == Notes ==

    deep:
    - Apply trigsimp inside functions

    recursive:
    - Use common subexpression elimination (cse()) and apply
    trigsimp recursively (recursively==True is quite expensive
    operation if the expression is large)

    == Examples ==
        >>> from sympy import trigsimp, sin, cos, log
        >>> from sympy.abc import x, y
        >>> e = 2*sin(x)**2 + 2*cos(x)**2
        >>> trigsimp(e)
        2
        >>> trigsimp(log(e))
        log(2*sin(x)**2 + 2*cos(x)**2)
        >>> trigsimp(log(e), deep=True)
        log(2)

    """
    sin, cos, tan, cot = C.sin, C.cos, C.tan, C.cot
    if not expr.has(sin, cos, tan, cot):
        return expr

    if recursive:
        w, g = cse(expr)
        g = trigsimp_nonrecursive(g[0])

        for sub in reversed(w):
            g = g.subs(sub[0], sub[1])
            g = trigsimp_nonrecursive(g)
        result = g
    else:
        result = trigsimp_nonrecursive(expr, deep)

    return result


def trigsimp_nonrecursive(expr, deep=False):
    """
    A nonrecursive trig simplifier, used from trigsimp.

    == Usage ==
        trigsimp_nonrecursive(expr) -> reduces expression by using known trig
                                       identities

    == Notes ==

    deep ........ apply trigsimp inside functions

    == Examples ==
        >>> from sympy import cos, sin, log
        >>> from sympy.simplify.simplify import trigsimp, trigsimp_nonrecursive
        >>> from sympy.abc import x, y
        >>> e = 2*sin(x)**2 + 2*cos(x)**2
        >>> trigsimp(e)
        2
        >>> trigsimp_nonrecursive(log(e))
        log(2*sin(x)**2 + 2*cos(x)**2)
        >>> trigsimp_nonrecursive(log(e), deep=True)
        log(2)

    """
    sin, cos, tan, cot = C.sin, C.cos, C.tan, C.cot

    if expr.is_Function:
        if deep:
            return expr.func(trigsimp_nonrecursive(expr.args[0], deep))
    elif expr.is_Mul:
        # do some simplifications like sin/cos -> tan:
        a,b,c = map(Wild, 'abc')
        matchers = (
                (a*sin(b)**c/cos(b)**c, a*tan(b)**c),
                (a*tan(b)**c*cos(b)**c, a*sin(b)**c),
                (a*cot(b)**c*sin(b)**c, a*cos(b)**c),
                (a*tan(b)**c/sin(b)**c, a/cos(b)**c),
                (a*cot(b)**c/cos(b)**c, a/sin(b)**c),
        )
        for pattern, simp in matchers:
            res = expr.match(pattern)
            if res is not None:
                # if c is missing or zero, do nothing:
                if (not c in res) or res[c] == 0:
                    continue
                # if "a" contains any of sin("b"), cos("b"), tan("b") or cot("b),
                # skip the simplification:
                if res[a].has(cos(res[b]), sin(res[b]), tan(res[b]), cot(res[b])):
                    continue
                # simplify and finish:
                expr = simp.subs(res)
                break
        if not expr.is_Mul:
            return trigsimp_nonrecursive(expr, deep)
        ret = S.One
        for x in expr.args:
            ret *= trigsimp_nonrecursive(x, deep)
        return ret
    elif expr.is_Pow:
        return Pow(trigsimp_nonrecursive(expr.base, deep),
                trigsimp_nonrecursive(expr.exp, deep))
    elif expr.is_Add:
        # TODO this needs to be faster

        # The types of trig functions we are looking for
        a,b,c = map(Wild, 'abc')
        matchers = (
            (a*sin(b)**2, a - a*cos(b)**2),
            (a*tan(b)**2, a*(1/cos(b))**2 - a),
            (a*cot(b)**2, a*(1/sin(b))**2 - a)
        )

        # Scan for the terms we need
        ret = S.Zero
        for term in expr.args:
            term = trigsimp_nonrecursive(term, deep)
            res = None
            for pattern, result in matchers:
                res = term.match(pattern)
                if res is not None:
                    ret += result.subs(res)
                    break
            if res is None:
                ret += term

        # Reduce any lingering artifacts, such as sin(x)**2 changing
        # to 1-cos(x)**2 when sin(x)**2 was "simpler"
        artifacts = (
            (a - a*cos(b)**2 + c, a*sin(b)**2 + c, cos),
            (a - a*(1/cos(b))**2 + c, -a*tan(b)**2 + c, cos),
            (a - a*(1/sin(b))**2 + c, -a*cot(b)**2 + c, sin)
        )

        expr = ret
        for pattern, result, ex in artifacts:
            # Substitute a new wild that excludes some function(s)
            # to help influence a better match. This is because
            # sometimes, for example, 'a' would match sec(x)**2
            a_t = Wild('a', exclude=[ex])
            pattern = pattern.subs(a, a_t)
            result = result.subs(a, a_t)
            if expr.is_number:
                continue

            m = expr.match(pattern)
            while m is not None:
                if m[a_t] == 0 or -m[a_t] in m[c].args or m[a_t] + m[c] == 0:
                    break
                expr = result.subs(m)
                m = expr.match(pattern)

        return expr
    return expr

def radsimp(expr):
    """
    Rationalize the denominator.

    Examples:
        >>> from sympy import radsimp, sqrt, Symbol
        >>> radsimp(1/(2+sqrt(2)))
        -2**(1/2)/2 + 1
        >>> x,y = map(Symbol, 'xy')
        >>> e = ((2+2*sqrt(2))*x+(2+sqrt(8))*y)/(2+sqrt(2))
        >>> radsimp(e)
        2**(1/2)*x + 2**(1/2)*y

    """
    n,d = fraction(expr)
    a,b,c = map(Wild, 'abc')
    r = d.match(a+b*sqrt(c))
    if r is not None:
        a = r[a]
        if r[b] == 0:
            b,c = 0,0
        else:
            b,c = r[b],r[c]

        syms = list(n.atoms(Symbol))
        n = collect((n*(a-b*sqrt(c))).expand(), syms)
        d = a**2 - c*b**2

    return n/d

def posify(eq):
    """Return eq (with generic symbols made positive) and a restore dictionary.

    Any symbol that has positive=None will be replaced with a positive dummy
    symbol having the same name. This replacement will allow more symbolic
    processing of expressions, especially those involving powers and logarithms.

    A dictionary that can be sent to subs to restore eq to its original symbols
    is also returned.

    >>> from sympy import posify, Symbol, log
    >>> from sympy.abc import x
    >>> posify(x + Symbol('p', positive=True) + Symbol('n', negative=True))
    (_x + n + p, {_x: x})

    >> log(1/x).expand() # should be log(1/x) but it comes back as -log(x)
    log(1/x)

    >>> log(posify(1/x)[0]).expand() # take [0] and ignore replacements
    -log(_x)
    >>> eq, rep = posify(1/x)
    >>> log(eq).expand().subs(rep)
    -log(x)
    >>> posify([x, 1 + x])
    ([_x, _x + 1], {_x: x})
    """
    eq = sympify(eq)
    if iterable(eq):
        f = type(eq)
        eq = list(eq)
        syms = set()
        for e in eq:
            syms = syms.union(e.atoms(C.Symbol))
        reps = {}
        for s in syms:
            reps.update(dict((v, k) for k, v in posify(s)[1].items()))
        for i, e in enumerate(eq):
            eq[i] = e.subs(reps)
        return f(eq), dict([(r,s) for s, r in reps.iteritems()])

    reps = dict([(s, Dummy(s.name, positive=True))
                 for s in eq.atoms(Symbol) if s.is_positive is None])
    eq = eq.subs(reps)
    return eq, dict([(r,s) for s, r in reps.iteritems()])

def powdenest(eq, force=False):
    """
    Collect exponents on powers as assumptions allow.

    Given (bb**be)**e, this can be simplified as follows:
        o if bb is positive or e is an integer, bb**(be*e)
        o if be has an integer in the denominatory, then
          all integers from its numerator can be joined with e
    Given a product of powers raised to a power, (bb1**be1 * bb2**be2...)**e,
    simplification can be done as follows:
        o if e is positive, the gcd of all bei can be joined with e;
        o all non-negative bb can be separated from those that are negative
          and their gcd can be joined with e; autosimplification already
          handles this separation.
        o integer factors from powers that have integers in the denominator
          of the exponent can be removed from any term and the gcd of such
          integers can be joined with e

    Setting ``force`` to True will make symbols that are not explicitly
    negative behave as though they are positive, resulting in more
    denesting.

    When there are sums of logs in exp() then a product of powers may be
    obtained e.g. exp(3*(log(a) + 2*log(b))) - > a**3*b**6.

    Examples:

    >>> from sympy.abc import a, b, x, y, z
    >>> from sympy import Symbol, exp, log, sqrt, symbols, powdenest

    >>> powdenest((x**(2*a/3))**(3*x))
    (x**(a/3))**(6*x)
    >>> powdenest(exp(3*x*log(2)))
    2**(3*x)

    Assumptions may prevent expansion:

    >> powdenest(sqrt(x**2))  # activate when log rules are fixed
    (x**2)**(1/2)

    >>> p = symbols('p', positive=True)
    >>> powdenest(sqrt(p**2))
    p

    No other expansion is done.

    >>> i, j = symbols('i,j', integer=1)
    >>> powdenest((x**x)**(i + j)) # -X-> (x**x)**i*(x**x)**j
    x**(x*(i + j))

    But exp() will be denested by moving all non-log terms outside of
    the function; this may result in the collapsing of the exp to a power
    with a different base:

    >>> powdenest(exp(3*y*log(x)))
    x**(3*y)
    >>> powdenest(exp(y*(log(a) + log(b))))
    (a*b)**y
    >>> powdenest(exp(3*(log(a) + log(b))))
    a**3*b**3

    If assumptions allow, symbols can also be moved to the outermost exponent:

    >>> i = Symbol('i', integer=True)
    >>> p = Symbol('p', positive=True)
    >>> powdenest(((x**(2*i))**(3*y))**x)
    ((x**(2*i))**(3*y))**x
    >>> powdenest(((x**(2*i))**(3*y))**x, force=1)
    x**(6*i*x*y)

    >> powdenest(((p**(2*a))**(3*y))**x)  # activate when log rules are fixed
    p**(6*a*x*y)

    >>> powdenest(((x**(2*a/3))**(3*y/i))**x)
    ((x**(a/3))**(y/i))**(6*x)
    >>> powdenest((x**(2*i)*y**(4*i))**z,1)
    (x*y**2)**(2*i*z)

    >>> n = Symbol('n', negative=1)

    >> powdenest((x**i)**y, force=1)  # activate when log rules are fixed
    x**(i*y)
    >> powdenest((n**i)**x, force=1)  # activate when log rules are fixed
    (n**i)**x

    """

    if force:
        eq, rep = posify(eq)
        return powdenest(eq, force=0).subs(rep)

    eq = S(eq)
    if eq.is_Atom:
        return eq

    # handle everything that is not a power
    #   if subs would work then one could replace the following with
    #      return eq.subs(dict([(p, powdenest(p)) for p in eq.atoms(Pow)]))
    #   but subs expands (3**x)**2 to 3**x * 3**x so the 3**(5*x)
    #   is not recognized; in addition, that would take 2 passes through
    #   the expression (once to find Pows and again to replace them). The
    #   following does it in one pass. Which is more important, efficiency
    #   or simplicity? On the other hand, this only does a shallow replacement
    #   and doesn't enter Integrals or functions, etc... so perhaps the subs
    #   approach (or adding a deep flag) is the thing to do.
    if not eq.is_Pow and not eq.func is exp:
        args = list(Add.make_args(eq))
        rebuild = False
        for i, arg in enumerate(args):
            margs = list(Mul.make_args(arg))
            changed = False
            for j, m in enumerate(margs):
                if not m.is_Pow:
                    continue
                m = powdenest(m, force=force)
                if m != margs[j]:
                    changed = True
                    margs[j] = m
            if changed:
                rebuild = True
                args[i] = C.Mul(*margs)
        if rebuild:
            eq = eq.func(*args)
        return eq

    b, e = eq.as_base_exp()

    # denest exp with log terms in exponent
    if b is S.Exp1 and e.is_Mul:
        logs = []
        other = []
        efunc = C.Mul
        for ei in Mul.make_args(e):
            if any(aj.func is C.log for a in Mul.make_args(ei)
                   for ai in Add.make_args(a) for aj in Mul.make_args(ai)):
                logs.append(ei)
            else:
                other.append(ei)
        logs = logcombine(efunc(*logs), force=force)
        return Pow(C.exp(logs), efunc(*other))

    bb, be = b.as_base_exp()
    if be is S.One and not (b.is_Mul or b.is_Rational):
        return eq

    # denest eq which is either Pow**e or Mul**e
    if force or e.is_integer:
        # replace all non-explicitly negative symbols with positive dummies
        syms = eq.atoms(Symbol)
        rep = [(s, C.Dummy(s.name, positive=True)) for s in syms if not s.is_negative]
        sub = eq.subs(rep)
    else:
        rep = []
        sub = eq

    # if any factor is a bare symbol then there is nothing to be done
    b, e = sub.as_base_exp()
    if e is S.One or any(s.is_Symbol for s in Mul.make_args(b)):
        return sub.subs([(new, old) for old, new in rep])
    # let log handle the case of the base of the argument being a mul, e.g.
    # sqrt(x**(2*i)*y**(6*i)) -> x**i*y**(3**i)
    gcd = terms_gcd(log(b).expand(log=1))
    if gcd.func is C.log or not gcd.is_Mul:
        if hasattr(gcd.args[0], 'exp'):
            gcd = powdenest(gcd.args[0])
            c, _ = gcd.exp.as_coeff_mul()
            ok = c.p != 1
            if ok:
                ok = c.q != 1
                if not ok:
                    n, d = gcd.exp.as_numer_denom()
                    ok = d is not S.One and any(di.is_integer for di in Mul.make_args(d))
            if ok:
                return Pow(Pow(gcd.base, gcd.exp/c.p), c.p*e)
        elif e.is_Mul:
            return Pow(b, e).subs([(new, old) for old, new in rep])
        return eq
    else:
        add= []
        other = []
        for g in gcd.args:
            if g.is_Add:
                add.append(g)
            else:
                other.append(g)
        return powdenest(Pow(exp(logcombine(Mul(*add))), e*Mul(*other))).subs([(new, old) for old, new in rep])

def powsimp(expr, deep=False, combine='all', force=False):
    """
    == Usage ==
        powsimp(expr, deep) -> reduces expression by combining powers with
        similar bases and exponents.

    == Notes ==
        If deep is True then powsimp() will also simplify arguments of
        functions. By default deep is set to False.

        If force is True then bases will be combined without checking for
        assumptions, e.g. sqrt(x)*sqrt(y) -> sqrt(x*y) which is not true
        if x and y are both negative.

        You can make powsimp() only combine bases or only combine exponents by
        changing combine='base' or combine='exp'.  By default, combine='all',
        which does both.  combine='base' will only combine::

             a   a          a                          2x      x
            x * y  =>  (x*y)   as well as things like 2   =>  4

        and combine='exp' will only combine
        ::

             a   b      (a + b)
            x * x  =>  x

        combine='exp' will strictly only combine exponents in the way that used
        to be automatic.  Also use deep=True if you need the old behavior.

        When combine='all', 'exp' is evaluated first.  Consider the first
        example below for when there could be an ambiguity relating to this.
        This is done so things like the second example can be completely
        combined.  If you want 'base' combined first, do something like
        powsimp(powsimp(expr, combine='base'), combine='exp').

    == Examples ==
        >>> from sympy import powsimp, exp, log, symbols
        >>> from sympy.abc import x, y, z, n
        >>> powsimp(x**y*x**z*y**z, combine='all')
        x**(y + z)*y**z
        >>> powsimp(x**y*x**z*y**z, combine='exp')
        x**(y + z)*y**z
        >>> powsimp(x**y*x**z*y**z, combine='base', force=True)
        x**y*(x*y)**z

        >>> powsimp(x**z*x**y*n**z*n**y, combine='all', force=True)
        (n*x)**(y + z)
        >>> powsimp(x**z*x**y*n**z*n**y, combine='exp')
        n**(y + z)*x**(y + z)
        >>> powsimp(x**z*x**y*n**z*n**y, combine='base', force=True)
        (n*x)**y*(n*x)**z

        >>> x, y = symbols('x y', positive=True)
        >>> powsimp(log(exp(x)*exp(y)))
        log(exp(x)*exp(y))
        >>> powsimp(log(exp(x)*exp(y)), deep=True)
        x + y

    """
    if combine not in ['all', 'exp', 'base']:
        raise ValueError("combine must be one of ('all', 'exp', 'base').")
    y = Dummy('y')
    if expr.is_Pow:
        if deep:
            return powsimp(y*powsimp(expr.base, deep, combine, force)**powsimp(\
            expr.exp, deep, combine, force), deep, combine, force)/y
        else:
            return powsimp(y*expr, deep, combine, force)/y # Trick it into being a Mul
    elif expr.is_Function:
        if expr.func is exp and deep:
            # Exp should really be like Pow
            return powsimp(y*exp(powsimp(expr.args[0], deep, combine, force)), deep, combine, force)/y
        elif expr.func is exp and not deep:
            return powsimp(y*expr, deep, combine, force)/y
        elif deep:
            return expr.func(*[powsimp(t, deep, combine, force) for t in expr.args])
        else:
            return expr
    elif expr.is_Add:
        return Add(*[powsimp(t, deep, combine, force) for t in expr.args])

    elif expr.is_Mul:
        if combine in ('exp', 'all'):
            # Collect base/exp data, while maintaining order in the
            # non-commutative parts of the product
            if combine is 'all' and deep and any((t.is_Add for t in expr.args)):
                # Once we get to 'base', there is no more 'exp', so we need to
                # distribute here.
                return powsimp(expand_mul(expr, deep=False), deep, combine, force)
            c_powers = {}
            nc_part = []
            newexpr = sympify(1)
            for term in expr.args:
                if term.is_Add and deep:
                    newexpr *= powsimp(term, deep, combine, force)
                else:
                    if term.is_commutative:
                        b, e = term.as_base_exp()
                        if deep:
                            b, e = [powsimp(i, deep, combine, force) for i in  [b, e]]
                        c_powers.setdefault(b, []).append(e)
                    else:
                        # This is the logic that combines exponents for equal,
                        # but non-commutative bases: A**x*A**y == A**(x+y).
                        if nc_part:
                            b1, e1 = nc_part[-1].as_base_exp()
                            b2, e2 = term.as_base_exp()
                            if (b1 == b2 and
                                e1.is_commutative and e2.is_commutative):
                                nc_part[-1] = Pow(b1, Add(e1, e2))
                                continue
                        nc_part.append(term)

            # add up exponents of common bases
            for b, e in c_powers.iteritems():
                c_powers[b] = Add(*e)

            # check for base and inverted base pairs
            be = c_powers.items()
            skip = set() # skip if we already saw them
            for b, e in be:
                if b in skip:
                    continue
                bpos = b.is_positive
                if bpos:
                    binv = 1/b
                    if b != binv and binv in c_powers:
                        if b.as_numer_denom()[0] is S.One:
                            c_powers.pop(b)
                            c_powers[binv] -= e
                        else:
                            skip.add(binv)
                            e = c_powers.pop(binv)
                            c_powers[b] -= e

            newexpr = Mul(*([newexpr] + [Pow(b, e) for b, e in c_powers.iteritems()]))
            if combine is 'exp':
                return Mul(newexpr, Mul(*nc_part))
            else:
                # combine is 'all', get stuff ready for 'base'
                if deep:
                    newexpr = expand_mul(newexpr, deep=False)
                if newexpr.is_Add:
                    return powsimp(Mul(*nc_part), deep, combine='base', force=force) * \
                           Add(*[powsimp(i, deep, combine='base', force=force)
                                 for i in newexpr.args])
                else:
                    return powsimp(Mul(*nc_part), deep, combine='base', force=force)*\
                    powsimp(newexpr, deep, combine='base', force=force)

        else:
            # combine is 'base'
            if deep:
                expr = expand_mul(expr, deep=False)
            if expr.is_Add:
                return Add(*[powsimp(i, deep, combine, force) for i in expr.args])
            else:
                # Build c_powers and nc_part.  These must both be lists not
                # dicts because exp's are not combined.
                c_powers = []
                nc_part = []
                for term in expr.args:
                    if term.is_commutative:
                        c_powers.append(list(term.as_base_exp()))
                    else:
                        # This is the logic that combines bases that are
                        # different and non-commutative, but with equal and
                        # commutative exponents: A**x*B**x == (A*B)**x.
                        if nc_part:
                            b1, e1 = nc_part[-1].as_base_exp()
                            b2, e2 = term.as_base_exp()
                            if (e1 == e2 and e2.is_commutative):
                                nc_part[-1] = Pow(Mul(b1, b2), e1)
                                continue
                        nc_part.append(term)

            # Pull out numerical coefficients from exponent if assumptions allow
            # e.g., 2**(2*x) => 4**x
            for i in xrange(len(c_powers)):
                b, e = c_powers[i]
                if not (b.is_nonnegative or e.is_integer or force):
                    continue
                exp_c, exp_t = e.as_coeff_mul()
                if not (exp_c is S.One) and exp_t:
                    c_powers[i] = [Pow(b, exp_c), e._new_rawargs(*exp_t)]


            # Combine bases whenever they have the same exponent and
            # assumptions allow

            # first gather the potential bases under the common exponent
            c_exp = {}
            for b, e in c_powers:
                if deep:
                    e = powsimp(e, deep, combine, force)
                c_exp.setdefault(e, []).append(b)
            del c_powers

            # Merge back in the results of the above to form a new product
            c_powers = {}
            for e in c_exp:
                bases = c_exp[e]

                # calculate the new base for e
                if len(bases) == 1:
                    new_base = bases[0]
                elif e.is_integer or force:
                    new_base = Mul(*bases)
                else:
                    # see which ones can be joined
                    unk=[]
                    nonneg=[]
                    neg=[]
                    for bi in bases:
                        if not bi.is_negative is None: #then we know the sign
                            if bi.is_negative:
                                neg.append(bi)
                            else:
                                nonneg.append(bi)
                        else:
                            unk.append(bi)
                    if len(unk) == 1 and not neg or len(neg) == 1 and not unk:
                        # a single neg or a single unk can join the rest
                        nonneg.extend(unk + neg)
                        unk = neg = []
                    elif neg:
                        # their negative signs cancel in pairs
                        neg = [-w for w in neg]
                        if len(neg) % 2:
                            unk.append(S.NegativeOne)

                    # these shouldn't be joined
                    for b in unk:
                        c_powers.setdefault(b, []).append(e)
                    # here is a new joined base
                    new_base = Mul(*(nonneg + neg))

                c_powers.setdefault(new_base, []).append(e)

            # break out the powers from c_powers now
            c_part = []
            if combine == 'all':
                #...joining the exponents
                for b, e in c_powers.iteritems():
                    c_part.append(Pow(b, Add(*e)))
            else:
                #...joining nothing
                for b, e in c_powers.iteritems():
                    for ei in e:
                        c_part.append(Pow(b, ei))

            # we're done
            return Mul(*(c_part + nc_part))

    else:
        return expr

def hypersimp(f, k):
    """Given combinatorial term f(k) simplify its consecutive term ratio
       i.e. f(k+1)/f(k).  The input term can be composed of functions and
       integer sequences which have equivalent representation in terms
       of gamma special function.

       The algorithm performs three basic steps:

           (1) Rewrite all functions in terms of gamma, if possible.

           (2) Rewrite all occurrences of gamma in terms of products
               of gamma and rising factorial with integer,  absolute
               constant exponent.

           (3) Perform simplification of nested fractions, powers
               and if the resulting expression is a quotient of
               polynomials, reduce their total degree.

       If f(k) is hypergeometric then as result we arrive with a
       quotient of polynomials of minimal degree. Otherwise None
       is returned.

       For more information on the implemented algorithm refer to:

       [1] W. Koepf, Algorithms for m-fold Hypergeometric Summation,
           Journal of Symbolic Computation (1995) 20, 399-417
    """
    f = sympify(f)

    g = f.subs(k, k+1) / f

    g = g.rewrite(gamma)
    g = expand_func(g)
    g = powsimp(g, deep=True, combine='exp')

    if g.is_rational_function(k):
        return simplify(g)
    else:
        return None

def hypersimilar(f, g, k):
    """Returns True if 'f' and 'g' are hyper-similar.

       Similarity in hypergeometric sense means that a quotient of
       f(k) and g(k) is a rational function in k.  This procedure
       is useful in solving recurrence relations.

       For more information see hypersimp().

    """
    f, g = map(sympify, (f, g))

    h = (f/g).rewrite(gamma)
    h = h.expand(func=True, basic=False)

    return h.is_rational_function(k)

def combsimp(expr):
    r"""
    Simplify combinatorial expressions.

    This function takes as input an expression containing factorials,
    binomials, Pochhammer symbol and other "combinatorial" functions,
    and tries to minimize the number of those functions and reduce
    the size of their arguments. The result is be given in terms of
    binomials and factorials.

    The algorithm works by rewriting all combinatorial functions as
    expressions involving rising factorials (Pochhammer symbols) and
    applies recurrence relations and other transformations applicable
    to rising factorials, to reduce their arguments, possibly letting
    the resulting rising factorial to cancel. Rising factorials with
    the second argument being an integer are expanded into polynomial
    forms and finally all other rising factorial are rewritten in terms
    more familiar binomials and factorials.

    All transformation rules can be found (or was derived from) here:

    1. http://functions.wolfram.com/GammaBetaErf/Pochhammer/17/01/02/
    2. http://functions.wolfram.com/GammaBetaErf/Pochhammer/27/01/0005/

    **Examples**

    >>> from sympy.simplify import combsimp
    >>> from sympy import factorial, binomial
    >>> from sympy.abc import n, k

    >>> combsimp(factorial(n)/factorial(n - 3))
    n*(n - 2)*(n - 1)
    >>> combsimp(binomial(n+1, k+1)/binomial(n, k))
    (n + 1)/(k + 1)

    """
    factorial = C.factorial
    binomial = C.binomial
    gamma = C.gamma

    def as_coeff_Add(expr):
        if expr.is_Add:
            coeff, args = expr.args[0], expr.args[1:]

            if coeff.is_Number:
                if len(args) == 1:
                    return coeff, args[0]
                else:
                    return coeff, expr._new_rawargs(*args)

        return S.Zero, expr

    class rf(Function):
        @classmethod
        def eval(cls, a, b):
            if b.is_Integer:
                if not b:
                    return S.Zero

                n, result = int(b), S.One

                if n > 0:
                    for i in xrange(0, n):
                        result *= a + i

                    return result
                else:
                    for i in xrange(1, -n+1):
                        result *= a - i

                    return 1/result
            else:
                c, _b = as_coeff_Add(b)

                if c.is_Integer:
                    if c > 0:
                        return rf(a, _b)*rf(a+_b, c)
                    elif c < 0:
                        return rf(a, _b)/rf(a+_b+c, -c)

                c, _a = as_coeff_Add(a)

                if c.is_Integer:
                    if c > 0:
                        return rf(_a, b)*rf(_a+b, c)/rf(_a, c)
                    elif c < 0:
                        return rf(_a, b)*rf(_a+c, -c)/rf(_a+b+c, -c)

    expr = expr.replace(binomial,
        lambda n, k: rf((n-k+1).expand(), k.expand())/rf(1, k.expand()))
    expr = expr.replace(factorial,
        lambda n: rf(1, n.expand()))
    expr = expr.replace(gamma,
        lambda n: rf(1, (n-1).expand()))

    expr = expr.replace(rf,
        lambda a, b: binomial(a+b-1, b)*factorial(b))

    def rule(n, k):
        coeff, rewrite = S.One, False

        cn, _n = as_coeff_Add(n)
        ck, _k = as_coeff_Add(k)

        if cn.is_Integer and cn:
            coeff *= rf(_n + 1, cn)/rf(_n - k + 1, cn)
            rewrite = True
            n = _n

        if ck.is_Integer and ck:
            coeff *= rf(n - ck - _k + 1, ck)/rf(_k + 1, ck)
            rewrite = True
            k = _k

        if rewrite:
            return coeff*binomial(n, k)

    expr = expr.replace(binomial, rule)

    return factor(expr)

def simplify(expr, ratio=1.7):
    """Naively simplifies the given expression.

       Simplification is not a well defined term and the exact strategies
       this function tries can change in the future versions of SymPy. If
       your algorithm relies on "simplification" (whatever it is), try to
       determine what you need exactly  -  is it powsimp()?, radsimp()?,
       together()?, logcombine()?, or something else? And use this particular
       function directly, because those are well defined and thus your algorithm
       will be robust.

       In some cases, applying :func:`simplify` may actually result in some more
       complicated expression.
       By default ``ratio=1.7`` prevents more extreme cases:
       if (result length)/(input length) > ratio, then input is returned
       unmodified (:func:`count_ops` is used to measure length).

       For example, if ``ratio=1``, ``simplify`` output can't be longer
       than input.

       ::

            >>> from sympy import S, simplify, count_ops, oo
            >>> root = S("(5/2 + 21**(1/2)/2)**(1/3)*(1/2 - I*3**(1/2)/2)"
            ... "+ 1/((1/2 - I*3**(1/2)/2)*(5/2 + 21**(1/2)/2)**(1/3))")

       Since ``simplify(root)`` would result in a slightly longer expression,
       root is returned inchanged instead::

            >>> simplify(root, ratio=1) is root
            True

       If ``ratio=oo``, simplify will be applied anyway::

            >>> count_ops(simplify(root, ratio=oo)) > count_ops(root)
            True

       Note that the shortest expression is not necessary the simplest, so
       setting ``ratio`` to 1 may not be a good idea.
       Heuristically, default value ``ratio=1.7`` seems like a reasonable choice.

    """
    expr = sympify(expr)

    if not isinstance(expr, Basic): # XXX: temporary hack
        return expr

    if isinstance(expr, Atom):
        return expr

    if isinstance(expr, C.Relational):
        return expr.__class__(simplify(expr.lhs, ratio=ratio),
                              simplify(expr.rhs, ratio=ratio))

    # TODO: Apply different strategies, considering expression pattern:
    # is it a purely rational function? Is there any trigonometric function?...
    # See also https://github.com/sympy/sympy/pull/185.

    original_expr = expr

    if expr.is_commutative is False:
        return together(powsimp(expr))

    expr = together(cancel(powsimp(expr)).expand())

    if not isinstance(expr, Basic): # XXX: temporary hack
        return expr

    if expr.has(C.TrigonometricFunction):
        expr = trigsimp(expr)

    if expr.has(C.log):
        expr = min([expand_log(expr, deep=True), logcombine(expr)],
                       key=count_ops)

    if expr.has(C.CombinatorialFunction, gamma):
        expr = combsimp(expr)

    expr = powsimp(expr, combine='exp', deep=True)
    numer, denom = expr.as_numer_denom()

    if denom.is_Add:
        a, b, c = map(Wild, 'abc')

        r = denom.match(a + b*c**S.Half)

        if r is not None and r[b]:
            a, b, c = r[a], r[b], r[c]

            numer *= a-b*c**S.Half
            numer = numer.expand()

            denom = a**2 - c*b**2

            expr = numer/denom

    if expr.could_extract_minus_sign():
        n, d = expr.as_numer_denom()
        if d != 0:
            expr = -n/(-d)

    if count_ops(expr) > ratio*count_ops(original_expr):
        return original_expr

    return expr

def _real_to_rational(expr):
    """
    Replace all reals in expr with rationals.

    >>> from sympy import nsimplify
    >>> from sympy.abc import x

    >>> nsimplify(.76 + .1*x**.5, rational=1)
    x**(1/2)/10 + 19/25

    """
    p = sympify(expr)
    for r in p.atoms(C.Float):
        newr = nsimplify(r)
        if not newr.is_Rational or \
           r.is_finite and not newr.is_finite:
            newr = r
            if newr < 0:
                s = -1
                newr *= s
            else:
                s = 1
            d = Pow(10, int((mpmath.log(newr)/mpmath.log(10))))
            newr = s*Rational(str(newr/d))*d
        p = p.subs(r, newr)
    return p

def nsimplify(expr, constants=[], tolerance=None, full=False, rational=False):
    """
    Replace numbers with simple representations.

    If rational=True then numbers are simply replaced with their rational
    equivalents.

    If rational=False, a simple formula that numerically matches the
    given expression is sought (and the input should be possible to evalf
    to a precision of at least 30 digits).

    Optionally, a list of (rationally independent) constants to
    include in the formula may be given.

    A lower tolerance may be set to find less exact matches.

    With full=True, a more extensive search is performed
    (this is useful to find simpler numbers when the tolerance
    is set low).

    Examples:

        >>> from sympy import nsimplify, sqrt, GoldenRatio, exp, I, exp, pi
        >>> nsimplify(4/(1+sqrt(5)), [GoldenRatio])
        -2 + 2*GoldenRatio
        >>> nsimplify((1/(exp(3*pi*I/5)+1)))
        1/2 - I*(5**(1/2)/10 + 1/4)**(1/2)
        >>> nsimplify(I**I, [pi])
        exp(-pi/2)
        >>> nsimplify(pi, tolerance=0.01)
        22/7

    """
    if rational:
        return _real_to_rational(expr)

    expr = sympify(expr)

    prec = 30
    bprec = int(prec*3.33)

    constants_dict = {}
    for constant in constants:
        constant = sympify(constant)
        v = constant.evalf(prec)
        if not v.is_Float:
            raise ValueError("constants must be real-valued")
        constants_dict[str(constant)] = v._to_mpmath(bprec)

    exprval = expr.evalf(prec, chop=True)
    re, im = exprval.as_real_imag()

    # Must be numerical
    if not ((re.is_Float or re.is_Integer) and (im.is_Float or im.is_Integer)):
        return expr

    def nsimplify_real(x):
        orig = mpmath.mp.dps
        xv = x._to_mpmath(bprec)
        try:
            # We'll be happy with low precision if a simple fraction
            if not (tolerance or full):
                mpmath.mp.dps = 15
                rat = mpmath.findpoly(xv, 1)
                if rat is not None:
                    return Rational(-int(rat[1]), int(rat[0]))
            mpmath.mp.dps = prec
            newexpr = mpmath.identify(xv, constants=constants_dict,
                tol=tolerance, full=full)
            if not newexpr:
                raise ValueError
            if full:
                newexpr = newexpr[0]
            return sympify(newexpr)
        finally:
            mpmath.mp.dps = orig
    try:
        if re: re = nsimplify_real(re)
        if im: im = nsimplify_real(im)
    except ValueError:
        return expr

    return re + im*S.ImaginaryUnit


def logcombine(expr, force=False):
    """
    Takes logarithms and combines them using the following rules:

    - log(x)+log(y) == log(x*y)
    - a*log(x) == log(x**a)

    These identities are only valid if x and y are positive and if a is real, so
    the function will not combine the terms unless the arguments have the proper
    assumptions on them.  Use logcombine(func, force=True) to
    automatically assume that the arguments of logs are positive and that
    coefficients are real.  Note that this will not change any assumptions
    already in place, so if the coefficient is imaginary or the argument
    negative, combine will still not combine the equations.  Change the
    assumptions on the variables to make them combine.

    Examples:
    >>> from sympy import Symbol, symbols, log, logcombine
    >>> from sympy.abc import a, x, y, z
    >>> logcombine(a*log(x)+log(y)-log(z))
    a*log(x) + log(y) - log(z)
    >>> logcombine(a*log(x)+log(y)-log(z), force=True)
    log(x**a*y/z)
    >>> x,y,z = symbols('x,y,z', positive=True)
    >>> a = Symbol('a', real=True)
    >>> logcombine(a*log(x)+log(y)-log(z))
    log(x**a*y/z)

    """
    # Try to make (a+bi)*log(x) == a*log(x)+bi*log(x).  This needs to be a
    # separate function call to avoid infinite recursion.
    expr = expand_mul(expr, deep=False)
    return _logcombine(expr, force)

def _logcombine(expr, force=False):
    """
    Does the main work for logcombine, it's a separate function to avoid an
    infinite recursion. See the docstrings of logcombine() for help.
    """
    def _getlogargs(expr):
        """
        Returns the arguments of the logarithm in an expression.
        Example:
        _getlogargs(a*log(x*y))
        x*y
        """
        if expr.func is log:
            return [expr.args[0]]
        else:
            args = []
            for i in expr.args:
                if i.func is log:
                    args.append(_getlogargs(i))
            return flatten(args)
        return None

    if type(expr) in (int, float) or expr.is_Number or expr.is_Rational or \
        expr.is_NumberSymbol or type(expr) == C.Integral:
        return expr

    if isinstance(expr, Equality):
        retval = Equality(_logcombine(expr.lhs-expr.rhs, force),\
        Integer(0))
        # If logcombine couldn't do much with the equality, try to make it like
        # it was.  Hopefully extract_additively won't become smart enought to
        # take logs apart :)
        right = retval.lhs.extract_additively(expr.lhs)
        if right:
            return Equality(expr.lhs, _logcombine(-right, force))
        else:
            return retval

    if expr.is_Add:
        argslist = 1
        notlogs = 0
        coeflogs = 0
        for i in expr.args:
            if i.func is log:
                if (i.args[0].is_positive or (force and not \
                i.args[0].is_nonpositive)):
                    argslist *= _logcombine(i.args[0], force)
                else:
                    notlogs += i
            elif i.is_Mul and any(map(lambda t: getattr(t,'func', False)==log,\
            i.args)):
                largs = _getlogargs(i)
                assert len(largs) != 0
                loglargs = 1
                for j in largs:
                    loglargs *= log(j)

                if  all(getattr(t,'is_positive') for t in largs)\
                    and getattr(i.extract_multiplicatively(loglargs),'is_real', False)\
                    or (force\
                        and not all(getattr(t,'is_nonpositive') for t in largs)\
                        and not getattr(i.extract_multiplicatively(loglargs),\
                        'is_real')==False):

                    coeflogs += _logcombine(i, force)
                else:
                    notlogs += i
            elif i.has(log):
                notlogs += _logcombine(i, force)
            else:
                notlogs += i
        if notlogs + log(argslist) + coeflogs == expr:
            return expr
        else:
            alllogs = _logcombine(log(argslist) + coeflogs, force)
            return notlogs + alllogs

    if expr.is_Mul:
        a = Wild('a')
        x = Wild('x')
        coef = expr.match(a*log(x))
        if coef\
            and (coef[a].is_real\
                or expr.is_Number\
                or expr.is_NumberSymbol\
                or type(coef[a]) in (int, float)\
                or (force\
                and not coef[a].is_imaginary))\
            and (coef[a].func != log\
                or force\
                or (not getattr(coef[a],'is_real')==False\
                    and getattr(x, 'is_positive'))):

            return log(coef[x]**coef[a])
        else:
            return _logcombine(expr.args[0], force)*reduce(lambda x, y:\
             _logcombine(x, force)*_logcombine(y, force),\
             expr.args[1:], 1)

    if expr.is_Function:
        return expr.func(*map(lambda t: _logcombine(t, force), expr.args))

    if expr.is_Pow:
        return _logcombine(expr.args[0], force)**\
        _logcombine(expr.args[1], force)

    return expr