Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

2028

2029

2030

2031

2032

2033

2034

2035

2036

2037

2038

2039

2040

2041

2042

2043

2044

2045

2046

2047

2048

2049

2050

2051

2052

2053

2054

2055

2056

2057

2058

2059

2060

2061

2062

2063

2064

2065

2066

2067

2068

2069

2070

2071

2072

2073

2074

2075

2076

2077

2078

2079

2080

2081

2082

2083

2084

2085

2086

2087

2088

2089

2090

2091

2092

2093

2094

2095

2096

2097

2098

2099

2100

2101

2102

2103

2104

2105

2106

2107

2108

2109

2110

2111

2112

2113

2114

2115

2116

2117

2118

2119

2120

2121

2122

2123

2124

2125

2126

2127

2128

2129

2130

2131

2132

2133

2134

2135

2136

""" 

Implementation of the trigsimp algorithm by Fu et al. 

 

The idea behind the ``fu`` algorithm is to use a sequence of rules, applied 

in what is heuristically known to be a smart order, to select a simpler 

expression that is equivalent to the input. 

 

There are transform rules in which a single rule is applied to the 

expression tree. The following are just mnemonic in nature; see the 

docstrings for examples. 

 

TR0 - simplify expression 

TR1 - sec-csc to cos-sin 

TR2 - tan-cot to sin-cos ratio 

TR2i - sin-cos ratio to tan 

TR3 - angle canonicalization 

TR4 - functions at special angles 

TR5 - powers of sin to powers of cos 

TR6 - powers of cos to powers of sin 

TR7 - reduce cos power (increase angle) 

TR8 - expand products of sin-cos to sums 

TR9 - contract sums of sin-cos to products 

TR10 - separate sin-cos arguments 

TR10i - collect sin-cos arguments 

TR11 - reduce double angles 

TR12 - separate tan arguments 

TR12i - collect tan arguments 

TR13 - expand product of tan-cot 

TRmorrie - prod(cos(x*2**i), (i, 0, k - 1)) -> sin(2**k*x)/(2**k*sin(x)) 

TR14 - factored powers of sin or cos to cos or sin power 

TR15 - negative powers of sin to cot power 

TR16 - negative powers of cos to tan power 

TR22 - tan-cot powers to negative powers of sec-csc functions 

TR111 - negative sin-cos-tan powers to csc-sec-cot 

 

There are 4 combination transforms (CTR1 - CTR4) in which a sequence of 

transformations are applied and the simplest expression is selected from 

a few options. 

 

Finally, there are the 2 rule lists (RL1 and RL2), which apply a 

sequence of transformations and combined transformations, and the ``fu`` 

algorithm itself, which applies rules and rule lists and selects the 

best expressions. There is also a function ``L`` which counts the number 

of trigonometric functions that appear in the expression. 

 

Other than TR0, re-writing of expressions is not done by the transformations. 

e.g. TR10i finds pairs of terms in a sum that are in the form like 

``cos(x)*cos(y) + sin(x)*sin(y)``. Such expression are targeted in a bottom-up 

traversal of the expression, but no manipulation to make them appear is 

attempted. For example, 

 

Set-up for examples below: 

 

>>> from sympy.simplify.fu import fu, L, TR9, TR10i, TR11 

>>> from sympy import factor, sin, cos, powsimp 

>>> from sympy.abc import x, y, z, a 

>>> from time import time 

 

>>> eq = cos(x + y)/cos(x) 

>>> TR10i(eq.expand(trig=True)) 

-sin(x)*sin(y)/cos(x) + cos(y) 

 

If the expression is put in "normal" form (with a common denominator) then 

the transformation is successful: 

 

>>> TR10i(_.normal()) 

cos(x + y)/cos(x) 

 

TR11's behavior is similar. It rewrites double angles as smaller angles but 

doesn't do any simplification of the result. 

 

>>> TR11(sin(2)**a*cos(1)**(-a), 1) 

(2*sin(1)*cos(1))**a*cos(1)**(-a) 

>>> powsimp(_) 

(2*sin(1))**a 

 

The temptation is to try make these TR rules "smarter" but that should really 

be done at a higher level; the TR rules should try maintain the "do one thing 

well" principle. There is one exception, however. In TR10i and TR9 terms are 

recognized even when they are each multiplied by a common factor: 

 

>>> fu(a*cos(x)*cos(y) + a*sin(x)*sin(y)) 

a*cos(x - y) 

 

Factoring with ``factor_terms`` is used but it it "JIT"-like, being delayed 

until it is deemed necessary. Furthermore, if the factoring does not 

help with the simplification, it is not retained, so 

``a*cos(x)*cos(y) + a*sin(x)*sin(z)`` does not become the factored 

(but unsimplified in the trigonometric sense) expression: 

 

>>> fu(a*cos(x)*cos(y) + a*sin(x)*sin(z)) 

a*sin(x)*sin(z) + a*cos(x)*cos(y) 

 

In some cases factoring might be a good idea, but the user is left 

to make that decision. For example: 

 

>>> expr=((15*sin(2*x) + 19*sin(x + y) + 17*sin(x + z) + 19*cos(x - z) + 

... 25)*(20*sin(2*x) + 15*sin(x + y) + sin(y + z) + 14*cos(x - z) + 

... 14*cos(y - z))*(9*sin(2*y) + 12*sin(y + z) + 10*cos(x - y) + 2*cos(y - 

... z) + 18)).expand(trig=True).expand() 

 

In the expanded state, there are nearly 1000 trig functions: 

 

>>> L(expr) 

932 

 

If the expression where factored first, this would take time but the 

resulting expression would be transformed very quickly: 

 

>>> def clock(f, n=2): 

... t=time(); f(); return round(time()-t, n) 

... 

>>> clock(lambda: factor(expr)) # doctest: +SKIP 

0.86 

>>> clock(lambda: TR10i(expr), 3) # doctest: +SKIP 

0.016 

 

If the unexpanded expression is used, the transformation takes longer but 

not as long as it took to factor it and then transform it: 

 

>>> clock(lambda: TR10i(expr), 2) # doctest: +SKIP 

0.28 

 

So neither expansion nor factoring is used in ``TR10i``: if the 

expression is already factored (or partially factored) then expansion 

with ``trig=True`` would destroy what is already known and take 

longer; if the expression is expanded, factoring may take longer than 

simply applying the transformation itself. 

 

Although the algorithms should be canonical, always giving the same 

result, they may not yield the best result. This, in general, is 

the nature of simplification where searching all possible transformation 

paths is very expensive. Here is a simple example. There are 6 terms 

in the following sum: 

 

>>> expr = (sin(x)**2*cos(y)*cos(z) + sin(x)*sin(y)*cos(x)*cos(z) + 

... sin(x)*sin(z)*cos(x)*cos(y) + sin(y)*sin(z)*cos(x)**2 + sin(y)*sin(z) + 

... cos(y)*cos(z)) 

>>> args = expr.args 

 

Serendipitously, fu gives the best result: 

 

>>> fu(expr) 

3*cos(y - z)/2 - cos(2*x + y + z)/2 

 

But if different terms were combined, a less-optimal result might be 

obtained, requiring some additional work to get better simplification, 

but still less than optimal. The following shows an alternative form 

of ``expr`` that resists optimal simplification once a given step 

is taken since it leads to a dead end: 

 

>>> TR9(-cos(x)**2*cos(y + z) + 3*cos(y - z)/2 + 

... cos(y + z)/2 + cos(-2*x + y + z)/4 - cos(2*x + y + z)/4) 

sin(2*x)*sin(y + z)/2 - cos(x)**2*cos(y + z) + 3*cos(y - z)/2 + cos(y + z)/2 

 

Here is a smaller expression that exhibits the same behavior: 

 

>>> a = sin(x)*sin(z)*cos(x)*cos(y) + sin(x)*sin(y)*cos(x)*cos(z) 

>>> TR10i(a) 

sin(x)*sin(y + z)*cos(x) 

>>> newa = _ 

>>> TR10i(expr - a) # this combines two more of the remaining terms 

sin(x)**2*cos(y)*cos(z) + sin(y)*sin(z)*cos(x)**2 + cos(y - z) 

>>> TR10i(_ + newa) == _ + newa # but now there is no more simplification 

True 

 

Without getting lucky or trying all possible pairings of arguments, the 

final result may be less than optimal and impossible to find without 

better heuristics or brute force trial of all possibilities. 

 

Notes 

===== 

 

This work was started by Dimitar Vlahovski at the Technological School 

"Electronic systems" (30.11.2011). 

 

References 

========== 

 

Fu, Hongguang, Xiuqin Zhong, and Zhenbing Zeng. "Automated and readable 

simplification of trigonometric expressions." Mathematical and computer 

modelling 44.11 (2006): 1169-1177. 

http://rfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/DESTIME2006/DES_contribs/Fu/simplification.pdf 

 

http://www.sosmath.com/trig/Trig5/trig5/pdf/pdf.html gives a formula sheet. 

 

""" 

 

from __future__ import print_function, division 

 

from collections import defaultdict 

 

from sympy.simplify.simplify import bottom_up 

from sympy.core.sympify import sympify 

from sympy.functions.elementary.trigonometric import ( 

cos, sin, tan, cot, sec, csc, sqrt, TrigonometricFunction) 

from sympy.functions.elementary.hyperbolic import ( 

cosh, sinh, tanh, coth, HyperbolicFunction) 

from sympy.core.compatibility import ordered, range 

from sympy.core.expr import Expr 

from sympy.core.mul import Mul 

from sympy.core.power import Pow 

from sympy.core.function import expand_mul 

from sympy.core.add import Add 

from sympy.core.symbol import Dummy 

from sympy.core.exprtools import Factors, gcd_terms, factor_terms 

from sympy.core.basic import S 

from sympy.core.numbers import pi, I 

from sympy.strategies.tree import greedy 

from sympy.strategies.core import identity, debug 

from sympy.polys.polytools import factor 

from sympy.ntheory.factor_ import perfect_power 

 

from sympy import SYMPY_DEBUG 

 

 

# ================== Fu-like tools =========================== 

 

 

def TR0(rv): 

"""Simplification of rational polynomials, trying to simplify 

the expression, e.g. combine things like 3*x + 2*x, etc.... 

""" 

# although it would be nice to use cancel, it doesn't work 

# with noncommutatives 

return rv.normal().factor().expand() 

 

 

def TR1(rv): 

"""Replace sec, csc with 1/cos, 1/sin 

 

Examples 

======== 

 

>>> from sympy.simplify.fu import TR1, sec, csc 

>>> from sympy.abc import x 

>>> TR1(2*csc(x) + sec(x)) 

1/cos(x) + 2/sin(x) 

""" 

 

def f(rv): 

if rv.func is sec: 

a = rv.args[0] 

return S.One/cos(a) 

elif rv.func is csc: 

a = rv.args[0] 

return S.One/sin(a) 

return rv 

 

return bottom_up(rv, f) 

 

 

def TR2(rv): 

"""Replace tan and cot with sin/cos and cos/sin 

 

Examples 

======== 

 

>>> from sympy.simplify.fu import TR2 

>>> from sympy.abc import x 

>>> from sympy import tan, cot, sin, cos 

>>> TR2(tan(x)) 

sin(x)/cos(x) 

>>> TR2(cot(x)) 

cos(x)/sin(x) 

>>> TR2(tan(tan(x) - sin(x)/cos(x))) 

0 

 

""" 

 

def f(rv): 

if rv.func is tan: 

a = rv.args[0] 

return sin(a)/cos(a) 

elif rv.func is cot: 

a = rv.args[0] 

return cos(a)/sin(a) 

return rv 

 

return bottom_up(rv, f) 

 

 

def TR2i(rv, half=False): 

"""Converts ratios involving sin and cos as follows:: 

sin(x)/cos(x) -> tan(x) 

sin(x)/(cos(x) + 1) -> tan(x/2) if half=True 

 

Examples 

======== 

 

>>> from sympy.simplify.fu import TR2i 

>>> from sympy.abc import x, a 

>>> from sympy import sin, cos 

>>> TR2i(sin(x)/cos(x)) 

tan(x) 

 

Powers of the numerator and denominator are also recognized 

 

>>> TR2i(sin(x)**2/(cos(x) + 1)**2, half=True) 

tan(x/2)**2 

 

The transformation does not take place unless assumptions allow 

(i.e. the base must be positive or the exponent must be an integer 

for both numerator and denominator) 

 

>>> TR2i(sin(x)**a/(cos(x) + 1)**a) 

(cos(x) + 1)**(-a)*sin(x)**a 

 

""" 

 

def f(rv): 

if not rv.is_Mul: 

return rv 

 

n, d = rv.as_numer_denom() 

if n.is_Atom or d.is_Atom: 

return rv 

 

def ok(k, e): 

# initial filtering of factors 

return ( 

(e.is_integer or k.is_positive) and ( 

k.func in (sin, cos) or (half and 

k.is_Add and 

len(k.args) >= 2 and 

any(any(ai.func is cos or ai.is_Pow and ai.base is cos 

for ai in Mul.make_args(a)) for a in k.args)))) 

 

n = n.as_powers_dict() 

ndone = [(k, n.pop(k)) for k in list(n.keys()) if not ok(k, n[k])] 

if not n: 

return rv 

 

d = d.as_powers_dict() 

ddone = [(k, d.pop(k)) for k in list(d.keys()) if not ok(k, d[k])] 

if not d: 

return rv 

 

# factoring if necessary 

 

def factorize(d, ddone): 

newk = [] 

for k in d: 

if k.is_Add and len(k.args) > 1: 

knew = factor(k) if half else factor_terms(k) 

if knew != k: 

newk.append((k, knew)) 

if newk: 

for i, (k, knew) in enumerate(newk): 

del d[k] 

newk[i] = knew 

newk = Mul(*newk).as_powers_dict() 

for k in newk: 

v = d[k] + newk[k] 

if ok(k, v): 

d[k] = v 

else: 

ddone.append((k, v)) 

del newk 

factorize(n, ndone) 

factorize(d, ddone) 

 

# joining 

t = [] 

for k in n: 

if k.func is sin: 

a = cos(k.args[0], evaluate=False) 

if a in d and d[a] == n[k]: 

t.append(tan(k.args[0])**n[k]) 

n[k] = d[a] = None 

elif half: 

a1 = 1 + a 

if a1 in d and d[a1] == n[k]: 

t.append((tan(k.args[0]/2))**n[k]) 

n[k] = d[a1] = None 

elif k.func is cos: 

a = sin(k.args[0], evaluate=False) 

if a in d and d[a] == n[k]: 

t.append(tan(k.args[0])**-n[k]) 

n[k] = d[a] = None 

elif half and k.is_Add and k.args[0] is S.One and \ 

k.args[1].func is cos: 

a = sin(k.args[1].args[0], evaluate=False) 

if a in d and d[a] == n[k] and (d[a].is_integer or \ 

a.is_positive): 

t.append(tan(a.args[0]/2)**-n[k]) 

n[k] = d[a] = None 

 

if t: 

rv = Mul(*(t + [b**e for b, e in n.items() if e]))/\ 

Mul(*[b**e for b, e in d.items() if e]) 

rv *= Mul(*[b**e for b, e in ndone])/Mul(*[b**e for b, e in ddone]) 

 

return rv 

 

return bottom_up(rv, f) 

 

 

def TR3(rv): 

"""Induced formula: example sin(-a) = -sin(a) 

 

Examples 

======== 

 

>>> from sympy.simplify.fu import TR3 

>>> from sympy.abc import x, y 

>>> from sympy import pi 

>>> from sympy import cos 

>>> TR3(cos(y - x*(y - x))) 

cos(x*(x - y) + y) 

>>> cos(pi/2 + x) 

-sin(x) 

>>> cos(30*pi/2 + x) 

-cos(x) 

 

""" 

from sympy.simplify.simplify import signsimp 

 

# Negative argument (already automatic for funcs like sin(-x) -> -sin(x) 

# but more complicated expressions can use it, too). Also, trig angles 

# between pi/4 and pi/2 are not reduced to an angle between 0 and pi/4. 

# The following are automatically handled: 

# Argument of type: pi/2 +/- angle 

# Argument of type: pi +/- angle 

# Argument of type : 2k*pi +/- angle 

 

def f(rv): 

if not isinstance(rv, TrigonometricFunction): 

return rv 

rv = rv.func(signsimp(rv.args[0])) 

if (rv.args[0] - S.Pi/4).is_positive is (S.Pi/2 - rv.args[0]).is_positive is True: 

fmap = {cos: sin, sin: cos, tan: cot, cot: tan, sec: csc, csc: sec} 

rv = fmap[rv.func](S.Pi/2 - rv.args[0]) 

return rv 

 

return bottom_up(rv, f) 

 

 

def TR4(rv): 

"""Identify values of special angles. 

 

a= 0 pi/6 pi/4 pi/3 pi/2 

---------------------------------------------------- 

cos(a) 0 1/2 sqrt(2)/2 sqrt(3)/2 1 

sin(a) 1 sqrt(3)/2 sqrt(2)/2 1/2 0 

tan(a) 0 sqt(3)/3 1 sqrt(3) -- 

 

Examples 

======== 

 

>>> from sympy.simplify.fu import TR4 

>>> from sympy import pi 

>>> from sympy import cos, sin, tan, cot 

>>> for s in (0, pi/6, pi/4, pi/3, pi/2): 

... print('%s %s %s %s' % (cos(s), sin(s), tan(s), cot(s))) 

... 

1 0 0 zoo 

sqrt(3)/2 1/2 sqrt(3)/3 sqrt(3) 

sqrt(2)/2 sqrt(2)/2 1 1 

1/2 sqrt(3)/2 sqrt(3) sqrt(3)/3 

0 1 zoo 0 

""" 

# special values at 0, pi/6, pi/4, pi/3, pi/2 already handled 

return rv 

 

 

def _TR56(rv, f, g, h, max, pow): 

"""Helper for TR5 and TR6 to replace f**2 with h(g**2) 

 

Options 

======= 

 

max : controls size of exponent that can appear on f 

e.g. if max=4 then f**4 will be changed to h(g**2)**2. 

pow : controls whether the exponent must be a perfect power of 2 

e.g. if pow=True (and max >= 6) then f**6 will not be changed 

but f**8 will be changed to h(g**2)**4 

 

>>> from sympy.simplify.fu import _TR56 as T 

>>> from sympy.abc import x 

>>> from sympy import sin, cos 

>>> h = lambda x: 1 - x 

>>> T(sin(x)**3, sin, cos, h, 4, False) 

sin(x)**3 

>>> T(sin(x)**6, sin, cos, h, 6, False) 

(-cos(x)**2 + 1)**3 

>>> T(sin(x)**6, sin, cos, h, 6, True) 

sin(x)**6 

>>> T(sin(x)**8, sin, cos, h, 10, True) 

(-cos(x)**2 + 1)**4 

""" 

 

def _f(rv): 

# I'm not sure if this transformation should target all even powers 

# or only those expressible as powers of 2. Also, should it only 

# make the changes in powers that appear in sums -- making an isolated 

# change is not going to allow a simplification as far as I can tell. 

if not (rv.is_Pow and rv.base.func == f): 

return rv 

 

if (rv.exp < 0) == True: 

return rv 

if (rv.exp > max) == True: 

return rv 

if rv.exp == 2: 

return h(g(rv.base.args[0])**2) 

else: 

if rv.exp == 4: 

e = 2 

elif not pow: 

if rv.exp % 2: 

return rv 

e = rv.exp//2 

else: 

p = perfect_power(rv.exp) 

if not p: 

return rv 

e = rv.exp//2 

return h(g(rv.base.args[0])**2)**e 

 

return bottom_up(rv, _f) 

 

 

def TR5(rv, max=4, pow=False): 

"""Replacement of sin**2 with 1 - cos(x)**2. 

 

See _TR56 docstring for advanced use of ``max`` and ``pow``. 

 

Examples 

======== 

 

>>> from sympy.simplify.fu import TR5 

>>> from sympy.abc import x 

>>> from sympy import sin 

>>> TR5(sin(x)**2) 

-cos(x)**2 + 1 

>>> TR5(sin(x)**-2) # unchanged 

sin(x)**(-2) 

>>> TR5(sin(x)**4) 

(-cos(x)**2 + 1)**2 

""" 

return _TR56(rv, sin, cos, lambda x: 1 - x, max=max, pow=pow) 

 

 

def TR6(rv, max=4, pow=False): 

"""Replacement of cos**2 with 1 - sin(x)**2. 

 

See _TR56 docstring for advanced use of ``max`` and ``pow``. 

 

Examples 

======== 

 

>>> from sympy.simplify.fu import TR6 

>>> from sympy.abc import x 

>>> from sympy import cos 

>>> TR6(cos(x)**2) 

-sin(x)**2 + 1 

>>> TR6(cos(x)**-2) #unchanged 

cos(x)**(-2) 

>>> TR6(cos(x)**4) 

(-sin(x)**2 + 1)**2 

""" 

return _TR56(rv, cos, sin, lambda x: 1 - x, max=max, pow=pow) 

 

 

def TR7(rv): 

"""Lowering the degree of cos(x)**2 

 

Examples 

======== 

 

>>> from sympy.simplify.fu import TR7 

>>> from sympy.abc import x 

>>> from sympy import cos 

>>> TR7(cos(x)**2) 

cos(2*x)/2 + 1/2 

>>> TR7(cos(x)**2 + 1) 

cos(2*x)/2 + 3/2 

 

""" 

 

def f(rv): 

if not (rv.is_Pow and rv.base.func == cos and rv.exp == 2): 

return rv 

return (1 + cos(2*rv.base.args[0]))/2 

 

return bottom_up(rv, f) 

 

 

def TR8(rv, first=True): 

"""Converting products of ``cos`` and/or ``sin`` to a sum or 

difference of ``cos`` and or ``sin`` terms. 

 

Examples 

======== 

 

>>> from sympy.simplify.fu import TR8, TR7 

>>> from sympy import cos, sin 

>>> TR8(cos(2)*cos(3)) 

cos(5)/2 + cos(1)/2 

>>> TR8(cos(2)*sin(3)) 

sin(5)/2 + sin(1)/2 

>>> TR8(sin(2)*sin(3)) 

-cos(5)/2 + cos(1)/2 

""" 

 

def f(rv): 

if not ( 

rv.is_Mul or 

rv.is_Pow and 

rv.base.func in (cos, sin) and 

(rv.exp.is_integer or rv.base.is_positive)): 

return rv 

 

if first: 

n, d = [expand_mul(i) for i in rv.as_numer_denom()] 

newn = TR8(n, first=False) 

newd = TR8(d, first=False) 

if newn != n or newd != d: 

rv = gcd_terms(newn/newd) 

if rv.is_Mul and rv.args[0].is_Rational and \ 

len(rv.args) == 2 and rv.args[1].is_Add: 

rv = Mul(*rv.as_coeff_Mul()) 

return rv 

 

args = {cos: [], sin: [], None: []} 

for a in ordered(Mul.make_args(rv)): 

if a.func in (cos, sin): 

args[a.func].append(a.args[0]) 

elif (a.is_Pow and a.exp.is_Integer and a.exp > 0 and \ 

a.base.func in (cos, sin)): 

# XXX this is ok but pathological expression could be handled 

# more efficiently as in TRmorrie 

args[a.base.func].extend([a.base.args[0]]*a.exp) 

else: 

args[None].append(a) 

c = args[cos] 

s = args[sin] 

if not (c and s or len(c) > 1 or len(s) > 1): 

return rv 

 

args = args[None] 

n = min(len(c), len(s)) 

for i in range(n): 

a1 = s.pop() 

a2 = c.pop() 

args.append((sin(a1 + a2) + sin(a1 - a2))/2) 

while len(c) > 1: 

a1 = c.pop() 

a2 = c.pop() 

args.append((cos(a1 + a2) + cos(a1 - a2))/2) 

if c: 

args.append(cos(c.pop())) 

while len(s) > 1: 

a1 = s.pop() 

a2 = s.pop() 

args.append((-cos(a1 + a2) + cos(a1 - a2))/2) 

if s: 

args.append(sin(s.pop())) 

return TR8(expand_mul(Mul(*args))) 

 

return bottom_up(rv, f) 

 

 

def TR9(rv): 

"""Sum of ``cos`` or ``sin`` terms as a product of ``cos`` or ``sin``. 

 

Examples 

======== 

 

>>> from sympy.simplify.fu import TR9 

>>> from sympy import cos, sin 

>>> TR9(cos(1) + cos(2)) 

2*cos(1/2)*cos(3/2) 

>>> TR9(cos(1) + 2*sin(1) + 2*sin(2)) 

cos(1) + 4*sin(3/2)*cos(1/2) 

 

If no change is made by TR9, no re-arrangement of the 

expression will be made. For example, though factoring 

of common term is attempted, if the factored expression 

wasn't changed, the original expression will be returned: 

 

>>> TR9(cos(3) + cos(3)*cos(2)) 

cos(3) + cos(2)*cos(3) 

 

""" 

 

def f(rv): 

if not rv.is_Add: 

return rv 

 

def do(rv, first=True): 

# cos(a)+/-cos(b) can be combined into a product of cosines and 

# sin(a)+/-sin(b) can be combined into a product of cosine and 

# sine. 

# 

# If there are more than two args, the pairs which "work" will 

# have a gcd extractable and the remaining two terms will have 

# the above structure -- all pairs must be checked to find the 

# ones that work. args that don't have a common set of symbols 

# are skipped since this doesn't lead to a simpler formula and 

# also has the arbitrariness of combining, for example, the x 

# and y term instead of the y and z term in something like 

# cos(x) + cos(y) + cos(z). 

 

if not rv.is_Add: 

return rv 

 

args = list(ordered(rv.args)) 

if len(args) != 2: 

hit = False 

for i in range(len(args)): 

ai = args[i] 

if ai is None: 

continue 

for j in range(i + 1, len(args)): 

aj = args[j] 

if aj is None: 

continue 

was = ai + aj 

new = do(was) 

if new != was: 

args[i] = new # update in place 

args[j] = None 

hit = True 

break # go to next i 

if hit: 

rv = Add(*[_f for _f in args if _f]) 

if rv.is_Add: 

rv = do(rv) 

 

return rv 

 

# two-arg Add 

split = trig_split(*args) 

if not split: 

return rv 

gcd, n1, n2, a, b, iscos = split 

 

# application of rule if possible 

if iscos: 

if n1 == n2: 

return gcd*n1*2*cos((a + b)/2)*cos((a - b)/2) 

if n1 < 0: 

a, b = b, a 

return -2*gcd*sin((a + b)/2)*sin((a - b)/2) 

else: 

if n1 == n2: 

return gcd*n1*2*sin((a + b)/2)*cos((a - b)/2) 

if n1 < 0: 

a, b = b, a 

return 2*gcd*cos((a + b)/2)*sin((a - b)/2) 

 

return process_common_addends(rv, do) # DON'T sift by free symbols 

 

return bottom_up(rv, f) 

 

 

def TR10(rv, first=True): 

"""Separate sums in ``cos`` and ``sin``. 

 

Examples 

======== 

 

>>> from sympy.simplify.fu import TR10 

>>> from sympy.abc import a, b, c 

>>> from sympy import cos, sin 

>>> TR10(cos(a + b)) 

-sin(a)*sin(b) + cos(a)*cos(b) 

>>> TR10(sin(a + b)) 

sin(a)*cos(b) + sin(b)*cos(a) 

>>> TR10(sin(a + b + c)) 

(-sin(a)*sin(b) + cos(a)*cos(b))*sin(c) + \ 

(sin(a)*cos(b) + sin(b)*cos(a))*cos(c) 

""" 

 

def f(rv): 

if not rv.func in (cos, sin): 

return rv 

 

f = rv.func 

arg = rv.args[0] 

if arg.is_Add: 

if first: 

args = list(ordered(arg.args)) 

else: 

args = list(arg.args) 

a = args.pop() 

b = Add._from_args(args) 

if b.is_Add: 

if f == sin: 

return sin(a)*TR10(cos(b), first=False) + \ 

cos(a)*TR10(sin(b), first=False) 

else: 

return cos(a)*TR10(cos(b), first=False) - \ 

sin(a)*TR10(sin(b), first=False) 

else: 

if f == sin: 

return sin(a)*cos(b) + cos(a)*sin(b) 

else: 

return cos(a)*cos(b) - sin(a)*sin(b) 

return rv 

 

return bottom_up(rv, f) 

 

 

def TR10i(rv): 

"""Sum of products to function of sum. 

 

Examples 

======== 

 

>>> from sympy.simplify.fu import TR10i 

>>> from sympy import cos, sin, pi, Add, Mul, sqrt, Symbol 

>>> from sympy.abc import x, y 

 

>>> TR10i(cos(1)*cos(3) + sin(1)*sin(3)) 

cos(2) 

>>> TR10i(cos(1)*sin(3) + sin(1)*cos(3) + cos(3)) 

cos(3) + sin(4) 

>>> TR10i(sqrt(2)*cos(x)*x + sqrt(6)*sin(x)*x) 

2*sqrt(2)*x*sin(x + pi/6) 

 

""" 

global _ROOT2, _ROOT3, _invROOT3 

if _ROOT2 is None: 

_roots() 

 

def f(rv): 

if not rv.is_Add: 

return rv 

 

def do(rv, first=True): 

# args which can be expressed as A*(cos(a)*cos(b)+/-sin(a)*sin(b)) 

# or B*(cos(a)*sin(b)+/-cos(b)*sin(a)) can be combined into 

# A*f(a+/-b) where f is either sin or cos. 

# 

# If there are more than two args, the pairs which "work" will have 

# a gcd extractable and the remaining two terms will have the above 

# structure -- all pairs must be checked to find the ones that 

# work. 

 

if not rv.is_Add: 

return rv 

 

args = list(ordered(rv.args)) 

if len(args) != 2: 

hit = False 

for i in range(len(args)): 

ai = args[i] 

if ai is None: 

continue 

for j in range(i + 1, len(args)): 

aj = args[j] 

if aj is None: 

continue 

was = ai + aj 

new = do(was) 

if new != was: 

args[i] = new # update in place 

args[j] = None 

hit = True 

break # go to next i 

if hit: 

rv = Add(*[_f for _f in args if _f]) 

if rv.is_Add: 

rv = do(rv) 

 

return rv 

 

# two-arg Add 

split = trig_split(*args, two=True) 

if not split: 

return rv 

gcd, n1, n2, a, b, same = split 

 

# identify and get c1 to be cos then apply rule if possible 

if same: # coscos, sinsin 

gcd = n1*gcd 

if n1 == n2: 

return gcd*cos(a - b) 

return gcd*cos(a + b) 

else: #cossin, cossin 

gcd = n1*gcd 

if n1 == n2: 

return gcd*sin(a + b) 

return gcd*sin(b - a) 

 

rv = process_common_addends( 

rv, do, lambda x: tuple(ordered(x.free_symbols))) 

 

# need to check for inducible pairs in ratio of sqrt(3):1 that 

# appeared in different lists when sorting by coefficient 

while rv.is_Add: 

byrad = defaultdict(list) 

for a in rv.args: 

hit = 0 

if a.is_Mul: 

for ai in a.args: 

if ai.is_Pow and ai.exp is S.Half and \ 

ai.base.is_Integer: 

byrad[ai].append(a) 

hit = 1 

break 

if not hit: 

byrad[S.One].append(a) 

 

# no need to check all pairs -- just check for the onees 

# that have the right ratio 

args = [] 

for a in byrad: 

for b in [_ROOT3*a, _invROOT3]: 

if b in byrad: 

for i in range(len(byrad[a])): 

if byrad[a][i] is None: 

continue 

for j in range(len(byrad[b])): 

if byrad[b][j] is None: 

continue 

was = Add(byrad[a][i] + byrad[b][j]) 

new = do(was) 

if new != was: 

args.append(new) 

byrad[a][i] = None 

byrad[b][j] = None 

break 

if args: 

rv = Add(*(args + [Add(*[_f for _f in v if _f]) 

for v in byrad.values()])) 

else: 

rv = do(rv) # final pass to resolve any new inducible pairs 

break 

 

return rv 

 

return bottom_up(rv, f) 

 

 

def TR11(rv, base=None): 

"""Function of double angle to product. The ``base`` argument can be used 

to indicate what is the un-doubled argument, e.g. if 3*pi/7 is the base 

then cosine and sine functions with argument 6*pi/7 will be replaced. 

 

Examples 

======== 

 

>>> from sympy.simplify.fu import TR11 

>>> from sympy import cos, sin, pi 

>>> from sympy.abc import x 

>>> TR11(sin(2*x)) 

2*sin(x)*cos(x) 

>>> TR11(cos(2*x)) 

-sin(x)**2 + cos(x)**2 

>>> TR11(sin(4*x)) 

4*(-sin(x)**2 + cos(x)**2)*sin(x)*cos(x) 

>>> TR11(sin(4*x/3)) 

4*(-sin(x/3)**2 + cos(x/3)**2)*sin(x/3)*cos(x/3) 

 

If the arguments are simply integers, no change is made 

unless a base is provided: 

 

>>> TR11(cos(2)) 

cos(2) 

>>> TR11(cos(4), 2) 

-sin(2)**2 + cos(2)**2 

 

There is a subtle issue here in that autosimplification will convert 

some higher angles to lower angles 

 

>>> cos(6*pi/7) + cos(3*pi/7) 

-cos(pi/7) + cos(3*pi/7) 

 

The 6*pi/7 angle is now pi/7 but can be targeted with TR11 by supplying 

the 3*pi/7 base: 

 

>>> TR11(_, 3*pi/7) 

-sin(3*pi/7)**2 + cos(3*pi/7)**2 + cos(3*pi/7) 

 

""" 

 

def f(rv): 

if not rv.func in (cos, sin): 

return rv 

 

if base: 

f = rv.func 

t = f(base*2) 

co = S.One 

if t.is_Mul: 

co, t = t.as_coeff_Mul() 

if not t.func in (cos, sin): 

return rv 

if rv.args[0] == t.args[0]: 

c = cos(base) 

s = sin(base) 

if f is cos: 

return (c**2 - s**2)/co 

else: 

return 2*c*s/co 

return rv 

 

elif not rv.args[0].is_Number: 

# make a change if the leading coefficient's numerator is 

# divisible by 2 

c, m = rv.args[0].as_coeff_Mul(rational=True) 

if c.p % 2 == 0: 

arg = c.p//2*m/c.q 

c = TR11(cos(arg)) 

s = TR11(sin(arg)) 

if rv.func == sin: 

rv = 2*s*c 

else: 

rv = c**2 - s**2 

return rv 

 

return bottom_up(rv, f) 

 

 

def TR12(rv, first=True): 

"""Separate sums in ``tan``. 

 

Examples 

======== 

 

>>> from sympy.simplify.fu import TR12 

>>> from sympy.abc import x, y 

>>> from sympy import tan 

>>> from sympy.simplify.fu import TR12 

>>> TR12(tan(x + y)) 

(tan(x) + tan(y))/(-tan(x)*tan(y) + 1) 

""" 

 

def f(rv): 

if not rv.func == tan: 

return rv 

 

arg = rv.args[0] 

if arg.is_Add: 

if first: 

args = list(ordered(arg.args)) 

else: 

args = list(arg.args) 

a = args.pop() 

b = Add._from_args(args) 

if b.is_Add: 

tb = TR12(tan(b), first=False) 

else: 

tb = tan(b) 

return (tan(a) + tb)/(1 - tan(a)*tb) 

return rv 

 

return bottom_up(rv, f) 

 

 

def TR12i(rv): 

"""Combine tan arguments as 

(tan(y) + tan(x))/(tan(x)*tan(y) - 1) -> -tan(x + y) 

 

Examples 

======== 

 

>>> from sympy.simplify.fu import TR12i 

>>> from sympy import tan 

>>> from sympy.abc import a, b, c 

>>> ta, tb, tc = [tan(i) for i in (a, b, c)] 

>>> TR12i((ta + tb)/(-ta*tb + 1)) 

tan(a + b) 

>>> TR12i((ta + tb)/(ta*tb - 1)) 

-tan(a + b) 

>>> TR12i((-ta - tb)/(ta*tb - 1)) 

tan(a + b) 

>>> eq = (ta + tb)/(-ta*tb + 1)**2*(-3*ta - 3*tc)/(2*(ta*tc - 1)) 

>>> TR12i(eq.expand()) 

-3*tan(a + b)*tan(a + c)/(2*(tan(a) + tan(b) - 1)) 

""" 

from sympy import factor 

 

def f(rv): 

if not (rv.is_Add or rv.is_Mul or rv.is_Pow): 

return rv 

 

n, d = rv.as_numer_denom() 

if not d.args or not n.args: 

return rv 

 

dok = {} 

 

def ok(di): 

m = as_f_sign_1(di) 

if m: 

g, f, s = m 

if s is S.NegativeOne and f.is_Mul and len(f.args) == 2 and \ 

all(fi.func is tan for fi in f.args): 

return g, f 

 

d_args = list(Mul.make_args(d)) 

for i, di in enumerate(d_args): 

m = ok(di) 

if m: 

g, t = m 

s = Add(*[_.args[0] for _ in t.args]) 

dok[s] = S.One 

d_args[i] = g 

continue 

if di.is_Add: 

di = factor(di) 

if di.is_Mul: 

d_args.extend(di.args) 

d_args[i] = S.One 

elif di.is_Pow and (di.exp.is_integer or di.base.is_positive): 

m = ok(di.base) 

if m: 

g, t = m 

s = Add(*[_.args[0] for _ in t.args]) 

dok[s] = di.exp 

d_args[i] = g**di.exp 

else: 

di = factor(di) 

if di.is_Mul: 

d_args.extend(di.args) 

d_args[i] = S.One 

if not dok: 

return rv 

 

def ok(ni): 

if ni.is_Add and len(ni.args) == 2: 

a, b = ni.args 

if a.func is tan and b.func is tan: 

return a, b 

n_args = list(Mul.make_args(factor_terms(n))) 

hit = False 

for i, ni in enumerate(n_args): 

m = ok(ni) 

if not m: 

m = ok(-ni) 

if m: 

n_args[i] = S.NegativeOne 

else: 

if ni.is_Add: 

ni = factor(ni) 

if ni.is_Mul: 

n_args.extend(ni.args) 

n_args[i] = S.One 

continue 

elif ni.is_Pow and ( 

ni.exp.is_integer or ni.base.is_positive): 

m = ok(ni.base) 

if m: 

n_args[i] = S.One 

else: 

ni = factor(ni) 

if ni.is_Mul: 

n_args.extend(ni.args) 

n_args[i] = S.One 

continue 

else: 

continue 

else: 

n_args[i] = S.One 

hit = True 

s = Add(*[_.args[0] for _ in m]) 

ed = dok[s] 

newed = ed.extract_additively(S.One) 

if newed is not None: 

if newed: 

dok[s] = newed 

else: 

dok.pop(s) 

n_args[i] *= -tan(s) 

 

if hit: 

rv = Mul(*n_args)/Mul(*d_args)/Mul(*[(Add(*[ 

tan(a) for a in i.args]) - 1)**e for i, e in dok.items()]) 

 

return rv 

 

return bottom_up(rv, f) 

 

 

def TR13(rv): 

"""Change products of ``tan`` or ``cot``. 

 

Examples 

======== 

 

>>> from sympy.simplify.fu import TR13 

>>> from sympy import tan, cot, cos 

>>> TR13(tan(3)*tan(2)) 

-tan(2)/tan(5) - tan(3)/tan(5) + 1 

>>> TR13(cot(3)*cot(2)) 

cot(2)*cot(5) + 1 + cot(3)*cot(5) 

""" 

 

def f(rv): 

if not rv.is_Mul: 

return rv 

 

# XXX handle products of powers? or let power-reducing handle it? 

args = {tan: [], cot: [], None: []} 

for a in ordered(Mul.make_args(rv)): 

if a.func in (tan, cot): 

args[a.func].append(a.args[0]) 

else: 

args[None].append(a) 

t = args[tan] 

c = args[cot] 

if len(t) < 2 and len(c) < 2: 

return rv 

args = args[None] 

while len(t) > 1: 

t1 = t.pop() 

t2 = t.pop() 

args.append(1 - (tan(t1)/tan(t1 + t2) + tan(t2)/tan(t1 + t2))) 

if t: 

args.append(tan(t.pop())) 

while len(c) > 1: 

t1 = c.pop() 

t2 = c.pop() 

args.append(1 + cot(t1)*cot(t1 + t2) + cot(t2)*cot(t1 + t2)) 

if c: 

args.append(cot(c.pop())) 

return Mul(*args) 

 

return bottom_up(rv, f) 

 

 

def TRmorrie(rv): 

"""Returns cos(x)*cos(2*x)*...*cos(2**(k-1)*x) -> sin(2**k*x)/(2**k*sin(x)) 

 

Examples 

======== 

 

>>> from sympy.simplify.fu import TRmorrie, TR8, TR3 

>>> from sympy.abc import x 

>>> from sympy import Mul, cos, pi 

>>> TRmorrie(cos(x)*cos(2*x)) 

sin(4*x)/(4*sin(x)) 

>>> TRmorrie(7*Mul(*[cos(x) for x in range(10)])) 

7*sin(12)*sin(16)*cos(5)*cos(7)*cos(9)/(64*sin(1)*sin(3)) 

 

Sometimes autosimplification will cause a power to be 

not recognized. e.g. in the following, cos(4*pi/7) automatically 

simplifies to -cos(3*pi/7) so only 2 of the 3 terms are 

recognized: 

 

>>> TRmorrie(cos(pi/7)*cos(2*pi/7)*cos(4*pi/7)) 

-sin(3*pi/7)*cos(3*pi/7)/(4*sin(pi/7)) 

 

A touch by TR8 resolves the expression to a Rational 

 

>>> TR8(_) 

-1/8 

 

In this case, if eq is unsimplified, the answer is obtained 

directly: 

 

>>> eq = cos(pi/9)*cos(2*pi/9)*cos(3*pi/9)*cos(4*pi/9) 

>>> TRmorrie(eq) 

1/16 

 

But if angles are made canonical with TR3 then the answer 

is not simplified without further work: 

 

>>> TR3(eq) 

sin(pi/18)*cos(pi/9)*cos(2*pi/9)/2 

>>> TRmorrie(_) 

sin(pi/18)*sin(4*pi/9)/(8*sin(pi/9)) 

>>> TR8(_) 

cos(7*pi/18)/(16*sin(pi/9)) 

>>> TR3(_) 

1/16 

 

The original expression would have resolve to 1/16 directly with TR8, 

however: 

 

>>> TR8(eq) 

1/16 

 

References 

========== 

 

http://en.wikipedia.org/wiki/Morrie%27s_law 

 

""" 

 

def f(rv): 

if not rv.is_Mul: 

return rv 

 

args = defaultdict(list) 

coss = {} 

other = [] 

for c in rv.args: 

b, e = c.as_base_exp() 

if e.is_Integer and b.func is cos: 

co, a = b.args[0].as_coeff_Mul() 

args[a].append(co) 

coss[b] = e 

else: 

other.append(c) 

 

new = [] 

for a in args: 

c = args[a] 

c.sort() 

no = [] 

while c: 

k = 0 

cc = ci = c[0] 

while cc in c: 

k += 1 

cc *= 2 

if k > 1: 

newarg = sin(2**k*ci*a)/2**k/sin(ci*a) 

# see how many times this can be taken 

take = None 

ccs = [] 

for i in range(k): 

cc /= 2 

key = cos(a*cc, evaluate=False) 

ccs.append(cc) 

take = min(coss[key], take or coss[key]) 

# update exponent counts 

for i in range(k): 

cc = ccs.pop() 

key = cos(a*cc, evaluate=False) 

coss[key] -= take 

if not coss[key]: 

c.remove(cc) 

new.append(newarg**take) 

else: 

no.append(c.pop(0)) 

c[:] = no 

 

if new: 

rv = Mul(*(new + other + [ 

cos(k*a, evaluate=False) for a in args for k in args[a]])) 

 

return rv 

 

return bottom_up(rv, f) 

 

 

def TR14(rv, first=True): 

"""Convert factored powers of sin and cos identities into simpler 

expressions. 

 

Examples 

======== 

 

>>> from sympy.simplify.fu import TR14 

>>> from sympy.abc import x, y 

>>> from sympy import cos, sin 

>>> TR14((cos(x) - 1)*(cos(x) + 1)) 

-sin(x)**2 

>>> TR14((sin(x) - 1)*(sin(x) + 1)) 

-cos(x)**2 

>>> p1 = (cos(x) + 1)*(cos(x) - 1) 

>>> p2 = (cos(y) - 1)*2*(cos(y) + 1) 

>>> p3 = (3*(cos(y) - 1))*(3*(cos(y) + 1)) 

>>> TR14(p1*p2*p3*(x - 1)) 

-18*(x - 1)*sin(x)**2*sin(y)**4 

 

""" 

 

def f(rv): 

if not rv.is_Mul: 

return rv 

 

if first: 

# sort them by location in numerator and denominator 

# so the code below can just deal with positive exponents 

n, d = rv.as_numer_denom() 

if d is not S.One: 

newn = TR14(n, first=False) 

newd = TR14(d, first=False) 

if newn != n or newd != d: 

rv = newn/newd 

return rv 

 

other = [] 

process = [] 

for a in rv.args: 

if a.is_Pow: 

b, e = a.as_base_exp() 

if not (e.is_integer or b.is_positive): 

other.append(a) 

continue 

a = b 

else: 

e = S.One 

m = as_f_sign_1(a) 

if not m or m[1].func not in (cos, sin): 

if e is S.One: 

other.append(a) 

else: 

other.append(a**e) 

continue 

g, f, si = m 

process.append((g, e.is_Number, e, f, si, a)) 

 

# sort them to get like terms next to each other 

process = list(ordered(process)) 

 

# keep track of whether there was any change 

nother = len(other) 

 

# access keys 

keys = (g, t, e, f, si, a) = list(range(6)) 

 

while process: 

A = process.pop(0) 

if process: 

B = process[0] 

 

if A[e].is_Number and B[e].is_Number: 

# both exponents are numbers 

if A[f] == B[f]: 

if A[si] != B[si]: 

B = process.pop(0) 

take = min(A[e], B[e]) 

 

# reinsert any remainder 

# the B will likely sort after A so check it first 

if B[e] != take: 

rem = [B[i] for i in keys] 

rem[e] -= take 

process.insert(0, rem) 

elif A[e] != take: 

rem = [A[i] for i in keys] 

rem[e] -= take 

process.insert(0, rem) 

 

if A[f].func is cos: 

t = sin 

else: 

t = cos 

other.append((-A[g]*B[g]*t(A[f].args[0])**2)**take) 

continue 

 

elif A[e] == B[e]: 

# both exponents are equal symbols 

if A[f] == B[f]: 

if A[si] != B[si]: 

B = process.pop(0) 

take = A[e] 

if A[f].func is cos: 

t = sin 

else: 

t = cos 

other.append((-A[g]*B[g]*t(A[f].args[0])**2)**take) 

continue 

 

# either we are done or neither condition above applied 

other.append(A[a]**A[e]) 

 

if len(other) != nother: 

rv = Mul(*other) 

 

return rv 

 

return bottom_up(rv, f) 

 

 

def TR15(rv, max=4, pow=False): 

"""Convert sin(x)*-2 to 1 + cot(x)**2. 

 

See _TR56 docstring for advanced use of ``max`` and ``pow``. 

 

Examples 

======== 

 

>>> from sympy.simplify.fu import TR15 

>>> from sympy.abc import x 

>>> from sympy import cos, sin 

>>> TR15(1 - 1/sin(x)**2) 

-cot(x)**2 

 

""" 

 

def f(rv): 

if not (isinstance(rv, Pow) and rv.base.func is sin): 

return rv 

 

ia = 1/rv 

a = _TR56(ia, sin, cot, lambda x: 1 + x, max=max, pow=pow) 

if a != ia: 

rv = a 

return rv 

 

return bottom_up(rv, f) 

 

 

def TR16(rv, max=4, pow=False): 

"""Convert cos(x)*-2 to 1 + tan(x)**2. 

 

See _TR56 docstring for advanced use of ``max`` and ``pow``. 

 

Examples 

======== 

 

>>> from sympy.simplify.fu import TR16 

>>> from sympy.abc import x 

>>> from sympy import cos, sin 

>>> TR16(1 - 1/cos(x)**2) 

-tan(x)**2 

 

""" 

 

def f(rv): 

if not (isinstance(rv, Pow) and rv.base.func is cos): 

return rv 

 

ia = 1/rv 

a = _TR56(ia, cos, tan, lambda x: 1 + x, max=max, pow=pow) 

if a != ia: 

rv = a 

return rv 

 

return bottom_up(rv, f) 

 

 

def TR111(rv): 

"""Convert f(x)**-i to g(x)**i where either ``i`` is an integer 

or the base is positive and f, g are: tan, cot; sin, csc; or cos, sec. 

 

Examples 

======== 

 

>>> from sympy.simplify.fu import TR111 

>>> from sympy.abc import x 

>>> from sympy import tan 

>>> TR111(1 - 1/tan(x)**2) 

-cot(x)**2 + 1 

 

""" 

 

def f(rv): 

if not ( 

isinstance(rv, Pow) and 

(rv.base.is_positive or rv.exp.is_integer and rv.exp.is_negative)): 

return rv 

 

if rv.base.func is tan: 

return cot(rv.base.args[0])**-rv.exp 

elif rv.base.func is sin: 

return csc(rv.base.args[0])**-rv.exp 

elif rv.base.func is cos: 

return sec(rv.base.args[0])**-rv.exp 

return rv 

 

return bottom_up(rv, f) 

 

 

def TR22(rv, max=4, pow=False): 

"""Convert tan(x)**2 to sec(x)**2 - 1 and cot(x)**2 to csc(x)**2 - 1. 

 

See _TR56 docstring for advanced use of ``max`` and ``pow``. 

 

Examples 

======== 

 

>>> from sympy.simplify.fu import TR22 

>>> from sympy.abc import x 

>>> from sympy import tan, cot 

>>> TR22(1 + tan(x)**2) 

sec(x)**2 

>>> TR22(1 + cot(x)**2) 

csc(x)**2 

 

""" 

 

def f(rv): 

if not (isinstance(rv, Pow) and rv.base.func in (cot, tan)): 

return rv 

 

rv = _TR56(rv, tan, sec, lambda x: x - 1, max=max, pow=pow) 

rv = _TR56(rv, cot, csc, lambda x: x - 1, max=max, pow=pow) 

return rv 

 

return bottom_up(rv, f) 

 

 

def L(rv): 

"""Return count of trigonometric functions in expression. 

 

Examples 

======== 

 

>>> from sympy.simplify.fu import L 

>>> from sympy.abc import x 

>>> from sympy import cos, sin 

>>> L(cos(x)+sin(x)) 

2 

""" 

return S(rv.count(TrigonometricFunction)) 

 

 

# ============== end of basic Fu-like tools ===================== 

 

if SYMPY_DEBUG: 

(TR0, TR1, TR2, TR3, TR4, TR5, TR6, TR7, TR8, TR9, TR10, TR11, TR12, TR13, 

TR2i, TRmorrie, TR14, TR15, TR16, TR12i, TR111, TR22 

)= list(map(debug, 

(TR0, TR1, TR2, TR3, TR4, TR5, TR6, TR7, TR8, TR9, TR10, TR11, TR12, TR13, 

TR2i, TRmorrie, TR14, TR15, TR16, TR12i, TR111, TR22))) 

 

 

# tuples are chains -- (f, g) -> lambda x: g(f(x)) 

# lists are choices -- [f, g] -> lambda x: min(f(x), g(x), key=objective) 

 

CTR1 = [(TR5, TR0), (TR6, TR0), identity] 

 

CTR2 = (TR11, [(TR5, TR0), (TR6, TR0), TR0]) 

 

CTR3 = [(TRmorrie, TR8, TR0), (TRmorrie, TR8, TR10i, TR0), identity] 

 

CTR4 = [(TR4, TR10i), identity] 

 

RL1 = (TR4, TR3, TR4, TR12, TR4, TR13, TR4, TR0) 

 

 

# XXX it's a little unclear how this one is to be implemented 

# see Fu paper of reference, page 7. What is the Union symbol refering to? 

# The diagram shows all these as one chain of transformations, but the 

# text refers to them being applied independently. Also, a break 

# if L starts to increase has not been implemented. 

RL2 = [ 

(TR4, TR3, TR10, TR4, TR3, TR11), 

(TR5, TR7, TR11, TR4), 

(CTR3, CTR1, TR9, CTR2, TR4, TR9, TR9, CTR4), 

identity, 

] 

 

 

def fu(rv, measure=lambda x: (L(x), x.count_ops())): 

"""Attempt to simplify expression by using transformation rules given 

in the algorithm by Fu et al. 

 

:func:`fu` will try to minimize the objective function ``measure``. 

By default this first minimizes the number of trig terms and then minimizes 

the number of total operations. 

 

Examples 

======== 

 

>>> from sympy.simplify.fu import fu 

>>> from sympy import cos, sin, tan, pi, S, sqrt 

>>> from sympy.abc import x, y, a, b 

 

>>> fu(sin(50)**2 + cos(50)**2 + sin(pi/6)) 

3/2 

>>> fu(sqrt(6)*cos(x) + sqrt(2)*sin(x)) 

2*sqrt(2)*sin(x + pi/3) 

 

CTR1 example 

 

>>> eq = sin(x)**4 - cos(y)**2 + sin(y)**2 + 2*cos(x)**2 

>>> fu(eq) 

cos(x)**4 - 2*cos(y)**2 + 2 

 

CTR2 example 

 

>>> fu(S.Half - cos(2*x)/2) 

sin(x)**2 

 

CTR3 example 

 

>>> fu(sin(a)*(cos(b) - sin(b)) + cos(a)*(sin(b) + cos(b))) 

sqrt(2)*sin(a + b + pi/4) 

 

CTR4 example 

 

>>> fu(sqrt(3)*cos(x)/2 + sin(x)/2) 

sin(x + pi/3) 

 

Example 1 

 

>>> fu(1-sin(2*x)**2/4-sin(y)**2-cos(x)**4) 

-cos(x)**2 + cos(y)**2 

 

Example 2 

 

>>> fu(cos(4*pi/9)) 

sin(pi/18) 

>>> fu(cos(pi/9)*cos(2*pi/9)*cos(3*pi/9)*cos(4*pi/9)) 

1/16 

 

Example 3 

 

>>> fu(tan(7*pi/18)+tan(5*pi/18)-sqrt(3)*tan(5*pi/18)*tan(7*pi/18)) 

-sqrt(3) 

 

Objective function example 

 

>>> fu(sin(x)/cos(x)) # default objective function 

tan(x) 

>>> fu(sin(x)/cos(x), measure=lambda x: -x.count_ops()) # maximize op count 

sin(x)/cos(x) 

 

References 

========== 

http://rfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/ 

DESTIME2006/DES_contribs/Fu/simplification.pdf 

""" 

fRL1 = greedy(RL1, measure) 

fRL2 = greedy(RL2, measure) 

 

was = rv 

rv = sympify(rv) 

if not isinstance(rv, Expr): 

return rv.func(*[fu(a, measure=measure) for a in rv.args]) 

rv = TR1(rv) 

if rv.has(tan, cot): 

rv1 = fRL1(rv) 

if (measure(rv1) < measure(rv)): 

rv = rv1 

if rv.has(tan, cot): 

rv = TR2(rv) 

if rv.has(sin, cos): 

rv1 = fRL2(rv) 

rv2 = TR8(TRmorrie(rv1)) 

rv = min([was, rv, rv1, rv2], key=measure) 

return min(TR2i(rv), rv, key=measure) 

 

 

def process_common_addends(rv, do, key2=None, key1=True): 

"""Apply ``do`` to addends of ``rv`` that (if key1=True) share at least 

a common absolute value of their coefficient and the value of ``key2`` when 

applied to the argument. If ``key1`` is False ``key2`` must be supplied and 

will be the only key applied. 

""" 

 

# collect by absolute value of coefficient and key2 

absc = defaultdict(list) 

if key1: 

for a in rv.args: 

c, a = a.as_coeff_Mul() 

if c < 0: 

c = -c 

a = -a # put the sign on `a` 

absc[(c, key2(a) if key2 else 1)].append(a) 

elif key2: 

for a in rv.args: 

absc[(S.One, key2(a))].append(a) 

else: 

raise ValueError('must have at least one key') 

 

args = [] 

hit = False 

for k in absc: 

v = absc[k] 

c, _ = k 

if len(v) > 1: 

e = Add(*v, evaluate=False) 

new = do(e) 

if new != e: 

e = new 

hit = True 

args.append(c*e) 

else: 

args.append(c*v[0]) 

if hit: 

rv = Add(*args) 

 

return rv 

 

 

fufuncs = ''' 

TR0 TR1 TR2 TR3 TR4 TR5 TR6 TR7 TR8 TR9 TR10 TR10i TR11 

TR12 TR13 L TR2i TRmorrie TR12i 

TR14 TR15 TR16 TR111 TR22'''.split() 

FU = dict(list(zip(fufuncs, list(map(locals().get, fufuncs))))) 

 

 

def _roots(): 

global _ROOT2, _ROOT3, _invROOT3 

_ROOT2, _ROOT3 = sqrt(2), sqrt(3) 

_invROOT3 = 1/_ROOT3 

_ROOT2 = None 

 

 

def trig_split(a, b, two=False): 

"""Return the gcd, s1, s2, a1, a2, bool where 

 

If two is False (default) then:: 

a + b = gcd*(s1*f(a1) + s2*f(a2)) where f = cos if bool else sin 

else: 

if bool, a + b was +/- cos(a1)*cos(a2) +/- sin(a1)*sin(a2) and equals 

n1*gcd*cos(a - b) if n1 == n2 else 

n1*gcd*cos(a + b) 

else a + b was +/- cos(a1)*sin(a2) +/- sin(a1)*cos(a2) and equals 

n1*gcd*sin(a + b) if n1 = n2 else 

n1*gcd*sin(b - a) 

 

Examples 

======== 

 

>>> from sympy.simplify.fu import trig_split 

>>> from sympy.abc import x, y, z 

>>> from sympy import cos, sin, sqrt 

 

>>> trig_split(cos(x), cos(y)) 

(1, 1, 1, x, y, True) 

>>> trig_split(2*cos(x), -2*cos(y)) 

(2, 1, -1, x, y, True) 

>>> trig_split(cos(x)*sin(y), cos(y)*sin(y)) 

(sin(y), 1, 1, x, y, True) 

 

>>> trig_split(cos(x), -sqrt(3)*sin(x), two=True) 

(2, 1, -1, x, pi/6, False) 

>>> trig_split(cos(x), sin(x), two=True) 

(sqrt(2), 1, 1, x, pi/4, False) 

>>> trig_split(cos(x), -sin(x), two=True) 

(sqrt(2), 1, -1, x, pi/4, False) 

>>> trig_split(sqrt(2)*cos(x), -sqrt(6)*sin(x), two=True) 

(2*sqrt(2), 1, -1, x, pi/6, False) 

>>> trig_split(-sqrt(6)*cos(x), -sqrt(2)*sin(x), two=True) 

(-2*sqrt(2), 1, 1, x, pi/3, False) 

>>> trig_split(cos(x)/sqrt(6), sin(x)/sqrt(2), two=True) 

(sqrt(6)/3, 1, 1, x, pi/6, False) 

>>> trig_split(-sqrt(6)*cos(x)*sin(y), -sqrt(2)*sin(x)*sin(y), two=True) 

(-2*sqrt(2)*sin(y), 1, 1, x, pi/3, False) 

 

>>> trig_split(cos(x), sin(x)) 

>>> trig_split(cos(x), sin(z)) 

>>> trig_split(2*cos(x), -sin(x)) 

>>> trig_split(cos(x), -sqrt(3)*sin(x)) 

>>> trig_split(cos(x)*cos(y), sin(x)*sin(z)) 

>>> trig_split(cos(x)*cos(y), sin(x)*sin(y)) 

>>> trig_split(-sqrt(6)*cos(x), sqrt(2)*sin(x)*sin(y), two=True) 

""" 

global _ROOT2, _ROOT3, _invROOT3 

if _ROOT2 is None: 

_roots() 

 

a, b = [Factors(i) for i in (a, b)] 

ua, ub = a.normal(b) 

gcd = a.gcd(b).as_expr() 

n1 = n2 = 1 

if S.NegativeOne in ua.factors: 

ua = ua.quo(S.NegativeOne) 

n1 = -n1 

elif S.NegativeOne in ub.factors: 

ub = ub.quo(S.NegativeOne) 

n2 = -n2 

a, b = [i.as_expr() for i in (ua, ub)] 

 

def pow_cos_sin(a, two): 

"""Return ``a`` as a tuple (r, c, s) such that 

``a = (r or 1)*(c or 1)*(s or 1)``. 

 

Three arguments are returned (radical, c-factor, s-factor) as 

long as the conditions set by ``two`` are met; otherwise None is 

returned. If ``two`` is True there will be one or two non-None 

values in the tuple: c and s or c and r or s and r or s or c with c 

being a cosine function (if possible) else a sine, and s being a sine 

function (if possible) else oosine. If ``two`` is False then there 

will only be a c or s term in the tuple. 

 

``two`` also require that either two cos and/or sin be present (with 

the condition that if the functions are the same the arguments are 

different or vice versa) or that a single cosine or a single sine 

be present with an optional radical. 

 

If the above conditions dictated by ``two`` are not met then None 

is returned. 

""" 

c = s = None 

co = S.One 

if a.is_Mul: 

co, a = a.as_coeff_Mul() 

if len(a.args) > 2 or not two: 

return None 

if a.is_Mul: 

args = list(a.args) 

else: 

args = [a] 

a = args.pop(0) 

if a.func is cos: 

c = a 

elif a.func is sin: 

s = a 

elif a.is_Pow and a.exp is S.Half: # autoeval doesn't allow -1/2 

co *= a 

else: 

return None 

if args: 

b = args[0] 

if b.func is cos: 

if c: 

s = b 

else: 

c = b 

elif b.func is sin: 

if s: 

c = b 

else: 

s = b 

elif b.is_Pow and b.exp is S.Half: 

co *= b 

else: 

return None 

return co if co is not S.One else None, c, s 

elif a.func is cos: 

c = a 

elif a.func is sin: 

s = a 

if c is None and s is None: 

return 

co = co if co is not S.One else None 

return co, c, s 

 

# get the parts 

m = pow_cos_sin(a, two) 

if m is None: 

return 

coa, ca, sa = m 

m = pow_cos_sin(b, two) 

if m is None: 

return 

cob, cb, sb = m 

 

# check them 

if (not ca) and cb or ca and ca.func is sin: 

coa, ca, sa, cob, cb, sb = cob, cb, sb, coa, ca, sa 

n1, n2 = n2, n1 

if not two: # need cos(x) and cos(y) or sin(x) and sin(y) 

c = ca or sa 

s = cb or sb 

if c.func is not s.func: 

return None 

return gcd, n1, n2, c.args[0], s.args[0], c.func is cos 

else: 

if not coa and not cob: 

if (ca and cb and sa and sb): 

if not ((ca.func is sa.func) is (cb.func is sb.func)): 

return 

args = {j.args for j in (ca, sa)} 

if not all(i.args in args for i in (cb, sb)): 

return 

return gcd, n1, n2, ca.args[0], sa.args[0], ca.func is sa.func 

if ca and sa or cb and sb or \ 

two and (ca is None and sa is None or cb is None and sb is None): 

return 

c = ca or sa 

s = cb or sb 

if c.args != s.args: 

return 

if not coa: 

coa = S.One 

if not cob: 

cob = S.One 

if coa is cob: 

gcd *= _ROOT2 

return gcd, n1, n2, c.args[0], pi/4, False 

elif coa/cob == _ROOT3: 

gcd *= 2*cob 

return gcd, n1, n2, c.args[0], pi/3, False 

elif coa/cob == _invROOT3: 

gcd *= 2*coa 

return gcd, n1, n2, c.args[0], pi/6, False 

 

 

def as_f_sign_1(e): 

"""If ``e`` is a sum that can be written as ``g*(a + s)`` where 

``s`` is ``+/-1``, return ``g``, ``a``, and ``s`` where ``a`` does 

not have a leading negative coefficient. 

 

Examples 

======== 

 

>>> from sympy.simplify.fu import as_f_sign_1 

>>> from sympy.abc import x 

>>> as_f_sign_1(x + 1) 

(1, x, 1) 

>>> as_f_sign_1(x - 1) 

(1, x, -1) 

>>> as_f_sign_1(-x + 1) 

(-1, x, -1) 

>>> as_f_sign_1(-x - 1) 

(-1, x, 1) 

>>> as_f_sign_1(2*x + 2) 

(2, x, 1) 

""" 

if not e.is_Add or len(e.args) != 2: 

return 

# exact match 

a, b = e.args 

if a in (S.NegativeOne, S.One): 

g = S.One 

if b.is_Mul and b.args[0].is_Number and b.args[0] < 0: 

a, b = -a, -b 

g = -g 

return g, b, a 

# gcd match 

a, b = [Factors(i) for i in e.args] 

ua, ub = a.normal(b) 

gcd = a.gcd(b).as_expr() 

if S.NegativeOne in ua.factors: 

ua = ua.quo(S.NegativeOne) 

n1 = -1 

n2 = 1 

elif S.NegativeOne in ub.factors: 

ub = ub.quo(S.NegativeOne) 

n1 = 1 

n2 = -1 

else: 

n1 = n2 = 1 

a, b = [i.as_expr() for i in (ua, ub)] 

if a is S.One: 

a, b = b, a 

n1, n2 = n2, n1 

if n1 == -1: 

gcd = -gcd 

n2 = -n2 

 

if b is S.One: 

return gcd, a, n2 

 

 

def _osborne(e, d): 

"""Replace all hyperbolic functions with trig functions using 

the Osborne rule. 

 

Notes 

===== 

 

``d`` is a dummy variable to prevent automatic evaluation 

of trigonometric/hyperbolic functions. 

 

 

References 

========== 

 

http://en.wikipedia.org/wiki/Hyperbolic_function 

""" 

 

def f(rv): 

if not isinstance(rv, HyperbolicFunction): 

return rv 

a = rv.args[0] 

a = a*d if not a.is_Add else Add._from_args([i*d for i in a.args]) 

if rv.func is sinh: 

return I*sin(a) 

elif rv.func is cosh: 

return cos(a) 

elif rv.func is tanh: 

return I*tan(a) 

elif rv.func is coth: 

return cot(a)/I 

else: 

raise NotImplementedError('unhandled %s' % rv.func) 

 

return bottom_up(e, f) 

 

 

def _osbornei(e, d): 

"""Replace all trig functions with hyperbolic functions using 

the Osborne rule. 

 

Notes 

===== 

 

``d`` is a dummy variable to prevent automatic evaluation 

of trigonometric/hyperbolic functions. 

 

References 

========== 

 

http://en.wikipedia.org/wiki/Hyperbolic_function 

""" 

 

def f(rv): 

if not isinstance(rv, TrigonometricFunction): 

return rv 

a = rv.args[0].xreplace({d: S.One}) 

if rv.func is sin: 

return sinh(a)/I 

elif rv.func is cos: 

return cosh(a) 

elif rv.func is tan: 

return tanh(a)/I 

elif rv.func is cot: 

return coth(a)*I 

elif rv.func is sec: 

return 1/cosh(a) 

elif rv.func is csc: 

return I/sinh(a) 

else: 

raise NotImplementedError('unhandled %s' % rv.func) 

 

return bottom_up(e, f) 

 

 

def hyper_as_trig(rv): 

"""Return an expression containing hyperbolic functions in terms 

of trigonometric functions. Any trigonometric functions initially 

present are replaced with Dummy symbols and the function to undo 

the masking and the conversion back to hyperbolics is also returned. It 

should always be true that:: 

 

t, f = hyper_as_trig(expr) 

expr == f(t) 

 

Examples 

======== 

 

>>> from sympy.simplify.fu import hyper_as_trig, fu 

>>> from sympy.abc import x 

>>> from sympy import cosh, sinh 

>>> eq = sinh(x)**2 + cosh(x)**2 

>>> t, f = hyper_as_trig(eq) 

>>> f(fu(t)) 

cosh(2*x) 

 

References 

========== 

 

http://en.wikipedia.org/wiki/Hyperbolic_function 

""" 

from sympy.simplify.simplify import signsimp 

from sympy.simplify.radsimp import collect 

 

# mask off trig functions 

trigs = rv.atoms(TrigonometricFunction) 

reps = [(t, Dummy()) for t in trigs] 

masked = rv.xreplace(dict(reps)) 

 

# get inversion substitutions in place 

reps = [(v, k) for k, v in reps] 

 

d = Dummy() 

 

return _osborne(masked, d), lambda x: collect(signsimp( 

_osbornei(x, d).xreplace(dict(reps))), S.ImaginaryUnit)