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

2137

2138

2139

2140

2141

2142

2143

2144

2145

2146

2147

2148

2149

2150

2151

2152

2153

2154

2155

2156

2157

2158

2159

2160

2161

2162

2163

2164

2165

2166

2167

2168

2169

2170

2171

2172

2173

2174

2175

2176

2177

2178

2179

2180

2181

2182

2183

2184

2185

2186

2187

2188

2189

2190

2191

2192

2193

2194

2195

2196

2197

2198

2199

2200

2201

2202

2203

2204

2205

2206

2207

2208

2209

2210

2211

2212

2213

2214

2215

2216

2217

2218

2219

2220

2221

2222

2223

2224

2225

2226

2227

2228

2229

2230

2231

2232

2233

2234

2235

2236

2237

2238

2239

2240

2241

2242

2243

2244

2245

2246

2247

2248

2249

2250

2251

2252

2253

2254

2255

2256

2257

2258

2259

2260

2261

2262

2263

2264

2265

2266

2267

2268

2269

2270

2271

2272

2273

2274

2275

2276

2277

2278

2279

2280

2281

2282

2283

2284

2285

2286

2287

2288

2289

2290

2291

2292

2293

2294

2295

2296

2297

2298

2299

2300

2301

2302

2303

2304

2305

2306

2307

2308

2309

2310

2311

2312

2313

2314

2315

2316

2317

2318

2319

2320

2321

2322

2323

2324

2325

2326

2327

2328

2329

2330

2331

2332

2333

2334

2335

2336

2337

2338

2339

2340

2341

2342

2343

2344

2345

2346

2347

2348

2349

2350

2351

2352

2353

2354

2355

2356

2357

2358

2359

2360

2361

2362

2363

2364

2365

2366

2367

2368

2369

2370

2371

2372

2373

2374

2375

2376

2377

2378

2379

2380

2381

2382

2383

2384

2385

2386

2387

2388

2389

2390

2391

2392

2393

2394

2395

2396

2397

2398

2399

2400

2401

2402

2403

2404

2405

2406

2407

2408

2409

2410

2411

2412

2413

2414

2415

2416

2417

2418

2419

2420

2421

2422

2423

2424

2425

2426

2427

2428

2429

2430

2431

2432

2433

2434

2435

2436

2437

2438

2439

2440

2441

2442

2443

2444

2445

2446

2447

2448

2449

2450

2451

2452

2453

2454

2455

2456

2457

2458

2459

2460

2461

2462

2463

2464

2465

2466

2467

2468

2469

2470

2471

2472

2473

2474

2475

2476

2477

2478

2479

2480

2481

2482

2483

2484

2485

2486

2487

2488

2489

2490

2491

2492

2493

2494

2495

2496

2497

2498

2499

2500

2501

2502

2503

2504

2505

2506

2507

2508

2509

2510

2511

2512

2513

2514

2515

2516

2517

2518

2519

2520

2521

2522

2523

2524

2525

2526

2527

2528

2529

2530

2531

2532

2533

2534

2535

2536

2537

2538

2539

2540

2541

2542

2543

2544

2545

2546

2547

2548

2549

2550

2551

2552

2553

2554

2555

2556

2557

2558

2559

2560

2561

2562

2563

2564

2565

2566

2567

2568

2569

2570

2571

2572

2573

2574

2575

2576

2577

2578

2579

2580

2581

2582

2583

2584

2585

2586

2587

2588

2589

2590

2591

2592

2593

2594

2595

2596

2597

2598

2599

2600

2601

2602

2603

2604

2605

2606

2607

2608

2609

2610

2611

2612

2613

2614

2615

2616

2617

2618

2619

2620

2621

2622

2623

2624

2625

2626

2627

2628

2629

2630

2631

2632

2633

2634

2635

2636

2637

2638

2639

2640

2641

2642

2643

2644

2645

2646

2647

2648

2649

2650

2651

2652

2653

2654

2655

2656

2657

2658

2659

2660

2661

2662

2663

2664

2665

2666

2667

2668

2669

2670

2671

2672

2673

2674

2675

2676

2677

2678

2679

2680

2681

2682

2683

2684

2685

2686

2687

2688

2689

2690

2691

2692

2693

2694

2695

2696

2697

2698

2699

2700

2701

2702

2703

2704

2705

2706

2707

2708

2709

2710

2711

2712

2713

2714

2715

2716

2717

2718

2719

2720

2721

2722

2723

2724

2725

2726

2727

2728

2729

2730

2731

2732

2733

2734

2735

2736

2737

2738

2739

2740

2741

2742

2743

2744

2745

2746

2747

2748

2749

2750

2751

2752

2753

2754

2755

2756

2757

2758

2759

2760

2761

2762

2763

2764

2765

2766

2767

2768

2769

2770

2771

2772

2773

2774

2775

2776

2777

2778

2779

2780

2781

2782

2783

2784

2785

2786

2787

2788

2789

2790

2791

2792

2793

2794

2795

2796

2797

2798

2799

2800

2801

2802

2803

2804

2805

2806

2807

2808

2809

2810

2811

2812

2813

2814

2815

2816

2817

2818

2819

2820

2821

2822

2823

2824

2825

2826

2827

2828

2829

2830

2831

2832

2833

2834

2835

2836

2837

2838

2839

2840

2841

2842

2843

2844

2845

2846

2847

2848

2849

2850

2851

2852

2853

2854

2855

2856

2857

2858

2859

2860

2861

2862

2863

2864

2865

2866

2867

2868

2869

2870

2871

2872

2873

2874

2875

2876

2877

2878

2879

2880

2881

2882

2883

2884

2885

2886

2887

2888

2889

2890

2891

2892

2893

2894

2895

2896

2897

2898

2899

2900

2901

2902

2903

2904

2905

2906

2907

2908

2909

2910

2911

2912

2913

2914

2915

2916

2917

2918

2919

2920

2921

2922

2923

2924

2925

2926

2927

2928

2929

2930

2931

2932

2933

2934

2935

2936

2937

2938

2939

2940

2941

2942

2943

2944

2945

2946

2947

2948

2949

2950

2951

2952

2953

2954

2955

2956

2957

2958

2959

2960

2961

2962

2963

2964

2965

2966

2967

2968

2969

2970

2971

2972

2973

2974

2975

2976

2977

2978

2979

2980

2981

2982

2983

2984

2985

2986

2987

2988

2989

2990

2991

2992

2993

2994

2995

2996

2997

2998

2999

3000

3001

3002

3003

3004

3005

3006

3007

3008

3009

3010

3011

3012

3013

3014

3015

3016

3017

3018

3019

3020

3021

3022

3023

3024

3025

3026

3027

3028

3029

3030

3031

3032

3033

3034

3035

3036

3037

3038

3039

3040

3041

3042

3043

3044

3045

3046

3047

3048

3049

3050

3051

3052

3053

3054

3055

3056

3057

3058

3059

3060

3061

3062

3063

3064

3065

3066

3067

3068

3069

3070

3071

3072

3073

3074

3075

3076

3077

3078

3079

3080

3081

3082

3083

3084

3085

3086

3087

3088

3089

3090

3091

3092

3093

3094

3095

3096

3097

3098

3099

3100

3101

3102

3103

3104

3105

3106

3107

3108

3109

3110

3111

3112

3113

3114

3115

3116

3117

3118

3119

3120

3121

3122

3123

3124

3125

3126

3127

3128

3129

3130

3131

3132

3133

3134

3135

3136

3137

3138

3139

3140

3141

3142

3143

3144

3145

3146

3147

3148

3149

3150

3151

3152

3153

3154

3155

3156

3157

3158

3159

3160

3161

3162

3163

3164

3165

3166

3167

3168

3169

3170

3171

3172

3173

3174

3175

3176

3177

3178

3179

3180

3181

3182

3183

3184

3185

3186

3187

3188

3189

3190

3191

3192

3193

3194

3195

3196

3197

3198

3199

3200

3201

3202

3203

3204

3205

3206

3207

3208

3209

3210

3211

3212

3213

3214

3215

3216

3217

3218

3219

3220

3221

3222

3223

3224

3225

3226

3227

3228

3229

3230

3231

3232

3233

3234

3235

3236

3237

3238

3239

3240

3241

3242

3243

3244

3245

3246

3247

3248

3249

3250

3251

3252

3253

3254

3255

3256

3257

3258

3259

3260

3261

3262

3263

3264

3265

3266

3267

3268

3269

3270

3271

3272

3273

3274

3275

3276

3277

3278

3279

3280

3281

3282

3283

3284

3285

3286

3287

3288

3289

3290

3291

3292

3293

3294

3295

3296

3297

3298

3299

3300

3301

3302

3303

3304

3305

3306

3307

3308

3309

3310

3311

3312

3313

3314

3315

3316

3317

3318

3319

3320

3321

3322

3323

3324

3325

3326

3327

3328

3329

3330

3331

3332

3333

3334

3335

3336

3337

3338

3339

3340

3341

3342

3343

3344

3345

3346

3347

3348

3349

3350

3351

3352

3353

3354

3355

3356

3357

3358

3359

3360

3361

3362

3363

3364

3365

3366

3367

3368

3369

3370

3371

3372

3373

3374

3375

3376

3377

3378

3379

3380

3381

3382

3383

3384

3385

3386

3387

3388

3389

3390

3391

3392

3393

3394

3395

3396

3397

3398

3399

3400

3401

3402

3403

3404

3405

3406

3407

3408

3409

3410

3411

3412

3413

3414

3415

3416

3417

3418

3419

3420

3421

3422

3423

3424

3425

3426

3427

3428

3429

3430

3431

3432

3433

3434

3435

3436

3437

3438

3439

3440

3441

3442

3443

3444

3445

3446

3447

3448

3449

3450

3451

3452

3453

3454

3455

3456

3457

3458

3459

3460

3461

3462

3463

3464

3465

3466

3467

3468

3469

3470

3471

3472

3473

3474

3475

3476

3477

3478

3479

3480

3481

3482

3483

3484

3485

3486

3487

3488

3489

3490

3491

3492

3493

3494

3495

3496

3497

3498

3499

3500

3501

3502

3503

3504

3505

3506

3507

3508

3509

3510

3511

3512

3513

3514

3515

3516

3517

3518

3519

3520

3521

3522

3523

3524

3525

3526

3527

3528

3529

3530

3531

3532

3533

3534

3535

3536

3537

3538

3539

3540

3541

3542

3543

3544

3545

3546

3547

3548

3549

3550

3551

3552

3553

3554

3555

3556

3557

3558

3559

3560

3561

3562

3563

3564

3565

3566

3567

3568

3569

3570

3571

3572

3573

3574

3575

3576

3577

3578

3579

3580

3581

3582

3583

3584

3585

3586

3587

3588

3589

3590

3591

3592

3593

3594

3595

3596

3597

3598

3599

3600

3601

3602

3603

3604

3605

3606

3607

3608

3609

3610

3611

3612

3613

3614

3615

3616

3617

3618

3619

3620

3621

3622

3623

3624

3625

3626

3627

3628

3629

3630

3631

3632

3633

3634

3635

3636

3637

3638

3639

3640

3641

3642

3643

3644

3645

3646

3647

3648

3649

3650

3651

3652

3653

3654

3655

3656

3657

3658

3659

3660

3661

3662

3663

3664

3665

3666

3667

3668

3669

3670

3671

3672

3673

3674

3675

3676

3677

3678

3679

3680

3681

3682

3683

3684

3685

3686

3687

3688

3689

3690

3691

3692

3693

3694

3695

3696

3697

3698

3699

3700

3701

3702

3703

3704

3705

3706

3707

3708

3709

3710

3711

3712

3713

3714

3715

3716

3717

3718

3719

3720

3721

3722

3723

3724

3725

3726

3727

3728

3729

3730

3731

3732

3733

3734

3735

3736

3737

3738

3739

3740

3741

3742

3743

3744

3745

3746

3747

3748

3749

3750

3751

3752

3753

3754

3755

3756

3757

3758

3759

3760

3761

3762

3763

3764

3765

3766

3767

3768

3769

3770

3771

3772

3773

3774

3775

3776

3777

3778

3779

3780

3781

3782

3783

3784

3785

3786

3787

3788

3789

3790

3791

3792

3793

3794

3795

3796

3797

3798

3799

3800

3801

3802

3803

3804

3805

3806

3807

3808

3809

3810

3811

3812

3813

3814

3815

3816

3817

3818

3819

3820

3821

3822

3823

3824

3825

3826

3827

3828

3829

3830

3831

3832

3833

3834

3835

3836

3837

3838

3839

3840

3841

3842

3843

3844

3845

3846

3847

3848

3849

3850

3851

3852

3853

3854

3855

3856

3857

3858

3859

3860

3861

3862

3863

3864

3865

3866

3867

3868

3869

3870

3871

3872

3873

3874

3875

3876

3877

3878

3879

3880

3881

3882

3883

3884

3885

3886

3887

3888

3889

3890

3891

3892

3893

3894

3895

3896

3897

3898

3899

3900

3901

3902

3903

3904

3905

3906

3907

3908

3909

3910

3911

3912

3913

3914

3915

3916

3917

3918

3919

3920

3921

3922

3923

3924

3925

3926

3927

3928

3929

3930

3931

3932

3933

3934

3935

3936

3937

3938

3939

3940

3941

3942

3943

3944

3945

3946

3947

3948

3949

3950

3951

3952

3953

3954

3955

3956

3957

3958

3959

3960

3961

3962

3963

3964

3965

3966

3967

3968

3969

3970

3971

3972

3973

3974

3975

3976

3977

3978

3979

3980

3981

3982

3983

3984

3985

3986

3987

3988

3989

3990

3991

3992

3993

3994

3995

3996

3997

3998

3999

4000

4001

4002

4003

4004

4005

4006

4007

4008

4009

4010

4011

4012

4013

4014

4015

4016

4017

4018

4019

4020

4021

4022

4023

4024

4025

4026

4027

4028

4029

4030

4031

4032

4033

4034

4035

4036

4037

4038

4039

4040

4041

4042

4043

4044

4045

4046

4047

4048

4049

4050

4051

4052

4053

4054

4055

4056

4057

4058

4059

4060

4061

4062

4063

4064

4065

4066

4067

4068

4069

4070

4071

4072

4073

4074

4075

4076

4077

4078

4079

4080

4081

4082

4083

4084

4085

4086

4087

4088

4089

4090

4091

4092

4093

4094

4095

4096

4097

4098

4099

4100

4101

4102

4103

4104

4105

4106

4107

4108

4109

4110

4111

4112

4113

4114

4115

4116

4117

4118

4119

4120

4121

4122

4123

4124

4125

4126

4127

4128

4129

4130

4131

4132

4133

4134

4135

4136

4137

4138

4139

4140

4141

4142

4143

4144

4145

4146

4147

4148

4149

4150

4151

4152

4153

4154

4155

4156

4157

4158

4159

4160

4161

4162

4163

4164

4165

4166

4167

4168

4169

4170

4171

4172

4173

4174

4175

4176

4177

4178

4179

4180

4181

4182

4183

4184

4185

4186

4187

4188

4189

4190

4191

4192

4193

4194

4195

4196

4197

4198

4199

4200

4201

4202

4203

4204

4205

4206

4207

4208

4209

4210

4211

4212

4213

4214

4215

4216

4217

4218

4219

4220

4221

4222

4223

4224

4225

4226

4227

4228

4229

4230

4231

4232

4233

4234

4235

4236

4237

4238

4239

4240

4241

4242

4243

4244

4245

4246

4247

4248

4249

4250

4251

4252

4253

4254

4255

4256

4257

4258

4259

4260

4261

4262

4263

4264

4265

4266

4267

4268

4269

4270

4271

4272

4273

4274

4275

4276

4277

4278

4279

4280

4281

4282

4283

4284

4285

4286

4287

4288

4289

4290

4291

4292

4293

4294

4295

4296

4297

4298

4299

4300

4301

4302

4303

4304

4305

4306

4307

4308

4309

4310

4311

4312

4313

4314

4315

4316

4317

4318

4319

4320

4321

4322

4323

4324

4325

4326

4327

4328

4329

4330

4331

4332

4333

4334

4335

4336

4337

4338

4339

4340

4341

4342

4343

4344

4345

4346

4347

4348

4349

4350

4351

4352

4353

4354

4355

4356

4357

4358

4359

4360

4361

4362

4363

4364

4365

4366

4367

4368

4369

4370

4371

4372

4373

4374

4375

4376

4377

4378

4379

4380

4381

4382

4383

4384

4385

4386

4387

4388

4389

4390

4391

4392

4393

4394

4395

4396

4397

4398

4399

4400

4401

4402

4403

4404

4405

4406

4407

4408

4409

4410

4411

4412

4413

4414

4415

4416

4417

4418

4419

4420

4421

4422

4423

4424

4425

4426

4427

4428

4429

4430

4431

4432

4433

4434

4435

4436

4437

4438

4439

4440

4441

4442

4443

4444

4445

4446

4447

4448

4449

4450

4451

4452

4453

4454

4455

4456

4457

4458

4459

4460

4461

4462

4463

4464

4465

4466

4467

4468

4469

4470

4471

4472

4473

4474

4475

4476

4477

4478

4479

4480

4481

4482

4483

4484

4485

4486

4487

4488

4489

4490

4491

4492

4493

4494

4495

4496

4497

4498

4499

4500

4501

4502

4503

4504

4505

4506

4507

4508

4509

4510

4511

4512

4513

4514

4515

4516

4517

4518

4519

4520

4521

4522

4523

4524

4525

4526

4527

4528

4529

4530

4531

4532

4533

4534

4535

4536

4537

4538

4539

4540

4541

4542

4543

4544

4545

4546

4547

4548

4549

4550

4551

4552

4553

4554

4555

4556

4557

4558

4559

4560

4561

4562

4563

4564

4565

4566

4567

4568

4569

4570

4571

4572

4573

4574

4575

4576

4577

4578

4579

4580

4581

4582

4583

4584

4585

4586

4587

4588

4589

4590

4591

4592

4593

4594

4595

4596

4597

4598

4599

4600

4601

4602

4603

4604

4605

4606

4607

4608

4609

4610

4611

4612

4613

4614

4615

4616

4617

4618

4619

4620

4621

4622

4623

4624

4625

4626

4627

4628

4629

4630

4631

4632

4633

4634

4635

4636

4637

4638

4639

4640

4641

4642

4643

4644

4645

4646

4647

4648

4649

4650

4651

4652

4653

4654

4655

4656

4657

4658

4659

4660

4661

4662

4663

4664

4665

4666

4667

4668

4669

4670

4671

4672

4673

4674

4675

4676

4677

4678

4679

4680

4681

4682

4683

4684

4685

4686

4687

4688

4689

4690

4691

4692

4693

4694

4695

4696

4697

4698

4699

4700

4701

4702

4703

4704

4705

4706

4707

4708

4709

4710

4711

4712

4713

4714

4715

4716

4717

4718

4719

4720

4721

4722

4723

4724

4725

4726

4727

4728

4729

4730

4731

4732

4733

4734

4735

4736

4737

4738

4739

4740

4741

4742

4743

4744

4745

4746

4747

4748

4749

4750

4751

4752

4753

4754

4755

4756

4757

4758

4759

4760

4761

4762

4763

4764

4765

4766

4767

4768

4769

4770

4771

4772

4773

4774

4775

4776

4777

4778

4779

4780

4781

4782

4783

4784

4785

4786

4787

4788

4789

4790

4791

4792

4793

4794

4795

4796

4797

4798

4799

4800

4801

4802

4803

4804

4805

4806

4807

4808

4809

4810

4811

4812

4813

4814

4815

4816

4817

4818

4819

4820

4821

4822

4823

4824

4825

4826

4827

4828

4829

4830

4831

4832

4833

4834

4835

4836

4837

4838

4839

4840

4841

4842

4843

4844

4845

4846

4847

4848

4849

4850

4851

4852

4853

4854

4855

4856

4857

4858

4859

4860

4861

4862

4863

4864

4865

4866

4867

4868

4869

4870

4871

4872

4873

4874

4875

4876

4877

4878

4879

4880

4881

4882

4883

4884

4885

4886

4887

4888

4889

4890

4891

4892

4893

4894

4895

4896

4897

4898

4899

4900

4901

4902

4903

4904

4905

4906

4907

4908

4909

4910

4911

4912

4913

4914

4915

4916

4917

4918

4919

4920

4921

4922

4923

4924

4925

4926

4927

4928

4929

4930

4931

4932

4933

4934

4935

4936

4937

4938

4939

4940

4941

4942

4943

4944

4945

4946

4947

4948

4949

4950

4951

4952

4953

4954

4955

4956

4957

4958

4959

4960

4961

4962

4963

4964

4965

4966

4967

4968

4969

4970

4971

4972

4973

4974

4975

4976

4977

4978

4979

4980

4981

4982

4983

4984

4985

4986

4987

4988

4989

4990

4991

4992

4993

4994

4995

4996

4997

4998

4999

5000

5001

5002

5003

5004

5005

5006

5007

5008

5009

5010

5011

5012

5013

5014

5015

5016

5017

5018

5019

5020

5021

5022

5023

5024

5025

5026

5027

5028

5029

5030

5031

5032

5033

5034

5035

5036

5037

5038

5039

5040

5041

5042

5043

5044

5045

5046

5047

5048

5049

5050

5051

5052

5053

5054

5055

5056

5057

5058

5059

5060

5061

5062

5063

5064

5065

5066

5067

5068

5069

5070

5071

5072

5073

5074

5075

5076

5077

5078

5079

5080

5081

5082

5083

5084

5085

5086

5087

5088

5089

5090

5091

5092

5093

5094

5095

5096

5097

5098

5099

5100

5101

5102

5103

5104

5105

5106

5107

5108

5109

5110

5111

5112

5113

5114

5115

5116

5117

5118

5119

5120

5121

5122

5123

5124

5125

5126

5127

5128

5129

5130

5131

5132

5133

5134

5135

5136

5137

5138

5139

5140

5141

5142

5143

5144

5145

5146

5147

5148

5149

5150

5151

5152

5153

5154

5155

5156

5157

5158

5159

5160

5161

5162

5163

5164

5165

5166

5167

5168

5169

5170

5171

5172

5173

5174

5175

5176

5177

5178

5179

5180

5181

5182

5183

5184

5185

5186

5187

5188

5189

5190

5191

5192

5193

5194

5195

5196

5197

5198

5199

5200

5201

5202

5203

5204

5205

5206

5207

5208

5209

5210

5211

5212

5213

5214

5215

5216

5217

5218

5219

5220

5221

5222

5223

5224

5225

5226

5227

5228

5229

5230

5231

5232

5233

5234

5235

5236

5237

5238

5239

5240

5241

5242

5243

5244

5245

5246

5247

5248

5249

5250

5251

5252

5253

5254

5255

5256

5257

5258

5259

5260

5261

5262

5263

5264

5265

5266

5267

5268

5269

5270

5271

5272

5273

5274

5275

5276

5277

5278

5279

5280

5281

5282

5283

5284

5285

5286

5287

5288

5289

5290

5291

5292

5293

5294

5295

5296

5297

5298

5299

5300

5301

5302

5303

5304

5305

5306

5307

5308

5309

5310

5311

5312

5313

5314

5315

5316

5317

5318

5319

5320

5321

5322

5323

5324

5325

5326

5327

5328

5329

5330

5331

5332

5333

5334

5335

5336

5337

5338

5339

5340

5341

5342

5343

5344

5345

5346

5347

5348

5349

5350

5351

5352

5353

5354

5355

5356

5357

5358

5359

5360

5361

5362

5363

5364

5365

5366

5367

5368

5369

5370

5371

5372

5373

5374

5375

5376

5377

5378

5379

5380

5381

5382

5383

5384

5385

5386

5387

5388

5389

5390

5391

5392

5393

5394

5395

5396

5397

5398

5399

5400

5401

5402

5403

5404

5405

5406

5407

5408

5409

5410

5411

5412

5413

5414

5415

5416

5417

5418

5419

5420

5421

5422

5423

5424

5425

5426

5427

5428

5429

5430

5431

5432

5433

5434

5435

5436

5437

5438

5439

5440

5441

5442

5443

5444

5445

5446

5447

5448

5449

5450

5451

5452

5453

5454

5455

5456

5457

5458

5459

5460

5461

5462

5463

5464

5465

5466

5467

5468

5469

5470

5471

5472

5473

5474

5475

5476

5477

5478

5479

5480

5481

5482

5483

5484

5485

5486

5487

5488

5489

5490

5491

5492

5493

5494

5495

5496

5497

5498

5499

5500

5501

5502

5503

5504

5505

5506

5507

5508

5509

5510

5511

5512

5513

5514

5515

5516

5517

5518

5519

5520

5521

5522

5523

5524

5525

5526

5527

5528

5529

5530

5531

5532

5533

5534

5535

5536

5537

5538

5539

5540

5541

5542

5543

5544

5545

5546

5547

5548

5549

5550

5551

5552

5553

5554

5555

5556

5557

5558

5559

5560

5561

5562

5563

5564

5565

5566

5567

5568

5569

5570

5571

5572

5573

5574

5575

5576

5577

5578

5579

5580

5581

5582

5583

5584

5585

5586

5587

5588

5589

5590

5591

5592

5593

5594

5595

5596

5597

5598

5599

5600

5601

5602

5603

5604

5605

5606

5607

5608

5609

5610

5611

5612

5613

5614

5615

5616

5617

5618

5619

5620

5621

5622

5623

5624

5625

5626

5627

5628

5629

5630

5631

5632

5633

5634

5635

5636

5637

5638

5639

5640

5641

5642

5643

5644

5645

5646

5647

5648

5649

5650

5651

5652

5653

5654

5655

5656

5657

5658

5659

5660

5661

5662

5663

5664

5665

5666

5667

5668

5669

5670

5671

5672

5673

5674

5675

5676

5677

5678

5679

5680

5681

5682

5683

5684

5685

5686

5687

5688

5689

5690

5691

5692

5693

5694

5695

5696

5697

5698

5699

5700

5701

5702

5703

5704

5705

5706

5707

5708

5709

5710

5711

5712

5713

5714

5715

5716

5717

5718

5719

5720

5721

5722

5723

5724

5725

5726

5727

5728

5729

5730

5731

5732

5733

5734

5735

5736

5737

5738

5739

5740

5741

5742

5743

5744

5745

5746

5747

5748

5749

5750

5751

5752

5753

5754

5755

5756

5757

5758

5759

5760

5761

5762

5763

5764

5765

5766

5767

5768

5769

5770

5771

5772

5773

5774

5775

5776

5777

5778

5779

5780

5781

5782

5783

5784

5785

5786

5787

5788

5789

5790

5791

5792

5793

5794

5795

5796

5797

5798

5799

5800

5801

5802

5803

5804

5805

5806

5807

5808

5809

5810

5811

5812

5813

5814

5815

5816

5817

5818

5819

5820

5821

5822

5823

5824

5825

5826

5827

5828

5829

5830

5831

5832

5833

5834

5835

5836

5837

5838

5839

5840

5841

5842

5843

5844

5845

5846

5847

5848

5849

5850

5851

5852

5853

5854

5855

5856

5857

5858

5859

5860

5861

5862

5863

5864

5865

5866

5867

5868

5869

5870

5871

5872

5873

5874

5875

5876

5877

5878

5879

5880

5881

5882

5883

5884

5885

5886

5887

5888

5889

5890

5891

5892

5893

5894

5895

5896

5897

5898

5899

5900

5901

5902

5903

5904

5905

5906

5907

5908

5909

5910

5911

5912

5913

5914

5915

5916

5917

5918

5919

5920

5921

5922

5923

5924

5925

5926

5927

5928

5929

5930

5931

5932

5933

5934

5935

5936

5937

5938

5939

5940

5941

5942

5943

5944

5945

5946

5947

5948

5949

5950

5951

5952

5953

5954

5955

5956

5957

5958

5959

5960

5961

5962

5963

5964

5965

5966

5967

5968

5969

5970

5971

5972

5973

5974

5975

5976

5977

5978

5979

5980

5981

5982

5983

5984

5985

5986

5987

5988

5989

5990

5991

5992

5993

5994

5995

5996

5997

5998

5999

6000

6001

6002

6003

6004

6005

6006

6007

6008

6009

6010

6011

6012

6013

6014

6015

6016

6017

6018

6019

6020

6021

6022

6023

6024

6025

6026

6027

6028

6029

6030

6031

6032

6033

6034

6035

6036

6037

6038

6039

6040

6041

6042

6043

6044

6045

6046

6047

6048

6049

6050

6051

6052

6053

6054

6055

6056

6057

6058

6059

6060

6061

6062

6063

6064

6065

6066

6067

6068

6069

6070

6071

6072

6073

6074

6075

6076

6077

6078

6079

6080

6081

6082

6083

6084

6085

6086

6087

6088

6089

6090

6091

6092

6093

6094

6095

6096

6097

6098

6099

6100

6101

6102

6103

6104

6105

6106

6107

6108

6109

6110

6111

6112

6113

6114

6115

6116

6117

6118

6119

6120

6121

6122

6123

6124

6125

6126

6127

6128

6129

6130

6131

6132

6133

6134

6135

6136

6137

6138

6139

6140

6141

6142

6143

6144

6145

6146

6147

6148

6149

6150

6151

6152

6153

6154

6155

6156

6157

6158

6159

6160

6161

6162

6163

6164

6165

6166

6167

6168

6169

6170

6171

6172

6173

6174

6175

6176

6177

6178

6179

6180

6181

6182

6183

6184

6185

6186

6187

6188

6189

6190

6191

6192

6193

6194

6195

6196

6197

6198

6199

6200

6201

6202

6203

6204

6205

6206

6207

6208

6209

6210

6211

6212

6213

6214

6215

6216

6217

6218

6219

6220

6221

6222

6223

6224

6225

6226

6227

6228

6229

6230

6231

6232

6233

6234

6235

6236

6237

6238

6239

6240

6241

6242

6243

6244

6245

6246

6247

6248

6249

6250

6251

6252

6253

6254

6255

6256

6257

6258

6259

6260

6261

6262

6263

6264

6265

6266

6267

6268

6269

6270

6271

6272

6273

6274

6275

6276

6277

6278

6279

6280

6281

6282

6283

6284

6285

6286

6287

6288

6289

6290

6291

6292

6293

6294

6295

6296

6297

6298

6299

6300

6301

6302

6303

6304

6305

6306

6307

6308

6309

6310

6311

6312

6313

6314

6315

6316

6317

6318

6319

6320

6321

6322

6323

6324

6325

6326

6327

6328

6329

6330

6331

6332

6333

6334

6335

6336

6337

6338

6339

6340

6341

6342

6343

6344

6345

6346

6347

6348

6349

6350

6351

6352

6353

6354

6355

6356

6357

6358

6359

6360

6361

6362

6363

6364

6365

6366

6367

6368

6369

6370

6371

6372

6373

6374

6375

6376

6377

6378

6379

6380

6381

6382

6383

6384

6385

6386

6387

6388

6389

6390

6391

6392

6393

6394

6395

6396

6397

6398

6399

6400

6401

6402

6403

6404

6405

6406

6407

6408

6409

6410

6411

6412

6413

6414

6415

6416

6417

6418

6419

6420

6421

6422

6423

6424

6425

6426

6427

6428

6429

6430

6431

6432

6433

6434

6435

6436

6437

6438

6439

6440

6441

6442

6443

6444

6445

6446

6447

6448

6449

6450

6451

6452

6453

6454

6455

6456

6457

6458

6459

6460

6461

6462

6463

6464

6465

6466

6467

6468

6469

6470

6471

6472

6473

6474

6475

6476

6477

6478

6479

6480

6481

6482

6483

6484

6485

6486

6487

6488

6489

6490

6491

6492

6493

6494

6495

6496

6497

6498

6499

6500

6501

6502

6503

6504

6505

6506

6507

6508

6509

6510

6511

6512

6513

6514

6515

6516

6517

6518

6519

6520

6521

6522

6523

6524

6525

6526

6527

6528

6529

6530

6531

6532

6533

6534

6535

6536

6537

6538

6539

6540

6541

6542

6543

6544

6545

6546

6547

6548

6549

6550

6551

6552

6553

6554

6555

6556

6557

6558

6559

6560

6561

6562

6563

6564

6565

6566

6567

6568

6569

6570

6571

6572

6573

6574

6575

6576

6577

6578

6579

6580

6581

6582

6583

6584

6585

6586

6587

6588

6589

6590

6591

6592

6593

6594

6595

6596

6597

6598

6599

6600

6601

6602

6603

6604

6605

6606

6607

6608

6609

6610

6611

6612

6613

6614

6615

6616

6617

6618

6619

6620

6621

6622

6623

6624

6625

6626

6627

6628

6629

6630

6631

6632

6633

6634

6635

6636

6637

6638

6639

6640

6641

6642

6643

6644

6645

6646

6647

6648

6649

6650

6651

6652

6653

6654

6655

6656

6657

6658

6659

6660

6661

6662

6663

6664

6665

6666

6667

6668

6669

6670

6671

6672

6673

6674

6675

6676

6677

6678

6679

6680

6681

6682

6683

6684

6685

6686

6687

6688

6689

6690

6691

6692

6693

6694

6695

6696

6697

6698

6699

6700

6701

6702

6703

6704

6705

6706

6707

6708

6709

6710

6711

6712

6713

6714

6715

6716

6717

6718

6719

6720

6721

6722

6723

6724

6725

6726

6727

6728

6729

6730

6731

6732

6733

6734

6735

6736

6737

6738

6739

6740

6741

6742

6743

6744

6745

6746

6747

6748

6749

6750

6751

6752

6753

6754

6755

6756

6757

6758

6759

6760

6761

6762

6763

6764

6765

6766

6767

6768

6769

6770

6771

6772

6773

6774

6775

6776

6777

6778

6779

6780

6781

6782

6783

6784

6785

6786

6787

6788

6789

6790

6791

6792

6793

6794

6795

6796

6797

6798

6799

6800

6801

6802

6803

6804

6805

6806

6807

6808

6809

6810

6811

6812

6813

6814

6815

6816

6817

6818

6819

6820

6821

6822

6823

6824

6825

6826

6827

6828

6829

6830

6831

6832

6833

6834

6835

6836

6837

6838

6839

6840

6841

6842

6843

6844

6845

6846

6847

6848

6849

6850

6851

6852

6853

6854

6855

6856

6857

6858

6859

6860

6861

6862

6863

6864

6865

6866

6867

6868

6869

6870

6871

6872

6873

6874

6875

6876

6877

6878

6879

6880

6881

6882

6883

6884

6885

6886

6887

6888

6889

6890

6891

6892

6893

6894

6895

6896

6897

6898

6899

6900

6901

6902

6903

6904

6905

6906

6907

6908

6909

6910

6911

6912

6913

6914

6915

6916

6917

6918

6919

6920

6921

6922

6923

6924

6925

6926

6927

6928

6929

6930

6931

6932

6933

6934

6935

6936

6937

6938

6939

6940

6941

6942

6943

6944

6945

6946

6947

6948

6949

6950

6951

6952

6953

6954

6955

6956

6957

6958

6959

6960

6961

6962

6963

6964

6965

6966

6967

6968

6969

6970

6971

6972

6973

6974

6975

6976

6977

6978

6979

6980

6981

6982

6983

6984

6985

6986

6987

6988

6989

6990

6991

6992

6993

6994

6995

6996

6997

6998

6999

7000

7001

7002

7003

7004

7005

7006

7007

7008

7009

7010

7011

7012

7013

7014

7015

7016

7017

7018

7019

7020

7021

7022

7023

7024

7025

7026

7027

7028

7029

7030

7031

7032

7033

7034

7035

7036

7037

7038

7039

7040

7041

7042

7043

7044

7045

7046

7047

7048

7049

7050

7051

7052

7053

7054

7055

7056

7057

7058

7059

7060

7061

7062

7063

7064

7065

7066

7067

7068

7069

7070

7071

7072

7073

7074

7075

7076

7077

7078

7079

7080

7081

7082

7083

7084

7085

7086

7087

7088

7089

7090

7091

7092

7093

7094

7095

7096

7097

7098

7099

7100

7101

7102

7103

7104

7105

7106

7107

7108

7109

7110

7111

7112

7113

7114

7115

7116

7117

7118

7119

7120

7121

7122

7123

7124

7125

7126

7127

7128

7129

7130

7131

7132

7133

7134

7135

7136

7137

7138

7139

7140

7141

7142

7143

7144

7145

7146

7147

7148

7149

7150

7151

7152

7153

7154

7155

7156

7157

7158

7159

7160

7161

7162

7163

7164

7165

7166

7167

7168

7169

7170

7171

7172

7173

7174

7175

7176

7177

7178

7179

7180

7181

7182

7183

7184

7185

7186

7187

7188

7189

7190

7191

7192

7193

7194

7195

7196

7197

7198

7199

7200

7201

7202

7203

7204

7205

7206

7207

7208

7209

7210

7211

7212

7213

7214

7215

7216

7217

7218

7219

7220

7221

7222

7223

7224

7225

7226

7227

7228

7229

7230

7231

7232

7233

7234

7235

7236

7237

7238

7239

7240

7241

7242

7243

7244

7245

7246

7247

7248

7249

7250

7251

7252

7253

7254

7255

7256

7257

7258

7259

7260

7261

7262

7263

7264

7265

7266

7267

7268

7269

7270

7271

7272

7273

7274

7275

7276

7277

7278

7279

7280

7281

7282

7283

7284

7285

7286

7287

7288

7289

7290

7291

7292

7293

7294

7295

7296

7297

7298

7299

7300

7301

7302

7303

7304

7305

7306

7307

7308

7309

7310

7311

7312

7313

7314

7315

7316

7317

7318

7319

7320

7321

7322

7323

7324

7325

7326

7327

7328

7329

7330

7331

7332

7333

7334

7335

7336

7337

7338

7339

7340

7341

7342

7343

7344

7345

7346

7347

7348

7349

7350

7351

7352

7353

7354

7355

7356

7357

7358

7359

7360

7361

7362

7363

7364

7365

7366

7367

7368

7369

7370

7371

7372

7373

7374

7375

7376

7377

7378

7379

7380

7381

7382

7383

7384

7385

7386

7387

7388

7389

7390

7391

7392

7393

7394

7395

7396

7397

7398

7399

7400

7401

7402

7403

7404

7405

7406

7407

7408

7409

7410

7411

7412

7413

7414

7415

7416

7417

7418

7419

7420

7421

7422

7423

7424

7425

7426

7427

7428

7429

7430

7431

7432

7433

7434

7435

7436

7437

7438

7439

7440

7441

7442

7443

7444

7445

7446

7447

7448

7449

7450

7451

7452

7453

7454

7455

7456

7457

7458

7459

7460

7461

7462

7463

7464

7465

7466

7467

7468

7469

7470

7471

7472

7473

7474

7475

7476

7477

7478

7479

7480

7481

7482

7483

7484

7485

7486

7487

7488

7489

7490

7491

7492

7493

7494

7495

7496

7497

7498

7499

7500

7501

7502

7503

7504

7505

7506

7507

7508

7509

7510

7511

7512

7513

7514

7515

7516

7517

7518

7519

7520

7521

7522

7523

7524

7525

7526

7527

7528

7529

7530

7531

7532

7533

7534

7535

7536

7537

7538

7539

7540

7541

7542

7543

7544

7545

7546

7547

7548

7549

7550

7551

7552

7553

7554

7555

7556

7557

7558

7559

7560

7561

7562

7563

7564

7565

7566

7567

7568

7569

7570

7571

7572

7573

7574

7575

7576

7577

7578

7579

7580

7581

7582

7583

7584

7585

7586

7587

7588

7589

7590

7591

7592

7593

7594

7595

7596

7597

7598

7599

7600

7601

7602

7603

7604

7605

7606

7607

7608

7609

7610

7611

7612

7613

7614

7615

7616

7617

7618

7619

7620

7621

7622

7623

7624

7625

7626

7627

7628

7629

7630

7631

7632

7633

7634

7635

7636

7637

7638

7639

7640

7641

7642

7643

7644

7645

7646

7647

7648

7649

7650

7651

7652

7653

7654

7655

7656

7657

7658

7659

7660

7661

7662

7663

7664

7665

7666

7667

7668

7669

7670

7671

7672

7673

7674

7675

7676

7677

7678

7679

7680

7681

7682

7683

7684

7685

7686

7687

7688

7689

7690

7691

7692

7693

7694

7695

7696

7697

7698

7699

7700

7701

7702

7703

7704

7705

7706

7707

7708

7709

7710

7711

7712

7713

7714

7715

7716

7717

7718

7719

7720

7721

7722

7723

7724

7725

7726

7727

7728

7729

7730

7731

7732

7733

7734

7735

7736

7737

7738

7739

7740

7741

7742

7743

7744

7745

7746

7747

7748

7749

7750

7751

7752

7753

7754

7755

7756

7757

7758

7759

7760

7761

7762

7763

7764

7765

7766

7767

7768

7769

7770

7771

7772

7773

7774

7775

7776

7777

7778

7779

7780

7781

7782

7783

7784

7785

7786

7787

7788

7789

7790

7791

7792

7793

7794

7795

7796

7797

7798

7799

7800

7801

7802

7803

7804

7805

7806

7807

7808

7809

7810

7811

7812

7813

7814

7815

7816

7817

7818

7819

7820

7821

7822

7823

7824

7825

7826

7827

7828

7829

7830

7831

7832

7833

7834

7835

7836

7837

7838

7839

7840

7841

7842

7843

7844

7845

7846

7847

7848

7849

7850

7851

7852

7853

7854

7855

7856

7857

7858

7859

7860

7861

7862

7863

7864

7865

7866

7867

7868

7869

7870

7871

7872

7873

7874

7875

7876

7877

7878

7879

7880

7881

7882

7883

7884

7885

7886

7887

7888

7889

7890

7891

7892

7893

7894

7895

7896

7897

7898

7899

7900

7901

7902

7903

7904

7905

7906

7907

7908

7909

7910

7911

7912

7913

7914

7915

7916

7917

7918

7919

7920

7921

7922

7923

7924

7925

7926

7927

7928

7929

7930

7931

7932

7933

7934

7935

7936

7937

7938

7939

7940

7941

7942

7943

7944

7945

7946

7947

7948

7949

7950

7951

7952

7953

7954

7955

7956

7957

7958

7959

7960

7961

7962

7963

7964

7965

7966

7967

7968

7969

7970

7971

7972

7973

7974

7975

7976

7977

7978

7979

7980

7981

7982

7983

7984

7985

7986

7987

7988

7989

7990

7991

7992

7993

7994

7995

7996

7997

7998

7999

8000

8001

8002

8003

8004

8005

8006

8007

8008

8009

8010

8011

8012

8013

8014

8015

8016

8017

8018

8019

8020

8021

8022

8023

8024

8025

8026

8027

8028

8029

8030

8031

8032

8033

8034

8035

8036

8037

8038

8039

8040

8041

8042

8043

8044

8045

8046

8047

8048

8049

8050

8051

8052

8053

8054

8055

8056

8057

8058

8059

8060

8061

8062

8063

8064

8065

8066

8067

8068

8069

8070

8071

8072

8073

8074

8075

8076

8077

8078

8079

8080

8081

8082

8083

8084

8085

8086

8087

8088

8089

8090

8091

8092

8093

8094

8095

8096

8097

8098

8099

8100

8101

8102

8103

8104

8105

8106

8107

8108

8109

8110

8111

8112

8113

8114

8115

8116

8117

8118

8119

8120

8121

8122

8123

8124

8125

8126

8127

8128

8129

8130

8131

8132

8133

8134

8135

8136

8137

8138

8139

8140

8141

8142

8143

8144

8145

8146

8147

8148

8149

8150

8151

8152

8153

8154

8155

8156

8157

8158

8159

8160

8161

8162

8163

8164

8165

8166

8167

8168

8169

8170

8171

8172

8173

8174

8175

8176

8177

8178

8179

8180

8181

8182

8183

8184

8185

8186

8187

8188

8189

8190

8191

8192

8193

8194

8195

8196

8197

8198

8199

8200

8201

8202

8203

8204

8205

8206

8207

8208

8209

8210

8211

8212

8213

8214

8215

8216

8217

8218

8219

8220

8221

8222

8223

8224

8225

8226

8227

8228

8229

8230

8231

8232

8233

8234

8235

8236

8237

8238

8239

8240

8241

8242

8243

8244

8245

8246

8247

8248

8249

8250

8251

8252

8253

8254

8255

8256

8257

8258

8259

8260

8261

8262

8263

8264

8265

8266

8267

8268

8269

8270

8271

8272

8273

8274

8275

8276

8277

8278

8279

8280

8281

8282

8283

8284

8285

8286

8287

8288

8289

8290

8291

8292

8293

8294

8295

8296

8297

8298

8299

8300

8301

8302

8303

8304

8305

8306

8307

8308

8309

8310

8311

8312

8313

8314

8315

8316

8317

8318

8319

8320

8321

8322

8323

8324

8325

8326

8327

8328

8329

8330

8331

8332

8333

8334

8335

8336

8337

8338

8339

8340

8341

8342

8343

8344

8345

8346

8347

8348

8349

8350

8351

8352

8353

8354

8355

8356

8357

8358

8359

8360

8361

8362

8363

8364

8365

8366

8367

8368

8369

8370

8371

8372

8373

8374

8375

8376

8377

8378

8379

8380

8381

8382

8383

8384

8385

8386

8387

8388

8389

8390

8391

8392

8393

8394

8395

8396

8397

8398

8399

8400

8401

8402

8403

8404

8405

8406

8407

8408

8409

8410

8411

8412

8413

8414

8415

8416

8417

8418

8419

8420

8421

8422

8423

8424

8425

8426

8427

8428

8429

8430

8431

8432

8433

8434

8435

8436

8437

8438

8439

8440

8441

8442

8443

r""" 

This module contains :py:meth:`~sympy.solvers.ode.dsolve` and different helper 

functions that it uses. 

 

:py:meth:`~sympy.solvers.ode.dsolve` solves ordinary differential equations. 

See the docstring on the various functions for their uses. Note that partial 

differential equations support is in ``pde.py``. Note that hint functions 

have docstrings describing their various methods, but they are intended for 

internal use. Use ``dsolve(ode, func, hint=hint)`` to solve an ODE using a 

specific hint. See also the docstring on 

:py:meth:`~sympy.solvers.ode.dsolve`. 

 

**Functions in this module** 

 

These are the user functions in this module: 

 

- :py:meth:`~sympy.solvers.ode.dsolve` - Solves ODEs. 

- :py:meth:`~sympy.solvers.ode.classify_ode` - Classifies ODEs into 

possible hints for :py:meth:`~sympy.solvers.ode.dsolve`. 

- :py:meth:`~sympy.solvers.ode.checkodesol` - Checks if an equation is the 

solution to an ODE. 

- :py:meth:`~sympy.solvers.ode.homogeneous_order` - Returns the 

homogeneous order of an expression. 

- :py:meth:`~sympy.solvers.ode.infinitesimals` - Returns the infinitesimals 

of the Lie group of point transformations of an ODE, such that it is 

invariant. 

- :py:meth:`~sympy.solvers.ode_checkinfsol` - Checks if the given infinitesimals 

are the actual infinitesimals of a first order ODE. 

 

These are the non-solver helper functions that are for internal use. The 

user should use the various options to 

:py:meth:`~sympy.solvers.ode.dsolve` to obtain the functionality provided 

by these functions: 

 

- :py:meth:`~sympy.solvers.ode.odesimp` - Does all forms of ODE 

simplification. 

- :py:meth:`~sympy.solvers.ode.ode_sol_simplicity` - A key function for 

comparing solutions by simplicity. 

- :py:meth:`~sympy.solvers.ode.constantsimp` - Simplifies arbitrary 

constants. 

- :py:meth:`~sympy.solvers.ode.constant_renumber` - Renumber arbitrary 

constants. 

- :py:meth:`~sympy.solvers.ode._handle_Integral` - Evaluate unevaluated 

Integrals. 

 

See also the docstrings of these functions. 

 

**Currently implemented solver methods** 

 

The following methods are implemented for solving ordinary differential 

equations. See the docstrings of the various hint functions for more 

information on each (run ``help(ode)``): 

 

- 1st order separable differential equations. 

- 1st order differential equations whose coefficients or `dx` and `dy` are 

functions homogeneous of the same order. 

- 1st order exact differential equations. 

- 1st order linear differential equations. 

- 1st order Bernoulli differential equations. 

- Power series solutions for first order differential equations. 

- Lie Group method of solving first order differential equations. 

- 2nd order Liouville differential equations. 

- Power series solutions for second order differential equations 

at ordinary and regular singular points. 

- `n`\th order linear homogeneous differential equation with constant 

coefficients. 

- `n`\th order linear inhomogeneous differential equation with constant 

coefficients using the method of undetermined coefficients. 

- `n`\th order linear inhomogeneous differential equation with constant 

coefficients using the method of variation of parameters. 

 

**Philosophy behind this module** 

 

This module is designed to make it easy to add new ODE solving methods without 

having to mess with the solving code for other methods. The idea is that 

there is a :py:meth:`~sympy.solvers.ode.classify_ode` function, which takes in 

an ODE and tells you what hints, if any, will solve the ODE. It does this 

without attempting to solve the ODE, so it is fast. Each solving method is a 

hint, and it has its own function, named ``ode_<hint>``. That function takes 

in the ODE and any match expression gathered by 

:py:meth:`~sympy.solvers.ode.classify_ode` and returns a solved result. If 

this result has any integrals in it, the hint function will return an 

unevaluated :py:class:`~sympy.integrals.Integral` class. 

:py:meth:`~sympy.solvers.ode.dsolve`, which is the user wrapper function 

around all of this, will then call :py:meth:`~sympy.solvers.ode.odesimp` on 

the result, which, among other things, will attempt to solve the equation for 

the dependent variable (the function we are solving for), simplify the 

arbitrary constants in the expression, and evaluate any integrals, if the hint 

allows it. 

 

**How to add new solution methods** 

 

If you have an ODE that you want :py:meth:`~sympy.solvers.ode.dsolve` to be 

able to solve, try to avoid adding special case code here. Instead, try 

finding a general method that will solve your ODE, as well as others. This 

way, the :py:mod:`~sympy.solvers.ode` module will become more robust, and 

unhindered by special case hacks. WolphramAlpha and Maple's 

DETools[odeadvisor] function are two resources you can use to classify a 

specific ODE. It is also better for a method to work with an `n`\th order ODE 

instead of only with specific orders, if possible. 

 

To add a new method, there are a few things that you need to do. First, you 

need a hint name for your method. Try to name your hint so that it is 

unambiguous with all other methods, including ones that may not be implemented 

yet. If your method uses integrals, also include a ``hint_Integral`` hint. 

If there is more than one way to solve ODEs with your method, include a hint 

for each one, as well as a ``<hint>_best`` hint. Your ``ode_<hint>_best()`` 

function should choose the best using min with ``ode_sol_simplicity`` as the 

key argument. See 

:py:meth:`~sympy.solvers.ode.ode_1st_homogeneous_coeff_best`, for example. 

The function that uses your method will be called ``ode_<hint>()``, so the 

hint must only use characters that are allowed in a Python function name 

(alphanumeric characters and the underscore '``_``' character). Include a 

function for every hint, except for ``_Integral`` hints 

(:py:meth:`~sympy.solvers.ode.dsolve` takes care of those automatically). 

Hint names should be all lowercase, unless a word is commonly capitalized 

(such as Integral or Bernoulli). If you have a hint that you do not want to 

run with ``all_Integral`` that doesn't have an ``_Integral`` counterpart (such 

as a best hint that would defeat the purpose of ``all_Integral``), you will 

need to remove it manually in the :py:meth:`~sympy.solvers.ode.dsolve` code. 

See also the :py:meth:`~sympy.solvers.ode.classify_ode` docstring for 

guidelines on writing a hint name. 

 

Determine *in general* how the solutions returned by your method compare with 

other methods that can potentially solve the same ODEs. Then, put your hints 

in the :py:data:`~sympy.solvers.ode.allhints` tuple in the order that they 

should be called. The ordering of this tuple determines which hints are 

default. Note that exceptions are ok, because it is easy for the user to 

choose individual hints with :py:meth:`~sympy.solvers.ode.dsolve`. In 

general, ``_Integral`` variants should go at the end of the list, and 

``_best`` variants should go before the various hints they apply to. For 

example, the ``undetermined_coefficients`` hint comes before the 

``variation_of_parameters`` hint because, even though variation of parameters 

is more general than undetermined coefficients, undetermined coefficients 

generally returns cleaner results for the ODEs that it can solve than 

variation of parameters does, and it does not require integration, so it is 

much faster. 

 

Next, you need to have a match expression or a function that matches the type 

of the ODE, which you should put in :py:meth:`~sympy.solvers.ode.classify_ode` 

(if the match function is more than just a few lines, like 

:py:meth:`~sympy.solvers.ode._undetermined_coefficients_match`, it should go 

outside of :py:meth:`~sympy.solvers.ode.classify_ode`). It should match the 

ODE without solving for it as much as possible, so that 

:py:meth:`~sympy.solvers.ode.classify_ode` remains fast and is not hindered by 

bugs in solving code. Be sure to consider corner cases. For example, if your 

solution method involves dividing by something, make sure you exclude the case 

where that division will be 0. 

 

In most cases, the matching of the ODE will also give you the various parts 

that you need to solve it. You should put that in a dictionary (``.match()`` 

will do this for you), and add that as ``matching_hints['hint'] = matchdict`` 

in the relevant part of :py:meth:`~sympy.solvers.ode.classify_ode`. 

:py:meth:`~sympy.solvers.ode.classify_ode` will then send this to 

:py:meth:`~sympy.solvers.ode.dsolve`, which will send it to your function as 

the ``match`` argument. Your function should be named ``ode_<hint>(eq, func, 

order, match)`. If you need to send more information, put it in the ``match`` 

dictionary. For example, if you had to substitute in a dummy variable in 

:py:meth:`~sympy.solvers.ode.classify_ode` to match the ODE, you will need to 

pass it to your function using the `match` dict to access it. You can access 

the independent variable using ``func.args[0]``, and the dependent variable 

(the function you are trying to solve for) as ``func.func``. If, while trying 

to solve the ODE, you find that you cannot, raise ``NotImplementedError``. 

:py:meth:`~sympy.solvers.ode.dsolve` will catch this error with the ``all`` 

meta-hint, rather than causing the whole routine to fail. 

 

Add a docstring to your function that describes the method employed. Like 

with anything else in SymPy, you will need to add a doctest to the docstring, 

in addition to real tests in ``test_ode.py``. Try to maintain consistency 

with the other hint functions' docstrings. Add your method to the list at the 

top of this docstring. Also, add your method to ``ode.rst`` in the 

``docs/src`` directory, so that the Sphinx docs will pull its docstring into 

the main SymPy documentation. Be sure to make the Sphinx documentation by 

running ``make html`` from within the doc directory to verify that the 

docstring formats correctly. 

 

If your solution method involves integrating, use :py:meth:`Integral() 

<sympy.integrals.integrals.Integral>` instead of 

:py:meth:`~sympy.core.expr.Expr.integrate`. This allows the user to bypass 

hard/slow integration by using the ``_Integral`` variant of your hint. In 

most cases, calling :py:meth:`sympy.core.basic.Basic.doit` will integrate your 

solution. If this is not the case, you will need to write special code in 

:py:meth:`~sympy.solvers.ode._handle_Integral`. Arbitrary constants should be 

symbols named ``C1``, ``C2``, and so on. All solution methods should return 

an equality instance. If you need an arbitrary number of arbitrary constants, 

you can use ``constants = numbered_symbols(prefix='C', cls=Symbol, start=1)``. 

If it is possible to solve for the dependent function in a general way, do so. 

Otherwise, do as best as you can, but do not call solve in your 

``ode_<hint>()`` function. :py:meth:`~sympy.solvers.ode.odesimp` will attempt 

to solve the solution for you, so you do not need to do that. Lastly, if your 

ODE has a common simplification that can be applied to your solutions, you can 

add a special case in :py:meth:`~sympy.solvers.ode.odesimp` for it. For 

example, solutions returned from the ``1st_homogeneous_coeff`` hints often 

have many :py:meth:`~sympy.functions.log` terms, so 

:py:meth:`~sympy.solvers.ode.odesimp` calls 

:py:meth:`~sympy.simplify.simplify.logcombine` on them (it also helps to write 

the arbitrary constant as ``log(C1)`` instead of ``C1`` in this case). Also 

consider common ways that you can rearrange your solution to have 

:py:meth:`~sympy.solvers.ode.constantsimp` take better advantage of it. It is 

better to put simplification in :py:meth:`~sympy.solvers.ode.odesimp` than in 

your method, because it can then be turned off with the simplify flag in 

:py:meth:`~sympy.solvers.ode.dsolve`. If you have any extraneous 

simplification in your function, be sure to only run it using ``if 

match.get('simplify', True):``, especially if it can be slow or if it can 

reduce the domain of the solution. 

 

Finally, as with every contribution to SymPy, your method will need to be 

tested. Add a test for each method in ``test_ode.py``. Follow the 

conventions there, i.e., test the solver using ``dsolve(eq, f(x), 

hint=your_hint)``, and also test the solution using 

:py:meth:`~sympy.solvers.ode.checkodesol` (you can put these in a separate 

tests and skip/XFAIL if it runs too slow/doesn't work). Be sure to call your 

hint specifically in :py:meth:`~sympy.solvers.ode.dsolve`, that way the test 

won't be broken simply by the introduction of another matching hint. If your 

method works for higher order (>1) ODEs, you will need to run ``sol = 

constant_renumber(sol, 'C', 1, order)`` for each solution, where ``order`` is 

the order of the ODE. This is because ``constant_renumber`` renumbers the 

arbitrary constants by printing order, which is platform dependent. Try to 

test every corner case of your solver, including a range of orders if it is a 

`n`\th order solver, but if your solver is slow, such as if it involves hard 

integration, try to keep the test run time down. 

 

Feel free to refactor existing hints to avoid duplicating code or creating 

inconsistencies. If you can show that your method exactly duplicates an 

existing method, including in the simplicity and speed of obtaining the 

solutions, then you can remove the old, less general method. The existing 

code is tested extensively in ``test_ode.py``, so if anything is broken, one 

of those tests will surely fail. 

 

""" 

from __future__ import print_function, division 

 

from collections import defaultdict 

from itertools import islice 

 

from sympy.core import Add, S, Mul, Pow, oo 

from sympy.core.compatibility import ordered, iterable, is_sequence, range 

from sympy.core.containers import Tuple 

from sympy.core.exprtools import factor_terms 

from sympy.core.expr import AtomicExpr, Expr 

from sympy.core.function import (Function, Derivative, AppliedUndef, diff, 

expand, expand_mul, Subs, _mexpand) 

from sympy.core.multidimensional import vectorize 

from sympy.core.numbers import NaN, zoo, I, Number 

from sympy.core.relational import Equality, Eq 

from sympy.core.symbol import Symbol, Wild, Dummy, symbols 

from sympy.core.sympify import sympify 

 

from sympy.logic.boolalg import BooleanAtom, And, Or, Not 

from sympy.functions import cos, exp, im, log, re, sin, tan, sqrt, \ 

atan2, conjugate, Piecewise 

from sympy.functions.combinatorial.factorials import factorial 

from sympy.integrals.integrals import Integral, integrate 

from sympy.matrices import wronskian, Matrix, eye, zeros 

from sympy.polys import (Poly, RootOf, rootof, terms_gcd, 

PolynomialError, lcm) 

from sympy.polys.polyroots import roots_quartic 

from sympy.polys.polytools import cancel, degree, div 

from sympy.series import Order 

from sympy.series.series import series 

from sympy.simplify import collect, logcombine, powsimp, separatevars, \ 

simplify, trigsimp, denom, posify, cse 

from sympy.simplify.powsimp import powdenest 

from sympy.simplify.radsimp import collect_const 

from sympy.solvers import solve 

from sympy.solvers.pde import pdsolve 

 

from sympy.utilities import numbered_symbols, default_sort_key, sift 

from sympy.solvers.deutils import _preprocess, ode_order, _desolve 

 

#: This is a list of hints in the order that they should be preferred by 

#: :py:meth:`~sympy.solvers.ode.classify_ode`. In general, hints earlier in the 

#: list should produce simpler solutions than those later in the list (for 

#: ODEs that fit both). For now, the order of this list is based on empirical 

#: observations by the developers of SymPy. 

#: 

#: The hint used by :py:meth:`~sympy.solvers.ode.dsolve` for a specific ODE 

#: can be overridden (see the docstring). 

#: 

#: In general, ``_Integral`` hints are grouped at the end of the list, unless 

#: there is a method that returns an unevaluable integral most of the time 

#: (which go near the end of the list anyway). ``default``, ``all``, 

#: ``best``, and ``all_Integral`` meta-hints should not be included in this 

#: list, but ``_best`` and ``_Integral`` hints should be included. 

allhints = ( 

"separable", 

"1st_exact", 

"1st_linear", 

"Bernoulli", 

"Riccati_special_minus2", 

"1st_homogeneous_coeff_best", 

"1st_homogeneous_coeff_subs_indep_div_dep", 

"1st_homogeneous_coeff_subs_dep_div_indep", 

"almost_linear", 

"linear_coefficients", 

"separable_reduced", 

"1st_power_series", 

"lie_group", 

"nth_linear_constant_coeff_homogeneous", 

"nth_linear_euler_eq_homogeneous", 

"nth_linear_constant_coeff_undetermined_coefficients", 

"nth_linear_euler_eq_nonhomogeneous_undetermined_coefficients", 

"nth_linear_constant_coeff_variation_of_parameters", 

"nth_linear_euler_eq_nonhomogeneous_variation_of_parameters", 

"Liouville", 

"2nd_power_series_ordinary", 

"2nd_power_series_regular", 

"separable_Integral", 

"1st_exact_Integral", 

"1st_linear_Integral", 

"Bernoulli_Integral", 

"1st_homogeneous_coeff_subs_indep_div_dep_Integral", 

"1st_homogeneous_coeff_subs_dep_div_indep_Integral", 

"almost_linear_Integral", 

"linear_coefficients_Integral", 

"separable_reduced_Integral", 

"nth_linear_constant_coeff_variation_of_parameters_Integral", 

"nth_linear_euler_eq_nonhomogeneous_variation_of_parameters_Integral", 

"Liouville_Integral", 

) 

 

lie_heuristics = ( 

"abaco1_simple", 

"abaco1_product", 

"abaco2_similar", 

"abaco2_unique_unknown", 

"abaco2_unique_general", 

"linear", 

"function_sum", 

"bivariate", 

"chi" 

) 

 

 

def sub_func_doit(eq, func, new): 

r""" 

When replacing the func with something else, we usually want the 

derivative evaluated, so this function helps in making that happen. 

 

To keep subs from having to look through all derivatives, we mask them off 

with dummy variables, do the func sub, and then replace masked-off 

derivatives with their doit values. 

 

Examples 

======== 

 

>>> from sympy import Derivative, symbols, Function 

>>> from sympy.solvers.ode import sub_func_doit 

>>> x, z = symbols('x, z') 

>>> y = Function('y') 

 

>>> sub_func_doit(3*Derivative(y(x), x) - 1, y(x), x) 

2 

 

>>> sub_func_doit(x*Derivative(y(x), x) - y(x)**2 + y(x), y(x), 

... 1/(x*(z + 1/x))) 

x*(-1/(x**2*(z + 1/x)) + 1/(x**3*(z + 1/x)**2)) + 1/(x*(z + 1/x)) 

...- 1/(x**2*(z + 1/x)**2) 

""" 

reps = {} 

repu = {} 

for d in eq.atoms(Derivative): 

u = Dummy('u') 

repu[u] = d.subs(func, new).doit() 

reps[d] = u 

 

# Make sure that expressions such as ``Derivative(f(x), (x, 2))`` get 

# replaced before ``Derivative(f(x), x)``: 

reps = sorted(reps.items(), key=lambda x: -x[0].derivative_count) 

return eq.subs(reps).subs(func, new).subs(repu) 

 

 

def get_numbered_constants(eq, num=1, start=1, prefix='C'): 

""" 

Returns a list of constants that do not occur 

in eq already. 

""" 

 

if isinstance(eq, Expr): 

eq = [eq] 

elif not iterable(eq): 

raise ValueError("Expected Expr or iterable but got %s" % eq) 

 

atom_set = set().union(*[i.free_symbols for i in eq]) 

ncs = numbered_symbols(start=start, prefix=prefix, exclude=atom_set) 

Cs = [next(ncs) for i in range(num)] 

return (Cs[0] if num == 1 else tuple(Cs)) 

 

 

def dsolve(eq, func=None, hint="default", simplify=True, 

ics= None, xi=None, eta=None, x0=0, n=6, **kwargs): 

r""" 

Solves any (supported) kind of ordinary differential equation and 

system of ordinary differential equations. 

 

For single ordinary differential equation 

========================================= 

 

It is classified under this when number of equation in ``eq`` is one. 

**Usage** 

 

``dsolve(eq, f(x), hint)`` -> Solve ordinary differential equation 

``eq`` for function ``f(x)``, using method ``hint``. 

 

**Details** 

 

``eq`` can be any supported ordinary differential equation (see the 

:py:mod:`~sympy.solvers.ode` docstring for supported methods). 

This can either be an :py:class:`~sympy.core.relational.Equality`, 

or an expression, which is assumed to be equal to ``0``. 

 

``f(x)`` is a function of one variable whose derivatives in that 

variable make up the ordinary differential equation ``eq``. In 

many cases it is not necessary to provide this; it will be 

autodetected (and an error raised if it couldn't be detected). 

 

``hint`` is the solving method that you want dsolve to use. Use 

``classify_ode(eq, f(x))`` to get all of the possible hints for an 

ODE. The default hint, ``default``, will use whatever hint is 

returned first by :py:meth:`~sympy.solvers.ode.classify_ode`. See 

Hints below for more options that you can use for hint. 

 

``simplify`` enables simplification by 

:py:meth:`~sympy.solvers.ode.odesimp`. See its docstring for more 

information. Turn this off, for example, to disable solving of 

solutions for ``func`` or simplification of arbitrary constants. 

It will still integrate with this hint. Note that the solution may 

contain more arbitrary constants than the order of the ODE with 

this option enabled. 

 

``xi`` and ``eta`` are the infinitesimal functions of an ordinary 

differential equation. They are the infinitesimals of the Lie group 

of point transformations for which the differential equation is 

invariant. The user can specify values for the infinitesimals. If 

nothing is specified, ``xi`` and ``eta`` are calculated using 

:py:meth:`~sympy.solvers.ode.infinitesimals` with the help of various 

heuristics. 

 

``ics`` is the set of initial/boundary conditions for the differential equation. 

It should be given in the form of ``{f(x0): x1, f(x).diff(x).subs(x, x2): 

x3}`` and so on. For power series solutions, if no initial 

conditions are specified ``f(0)`` is assumed to be ``C0`` and the power 

series solution is calculated about 0. 

 

``x0`` is the point about which the power series solution of a differential 

equation is to be evaluated. 

 

``n`` gives the exponent of the dependent variable up to which the power series 

solution of a differential equation is to be evaluated. 

 

**Hints** 

 

Aside from the various solving methods, there are also some meta-hints 

that you can pass to :py:meth:`~sympy.solvers.ode.dsolve`: 

 

``default``: 

This uses whatever hint is returned first by 

:py:meth:`~sympy.solvers.ode.classify_ode`. This is the 

default argument to :py:meth:`~sympy.solvers.ode.dsolve`. 

 

``all``: 

To make :py:meth:`~sympy.solvers.ode.dsolve` apply all 

relevant classification hints, use ``dsolve(ODE, func, 

hint="all")``. This will return a dictionary of 

``hint:solution`` terms. If a hint causes dsolve to raise the 

``NotImplementedError``, value of that hint's key will be the 

exception object raised. The dictionary will also include 

some special keys: 

 

- ``order``: The order of the ODE. See also 

:py:meth:`~sympy.solvers.deutils.ode_order` in 

``deutils.py``. 

- ``best``: The simplest hint; what would be returned by 

``best`` below. 

- ``best_hint``: The hint that would produce the solution 

given by ``best``. If more than one hint produces the best 

solution, the first one in the tuple returned by 

:py:meth:`~sympy.solvers.ode.classify_ode` is chosen. 

- ``default``: The solution that would be returned by default. 

This is the one produced by the hint that appears first in 

the tuple returned by 

:py:meth:`~sympy.solvers.ode.classify_ode`. 

 

``all_Integral``: 

This is the same as ``all``, except if a hint also has a 

corresponding ``_Integral`` hint, it only returns the 

``_Integral`` hint. This is useful if ``all`` causes 

:py:meth:`~sympy.solvers.ode.dsolve` to hang because of a 

difficult or impossible integral. This meta-hint will also be 

much faster than ``all``, because 

:py:meth:`~sympy.core.expr.Expr.integrate` is an expensive 

routine. 

 

``best``: 

To have :py:meth:`~sympy.solvers.ode.dsolve` try all methods 

and return the simplest one. This takes into account whether 

the solution is solvable in the function, whether it contains 

any Integral classes (i.e. unevaluatable integrals), and 

which one is the shortest in size. 

 

See also the :py:meth:`~sympy.solvers.ode.classify_ode` docstring for 

more info on hints, and the :py:mod:`~sympy.solvers.ode` docstring for 

a list of all supported hints. 

 

**Tips** 

 

- You can declare the derivative of an unknown function this way: 

 

>>> from sympy import Function, Derivative 

>>> from sympy.abc import x # x is the independent variable 

>>> f = Function("f")(x) # f is a function of x 

>>> # f_ will be the derivative of f with respect to x 

>>> f_ = Derivative(f, x) 

 

- See ``test_ode.py`` for many tests, which serves also as a set of 

examples for how to use :py:meth:`~sympy.solvers.ode.dsolve`. 

- :py:meth:`~sympy.solvers.ode.dsolve` always returns an 

:py:class:`~sympy.core.relational.Equality` class (except for the 

case when the hint is ``all`` or ``all_Integral``). If possible, it 

solves the solution explicitly for the function being solved for. 

Otherwise, it returns an implicit solution. 

- Arbitrary constants are symbols named ``C1``, ``C2``, and so on. 

- Because all solutions should be mathematically equivalent, some 

hints may return the exact same result for an ODE. Often, though, 

two different hints will return the same solution formatted 

differently. The two should be equivalent. Also note that sometimes 

the values of the arbitrary constants in two different solutions may 

not be the same, because one constant may have "absorbed" other 

constants into it. 

- Do ``help(ode.ode_<hintname>)`` to get help more information on a 

specific hint, where ``<hintname>`` is the name of a hint without 

``_Integral``. 

 

For system of ordinary differential equations 

============================================= 

 

**Usage** 

``dsolve(eq, func)`` -> Solve a system of ordinary differential 

equations ``eq`` for ``func`` being list of functions including 

`x(t)`, `y(t)`, `z(t)` where number of functions in the list depends 

upon the number of equations provided in ``eq``. 

 

**Details** 

 

``eq`` can be any supported system of ordinary differential equations 

This can either be an :py:class:`~sympy.core.relational.Equality`, 

or an expression, which is assumed to be equal to ``0``. 

 

``func`` holds ``x(t)`` and ``y(t)`` being functions of one variable which 

together with some of their derivatives make up the system of ordinary 

differential equation ``eq``. It is not necessary to provide this; it 

will be autodetected (and an error raised if it couldn't be detected). 

 

**Hints** 

 

The hints are formed by parameters returned by classify_sysode, combining 

them give hints name used later for forming method name. 

 

Examples 

======== 

 

>>> from sympy import Function, dsolve, Eq, Derivative, sin, cos, symbols 

>>> from sympy.abc import x 

>>> f = Function('f') 

>>> dsolve(Derivative(f(x), x, x) + 9*f(x), f(x)) 

Eq(f(x), C1*sin(3*x) + C2*cos(3*x)) 

 

>>> eq = sin(x)*cos(f(x)) + cos(x)*sin(f(x))*f(x).diff(x) 

>>> dsolve(eq, hint='1st_exact') 

[Eq(f(x), -acos(C1/cos(x)) + 2*pi), Eq(f(x), acos(C1/cos(x)))] 

>>> dsolve(eq, hint='almost_linear') 

[Eq(f(x), -acos(C1/cos(x)) + 2*pi), Eq(f(x), acos(C1/cos(x)))] 

>>> t = symbols('t') 

>>> x, y = symbols('x, y', function=True) 

>>> eq = (Eq(Derivative(x(t),t), 12*t*x(t) + 8*y(t)), Eq(Derivative(y(t),t), 21*x(t) + 7*t*y(t))) 

>>> dsolve(eq) 

[Eq(x(t), C1*x0 + C2*x0*Integral(8*exp(Integral(7*t, t))*exp(Integral(12*t, t))/x0**2, t)), 

Eq(y(t), C1*y0 + C2(y0*Integral(8*exp(Integral(7*t, t))*exp(Integral(12*t, t))/x0**2, t) + 

exp(Integral(7*t, t))*exp(Integral(12*t, t))/x0))] 

>>> eq = (Eq(Derivative(x(t),t),x(t)*y(t)*sin(t)), Eq(Derivative(y(t),t),y(t)**2*sin(t))) 

>>> dsolve(eq) 

{Eq(x(t), -exp(C1)/(C2*exp(C1) - cos(t))), Eq(y(t), -1/(C1 - cos(t)))} 

""" 

if iterable(eq): 

match = classify_sysode(eq, func) 

eq = match['eq'] 

order = match['order'] 

func = match['func'] 

t = list(list(eq[0].atoms(Derivative))[0].atoms(Symbol))[0] 

 

# keep highest order term coefficient positive 

for i in range(len(eq)): 

for func_ in func: 

if isinstance(func_, list): 

pass 

else: 

if eq[i].coeff(diff(func[i],t,ode_order(eq[i], func[i]))).is_negative: 

eq[i] = -eq[i] 

match['eq'] = eq 

if len(set(order.values()))!=1: 

raise ValueError("It solves only those systems of equations whose orders are equal") 

match['order'] = list(order.values())[0] 

def recur_len(l): 

return sum(recur_len(item) if isinstance(item,list) else 1 for item in l) 

if recur_len(func) != len(eq): 

raise ValueError("dsolve() and classify_sysode() work with " 

"number of functions being equal to number of equations") 

if match['type_of_equation'] is None: 

raise NotImplementedError 

else: 

if match['is_linear'] == True: 

if match['no_of_equation'] > 3: 

solvefunc = globals()['sysode_linear_neq_order%(order)s' % match] 

else: 

solvefunc = globals()['sysode_linear_%(no_of_equation)seq_order%(order)s' % match] 

else: 

solvefunc = globals()['sysode_nonlinear_%(no_of_equation)seq_order%(order)s' % match] 

sols = solvefunc(match) 

if ics: 

constants = Tuple(*sols).free_symbols - Tuple(*eq).free_symbols 

solved_constants = solve_ics(sols, func, constants, ics) 

return [sol.subs(solved_constants) for sol in sols] 

return sols 

else: 

given_hint = hint # hint given by the user 

 

# See the docstring of _desolve for more details. 

hints = _desolve(eq, func=func, 

hint=hint, simplify=True, xi=xi, eta=eta, type='ode', ics=ics, 

x0=x0, n=n, **kwargs) 

 

eq = hints.pop('eq', eq) 

all_ = hints.pop('all', False) 

if all_: 

retdict = {} 

failed_hints = {} 

gethints = classify_ode(eq, dict=True) 

orderedhints = gethints['ordered_hints'] 

for hint in hints: 

try: 

rv = _helper_simplify(eq, hint, hints[hint], simplify) 

except NotImplementedError as detail: 

failed_hints[hint] = detail 

else: 

retdict[hint] = rv 

func = hints[hint]['func'] 

 

retdict['best'] = min(list(retdict.values()), key=lambda x: 

ode_sol_simplicity(x, func, trysolving=not simplify)) 

if given_hint == 'best': 

return retdict['best'] 

for i in orderedhints: 

if retdict['best'] == retdict.get(i, None): 

retdict['best_hint'] = i 

break 

retdict['default'] = gethints['default'] 

retdict['order'] = gethints['order'] 

retdict.update(failed_hints) 

return retdict 

 

else: 

# The key 'hint' stores the hint needed to be solved for. 

hint = hints['hint'] 

return _helper_simplify(eq, hint, hints, simplify, ics=ics) 

 

def _helper_simplify(eq, hint, match, simplify=True, ics=None, **kwargs): 

r""" 

Helper function of dsolve that calls the respective 

:py:mod:`~sympy.solvers.ode` functions to solve for the ordinary 

differential equations. This minimizes the computation in calling 

:py:meth:`~sympy.solvers.deutils._desolve` multiple times. 

""" 

r = match 

if hint.endswith('_Integral'): 

solvefunc = globals()['ode_' + hint[:-len('_Integral')]] 

else: 

solvefunc = globals()['ode_' + hint] 

func = r['func'] 

order = r['order'] 

match = r[hint] 

 

free = eq.free_symbols 

cons = lambda s: s.free_symbols.difference(free) 

 

if simplify: 

# odesimp() will attempt to integrate, if necessary, apply constantsimp(), 

# attempt to solve for func, and apply any other hint specific 

# simplifications 

sols = solvefunc(eq, func, order, match) 

if isinstance(sols, Expr): 

rv = odesimp(sols, func, order, cons(sols), hint) 

else: 

rv = [odesimp(s, func, order, cons(s), hint) for s in sols] 

else: 

# We still want to integrate (you can disable it separately with the hint) 

match['simplify'] = False # Some hints can take advantage of this option 

rv = _handle_Integral(solvefunc(eq, func, order, match), 

func, order, hint) 

 

if ics and not 'power_series' in hint: 

if isinstance(rv, Expr): 

solved_constants = solve_ics([rv], [r['func']], cons(rv), ics) 

rv = rv.subs(solved_constants) 

else: 

rv1 = [] 

for s in rv: 

solved_constants = solve_ics([s], [r['func']], cons(s), ics) 

rv1.append(s.subs(solved_constants)) 

rv = rv1 

return rv 

 

def solve_ics(sols, funcs, constants, ics): 

""" 

Solve for the constants given initial conditions 

 

``sols`` is a list of solutions. 

 

``funcs`` is a list of functions. 

 

``constants`` is a list of constants. 

 

``ics`` is the set of initial/boundary conditions for the differential 

equation. It should be given in the form of ``{f(x0): x1, 

f(x).diff(x).subs(x, x2): x3}`` and so on. 

 

Returns a dictionary mapping constants to values. 

``solution.subs(constants)`` will replace the constants in ``solution``. 

 

Example 

======= 

>>> # From dsolve(f(x).diff(x) - f(x), f(x)) 

>>> from sympy import symbols, Eq, exp, Function 

>>> from sympy.solvers.ode import solve_ics 

>>> f = Function('f') 

>>> x, C1 = symbols('x C1') 

>>> sols = [Eq(f(x), C1*exp(x))] 

>>> funcs = [f(x)] 

>>> constants = [C1] 

>>> ics = {f(0): 2} 

>>> solved_constants = solve_ics(sols, funcs, constants, ics) 

>>> solved_constants 

{C1: 2} 

>>> sols[0].subs(solved_constants) 

Eq(f(x), 2*exp(x)) 

 

""" 

# Assume ics are of the form f(x0): value or Subs(diff(f(x), x, n), (x, 

# x0)): value (currently checked by classify_ode). To solve, replace x 

# with x0, f(x0) with value, then solve for constants. For f^(n)(x0), 

# differentiate the solution n times, so that f^(n)(x) appears. 

x = funcs[0].args[0] 

diff_sols = [] 

subs_sols = [] 

diff_variables = set() 

for funcarg, value in ics.items(): 

if isinstance(funcarg, AppliedUndef): 

x0 = funcarg.args[0] 

matching_func = [f for f in funcs if f.func == funcarg.func][0] 

S = sols 

elif isinstance(funcarg, (Subs, Derivative)): 

if isinstance(funcarg, Subs): 

# Make sure it stays a subs. Otherwise subs below will produce 

# a different looking term. 

funcarg = funcarg.doit() 

if isinstance(funcarg, Subs): 

deriv = funcarg.expr 

x0 = funcarg.point[0] 

variables = funcarg.expr.variables 

matching_func = deriv 

elif isinstance(funcarg, Derivative): 

deriv = funcarg 

x0 = funcarg.variables[0] 

variables = (x,)*len(funcarg.variables) 

matching_func = deriv.subs(x0, x) 

if variables not in diff_variables: 

for sol in sols: 

if sol.has(deriv.expr.func): 

diff_sols.append(Eq(sol.lhs.diff(*variables), sol.rhs.diff(*variables))) 

diff_variables.add(variables) 

S = diff_sols 

else: 

raise NotImplementedError("Unrecognized initial condition") 

 

for sol in S: 

if sol.has(matching_func): 

sol2 = sol 

sol2 = sol2.subs(x, x0) 

sol2 = sol2.subs(funcarg, value) 

subs_sols.append(sol2) 

 

# TODO: Use solveset here 

try: 

solved_constants = solve(subs_sols, constants, dict=True) 

except NotImplementedError: 

solved_constants = [] 

 

# XXX: We can't differentiate between the solution not existing because of 

# invalid initial conditions, and not existing because solve is not smart 

# enough. If we could use solveset, this might be improvable, but for now, 

# we use NotImplementedError in this case. 

if not solved_constants: 

raise NotImplementedError("Couldn't solve for initial conditions") 

 

if solved_constants == True: 

raise ValueError("Initial conditions did not produce any solutions for constants. Perhaps they are degenerate.") 

 

if len(solved_constants) > 1: 

raise NotImplementedError("Initial conditions produced too many solutions for constants") 

 

if len(solved_constants[0]) != len(constants): 

raise ValueError("Initial conditions did not produce a solution for all constants. Perhaps they are under-specified.") 

 

return solved_constants[0] 

 

def classify_ode(eq, func=None, dict=False, ics=None, **kwargs): 

r""" 

Returns a tuple of possible :py:meth:`~sympy.solvers.ode.dsolve` 

classifications for an ODE. 

 

The tuple is ordered so that first item is the classification that 

:py:meth:`~sympy.solvers.ode.dsolve` uses to solve the ODE by default. In 

general, classifications at the near the beginning of the list will 

produce better solutions faster than those near the end, thought there are 

always exceptions. To make :py:meth:`~sympy.solvers.ode.dsolve` use a 

different classification, use ``dsolve(ODE, func, 

hint=<classification>)``. See also the 

:py:meth:`~sympy.solvers.ode.dsolve` docstring for different meta-hints 

you can use. 

 

If ``dict`` is true, :py:meth:`~sympy.solvers.ode.classify_ode` will 

return a dictionary of ``hint:match`` expression terms. This is intended 

for internal use by :py:meth:`~sympy.solvers.ode.dsolve`. Note that 

because dictionaries are ordered arbitrarily, this will most likely not be 

in the same order as the tuple. 

 

You can get help on different hints by executing 

``help(ode.ode_hintname)``, where ``hintname`` is the name of the hint 

without ``_Integral``. 

 

See :py:data:`~sympy.solvers.ode.allhints` or the 

:py:mod:`~sympy.solvers.ode` docstring for a list of all supported hints 

that can be returned from :py:meth:`~sympy.solvers.ode.classify_ode`. 

 

Notes 

===== 

 

These are remarks on hint names. 

 

``_Integral`` 

 

If a classification has ``_Integral`` at the end, it will return the 

expression with an unevaluated :py:class:`~sympy.integrals.Integral` 

class in it. Note that a hint may do this anyway if 

:py:meth:`~sympy.core.expr.Expr.integrate` cannot do the integral, 

though just using an ``_Integral`` will do so much faster. Indeed, an 

``_Integral`` hint will always be faster than its corresponding hint 

without ``_Integral`` because 

:py:meth:`~sympy.core.expr.Expr.integrate` is an expensive routine. 

If :py:meth:`~sympy.solvers.ode.dsolve` hangs, it is probably because 

:py:meth:`~sympy.core.expr.Expr.integrate` is hanging on a tough or 

impossible integral. Try using an ``_Integral`` hint or 

``all_Integral`` to get it return something. 

 

Note that some hints do not have ``_Integral`` counterparts. This is 

because :py:meth:`~sympy.solvers.ode.integrate` is not used in solving 

the ODE for those method. For example, `n`\th order linear homogeneous 

ODEs with constant coefficients do not require integration to solve, 

so there is no ``nth_linear_homogeneous_constant_coeff_Integrate`` 

hint. You can easily evaluate any unevaluated 

:py:class:`~sympy.integrals.Integral`\s in an expression by doing 

``expr.doit()``. 

 

Ordinals 

 

Some hints contain an ordinal such as ``1st_linear``. This is to help 

differentiate them from other hints, as well as from other methods 

that may not be implemented yet. If a hint has ``nth`` in it, such as 

the ``nth_linear`` hints, this means that the method used to applies 

to ODEs of any order. 

 

``indep`` and ``dep`` 

 

Some hints contain the words ``indep`` or ``dep``. These reference 

the independent variable and the dependent function, respectively. For 

example, if an ODE is in terms of `f(x)`, then ``indep`` will refer to 

`x` and ``dep`` will refer to `f`. 

 

``subs`` 

 

If a hints has the word ``subs`` in it, it means the the ODE is solved 

by substituting the expression given after the word ``subs`` for a 

single dummy variable. This is usually in terms of ``indep`` and 

``dep`` as above. The substituted expression will be written only in 

characters allowed for names of Python objects, meaning operators will 

be spelled out. For example, ``indep``/``dep`` will be written as 

``indep_div_dep``. 

 

``coeff`` 

 

The word ``coeff`` in a hint refers to the coefficients of something 

in the ODE, usually of the derivative terms. See the docstring for 

the individual methods for more info (``help(ode)``). This is 

contrast to ``coefficients``, as in ``undetermined_coefficients``, 

which refers to the common name of a method. 

 

``_best`` 

 

Methods that have more than one fundamental way to solve will have a 

hint for each sub-method and a ``_best`` meta-classification. This 

will evaluate all hints and return the best, using the same 

considerations as the normal ``best`` meta-hint. 

 

 

Examples 

======== 

 

>>> from sympy import Function, classify_ode, Eq 

>>> from sympy.abc import x 

>>> f = Function('f') 

>>> classify_ode(Eq(f(x).diff(x), 0), f(x)) 

('separable', '1st_linear', '1st_homogeneous_coeff_best', 

'1st_homogeneous_coeff_subs_indep_div_dep', 

'1st_homogeneous_coeff_subs_dep_div_indep', 

'1st_power_series', 'lie_group', 

'nth_linear_constant_coeff_homogeneous', 

'separable_Integral', '1st_linear_Integral', 

'1st_homogeneous_coeff_subs_indep_div_dep_Integral', 

'1st_homogeneous_coeff_subs_dep_div_indep_Integral') 

>>> classify_ode(f(x).diff(x, 2) + 3*f(x).diff(x) + 2*f(x) - 4) 

('nth_linear_constant_coeff_undetermined_coefficients', 

'nth_linear_constant_coeff_variation_of_parameters', 

'nth_linear_constant_coeff_variation_of_parameters_Integral') 

 

""" 

ics = sympify(ics) 

 

prep = kwargs.pop('prep', True) 

 

if func and len(func.args) != 1: 

raise ValueError("dsolve() and classify_ode() only " 

"work with functions of one variable, not %s" % func) 

if prep or func is None: 

eq, func_ = _preprocess(eq, func) 

if func is None: 

func = func_ 

x = func.args[0] 

f = func.func 

y = Dummy('y') 

xi = kwargs.get('xi') 

eta = kwargs.get('eta') 

terms = kwargs.get('n') 

 

if isinstance(eq, Equality): 

if eq.rhs != 0: 

return classify_ode(eq.lhs - eq.rhs, func, dict=dict, ics=ics, xi=xi, 

n=terms, eta=eta, prep=False) 

eq = eq.lhs 

order = ode_order(eq, f(x)) 

# hint:matchdict or hint:(tuple of matchdicts) 

# Also will contain "default":<default hint> and "order":order items. 

matching_hints = {"order": order} 

 

if not order: 

if dict: 

matching_hints["default"] = None 

return matching_hints 

else: 

return () 

 

df = f(x).diff(x) 

a = Wild('a', exclude=[f(x)]) 

b = Wild('b', exclude=[f(x)]) 

c = Wild('c', exclude=[f(x)]) 

d = Wild('d', exclude=[df, f(x).diff(x, 2)]) 

e = Wild('e', exclude=[df]) 

k = Wild('k', exclude=[df]) 

n = Wild('n', exclude=[x, f(x), df]) 

c1 = Wild('c1', exclude=[x]) 

a2 = Wild('a2', exclude=[x, f(x), df]) 

b2 = Wild('b2', exclude=[x, f(x), df]) 

c2 = Wild('c2', exclude=[x, f(x), df]) 

d2 = Wild('d2', exclude=[x, f(x), df]) 

a3 = Wild('a3', exclude=[f(x), df, f(x).diff(x, 2)]) 

b3 = Wild('b3', exclude=[f(x), df, f(x).diff(x, 2)]) 

c3 = Wild('c3', exclude=[f(x), df, f(x).diff(x, 2)]) 

r3 = {'xi': xi, 'eta': eta} # Used for the lie_group hint 

boundary = {} # Used to extract initial conditions 

C1 = Symbol("C1") 

eq = expand(eq) 

 

# Preprocessing to get the initial conditions out 

if ics is not None: 

for funcarg in ics: 

# Separating derivatives 

if isinstance(funcarg, (Subs, Derivative)): 

# f(x).diff(x).subs(x, 0) is a Subs, but f(x).diff(x).subs(x, 

# y) is a Derivative 

if isinstance(funcarg, Subs): 

deriv = funcarg.expr 

old = funcarg.variables[0] 

new = funcarg.point[0] 

elif isinstance(funcarg, Derivative): 

deriv = funcarg 

# No information on this. Just assume it was x 

old = x 

new = funcarg.variables[0] 

 

if (isinstance(deriv, Derivative) and isinstance(deriv.args[0], 

AppliedUndef) and deriv.args[0].func == f and 

len(deriv.args[0].args) == 1 and old == x and not 

new.has(x) and all(i == deriv.variables[0] for i in 

deriv.variables) and not ics[funcarg].has(f)): 

 

dorder = ode_order(deriv, x) 

temp = 'f' + str(dorder) 

boundary.update({temp: new, temp + 'val': ics[funcarg]}) 

else: 

raise ValueError("Enter valid boundary conditions for Derivatives") 

 

 

# Separating functions 

elif isinstance(funcarg, AppliedUndef): 

if (funcarg.func == f and len(funcarg.args) == 1 and 

not funcarg.args[0].has(x) and not ics[funcarg].has(f)): 

boundary.update({'f0': funcarg.args[0], 'f0val': ics[funcarg]}) 

else: 

raise ValueError("Enter valid boundary conditions for Function") 

 

else: 

raise ValueError("Enter boundary conditions of the form ics={f(point}: value, f(x).diff(x, order).subs(x, point): value}") 

 

# Precondition to try remove f(x) from highest order derivative 

reduced_eq = None 

if eq.is_Add: 

deriv_coef = eq.coeff(f(x).diff(x, order)) 

if deriv_coef not in (1, 0): 

r = deriv_coef.match(a*f(x)**c1) 

if r and r[c1]: 

den = f(x)**r[c1] 

reduced_eq = Add(*[arg/den for arg in eq.args]) 

if not reduced_eq: 

reduced_eq = eq 

 

if order == 1: 

 

## Linear case: a(x)*y'+b(x)*y+c(x) == 0 

if eq.is_Add: 

ind, dep = reduced_eq.as_independent(f) 

else: 

u = Dummy('u') 

ind, dep = (reduced_eq + u).as_independent(f) 

ind, dep = [tmp.subs(u, 0) for tmp in [ind, dep]] 

r = {a: dep.coeff(df), 

b: dep.coeff(f(x)), 

c: ind} 

# double check f[a] since the preconditioning may have failed 

if not r[a].has(f) and not r[b].has(f) and ( 

r[a]*df + r[b]*f(x) + r[c]).expand() - reduced_eq == 0: 

r['a'] = a 

r['b'] = b 

r['c'] = c 

matching_hints["1st_linear"] = r 

matching_hints["1st_linear_Integral"] = r 

 

## Bernoulli case: a(x)*y'+b(x)*y+c(x)*y**n == 0 

r = collect( 

reduced_eq, f(x), exact=True).match(a*df + b*f(x) + c*f(x)**n) 

if r and r[c] != 0 and r[n] != 1: # See issue 4676 

r['a'] = a 

r['b'] = b 

r['c'] = c 

r['n'] = n 

matching_hints["Bernoulli"] = r 

matching_hints["Bernoulli_Integral"] = r 

 

## Riccati special n == -2 case: a2*y'+b2*y**2+c2*y/x+d2/x**2 == 0 

r = collect(reduced_eq, 

f(x), exact=True).match(a2*df + b2*f(x)**2 + c2*f(x)/x + d2/x**2) 

if r and r[b2] != 0 and (r[c2] != 0 or r[d2] != 0): 

r['a2'] = a2 

r['b2'] = b2 

r['c2'] = c2 

r['d2'] = d2 

matching_hints["Riccati_special_minus2"] = r 

 

# NON-REDUCED FORM OF EQUATION matches 

r = collect(eq, df, exact=True).match(d + e * df) 

if r: 

r['d'] = d 

r['e'] = e 

r['y'] = y 

r[d] = r[d].subs(f(x), y) 

r[e] = r[e].subs(f(x), y) 

 

# FIRST ORDER POWER SERIES WHICH NEEDS INITIAL CONDITIONS 

# TODO: Hint first order series should match only if d/e is analytic. 

# For now, only d/e and (d/e).diff(arg) is checked for existence at 

# at a given point. 

# This is currently done internally in ode_1st_power_series. 

point = boundary.get('f0', 0) 

value = boundary.get('f0val', C1) 

check = cancel(r[d]/r[e]) 

check1 = check.subs({x: point, y: value}) 

if not check1.has(oo) and not check1.has(zoo) and \ 

not check1.has(NaN) and not check1.has(-oo): 

check2 = (check1.diff(x)).subs({x: point, y: value}) 

if not check2.has(oo) and not check2.has(zoo) and \ 

not check2.has(NaN) and not check2.has(-oo): 

rseries = r.copy() 

rseries.update({'terms': terms, 'f0': point, 'f0val': value}) 

matching_hints["1st_power_series"] = rseries 

 

r3.update(r) 

## Exact Differential Equation: P(x, y) + Q(x, y)*y' = 0 where 

# dP/dy == dQ/dx 

try: 

if r[d] != 0: 

numerator = simplify(r[d].diff(y) - r[e].diff(x)) 

# The following few conditions try to convert a non-exact 

# differential equation into an exact one. 

# References : Differential equations with applications 

# and historical notes - George E. Simmons 

 

if numerator: 

# If (dP/dy - dQ/dx) / Q = f(x) 

# then exp(integral(f(x))*equation becomes exact 

factor = simplify(numerator/r[e]) 

variables = factor.free_symbols 

if len(variables) == 1 and x == variables.pop(): 

factor = exp(Integral(factor).doit()) 

r[d] *= factor 

r[e] *= factor 

matching_hints["1st_exact"] = r 

matching_hints["1st_exact_Integral"] = r 

else: 

# If (dP/dy - dQ/dx) / -P = f(y) 

# then exp(integral(f(y))*equation becomes exact 

factor = simplify(-numerator/r[d]) 

variables = factor.free_symbols 

if len(variables) == 1 and y == variables.pop(): 

factor = exp(Integral(factor).doit()) 

r[d] *= factor 

r[e] *= factor 

matching_hints["1st_exact"] = r 

matching_hints["1st_exact_Integral"] = r 

else: 

matching_hints["1st_exact"] = r 

matching_hints["1st_exact_Integral"] = r 

 

except NotImplementedError: 

# Differentiating the coefficients might fail because of things 

# like f(2*x).diff(x). See issue 4624 and issue 4719. 

pass 

 

# Any first order ODE can be ideally solved by the Lie Group 

# method 

matching_hints["lie_group"] = r3 

 

# This match is used for several cases below; we now collect on 

# f(x) so the matching works. 

r = collect(reduced_eq, df, exact=True).match(d + e*df) 

if r: 

# Using r[d] and r[e] without any modification for hints 

# linear-coefficients and separable-reduced. 

num, den = r[d], r[e] # ODE = d/e + df 

r['d'] = d 

r['e'] = e 

r['y'] = y 

r[d] = num.subs(f(x), y) 

r[e] = den.subs(f(x), y) 

 

## Separable Case: y' == P(y)*Q(x) 

r[d] = separatevars(r[d]) 

r[e] = separatevars(r[e]) 

# m1[coeff]*m1[x]*m1[y] + m2[coeff]*m2[x]*m2[y]*y' 

m1 = separatevars(r[d], dict=True, symbols=(x, y)) 

m2 = separatevars(r[e], dict=True, symbols=(x, y)) 

if m1 and m2: 

r1 = {'m1': m1, 'm2': m2, 'y': y} 

matching_hints["separable"] = r1 

matching_hints["separable_Integral"] = r1 

 

## First order equation with homogeneous coefficients: 

# dy/dx == F(y/x) or dy/dx == F(x/y) 

ordera = homogeneous_order(r[d], x, y) 

if ordera is not None: 

orderb = homogeneous_order(r[e], x, y) 

if ordera == orderb: 

# u1=y/x and u2=x/y 

u1 = Dummy('u1') 

u2 = Dummy('u2') 

s = "1st_homogeneous_coeff_subs" 

s1 = s + "_dep_div_indep" 

s2 = s + "_indep_div_dep" 

if simplify((r[d] + u1*r[e]).subs({x: 1, y: u1})) != 0: 

matching_hints[s1] = r 

matching_hints[s1 + "_Integral"] = r 

if simplify((r[e] + u2*r[d]).subs({x: u2, y: 1})) != 0: 

matching_hints[s2] = r 

matching_hints[s2 + "_Integral"] = r 

if s1 in matching_hints and s2 in matching_hints: 

matching_hints["1st_homogeneous_coeff_best"] = r 

 

## Linear coefficients of the form 

# y'+ F((a*x + b*y + c)/(a'*x + b'y + c')) = 0 

# that can be reduced to homogeneous form. 

F = num/den 

params = _linear_coeff_match(F, func) 

if params: 

xarg, yarg = params 

u = Dummy('u') 

t = Dummy('t') 

# Dummy substitution for df and f(x). 

dummy_eq = reduced_eq.subs(((df, t), (f(x), u))) 

reps = ((x, x + xarg), (u, u + yarg), (t, df), (u, f(x))) 

dummy_eq = simplify(dummy_eq.subs(reps)) 

# get the re-cast values for e and d 

r2 = collect(expand(dummy_eq), [df, f(x)]).match(e*df + d) 

if r2: 

orderd = homogeneous_order(r2[d], x, f(x)) 

if orderd is not None: 

ordere = homogeneous_order(r2[e], x, f(x)) 

if orderd == ordere: 

# Match arguments are passed in such a way that it 

# is coherent with the already existing homogeneous 

# functions. 

r2[d] = r2[d].subs(f(x), y) 

r2[e] = r2[e].subs(f(x), y) 

r2.update({'xarg': xarg, 'yarg': yarg, 

'd': d, 'e': e, 'y': y}) 

matching_hints["linear_coefficients"] = r2 

matching_hints["linear_coefficients_Integral"] = r2 

 

## Equation of the form y' + (y/x)*H(x^n*y) = 0 

# that can be reduced to separable form 

 

factor = simplify(x/f(x)*num/den) 

 

# Try representing factor in terms of x^n*y 

# where n is lowest power of x in factor; 

# first remove terms like sqrt(2)*3 from factor.atoms(Mul) 

u = None 

for mul in ordered(factor.atoms(Mul)): 

if mul.has(x): 

_, u = mul.as_independent(x, f(x)) 

break 

if u and u.has(f(x)): 

h = x**(degree(Poly(u.subs(f(x), y), gen=x)))*f(x) 

p = Wild('p') 

if (u/h == 1) or ((u/h).simplify().match(x**p)): 

t = Dummy('t') 

r2 = {'t': t} 

xpart, ypart = u.as_independent(f(x)) 

test = factor.subs(((u, t), (1/u, 1/t))) 

free = test.free_symbols 

if len(free) == 1 and free.pop() == t: 

r2.update({'power': xpart.as_base_exp()[1], 'u': test}) 

matching_hints["separable_reduced"] = r2 

matching_hints["separable_reduced_Integral"] = r2 

 

## Almost-linear equation of the form f(x)*g(y)*y' + k(x)*l(y) + m(x) = 0 

r = collect(eq, [df, f(x)]).match(e*df + d) 

if r: 

r2 = r.copy() 

r2[c] = S.Zero 

if r2[d].is_Add: 

# Separate the terms having f(x) to r[d] and 

# remaining to r[c] 

no_f, r2[d] = r2[d].as_independent(f(x)) 

r2[c] += no_f 

factor = simplify(r2[d].diff(f(x))/r[e]) 

if factor and not factor.has(f(x)): 

r2[d] = factor_terms(r2[d]) 

u = r2[d].as_independent(f(x), as_Add=False)[1] 

r2.update({'a': e, 'b': d, 'c': c, 'u': u}) 

r2[d] /= u 

r2[e] /= u.diff(f(x)) 

matching_hints["almost_linear"] = r2 

matching_hints["almost_linear_Integral"] = r2 

 

 

elif order == 2: 

# Liouville ODE in the form 

# f(x).diff(x, 2) + g(f(x))*(f(x).diff(x))**2 + h(x)*f(x).diff(x) 

# See Goldstein and Braun, "Advanced Methods for the Solution of 

# Differential Equations", pg. 98 

 

s = d*f(x).diff(x, 2) + e*df**2 + k*df 

r = reduced_eq.match(s) 

if r and r[d] != 0: 

y = Dummy('y') 

g = simplify(r[e]/r[d]).subs(f(x), y) 

h = simplify(r[k]/r[d]) 

if h.has(f(x)) or g.has(x): 

pass 

else: 

r = {'g': g, 'h': h, 'y': y} 

matching_hints["Liouville"] = r 

matching_hints["Liouville_Integral"] = r 

 

# Homogeneous second order differential equation of the form 

# a3*f(x).diff(x, 2) + b3*f(x).diff(x) + c3, where 

# for simplicity, a3, b3 and c3 are assumed to be polynomials. 

# It has a definite power series solution at point x0 if, b3/a3 and c3/a3 

# are analytic at x0. 

deq = a3*(f(x).diff(x, 2)) + b3*df + c3*f(x) 

r = collect(reduced_eq, 

[f(x).diff(x, 2), f(x).diff(x), f(x)]).match(deq) 

ordinary = False 

if r and r[a3] != 0: 

if all([r[key].is_polynomial() for key in r]): 

p = cancel(r[b3]/r[a3]) # Used below 

q = cancel(r[c3]/r[a3]) # Used below 

point = kwargs.get('x0', 0) 

check = p.subs(x, point) 

if not check.has(oo) and not check.has(NaN) and \ 

not check.has(zoo) and not check.has(-oo): 

check = q.subs(x, point) 

if not check.has(oo) and not check.has(NaN) and \ 

not check.has(zoo) and not check.has(-oo): 

ordinary = True 

r.update({'a3': a3, 'b3': b3, 'c3': c3, 'x0': point, 'terms': terms}) 

matching_hints["2nd_power_series_ordinary"] = r 

 

# Checking if the differential equation has a regular singular point 

# at x0. It has a regular singular point at x0, if (b3/a3)*(x - x0) 

# and (c3/a3)*((x - x0)**2) are analytic at x0. 

if not ordinary: 

p = cancel((x - point)*p) 

check = p.subs(x, point) 

if not check.has(oo) and not check.has(NaN) and \ 

not check.has(zoo) and not check.has(-oo): 

q = cancel(((x - point)**2)*q) 

check = q.subs(x, point) 

if not check.has(oo) and not check.has(NaN) and \ 

not check.has(zoo) and not check.has(-oo): 

coeff_dict = {'p': p, 'q': q, 'x0': point, 'terms': terms} 

matching_hints["2nd_power_series_regular"] = coeff_dict 

 

 

if order > 0: 

# nth order linear ODE 

# a_n(x)y^(n) + ... + a_1(x)y' + a_0(x)y = F(x) = b 

 

r = _nth_linear_match(reduced_eq, func, order) 

 

# Constant coefficient case (a_i is constant for all i) 

if r and not any(r[i].has(x) for i in r if i >= 0): 

# Inhomogeneous case: F(x) is not identically 0 

if r[-1]: 

undetcoeff = _undetermined_coefficients_match(r[-1], x) 

s = "nth_linear_constant_coeff_variation_of_parameters" 

matching_hints[s] = r 

matching_hints[s + "_Integral"] = r 

if undetcoeff['test']: 

r['trialset'] = undetcoeff['trialset'] 

matching_hints[ 

"nth_linear_constant_coeff_undetermined_coefficients" 

] = r 

 

# Homogeneous case: F(x) is identically 0 

else: 

matching_hints["nth_linear_constant_coeff_homogeneous"] = r 

 

# nth order Euler equation a_n*x**n*y^(n) + ... + a_1*x*y' + a_0*y = F(x) 

#In case of Homogeneous euler equation F(x) = 0 

def _test_term(coeff, order): 

r""" 

Linear Euler ODEs have the form K*x**order*diff(y(x),x,order) = F(x), 

where K is independent of x and y(x), order>= 0. 

So we need to check that for each term, coeff == K*x**order from 

some K. We have a few cases, since coeff may have several 

different types. 

""" 

if order < 0: 

raise ValueError("order should be greater than 0") 

if coeff == 0: 

return True 

if order == 0: 

if x in coeff.free_symbols: 

return False 

return True 

if coeff.is_Mul: 

if coeff.has(f(x)): 

return False 

return x**order in coeff.args 

elif coeff.is_Pow: 

return coeff.as_base_exp() == (x, order) 

elif order == 1: 

return x == coeff 

return False 

if r and not any(not _test_term(r[i], i) for i in r if i >= 0): 

if not r[-1]: 

matching_hints["nth_linear_euler_eq_homogeneous"] = r 

else: 

matching_hints["nth_linear_euler_eq_nonhomogeneous_variation_of_parameters"] = r 

matching_hints["nth_linear_euler_eq_nonhomogeneous_variation_of_parameters_Integral"] = r 

e, re = posify(r[-1].subs(x, exp(x))) 

undetcoeff = _undetermined_coefficients_match(e.subs(re), x) 

if undetcoeff['test']: 

r['trialset'] = undetcoeff['trialset'] 

matching_hints["nth_linear_euler_eq_nonhomogeneous_undetermined_coefficients"] = r 

 

 

# Order keys based on allhints. 

retlist = [i for i in allhints if i in matching_hints] 

 

if dict: 

# Dictionaries are ordered arbitrarily, so make note of which 

# hint would come first for dsolve(). Use an ordered dict in Py 3. 

matching_hints["default"] = retlist[0] if retlist else None 

matching_hints["ordered_hints"] = tuple(retlist) 

return matching_hints 

else: 

return tuple(retlist) 

 

def classify_sysode(eq, funcs=None, **kwargs): 

r""" 

Returns a dictionary of parameter names and values that define the system 

of ordinary differential equations in ``eq``. 

The parameters are further used in 

:py:meth:`~sympy.solvers.ode.dsolve` for solving that system. 

 

The parameter names and values are: 

 

'is_linear' (boolean), which tells whether the given system is linear. 

Note that "linear" here refers to the operator: terms such as ``x*diff(x,t)`` are 

nonlinear, whereas terms like ``sin(t)*diff(x,t)`` are still linear operators. 

 

'func' (list) contains the :py:class:`~sympy.core.function.Function`s that 

appear with a derivative in the ODE, i.e. those that we are trying to solve 

the ODE for. 

 

'order' (dict) with the maximum derivative for each element of the 'func' 

parameter. 

 

'func_coeff' (dict) with the coefficient for each triple ``(equation number, 

function, order)```. The coefficients are those subexpressions that do not 

appear in 'func', and hence can be considered constant for purposes of ODE 

solving. 

 

'eq' (list) with the equations from ``eq``, sympified and transformed into 

expressions (we are solving for these expressions to be zero). 

 

'no_of_equations' (int) is the number of equations (same as ``len(eq)``). 

 

'type_of_equation' (string) is an internal classification of the type of 

ODE. 

 

References 

========== 

-http://eqworld.ipmnet.ru/en/solutions/sysode/sode-toc1.htm 

-A. D. Polyanin and A. V. Manzhirov, Handbook of Mathematics for Engineers and Scientists 

 

Examples 

======== 

 

>>> from sympy import Function, Eq, symbols, diff 

>>> from sympy.solvers.ode import classify_sysode 

>>> from sympy.abc import t 

>>> f, x, y = symbols('f, x, y', function=True) 

>>> k, l, m, n = symbols('k, l, m, n', Integer=True) 

>>> x1 = diff(x(t), t) ; y1 = diff(y(t), t) 

>>> x2 = diff(x(t), t, t) ; y2 = diff(y(t), t, t) 

>>> eq = (Eq(5*x1, 12*x(t) - 6*y(t)), Eq(2*y1, 11*x(t) + 3*y(t))) 

>>> classify_sysode(eq) 

{'eq': [-12*x(t) + 6*y(t) + 5*Derivative(x(t), t), -11*x(t) - 3*y(t) + 2*Derivative(y(t), t)], 

'func': [x(t), y(t)], 'func_coeff': {(0, x(t), 0): -12, (0, x(t), 1): 5, (0, y(t), 0): 6, 

(0, y(t), 1): 0, (1, x(t), 0): -11, (1, x(t), 1): 0, (1, y(t), 0): -3, (1, y(t), 1): 2}, 

'is_linear': True, 'no_of_equation': 2, 'order': {x(t): 1, y(t): 1}, 'type_of_equation': 'type1'} 

>>> eq = (Eq(diff(x(t),t), 5*t*x(t) + t**2*y(t)), Eq(diff(y(t),t), -t**2*x(t) + 5*t*y(t))) 

>>> classify_sysode(eq) 

{'eq': [-t**2*y(t) - 5*t*x(t) + Derivative(x(t), t), t**2*x(t) - 5*t*y(t) + Derivative(y(t), t)], 

'func': [x(t), y(t)], 'func_coeff': {(0, x(t), 0): -5*t, (0, x(t), 1): 1, (0, y(t), 0): -t**2, 

(0, y(t), 1): 0, (1, x(t), 0): t**2, (1, x(t), 1): 0, (1, y(t), 0): -5*t, (1, y(t), 1): 1}, 

'is_linear': True, 'no_of_equation': 2, 'order': {x(t): 1, y(t): 1}, 'type_of_equation': 'type4'} 

 

""" 

 

# Sympify equations and convert iterables of equations into 

# a list of equations 

def _sympify(eq): 

return list(map(sympify, eq if iterable(eq) else [eq])) 

 

eq, funcs = (_sympify(w) for w in [eq, funcs]) 

for i, fi in enumerate(eq): 

if isinstance(fi, Equality): 

eq[i] = fi.lhs - fi.rhs 

matching_hints = {"no_of_equation":i+1} 

matching_hints['eq'] = eq 

if i==0: 

raise ValueError("classify_sysode() works for systems of ODEs. " 

"For scalar ODEs, classify_ode should be used") 

t = list(list(eq[0].atoms(Derivative))[0].atoms(Symbol))[0] 

 

# find all the functions if not given 

order = dict() 

if funcs==[None]: 

funcs = [] 

for eqs in eq: 

derivs = eqs.atoms(Derivative) 

func = set().union(*[d.atoms(AppliedUndef) for d in derivs]) 

for func_ in func: 

funcs.append(func_) 

funcs = list(set(funcs)) 

if len(funcs) < len(eq): 

raise ValueError("Number of functions given is less than number of equations %s" % funcs) 

func_dict = dict() 

for func in funcs: 

if not order.get(func, False): 

max_order = 0 

for i, eqs_ in enumerate(eq): 

order_ = ode_order(eqs_,func) 

if max_order < order_: 

max_order = order_ 

eq_no = i 

if eq_no in func_dict: 

list_func = [] 

list_func.append(func_dict[eq_no]) 

list_func.append(func) 

func_dict[eq_no] = list_func 

else: 

func_dict[eq_no] = func 

order[func] = max_order 

funcs = [func_dict[i] for i in range(len(func_dict))] 

matching_hints['func'] = funcs 

for func in funcs: 

if isinstance(func, list): 

for func_elem in func: 

if len(func_elem.args) != 1: 

raise ValueError("dsolve() and classify_sysode() work with " 

"functions of one variable only, not %s" % func) 

else: 

if func and len(func.args) != 1: 

raise ValueError("dsolve() and classify_sysode() work with " 

"functions of one variable only, not %s" % func) 

 

# find the order of all equation in system of odes 

matching_hints["order"] = order 

 

# find coefficients of terms f(t), diff(f(t),t) and higher derivatives 

# and similarly for other functions g(t), diff(g(t),t) in all equations. 

# Here j denotes the equation number, funcs[l] denotes the function about 

# which we are talking about and k denotes the order of function funcs[l] 

# whose coefficient we are calculating. 

def linearity_check(eqs, j, func, is_linear_): 

for k in range(order[func] + 1): 

func_coef[j, func, k] = collect(eqs.expand(), [diff(func, t, k)]).coeff(diff(func, t, k)) 

if is_linear_ == True: 

if func_coef[j, func, k] == 0: 

if k == 0: 

coef = eqs.as_independent(func, as_Add=True)[1] 

for xr in range(1, ode_order(eqs,func) + 1): 

coef -= eqs.as_independent(diff(func, t, xr), as_Add=True)[1] 

if coef != 0: 

is_linear_ = False 

else: 

if eqs.as_independent(diff(func, t, k), as_Add=True)[1]: 

is_linear_ = False 

else: 

for func_ in funcs: 

if isinstance(func_, list): 

for elem_func_ in func_: 

dep = func_coef[j, func, k].as_independent(elem_func_, as_Add=True)[1] 

if dep != 0: 

is_linear_ = False 

else: 

dep = func_coef[j, func, k].as_independent(func_, as_Add=True)[1] 

if dep != 0: 

is_linear_ = False 

return is_linear_ 

 

func_coef = {} 

is_linear = True 

for j, eqs in enumerate(eq): 

for func in funcs: 

if isinstance(func, list): 

for func_elem in func: 

is_linear = linearity_check(eqs, j, func_elem, is_linear) 

else: 

is_linear = linearity_check(eqs, j, func, is_linear) 

matching_hints['func_coeff'] = func_coef 

matching_hints['is_linear'] = is_linear 

 

if len(set(order.values()))==1: 

order_eq = list(matching_hints['order'].values())[0] 

if matching_hints['is_linear'] == True: 

if matching_hints['no_of_equation'] == 2: 

if order_eq == 1: 

type_of_equation = check_linear_2eq_order1(eq, funcs, func_coef) 

elif order_eq == 2: 

type_of_equation = check_linear_2eq_order2(eq, funcs, func_coef) 

else: 

type_of_equation = None 

 

elif matching_hints['no_of_equation'] == 3: 

if order_eq == 1: 

type_of_equation = check_linear_3eq_order1(eq, funcs, func_coef) 

if type_of_equation==None: 

type_of_equation = check_linear_neq_order1(eq, funcs, func_coef) 

else: 

type_of_equation = None 

else: 

if order_eq == 1: 

type_of_equation = check_linear_neq_order1(eq, funcs, func_coef) 

else: 

type_of_equation = None 

else: 

if matching_hints['no_of_equation'] == 2: 

if order_eq == 1: 

type_of_equation = check_nonlinear_2eq_order1(eq, funcs, func_coef) 

else: 

type_of_equation = None 

elif matching_hints['no_of_equation'] == 3: 

if order_eq == 1: 

type_of_equation = check_nonlinear_3eq_order1(eq, funcs, func_coef) 

else: 

type_of_equation = None 

else: 

type_of_equation = None 

else: 

type_of_equation = None 

 

matching_hints['type_of_equation'] = type_of_equation 

 

return matching_hints 

 

 

def check_linear_2eq_order1(eq, func, func_coef): 

x = func[0].func 

y = func[1].func 

fc = func_coef 

t = list(list(eq[0].atoms(Derivative))[0].atoms(Symbol))[0] 

r = dict() 

# for equations Eq(a1*diff(x(t),t), b1*x(t) + c1*y(t) + d1) 

# and Eq(a2*diff(y(t),t), b2*x(t) + c2*y(t) + d2) 

r['a1'] = fc[0,x(t),1] ; r['a2'] = fc[1,y(t),1] 

r['b1'] = -fc[0,x(t),0]/fc[0,x(t),1] ; r['b2'] = -fc[1,x(t),0]/fc[1,y(t),1] 

r['c1'] = -fc[0,y(t),0]/fc[0,x(t),1] ; r['c2'] = -fc[1,y(t),0]/fc[1,y(t),1] 

forcing = [S(0),S(0)] 

for i in range(2): 

for j in Add.make_args(eq[i]): 

if not j.has(x(t), y(t)): 

forcing[i] += j 

if not (forcing[0].has(t) or forcing[1].has(t)): 

# We can handle homogeneous case and simple constant forcings 

r['d1'] = forcing[0] 

r['d2'] = forcing[1] 

else: 

# Issue #9244: nonhomogeneous linear systems are not supported 

return None 

 

# Conditions to check for type 6 whose equations are Eq(diff(x(t),t), f(t)*x(t) + g(t)*y(t)) and 

# Eq(diff(y(t),t), a*[f(t) + a*h(t)]x(t) + a*[g(t) - h(t)]*y(t)) 

p = 0 

q = 0 

p1 = cancel(r['b2']/(cancel(r['b2']/r['c2']).as_numer_denom()[0])) 

p2 = cancel(r['b1']/(cancel(r['b1']/r['c1']).as_numer_denom()[0])) 

for n, i in enumerate([p1, p2]): 

for j in Mul.make_args(collect_const(i)): 

if not j.has(t): 

q = j 

if q and n==0: 

if ((r['b2']/j - r['b1'])/(r['c1'] - r['c2']/j)) == j: 

p = 1 

elif q and n==1: 

if ((r['b1']/j - r['b2'])/(r['c2'] - r['c1']/j)) == j: 

p = 2 

# End of condition for type 6 

 

if r['d1']!=0 or r['d2']!=0: 

if not r['d1'].has(t) and not r['d2'].has(t): 

if all(not r[k].has(t) for k in 'a1 a2 b1 b2 c1 c2'.split()): 

# Equations for type 2 are Eq(a1*diff(x(t),t),b1*x(t)+c1*y(t)+d1) and Eq(a2*diff(y(t),t),b2*x(t)+c2*y(t)+d2) 

return "type2" 

else: 

return None 

else: 

if all(not r[k].has(t) for k in 'a1 a2 b1 b2 c1 c2'.split()): 

# Equations for type 1 are Eq(a1*diff(x(t),t),b1*x(t)+c1*y(t)) and Eq(a2*diff(y(t),t),b2*x(t)+c2*y(t)) 

return "type1" 

else: 

r['b1'] = r['b1']/r['a1'] ; r['b2'] = r['b2']/r['a2'] 

r['c1'] = r['c1']/r['a1'] ; r['c2'] = r['c2']/r['a2'] 

if (r['b1'] == r['c2']) and (r['c1'] == r['b2']): 

# Equation for type 3 are Eq(diff(x(t),t), f(t)*x(t) + g(t)*y(t)) and Eq(diff(y(t),t), g(t)*x(t) + f(t)*y(t)) 

return "type3" 

elif (r['b1'] == r['c2']) and (r['c1'] == -r['b2']) or (r['b1'] == -r['c2']) and (r['c1'] == r['b2']): 

# Equation for type 4 are Eq(diff(x(t),t), f(t)*x(t) + g(t)*y(t)) and Eq(diff(y(t),t), -g(t)*x(t) + f(t)*y(t)) 

return "type4" 

elif (not cancel(r['b2']/r['c1']).has(t) and not cancel((r['c2']-r['b1'])/r['c1']).has(t)) \ 

or (not cancel(r['b1']/r['c2']).has(t) and not cancel((r['c1']-r['b2'])/r['c2']).has(t)): 

# Equations for type 5 are Eq(diff(x(t),t), f(t)*x(t) + g(t)*y(t)) and Eq(diff(y(t),t), a*g(t)*x(t) + [f(t) + b*g(t)]*y(t) 

return "type5" 

elif p: 

return "type6" 

else: 

# Equations for type 7 are Eq(diff(x(t),t), f(t)*x(t) + g(t)*y(t)) and Eq(diff(y(t),t), h(t)*x(t) + p(t)*y(t)) 

return "type7" 

 

def check_linear_2eq_order2(eq, func, func_coef): 

x = func[0].func 

y = func[1].func 

fc = func_coef 

t = list(list(eq[0].atoms(Derivative))[0].atoms(Symbol))[0] 

r = dict() 

a = Wild('a', exclude=[1/t]) 

b = Wild('b', exclude=[1/t**2]) 

u = Wild('u', exclude=[t, t**2]) 

v = Wild('v', exclude=[t, t**2]) 

w = Wild('w', exclude=[t, t**2]) 

p = Wild('p', exclude=[t, t**2]) 

r['a1'] = fc[0,x(t),2] ; r['a2'] = fc[1,y(t),2] 

r['b1'] = fc[0,x(t),1] ; r['b2'] = fc[1,x(t),1] 

r['c1'] = fc[0,y(t),1] ; r['c2'] = fc[1,y(t),1] 

r['d1'] = fc[0,x(t),0] ; r['d2'] = fc[1,x(t),0] 

r['e1'] = fc[0,y(t),0] ; r['e2'] = fc[1,y(t),0] 

const = [S(0), S(0)] 

for i in range(2): 

for j in Add.make_args(eq[i]): 

if not (j.has(x(t)) or j.has(y(t))): 

const[i] += j 

r['f1'] = const[0] 

r['f2'] = const[1] 

if r['f1']!=0 or r['f2']!=0: 

if all(not r[k].has(t) for k in 'a1 a2 d1 d2 e1 e2 f1 f2'.split()) \ 

and r['b1']==r['c1']==r['b2']==r['c2']==0: 

return "type2" 

 

elif all(not r[k].has(t) for k in 'a1 a2 b1 b2 c1 c2 d1 d2 e1 e1'.split()): 

p = [S(0), S(0)] ; q = [S(0), S(0)] 

for n, e in enumerate([r['f1'], r['f2']]): 

if e.has(t): 

tpart = e.as_independent(t, Mul)[1] 

for i in Mul.make_args(tpart): 

if i.has(exp): 

b, e = i.as_base_exp() 

co = e.coeff(t) 

if co and not co.has(t) and co.has(I): 

p[n] = 1 

else: 

q[n] = 1 

else: 

q[n] = 1 

else: 

q[n] = 1 

 

if p[0]==1 and p[1]==1 and q[0]==0 and q[1]==0: 

return "type4" 

else: 

return None 

else: 

return None 

else: 

if r['b1']==r['b2']==r['c1']==r['c2']==0 and all(not r[k].has(t) \ 

for k in 'a1 a2 d1 d2 e1 e2'.split()): 

return "type1" 

 

elif r['b1']==r['e1']==r['c2']==r['d2']==0 and all(not r[k].has(t) \ 

for k in 'a1 a2 b2 c1 d1 e2'.split()) and r['c1'] == -r['b2'] and \ 

r['d1'] == r['e2']: 

return "type3" 

 

elif cancel(-r['b2']/r['d2'])==t and cancel(-r['c1']/r['e1'])==t and not \ 

(r['d2']/r['a2']).has(t) and not (r['e1']/r['a1']).has(t) and \ 

r['b1']==r['d1']==r['c2']==r['e2']==0: 

return "type5" 

 

elif ((r['a1']/r['d1']).expand()).match((p*(u*t**2+v*t+w)**2).expand()) and not \ 

(cancel(r['a1']*r['d2']/(r['a2']*r['d1']))).has(t) and not (r['d1']/r['e1']).has(t) and not \ 

(r['d2']/r['e2']).has(t) and r['b1'] == r['b2'] == r['c1'] == r['c2'] == 0: 

return "type10" 

 

elif not cancel(r['d1']/r['e1']).has(t) and not cancel(r['d2']/r['e2']).has(t) and not \ 

cancel(r['d1']*r['a2']/(r['d2']*r['a1'])).has(t) and r['b1']==r['b2']==r['c1']==r['c2']==0: 

return "type6" 

 

elif not cancel(r['b1']/r['c1']).has(t) and not cancel(r['b2']/r['c2']).has(t) and not \ 

cancel(r['b1']*r['a2']/(r['b2']*r['a1'])).has(t) and r['d1']==r['d2']==r['e1']==r['e2']==0: 

return "type7" 

 

elif cancel(-r['b2']/r['d2'])==t and cancel(-r['c1']/r['e1'])==t and not \ 

cancel(r['e1']*r['a2']/(r['d2']*r['a1'])).has(t) and r['e1'].has(t) \ 

and r['b1']==r['d1']==r['c2']==r['e2']==0: 

return "type8" 

 

elif (r['b1']/r['a1']).match(a/t) and (r['b2']/r['a2']).match(a/t) and not \ 

(r['b1']/r['c1']).has(t) and not (r['b2']/r['c2']).has(t) and \ 

(r['d1']/r['a1']).match(b/t**2) and (r['d2']/r['a2']).match(b/t**2) \ 

and not (r['d1']/r['e1']).has(t) and not (r['d2']/r['e2']).has(t): 

return "type9" 

 

elif -r['b1']/r['d1']==-r['c1']/r['e1']==-r['b2']/r['d2']==-r['c2']/r['e2']==t: 

return "type11" 

 

else: 

return None 

 

def check_linear_3eq_order1(eq, func, func_coef): 

x = func[0].func 

y = func[1].func 

z = func[2].func 

fc = func_coef 

t = list(list(eq[0].atoms(Derivative))[0].atoms(Symbol))[0] 

r = dict() 

r['a1'] = fc[0,x(t),1]; r['a2'] = fc[1,y(t),1]; r['a3'] = fc[2,z(t),1] 

r['b1'] = fc[0,x(t),0]; r['b2'] = fc[1,x(t),0]; r['b3'] = fc[2,x(t),0] 

r['c1'] = fc[0,y(t),0]; r['c2'] = fc[1,y(t),0]; r['c3'] = fc[2,y(t),0] 

r['d1'] = fc[0,z(t),0]; r['d2'] = fc[1,z(t),0]; r['d3'] = fc[2,z(t),0] 

forcing = [S(0), S(0), S(0)] 

for i in range(3): 

for j in Add.make_args(eq[i]): 

if not j.has(x(t), y(t), z(t)): 

forcing[i] += j 

if forcing[0].has(t) or forcing[1].has(t) or forcing[2].has(t): 

# We can handle homogeneous case and simple constant forcings. 

# Issue #9244: nonhomogeneous linear systems are not supported 

return None 

 

if all(not r[k].has(t) for k in 'a1 a2 a3 b1 b2 b3 c1 c2 c3 d1 d2 d3'.split()): 

if r['c1']==r['d1']==r['d2']==0: 

return 'type1' 

elif r['c1'] == -r['b2'] and r['d1'] == -r['b3'] and r['d2'] == -r['c3'] \ 

and r['b1'] == r['c2'] == r['d3'] == 0: 

return 'type2' 

elif r['b1'] == r['c2'] == r['d3'] == 0 and r['c1']/r['a1'] == -r['d1']/r['a1'] \ 

and r['d2']/r['a2'] == -r['b2']/r['a2'] and r['b3']/r['a3'] == -r['c3']/r['a3']: 

return 'type3' 

else: 

return None 

else: 

for k1 in 'c1 d1 b2 d2 b3 c3'.split(): 

if r[k1] == 0: 

continue 

else: 

if all(not cancel(r[k1]/r[k]).has(t) for k in 'd1 b2 d2 b3 c3'.split() if r[k]!=0) \ 

and all(not cancel(r[k1]/(r['b1'] - r[k])).has(t) for k in 'b1 c2 d3'.split() if r['b1']!=r[k]): 

return 'type4' 

else: 

break 

return None 

 

def check_linear_neq_order1(eq, func, func_coef): 

x = func[0].func 

y = func[1].func 

z = func[2].func 

fc = func_coef 

t = list(list(eq[0].atoms(Derivative))[0].atoms(Symbol))[0] 

r = dict() 

n = len(eq) 

for i in range(n): 

for j in range(n): 

if (fc[i,func[j],0]/fc[i,func[i],1]).has(t): 

return None 

if len(eq)==3: 

return 'type6' 

return 'type1' 

 

def check_nonlinear_2eq_order1(eq, func, func_coef): 

t = list(list(eq[0].atoms(Derivative))[0].atoms(Symbol))[0] 

f = Wild('f') 

g = Wild('g') 

u, v = symbols('u, v', cls=Dummy) 

def check_type(x, y): 

r1 = eq[0].match(t*diff(x(t),t) - x(t) + f) 

r2 = eq[1].match(t*diff(y(t),t) - y(t) + g) 

if not (r1 and r2): 

r1 = eq[0].match(diff(x(t),t) - x(t)/t + f/t) 

r2 = eq[1].match(diff(y(t),t) - y(t)/t + g/t) 

if not (r1 and r2): 

r1 = (-eq[0]).match(t*diff(x(t),t) - x(t) + f) 

r2 = (-eq[1]).match(t*diff(y(t),t) - y(t) + g) 

if not (r1 and r2): 

r1 = (-eq[0]).match(diff(x(t),t) - x(t)/t + f/t) 

r2 = (-eq[1]).match(diff(y(t),t) - y(t)/t + g/t) 

if r1 and r2 and not (r1[f].subs(diff(x(t),t),u).subs(diff(y(t),t),v).has(t) \ 

or r2[g].subs(diff(x(t),t),u).subs(diff(y(t),t),v).has(t)): 

return 'type5' 

else: 

return None 

for func_ in func: 

if isinstance(func_, list): 

x = func[0][0].func 

y = func[0][1].func 

eq_type = check_type(x, y) 

if not eq_type: 

eq_type = check_type(y, x) 

return eq_type 

x = func[0].func 

y = func[1].func 

fc = func_coef 

n = Wild('n', exclude=[x(t),y(t)]) 

f1 = Wild('f1', exclude=[v,t]) 

f2 = Wild('f2', exclude=[v,t]) 

g1 = Wild('g1', exclude=[u,t]) 

g2 = Wild('g2', exclude=[u,t]) 

for i in range(2): 

eqs = 0 

for terms in Add.make_args(eq[i]): 

eqs += terms/fc[i,func[i],1] 

eq[i] = eqs 

r = eq[0].match(diff(x(t),t) - x(t)**n*f) 

if r: 

g = (diff(y(t),t) - eq[1])/r[f] 

if r and not (g.has(x(t)) or g.subs(y(t),v).has(t) or r[f].subs(x(t),u).subs(y(t),v).has(t)): 

return 'type1' 

r = eq[0].match(diff(x(t),t) - exp(n*x(t))*f) 

if r: 

g = (diff(y(t),t) - eq[1])/r[f] 

if r and not (g.has(x(t)) or g.subs(y(t),v).has(t) or r[f].subs(x(t),u).subs(y(t),v).has(t)): 

return 'type2' 

g = Wild('g') 

r1 = eq[0].match(diff(x(t),t) - f) 

r2 = eq[1].match(diff(y(t),t) - g) 

if r1 and r2 and not (r1[f].subs(x(t),u).subs(y(t),v).has(t) or \ 

r2[g].subs(x(t),u).subs(y(t),v).has(t)): 

return 'type3' 

r1 = eq[0].match(diff(x(t),t) - f) 

r2 = eq[1].match(diff(y(t),t) - g) 

num, den = ( 

(r1[f].subs(x(t),u).subs(y(t),v))/ 

(r2[g].subs(x(t),u).subs(y(t),v))).as_numer_denom() 

R1 = num.match(f1*g1) 

R2 = den.match(f2*g2) 

phi = (r1[f].subs(x(t),u).subs(y(t),v))/num 

if R1 and R2: 

return 'type4' 

return None 

 

 

def check_nonlinear_2eq_order2(eq, func, func_coef): 

return None 

 

def check_nonlinear_3eq_order1(eq, func, func_coef): 

x = func[0].func 

y = func[1].func 

z = func[2].func 

fc = func_coef 

t = list(list(eq[0].atoms(Derivative))[0].atoms(Symbol))[0] 

u, v, w = symbols('u, v, w', cls=Dummy) 

a = Wild('a', exclude=[x(t), y(t), z(t), t]) 

b = Wild('b', exclude=[x(t), y(t), z(t), t]) 

c = Wild('c', exclude=[x(t), y(t), z(t), t]) 

f = Wild('f') 

F1 = Wild('F1') 

F2 = Wild('F2') 

F3 = Wild('F3') 

for i in range(3): 

eqs = 0 

for terms in Add.make_args(eq[i]): 

eqs += terms/fc[i,func[i],1] 

eq[i] = eqs 

r1 = eq[0].match(diff(x(t),t) - a*y(t)*z(t)) 

r2 = eq[1].match(diff(y(t),t) - b*z(t)*x(t)) 

r3 = eq[2].match(diff(z(t),t) - c*x(t)*y(t)) 

if r1 and r2 and r3: 

num1, den1 = r1[a].as_numer_denom() 

num2, den2 = r2[b].as_numer_denom() 

num3, den3 = r3[c].as_numer_denom() 

if solve([num1*u-den1*(v-w), num2*v-den2*(w-u), num3*w-den3*(u-v)],[u, v]): 

return 'type1' 

r = eq[0].match(diff(x(t),t) - y(t)*z(t)*f) 

if r: 

r1 = collect_const(r[f]).match(a*f) 

r2 = ((diff(y(t),t) - eq[1])/r1[f]).match(b*z(t)*x(t)) 

r3 = ((diff(z(t),t) - eq[2])/r1[f]).match(c*x(t)*y(t)) 

if r1 and r2 and r3: 

num1, den1 = r1[a].as_numer_denom() 

num2, den2 = r2[b].as_numer_denom() 

num3, den3 = r3[c].as_numer_denom() 

if solve([num1*u-den1*(v-w), num2*v-den2*(w-u), num3*w-den3*(u-v)],[u, v]): 

return 'type2' 

r = eq[0].match(diff(x(t),t) - (F2-F3)) 

if r: 

r1 = collect_const(r[F2]).match(c*F2) 

r1.update(collect_const(r[F3]).match(b*F3)) 

if r1: 

if eq[1].has(r1[F2]) and not eq[1].has(r1[F3]): 

r1[F2], r1[F3] = r1[F3], r1[F2] 

r1[c], r1[b] = -r1[b], -r1[c] 

r2 = eq[1].match(diff(y(t),t) - a*r1[F3] + r1[c]*F1) 

if r2: 

r3 = (eq[2] == diff(z(t),t) - r1[b]*r2[F1] + r2[a]*r1[F2]) 

if r1 and r2 and r3: 

return 'type3' 

r = eq[0].match(diff(x(t),t) - z(t)*F2 + y(t)*F3) 

if r: 

r1 = collect_const(r[F2]).match(c*F2) 

r1.update(collect_const(r[F3]).match(b*F3)) 

if r1: 

if eq[1].has(r1[F2]) and not eq[1].has(r1[F3]): 

r1[F2], r1[F3] = r1[F3], r1[F2] 

r1[c], r1[b] = -r1[b], -r1[c] 

r2 = (diff(y(t),t) - eq[1]).match(a*x(t)*r1[F3] - r1[c]*z(t)*F1) 

if r2: 

r3 = (diff(z(t),t) - eq[2] == r1[b]*y(t)*r2[F1] - r2[a]*x(t)*r1[F2]) 

if r1 and r2 and r3: 

return 'type4' 

r = (diff(x(t),t) - eq[0]).match(x(t)*(F2 - F3)) 

if r: 

r1 = collect_const(r[F2]).match(c*F2) 

r1.update(collect_const(r[F3]).match(b*F3)) 

if r1: 

if eq[1].has(r1[F2]) and not eq[1].has(r1[F3]): 

r1[F2], r1[F3] = r1[F3], r1[F2] 

r1[c], r1[b] = -r1[b], -r1[c] 

r2 = (diff(y(t),t) - eq[1]).match(y(t)*(a*r1[F3] - r1[c]*F1)) 

if r2: 

r3 = (diff(z(t),t) - eq[2] == z(t)*(r1[b]*r2[F1] - r2[a]*r1[F2])) 

if r1 and r2 and r3: 

return 'type5' 

return None 

 

def check_nonlinear_3eq_order2(eq, func, func_coef): 

return None 

 

 

def checksysodesol(eqs, sols, func=None): 

r""" 

Substitutes corresponding ``sols`` for each functions into each ``eqs`` and 

checks that the result of substitutions for each equation is ``0``. The 

equations and solutions passed can be any iterable. 

 

This only works when each ``sols`` have one function only, like `x(t)` or `y(t)`. 

For each function, ``sols`` can have a single solution or a list of solutions. 

In most cases it will not be necessary to explicitly identify the function, 

but if the function cannot be inferred from the original equation it 

can be supplied through the ``func`` argument. 

 

When a sequence of equations is passed, the same sequence is used to return 

the result for each equation with each function substituted with corresponding 

solutions. 

 

It tries the following method to find zero equivalence for each equation: 

 

Substitute the solutions for functions, like `x(t)` and `y(t)` into the 

original equations containing those functions. 

This function returns a tuple. The first item in the tuple is ``True`` if 

the substitution results for each equation is ``0``, and ``False`` otherwise. 

The second item in the tuple is what the substitution results in. Each element 

of the ``list`` should always be ``0`` corresponding to each equation if the 

first item is ``True``. Note that sometimes this function may return ``False``, 

but with an expression that is identically equal to ``0``, instead of returning 

``True``. This is because :py:meth:`~sympy.simplify.simplify.simplify` cannot 

reduce the expression to ``0``. If an expression returned by each function 

vanishes identically, then ``sols`` really is a solution to ``eqs``. 

 

If this function seems to hang, it is probably because of a difficult simplification. 

 

Examples 

======== 

 

>>> from sympy import Eq, diff, symbols, sin, cos, exp, sqrt, S 

>>> from sympy.solvers.ode import checksysodesol 

>>> C1, C2 = symbols('C1:3') 

>>> t = symbols('t') 

>>> x, y = symbols('x, y', function=True) 

>>> eq = (Eq(diff(x(t),t), x(t) + y(t) + 17), Eq(diff(y(t),t), -2*x(t) + y(t) + 12)) 

>>> sol = [Eq(x(t), (C1*sin(sqrt(2)*t) + C2*cos(sqrt(2)*t))*exp(t) - S(5)/3), 

... Eq(y(t), (sqrt(2)*C1*cos(sqrt(2)*t) - sqrt(2)*C2*sin(sqrt(2)*t))*exp(t) - S(46)/3)] 

>>> checksysodesol(eq, sol) 

(True, [0, 0]) 

>>> eq = (Eq(diff(x(t),t),x(t)*y(t)**4), Eq(diff(y(t),t),y(t)**3)) 

>>> sol = [Eq(x(t), C1*exp(-1/(4*(C2 + t)))), Eq(y(t), -sqrt(2)*sqrt(-1/(C2 + t))/2), 

... Eq(x(t), C1*exp(-1/(4*(C2 + t)))), Eq(y(t), sqrt(2)*sqrt(-1/(C2 + t))/2)] 

>>> checksysodesol(eq, sol) 

(True, [0, 0]) 

 

""" 

def _sympify(eq): 

return list(map(sympify, eq if iterable(eq) else [eq])) 

eqs = _sympify(eqs) 

for i in range(len(eqs)): 

if isinstance(eqs[i], Equality): 

eqs[i] = eqs[i].lhs - eqs[i].rhs 

if func is None: 

funcs = [] 

for eq in eqs: 

derivs = eq.atoms(Derivative) 

func = set().union(*[d.atoms(AppliedUndef) for d in derivs]) 

for func_ in func: 

funcs.append(func_) 

funcs = list(set(funcs)) 

if not all(isinstance(func, AppliedUndef) and len(func.args) == 1 for func in funcs)\ 

and len({func.args for func in funcs})!=1: 

raise ValueError("func must be a function of one variable, not %s" % func) 

for sol in sols: 

if len(sol.atoms(AppliedUndef)) != 1: 

raise ValueError("solutions should have one function only") 

if len(funcs) != len({sol.lhs for sol in sols}): 

raise ValueError("number of solutions provided does not match the number of equations") 

t = funcs[0].args[0] 

dictsol = dict() 

for sol in sols: 

func = list(sol.atoms(AppliedUndef))[0] 

if sol.rhs == func: 

sol = sol.reversed 

solved = sol.lhs == func and not sol.rhs.has(func) 

if not solved: 

rhs = solve(sol, func) 

if not rhs: 

raise NotImplementedError 

else: 

rhs = sol.rhs 

dictsol[func] = rhs 

checkeq = [] 

for eq in eqs: 

for func in funcs: 

eq = sub_func_doit(eq, func, dictsol[func]) 

ss = simplify(eq) 

if ss != 0: 

eq = ss.expand(force=True) 

else: 

eq = 0 

checkeq.append(eq) 

if len(set(checkeq)) == 1 and list(set(checkeq))[0] == 0: 

return (True, checkeq) 

else: 

return (False, checkeq) 

 

 

@vectorize(0) 

def odesimp(eq, func, order, constants, hint): 

r""" 

Simplifies ODEs, including trying to solve for ``func`` and running 

:py:meth:`~sympy.solvers.ode.constantsimp`. 

 

It may use knowledge of the type of solution that the hint returns to 

apply additional simplifications. 

 

It also attempts to integrate any :py:class:`~sympy.integrals.Integral`\s 

in the expression, if the hint is not an ``_Integral`` hint. 

 

This function should have no effect on expressions returned by 

:py:meth:`~sympy.solvers.ode.dsolve`, as 

:py:meth:`~sympy.solvers.ode.dsolve` already calls 

:py:meth:`~sympy.solvers.ode.odesimp`, but the individual hint functions 

do not call :py:meth:`~sympy.solvers.ode.odesimp` (because the 

:py:meth:`~sympy.solvers.ode.dsolve` wrapper does). Therefore, this 

function is designed for mainly internal use. 

 

Examples 

======== 

 

>>> from sympy import sin, symbols, dsolve, pprint, Function 

>>> from sympy.solvers.ode import odesimp 

>>> x , u2, C1= symbols('x,u2,C1') 

>>> f = Function('f') 

 

>>> eq = dsolve(x*f(x).diff(x) - f(x) - x*sin(f(x)/x), f(x), 

... hint='1st_homogeneous_coeff_subs_indep_div_dep_Integral', 

... simplify=False) 

>>> pprint(eq, wrap_line=False) 

x 

---- 

f(x) 

/ 

| 

| / 1 \ 

| -|u2 + -------| 

| | /1 \| 

| | sin|--|| 

| \ \u2// 

log(f(x)) = log(C1) + | ---------------- d(u2) 

| 2 

| u2 

| 

/ 

 

>>> pprint(odesimp(eq, f(x), 1, {C1}, 

... hint='1st_homogeneous_coeff_subs_indep_div_dep' 

... )) #doctest: +SKIP 

x 

--------- = C1 

/f(x)\ 

tan|----| 

\2*x / 

 

""" 

x = func.args[0] 

f = func.func 

C1 = get_numbered_constants(eq, num=1) 

 

# First, integrate if the hint allows it. 

eq = _handle_Integral(eq, func, order, hint) 

if hint.startswith("nth_linear_euler_eq_nonhomogeneous"): 

eq = simplify(eq) 

if not isinstance(eq, Equality): 

raise TypeError("eq should be an instance of Equality") 

 

# Second, clean up the arbitrary constants. 

# Right now, nth linear hints can put as many as 2*order constants in an 

# expression. If that number grows with another hint, the third argument 

# here should be raised accordingly, or constantsimp() rewritten to handle 

# an arbitrary number of constants. 

eq = constantsimp(eq, constants) 

 

# Lastly, now that we have cleaned up the expression, try solving for func. 

# When CRootOf is implemented in solve(), we will want to return a CRootOf 

# every time instead of an Equality. 

 

# Get the f(x) on the left if possible. 

if eq.rhs == func and not eq.lhs.has(func): 

eq = [Eq(eq.rhs, eq.lhs)] 

 

# make sure we are working with lists of solutions in simplified form. 

if eq.lhs == func and not eq.rhs.has(func): 

# The solution is already solved 

eq = [eq] 

 

# special simplification of the rhs 

if hint.startswith("nth_linear_constant_coeff"): 

# Collect terms to make the solution look nice. 

# This is also necessary for constantsimp to remove unnecessary 

# terms from the particular solution from variation of parameters 

# 

# Collect is not behaving reliably here. The results for 

# some linear constant-coefficient equations with repeated 

# roots do not properly simplify all constants sometimes. 

# 'collectterms' gives different orders sometimes, and results 

# differ in collect based on that order. The 

# sort-reverse trick fixes things, but may fail in the 

# future. In addition, collect is splitting exponentials with 

# rational powers for no reason. We have to do a match 

# to fix this using Wilds. 

global collectterms 

try: 

collectterms.sort(key=default_sort_key) 

collectterms.reverse() 

except Exception: 

pass 

assert len(eq) == 1 and eq[0].lhs == f(x) 

sol = eq[0].rhs 

sol = expand_mul(sol) 

for i, reroot, imroot in collectterms: 

sol = collect(sol, x**i*exp(reroot*x)*sin(abs(imroot)*x)) 

sol = collect(sol, x**i*exp(reroot*x)*cos(imroot*x)) 

for i, reroot, imroot in collectterms: 

sol = collect(sol, x**i*exp(reroot*x)) 

del collectterms 

 

# Collect is splitting exponentials with rational powers for 

# no reason. We call powsimp to fix. 

sol = powsimp(sol) 

 

eq[0] = Eq(f(x), sol) 

 

else: 

# The solution is not solved, so try to solve it 

try: 

floats = any(i.is_Float for i in eq.atoms(Number)) 

eqsol = solve(eq, func, force=True, rational=False if floats else None) 

if not eqsol: 

raise NotImplementedError 

except (NotImplementedError, PolynomialError): 

eq = [eq] 

else: 

def _expand(expr): 

numer, denom = expr.as_numer_denom() 

 

if denom.is_Add: 

return expr 

else: 

return powsimp(expr.expand(), combine='exp', deep=True) 

 

# XXX: the rest of odesimp() expects each ``t`` to be in a 

# specific normal form: rational expression with numerator 

# expanded, but with combined exponential functions (at 

# least in this setup all tests pass). 

eq = [Eq(f(x), _expand(t)) for t in eqsol] 

 

# special simplification of the lhs. 

if hint.startswith("1st_homogeneous_coeff"): 

for j, eqi in enumerate(eq): 

newi = logcombine(eqi, force=True) 

if isinstance(newi.lhs, log) and newi.rhs == 0: 

newi = Eq(newi.lhs.args[0]/C1, C1) 

eq[j] = newi 

 

# We cleaned up the constants before solving to help the solve engine with 

# a simpler expression, but the solved expression could have introduced 

# things like -C1, so rerun constantsimp() one last time before returning. 

for i, eqi in enumerate(eq): 

eq[i] = constantsimp(eqi, constants) 

eq[i] = constant_renumber(eq[i], 'C', 1, 2*order) 

 

# If there is only 1 solution, return it; 

# otherwise return the list of solutions. 

if len(eq) == 1: 

eq = eq[0] 

return eq 

 

def checkodesol(ode, sol, func=None, order='auto', solve_for_func=True): 

r""" 

Substitutes ``sol`` into ``ode`` and checks that the result is ``0``. 

 

This only works when ``func`` is one function, like `f(x)`. ``sol`` can 

be a single solution or a list of solutions. Each solution may be an 

:py:class:`~sympy.core.relational.Equality` that the solution satisfies, 

e.g. ``Eq(f(x), C1), Eq(f(x) + C1, 0)``; or simply an 

:py:class:`~sympy.core.expr.Expr`, e.g. ``f(x) - C1``. In most cases it 

will not be necessary to explicitly identify the function, but if the 

function cannot be inferred from the original equation it can be supplied 

through the ``func`` argument. 

 

If a sequence of solutions is passed, the same sort of container will be 

used to return the result for each solution. 

 

It tries the following methods, in order, until it finds zero equivalence: 

 

1. Substitute the solution for `f` in the original equation. This only 

works if ``ode`` is solved for `f`. It will attempt to solve it first 

unless ``solve_for_func == False``. 

2. Take `n` derivatives of the solution, where `n` is the order of 

``ode``, and check to see if that is equal to the solution. This only 

works on exact ODEs. 

3. Take the 1st, 2nd, ..., `n`\th derivatives of the solution, each time 

solving for the derivative of `f` of that order (this will always be 

possible because `f` is a linear operator). Then back substitute each 

derivative into ``ode`` in reverse order. 

 

This function returns a tuple. The first item in the tuple is ``True`` if 

the substitution results in ``0``, and ``False`` otherwise. The second 

item in the tuple is what the substitution results in. It should always 

be ``0`` if the first item is ``True``. Sometimes this function will 

return ``False`` even when an expression is identically equal to ``0``. 

This happens when :py:meth:`~sympy.simplify.simplify.simplify` does not 

reduce the expression to ``0``. If an expression returned by this 

function vanishes identically, then ``sol`` really is a solution to 

the ``ode``. 

 

If this function seems to hang, it is probably because of a hard 

simplification. 

 

To use this function to test, test the first item of the tuple. 

 

Examples 

======== 

 

>>> from sympy import Eq, Function, checkodesol, symbols 

>>> x, C1 = symbols('x,C1') 

>>> f = Function('f') 

>>> checkodesol(f(x).diff(x), Eq(f(x), C1)) 

(True, 0) 

>>> assert checkodesol(f(x).diff(x), C1)[0] 

>>> assert not checkodesol(f(x).diff(x), x)[0] 

>>> checkodesol(f(x).diff(x, 2), x**2) 

(False, 2) 

 

""" 

if not isinstance(ode, Equality): 

ode = Eq(ode, 0) 

if func is None: 

try: 

_, func = _preprocess(ode.lhs) 

except ValueError: 

funcs = [s.atoms(AppliedUndef) for s in ( 

sol if is_sequence(sol, set) else [sol])] 

funcs = set().union(*funcs) 

if len(funcs) != 1: 

raise ValueError( 

'must pass func arg to checkodesol for this case.') 

func = funcs.pop() 

if not isinstance(func, AppliedUndef) or len(func.args) != 1: 

raise ValueError( 

"func must be a function of one variable, not %s" % func) 

if is_sequence(sol, set): 

return type(sol)([checkodesol(ode, i, order=order, solve_for_func=solve_for_func) for i in sol]) 

 

if not isinstance(sol, Equality): 

sol = Eq(func, sol) 

elif sol.rhs == func: 

sol = sol.reversed 

 

if order == 'auto': 

order = ode_order(ode, func) 

solved = sol.lhs == func and not sol.rhs.has(func) 

if solve_for_func and not solved: 

rhs = solve(sol, func) 

if rhs: 

eqs = [Eq(func, t) for t in rhs] 

if len(rhs) == 1: 

eqs = eqs[0] 

return checkodesol(ode, eqs, order=order, 

solve_for_func=False) 

 

s = True 

testnum = 0 

x = func.args[0] 

while s: 

if testnum == 0: 

# First pass, try substituting a solved solution directly into the 

# ODE. This has the highest chance of succeeding. 

ode_diff = ode.lhs - ode.rhs 

 

if sol.lhs == func: 

s = sub_func_doit(ode_diff, func, sol.rhs) 

else: 

testnum += 1 

continue 

ss = simplify(s) 

if ss: 

# with the new numer_denom in power.py, if we do a simple 

# expansion then testnum == 0 verifies all solutions. 

s = ss.expand(force=True) 

else: 

s = 0 

testnum += 1 

elif testnum == 1: 

# Second pass. If we cannot substitute f, try seeing if the nth 

# derivative is equal, this will only work for odes that are exact, 

# by definition. 

s = simplify( 

trigsimp(diff(sol.lhs, x, order) - diff(sol.rhs, x, order)) - 

trigsimp(ode.lhs) + trigsimp(ode.rhs)) 

# s2 = simplify( 

# diff(sol.lhs, x, order) - diff(sol.rhs, x, order) - \ 

# ode.lhs + ode.rhs) 

testnum += 1 

elif testnum == 2: 

# Third pass. Try solving for df/dx and substituting that into the 

# ODE. Thanks to Chris Smith for suggesting this method. Many of 

# the comments below are his, too. 

# The method: 

# - Take each of 1..n derivatives of the solution. 

# - Solve each nth derivative for d^(n)f/dx^(n) 

# (the differential of that order) 

# - Back substitute into the ODE in decreasing order 

# (i.e., n, n-1, ...) 

# - Check the result for zero equivalence 

if sol.lhs == func and not sol.rhs.has(func): 

diffsols = {0: sol.rhs} 

elif sol.rhs == func and not sol.lhs.has(func): 

diffsols = {0: sol.lhs} 

else: 

diffsols = {} 

sol = sol.lhs - sol.rhs 

for i in range(1, order + 1): 

# Differentiation is a linear operator, so there should always 

# be 1 solution. Nonetheless, we test just to make sure. 

# We only need to solve once. After that, we automatically 

# have the solution to the differential in the order we want. 

if i == 1: 

ds = sol.diff(x) 

try: 

sdf = solve(ds, func.diff(x, i)) 

if not sdf: 

raise NotImplementedError 

except NotImplementedError: 

testnum += 1 

break 

else: 

diffsols[i] = sdf[0] 

else: 

# This is what the solution says df/dx should be. 

diffsols[i] = diffsols[i - 1].diff(x) 

 

# Make sure the above didn't fail. 

if testnum > 2: 

continue 

else: 

# Substitute it into ODE to check for self consistency. 

lhs, rhs = ode.lhs, ode.rhs 

for i in range(order, -1, -1): 

if i == 0 and 0 not in diffsols: 

# We can only substitute f(x) if the solution was 

# solved for f(x). 

break 

lhs = sub_func_doit(lhs, func.diff(x, i), diffsols[i]) 

rhs = sub_func_doit(rhs, func.diff(x, i), diffsols[i]) 

ode_or_bool = Eq(lhs, rhs) 

ode_or_bool = simplify(ode_or_bool) 

 

if isinstance(ode_or_bool, (bool, BooleanAtom)): 

if ode_or_bool: 

lhs = rhs = S.Zero 

else: 

lhs = ode_or_bool.lhs 

rhs = ode_or_bool.rhs 

# No sense in overworking simplify -- just prove that the 

# numerator goes to zero 

num = trigsimp((lhs - rhs).as_numer_denom()[0]) 

# since solutions are obtained using force=True we test 

# using the same level of assumptions 

## replace function with dummy so assumptions will work 

_func = Dummy('func') 

num = num.subs(func, _func) 

## posify the expression 

num, reps = posify(num) 

s = simplify(num).xreplace(reps).xreplace({_func: func}) 

testnum += 1 

else: 

break 

 

if not s: 

return (True, s) 

elif s is True: # The code above never was able to change s 

raise NotImplementedError("Unable to test if " + str(sol) + 

" is a solution to " + str(ode) + ".") 

else: 

return (False, s) 

 

 

def ode_sol_simplicity(sol, func, trysolving=True): 

r""" 

Returns an extended integer representing how simple a solution to an ODE 

is. 

 

The following things are considered, in order from most simple to least: 

 

- ``sol`` is solved for ``func``. 

- ``sol`` is not solved for ``func``, but can be if passed to solve (e.g., 

a solution returned by ``dsolve(ode, func, simplify=False``). 

- If ``sol`` is not solved for ``func``, then base the result on the 

length of ``sol``, as computed by ``len(str(sol))``. 

- If ``sol`` has any unevaluated :py:class:`~sympy.integrals.Integral`\s, 

this will automatically be considered less simple than any of the above. 

 

This function returns an integer such that if solution A is simpler than 

solution B by above metric, then ``ode_sol_simplicity(sola, func) < 

ode_sol_simplicity(solb, func)``. 

 

Currently, the following are the numbers returned, but if the heuristic is 

ever improved, this may change. Only the ordering is guaranteed. 

 

+----------------------------------------------+-------------------+ 

| Simplicity | Return | 

+==============================================+===================+ 

| ``sol`` solved for ``func`` | ``-2`` | 

+----------------------------------------------+-------------------+ 

| ``sol`` not solved for ``func`` but can be | ``-1`` | 

+----------------------------------------------+-------------------+ 

| ``sol`` is not solved nor solvable for | ``len(str(sol))`` | 

| ``func`` | | 

+----------------------------------------------+-------------------+ 

| ``sol`` contains an | ``oo`` | 

| :py:class:`~sympy.integrals.Integral` | | 

+----------------------------------------------+-------------------+ 

 

``oo`` here means the SymPy infinity, which should compare greater than 

any integer. 

 

If you already know :py:meth:`~sympy.solvers.solvers.solve` cannot solve 

``sol``, you can use ``trysolving=False`` to skip that step, which is the 

only potentially slow step. For example, 

:py:meth:`~sympy.solvers.ode.dsolve` with the ``simplify=False`` flag 

should do this. 

 

If ``sol`` is a list of solutions, if the worst solution in the list 

returns ``oo`` it returns that, otherwise it returns ``len(str(sol))``, 

that is, the length of the string representation of the whole list. 

 

Examples 

======== 

 

This function is designed to be passed to ``min`` as the key argument, 

such as ``min(listofsolutions, key=lambda i: ode_sol_simplicity(i, 

f(x)))``. 

 

>>> from sympy import symbols, Function, Eq, tan, cos, sqrt, Integral 

>>> from sympy.solvers.ode import ode_sol_simplicity 

>>> x, C1, C2 = symbols('x, C1, C2') 

>>> f = Function('f') 

 

>>> ode_sol_simplicity(Eq(f(x), C1*x**2), f(x)) 

-2 

>>> ode_sol_simplicity(Eq(x**2 + f(x), C1), f(x)) 

-1 

>>> ode_sol_simplicity(Eq(f(x), C1*Integral(2*x, x)), f(x)) 

oo 

>>> eq1 = Eq(f(x)/tan(f(x)/(2*x)), C1) 

>>> eq2 = Eq(f(x)/tan(f(x)/(2*x) + f(x)), C2) 

>>> [ode_sol_simplicity(eq, f(x)) for eq in [eq1, eq2]] 

[28, 35] 

>>> min([eq1, eq2], key=lambda i: ode_sol_simplicity(i, f(x))) 

Eq(f(x)/tan(f(x)/(2*x)), C1) 

 

""" 

# TODO: if two solutions are solved for f(x), we still want to be 

# able to get the simpler of the two 

 

# See the docstring for the coercion rules. We check easier (faster) 

# things here first, to save time. 

 

if iterable(sol): 

# See if there are Integrals 

for i in sol: 

if ode_sol_simplicity(i, func, trysolving=trysolving) == oo: 

return oo 

 

return len(str(sol)) 

 

if sol.has(Integral): 

return oo 

 

# Next, try to solve for func. This code will change slightly when CRootOf 

# is implemented in solve(). Probably a CRootOf solution should fall 

# somewhere between a normal solution and an unsolvable expression. 

 

# First, see if they are already solved 

if sol.lhs == func and not sol.rhs.has(func) or \ 

sol.rhs == func and not sol.lhs.has(func): 

return -2 

# We are not so lucky, try solving manually 

if trysolving: 

try: 

sols = solve(sol, func) 

if not sols: 

raise NotImplementedError 

except NotImplementedError: 

pass 

else: 

return -1 

 

# Finally, a naive computation based on the length of the string version 

# of the expression. This may favor combined fractions because they 

# will not have duplicate denominators, and may slightly favor expressions 

# with fewer additions and subtractions, as those are separated by spaces 

# by the printer. 

 

# Additional ideas for simplicity heuristics are welcome, like maybe 

# checking if a equation has a larger domain, or if constantsimp has 

# introduced arbitrary constants numbered higher than the order of a 

# given ODE that sol is a solution of. 

return len(str(sol)) 

 

 

def _get_constant_subexpressions(expr, Cs): 

Cs = set(Cs) 

Ces = [] 

def _recursive_walk(expr): 

expr_syms = expr.free_symbols 

if len(expr_syms) > 0 and expr_syms.issubset(Cs): 

Ces.append(expr) 

else: 

if expr.func == exp: 

expr = expr.expand(mul=True) 

if expr.func in (Add, Mul): 

d = sift(expr.args, lambda i : i.free_symbols.issubset(Cs)) 

if len(d[True]) > 1: 

x = expr.func(*d[True]) 

if not x.is_number: 

Ces.append(x) 

elif isinstance(expr, Integral): 

if expr.free_symbols.issubset(Cs) and \ 

all(len(x) == 3 for x in expr.limits): 

Ces.append(expr) 

for i in expr.args: 

_recursive_walk(i) 

return 

_recursive_walk(expr) 

return Ces 

 

def __remove_linear_redundancies(expr, Cs): 

cnts = {i: expr.count(i) for i in Cs} 

Cs = [i for i in Cs if cnts[i] > 0] 

 

def _linear(expr): 

if isinstance(expr, Add): 

xs = [i for i in Cs if expr.count(i)==cnts[i] \ 

and 0 == expr.diff(i, 2)] 

d = {} 

for x in xs: 

y = expr.diff(x) 

if y not in d: 

d[y]=[] 

d[y].append(x) 

for y in d: 

if len(d[y]) > 1: 

d[y].sort(key=str) 

for x in d[y][1:]: 

expr = expr.subs(x, 0) 

return expr 

 

def _recursive_walk(expr): 

if len(expr.args) != 0: 

expr = expr.func(*[_recursive_walk(i) for i in expr.args]) 

expr = _linear(expr) 

return expr 

 

if isinstance(expr, Equality): 

lhs, rhs = [_recursive_walk(i) for i in expr.args] 

f = lambda i: isinstance(i, Number) or i in Cs 

if isinstance(lhs, Symbol) and lhs in Cs: 

rhs, lhs = lhs, rhs 

if lhs.func in (Add, Symbol) and rhs.func in (Add, Symbol): 

dlhs = sift([lhs] if isinstance(lhs, AtomicExpr) else lhs.args, f) 

drhs = sift([rhs] if isinstance(rhs, AtomicExpr) else rhs.args, f) 

for i in [True, False]: 

for hs in [dlhs, drhs]: 

if i not in hs: 

hs[i] = [0] 

# this calculation can be simplified 

lhs = Add(*dlhs[False]) - Add(*drhs[False]) 

rhs = Add(*drhs[True]) - Add(*dlhs[True]) 

elif lhs.func in (Mul, Symbol) and rhs.func in (Mul, Symbol): 

dlhs = sift([lhs] if isinstance(lhs, AtomicExpr) else lhs.args, f) 

if True in dlhs: 

if False not in dlhs: 

dlhs[False] = [1] 

lhs = Mul(*dlhs[False]) 

rhs = rhs/Mul(*dlhs[True]) 

return Eq(lhs, rhs) 

else: 

return _recursive_walk(expr) 

 

@vectorize(0) 

def constantsimp(expr, constants): 

r""" 

Simplifies an expression with arbitrary constants in it. 

 

This function is written specifically to work with 

:py:meth:`~sympy.solvers.ode.dsolve`, and is not intended for general use. 

 

Simplification is done by "absorbing" the arbitrary constants into other 

arbitrary constants, numbers, and symbols that they are not independent 

of. 

 

The symbols must all have the same name with numbers after it, for 

example, ``C1``, ``C2``, ``C3``. The ``symbolname`` here would be 

'``C``', the ``startnumber`` would be 1, and the ``endnumber`` would be 3. 

If the arbitrary constants are independent of the variable ``x``, then the 

independent symbol would be ``x``. There is no need to specify the 

dependent function, such as ``f(x)``, because it already has the 

independent symbol, ``x``, in it. 

 

Because terms are "absorbed" into arbitrary constants and because 

constants are renumbered after simplifying, the arbitrary constants in 

expr are not necessarily equal to the ones of the same name in the 

returned result. 

 

If two or more arbitrary constants are added, multiplied, or raised to the 

power of each other, they are first absorbed together into a single 

arbitrary constant. Then the new constant is combined into other terms if 

necessary. 

 

Absorption of constants is done with limited assistance: 

 

1. terms of :py:class:`~sympy.core.add.Add`\s are collected to try join 

constants so `e^x (C_1 \cos(x) + C_2 \cos(x))` will simplify to `e^x 

C_1 \cos(x)`; 

 

2. powers with exponents that are :py:class:`~sympy.core.add.Add`\s are 

expanded so `e^{C_1 + x}` will be simplified to `C_1 e^x`. 

 

Use :py:meth:`~sympy.solvers.ode.constant_renumber` to renumber constants 

after simplification or else arbitrary numbers on constants may appear, 

e.g. `C_1 + C_3 x`. 

 

In rare cases, a single constant can be "simplified" into two constants. 

Every differential equation solution should have as many arbitrary 

constants as the order of the differential equation. The result here will 

be technically correct, but it may, for example, have `C_1` and `C_2` in 

an expression, when `C_1` is actually equal to `C_2`. Use your discretion 

in such situations, and also take advantage of the ability to use hints in 

:py:meth:`~sympy.solvers.ode.dsolve`. 

 

Examples 

======== 

 

>>> from sympy import symbols 

>>> from sympy.solvers.ode import constantsimp 

>>> C1, C2, C3, x, y = symbols('C1, C2, C3, x, y') 

>>> constantsimp(2*C1*x, {C1, C2, C3}) 

C1*x 

>>> constantsimp(C1 + 2 + x, {C1, C2, C3}) 

C1 + x 

>>> constantsimp(C1*C2 + 2 + C2 + C3*x, {C1, C2, C3}) 

C1 + C3*x 

 

""" 

# This function works recursively. The idea is that, for Mul, 

# Add, Pow, and Function, if the class has a constant in it, then 

# we can simplify it, which we do by recursing down and 

# simplifying up. Otherwise, we can skip that part of the 

# expression. 

 

Cs = constants 

 

orig_expr = expr 

 

constant_subexprs = _get_constant_subexpressions(expr, Cs) 

for xe in constant_subexprs: 

xes = list(xe.free_symbols) 

if not xes: 

continue 

if all([expr.count(c) == xe.count(c) for c in xes]): 

xes.sort(key=str) 

expr = expr.subs(xe, xes[0]) 

 

# try to perform common sub-expression elimination of constant terms 

try: 

commons, rexpr = cse(expr) 

commons.reverse() 

rexpr = rexpr[0] 

for s in commons: 

cs = list(s[1].atoms(Symbol)) 

if len(cs) == 1 and cs[0] in Cs and \ 

cs[0] not in rexpr.atoms(Symbol) and \ 

not any(cs[0] in ex for ex in commons if ex != s): 

rexpr = rexpr.subs(s[0], cs[0]) 

else: 

rexpr = rexpr.subs(*s) 

expr = rexpr 

except Exception: 

pass 

expr = __remove_linear_redundancies(expr, Cs) 

 

def _conditional_term_factoring(expr): 

new_expr = terms_gcd(expr, clear=False, deep=True, expand=False) 

 

# we do not want to factor exponentials, so handle this separately 

if new_expr.is_Mul: 

infac = False 

asfac = False 

for m in new_expr.args: 

if isinstance(m, exp): 

asfac = True 

elif m.is_Add: 

infac = any(isinstance(fi, exp) for t in m.args 

for fi in Mul.make_args(t)) 

if asfac and infac: 

new_expr = expr 

break 

return new_expr 

 

expr = _conditional_term_factoring(expr) 

 

# call recursively if more simplification is possible 

if orig_expr != expr: 

return constantsimp(expr, Cs) 

return expr 

 

 

def constant_renumber(expr, symbolname, startnumber, endnumber): 

r""" 

Renumber arbitrary constants in ``expr`` to have numbers 1 through `N` 

where `N` is ``endnumber - startnumber + 1`` at most. 

In the process, this reorders expression terms in a standard way. 

 

This is a simple function that goes through and renumbers any 

:py:class:`~sympy.core.symbol.Symbol` with a name in the form ``symbolname 

+ num`` where ``num`` is in the range from ``startnumber`` to 

``endnumber``. 

 

Symbols are renumbered based on ``.sort_key()``, so they should be 

numbered roughly in the order that they appear in the final, printed 

expression. Note that this ordering is based in part on hashes, so it can 

produce different results on different machines. 

 

The structure of this function is very similar to that of 

:py:meth:`~sympy.solvers.ode.constantsimp`. 

 

Examples 

======== 

 

>>> from sympy import symbols, Eq, pprint 

>>> from sympy.solvers.ode import constant_renumber 

>>> x, C0, C1, C2, C3, C4 = symbols('x,C:5') 

 

Only constants in the given range (inclusive) are renumbered; 

the renumbering always starts from 1: 

 

>>> constant_renumber(C1 + C3 + C4, 'C', 1, 3) 

C1 + C2 + C4 

>>> constant_renumber(C0 + C1 + C3 + C4, 'C', 2, 4) 

C0 + 2*C1 + C2 

>>> constant_renumber(C0 + 2*C1 + C2, 'C', 0, 1) 

C1 + 3*C2 

>>> pprint(C2 + C1*x + C3*x**2) 

2 

C1*x + C2 + C3*x 

>>> pprint(constant_renumber(C2 + C1*x + C3*x**2, 'C', 1, 3)) 

2 

C1 + C2*x + C3*x 

 

""" 

if type(expr) in (set, list, tuple): 

return type(expr)( 

[constant_renumber(i, symbolname=symbolname, startnumber=startnumber, endnumber=endnumber) 

for i in expr] 

) 

global newstartnumber 

newstartnumber = 1 

constants_found = [None]*(endnumber + 2) 

constantsymbols = [Symbol( 

symbolname + "%d" % t) for t in range(startnumber, 

endnumber + 1)] 

 

# make a mapping to send all constantsymbols to S.One and use 

# that to make sure that term ordering is not dependent on 

# the indexed value of C 

C_1 = [(ci, S.One) for ci in constantsymbols] 

sort_key=lambda arg: default_sort_key(arg.subs(C_1)) 

 

def _constant_renumber(expr): 

r""" 

We need to have an internal recursive function so that 

newstartnumber maintains its values throughout recursive calls. 

 

""" 

global newstartnumber 

 

if isinstance(expr, Equality): 

return Eq( 

_constant_renumber(expr.lhs), 

_constant_renumber(expr.rhs)) 

 

if type(expr) not in (Mul, Add, Pow) and not expr.is_Function and \ 

not expr.has(*constantsymbols): 

# Base case, as above. Hope there aren't constants inside 

# of some other class, because they won't be renumbered. 

return expr 

elif expr.is_Piecewise: 

return expr 

elif expr in constantsymbols: 

if expr not in constants_found: 

constants_found[newstartnumber] = expr 

newstartnumber += 1 

return expr 

elif expr.is_Function or expr.is_Pow or isinstance(expr, Tuple): 

return expr.func( 

*[_constant_renumber(x) for x in expr.args]) 

else: 

sortedargs = list(expr.args) 

sortedargs.sort(key=sort_key) 

return expr.func(*[_constant_renumber(x) for x in sortedargs]) 

expr = _constant_renumber(expr) 

# Renumbering happens here 

newconsts = symbols('C1:%d' % newstartnumber) 

expr = expr.subs(zip(constants_found[1:], newconsts), simultaneous=True) 

return expr 

 

 

def _handle_Integral(expr, func, order, hint): 

r""" 

Converts a solution with Integrals in it into an actual solution. 

 

For most hints, this simply runs ``expr.doit()``. 

 

""" 

global y 

x = func.args[0] 

f = func.func 

if hint == "1st_exact": 

sol = (expr.doit()).subs(y, f(x)) 

del y 

elif hint == "1st_exact_Integral": 

sol = Eq(Subs(expr.lhs, y, f(x)), expr.rhs) 

del y 

elif hint == "nth_linear_constant_coeff_homogeneous": 

sol = expr 

elif not hint.endswith("_Integral"): 

sol = expr.doit() 

else: 

sol = expr 

return sol 

 

 

# FIXME: replace the general solution in the docstring with 

# dsolve(equation, hint='1st_exact_Integral'). You will need to be able 

# to have assumptions on P and Q that dP/dy = dQ/dx. 

def ode_1st_exact(eq, func, order, match): 

r""" 

Solves 1st order exact ordinary differential equations. 

 

A 1st order differential equation is called exact if it is the total 

differential of a function. That is, the differential equation 

 

.. math:: P(x, y) \,\partial{}x + Q(x, y) \,\partial{}y = 0 

 

is exact if there is some function `F(x, y)` such that `P(x, y) = 

\partial{}F/\partial{}x` and `Q(x, y) = \partial{}F/\partial{}y`. It can 

be shown that a necessary and sufficient condition for a first order ODE 

to be exact is that `\partial{}P/\partial{}y = \partial{}Q/\partial{}x`. 

Then, the solution will be as given below:: 

 

>>> from sympy import Function, Eq, Integral, symbols, pprint 

>>> x, y, t, x0, y0, C1= symbols('x,y,t,x0,y0,C1') 

>>> P, Q, F= map(Function, ['P', 'Q', 'F']) 

>>> pprint(Eq(Eq(F(x, y), Integral(P(t, y), (t, x0, x)) + 

... Integral(Q(x0, t), (t, y0, y))), C1)) 

x y 

/ / 

| | 

F(x, y) = | P(t, y) dt + | Q(x0, t) dt = C1 

| | 

/ / 

x0 y0 

 

Where the first partials of `P` and `Q` exist and are continuous in a 

simply connected region. 

 

A note: SymPy currently has no way to represent inert substitution on an 

expression, so the hint ``1st_exact_Integral`` will return an integral 

with `dy`. This is supposed to represent the function that you are 

solving for. 

 

Examples 

======== 

 

>>> from sympy import Function, dsolve, cos, sin 

>>> from sympy.abc import x 

>>> f = Function('f') 

>>> dsolve(cos(f(x)) - (x*sin(f(x)) - f(x)**2)*f(x).diff(x), 

... f(x), hint='1st_exact') 

Eq(x*cos(f(x)) + f(x)**3/3, C1) 

 

References 

========== 

 

- http://en.wikipedia.org/wiki/Exact_differential_equation 

- M. Tenenbaum & H. Pollard, "Ordinary Differential Equations", 

Dover 1963, pp. 73 

 

# indirect doctest 

 

""" 

x = func.args[0] 

f = func.func 

r = match # d+e*diff(f(x),x) 

e = r[r['e']] 

d = r[r['d']] 

global y # This is the only way to pass dummy y to _handle_Integral 

y = r['y'] 

C1 = get_numbered_constants(eq, num=1) 

# Refer Joel Moses, "Symbolic Integration - The Stormy Decade", 

# Communications of the ACM, Volume 14, Number 8, August 1971, pp. 558 

# which gives the method to solve an exact differential equation. 

sol = Integral(d, x) + Integral((e - (Integral(d, x).diff(y))), y) 

return Eq(sol, C1) 

 

 

def ode_1st_homogeneous_coeff_best(eq, func, order, match): 

r""" 

Returns the best solution to an ODE from the two hints 

``1st_homogeneous_coeff_subs_dep_div_indep`` and 

``1st_homogeneous_coeff_subs_indep_div_dep``. 

 

This is as determined by :py:meth:`~sympy.solvers.ode.ode_sol_simplicity`. 

 

See the 

:py:meth:`~sympy.solvers.ode.ode_1st_homogeneous_coeff_subs_indep_div_dep` 

and 

:py:meth:`~sympy.solvers.ode.ode_1st_homogeneous_coeff_subs_dep_div_indep` 

docstrings for more information on these hints. Note that there is no 

``ode_1st_homogeneous_coeff_best_Integral`` hint. 

 

Examples 

======== 

 

>>> from sympy import Function, dsolve, pprint 

>>> from sympy.abc import x 

>>> f = Function('f') 

>>> pprint(dsolve(2*x*f(x) + (x**2 + f(x)**2)*f(x).diff(x), f(x), 

... hint='1st_homogeneous_coeff_best', simplify=False)) 

/ 2 \ 

| 3*x | 

log|----- + 1| 

| 2 | 

\f (x) / 

log(f(x)) = log(C1) - -------------- 

3 

 

References 

========== 

 

- http://en.wikipedia.org/wiki/Homogeneous_differential_equation 

- M. Tenenbaum & H. Pollard, "Ordinary Differential Equations", 

Dover 1963, pp. 59 

 

# indirect doctest 

 

""" 

# There are two substitutions that solve the equation, u1=y/x and u2=x/y 

# They produce different integrals, so try them both and see which 

# one is easier. 

sol1 = ode_1st_homogeneous_coeff_subs_indep_div_dep(eq, 

func, order, match) 

sol2 = ode_1st_homogeneous_coeff_subs_dep_div_indep(eq, 

func, order, match) 

simplify = match.get('simplify', True) 

if simplify: 

# why is odesimp called here? Should it be at the usual spot? 

constants = sol1.free_symbols.difference(eq.free_symbols) 

sol1 = odesimp( 

sol1, func, order, constants, 

"1st_homogeneous_coeff_subs_indep_div_dep") 

constants = sol2.free_symbols.difference(eq.free_symbols) 

sol2 = odesimp( 

sol2, func, order, constants, 

"1st_homogeneous_coeff_subs_dep_div_indep") 

return min([sol1, sol2], key=lambda x: ode_sol_simplicity(x, func, 

trysolving=not simplify)) 

 

 

def ode_1st_homogeneous_coeff_subs_dep_div_indep(eq, func, order, match): 

r""" 

Solves a 1st order differential equation with homogeneous coefficients 

using the substitution `u_1 = \frac{\text{<dependent 

variable>}}{\text{<independent variable>}}`. 

 

This is a differential equation 

 

.. math:: P(x, y) + Q(x, y) dy/dx = 0 

 

such that `P` and `Q` are homogeneous and of the same order. A function 

`F(x, y)` is homogeneous of order `n` if `F(x t, y t) = t^n F(x, y)`. 

Equivalently, `F(x, y)` can be rewritten as `G(y/x)` or `H(x/y)`. See 

also the docstring of :py:meth:`~sympy.solvers.ode.homogeneous_order`. 

 

If the coefficients `P` and `Q` in the differential equation above are 

homogeneous functions of the same order, then it can be shown that the 

substitution `y = u_1 x` (i.e. `u_1 = y/x`) will turn the differential 

equation into an equation separable in the variables `x` and `u`. If 

`h(u_1)` is the function that results from making the substitution `u_1 = 

f(x)/x` on `P(x, f(x))` and `g(u_2)` is the function that results from the 

substitution on `Q(x, f(x))` in the differential equation `P(x, f(x)) + 

Q(x, f(x)) f'(x) = 0`, then the general solution is:: 

 

>>> from sympy import Function, dsolve, pprint 

>>> from sympy.abc import x 

>>> f, g, h = map(Function, ['f', 'g', 'h']) 

>>> genform = g(f(x)/x) + h(f(x)/x)*f(x).diff(x) 

>>> pprint(genform) 

/f(x)\ /f(x)\ d 

g|----| + h|----|*--(f(x)) 

\ x / \ x / dx 

>>> pprint(dsolve(genform, f(x), 

... hint='1st_homogeneous_coeff_subs_dep_div_indep_Integral')) 

f(x) 

---- 

x 

/ 

| 

| -h(u1) 

log(x) = C1 + | ---------------- d(u1) 

| u1*h(u1) + g(u1) 

| 

/ 

 

Where `u_1 h(u_1) + g(u_1) \ne 0` and `x \ne 0`. 

 

See also the docstrings of 

:py:meth:`~sympy.solvers.ode.ode_1st_homogeneous_coeff_best` and 

:py:meth:`~sympy.solvers.ode.ode_1st_homogeneous_coeff_subs_indep_div_dep`. 

 

Examples 

======== 

 

>>> from sympy import Function, dsolve 

>>> from sympy.abc import x 

>>> f = Function('f') 

>>> pprint(dsolve(2*x*f(x) + (x**2 + f(x)**2)*f(x).diff(x), f(x), 

... hint='1st_homogeneous_coeff_subs_dep_div_indep', simplify=False)) 

/ 3 \ 

|3*f(x) f (x)| 

log|------ + -----| 

| x 3 | 

\ x / 

log(x) = log(C1) - ------------------- 

3 

 

References 

========== 

 

- http://en.wikipedia.org/wiki/Homogeneous_differential_equation 

- M. Tenenbaum & H. Pollard, "Ordinary Differential Equations", 

Dover 1963, pp. 59 

 

# indirect doctest 

 

""" 

x = func.args[0] 

f = func.func 

u = Dummy('u') 

u1 = Dummy('u1') # u1 == f(x)/x 

r = match # d+e*diff(f(x),x) 

C1 = get_numbered_constants(eq, num=1) 

xarg = match.get('xarg', 0) 

yarg = match.get('yarg', 0) 

int = Integral( 

(-r[r['e']]/(r[r['d']] + u1*r[r['e']])).subs({x: 1, r['y']: u1}), 

(u1, None, f(x)/x)) 

sol = logcombine(Eq(log(x), int + log(C1)), force=True) 

sol = sol.subs(f(x), u).subs(((u, u - yarg), (x, x - xarg), (u, f(x)))) 

return sol 

 

 

def ode_1st_homogeneous_coeff_subs_indep_div_dep(eq, func, order, match): 

r""" 

Solves a 1st order differential equation with homogeneous coefficients 

using the substitution `u_2 = \frac{\text{<independent 

variable>}}{\text{<dependent variable>}}`. 

 

This is a differential equation 

 

.. math:: P(x, y) + Q(x, y) dy/dx = 0 

 

such that `P` and `Q` are homogeneous and of the same order. A function 

`F(x, y)` is homogeneous of order `n` if `F(x t, y t) = t^n F(x, y)`. 

Equivalently, `F(x, y)` can be rewritten as `G(y/x)` or `H(x/y)`. See 

also the docstring of :py:meth:`~sympy.solvers.ode.homogeneous_order`. 

 

If the coefficients `P` and `Q` in the differential equation above are 

homogeneous functions of the same order, then it can be shown that the 

substitution `x = u_2 y` (i.e. `u_2 = x/y`) will turn the differential 

equation into an equation separable in the variables `y` and `u_2`. If 

`h(u_2)` is the function that results from making the substitution `u_2 = 

x/f(x)` on `P(x, f(x))` and `g(u_2)` is the function that results from the 

substitution on `Q(x, f(x))` in the differential equation `P(x, f(x)) + 

Q(x, f(x)) f'(x) = 0`, then the general solution is: 

 

>>> from sympy import Function, dsolve, pprint 

>>> from sympy.abc import x 

>>> f, g, h = map(Function, ['f', 'g', 'h']) 

>>> genform = g(x/f(x)) + h(x/f(x))*f(x).diff(x) 

>>> pprint(genform) 

/ x \ / x \ d 

g|----| + h|----|*--(f(x)) 

\f(x)/ \f(x)/ dx 

>>> pprint(dsolve(genform, f(x), 

... hint='1st_homogeneous_coeff_subs_indep_div_dep_Integral')) 

x 

---- 

f(x) 

/ 

| 

| -g(u2) 

| ---------------- d(u2) 

| u2*g(u2) + h(u2) 

| 

/ 

<BLANKLINE> 

f(x) = C1*e 

 

Where `u_2 g(u_2) + h(u_2) \ne 0` and `f(x) \ne 0`. 

 

See also the docstrings of 

:py:meth:`~sympy.solvers.ode.ode_1st_homogeneous_coeff_best` and 

:py:meth:`~sympy.solvers.ode.ode_1st_homogeneous_coeff_subs_dep_div_indep`. 

 

Examples 

======== 

 

>>> from sympy import Function, pprint, dsolve 

>>> from sympy.abc import x 

>>> f = Function('f') 

>>> pprint(dsolve(2*x*f(x) + (x**2 + f(x)**2)*f(x).diff(x), f(x), 

... hint='1st_homogeneous_coeff_subs_indep_div_dep', 

... simplify=False)) 

/ 2 \ 

| 3*x | 

log|----- + 1| 

| 2 | 

\f (x) / 

log(f(x)) = log(C1) - -------------- 

3 

 

References 

========== 

 

- http://en.wikipedia.org/wiki/Homogeneous_differential_equation 

- M. Tenenbaum & H. Pollard, "Ordinary Differential Equations", 

Dover 1963, pp. 59 

 

# indirect doctest 

 

""" 

x = func.args[0] 

f = func.func 

u = Dummy('u') 

u2 = Dummy('u2') # u2 == x/f(x) 

r = match # d+e*diff(f(x),x) 

C1 = get_numbered_constants(eq, num=1) 

xarg = match.get('xarg', 0) # If xarg present take xarg, else zero 

yarg = match.get('yarg', 0) # If yarg present take yarg, else zero 

int = Integral( 

simplify( 

(-r[r['d']]/(r[r['e']] + u2*r[r['d']])).subs({x: u2, r['y']: 1})), 

(u2, None, x/f(x))) 

sol = logcombine(Eq(log(f(x)), int + log(C1)), force=True) 

sol = sol.subs(f(x), u).subs(((u, u - yarg), (x, x - xarg), (u, f(x)))) 

return sol 

 

# XXX: Should this function maybe go somewhere else? 

 

 

def homogeneous_order(eq, *symbols): 

r""" 

Returns the order `n` if `g` is homogeneous and ``None`` if it is not 

homogeneous. 

 

Determines if a function is homogeneous and if so of what order. A 

function `f(x, y, \cdots)` is homogeneous of order `n` if `f(t x, t y, 

\cdots) = t^n f(x, y, \cdots)`. 

 

If the function is of two variables, `F(x, y)`, then `f` being homogeneous 

of any order is equivalent to being able to rewrite `F(x, y)` as `G(x/y)` 

or `H(y/x)`. This fact is used to solve 1st order ordinary differential 

equations whose coefficients are homogeneous of the same order (see the 

docstrings of 

:py:meth:`~solvers.ode.ode_1st_homogeneous_coeff_subs_dep_div_indep` and 

:py:meth:`~solvers.ode.ode_1st_homogeneous_coeff_subs_indep_div_dep`). 

 

Symbols can be functions, but every argument of the function must be a 

symbol, and the arguments of the function that appear in the expression 

must match those given in the list of symbols. If a declared function 

appears with different arguments than given in the list of symbols, 

``None`` is returned. 

 

Examples 

======== 

 

>>> from sympy import Function, homogeneous_order, sqrt 

>>> from sympy.abc import x, y 

>>> f = Function('f') 

>>> homogeneous_order(f(x), f(x)) is None 

True 

>>> homogeneous_order(f(x,y), f(y, x), x, y) is None 

True 

>>> homogeneous_order(f(x), f(x), x) 

1 

>>> homogeneous_order(x**2*f(x)/sqrt(x**2+f(x)**2), x, f(x)) 

2 

>>> homogeneous_order(x**2+f(x), x, f(x)) is None 

True 

 

""" 

 

if not symbols: 

raise ValueError("homogeneous_order: no symbols were given.") 

symset = set(symbols) 

eq = sympify(eq) 

 

# The following are not supported 

if eq.has(Order, Derivative): 

return None 

 

# These are all constants 

if (eq.is_Number or 

eq.is_NumberSymbol or 

eq.is_number 

): 

return S.Zero 

 

# Replace all functions with dummy variables 

dum = numbered_symbols(prefix='d', cls=Dummy) 

newsyms = set() 

for i in [j for j in symset if getattr(j, 'is_Function')]: 

iargs = set(i.args) 

if iargs.difference(symset): 

return None 

else: 

dummyvar = next(dum) 

eq = eq.subs(i, dummyvar) 

symset.remove(i) 

newsyms.add(dummyvar) 

symset.update(newsyms) 

 

if not eq.free_symbols & symset: 

return None 

 

# assuming order of a nested function can only be equal to zero 

if isinstance(eq, Function): 

return None if homogeneous_order( 

eq.args[0], *tuple(symset)) != 0 else S.Zero 

 

# make the replacement of x with x*t and see if t can be factored out 

t = Dummy('t', positive=True) # It is sufficient that t > 0 

eqs = separatevars(eq.subs([(i, t*i) for i in symset]), [t], dict=True)[t] 

if eqs is S.One: 

return S.Zero # there was no term with only t 

i, d = eqs.as_independent(t, as_Add=False) 

b, e = d.as_base_exp() 

if b == t: 

return e 

 

 

def ode_1st_linear(eq, func, order, match): 

r""" 

Solves 1st order linear differential equations. 

 

These are differential equations of the form 

 

.. math:: dy/dx + P(x) y = Q(x)\text{.} 

 

These kinds of differential equations can be solved in a general way. The 

integrating factor `e^{\int P(x) \,dx}` will turn the equation into a 

separable equation. The general solution is:: 

 

>>> from sympy import Function, dsolve, Eq, pprint, diff, sin 

>>> from sympy.abc import x 

>>> f, P, Q = map(Function, ['f', 'P', 'Q']) 

>>> genform = Eq(f(x).diff(x) + P(x)*f(x), Q(x)) 

>>> pprint(genform) 

d 

P(x)*f(x) + --(f(x)) = Q(x) 

dx 

>>> pprint(dsolve(genform, f(x), hint='1st_linear_Integral')) 

/ / \ 

| | | 

| | / | / 

| | | | | 

| | | P(x) dx | - | P(x) dx 

| | | | | 

| | / | / 

f(x) = |C1 + | Q(x)*e dx|*e 

| | | 

\ / / 

 

 

Examples 

======== 

 

>>> f = Function('f') 

>>> pprint(dsolve(Eq(x*diff(f(x), x) - f(x), x**2*sin(x)), 

... f(x), '1st_linear')) 

f(x) = x*(C1 - cos(x)) 

 

References 

========== 

 

- http://en.wikipedia.org/wiki/Linear_differential_equation#First_order_equation 

- M. Tenenbaum & H. Pollard, "Ordinary Differential Equations", 

Dover 1963, pp. 92 

 

# indirect doctest 

 

""" 

x = func.args[0] 

f = func.func 

r = match # a*diff(f(x),x) + b*f(x) + c 

C1 = get_numbered_constants(eq, num=1) 

t = exp(Integral(r[r['b']]/r[r['a']], x)) 

tt = Integral(t*(-r[r['c']]/r[r['a']]), x) 

f = match.get('u', f(x)) # take almost-linear u if present, else f(x) 

return Eq(f, (tt + C1)/t) 

 

 

def ode_Bernoulli(eq, func, order, match): 

r""" 

Solves Bernoulli differential equations. 

 

These are equations of the form 

 

.. math:: dy/dx + P(x) y = Q(x) y^n\text{, }n \ne 1`\text{.} 

 

The substitution `w = 1/y^{1-n}` will transform an equation of this form 

into one that is linear (see the docstring of 

:py:meth:`~sympy.solvers.ode.ode_1st_linear`). The general solution is:: 

 

>>> from sympy import Function, dsolve, Eq, pprint 

>>> from sympy.abc import x, n 

>>> f, P, Q = map(Function, ['f', 'P', 'Q']) 

>>> genform = Eq(f(x).diff(x) + P(x)*f(x), Q(x)*f(x)**n) 

>>> pprint(genform) 

d n 

P(x)*f(x) + --(f(x)) = Q(x)*f (x) 

dx 

>>> pprint(dsolve(genform, f(x), hint='Bernoulli_Integral')) #doctest: +SKIP 

1 

---- 

1 - n 

// / \ \ 

|| | | | 

|| | / | / | 

|| | | | | | 

|| | (1 - n)* | P(x) dx | (-1 + n)* | P(x) dx| 

|| | | | | | 

|| | / | / | 

f(x) = ||C1 + (-1 + n)* | -Q(x)*e dx|*e | 

|| | | | 

\\ / / / 

 

 

Note that the equation is separable when `n = 1` (see the docstring of 

:py:meth:`~sympy.solvers.ode.ode_separable`). 

 

>>> pprint(dsolve(Eq(f(x).diff(x) + P(x)*f(x), Q(x)*f(x)), f(x), 

... hint='separable_Integral')) 

f(x) 

/ 

| / 

| 1 | 

| - dy = C1 + | (-P(x) + Q(x)) dx 

| y | 

| / 

/ 

 

 

Examples 

======== 

 

>>> from sympy import Function, dsolve, Eq, pprint, log 

>>> from sympy.abc import x 

>>> f = Function('f') 

 

>>> pprint(dsolve(Eq(x*f(x).diff(x) + f(x), log(x)*f(x)**2), 

... f(x), hint='Bernoulli')) 

1 

f(x) = ------------------- 

/ log(x) 1\ 

x*|C1 + ------ + -| 

\ x x/ 

 

References 

========== 

 

- http://en.wikipedia.org/wiki/Bernoulli_differential_equation 

- M. Tenenbaum & H. Pollard, "Ordinary Differential Equations", 

Dover 1963, pp. 95 

 

# indirect doctest 

 

""" 

x = func.args[0] 

f = func.func 

r = match # a*diff(f(x),x) + b*f(x) + c*f(x)**n, n != 1 

C1 = get_numbered_constants(eq, num=1) 

t = exp((1 - r[r['n']])*Integral(r[r['b']]/r[r['a']], x)) 

tt = (r[r['n']] - 1)*Integral(t*r[r['c']]/r[r['a']], x) 

return Eq(f(x), ((tt + C1)/t)**(1/(1 - r[r['n']]))) 

 

 

def ode_Riccati_special_minus2(eq, func, order, match): 

r""" 

The general Riccati equation has the form 

 

.. math:: dy/dx = f(x) y^2 + g(x) y + h(x)\text{.} 

 

While it does not have a general solution [1], the "special" form, `dy/dx 

= a y^2 - b x^c`, does have solutions in many cases [2]. This routine 

returns a solution for `a(dy/dx) = b y^2 + c y/x + d/x^2` that is obtained 

by using a suitable change of variables to reduce it to the special form 

and is valid when neither `a` nor `b` are zero and either `c` or `d` is 

zero. 

 

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

>>> from sympy.solvers.ode import dsolve, checkodesol 

>>> from sympy import pprint, Function 

>>> f = Function('f') 

>>> y = f(x) 

>>> genform = a*y.diff(x) - (b*y**2 + c*y/x + d/x**2) 

>>> sol = dsolve(genform, y) 

>>> pprint(sol, wrap_line=False) 

/ / __________________ \\ 

| __________________ | / 2 || 

| / 2 | \/ 4*b*d - (a + c) *log(x)|| 

-|a + c - \/ 4*b*d - (a + c) *tan|C1 + ----------------------------|| 

\ \ 2*a // 

f(x) = ------------------------------------------------------------------------ 

2*b*x 

 

>>> checkodesol(genform, sol, order=1)[0] 

True 

 

References 

========== 

 

1. http://www.maplesoft.com/support/help/Maple/view.aspx?path=odeadvisor/Riccati 

2. http://eqworld.ipmnet.ru/en/solutions/ode/ode0106.pdf - 

http://eqworld.ipmnet.ru/en/solutions/ode/ode0123.pdf 

""" 

 

x = func.args[0] 

f = func.func 

r = match # a2*diff(f(x),x) + b2*f(x) + c2*f(x)/x + d2/x**2 

a2, b2, c2, d2 = [r[r[s]] for s in 'a2 b2 c2 d2'.split()] 

C1 = get_numbered_constants(eq, num=1) 

mu = sqrt(4*d2*b2 - (a2 - c2)**2) 

return Eq(f(x), (a2 - c2 - mu*tan(mu/(2*a2)*log(x) + C1))/(2*b2*x)) 

 

 

def ode_Liouville(eq, func, order, match): 

r""" 

Solves 2nd order Liouville differential equations. 

 

The general form of a Liouville ODE is 

 

.. math:: \frac{d^2 y}{dx^2} + g(y) \left(\! 

\frac{dy}{dx}\!\right)^2 + h(x) 

\frac{dy}{dx}\text{.} 

 

The general solution is: 

 

>>> from sympy import Function, dsolve, Eq, pprint, diff 

>>> from sympy.abc import x 

>>> f, g, h = map(Function, ['f', 'g', 'h']) 

>>> genform = Eq(diff(f(x),x,x) + g(f(x))*diff(f(x),x)**2 + 

... h(x)*diff(f(x),x), 0) 

>>> pprint(genform) 

2 2 

/d \ d d 

g(f(x))*|--(f(x))| + h(x)*--(f(x)) + ---(f(x)) = 0 

\dx / dx 2 

dx 

>>> pprint(dsolve(genform, f(x), hint='Liouville_Integral')) 

f(x) 

/ / 

| | 

| / | / 

| | | | 

| - | h(x) dx | | g(y) dy 

| | | | 

| / | / 

C1 + C2* | e dx + | e dy = 0 

| | 

/ / 

 

Examples 

======== 

 

>>> from sympy import Function, dsolve, Eq, pprint 

>>> from sympy.abc import x 

>>> f = Function('f') 

>>> pprint(dsolve(diff(f(x), x, x) + diff(f(x), x)**2/f(x) + 

... diff(f(x), x)/x, f(x), hint='Liouville')) 

________________ ________________ 

[f(x) = -\/ C1 + C2*log(x) , f(x) = \/ C1 + C2*log(x) ] 

 

References 

========== 

 

- Goldstein and Braun, "Advanced Methods for the Solution of Differential 

Equations", pp. 98 

- http://www.maplesoft.com/support/help/Maple/view.aspx?path=odeadvisor/Liouville 

 

# indirect doctest 

 

""" 

# Liouville ODE: 

# f(x).diff(x, 2) + g(f(x))*(f(x).diff(x, 2))**2 + h(x)*f(x).diff(x) 

# See Goldstein and Braun, "Advanced Methods for the Solution of 

# Differential Equations", pg. 98, as well as 

# http://www.maplesoft.com/support/help/view.aspx?path=odeadvisor/Liouville 

x = func.args[0] 

f = func.func 

r = match # f(x).diff(x, 2) + g*f(x).diff(x)**2 + h*f(x).diff(x) 

y = r['y'] 

C1, C2 = get_numbered_constants(eq, num=2) 

int = Integral(exp(Integral(r['g'], y)), (y, None, f(x))) 

sol = Eq(int + C1*Integral(exp(-Integral(r['h'], x)), x) + C2, 0) 

return sol 

 

 

def ode_2nd_power_series_ordinary(eq, func, order, match): 

r""" 

Gives a power series solution to a second order homogeneous differential 

equation with polynomial coefficients at an ordinary point. A homogenous 

differential equation is of the form 

 

.. math :: P(x)\frac{d^2y}{dx^2} + Q(x)\frac{dy}{dx} + R(x) = 0 

 

For simplicity it is assumed that `P(x)`, `Q(x)` and `R(x)` are polynomials, 

it is sufficient that `\frac{Q(x)}{P(x)}` and `\frac{R(x)}{P(x)}` exists at 

`x_{0}`. A recurrence relation is obtained by substituting `y` as `\sum_{n=0}^\infty a_{n}x^{n}`, 

in the differential equation, and equating the nth term. Using this relation 

various terms can be generated. 

 

 

Examples 

======== 

 

>>> from sympy import dsolve, Function, pprint 

>>> from sympy.abc import x, y 

>>> f = Function("f") 

>>> eq = f(x).diff(x, 2) + f(x) 

>>> pprint(dsolve(eq, hint='2nd_power_series_ordinary')) 

/ 4 2 \ / 2 \ 

|x x | | x | / 6\ 

f(x) = C2*|-- - -- + 1| + C1*x*|- -- + 1| + O\x / 

\24 2 / \ 6 / 

 

 

References 

========== 

- http://tutorial.math.lamar.edu/Classes/DE/SeriesSolutions.aspx 

- George E. Simmons, "Differential Equations with Applications and 

Historical Notes", p.p 176 - 184 

 

""" 

x = func.args[0] 

f = func.func 

C0, C1 = get_numbered_constants(eq, num=2) 

n = Dummy("n", integer=True) 

s = Wild("s") 

k = Wild("k", exclude=[x]) 

x0 = match.get('x0') 

terms = match.get('terms', 5) 

p = match[match['a3']] 

q = match[match['b3']] 

r = match[match['c3']] 

seriesdict = {} 

recurr = Function("r") 

 

# Generating the recurrence relation which works this way: 

# for the second order term the summation begins at n = 2. The coefficients 

# p is multiplied with an*(n - 1)*(n - 2)*x**n-2 and a substitution is made such that 

# the exponent of x becomes n. 

# For example, if p is x, then the second degree recurrence term is 

# an*(n - 1)*(n - 2)*x**n-1, substituting (n - 1) as n, it transforms to 

# an+1*n*(n - 1)*x**n. 

# A similar process is done with the first order and zeroth order term. 

 

coefflist = [(recurr(n), r), (n*recurr(n), q), (n*(n - 1)*recurr(n), p)] 

for index, coeff in enumerate(coefflist): 

if coeff[1]: 

f2 = powsimp(expand((coeff[1]*(x - x0)**(n - index)).subs(x, x + x0))) 

if f2.is_Add: 

addargs = f2.args 

else: 

addargs = [f2] 

for arg in addargs: 

powm = arg.match(s*x**k) 

term = coeff[0]*powm[s] 

if not powm[k].is_Symbol: 

term = term.subs(n, n - powm[k].as_independent(n)[0]) 

startind = powm[k].subs(n, index) 

# Seeing if the startterm can be reduced further. 

# If it vanishes for n lesser than startind, it is 

# equal to summation from n. 

if startind: 

for i in reversed(range(startind)): 

if not term.subs(n, i): 

seriesdict[term] = i 

else: 

seriesdict[term] = i + 1 

break 

else: 

seriesdict[term] = S(0) 

 

# Stripping of terms so that the sum starts with the same number. 

teq = S(0) 

suminit = seriesdict.values() 

rkeys = seriesdict.keys() 

req = Add(*rkeys) 

if any(suminit): 

maxval = max(suminit) 

for term in seriesdict: 

val = seriesdict[term] 

if val != maxval: 

for i in range(val, maxval): 

teq += term.subs(n, val) 

 

finaldict = {} 

if teq: 

fargs = teq.atoms(AppliedUndef) 

if len(fargs) == 1: 

finaldict[fargs.pop()] = 0 

else: 

maxf = max(fargs, key = lambda x: x.args[0]) 

sol = solve(teq, maxf) 

if isinstance(sol, list): 

sol = sol[0] 

finaldict[maxf] = sol 

 

# Finding the recurrence relation in terms of the largest term. 

fargs = req.atoms(AppliedUndef) 

maxf = max(fargs, key = lambda x: x.args[0]) 

minf = min(fargs, key = lambda x: x.args[0]) 

if minf.args[0].is_Symbol: 

startiter = 0 

else: 

startiter = -minf.args[0].as_independent(n)[0] 

lhs = maxf 

rhs = solve(req, maxf) 

if isinstance(rhs, list): 

rhs = rhs[0] 

 

# Checking how many values are already present 

tcounter = len([t for t in finaldict.values() if t]) 

 

for _ in range(tcounter, terms - 3): # Assuming c0 and c1 to be arbitrary 

check = rhs.subs(n, startiter) 

nlhs = lhs.subs(n, startiter) 

nrhs = check.subs(finaldict) 

finaldict[nlhs] = nrhs 

startiter += 1 

 

# Post processing 

series = C0 + C1*(x - x0) 

for term in finaldict: 

if finaldict[term]: 

fact = term.args[0] 

series += (finaldict[term].subs([(recurr(0), C0), (recurr(1), C1)])*( 

x - x0)**fact) 

series = collect(expand_mul(series), [C0, C1]) + Order(x**terms) 

return Eq(f(x), series) 

 

 

 

def ode_2nd_power_series_regular(eq, func, order, match): 

r""" 

Gives a power series solution to a second order homogeneous differential 

equation with polynomial coefficients at a regular point. A second order 

homogenous differential equation is of the form 

 

.. math :: P(x)\frac{d^2y}{dx^2} + Q(x)\frac{dy}{dx} + R(x) = 0 

 

A point is said to regular singular at `x0` if `x - x0\frac{Q(x)}{P(x)}` 

and `(x - x0)^{2}\frac{R(x)}{P(x)}` are analytic at `x0`. For simplicity 

`P(x)`, `Q(x)` and `R(x)` are assumed to be polynomials. The algorithm for 

finding the power series solutions is: 

 

1. Try expressing `(x - x0)P(x)` and `((x - x0)^{2})Q(x)` as power series 

solutions about x0. Find `p0` and `q0` which are the constants of the 

power series expansions. 

2. Solve the indicial equation `f(m) = m(m - 1) + m*p0 + q0`, to obtain the 

roots `m1` and `m2` of the indicial equation. 

3. If `m1 - m2` is a non integer there exists two series solutions. If 

`m1 = m2`, there exists only one solution. If `m1 - m2` is an integer, 

then the existence of one solution is confirmed. The other solution may 

or may not exist. 

 

The power series solution is of the form `x^{m}\sum_{n=0}^\infty a_{n}x^{n}`. The 

coefficients are determined by the following recurrence relation. 

`a_{n} = -\frac{\sum_{k=0}^{n-1} q_{n-k} + (m + k)p_{n-k}}{f(m + n)}`. For the case 

in which `m1 - m2` is an integer, it can be seen from the recurrence relation 

that for the lower root `m`, when `n` equals the difference of both the 

roots, the denominator becomes zero. So if the numerator is not equal to zero, 

a second series solution exists. 

 

 

Examples 

======== 

 

>>> from sympy import dsolve, Function, pprint 

>>> from sympy.abc import x, y 

>>> f = Function("f") 

>>> eq = x*(f(x).diff(x, 2)) + 2*(f(x).diff(x)) + x*f(x) 

>>> pprint(dsolve(eq)) 

/ 6 4 2 \ 

| x x x | 

/ 4 2 \ C1*|- --- + -- - -- + 1| 

| x x | \ 720 24 2 / / 6\ 

f(x) = C2*|--- - -- + 1| + ------------------------ + O\x / 

\120 6 / x 

 

 

References 

========== 

- George E. Simmons, "Differential Equations with Applications and 

Historical Notes", p.p 176 - 184 

 

""" 

x = func.args[0] 

f = func.func 

C0, C1 = get_numbered_constants(eq, num=2) 

n = Dummy("n") 

m = Dummy("m") # for solving the indicial equation 

s = Wild("s") 

k = Wild("k", exclude=[x]) 

x0 = match.get('x0') 

terms = match.get('terms', 5) 

p = match['p'] 

q = match['q'] 

 

# Generating the indicial equation 

indicial = [] 

for term in [p, q]: 

if not term.has(x): 

indicial.append(term) 

else: 

term = series(term, n=1, x0=x0) 

if isinstance(term, Order): 

indicial.append(S(0)) 

else: 

for arg in term.args: 

if not arg.has(x): 

indicial.append(arg) 

break 

 

p0, q0 = indicial 

sollist = solve(m*(m - 1) + m*p0 + q0, m) 

if sollist and isinstance(sollist, list) and all( 

[sol.is_real for sol in sollist]): 

serdict1 = {} 

serdict2 = {} 

if len(sollist) == 1: 

# Only one series solution exists in this case. 

m1 = m2 = sollist.pop() 

if terms-m1-1 <= 0: 

return Eq(f(x), Order(terms)) 

serdict1 = _frobenius(terms-m1-1, m1, p0, q0, p, q, x0, x, C0) 

 

else: 

m1 = sollist[0] 

m2 = sollist[1] 

if m1 < m2: 

m1, m2 = m2, m1 

# Irrespective of whether m1 - m2 is an integer or not, one 

# Frobenius series solution exists. 

serdict1 = _frobenius(terms-m1-1, m1, p0, q0, p, q, x0, x, C0) 

if not (m1 - m2).is_integer: 

# Second frobenius series solution exists. 

serdict2 = _frobenius(terms-m2-1, m2, p0, q0, p, q, x0, x, C1) 

else: 

# Check if second frobenius series solution exists. 

serdict2 = _frobenius(terms-m2-1, m2, p0, q0, p, q, x0, x, C1, check=m1) 

 

if serdict1: 

finalseries1 = C0 

for key in serdict1: 

power = int(key.name[1:]) 

finalseries1 += serdict1[key]*(x - x0)**power 

finalseries1 = (x - x0)**m1*finalseries1 

finalseries2 = S(0) 

if serdict2: 

for key in serdict2: 

power = int(key.name[1:]) 

finalseries2 += serdict2[key]*(x - x0)**power 

finalseries2 += C1 

finalseries2 = (x - x0)**m2*finalseries2 

return Eq(f(x), collect(finalseries1 + finalseries2, 

[C0, C1]) + Order(x**terms)) 

 

def _frobenius(n, m, p0, q0, p, q, x0, x, c, check=None): 

r""" 

Returns a dict with keys as coefficients and values as their values in terms of C0 

""" 

n = int(n) 

# In cases where m1 - m2 is not an integer 

m2 = check 

 

d = Dummy("d") 

numsyms = numbered_symbols("C", start=0) 

numsyms = [next(numsyms) for i in range(n + 1)] 

C0 = Symbol("C0") 

serlist = [] 

for ser in [p, q]: 

# Order term not present 

if ser.is_polynomial(x) and Poly(ser, x).degree() <= n: 

if x0: 

ser = ser.subs(x, x + x0) 

dict_ = Poly(ser, x).as_dict() 

# Order term present 

else: 

tseries = series(ser, x=x0, n=n+1) 

# Removing order 

dict_ = Poly(list(ordered(tseries.args))[: -1], x).as_dict() 

# Fill in with zeros, if coefficients are zero. 

for i in range(n + 1): 

if (i,) not in dict_: 

dict_[(i,)] = S(0) 

serlist.append(dict_) 

 

pseries = serlist[0] 

qseries = serlist[1] 

indicial = d*(d - 1) + d*p0 + q0 

frobdict = {} 

for i in range(1, n + 1): 

num = c*(m*pseries[(i,)] + qseries[(i,)]) 

for j in range(1, i): 

sym = Symbol("C" + str(j)) 

num += frobdict[sym]*((m + j)*pseries[(i - j,)] + qseries[(i - j,)]) 

 

# Checking for cases when m1 - m2 is an integer. If num equals zero 

# then a second Frobenius series solution cannot be found. If num is not zero 

# then set constant as zero and proceed. 

if m2 is not None and i == m2 - m: 

if num: 

return False 

else: 

frobdict[numsyms[i]] = S(0) 

else: 

frobdict[numsyms[i]] = -num/(indicial.subs(d, m+i)) 

 

return frobdict 

 

def _nth_linear_match(eq, func, order): 

r""" 

Matches a differential equation to the linear form: 

 

.. math:: a_n(x) y^{(n)} + \cdots + a_1(x)y' + a_0(x) y + B(x) = 0 

 

Returns a dict of order:coeff terms, where order is the order of the 

derivative on each term, and coeff is the coefficient of that derivative. 

The key ``-1`` holds the function `B(x)`. Returns ``None`` if the ODE is 

not linear. This function assumes that ``func`` has already been checked 

to be good. 

 

Examples 

======== 

 

>>> from sympy import Function, cos, sin 

>>> from sympy.abc import x 

>>> from sympy.solvers.ode import _nth_linear_match 

>>> f = Function('f') 

>>> _nth_linear_match(f(x).diff(x, 3) + 2*f(x).diff(x) + 

... x*f(x).diff(x, 2) + cos(x)*f(x).diff(x) + x - f(x) - 

... sin(x), f(x), 3) 

{-1: x - sin(x), 0: -1, 1: cos(x) + 2, 2: x, 3: 1} 

>>> _nth_linear_match(f(x).diff(x, 3) + 2*f(x).diff(x) + 

... x*f(x).diff(x, 2) + cos(x)*f(x).diff(x) + x - f(x) - 

... sin(f(x)), f(x), 3) == None 

True 

 

""" 

x = func.args[0] 

one_x = {x} 

terms = {i: S.Zero for i in range(-1, order + 1)} 

for i in Add.make_args(eq): 

if not i.has(func): 

terms[-1] += i 

else: 

c, f = i.as_independent(func) 

if isinstance(f, Derivative) and set(f.variables) == one_x: 

terms[f.derivative_count] += c 

elif f == func: 

terms[len(f.args[1:])] += c 

else: 

return None 

return terms 

 

 

def ode_nth_linear_euler_eq_homogeneous(eq, func, order, match, returns='sol'): 

r""" 

Solves an `n`\th order linear homogeneous variable-coefficient 

Cauchy-Euler equidimensional ordinary differential equation. 

 

This is an equation with form `0 = a_0 f(x) + a_1 x f'(x) + a_2 x^2 f''(x) 

\cdots`. 

 

These equations can be solved in a general manner, by substituting 

solutions of the form `f(x) = x^r`, and deriving a characteristic equation 

for `r`. When there are repeated roots, we include extra terms of the 

form `C_{r k} \ln^k(x) x^r`, where `C_{r k}` is an arbitrary integration 

constant, `r` is a root of the characteristic equation, and `k` ranges 

over the multiplicity of `r`. In the cases where the roots are complex, 

solutions of the form `C_1 x^a \sin(b \log(x)) + C_2 x^a \cos(b \log(x))` 

are returned, based on expansions with Euler's formula. The general 

solution is the sum of the terms found. If SymPy cannot find exact roots 

to the characteristic equation, a 

:py:class:`~sympy.polys.rootoftools.CRootOf` instance will be returned 

instead. 

 

>>> from sympy import Function, dsolve, Eq 

>>> from sympy.abc import x 

>>> f = Function('f') 

>>> dsolve(4*x**2*f(x).diff(x, 2) + f(x), f(x), 

... hint='nth_linear_euler_eq_homogeneous') 

... # doctest: +NORMALIZE_WHITESPACE 

Eq(f(x), sqrt(x)*(C1 + C2*log(x))) 

 

Note that because this method does not involve integration, there is no 

``nth_linear_euler_eq_homogeneous_Integral`` hint. 

 

The following is for internal use: 

 

- ``returns = 'sol'`` returns the solution to the ODE. 

- ``returns = 'list'`` returns a list of linearly independent solutions, 

corresponding to the fundamental solution set, for use with non 

homogeneous solution methods like variation of parameters and 

undetermined coefficients. Note that, though the solutions should be 

linearly independent, this function does not explicitly check that. You 

can do ``assert simplify(wronskian(sollist)) != 0`` to check for linear 

independence. Also, ``assert len(sollist) == order`` will need to pass. 

- ``returns = 'both'``, return a dictionary ``{'sol': <solution to ODE>, 

'list': <list of linearly independent solutions>}``. 

 

Examples 

======== 

 

>>> from sympy import Function, dsolve, pprint 

>>> from sympy.abc import x 

>>> f = Function('f') 

>>> eq = f(x).diff(x, 2)*x**2 - 4*f(x).diff(x)*x + 6*f(x) 

>>> pprint(dsolve(eq, f(x), 

... hint='nth_linear_euler_eq_homogeneous')) 

2 

f(x) = x *(C1 + C2*x) 

 

References 

========== 

 

- http://en.wikipedia.org/wiki/Cauchy%E2%80%93Euler_equation 

- C. Bender & S. Orszag, "Advanced Mathematical Methods for Scientists and 

Engineers", Springer 1999, pp. 12 

 

# indirect doctest 

 

""" 

global collectterms 

collectterms = [] 

 

x = func.args[0] 

f = func.func 

r = match 

 

# First, set up characteristic equation. 

chareq, symbol = S.Zero, Dummy('x') 

 

for i in r.keys(): 

if not isinstance(i, str) and i >= 0: 

chareq += (r[i]*diff(x**symbol, x, i)*x**-symbol).expand() 

 

chareq = Poly(chareq, symbol) 

chareqroots = [rootof(chareq, k) for k in range(chareq.degree())] 

 

# A generator of constants 

constants = list(get_numbered_constants(eq, num=chareq.degree()*2)) 

constants.reverse() 

 

# Create a dict root: multiplicity or charroots 

charroots = defaultdict(int) 

for root in chareqroots: 

charroots[root] += 1 

gsol = S(0) 

# We need keep track of terms so we can run collect() at the end. 

# This is necessary for constantsimp to work properly. 

ln = log 

for root, multiplicity in charroots.items(): 

for i in range(multiplicity): 

if isinstance(root, RootOf): 

gsol += (x**root) * constants.pop() 

if multiplicity != 1: 

raise ValueError("Value should be 1") 

collectterms = [(0, root, 0)] + collectterms 

elif root.is_real: 

gsol += ln(x)**i*(x**root) * constants.pop() 

collectterms = [(i, root, 0)] + collectterms 

else: 

reroot = re(root) 

imroot = im(root) 

gsol += ln(x)**i * (x**reroot) * ( 

constants.pop() * sin(abs(imroot)*ln(x)) 

+ constants.pop() * cos(imroot*ln(x))) 

# Preserve ordering (multiplicity, real part, imaginary part) 

# It will be assumed implicitly when constructing 

# fundamental solution sets. 

collectterms = [(i, reroot, imroot)] + collectterms 

if returns == 'sol': 

return Eq(f(x), gsol) 

elif returns in ('list' 'both'): 

# HOW TO TEST THIS CODE? (dsolve does not pass 'returns' through) 

# Create a list of (hopefully) linearly independent solutions 

gensols = [] 

# Keep track of when to use sin or cos for nonzero imroot 

for i, reroot, imroot in collectterms: 

if imroot == 0: 

gensols.append(ln(x)**i*x**reroot) 

else: 

sin_form = ln(x)**i*x**reroot*sin(abs(imroot)*ln(x)) 

if sin_form in gensols: 

cos_form = ln(x)**i*x**reroot*cos(imroot*ln(x)) 

gensols.append(cos_form) 

else: 

gensols.append(sin_form) 

if returns == 'list': 

return gensols 

else: 

return {'sol': Eq(f(x), gsol), 'list': gensols} 

else: 

raise ValueError('Unknown value for key "returns".') 

 

 

def ode_nth_linear_euler_eq_nonhomogeneous_undetermined_coefficients(eq, func, order, match, returns='sol'): 

r""" 

Solves an `n`\th order linear non homogeneous Cauchy-Euler equidimensional 

ordinary differential equation using undetermined coefficients. 

 

This is an equation with form `g(x) = a_0 f(x) + a_1 x f'(x) + a_2 x^2 f''(x) 

\cdots`. 

 

These equations can be solved in a general manner, by substituting 

solutions of the form `x = exp(t)`, and deriving a characteristic equation 

of form `g(exp(t)) = b_0 f(t) + b_1 f'(t) + b_2 f''(t) \cdots` which can 

be then solved by nth_linear_constant_coeff_undetermined_coefficients if 

g(exp(t)) has finite number of linearly independent derivatives. 

 

Functions that fit this requirement are finite sums functions of the form 

`a x^i e^{b x} \sin(c x + d)` or `a x^i e^{b x} \cos(c x + d)`, where `i` 

is a non-negative integer and `a`, `b`, `c`, and `d` are constants. For 

example any polynomial in `x`, functions like `x^2 e^{2 x}`, `x \sin(x)`, 

and `e^x \cos(x)` can all be used. Products of `\sin`'s and `\cos`'s have 

a finite number of derivatives, because they can be expanded into `\sin(a 

x)` and `\cos(b x)` terms. However, SymPy currently cannot do that 

expansion, so you will need to manually rewrite the expression in terms of 

the above to use this method. So, for example, you will need to manually 

convert `\sin^2(x)` into `(1 + \cos(2 x))/2` to properly apply the method 

of undetermined coefficients on it. 

 

After replacement of x by exp(t), this method works by creating a trial function 

from the expression and all of its linear independent derivatives and 

substituting them into the original ODE. The coefficients for each term 

will be a system of linear equations, which are be solved for and 

substituted, giving the solution. If any of the trial functions are linearly 

dependent on the solution to the homogeneous equation, they are multiplied 

by sufficient `x` to make them linearly independent. 

 

Examples 

======== 

 

>>> from sympy import dsolve, Function, Derivative, log 

>>> from sympy.abc import x 

>>> f = Function('f') 

>>> eq = x**2*Derivative(f(x), x, x) - 2*x*Derivative(f(x), x) + 2*f(x) - log(x) 

>>> dsolve(eq, f(x), 

... hint='nth_linear_euler_eq_nonhomogeneous_undetermined_coefficients').expand() 

Eq(f(x), C1*x + C2*x**2 + log(x)/2 + 3/4) 

 

""" 

x = func.args[0] 

f = func.func 

r = match 

 

chareq, eq, symbol = S.Zero, S.Zero, Dummy('x') 

 

for i in r.keys(): 

if not isinstance(i, str) and i >= 0: 

chareq += (r[i]*diff(x**symbol, x, i)*x**-symbol).expand() 

 

for i in range(1,degree(Poly(chareq, symbol))+1): 

eq += chareq.coeff(symbol**i)*diff(f(x), x, i) 

 

if chareq.as_coeff_add(symbol)[0]: 

eq += chareq.as_coeff_add(symbol)[0]*f(x) 

e, re = posify(r[-1].subs(x, exp(x))) 

eq += e.subs(re) 

 

match = _nth_linear_match(eq, f(x), ode_order(eq, f(x))) 

match['trialset'] = r['trialset'] 

return ode_nth_linear_constant_coeff_undetermined_coefficients(eq, func, order, match).subs(x, log(x)).subs(f(log(x)), f(x)).expand() 

 

 

def ode_nth_linear_euler_eq_nonhomogeneous_variation_of_parameters(eq, func, order, match, returns='sol'): 

r""" 

Solves an `n`\th order linear non homogeneous Cauchy-Euler equidimensional 

ordinary differential equation using variation of parameters. 

 

This is an equation with form `g(x) = a_0 f(x) + a_1 x f'(x) + a_2 x^2 f''(x) 

\cdots`. 

 

This method works by assuming that the particular solution takes the form 

 

.. math:: \sum_{x=1}^{n} c_i(x) y_i(x) {a_n} {x^n} \text{,} 

 

where `y_i` is the `i`\th solution to the homogeneous equation. The 

solution is then solved using Wronskian's and Cramer's Rule. The 

particular solution is given by multiplying eq given below with `a_n x^{n}` 

 

.. math:: \sum_{x=1}^n \left( \int \frac{W_i(x)}{W(x)} \,dx 

\right) y_i(x) \text{,} 

 

where `W(x)` is the Wronskian of the fundamental system (the system of `n` 

linearly independent solutions to the homogeneous equation), and `W_i(x)` 

is the Wronskian of the fundamental system with the `i`\th column replaced 

with `[0, 0, \cdots, 0, \frac{x^{- n}}{a_n} g{\left (x \right )}]`. 

 

This method is general enough to solve any `n`\th order inhomogeneous 

linear differential equation, but sometimes SymPy cannot simplify the 

Wronskian well enough to integrate it. If this method hangs, try using the 

``nth_linear_constant_coeff_variation_of_parameters_Integral`` hint and 

simplifying the integrals manually. Also, prefer using 

``nth_linear_constant_coeff_undetermined_coefficients`` when it 

applies, because it doesn't use integration, making it faster and more 

reliable. 

 

Warning, using simplify=False with 

'nth_linear_constant_coeff_variation_of_parameters' in 

:py:meth:`~sympy.solvers.ode.dsolve` may cause it to hang, because it will 

not attempt to simplify the Wronskian before integrating. It is 

recommended that you only use simplify=False with 

'nth_linear_constant_coeff_variation_of_parameters_Integral' for this 

method, especially if the solution to the homogeneous equation has 

trigonometric functions in it. 

 

Examples 

======== 

 

>>> from sympy import Function, dsolve, Derivative 

>>> from sympy.abc import x 

>>> f = Function('f') 

>>> eq = x**2*Derivative(f(x), x, x) - 2*x*Derivative(f(x), x) + 2*f(x) - x**4 

>>> dsolve(eq, f(x), 

... hint='nth_linear_euler_eq_nonhomogeneous_variation_of_parameters').expand() 

Eq(f(x), C1*x + C2*x**2 + x**4/6) 

 

""" 

x = func.args[0] 

f = func.func 

r = match 

 

gensol = ode_nth_linear_euler_eq_homogeneous(eq, func, order, match, returns='both') 

match.update(gensol) 

r[-1] = r[-1]/r[ode_order(eq, f(x))] 

sol = _solve_variation_of_parameters(eq, func, order, match) 

return Eq(f(x), r['sol'].rhs + (sol.rhs - r['sol'].rhs)*r[ode_order(eq, f(x))]) 

 

 

def ode_almost_linear(eq, func, order, match): 

r""" 

Solves an almost-linear differential equation. 

 

The general form of an almost linear differential equation is 

 

.. math:: f(x) g(y) y + k(x) l(y) + m(x) = 0 

\text{where} l'(y) = g(y)\text{.} 

 

This can be solved by substituting `l(y) = u(y)`. Making the given 

substitution reduces it to a linear differential equation of the form `u' 

+ P(x) u + Q(x) = 0`. 

 

The general solution is 

 

>>> from sympy import Function, dsolve, Eq, pprint 

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

>>> f, g, k, l = map(Function, ['f', 'g', 'k', 'l']) 

>>> genform = Eq(f(x)*(l(y).diff(y)) + k(x)*l(y) + g(x)) 

>>> pprint(genform) 

d 

f(x)*--(l(y)) + g(x) + k(x)*l(y) = 0 

dy 

>>> pprint(dsolve(genform, hint = 'almost_linear')) 

/ // y*k(x) \\ 

| || ------ || 

| || f(x) || -y*k(x) 

| ||-g(x)*e || -------- 

| ||-------------- for k(x) != 0|| f(x) 

l(y) = |C1 + |< k(x) ||*e 

| || || 

| || -y*g(x) || 

| || -------- otherwise || 

| || f(x) || 

\ \\ // 

 

 

See Also 

======== 

:meth:`sympy.solvers.ode.ode_1st_linear` 

 

Examples 

======== 

 

>>> from sympy import Function, Derivative, pprint 

>>> from sympy.solvers.ode import dsolve, classify_ode 

>>> from sympy.abc import x 

>>> f = Function('f') 

>>> d = f(x).diff(x) 

>>> eq = x*d + x*f(x) + 1 

>>> dsolve(eq, f(x), hint='almost_linear') 

Eq(f(x), (C1 - Ei(x))*exp(-x)) 

>>> pprint(dsolve(eq, f(x), hint='almost_linear')) 

-x 

f(x) = (C1 - Ei(x))*e 

 

References 

========== 

 

- Joel Moses, "Symbolic Integration - The Stormy Decade", Communications 

of the ACM, Volume 14, Number 8, August 1971, pp. 558 

""" 

 

# Since ode_1st_linear has already been implemented, and the 

# coefficients have been modified to the required form in 

# classify_ode, just passing eq, func, order and match to 

# ode_1st_linear will give the required output. 

return ode_1st_linear(eq, func, order, match) 

 

def _linear_coeff_match(expr, func): 

r""" 

Helper function to match hint ``linear_coefficients``. 

 

Matches the expression to the form `(a_1 x + b_1 f(x) + c_1)/(a_2 x + b_2 

f(x) + c_2)` where the following conditions hold: 

 

1. `a_1`, `b_1`, `c_1`, `a_2`, `b_2`, `c_2` are Rationals; 

2. `c_1` or `c_2` are not equal to zero; 

3. `a_2 b_1 - a_1 b_2` is not equal to zero. 

 

Return ``xarg``, ``yarg`` where 

 

1. ``xarg`` = `(b_2 c_1 - b_1 c_2)/(a_2 b_1 - a_1 b_2)` 

2. ``yarg`` = `(a_1 c_2 - a_2 c_1)/(a_2 b_1 - a_1 b_2)` 

 

 

Examples 

======== 

 

>>> from sympy import Function 

>>> from sympy.abc import x 

>>> from sympy.solvers.ode import _linear_coeff_match 

>>> from sympy.functions.elementary.trigonometric import sin 

>>> f = Function('f') 

>>> _linear_coeff_match(( 

... (-25*f(x) - 8*x + 62)/(4*f(x) + 11*x - 11)), f(x)) 

(1/9, 22/9) 

>>> _linear_coeff_match( 

... sin((-5*f(x) - 8*x + 6)/(4*f(x) + x - 1)), f(x)) 

(19/27, 2/27) 

>>> _linear_coeff_match(sin(f(x)/x), f(x)) 

 

""" 

f = func.func 

x = func.args[0] 

def abc(eq): 

r''' 

Internal function of _linear_coeff_match 

that returns Rationals a, b, c 

if eq is a*x + b*f(x) + c, else None. 

''' 

eq = _mexpand(eq) 

c = eq.as_independent(x, f(x), as_Add=True)[0] 

if not c.is_Rational: 

return 

a = eq.coeff(x) 

if not a.is_Rational: 

return 

b = eq.coeff(f(x)) 

if not b.is_Rational: 

return 

if eq == a*x + b*f(x) + c: 

return a, b, c 

 

def match(arg): 

r''' 

Internal function of _linear_coeff_match that returns Rationals a1, 

b1, c1, a2, b2, c2 and a2*b1 - a1*b2 of the expression (a1*x + b1*f(x) 

+ c1)/(a2*x + b2*f(x) + c2) if one of c1 or c2 and a2*b1 - a1*b2 is 

non-zero, else None. 

''' 

n, d = arg.together().as_numer_denom() 

m = abc(n) 

if m is not None: 

a1, b1, c1 = m 

m = abc(d) 

if m is not None: 

a2, b2, c2 = m 

d = a2*b1 - a1*b2 

if (c1 or c2) and d: 

return a1, b1, c1, a2, b2, c2, d 

 

m = [fi.args[0] for fi in expr.atoms(Function) if fi.func != f and 

len(fi.args) == 1 and not fi.args[0].is_Function] or {expr} 

m1 = match(m.pop()) 

if m1 and all(match(mi) == m1 for mi in m): 

a1, b1, c1, a2, b2, c2, denom = m1 

return (b2*c1 - b1*c2)/denom, (a1*c2 - a2*c1)/denom 

 

def ode_linear_coefficients(eq, func, order, match): 

r""" 

Solves a differential equation with linear coefficients. 

 

The general form of a differential equation with linear coefficients is 

 

.. math:: y' + F\left(\!\frac{a_1 x + b_1 y + c_1}{a_2 x + b_2 y + 

c_2}\!\right) = 0\text{,} 

 

where `a_1`, `b_1`, `c_1`, `a_2`, `b_2`, `c_2` are constants and `a_1 b_2 

- a_2 b_1 \ne 0`. 

 

This can be solved by substituting: 

 

.. math:: x = x' + \frac{b_2 c_1 - b_1 c_2}{a_2 b_1 - a_1 b_2} 

 

y = y' + \frac{a_1 c_2 - a_2 c_1}{a_2 b_1 - a_1 

b_2}\text{.} 

 

This substitution reduces the equation to a homogeneous differential 

equation. 

 

See Also 

======== 

:meth:`sympy.solvers.ode.ode_1st_homogeneous_coeff_best` 

:meth:`sympy.solvers.ode.ode_1st_homogeneous_coeff_subs_indep_div_dep` 

:meth:`sympy.solvers.ode.ode_1st_homogeneous_coeff_subs_dep_div_indep` 

 

Examples 

======== 

 

>>> from sympy import Function, Derivative, pprint 

>>> from sympy.solvers.ode import dsolve, classify_ode 

>>> from sympy.abc import x 

>>> f = Function('f') 

>>> df = f(x).diff(x) 

>>> eq = (x + f(x) + 1)*df + (f(x) - 6*x + 1) 

>>> dsolve(eq, hint='linear_coefficients') 

[Eq(f(x), -x - sqrt(C1 + 7*x**2) - 1), Eq(f(x), -x + sqrt(C1 + 7*x**2) - 1)] 

>>> pprint(dsolve(eq, hint='linear_coefficients')) 

___________ ___________ 

/ 2 / 2 

[f(x) = -x - \/ C1 + 7*x - 1, f(x) = -x + \/ C1 + 7*x - 1] 

 

 

References 

========== 

 

- Joel Moses, "Symbolic Integration - The Stormy Decade", Communications 

of the ACM, Volume 14, Number 8, August 1971, pp. 558 

""" 

 

return ode_1st_homogeneous_coeff_best(eq, func, order, match) 

 

 

def ode_separable_reduced(eq, func, order, match): 

r""" 

Solves a differential equation that can be reduced to the separable form. 

 

The general form of this equation is 

 

.. math:: y' + (y/x) H(x^n y) = 0\text{}. 

 

This can be solved by substituting `u(y) = x^n y`. The equation then 

reduces to the separable form `\frac{u'}{u (\mathrm{power} - H(u))} - 

\frac{1}{x} = 0`. 

 

The general solution is: 

 

>>> from sympy import Function, dsolve, Eq, pprint 

>>> from sympy.abc import x, n 

>>> f, g = map(Function, ['f', 'g']) 

>>> genform = f(x).diff(x) + (f(x)/x)*g(x**n*f(x)) 

>>> pprint(genform) 

/ n \ 

d f(x)*g\x *f(x)/ 

--(f(x)) + --------------- 

dx x 

>>> pprint(dsolve(genform, hint='separable_reduced')) 

n 

x *f(x) 

/ 

| 

| 1 

| ------------ dy = C1 + log(x) 

| y*(n - g(y)) 

| 

/ 

 

See Also 

======== 

:meth:`sympy.solvers.ode.ode_separable` 

 

Examples 

======== 

 

>>> from sympy import Function, Derivative, pprint 

>>> from sympy.solvers.ode import dsolve, classify_ode 

>>> from sympy.abc import x 

>>> f = Function('f') 

>>> d = f(x).diff(x) 

>>> eq = (x - x**2*f(x))*d - f(x) 

>>> dsolve(eq, hint='separable_reduced') 

[Eq(f(x), (-sqrt(C1*x**2 + 1) + 1)/x), Eq(f(x), (sqrt(C1*x**2 + 1) + 1)/x)] 

>>> pprint(dsolve(eq, hint='separable_reduced')) 

___________ ___________ 

/ 2 / 2 

- \/ C1*x + 1 + 1 \/ C1*x + 1 + 1 

[f(x) = --------------------, f(x) = ------------------] 

x x 

 

References 

========== 

 

- Joel Moses, "Symbolic Integration - The Stormy Decade", Communications 

of the ACM, Volume 14, Number 8, August 1971, pp. 558 

""" 

 

# Arguments are passed in a way so that they are coherent with the 

# ode_separable function 

x = func.args[0] 

f = func.func 

y = Dummy('y') 

u = match['u'].subs(match['t'], y) 

ycoeff = 1/(y*(match['power'] - u)) 

m1 = {y: 1, x: -1/x, 'coeff': 1} 

m2 = {y: ycoeff, x: 1, 'coeff': 1} 

r = {'m1': m1, 'm2': m2, 'y': y, 'hint': x**match['power']*f(x)} 

return ode_separable(eq, func, order, r) 

 

 

def ode_1st_power_series(eq, func, order, match): 

r""" 

The power series solution is a method which gives the Taylor series expansion 

to the solution of a differential equation. 

 

For a first order differential equation `\frac{dy}{dx} = h(x, y)`, a power 

series solution exists at a point `x = x_{0}` if `h(x, y)` is analytic at `x_{0}`. 

The solution is given by 

 

.. math:: y(x) = y(x_{0}) + \sum_{n = 1}^{\infty} \frac{F_{n}(x_{0},b)(x - x_{0})^n}{n!}, 

 

where `y(x_{0}) = b` is the value of y at the initial value of `x_{0}`. 

To compute the values of the `F_{n}(x_{0},b)` the following algorithm is 

followed, until the required number of terms are generated. 

 

1. `F_1 = h(x_{0}, b)` 

2. `F_{n+1} = \frac{\partial F_{n}}{\partial x} + \frac{\partial F_{n}}{\partial y}F_{1}` 

 

Examples 

======== 

 

>>> from sympy import Function, Derivative, pprint, exp 

>>> from sympy.solvers.ode import dsolve 

>>> from sympy.abc import x 

>>> f = Function('f') 

>>> eq = exp(x)*(f(x).diff(x)) - f(x) 

>>> pprint(dsolve(eq, hint='1st_power_series')) 

3 4 5 

C1*x C1*x C1*x / 6\ 

f(x) = C1 + C1*x - ----- + ----- + ----- + O\x / 

6 24 60 

 

 

References 

========== 

 

- Travis W. Walker, Analytic power series technique for solving first-order 

differential equations, p.p 17, 18 

 

""" 

x = func.args[0] 

y = match['y'] 

f = func.func 

h = -match[match['d']]/match[match['e']] 

point = match.get('f0') 

value = match.get('f0val') 

terms = match.get('terms') 

 

# First term 

F = h 

if not h: 

return Eq(f(x), value) 

 

# Initialization 

series = value 

if terms > 1: 

hc = h.subs({x: point, y: value}) 

if hc.has(oo) or hc.has(NaN) or hc.has(zoo): 

# Derivative does not exist, not analytic 

return Eq(f(x), oo) 

elif hc: 

series += hc*(x - point) 

 

for factcount in range(2, terms): 

Fnew = F.diff(x) + F.diff(y)*h 

Fnewc = Fnew.subs({x: point, y: value}) 

# Same logic as above 

if Fnewc.has(oo) or Fnewc.has(NaN) or Fnewc.has(-oo) or Fnewc.has(zoo): 

return Eq(f(x), oo) 

series += Fnewc*((x - point)**factcount)/factorial(factcount) 

F = Fnew 

series += Order(x**terms) 

return Eq(f(x), series) 

 

 

def ode_nth_linear_constant_coeff_homogeneous(eq, func, order, match, 

returns='sol'): 

r""" 

Solves an `n`\th order linear homogeneous differential equation with 

constant coefficients. 

 

This is an equation of the form 

 

.. math:: a_n f^{(n)}(x) + a_{n-1} f^{(n-1)}(x) + \cdots + a_1 f'(x) 

+ a_0 f(x) = 0\text{.} 

 

These equations can be solved in a general manner, by taking the roots of 

the characteristic equation `a_n m^n + a_{n-1} m^{n-1} + \cdots + a_1 m + 

a_0 = 0`. The solution will then be the sum of `C_n x^i e^{r x}` terms, 

for each where `C_n` is an arbitrary constant, `r` is a root of the 

characteristic equation and `i` is one of each from 0 to the multiplicity 

of the root - 1 (for example, a root 3 of multiplicity 2 would create the 

terms `C_1 e^{3 x} + C_2 x e^{3 x}`). The exponential is usually expanded 

for complex roots using Euler's equation `e^{I x} = \cos(x) + I \sin(x)`. 

Complex roots always come in conjugate pairs in polynomials with real 

coefficients, so the two roots will be represented (after simplifying the 

constants) as `e^{a x} \left(C_1 \cos(b x) + C_2 \sin(b x)\right)`. 

 

If SymPy cannot find exact roots to the characteristic equation, a 

:py:class:`~sympy.polys.rootoftools.CRootOf` instance will be return 

instead. 

 

>>> from sympy import Function, dsolve, Eq 

>>> from sympy.abc import x 

>>> f = Function('f') 

>>> dsolve(f(x).diff(x, 5) + 10*f(x).diff(x) - 2*f(x), f(x), 

... hint='nth_linear_constant_coeff_homogeneous') 

... # doctest: +NORMALIZE_WHITESPACE 

Eq(f(x), C1*exp(x*CRootOf(_x**5 + 10*_x - 2, 0)) + 

C2*exp(x*CRootOf(_x**5 + 10*_x - 2, 1)) + 

C3*exp(x*CRootOf(_x**5 + 10*_x - 2, 2)) + 

C4*exp(x*CRootOf(_x**5 + 10*_x - 2, 3)) + 

C5*exp(x*CRootOf(_x**5 + 10*_x - 2, 4))) 

 

Note that because this method does not involve integration, there is no 

``nth_linear_constant_coeff_homogeneous_Integral`` hint. 

 

The following is for internal use: 

 

- ``returns = 'sol'`` returns the solution to the ODE. 

- ``returns = 'list'`` returns a list of linearly independent solutions, 

for use with non homogeneous solution methods like variation of 

parameters and undetermined coefficients. Note that, though the 

solutions should be linearly independent, this function does not 

explicitly check that. You can do ``assert simplify(wronskian(sollist)) 

!= 0`` to check for linear independence. Also, ``assert len(sollist) == 

order`` will need to pass. 

- ``returns = 'both'``, return a dictionary ``{'sol': <solution to ODE>, 

'list': <list of linearly independent solutions>}``. 

 

Examples 

======== 

 

>>> from sympy import Function, dsolve, pprint 

>>> from sympy.abc import x 

>>> f = Function('f') 

>>> pprint(dsolve(f(x).diff(x, 4) + 2*f(x).diff(x, 3) - 

... 2*f(x).diff(x, 2) - 6*f(x).diff(x) + 5*f(x), f(x), 

... hint='nth_linear_constant_coeff_homogeneous')) 

x -2*x 

f(x) = (C1 + C2*x)*e + (C3*sin(x) + C4*cos(x))*e 

 

References 

========== 

 

- http://en.wikipedia.org/wiki/Linear_differential_equation section: 

Nonhomogeneous_equation_with_constant_coefficients 

- M. Tenenbaum & H. Pollard, "Ordinary Differential Equations", 

Dover 1963, pp. 211 

 

# indirect doctest 

 

""" 

x = func.args[0] 

f = func.func 

r = match 

 

# First, set up characteristic equation. 

chareq, symbol = S.Zero, Dummy('x') 

 

for i in r.keys(): 

if type(i) == str or i < 0: 

pass 

else: 

chareq += r[i]*symbol**i 

 

chareq = Poly(chareq, symbol) 

chareqroots = [rootof(chareq, k) for k in range(chareq.degree())] 

chareq_is_complex = not all([i.is_real for i in chareq.all_coeffs()]) 

 

# A generator of constants 

constants = list(get_numbered_constants(eq, num=chareq.degree()*2)) 

 

# Create a dict root: multiplicity or charroots 

charroots = defaultdict(int) 

for root in chareqroots: 

charroots[root] += 1 

gsol = S(0) 

# We need to keep track of terms so we can run collect() at the end. 

# This is necessary for constantsimp to work properly. 

global collectterms 

collectterms = [] 

gensols = [] 

conjugate_roots = [] # used to prevent double-use of conjugate roots 

for root, multiplicity in charroots.items(): 

for i in range(multiplicity): 

if isinstance(root, RootOf): 

gensols.append(exp(root*x)) 

if multiplicity != 1: 

raise ValueError("Value should be 1") 

# This ordering is important 

collectterms = [(0, root, 0)] + collectterms 

else: 

if chareq_is_complex: 

gensols.append(x**i*exp(root*x)) 

collectterms = [(i, root, 0)] + collectterms 

continue 

reroot = re(root) 

imroot = im(root) 

if imroot.has(atan2) and reroot.has(atan2): 

# Remove this condition when re and im stop returning 

# circular atan2 usages. 

gensols.append(x**i*exp(root*x)) 

collectterms = [(i, root, 0)] + collectterms 

else: 

if root in conjugate_roots: 

collectterms = [(i, reroot, imroot)] + collectterms 

continue 

if imroot == 0: 

gensols.append(x**i*exp(reroot*x)) 

collectterms = [(i, reroot, 0)] + collectterms 

continue 

conjugate_roots.append(conjugate(root)) 

gensols.append(x**i*exp(reroot*x) * sin(abs(imroot) * x)) 

gensols.append(x**i*exp(reroot*x) * cos( imroot * x)) 

 

# This ordering is important 

collectterms = [(i, reroot, imroot)] + collectterms 

if returns == 'list': 

return gensols 

elif returns in ('sol' 'both'): 

gsol = Add(*[i*j for (i,j) in zip(constants, gensols)]) 

if returns == 'sol': 

return Eq(f(x), gsol) 

else: 

return {'sol': Eq(f(x), gsol), 'list': gensols} 

else: 

raise ValueError('Unknown value for key "returns".') 

 

 

def ode_nth_linear_constant_coeff_undetermined_coefficients(eq, func, order, match): 

r""" 

Solves an `n`\th order linear differential equation with constant 

coefficients using the method of undetermined coefficients. 

 

This method works on differential equations of the form 

 

.. math:: a_n f^{(n)}(x) + a_{n-1} f^{(n-1)}(x) + \cdots + a_1 f'(x) 

+ a_0 f(x) = P(x)\text{,} 

 

where `P(x)` is a function that has a finite number of linearly 

independent derivatives. 

 

Functions that fit this requirement are finite sums functions of the form 

`a x^i e^{b x} \sin(c x + d)` or `a x^i e^{b x} \cos(c x + d)`, where `i` 

is a non-negative integer and `a`, `b`, `c`, and `d` are constants. For 

example any polynomial in `x`, functions like `x^2 e^{2 x}`, `x \sin(x)`, 

and `e^x \cos(x)` can all be used. Products of `\sin`'s and `\cos`'s have 

a finite number of derivatives, because they can be expanded into `\sin(a 

x)` and `\cos(b x)` terms. However, SymPy currently cannot do that 

expansion, so you will need to manually rewrite the expression in terms of 

the above to use this method. So, for example, you will need to manually 

convert `\sin^2(x)` into `(1 + \cos(2 x))/2` to properly apply the method 

of undetermined coefficients on it. 

 

This method works by creating a trial function from the expression and all 

of its linear independent derivatives and substituting them into the 

original ODE. The coefficients for each term will be a system of linear 

equations, which are be solved for and substituted, giving the solution. 

If any of the trial functions are linearly dependent on the solution to 

the homogeneous equation, they are multiplied by sufficient `x` to make 

them linearly independent. 

 

Examples 

======== 

 

>>> from sympy import Function, dsolve, pprint, exp, cos 

>>> from sympy.abc import x 

>>> f = Function('f') 

>>> pprint(dsolve(f(x).diff(x, 2) + 2*f(x).diff(x) + f(x) - 

... 4*exp(-x)*x**2 + cos(2*x), f(x), 

... hint='nth_linear_constant_coeff_undetermined_coefficients')) 

/ 4\ 

| x | -x 4*sin(2*x) 3*cos(2*x) 

f(x) = |C1 + C2*x + --|*e - ---------- + ---------- 

\ 3 / 25 25 

 

References 

========== 

 

- http://en.wikipedia.org/wiki/Method_of_undetermined_coefficients 

- M. Tenenbaum & H. Pollard, "Ordinary Differential Equations", 

Dover 1963, pp. 221 

 

# indirect doctest 

 

""" 

gensol = ode_nth_linear_constant_coeff_homogeneous(eq, func, order, match, 

returns='both') 

match.update(gensol) 

return _solve_undetermined_coefficients(eq, func, order, match) 

 

 

def _solve_undetermined_coefficients(eq, func, order, match): 

r""" 

Helper function for the method of undetermined coefficients. 

 

See the 

:py:meth:`~sympy.solvers.ode.ode_nth_linear_constant_coeff_undetermined_coefficients` 

docstring for more information on this method. 

 

The parameter ``match`` should be a dictionary that has the following 

keys: 

 

``list`` 

A list of solutions to the homogeneous equation, such as the list 

returned by 

``ode_nth_linear_constant_coeff_homogeneous(returns='list')``. 

 

``sol`` 

The general solution, such as the solution returned by 

``ode_nth_linear_constant_coeff_homogeneous(returns='sol')``. 

 

``trialset`` 

The set of trial functions as returned by 

``_undetermined_coefficients_match()['trialset']``. 

 

""" 

x = func.args[0] 

f = func.func 

r = match 

coeffs = numbered_symbols('a', cls=Dummy) 

coefflist = [] 

gensols = r['list'] 

gsol = r['sol'] 

trialset = r['trialset'] 

notneedset = set([]) 

newtrialset = set([]) 

global collectterms 

if len(gensols) != order: 

raise NotImplementedError("Cannot find " + str(order) + 

" solutions to the homogeneous equation necessary to apply" + 

" undetermined coefficients to " + str(eq) + 

" (number of terms != order)") 

usedsin = set([]) 

mult = 0 # The multiplicity of the root 

getmult = True 

for i, reroot, imroot in collectterms: 

if getmult: 

mult = i + 1 

getmult = False 

if i == 0: 

getmult = True 

if imroot: 

# Alternate between sin and cos 

if (i, reroot) in usedsin: 

check = x**i*exp(reroot*x)*cos(imroot*x) 

else: 

check = x**i*exp(reroot*x)*sin(abs(imroot)*x) 

usedsin.add((i, reroot)) 

else: 

check = x**i*exp(reroot*x) 

 

if check in trialset: 

# If an element of the trial function is already part of the 

# homogeneous solution, we need to multiply by sufficient x to 

# make it linearly independent. We also don't need to bother 

# checking for the coefficients on those elements, since we 

# already know it will be 0. 

while True: 

if check*x**mult in trialset: 

mult += 1 

else: 

break 

trialset.add(check*x**mult) 

notneedset.add(check) 

 

newtrialset = trialset - notneedset 

 

trialfunc = 0 

for i in newtrialset: 

c = next(coeffs) 

coefflist.append(c) 

trialfunc += c*i 

 

eqs = sub_func_doit(eq, f(x), trialfunc) 

 

coeffsdict = dict(list(zip(trialset, [0]*(len(trialset) + 1)))) 

 

eqs = _mexpand(eqs) 

 

for i in Add.make_args(eqs): 

s = separatevars(i, dict=True, symbols=[x]) 

coeffsdict[s[x]] += s['coeff'] 

 

coeffvals = solve(list(coeffsdict.values()), coefflist) 

 

if not coeffvals: 

raise NotImplementedError( 

"Could not solve `%s` using the " 

"method of undetermined coefficients " 

"(unable to solve for coefficients)." % eq) 

 

psol = trialfunc.subs(coeffvals) 

 

return Eq(f(x), gsol.rhs + psol) 

 

 

def _undetermined_coefficients_match(expr, x): 

r""" 

Returns a trial function match if undetermined coefficients can be applied 

to ``expr``, and ``None`` otherwise. 

 

A trial expression can be found for an expression for use with the method 

of undetermined coefficients if the expression is an 

additive/multiplicative combination of constants, polynomials in `x` (the 

independent variable of expr), `\sin(a x + b)`, `\cos(a x + b)`, and 

`e^{a x}` terms (in other words, it has a finite number of linearly 

independent derivatives). 

 

Note that you may still need to multiply each term returned here by 

sufficient `x` to make it linearly independent with the solutions to the 

homogeneous equation. 

 

This is intended for internal use by ``undetermined_coefficients`` hints. 

 

SymPy currently has no way to convert `\sin^n(x) \cos^m(y)` into a sum of 

only `\sin(a x)` and `\cos(b x)` terms, so these are not implemented. So, 

for example, you will need to manually convert `\sin^2(x)` into `[1 + 

\cos(2 x)]/2` to properly apply the method of undetermined coefficients on 

it. 

 

Examples 

======== 

 

>>> from sympy import log, exp 

>>> from sympy.solvers.ode import _undetermined_coefficients_match 

>>> from sympy.abc import x 

>>> _undetermined_coefficients_match(9*x*exp(x) + exp(-x), x) 

{'test': True, 'trialset': {x*exp(x), exp(-x), exp(x)}} 

>>> _undetermined_coefficients_match(log(x), x) 

{'test': False} 

 

""" 

a = Wild('a', exclude=[x]) 

b = Wild('b', exclude=[x]) 

expr = powsimp(expr, combine='exp') # exp(x)*exp(2*x + 1) => exp(3*x + 1) 

retdict = {} 

 

def _test_term(expr, x): 

r""" 

Test if ``expr`` fits the proper form for undetermined coefficients. 

""" 

if expr.is_Add: 

return all(_test_term(i, x) for i in expr.args) 

elif expr.is_Mul: 

if expr.has(sin, cos): 

foundtrig = False 

# Make sure that there is only one trig function in the args. 

# See the docstring. 

for i in expr.args: 

if i.has(sin, cos): 

if foundtrig: 

return False 

else: 

foundtrig = True 

return all(_test_term(i, x) for i in expr.args) 

elif expr.is_Function: 

if expr.func in (sin, cos, exp): 

if expr.args[0].match(a*x + b): 

return True 

else: 

return False 

else: 

return False 

elif expr.is_Pow and expr.base.is_Symbol and expr.exp.is_Integer and \ 

expr.exp >= 0: 

return True 

elif expr.is_Pow and expr.base.is_number: 

if expr.exp.match(a*x + b): 

return True 

else: 

return False 

elif expr.is_Symbol or expr.is_number: 

return True 

else: 

return False 

 

def _get_trial_set(expr, x, exprs=set([])): 

r""" 

Returns a set of trial terms for undetermined coefficients. 

 

The idea behind undetermined coefficients is that the terms expression 

repeat themselves after a finite number of derivatives, except for the 

coefficients (they are linearly dependent). So if we collect these, 

we should have the terms of our trial function. 

""" 

def _remove_coefficient(expr, x): 

r""" 

Returns the expression without a coefficient. 

 

Similar to expr.as_independent(x)[1], except it only works 

multiplicatively. 

""" 

term = S.One 

if expr.is_Mul: 

for i in expr.args: 

if i.has(x): 

term *= i 

elif expr.has(x): 

term = expr 

return term 

 

expr = expand_mul(expr) 

if expr.is_Add: 

for term in expr.args: 

if _remove_coefficient(term, x) in exprs: 

pass 

else: 

exprs.add(_remove_coefficient(term, x)) 

exprs = exprs.union(_get_trial_set(term, x, exprs)) 

else: 

term = _remove_coefficient(expr, x) 

tmpset = exprs.union({term}) 

oldset = set([]) 

while tmpset != oldset: 

# If you get stuck in this loop, then _test_term is probably 

# broken 

oldset = tmpset.copy() 

expr = expr.diff(x) 

term = _remove_coefficient(expr, x) 

if term.is_Add: 

tmpset = tmpset.union(_get_trial_set(term, x, tmpset)) 

else: 

tmpset.add(term) 

exprs = tmpset 

return exprs 

 

retdict['test'] = _test_term(expr, x) 

if retdict['test']: 

# Try to generate a list of trial solutions that will have the 

# undetermined coefficients. Note that if any of these are not linearly 

# independent with any of the solutions to the homogeneous equation, 

# then they will need to be multiplied by sufficient x to make them so. 

# This function DOES NOT do that (it doesn't even look at the 

# homogeneous equation). 

retdict['trialset'] = _get_trial_set(expr, x) 

 

return retdict 

 

 

def ode_nth_linear_constant_coeff_variation_of_parameters(eq, func, order, match): 

r""" 

Solves an `n`\th order linear differential equation with constant 

coefficients using the method of variation of parameters. 

 

This method works on any differential equations of the form 

 

.. math:: f^{(n)}(x) + a_{n-1} f^{(n-1)}(x) + \cdots + a_1 f'(x) + a_0 

f(x) = P(x)\text{.} 

 

This method works by assuming that the particular solution takes the form 

 

.. math:: \sum_{x=1}^{n} c_i(x) y_i(x)\text{,} 

 

where `y_i` is the `i`\th solution to the homogeneous equation. The 

solution is then solved using Wronskian's and Cramer's Rule. The 

particular solution is given by 

 

.. math:: \sum_{x=1}^n \left( \int \frac{W_i(x)}{W(x)} \,dx 

\right) y_i(x) \text{,} 

 

where `W(x)` is the Wronskian of the fundamental system (the system of `n` 

linearly independent solutions to the homogeneous equation), and `W_i(x)` 

is the Wronskian of the fundamental system with the `i`\th column replaced 

with `[0, 0, \cdots, 0, P(x)]`. 

 

This method is general enough to solve any `n`\th order inhomogeneous 

linear differential equation with constant coefficients, but sometimes 

SymPy cannot simplify the Wronskian well enough to integrate it. If this 

method hangs, try using the 

``nth_linear_constant_coeff_variation_of_parameters_Integral`` hint and 

simplifying the integrals manually. Also, prefer using 

``nth_linear_constant_coeff_undetermined_coefficients`` when it 

applies, because it doesn't use integration, making it faster and more 

reliable. 

 

Warning, using simplify=False with 

'nth_linear_constant_coeff_variation_of_parameters' in 

:py:meth:`~sympy.solvers.ode.dsolve` may cause it to hang, because it will 

not attempt to simplify the Wronskian before integrating. It is 

recommended that you only use simplify=False with 

'nth_linear_constant_coeff_variation_of_parameters_Integral' for this 

method, especially if the solution to the homogeneous equation has 

trigonometric functions in it. 

 

Examples 

======== 

 

>>> from sympy import Function, dsolve, pprint, exp, log 

>>> from sympy.abc import x 

>>> f = Function('f') 

>>> pprint(dsolve(f(x).diff(x, 3) - 3*f(x).diff(x, 2) + 

... 3*f(x).diff(x) - f(x) - exp(x)*log(x), f(x), 

... hint='nth_linear_constant_coeff_variation_of_parameters')) 

/ 3 \ 

| 2 x *(6*log(x) - 11)| x 

f(x) = |C1 + C2*x + C3*x + ------------------|*e 

\ 36 / 

 

References 

========== 

 

- http://en.wikipedia.org/wiki/Variation_of_parameters 

- http://planetmath.org/VariationOfParameters 

- M. Tenenbaum & H. Pollard, "Ordinary Differential Equations", 

Dover 1963, pp. 233 

 

# indirect doctest 

 

""" 

 

gensol = ode_nth_linear_constant_coeff_homogeneous(eq, func, order, match, 

returns='both') 

match.update(gensol) 

return _solve_variation_of_parameters(eq, func, order, match) 

 

 

def _solve_variation_of_parameters(eq, func, order, match): 

r""" 

Helper function for the method of variation of parameters and nonhomogeneous euler eq. 

 

See the 

:py:meth:`~sympy.solvers.ode.ode_nth_linear_constant_coeff_variation_of_parameters` 

docstring for more information on this method. 

 

The parameter ``match`` should be a dictionary that has the following 

keys: 

 

``list`` 

A list of solutions to the homogeneous equation, such as the list 

returned by 

``ode_nth_linear_constant_coeff_homogeneous(returns='list')``. 

 

``sol`` 

The general solution, such as the solution returned by 

``ode_nth_linear_constant_coeff_homogeneous(returns='sol')``. 

 

""" 

 

x = func.args[0] 

f = func.func 

r = match 

psol = 0 

gensols = r['list'] 

gsol = r['sol'] 

wr = wronskian(gensols, x) 

 

if r.get('simplify', True): 

wr = simplify(wr) # We need much better simplification for 

# some ODEs. See issue 4662, for example. 

 

# To reduce commonly occurring sin(x)**2 + cos(x)**2 to 1 

wr = trigsimp(wr, deep=True, recursive=True) 

if not wr: 

# The wronskian will be 0 iff the solutions are not linearly 

# independent. 

raise NotImplementedError("Cannot find " + str(order) + 

" solutions to the homogeneous equation necessary to apply " + 

"variation of parameters to " + str(eq) + " (Wronskian == 0)") 

if len(gensols) != order: 

raise NotImplementedError("Cannot find " + str(order) + 

" solutions to the homogeneous equation necessary to apply " + 

"variation of parameters to " + 

str(eq) + " (number of terms != order)") 

negoneterm = (-1)**(order) 

for i in gensols: 

psol += negoneterm*Integral(wronskian([sol for sol in gensols if sol != i], x)*r[-1]/wr, x)*i/r[order] 

negoneterm *= -1 

 

if r.get('simplify', True): 

psol = simplify(psol) 

psol = trigsimp(psol, deep=True) 

return Eq(f(x), gsol.rhs + psol) 

 

 

def ode_separable(eq, func, order, match): 

r""" 

Solves separable 1st order differential equations. 

 

This is any differential equation that can be written as `P(y) 

\tfrac{dy}{dx} = Q(x)`. The solution can then just be found by 

rearranging terms and integrating: `\int P(y) \,dy = \int Q(x) \,dx`. 

This hint uses :py:meth:`sympy.simplify.simplify.separatevars` as its back 

end, so if a separable equation is not caught by this solver, it is most 

likely the fault of that function. 

:py:meth:`~sympy.simplify.simplify.separatevars` is 

smart enough to do most expansion and factoring necessary to convert a 

separable equation `F(x, y)` into the proper form `P(x)\cdot{}Q(y)`. The 

general solution is:: 

 

>>> from sympy import Function, dsolve, Eq, pprint 

>>> from sympy.abc import x 

>>> a, b, c, d, f = map(Function, ['a', 'b', 'c', 'd', 'f']) 

>>> genform = Eq(a(x)*b(f(x))*f(x).diff(x), c(x)*d(f(x))) 

>>> pprint(genform) 

d 

a(x)*b(f(x))*--(f(x)) = c(x)*d(f(x)) 

dx 

>>> pprint(dsolve(genform, f(x), hint='separable_Integral')) 

f(x) 

/ / 

| | 

| b(y) | c(x) 

| ---- dy = C1 + | ---- dx 

| d(y) | a(x) 

| | 

/ / 

 

Examples 

======== 

 

>>> from sympy import Function, dsolve, Eq 

>>> from sympy.abc import x 

>>> f = Function('f') 

>>> pprint(dsolve(Eq(f(x)*f(x).diff(x) + x, 3*x*f(x)**2), f(x), 

... hint='separable', simplify=False)) 

/ 2 \ 2 

log\3*f (x) - 1/ x 

---------------- = C1 + -- 

6 2 

 

References 

========== 

 

- M. Tenenbaum & H. Pollard, "Ordinary Differential Equations", 

Dover 1963, pp. 52 

 

# indirect doctest 

 

""" 

x = func.args[0] 

f = func.func 

C1 = get_numbered_constants(eq, num=1) 

r = match # {'m1':m1, 'm2':m2, 'y':y} 

u = r.get('hint', f(x)) # get u from separable_reduced else get f(x) 

return Eq(Integral(r['m2']['coeff']*r['m2'][r['y']]/r['m1'][r['y']], 

(r['y'], None, u)), Integral(-r['m1']['coeff']*r['m1'][x]/ 

r['m2'][x], x) + C1) 

 

 

def checkinfsol(eq, infinitesimals, func=None, order=None): 

r""" 

This function is used to check if the given infinitesimals are the 

actual infinitesimals of the given first order differential equation. 

This method is specific to the Lie Group Solver of ODEs. 

 

As of now, it simply checks, by substituting the infinitesimals in the 

partial differential equation. 

 

 

.. math:: \frac{\partial \eta}{\partial x} + \left(\frac{\partial \eta}{\partial y} 

- \frac{\partial \xi}{\partial x}\right)*h 

- \frac{\partial \xi}{\partial y}*h^{2} 

- \xi\frac{\partial h}{\partial x} - \eta\frac{\partial h}{\partial y} = 0 

 

 

where `\eta`, and `\xi` are the infinitesimals and `h(x,y) = \frac{dy}{dx}` 

 

The infinitesimals should be given in the form of a list of dicts 

``[{xi(x, y): inf, eta(x, y): inf}]``, corresponding to the 

output of the function infinitesimals. It returns a list 

of values of the form ``[(True/False, sol)]`` where ``sol`` is the value 

obtained after substituting the infinitesimals in the PDE. If it 

is ``True``, then ``sol`` would be 0. 

 

""" 

if isinstance(eq, Equality): 

eq = eq.lhs - eq.rhs 

if not func: 

eq, func = _preprocess(eq) 

variables = func.args 

if len(variables) != 1: 

raise ValueError("ODE's have only one independent variable") 

else: 

x = variables[0] 

if not order: 

order = ode_order(eq, func) 

if order != 1: 

raise NotImplementedError("Lie groups solver has been implemented " 

"only for first order differential equations") 

else: 

df = func.diff(x) 

a = Wild('a', exclude = [df]) 

b = Wild('b', exclude = [df]) 

match = collect(expand(eq), df).match(a*df + b) 

 

if match: 

h = -simplify(match[b]/match[a]) 

else: 

try: 

sol = solve(eq, df) 

except NotImplementedError: 

raise NotImplementedError("Infinitesimals for the " 

"first order ODE could not be found") 

else: 

h = sol[0] # Find infinitesimals for one solution 

 

y = Dummy('y') 

h = h.subs(func, y) 

xi = Function('xi')(x, y) 

eta = Function('eta')(x, y) 

dxi = Function('xi')(x, func) 

deta = Function('eta')(x, func) 

pde = (eta.diff(x) + (eta.diff(y) - xi.diff(x))*h - 

(xi.diff(y))*h**2 - xi*(h.diff(x)) - eta*(h.diff(y))) 

soltup = [] 

for sol in infinitesimals: 

tsol = {xi: S(sol[dxi]).subs(func, y), 

eta: S(sol[deta]).subs(func, y)} 

sol = simplify(pde.subs(tsol).doit()) 

if sol: 

soltup.append((False, sol.subs(y, func))) 

else: 

soltup.append((True, 0)) 

return soltup 

 

def ode_lie_group(eq, func, order, match): 

r""" 

This hint implements the Lie group method of solving first order differential 

equations. The aim is to convert the given differential equation from the 

given coordinate given system into another coordinate system where it becomes 

invariant under the one-parameter Lie group of translations. The converted ODE is 

quadrature and can be solved easily. It makes use of the 

:py:meth:`sympy.solvers.ode.infinitesimals` function which returns the 

infinitesimals of the transformation. 

 

The coordinates `r` and `s` can be found by solving the following Partial 

Differential Equations. 

 

.. math :: \xi\frac{\partial r}{\partial x} + \eta\frac{\partial r}{\partial y} 

= 0 

 

.. math :: \xi\frac{\partial s}{\partial x} + \eta\frac{\partial s}{\partial y} 

= 1 

 

The differential equation becomes separable in the new coordinate system 

 

.. math :: \frac{ds}{dr} = \frac{\frac{\partial s}{\partial x} + 

h(x, y)\frac{\partial s}{\partial y}}{ 

\frac{\partial r}{\partial x} + h(x, y)\frac{\partial r}{\partial y}} 

 

After finding the solution by integration, it is then converted back to the original 

coordinate system by substituting `r` and `s` in terms of `x` and `y` again. 

 

Examples 

======== 

 

>>> from sympy import Function, dsolve, Eq, exp, pprint 

>>> from sympy.abc import x 

>>> f = Function('f') 

>>> pprint(dsolve(f(x).diff(x) + 2*x*f(x) - x*exp(-x**2), f(x), 

... hint='lie_group')) 

/ 2\ 2 

| x | -x 

f(x) = |C1 + --|*e 

\ 2 / 

 

 

References 

========== 

 

- Solving differential equations by Symmetry Groups, 

John Starrett, pp. 1 - pp. 14 

 

""" 

 

heuristics = lie_heuristics 

inf = {} 

f = func.func 

x = func.args[0] 

df = func.diff(x) 

xi = Function("xi") 

eta = Function("eta") 

a = Wild('a', exclude = [df]) 

b = Wild('b', exclude = [df]) 

xis = match.pop('xi') 

etas = match.pop('eta') 

 

if match: 

h = -simplify(match[match['d']]/match[match['e']]) 

y = match['y'] 

else: 

try: 

sol = solve(eq, df) 

except NotImplementedError: 

raise NotImplementedError("Unable to solve the differential equation " + 

str(eq) + " by the lie group method") 

else: 

y = Dummy("y") 

h = sol[0].subs(func, y) 

 

if xis is not None and etas is not None: 

inf = [{xi(x, f(x)): S(xis), eta(x, f(x)): S(etas)}] 

 

if not checkinfsol(eq, inf, func=f(x), order=1)[0][0]: 

raise ValueError("The given infinitesimals xi and eta" 

" are not the infinitesimals to the given equation") 

else: 

heuristics = ["user_defined"] 

 

match = {'h': h, 'y': y} 

 

# This is done so that if: 

# a] solve raises a NotImplementedError. 

# b] any heuristic raises a ValueError 

# another heuristic can be used. 

tempsol = [] # Used by solve below 

for heuristic in heuristics: 

try: 

if not inf: 

inf = infinitesimals(eq, hint=heuristic, func=func, order=1, match=match) 

except ValueError: 

continue 

else: 

for infsim in inf: 

xiinf = (infsim[xi(x, func)]).subs(func, y) 

etainf = (infsim[eta(x, func)]).subs(func, y) 

# This condition creates recursion while using pdsolve. 

# Since the first step while solving a PDE of form 

# a*(f(x, y).diff(x)) + b*(f(x, y).diff(y)) + c = 0 

# is to solve the ODE dy/dx = b/a 

if simplify(etainf/xiinf) == h: 

continue 

rpde = f(x, y).diff(x)*xiinf + f(x, y).diff(y)*etainf 

r = pdsolve(rpde, func=f(x, y)).rhs 

s = pdsolve(rpde - 1, func=f(x, y)).rhs 

newcoord = [_lie_group_remove(coord) for coord in [r, s]] 

r = Dummy("r") 

s = Dummy("s") 

C1 = Symbol("C1") 

rcoord = newcoord[0] 

scoord = newcoord[-1] 

try: 

sol = solve([r - rcoord, s - scoord], x, y, dict=True) 

except NotImplementedError: 

continue 

else: 

sol = sol[0] 

xsub = sol[x] 

ysub = sol[y] 

num = simplify(scoord.diff(x) + scoord.diff(y)*h) 

denom = simplify(rcoord.diff(x) + rcoord.diff(y)*h) 

if num and denom: 

diffeq = simplify((num/denom).subs([(x, xsub), (y, ysub)])) 

sep = separatevars(diffeq, symbols=[r, s], dict=True) 

if sep: 

# Trying to separate, r and s coordinates 

deq = integrate((1/sep[s]), s) + C1 - integrate(sep['coeff']*sep[r], r) 

# Substituting and reverting back to original coordinates 

deq = deq.subs([(r, rcoord), (s, scoord)]) 

try: 

sdeq = solve(deq, y) 

except NotImplementedError: 

tempsol.append(deq) 

else: 

if len(sdeq) == 1: 

return Eq(f(x), sdeq.pop()) 

else: 

return [Eq(f(x), sol) for sol in sdeq] 

 

 

elif denom: # (ds/dr) is zero which means s is constant 

return Eq(f(x), solve(scoord - C1, y)[0]) 

 

elif num: # (dr/ds) is zero which means r is constant 

return Eq(f(x), solve(rcoord - C1, y)[0]) 

 

# If nothing works, return solution as it is, without solving for y 

if tempsol: 

if len(tempsol) == 1: 

return Eq(tempsol.pop().subs(y, f(x)), 0) 

else: 

return [Eq(sol.subs(y, f(x)), 0) for sol in tempsol] 

 

raise NotImplementedError("The given ODE " + str(eq) + " cannot be solved by" 

+ " the lie group method") 

 

 

def _lie_group_remove(coords): 

r""" 

This function is strictly meant for internal use by the Lie group ODE solving 

method. It replaces arbitrary functions returned by pdsolve with either 0 or 1 or the 

args of the arbitrary function. 

 

The algorithm used is: 

1] If coords is an instance of an Undefined Function, then the args are returned 

2] If the arbitrary function is present in an Add object, it is replaced by zero. 

3] If the arbitrary function is present in an Mul object, it is replaced by one. 

4] If coords has no Undefined Function, it is returned as it is. 

 

Examples 

======== 

 

>>> from sympy.solvers.ode import _lie_group_remove 

>>> from sympy import Function 

>>> from sympy.abc import x, y 

>>> F = Function("F") 

>>> eq = x**2*y 

>>> _lie_group_remove(eq) 

x**2*y 

>>> eq = F(x**2*y) 

>>> _lie_group_remove(eq) 

x**2*y 

>>> eq = y**2*x + F(x**3) 

>>> _lie_group_remove(eq) 

x*y**2 

>>> eq = (F(x**3) + y)*x**4 

>>> _lie_group_remove(eq) 

x**4*y 

 

""" 

if isinstance(coords, AppliedUndef): 

return coords.args[0] 

elif coords.is_Add: 

subfunc = coords.atoms(AppliedUndef) 

if subfunc: 

for func in subfunc: 

coords = coords.subs(func, 0) 

return coords 

elif coords.is_Pow: 

base, expr = coords.as_base_exp() 

base = _lie_group_remove(base) 

expr = _lie_group_remove(expr) 

return base**expr 

elif coords.is_Mul: 

mulargs = [] 

coordargs = coords.args 

for arg in coordargs: 

if not isinstance(coords, AppliedUndef): 

mulargs.append(_lie_group_remove(arg)) 

return Mul(*mulargs) 

return coords 

 

def infinitesimals(eq, func=None, order=None, hint='default', match=None): 

r""" 

The infinitesimal functions of an ordinary differential equation, `\xi(x,y)` 

and `\eta(x,y)`, are the infinitesimals of the Lie group of point transformations 

for which the differential equation is invariant. So, the ODE `y'=f(x,y)` 

would admit a Lie group `x^*=X(x,y;\varepsilon)=x+\varepsilon\xi(x,y)`, 

`y^*=Y(x,y;\varepsilon)=y+\varepsilon\eta(x,y)` such that `(y^*)'=f(x^*, y^*)`. 

A change of coordinates, to `r(x,y)` and `s(x,y)`, can be performed so this Lie group 

becomes the translation group, `r^*=r` and `s^*=s+\varepsilon`. 

They are tangents to the coordinate curves of the new system. 

 

Consider the transformation `(x, y) \to (X, Y)` such that the 

differential equation remains invariant. `\xi` and `\eta` are the tangents to 

the transformed coordinates `X` and `Y`, at `\varepsilon=0`. 

 

.. math:: \left(\frac{\partial X(x,y;\varepsilon)}{\partial\varepsilon 

}\right)|_{\varepsilon=0} = \xi, 

\left(\frac{\partial Y(x,y;\varepsilon)}{\partial\varepsilon 

}\right)|_{\varepsilon=0} = \eta, 

 

The infinitesimals can be found by solving the following PDE: 

 

>>> from sympy import Function, diff, Eq, pprint 

>>> from sympy.abc import x, y 

>>> xi, eta, h = map(Function, ['xi', 'eta', 'h']) 

>>> h = h(x, y) # dy/dx = h 

>>> eta = eta(x, y) 

>>> xi = xi(x, y) 

>>> genform = Eq(eta.diff(x) + (eta.diff(y) - xi.diff(x))*h 

... - (xi.diff(y))*h**2 - xi*(h.diff(x)) - eta*(h.diff(y)), 0) 

>>> pprint(genform) 

/d d \ d 2 d 

|--(eta(x, y)) - --(xi(x, y))|*h(x, y) - eta(x, y)*--(h(x, y)) - h (x, y)*--(x 

\dy dx / dy dy 

<BLANKLINE> 

d d 

i(x, y)) - xi(x, y)*--(h(x, y)) + --(eta(x, y)) = 0 

dx dx 

 

Solving the above mentioned PDE is not trivial, and can be solved only by 

making intelligent assumptions for `\xi` and `\eta` (heuristics). Once an 

infinitesimal is found, the attempt to find more heuristics stops. This is done to 

optimise the speed of solving the differential equation. If a list of all the 

infinitesimals is needed, ``hint`` should be flagged as ``all``, which gives 

the complete list of infinitesimals. If the infinitesimals for a particular 

heuristic needs to be found, it can be passed as a flag to ``hint``. 

 

Examples 

======== 

 

>>> from sympy import Function, diff 

>>> from sympy.solvers.ode import infinitesimals 

>>> from sympy.abc import x 

>>> f = Function('f') 

>>> eq = f(x).diff(x) - x**2*f(x) 

>>> infinitesimals(eq) 

[{eta(x, f(x)): exp(x**3/3), xi(x, f(x)): 0}] 

 

References 

========== 

 

- Solving differential equations by Symmetry Groups, 

John Starrett, pp. 1 - pp. 14 

 

""" 

 

if isinstance(eq, Equality): 

eq = eq.lhs - eq.rhs 

if not func: 

eq, func = _preprocess(eq) 

variables = func.args 

if len(variables) != 1: 

raise ValueError("ODE's have only one independent variable") 

else: 

x = variables[0] 

if not order: 

order = ode_order(eq, func) 

if order != 1: 

raise NotImplementedError("Infinitesimals for only " 

"first order ODE's have been implemented") 

else: 

df = func.diff(x) 

# Matching differential equation of the form a*df + b 

a = Wild('a', exclude = [df]) 

b = Wild('b', exclude = [df]) 

if match: # Used by lie_group hint 

h = match['h'] 

y = match['y'] 

else: 

match = collect(expand(eq), df).match(a*df + b) 

if match: 

h = -simplify(match[b]/match[a]) 

else: 

try: 

sol = solve(eq, df) 

except NotImplementedError: 

raise NotImplementedError("Infinitesimals for the " 

"first order ODE could not be found") 

else: 

h = sol[0] # Find infinitesimals for one solution 

y = Dummy("y") 

h = h.subs(func, y) 

 

u = Dummy("u") 

hx = h.diff(x) 

hy = h.diff(y) 

hinv = ((1/h).subs([(x, u), (y, x)])).subs(u, y) # Inverse ODE 

match = {'h': h, 'func': func, 'hx': hx, 'hy': hy, 'y': y, 'hinv': hinv} 

if hint == 'all': 

xieta = [] 

for heuristic in lie_heuristics: 

function = globals()['lie_heuristic_' + heuristic] 

inflist = function(match, comp=True) 

if inflist: 

xieta.extend([inf for inf in inflist if inf not in xieta]) 

if xieta: 

return xieta 

else: 

raise NotImplementedError("Infinitesimals could not be found for " 

"the given ODE") 

 

elif hint == 'default': 

for heuristic in lie_heuristics: 

function = globals()['lie_heuristic_' + heuristic] 

xieta = function(match, comp=False) 

if xieta: 

return xieta 

 

raise NotImplementedError("Infinitesimals could not be found for" 

" the given ODE") 

 

elif hint not in lie_heuristics: 

raise ValueError("Heuristic not recognized: " + hint) 

 

else: 

function = globals()['lie_heuristic_' + hint] 

xieta = function(match, comp=True) 

if xieta: 

return xieta 

else: 

raise ValueError("Infinitesimals could not be found using the" 

" given heuristic") 

 

 

def lie_heuristic_abaco1_simple(match, comp=False): 

r""" 

The first heuristic uses the following four sets of 

assumptions on `\xi` and `\eta` 

 

.. math:: \xi = 0, \eta = f(x) 

 

.. math:: \xi = 0, \eta = f(y) 

 

.. math:: \xi = f(x), \eta = 0 

 

.. math:: \xi = f(y), \eta = 0 

 

The success of this heuristic is determined by algebraic factorisation. 

For the first assumption `\xi = 0` and `\eta` to be a function of `x`, the PDE 

 

.. math:: \frac{\partial \eta}{\partial x} + (\frac{\partial \eta}{\partial y} 

- \frac{\partial \xi}{\partial x})*h 

- \frac{\partial \xi}{\partial y}*h^{2} 

- \xi*\frac{\partial h}{\partial x} - \eta*\frac{\partial h}{\partial y} = 0 

 

reduces to `f'(x) - f\frac{\partial h}{\partial y} = 0` 

If `\frac{\partial h}{\partial y}` is a function of `x`, then this can usually 

be integrated easily. A similar idea is applied to the other 3 assumptions as well. 

 

 

References 

========== 

 

- E.S Cheb-Terrab, L.G.S Duarte and L.A,C.P da Mota, Computer Algebra 

Solving of First Order ODEs Using Symmetry Methods, pp. 8 

 

 

""" 

 

xieta = [] 

y = match['y'] 

h = match['h'] 

func = match['func'] 

x = func.args[0] 

hx = match['hx'] 

hy = match['hy'] 

xi = Function('xi')(x, func) 

eta = Function('eta')(x, func) 

 

hysym = hy.free_symbols 

if y not in hysym: 

try: 

fx = exp(integrate(hy, x)) 

except NotImplementedError: 

pass 

else: 

inf = {xi: S(0), eta: fx} 

if not comp: 

return [inf] 

if comp and inf not in xieta: 

xieta.append(inf) 

 

factor = hy/h 

facsym = factor.free_symbols 

if x not in facsym: 

try: 

fy = exp(integrate(factor, y)) 

except NotImplementedError: 

pass 

else: 

inf = {xi: S(0), eta: fy.subs(y, func)} 

if not comp: 

return [inf] 

if comp and inf not in xieta: 

xieta.append(inf) 

 

factor = -hx/h 

facsym = factor.free_symbols 

if y not in facsym: 

try: 

fx = exp(integrate(factor, x)) 

except NotImplementedError: 

pass 

else: 

inf = {xi: fx, eta: S(0)} 

if not comp: 

return [inf] 

if comp and inf not in xieta: 

xieta.append(inf) 

 

factor = -hx/(h**2) 

facsym = factor.free_symbols 

if x not in facsym: 

try: 

fy = exp(integrate(factor, y)) 

except NotImplementedError: 

pass 

else: 

inf = {xi: fy.subs(y, func), eta: S(0)} 

if not comp: 

return [inf] 

if comp and inf not in xieta: 

xieta.append(inf) 

 

if xieta: 

return xieta 

 

def lie_heuristic_abaco1_product(match, comp=False): 

r""" 

The second heuristic uses the following two assumptions on `\xi` and `\eta` 

 

.. math:: \eta = 0, \xi = f(x)*g(y) 

 

.. math:: \eta = f(x)*g(y), \xi = 0 

 

The first assumption of this heuristic holds good if 

`\frac{1}{h^{2}}\frac{\partial^2}{\partial x \partial y}\log(h)` is 

separable in `x` and `y`, then the separated factors containing `x` 

is `f(x)`, and `g(y)` is obtained by 

 

.. math:: e^{\int f\frac{\partial}{\partial x}\left(\frac{1}{f*h}\right)\,dy} 

 

provided `f\frac{\partial}{\partial x}\left(\frac{1}{f*h}\right)` is a function 

of `y` only. 

 

The second assumption holds good if `\frac{dy}{dx} = h(x, y)` is rewritten as 

`\frac{dy}{dx} = \frac{1}{h(y, x)}` and the same properties of the first assumption 

satisfies. After obtaining `f(x)` and `g(y)`, the coordinates are again 

interchanged, to get `\eta` as `f(x)*g(y)` 

 

 

References 

========== 

- E.S. Cheb-Terrab, A.D. Roche, Symmetries and First Order 

ODE Patterns, pp. 7 - pp. 8 

 

""" 

 

xieta = [] 

y = match['y'] 

h = match['h'] 

hinv = match['hinv'] 

func = match['func'] 

x = func.args[0] 

xi = Function('xi')(x, func) 

eta = Function('eta')(x, func) 

 

 

inf = separatevars(((log(h).diff(y)).diff(x))/h**2, dict=True, symbols=[x, y]) 

if inf and inf['coeff']: 

fx = inf[x] 

gy = simplify(fx*((1/(fx*h)).diff(x))) 

gysyms = gy.free_symbols 

if x not in gysyms: 

gy = exp(integrate(gy, y)) 

inf = {eta: S(0), xi: (fx*gy).subs(y, func)} 

if not comp: 

return [inf] 

if comp and inf not in xieta: 

xieta.append(inf) 

 

u1 = Dummy("u1") 

inf = separatevars(((log(hinv).diff(y)).diff(x))/hinv**2, dict=True, symbols=[x, y]) 

if inf and inf['coeff']: 

fx = inf[x] 

gy = simplify(fx*((1/(fx*hinv)).diff(x))) 

gysyms = gy.free_symbols 

if x not in gysyms: 

gy = exp(integrate(gy, y)) 

etaval = fx*gy 

etaval = (etaval.subs([(x, u1), (y, x)])).subs(u1, y) 

inf = {eta: etaval.subs(y, func), xi: S(0)} 

if not comp: 

return [inf] 

if comp and inf not in xieta: 

xieta.append(inf) 

 

if xieta: 

return xieta 

 

def lie_heuristic_bivariate(match, comp=False): 

r""" 

The third heuristic assumes the infinitesimals `\xi` and `\eta` 

to be bi-variate polynomials in `x` and `y`. The assumption made here 

for the logic below is that `h` is a rational function in `x` and `y` 

though that may not be necessary for the infinitesimals to be 

bivariate polynomials. The coefficients of the infinitesimals 

are found out by substituting them in the PDE and grouping similar terms 

that are polynomials and since they form a linear system, solve and check 

for non trivial solutions. The degree of the assumed bivariates 

are increased till a certain maximum value. 

 

References 

========== 

- Lie Groups and Differential Equations 

pp. 327 - pp. 329 

 

""" 

 

h = match['h'] 

hx = match['hx'] 

hy = match['hy'] 

func = match['func'] 

x = func.args[0] 

y = match['y'] 

xi = Function('xi')(x, func) 

eta = Function('eta')(x, func) 

 

if h.is_rational_function(): 

# The maximum degree that the infinitesimals can take is 

# calculated by this technique. 

etax, etay, etad, xix, xiy, xid = symbols("etax etay etad xix xiy xid") 

ipde = etax + (etay - xix)*h - xiy*h**2 - xid*hx - etad*hy 

num, denom = cancel(ipde).as_numer_denom() 

deg = Poly(num, x, y).total_degree() 

deta = Function('deta')(x, y) 

dxi = Function('dxi')(x, y) 

ipde = (deta.diff(x) + (deta.diff(y) - dxi.diff(x))*h - (dxi.diff(y))*h**2 

- dxi*hx - deta*hy) 

xieq = Symbol("xi0") 

etaeq = Symbol("eta0") 

 

for i in range(deg + 1): 

if i: 

xieq += Add(*[ 

Symbol("xi_" + str(power) + "_" + str(i - power))*x**power*y**(i - power) 

for power in range(i + 1)]) 

etaeq += Add(*[ 

Symbol("eta_" + str(power) + "_" + str(i - power))*x**power*y**(i - power) 

for power in range(i + 1)]) 

pden, denom = (ipde.subs({dxi: xieq, deta: etaeq}).doit()).as_numer_denom() 

pden = expand(pden) 

 

# If the individual terms are monomials, the coefficients 

# are grouped 

if pden.is_polynomial(x, y) and pden.is_Add: 

polyy = Poly(pden, x, y).as_dict() 

if polyy: 

symset = xieq.free_symbols.union(etaeq.free_symbols) - {x, y} 

soldict = solve(polyy.values(), *symset) 

if isinstance(soldict, list): 

soldict = soldict[0] 

if any(x for x in soldict.values()): 

xired = xieq.subs(soldict) 

etared = etaeq.subs(soldict) 

# Scaling is done by substituting one for the parameters 

# This can be any number except zero. 

dict_ = dict((sym, 1) for sym in symset) 

inf = {eta: etared.subs(dict_).subs(y, func), 

xi: xired.subs(dict_).subs(y, func)} 

return [inf] 

 

def lie_heuristic_chi(match, comp=False): 

r""" 

The aim of the fourth heuristic is to find the function `\chi(x, y)` 

that satisfies the PDE `\frac{d\chi}{dx} + h\frac{d\chi}{dx} 

- \frac{\partial h}{\partial y}\chi = 0`. 

 

This assumes `\chi` to be a bivariate polynomial in `x` and `y`. By intuition, 

`h` should be a rational function in `x` and `y`. The method used here is 

to substitute a general binomial for `\chi` up to a certain maximum degree 

is reached. The coefficients of the polynomials, are calculated by by collecting 

terms of the same order in `x` and `y`. 

 

After finding `\chi`, the next step is to use `\eta = \xi*h + \chi`, to 

determine `\xi` and `\eta`. This can be done by dividing `\chi` by `h` 

which would give `-\xi` as the quotient and `\eta` as the remainder. 

 

 

References 

========== 

- E.S Cheb-Terrab, L.G.S Duarte and L.A,C.P da Mota, Computer Algebra 

Solving of First Order ODEs Using Symmetry Methods, pp. 8 

 

""" 

 

h = match['h'] 

hx = match['hx'] 

hy = match['hy'] 

func = match['func'] 

x = func.args[0] 

y = match['y'] 

xi = Function('xi')(x, func) 

eta = Function('eta')(x, func) 

 

if h.is_rational_function(): 

schi, schix, schiy = symbols("schi, schix, schiy") 

cpde = schix + h*schiy - hy*schi 

num, denom = cancel(cpde).as_numer_denom() 

deg = Poly(num, x, y).total_degree() 

 

chi = Function('chi')(x, y) 

chix = chi.diff(x) 

chiy = chi.diff(y) 

cpde = chix + h*chiy - hy*chi 

chieq = Symbol("chi") 

for i in range(1, deg + 1): 

chieq += Add(*[ 

Symbol("chi_" + str(power) + "_" + str(i - power))*x**power*y**(i - power) 

for power in range(i + 1)]) 

cnum, cden = cancel(cpde.subs({chi : chieq}).doit()).as_numer_denom() 

cnum = expand(cnum) 

if cnum.is_polynomial(x, y) and cnum.is_Add: 

cpoly = Poly(cnum, x, y).as_dict() 

if cpoly: 

solsyms = chieq.free_symbols - {x, y} 

soldict = solve(cpoly.values(), *solsyms) 

if isinstance(soldict, list): 

soldict = soldict[0] 

if any(x for x in soldict.values()): 

chieq = chieq.subs(soldict) 

dict_ = dict((sym, 1) for sym in solsyms) 

chieq = chieq.subs(dict_) 

# After finding chi, the main aim is to find out 

# eta, xi by the equation eta = xi*h + chi 

# One method to set xi, would be rearranging it to 

# (eta/h) - xi = (chi/h). This would mean dividing 

# chi by h would give -xi as the quotient and eta 

# as the remainder. Thanks to Sean Vig for suggesting 

# this method. 

xic, etac = div(chieq, h) 

inf = {eta: etac.subs(y, func), xi: -xic.subs(y, func)} 

return [inf] 

 

def lie_heuristic_function_sum(match, comp=False): 

r""" 

This heuristic uses the following two assumptions on `\xi` and `\eta` 

 

.. math:: \eta = 0, \xi = f(x) + g(y) 

 

.. math:: \eta = f(x) + g(y), \xi = 0 

 

The first assumption of this heuristic holds good if 

 

.. math:: \frac{\partial}{\partial y}[(h\frac{\partial^{2}}{ 

\partial x^{2}}(h^{-1}))^{-1}] 

 

is separable in `x` and `y`, 

 

1. The separated factors containing `y` is `\frac{\partial g}{\partial y}`. 

From this `g(y)` can be determined. 

2. The separated factors containing `x` is `f''(x)`. 

3. `h\frac{\partial^{2}}{\partial x^{2}}(h^{-1})` equals 

`\frac{f''(x)}{f(x) + g(y)}`. From this `f(x)` can be determined. 

 

The second assumption holds good if `\frac{dy}{dx} = h(x, y)` is rewritten as 

`\frac{dy}{dx} = \frac{1}{h(y, x)}` and the same properties of the first 

assumption satisfies. After obtaining `f(x)` and `g(y)`, the coordinates 

are again interchanged, to get `\eta` as `f(x) + g(y)`. 

 

For both assumptions, the constant factors are separated among `g(y)` 

and `f''(x)`, such that `f''(x)` obtained from 3] is the same as that 

obtained from 2]. If not possible, then this heuristic fails. 

 

 

References 

========== 

- E.S. Cheb-Terrab, A.D. Roche, Symmetries and First Order 

ODE Patterns, pp. 7 - pp. 8 

 

""" 

 

xieta = [] 

h = match['h'] 

hx = match['hx'] 

hy = match['hy'] 

func = match['func'] 

hinv = match['hinv'] 

x = func.args[0] 

y = match['y'] 

xi = Function('xi')(x, func) 

eta = Function('eta')(x, func) 

 

for odefac in [h, hinv]: 

factor = odefac*((1/odefac).diff(x, 2)) 

sep = separatevars((1/factor).diff(y), dict=True, symbols=[x, y]) 

if sep and sep['coeff'] and sep[x].has(x) and sep[y].has(y): 

k = Dummy("k") 

try: 

gy = k*integrate(sep[y], y) 

except NotImplementedError: 

pass 

else: 

fdd = 1/(k*sep[x]*sep['coeff']) 

fx = simplify(fdd/factor - gy) 

check = simplify(fx.diff(x, 2) - fdd) 

if fx: 

if not check: 

fx = fx.subs(k, 1) 

gy = (gy/k) 

else: 

sol = solve(check, k) 

if sol: 

sol = sol[0] 

fx = fx.subs(k, sol) 

gy = (gy/k)*sol 

else: 

continue 

if odefac == hinv: # Inverse ODE 

fx = fx.subs(x, y) 

gy = gy.subs(y, x) 

etaval = factor_terms(fx + gy) 

if etaval.is_Mul: 

etaval = Mul(*[arg for arg in etaval.args if arg.has(x, y)]) 

if odefac == hinv: # Inverse ODE 

inf = {eta: etaval.subs(y, func), xi : S(0)} 

else: 

inf = {xi: etaval.subs(y, func), eta : S(0)} 

if not comp: 

return [inf] 

else: 

xieta.append(inf) 

 

if xieta: 

return xieta 

 

def lie_heuristic_abaco2_similar(match, comp=False): 

r""" 

This heuristic uses the following two assumptions on `\xi` and `\eta` 

 

.. math:: \eta = g(x), \xi = f(x) 

 

.. math:: \eta = f(y), \xi = g(y) 

 

For the first assumption, 

 

1. First `\frac{\frac{\partial h}{\partial y}}{\frac{\partial^{2} h}{ 

\partial yy}}` is calculated. Let us say this value is A 

 

2. If this is constant, then `h` is matched to the form `A(x) + B(x)e^{ 

\frac{y}{C}}` then, `\frac{e^{\int \frac{A(x)}{C} \,dx}}{B(x)}` gives `f(x)` 

and `A(x)*f(x)` gives `g(x)` 

 

3. Otherwise `\frac{\frac{\partial A}{\partial X}}{\frac{\partial A}{ 

\partial Y}} = \gamma` is calculated. If 

 

a] `\gamma` is a function of `x` alone 

 

b] `\frac{\gamma\frac{\partial h}{\partial y} - \gamma'(x) - \frac{ 

\partial h}{\partial x}}{h + \gamma} = G` is a function of `x` alone. 

then, `e^{\int G \,dx}` gives `f(x)` and `-\gamma*f(x)` gives `g(x)` 

 

The second assumption holds good if `\frac{dy}{dx} = h(x, y)` is rewritten as 

`\frac{dy}{dx} = \frac{1}{h(y, x)}` and the same properties of the first assumption 

satisfies. After obtaining `f(x)` and `g(x)`, the coordinates are again 

interchanged, to get `\xi` as `f(x^*)` and `\eta` as `g(y^*)` 

 

References 

========== 

- E.S. Cheb-Terrab, A.D. Roche, Symmetries and First Order 

ODE Patterns, pp. 10 - pp. 12 

 

""" 

 

xieta = [] 

h = match['h'] 

hx = match['hx'] 

hy = match['hy'] 

func = match['func'] 

hinv = match['hinv'] 

x = func.args[0] 

y = match['y'] 

xi = Function('xi')(x, func) 

eta = Function('eta')(x, func) 

 

factor = cancel(h.diff(y)/h.diff(y, 2)) 

factorx = factor.diff(x) 

factory = factor.diff(y) 

if not factor.has(x) and not factor.has(y): 

A = Wild('A', exclude=[y]) 

B = Wild('B', exclude=[y]) 

C = Wild('C', exclude=[x, y]) 

match = h.match(A + B*exp(y/C)) 

try: 

tau = exp(-integrate(match[A]/match[C]), x)/match[B] 

except NotImplementedError: 

pass 

else: 

gx = match[A]*tau 

return [{xi: tau, eta: gx}] 

 

else: 

gamma = cancel(factorx/factory) 

if not gamma.has(y): 

tauint = cancel((gamma*hy - gamma.diff(x) - hx)/(h + gamma)) 

if not tauint.has(y): 

try: 

tau = exp(integrate(tauint, x)) 

except NotImplementedError: 

pass 

else: 

gx = -tau*gamma 

return [{xi: tau, eta: gx}] 

 

factor = cancel(hinv.diff(y)/hinv.diff(y, 2)) 

factorx = factor.diff(x) 

factory = factor.diff(y) 

if not factor.has(x) and not factor.has(y): 

A = Wild('A', exclude=[y]) 

B = Wild('B', exclude=[y]) 

C = Wild('C', exclude=[x, y]) 

match = h.match(A + B*exp(y/C)) 

try: 

tau = exp(-integrate(match[A]/match[C]), x)/match[B] 

except NotImplementedError: 

pass 

else: 

gx = match[A]*tau 

return [{eta: tau.subs(x, func), xi: gx.subs(x, func)}] 

 

else: 

gamma = cancel(factorx/factory) 

if not gamma.has(y): 

tauint = cancel((gamma*hinv.diff(y) - gamma.diff(x) - hinv.diff(x))/( 

hinv + gamma)) 

if not tauint.has(y): 

try: 

tau = exp(integrate(tauint, x)) 

except NotImplementedError: 

pass 

else: 

gx = -tau*gamma 

return [{eta: tau.subs(x, func), xi: gx.subs(x, func)}] 

 

 

def lie_heuristic_abaco2_unique_unknown(match, comp=False): 

r""" 

This heuristic assumes the presence of unknown functions or known functions 

with non-integer powers. 

 

1. A list of all functions and non-integer powers containing x and y 

2. Loop over each element `f` in the list, find `\frac{\frac{\partial f}{\partial x}}{ 

\frac{\partial f}{\partial x}} = R` 

 

If it is separable in `x` and `y`, let `X` be the factors containing `x`. Then 

 

a] Check if `\xi = X` and `\eta = -\frac{X}{R}` satisfy the PDE. If yes, then return 

`\xi` and `\eta` 

b] Check if `\xi = \frac{-R}{X}` and `\eta = -\frac{1}{X}` satisfy the PDE. 

If yes, then return `\xi` and `\eta` 

 

If not, then check if 

 

a] :math:`\xi = -R,\eta = 1` 

 

b] :math:`\xi = 1, \eta = -\frac{1}{R}` 

 

are solutions. 

 

References 

========== 

- E.S. Cheb-Terrab, A.D. Roche, Symmetries and First Order 

ODE Patterns, pp. 10 - pp. 12 

 

""" 

 

xieta = [] 

h = match['h'] 

hx = match['hx'] 

hy = match['hy'] 

func = match['func'] 

hinv = match['hinv'] 

x = func.args[0] 

y = match['y'] 

xi = Function('xi')(x, func) 

eta = Function('eta')(x, func) 

 

funclist = [] 

for atom in h.atoms(Pow): 

base, exp = atom.as_base_exp() 

if base.has(x) and base.has(y): 

if not exp.is_Integer: 

funclist.append(atom) 

 

for function in h.atoms(AppliedUndef): 

syms = function.free_symbols 

if x in syms and y in syms: 

funclist.append(function) 

 

for f in funclist: 

frac = cancel(f.diff(y)/f.diff(x)) 

sep = separatevars(frac, dict=True, symbols=[x, y]) 

if sep and sep['coeff']: 

xitry1 = sep[x] 

etatry1 = -1/(sep[y]*sep['coeff']) 

pde1 = etatry1.diff(y)*h - xitry1.diff(x)*h - xitry1*hx - etatry1*hy 

if not simplify(pde1): 

return [{xi: xitry1, eta: etatry1.subs(y, func)}] 

xitry2 = 1/etatry1 

etatry2 = 1/xitry1 

pde2 = etatry2.diff(x) - (xitry2.diff(y))*h**2 - xitry2*hx - etatry2*hy 

if not simplify(expand(pde2)): 

return [{xi: xitry2.subs(y, func), eta: etatry2}] 

 

else: 

etatry = -1/frac 

pde = etatry.diff(x) + etatry.diff(y)*h - hx - etatry*hy 

if not simplify(pde): 

return [{xi: S(1), eta: etatry.subs(y, func)}] 

xitry = -frac 

pde = -xitry.diff(x)*h -xitry.diff(y)*h**2 - xitry*hx -hy 

if not simplify(expand(pde)): 

return [{xi: xitry.subs(y, func), eta: S(1)}] 

 

 

def lie_heuristic_abaco2_unique_general(match, comp=False): 

r""" 

This heuristic finds if infinitesimals of the form `\eta = f(x)`, `\xi = g(y)` 

without making any assumptions on `h`. 

 

The complete sequence of steps is given in the paper mentioned below. 

 

References 

========== 

- E.S. Cheb-Terrab, A.D. Roche, Symmetries and First Order 

ODE Patterns, pp. 10 - pp. 12 

 

""" 

xieta = [] 

h = match['h'] 

hx = match['hx'] 

hy = match['hy'] 

func = match['func'] 

hinv = match['hinv'] 

x = func.args[0] 

y = match['y'] 

xi = Function('xi')(x, func) 

eta = Function('eta')(x, func) 

 

C = S(0) 

A = hx.diff(y) 

B = hy.diff(y) + hy**2 

C = hx.diff(x) - hx**2 

 

if not (A and B and C): 

return 

 

Ax = A.diff(x) 

Ay = A.diff(y) 

Axy = Ax.diff(y) 

Axx = Ax.diff(x) 

Ayy = Ay.diff(y) 

D = simplify(2*Axy + hx*Ay - Ax*hy + (hx*hy + 2*A)*A)*A - 3*Ax*Ay 

if not D: 

E1 = simplify(3*Ax**2 + ((hx**2 + 2*C)*A - 2*Axx)*A) 

if E1: 

E2 = simplify((2*Ayy + (2*B - hy**2)*A)*A - 3*Ay**2) 

if not E2: 

E3 = simplify( 

E1*((28*Ax + 4*hx*A)*A**3 - E1*(hy*A + Ay)) - E1.diff(x)*8*A**4) 

if not E3: 

etaval = cancel((4*A**3*(Ax - hx*A) + E1*(hy*A - Ay))/(S(2)*A*E1)) 

if x not in etaval: 

try: 

etaval = exp(integrate(etaval, y)) 

except NotImplementedError: 

pass 

else: 

xival = -4*A**3*etaval/E1 

if y not in xival: 

return [{xi: xival, eta: etaval.subs(y, func)}] 

 

else: 

E1 = simplify((2*Ayy + (2*B - hy**2)*A)*A - 3*Ay**2) 

if E1: 

E2 = simplify( 

4*A**3*D - D**2 + E1*((2*Axx - (hx**2 + 2*C)*A)*A - 3*Ax**2)) 

if not E2: 

E3 = simplify( 

-(A*D)*E1.diff(y) + ((E1.diff(x) - hy*D)*A + 3*Ay*D + 

(A*hx - 3*Ax)*E1)*E1) 

if not E3: 

etaval = cancel(((A*hx - Ax)*E1 - (Ay + A*hy)*D)/(S(2)*A*D)) 

if x not in etaval: 

try: 

etaval = exp(integrate(etaval, y)) 

except NotImplementedError: 

pass 

else: 

xival = -E1*etaval/D 

if y not in xival: 

return [{xi: xival, eta: etaval.subs(y, func)}] 

 

 

def lie_heuristic_linear(match, comp=False): 

r""" 

This heuristic assumes 

 

1. `\xi = ax + by + c` and 

2. `\eta = fx + gy + h` 

 

After substituting the following assumptions in the determining PDE, it 

reduces to 

 

.. math:: f + (g - a)h - bh^{2} - (ax + by + c)\frac{\partial h}{\partial x} 

- (fx + gy + c)\frac{\partial h}{\partial y} 

 

Solving the reduced PDE obtained, using the method of characteristics, becomes 

impractical. The method followed is grouping similar terms and solving the system 

of linear equations obtained. The difference between the bivariate heuristic is that 

`h` need not be a rational function in this case. 

 

References 

========== 

- E.S. Cheb-Terrab, A.D. Roche, Symmetries and First Order 

ODE Patterns, pp. 10 - pp. 12 

 

""" 

xieta = [] 

h = match['h'] 

hx = match['hx'] 

hy = match['hy'] 

func = match['func'] 

hinv = match['hinv'] 

x = func.args[0] 

y = match['y'] 

xi = Function('xi')(x, func) 

eta = Function('eta')(x, func) 

 

coeffdict = {} 

symbols = numbered_symbols("c", cls=Dummy) 

symlist = [next(symbols) for i in islice(symbols, 6)] 

C0, C1, C2, C3, C4, C5 = symlist 

pde = C3 + (C4 - C0)*h -(C0*x + C1*y + C2)*hx - (C3*x + C4*y + C5)*hy - C1*h**2 

pde, denom = pde.as_numer_denom() 

pde = powsimp(expand(pde)) 

if pde.is_Add: 

terms = pde.args 

for term in terms: 

if term.is_Mul: 

rem = Mul(*[m for m in term.args if not m.has(x, y)]) 

xypart = term/rem 

if xypart not in coeffdict: 

coeffdict[xypart] = rem 

else: 

coeffdict[xypart] += rem 

else: 

if term not in coeffdict: 

coeffdict[term] = S(1) 

else: 

coeffdict[term] += S(1) 

 

sollist = coeffdict.values() 

soldict = solve(sollist, symlist) 

if soldict: 

if isinstance(soldict, list): 

soldict = soldict[0] 

subval = soldict.values() 

if any(t for t in subval): 

onedict = dict(zip(symlist, [1]*6)) 

xival = C0*x + C1*func + C2 

etaval = C3*x + C4*func + C5 

xival = xival.subs(soldict) 

etaval = etaval.subs(soldict) 

xival = xival.subs(onedict) 

etaval = etaval.subs(onedict) 

return [{xi: xival, eta: etaval}] 

 

 

def sysode_linear_2eq_order1(match_): 

x = match_['func'][0].func 

y = match_['func'][1].func 

func = match_['func'] 

fc = match_['func_coeff'] 

eq = match_['eq'] 

C1, C2, C3, C4 = get_numbered_constants(eq, num=4) 

r = dict() 

t = list(list(eq[0].atoms(Derivative))[0].atoms(Symbol))[0] 

for i in range(2): 

eqs = 0 

for terms in Add.make_args(eq[i]): 

eqs += terms/fc[i,func[i],1] 

eq[i] = eqs 

 

# for equations Eq(a1*diff(x(t),t), a*x(t) + b*y(t) + k1) 

# and Eq(a2*diff(x(t),t), c*x(t) + d*y(t) + k2) 

r['a'] = -fc[0,x(t),0]/fc[0,x(t),1] 

r['c'] = -fc[1,x(t),0]/fc[1,y(t),1] 

r['b'] = -fc[0,y(t),0]/fc[0,x(t),1] 

r['d'] = -fc[1,y(t),0]/fc[1,y(t),1] 

forcing = [S(0),S(0)] 

for i in range(2): 

for j in Add.make_args(eq[i]): 

if not j.has(x(t), y(t)): 

forcing[i] += j 

if not (forcing[0].has(t) or forcing[1].has(t)): 

r['k1'] = forcing[0] 

r['k2'] = forcing[1] 

else: 

raise NotImplementedError("Only homogeneous problems are supported" + 

" (and constant inhomogeneity)") 

 

if match_['type_of_equation'] == 'type1': 

sol = _linear_2eq_order1_type1(x, y, t, r, eq) 

if match_['type_of_equation'] == 'type2': 

gsol = _linear_2eq_order1_type1(x, y, t, r, eq) 

psol = _linear_2eq_order1_type2(x, y, t, r, eq) 

sol = [Eq(x(t), gsol[0].rhs+psol[0]), Eq(y(t), gsol[1].rhs+psol[1])] 

if match_['type_of_equation'] == 'type3': 

sol = _linear_2eq_order1_type3(x, y, t, r, eq) 

if match_['type_of_equation'] == 'type4': 

sol = _linear_2eq_order1_type4(x, y, t, r, eq) 

if match_['type_of_equation'] == 'type5': 

sol = _linear_2eq_order1_type5(x, y, t, r, eq) 

if match_['type_of_equation'] == 'type6': 

sol = _linear_2eq_order1_type6(x, y, t, r, eq) 

if match_['type_of_equation'] == 'type7': 

sol = _linear_2eq_order1_type7(x, y, t, r, eq) 

return sol 

 

def _linear_2eq_order1_type1(x, y, t, r, eq): 

r""" 

It is classified under system of two linear homogeneous first-order constant-coefficient 

ordinary differential equations. 

 

The equations which come under this type are 

 

.. math:: x' = ax + by, 

 

.. math:: y' = cx + dy 

 

The characteristics equation is written as 

 

.. math:: \lambda^{2} + (a+d) \lambda + ad - bc = 0 

 

and its discriminant is `D = (a-d)^{2} + 4bc`. There are several cases 

 

1. Case when `ad - bc \neq 0`. The origin of coordinates, `x = y = 0`, 

is the only stationary point; it is 

- a node if `D = 0` 

- a node if `D > 0` and `ad - bc > 0` 

- a saddle if `D > 0` and `ad - bc < 0` 

- a focus if `D < 0` and `a + d \neq 0` 

- a centre if `D < 0` and `a + d \neq 0`. 

 

1.1. If `D > 0`. The characteristic equation has two distinct real roots 

`\lambda_1` and `\lambda_ 2` . The general solution of the system in question is expressed as 

 

.. math:: x = C_1 b e^{\lambda_1 t} + C_2 b e^{\lambda_2 t} 

 

.. math:: y = C_1 (\lambda_1 - a) e^{\lambda_1 t} + C_2 (\lambda_2 - a) e^{\lambda_2 t} 

 

where `C_1` and `C_2` being arbitrary constants 

 

1.2. If `D < 0`. The characteristics equation has two conjugate 

roots, `\lambda_1 = \sigma + i \beta` and `\lambda_2 = \sigma - i \beta`. 

The general solution of the system is given by 

 

.. math:: x = b e^{\sigma t} (C_1 \sin(\beta t) + C_2 \cos(\beta t)) 

 

.. math:: y = e^{\sigma t} ([(\sigma - a) C_1 - \beta C_2] \sin(\beta t) + [\beta C_1 + (\sigma - a) C_2 \cos(\beta t)]) 

 

1.3. If `D = 0` and `a \neq d`. The characteristic equation has 

two equal roots, `\lambda_1 = \lambda_2`. The general solution of the system is written as 

 

.. math:: x = 2b (C_1 + \frac{C_2}{a-d} + C_2 t) e^{\frac{a+d}{2} t} 

 

.. math:: y = [(d - a) C_1 + C_2 + (d - a) C_2 t] e^{\frac{a+d}{2} t} 

 

1.4. If `D = 0` and `a = d \neq 0` and `b = 0` 

 

.. math:: x = C_1 e^{a t} , y = (c C_1 t + C_2) e^{a t} 

 

1.5. If `D = 0` and `a = d \neq 0` and `c = 0` 

 

.. math:: x = (b C_1 t + C_2) e^{a t} , y = C_1 e^{a t} 

 

2. Case when `ad - bc = 0` and `a^{2} + b^{2} > 0`. The whole straight 

line `ax + by = 0` consists of singular points. The original system of differential 

equations can be rewritten as 

 

.. math:: x' = ax + by , y' = k (ax + by) 

 

2.1 If `a + bk \neq 0`, solution will be 

 

.. math:: x = b C_1 + C_2 e^{(a + bk) t} , y = -a C_1 + k C_2 e^{(a + bk) t} 

 

2.2 If `a + bk = 0`, solution will be 

 

.. math:: x = C_1 (bk t - 1) + b C_2 t , y = k^{2} b C_1 t + (b k^{2} t + 1) C_2 

 

""" 

 

l = Dummy('l') 

C1, C2 = get_numbered_constants(eq, num=2) 

a, b, c, d = r['a'], r['b'], r['c'], r['d'] 

real_coeff = all(v.is_real for v in (a, b, c, d)) 

D = (a - d)**2 + 4*b*c 

l1 = (a + d + sqrt(D))/2 

l2 = (a + d - sqrt(D))/2 

equal_roots = Eq(D, 0).expand() 

gsol1, gsol2 = [], [] 

 

# Solutions have exponential form if either D > 0 with real coefficients 

# or D != 0 with complex coefficients. Eigenvalues are distinct. 

# For each eigenvalue lam, pick an eigenvector, making sure we don't get (0, 0) 

# The candidates are (b, lam-a) and (lam-d, c). 

exponential_form = D > 0 if real_coeff else Not(equal_roots) 

bad_ab_vector1 = And(Eq(b, 0), Eq(l1, a)) 

bad_ab_vector2 = And(Eq(b, 0), Eq(l2, a)) 

vector1 = Matrix((Piecewise((l1 - d, bad_ab_vector1), (b, True)), 

Piecewise((c, bad_ab_vector1), (l1 - a, True)))) 

vector2 = Matrix((Piecewise((l2 - d, bad_ab_vector2), (b, True)), 

Piecewise((c, bad_ab_vector2), (l2 - a, True)))) 

sol_vector = C1*exp(l1*t)*vector1 + C2*exp(l2*t)*vector2 

gsol1.append((sol_vector[0], exponential_form)) 

gsol2.append((sol_vector[1], exponential_form)) 

 

# Solutions have trigonometric form for real coefficients with D < 0 

# Both b and c are nonzero in this case, so (b, lam-a) is an eigenvector 

# It splits into real/imag parts as (b, sigma-a) and (0, beta). Then 

# multiply it by C1(cos(beta*t) + I*C2*sin(beta*t)) and separate real/imag 

trigonometric_form = D < 0 if real_coeff else False 

sigma = re(l1) 

if im(l1).is_positive: 

beta = im(l1) 

else: 

beta = im(l2) 

vector1 = Matrix((b, sigma - a)) 

vector2 = Matrix((0, beta)) 

sol_vector = exp(sigma*t) * (C1*(cos(beta*t)*vector1 - sin(beta*t)*vector2) + \ 

C2*(sin(beta*t)*vector1 + cos(beta*t)*vector2)) 

gsol1.append((sol_vector[0], trigonometric_form)) 

gsol2.append((sol_vector[1], trigonometric_form)) 

 

# Final case is D == 0, a single eigenvalue. If the eigenspace is 2-dimensional 

# then we have a scalar matrix, deal with this case first. 

scalar_matrix = And(Eq(a, d), Eq(b, 0), Eq(c, 0)) 

 

vector1 = Matrix((S.One, S.Zero)) 

vector2 = Matrix((S.Zero, S.One)) 

sol_vector = exp(l1*t) * (C1*vector1 + C2*vector2) 

gsol1.append((sol_vector[0], scalar_matrix)) 

gsol2.append((sol_vector[1], scalar_matrix)) 

 

# Have one eigenvector. Get a generalized eigenvector from (A-lam)*vector2 = vector1 

vector1 = Matrix((Piecewise((l1 - d, bad_ab_vector1), (b, True)), 

Piecewise((c, bad_ab_vector1), (l1 - a, True)))) 

vector2 = Matrix((Piecewise((S.One, bad_ab_vector1), (S.Zero, Eq(a, l1)), 

(b/(a - l1), True)), 

Piecewise((S.Zero, bad_ab_vector1), (S.One, Eq(a, l1)), 

(S.Zero, True)))) 

sol_vector = exp(l1*t) * (C1*vector1 + C2*(vector2 + t*vector1)) 

gsol1.append((sol_vector[0], equal_roots)) 

gsol2.append((sol_vector[1], equal_roots)) 

return [Eq(x(t), Piecewise(*gsol1)), Eq(y(t), Piecewise(*gsol2))] 

 

 

def _linear_2eq_order1_type2(x, y, t, r, eq): 

r""" 

The equations of this type are 

 

.. math:: x' = ax + by + k1 , y' = cx + dy + k2 

 

The general solution of this system is given by sum of its particular solution and the 

general solution of the corresponding homogeneous system is obtained from type1. 

 

1. When `ad - bc \neq 0`. The particular solution will be 

`x = x_0` and `y = y_0` where `x_0` and `y_0` are determined by solving linear system of equations 

 

.. math:: a x_0 + b y_0 + k1 = 0 , c x_0 + d y_0 + k2 = 0 

 

2. When `ad - bc = 0` and `a^{2} + b^{2} > 0`. In this case, the system of equation becomes 

 

.. math:: x' = ax + by + k_1 , y' = k (ax + by) + k_2 

 

2.1 If `\sigma = a + bk \neq 0`, particular solution is given by 

 

.. math:: x = b \sigma^{-1} (c_1 k - c_2) t - \sigma^{-2} (a c_1 + b c_2) 

 

.. math:: y = kx + (c_2 - c_1 k) t 

 

2.2 If `\sigma = a + bk = 0`, particular solution is given by 

 

.. math:: x = \frac{1}{2} b (c_2 - c_1 k) t^{2} + c_1 t 

 

.. math:: y = kx + (c_2 - c_1 k) t 

 

""" 

r['k1'] = -r['k1']; r['k2'] = -r['k2'] 

if (r['a']*r['d'] - r['b']*r['c']) != 0: 

x0, y0 = symbols('x0, y0', cls=Dummy) 

sol = solve((r['a']*x0+r['b']*y0+r['k1'], r['c']*x0+r['d']*y0+r['k2']), x0, y0) 

psol = [sol[x0], sol[y0]] 

elif (r['a']*r['d'] - r['b']*r['c']) == 0 and (r['a']**2+r['b']**2) > 0: 

k = r['c']/r['a'] 

sigma = r['a'] + r['b']*k 

if sigma != 0: 

sol1 = r['b']*sigma**-1*(r['k1']*k-r['k2'])*t - sigma**-2*(r['a']*r['k1']+r['b']*r['k2']) 

sol2 = k*sol1 + (r['k2']-r['k1']*k)*t 

else: 

# FIXME: a previous typo fix shows this is not covered by tests 

sol1 = r['b']*(r['k2']-r['k1']*k)*t**2 + r['k1']*t 

sol2 = k*sol1 + (r['k2']-r['k1']*k)*t 

psol = [sol1, sol2] 

return psol 

 

def _linear_2eq_order1_type3(x, y, t, r, eq): 

r""" 

The equations of this type of ode are 

 

.. math:: x' = f(t) x + g(t) y 

 

.. math:: y' = g(t) x + f(t) y 

 

The solution of such equations is given by 

 

.. math:: x = e^{F} (C_1 e^{G} + C_2 e^{-G}) , y = e^{F} (C_1 e^{G} - C_2 e^{-G}) 

 

where `C_1` and `C_2` are arbitrary constants, and 

 

.. math:: F = \int f(t) \,dt , G = \int g(t) \,dt 

 

""" 

C1, C2, C3, C4 = get_numbered_constants(eq, num=4) 

F = Integral(r['a'], t) 

G = Integral(r['b'], t) 

sol1 = exp(F)*(C1*exp(G) + C2*exp(-G)) 

sol2 = exp(F)*(C1*exp(G) - C2*exp(-G)) 

return [Eq(x(t), sol1), Eq(y(t), sol2)] 

 

def _linear_2eq_order1_type4(x, y, t, r, eq): 

r""" 

The equations of this type of ode are . 

 

.. math:: x' = f(t) x + g(t) y 

 

.. math:: y' = -g(t) x + f(t) y 

 

The solution is given by 

 

.. math:: x = F (C_1 \cos(G) + C_2 \sin(G)), y = F (-C_1 \sin(G) + C_2 \cos(G)) 

 

where `C_1` and `C_2` are arbitrary constants, and 

 

.. math:: F = \int f(t) \,dt , G = \int g(t) \,dt 

 

""" 

C1, C2, C3, C4 = get_numbered_constants(eq, num=4) 

if r['b'] == -r['c']: 

F = exp(Integral(r['a'], t)) 

G = Integral(r['b'], t) 

sol1 = F*(C1*cos(G) + C2*sin(G)) 

sol2 = F*(-C1*sin(G) + C2*cos(G)) 

elif r['d'] == -r['a']: 

F = exp(Integral(r['c'], t)) 

G = Integral(r['d'], t) 

sol1 = F*(-C1*sin(G) + C2*cos(G)) 

sol2 = F*(C1*cos(G) + C2*sin(G)) 

return [Eq(x(t), sol1), Eq(y(t), sol2)] 

 

def _linear_2eq_order1_type5(x, y, t, r, eq): 

r""" 

The equations of this type of ode are . 

 

.. math:: x' = f(t) x + g(t) y 

 

.. math:: y' = a g(t) x + [f(t) + b g(t)] y 

 

The transformation of 

 

.. math:: x = e^{\int f(t) \,dt} u , y = e^{\int f(t) \,dt} v , T = \int g(t) \,dt 

 

leads to a system of constant coefficient linear differential equations 

 

.. math:: u'(T) = v , v'(T) = au + bv 

 

""" 

C1, C2, C3, C4 = get_numbered_constants(eq, num=4) 

u, v = symbols('u, v', function=True) 

T = Symbol('T') 

if not cancel(r['c']/r['b']).has(t): 

p = cancel(r['c']/r['b']) 

q = cancel((r['d']-r['a'])/r['b']) 

eq = (Eq(diff(u(T),T), v(T)), Eq(diff(v(T),T), p*u(T)+q*v(T))) 

sol = dsolve(eq) 

sol1 = exp(Integral(r['a'], t))*sol[0].rhs.subs(T, Integral(r['b'],t)) 

sol2 = exp(Integral(r['a'], t))*sol[1].rhs.subs(T, Integral(r['b'],t)) 

if not cancel(r['a']/r['d']).has(t): 

p = cancel(r['a']/r['d']) 

q = cancel((r['b']-r['c'])/r['d']) 

sol = dsolve(Eq(diff(u(T),T), v(T)), Eq(diff(v(T),T), p*u(T)+q*v(T))) 

sol1 = exp(Integral(r['c'], t))*sol[1].rhs.subs(T, Integral(r['d'],t)) 

sol2 = exp(Integral(r['c'], t))*sol[0].rhs.subs(T, Integral(r['d'],t)) 

return [Eq(x(t), sol1), Eq(y(t), sol2)] 

 

def _linear_2eq_order1_type6(x, y, t, r, eq): 

r""" 

The equations of this type of ode are . 

 

.. math:: x' = f(t) x + g(t) y 

 

.. math:: y' = a [f(t) + a h(t)] x + a [g(t) - h(t)] y 

 

This is solved by first multiplying the first equation by `-a` and adding 

it to the second equation to obtain 

 

.. math:: y' - a x' = -a h(t) (y - a x) 

 

Setting `U = y - ax` and integrating the equation we arrive at 

 

.. math:: y - ax = C_1 e^{-a \int h(t) \,dt} 

 

and on substituting the value of y in first equation give rise to first order ODEs. After solving for 

`x`, we can obtain `y` by substituting the value of `x` in second equation. 

 

""" 

C1, C2, C3, C4 = get_numbered_constants(eq, num=4) 

p = 0 

q = 0 

p1 = cancel(r['c']/cancel(r['c']/r['d']).as_numer_denom()[0]) 

p2 = cancel(r['a']/cancel(r['a']/r['b']).as_numer_denom()[0]) 

for n, i in enumerate([p1, p2]): 

for j in Mul.make_args(collect_const(i)): 

if not j.has(t): 

q = j 

if q!=0 and n==0: 

if ((r['c']/j - r['a'])/(r['b'] - r['d']/j)) == j: 

p = 1 

s = j 

break 

if q!=0 and n==1: 

if ((r['a']/j - r['c'])/(r['d'] - r['b']/j)) == j: 

p = 2 

s = j 

break 

if p == 1: 

equ = diff(x(t),t) - r['a']*x(t) - r['b']*(s*x(t) + C1*exp(-s*Integral(r['b'] - r['d']/s, t))) 

hint1 = classify_ode(equ)[1] 

sol1 = dsolve(equ, hint=hint1+'_Integral').rhs 

sol2 = s*sol1 + C1*exp(-s*Integral(r['b'] - r['d']/s, t)) 

elif p ==2: 

equ = diff(y(t),t) - r['c']*y(t) - r['d']*s*y(t) + C1*exp(-s*Integral(r['d'] - r['b']/s, t)) 

hint1 = classify_ode(equ)[1] 

sol2 = dsolve(equ, hint=hint1+'_Integral').rhs 

sol1 = s*sol2 + C1*exp(-s*Integral(r['d'] - r['b']/s, t)) 

return [Eq(x(t), sol1), Eq(y(t), sol2)] 

 

def _linear_2eq_order1_type7(x, y, t, r, eq): 

r""" 

The equations of this type of ode are . 

 

.. math:: x' = f(t) x + g(t) y 

 

.. math:: y' = h(t) x + p(t) y 

 

Differentiating the first equation and substituting the value of `y` 

from second equation will give a second-order linear equation 

 

.. math:: g x'' - (fg + gp + g') x' + (fgp - g^{2} h + f g' - f' g) x = 0 

 

This above equation can be easily integrated if following conditions are satisfied. 

 

1. `fgp - g^{2} h + f g' - f' g = 0` 

 

2. `fgp - g^{2} h + f g' - f' g = ag, fg + gp + g' = bg` 

 

If first condition is satisfied then it is solved by current dsolve solver and in second case it becomes 

a constant coefficient differential equation which is also solved by current solver. 

 

Otherwise if the above condition fails then, 

a particular solution is assumed as `x = x_0(t)` and `y = y_0(t)` 

Then the general solution is expressed as 

 

.. math:: x = C_1 x_0(t) + C_2 x_0(t) \int \frac{g(t) F(t) P(t)}{x_0^{2}(t)} \,dt 

 

.. math:: y = C_1 y_0(t) + C_2 [\frac{F(t) P(t)}{x_0(t)} + y_0(t) \int \frac{g(t) F(t) P(t)}{x_0^{2}(t)} \,dt] 

 

where C1 and C2 are arbitrary constants and 

 

.. math:: F(t) = e^{\int f(t) \,dt} , P(t) = e^{\int p(t) \,dt} 

 

""" 

C1, C2, C3, C4 = get_numbered_constants(eq, num=4) 

e1 = r['a']*r['b']*r['c'] - r['b']**2*r['c'] + r['a']*diff(r['b'],t) - diff(r['a'],t)*r['b'] 

e2 = r['a']*r['c']*r['d'] - r['b']*r['c']**2 + diff(r['c'],t)*r['d'] - r['c']*diff(r['d'],t) 

m1 = r['a']*r['b'] + r['b']*r['d'] + diff(r['b'],t) 

m2 = r['a']*r['c'] + r['c']*r['d'] + diff(r['c'],t) 

if e1 == 0: 

sol1 = dsolve(r['b']*diff(x(t),t,t) - m1*diff(x(t),t)).rhs 

sol2 = dsolve(diff(y(t),t) - r['c']*sol1 - r['d']*y(t)).rhs 

elif e2 == 0: 

sol2 = dsolve(r['c']*diff(y(t),t,t) - m2*diff(y(t),t)).rhs 

sol1 = dsolve(diff(x(t),t) - r['a']*x(t) - r['b']*sol2).rhs 

elif not (e1/r['b']).has(t) and not (m1/r['b']).has(t): 

sol1 = dsolve(diff(x(t),t,t) - (m1/r['b'])*diff(x(t),t) - (e1/r['b'])*x(t)).rhs 

sol2 = dsolve(diff(y(t),t) - r['c']*sol1 - r['d']*y(t)).rhs 

elif not (e2/r['c']).has(t) and not (m2/r['c']).has(t): 

sol2 = dsolve(diff(y(t),t,t) - (m2/r['c'])*diff(y(t),t) - (e2/r['c'])*y(t)).rhs 

sol1 = dsolve(diff(x(t),t) - r['a']*x(t) - r['b']*sol2).rhs 

else: 

x0, y0 = symbols('x0, y0') #x0 and y0 being particular solutions 

F = exp(Integral(r['a'],t)) 

P = exp(Integral(r['d'],t)) 

sol1 = C1*x0 + C2*x0*Integral(r['b']*F*P/x0**2, t) 

sol2 = C1*y0 + C2(F*P/x0 + y0*Integral(r['b']*F*P/x0**2, t)) 

return [Eq(x(t), sol1), Eq(y(t), sol2)] 

 

 

def sysode_linear_2eq_order2(match_): 

x = match_['func'][0].func 

y = match_['func'][1].func 

func = match_['func'] 

fc = match_['func_coeff'] 

eq = match_['eq'] 

C1, C2, C3, C4 = get_numbered_constants(eq, num=4) 

r = dict() 

t = list(list(eq[0].atoms(Derivative))[0].atoms(Symbol))[0] 

for i in range(2): 

eqs = [] 

for terms in Add.make_args(eq[i]): 

eqs.append(terms/fc[i,func[i],2]) 

eq[i] = Add(*eqs) 

# for equations Eq(diff(x(t),t,t), a1*diff(x(t),t)+b1*diff(y(t),t)+c1*x(t)+d1*y(t)+e1) 

# and Eq(a2*diff(y(t),t,t), a2*diff(x(t),t)+b2*diff(y(t),t)+c2*x(t)+d2*y(t)+e2) 

r['a1'] = -fc[0,x(t),1]/fc[0,x(t),2] ; r['a2'] = -fc[1,x(t),1]/fc[1,y(t),2] 

r['b1'] = -fc[0,y(t),1]/fc[0,x(t),2] ; r['b2'] = -fc[1,y(t),1]/fc[1,y(t),2] 

r['c1'] = -fc[0,x(t),0]/fc[0,x(t),2] ; r['c2'] = -fc[1,x(t),0]/fc[1,y(t),2] 

r['d1'] = -fc[0,y(t),0]/fc[0,x(t),2] ; r['d2'] = -fc[1,y(t),0]/fc[1,y(t),2] 

const = [S(0), S(0)] 

for i in range(2): 

for j in Add.make_args(eq[i]): 

if not (j.has(x(t)) or j.has(y(t))): 

const[i] += j 

r['e1'] = -const[0] 

r['e2'] = -const[1] 

if match_['type_of_equation'] == 'type1': 

sol = _linear_2eq_order2_type1(x, y, t, r, eq) 

elif match_['type_of_equation'] == 'type2': 

gsol = _linear_2eq_order2_type1(x, y, t, r, eq) 

psol = _linear_2eq_order2_type2(x, y, t, r, eq) 

sol = [Eq(x(t), gsol[0].rhs+psol[0]), Eq(y(t), gsol[1].rhs+psol[1])] 

elif match_['type_of_equation'] == 'type3': 

sol = _linear_2eq_order2_type3(x, y, t, r, eq) 

elif match_['type_of_equation'] == 'type4': 

sol = _linear_2eq_order2_type4(x, y, t, r, eq) 

elif match_['type_of_equation'] == 'type5': 

sol = _linear_2eq_order2_type5(x, y, t, r, eq) 

elif match_['type_of_equation'] == 'type6': 

sol = _linear_2eq_order2_type6(x, y, t, r, eq) 

elif match_['type_of_equation'] == 'type7': 

sol = _linear_2eq_order2_type7(x, y, t, r, eq) 

elif match_['type_of_equation'] == 'type8': 

sol = _linear_2eq_order2_type8(x, y, t, r, eq) 

elif match_['type_of_equation'] == 'type9': 

sol = _linear_2eq_order2_type9(x, y, t, r, eq) 

elif match_['type_of_equation'] == 'type10': 

sol = _linear_2eq_order2_type10(x, y, t, r, eq) 

elif match_['type_of_equation'] == 'type11': 

sol = _linear_2eq_order2_type11(x, y, t, r, eq) 

return sol 

 

def _linear_2eq_order2_type1(x, y, t, r, eq): 

r""" 

System of two constant-coefficient second-order linear homogeneous differential equations 

 

.. math:: x'' = ax + by 

 

.. math:: y'' = cx + dy 

 

The characteristic equation for above equations 

 

.. math:: \lambda^4 - (a + d) \lambda^2 + ad - bc = 0 

 

whose discriminant is `D = (a - d)^2 + 4bc \neq 0` 

 

1. When `ad - bc \neq 0` 

 

1.1. If `D \neq 0`. The characteristic equation has four distinct roots, `\lambda_1, \lambda_2, \lambda_3, \lambda_4`. 

The general solution of the system is 

 

.. math:: x = C_1 b e^{\lambda_1 t} + C_2 b e^{\lambda_2 t} + C_3 b e^{\lambda_3 t} + C_4 b e^{\lambda_4 t} 

 

.. math:: y = C_1 (\lambda_1^{2} - a) e^{\lambda_1 t} + C_2 (\lambda_2^{2} - a) e^{\lambda_2 t} + C_3 (\lambda_3^{2} - a) e^{\lambda_3 t} + C_4 (\lambda_4^{2} - a) e^{\lambda_4 t} 

 

where `C_1,..., C_4` are arbitrary constants. 

 

1.2. If `D = 0` and `a \neq d`: 

 

.. math:: x = 2 C_1 (bt + \frac{2bk}{a - d}) e^{\frac{kt}{2}} + 2 C_2 (bt + \frac{2bk}{a - d}) e^{\frac{-kt}{2}} + 2b C_3 t e^{\frac{kt}{2}} + 2b C_4 t e^{\frac{-kt}{2}} 

 

.. math:: y = C_1 (d - a) t e^{\frac{kt}{2}} + C_2 (d - a) t e^{\frac{-kt}{2}} + C_3 [(d - a) t + 2k] e^{\frac{kt}{2}} + C_4 [(d - a) t - 2k] e^{\frac{-kt}{2}} 

 

where `C_1,..., C_4` are arbitrary constants and `k = \sqrt{2 (a + d)}` 

 

1.3. If `D = 0` and `a = d \neq 0` and `b = 0`: 

 

.. math:: x = 2 \sqrt{a} C_1 e^{\sqrt{a} t} + 2 \sqrt{a} C_2 e^{-\sqrt{a} t} 

 

.. math:: y = c C_1 t e^{\sqrt{a} t} - c C_2 t e^{-\sqrt{a} t} + C_3 e^{\sqrt{a} t} + C_4 e^{-\sqrt{a} t} 

 

1.4. If `D = 0` and `a = d \neq 0` and `c = 0`: 

 

.. math:: x = b C_1 t e^{\sqrt{a} t} - b C_2 t e^{-\sqrt{a} t} + C_3 e^{\sqrt{a} t} + C_4 e^{-\sqrt{a} t} 

 

.. math:: y = 2 \sqrt{a} C_1 e^{\sqrt{a} t} + 2 \sqrt{a} C_2 e^{-\sqrt{a} t} 

 

2. When `ad - bc = 0` and `a^2 + b^2 > 0`. Then the original system becomes 

 

.. math:: x'' = ax + by 

 

.. math:: y'' = k (ax + by) 

 

2.1. If `a + bk \neq 0`: 

 

.. math:: x = C_1 e^{t \sqrt{a + bk}} + C_2 e^{-t \sqrt{a + bk}} + C_3 bt + C_4 b 

 

.. math:: y = C_1 k e^{t \sqrt{a + bk}} + C_2 k e^{-t \sqrt{a + bk}} - C_3 at - C_4 a 

 

2.2. If `a + bk = 0`: 

 

.. math:: x = C_1 b t^3 + C_2 b t^2 + C_3 t + C_4 

 

.. math:: y = kx + 6 C_1 t + 2 C_2 

 

""" 

r['a'] = r['c1'] 

r['b'] = r['d1'] 

r['c'] = r['c2'] 

r['d'] = r['d2'] 

l = Symbol('l') 

C1, C2, C3, C4 = get_numbered_constants(eq, num=4) 

chara_eq = l**4 - (r['a']+r['d'])*l**2 + r['a']*r['d'] - r['b']*r['c'] 

l1 = rootof(chara_eq, 0) 

l2 = rootof(chara_eq, 1) 

l3 = rootof(chara_eq, 2) 

l4 = rootof(chara_eq, 3) 

D = (r['a'] - r['d'])**2 + 4*r['b']*r['c'] 

if (r['a']*r['d'] - r['b']*r['c']) != 0: 

if D != 0: 

gsol1 = C1*r['b']*exp(l1*t) + C2*r['b']*exp(l2*t) + C3*r['b']*exp(l3*t) \ 

+ C4*r['b']*exp(l4*t) 

gsol2 = C1*(l1**2-r['a'])*exp(l1*t) + C2*(l2**2-r['a'])*exp(l2*t) + \ 

C3*(l3**2-r['a'])*exp(l3*t) + C4*(l4**2-r['a'])*exp(l4*t) 

else: 

if r['a'] != r['d']: 

k = sqrt(2*(r['a']+r['d'])) 

mid = r['b']*t+2*r['b']*k/(r['a']-r['d']) 

gsol1 = 2*C1*mid*exp(k*t/2) + 2*C2*mid*exp(-k*t/2) + \ 

2*r['b']*C3*t*exp(k*t/2) + 2*r['b']*C4*t*exp(-k*t/2) 

gsol2 = C1*(r['d']-r['a'])*t*exp(k*t/2) + C2*(r['d']-r['a'])*t*exp(-k*t/2) + \ 

C3*((r['d']-r['a'])*t+2*k)*exp(k*t/2) + C4*((r['d']-r['a'])*t-2*k)*exp(-k*t/2) 

elif r['a'] == r['d'] != 0 and r['b'] == 0: 

sa = sqrt(r['a']) 

gsol1 = 2*sa*C1*exp(sa*t) + 2*sa*C2*exp(-sa*t) 

gsol2 = r['c']*C1*t*exp(sa*t)-r['c']*C2*t*exp(-sa*t)+C3*exp(sa*t)+C4*exp(-sa*t) 

elif r['a'] == r['d'] != 0 and r['c'] == 0: 

sa = sqrt(r['a']) 

gsol1 = r['b']*C1*t*exp(sa*t)-r['b']*C2*t*exp(-sa*t)+C3*exp(sa*t)+C4*exp(-sa*t) 

gsol2 = 2*sa*C1*exp(sa*t) + 2*sa*C2*exp(-sa*t) 

elif (r['a']*r['d'] - r['b']*r['c']) == 0 and (r['a']**2 + r['b']**2) > 0: 

k = r['c']/r['a'] 

if r['a'] + r['b']*k != 0: 

mid = sqrt(r['a'] + r['b']*k) 

gsol1 = C1*exp(mid*t) + C2*exp(-mid*t) + C3*r['b']*t + C4*r['b'] 

gsol2 = C1*k*exp(mid*t) + C2*k*exp(-mid*t) - C3*r['a']*t - C4*r['a'] 

else: 

gsol1 = C1*r['b']*t**3 + C2*r['b']*t**2 + C3*t + C4 

gsol2 = k*gsol1 + 6*C1*t + 2*C2 

return [Eq(x(t), gsol1), Eq(y(t), gsol2)] 

 

def _linear_2eq_order2_type2(x, y, t, r, eq): 

r""" 

The equations in this type are 

 

.. math:: x'' = a_1 x + b_1 y + c_1 

 

.. math:: y'' = a_2 x + b_2 y + c_2 

 

The general solution of this system is given by the sum of its particular solution 

and the general solution of the homogeneous system. The general solution is given 

by the linear system of 2 equation of order 2 and type 1 

 

1. If `a_1 b_2 - a_2 b_1 \neq 0`. A particular solution will be `x = x_0` and `y = y_0` 

where the constants `x_0` and `y_0` are determined by solving the linear algebraic system 

 

.. math:: a_1 x_0 + b_1 y_0 + c_1 = 0, a_2 x_0 + b_2 y_0 + c_2 = 0 

 

2. If `a_1 b_2 - a_2 b_1 = 0` and `a_1^2 + b_1^2 > 0`. In this case, the system in question becomes 

 

.. math:: x'' = ax + by + c_1, y'' = k (ax + by) + c_2 

 

2.1. If `\sigma = a + bk \neq 0`, the particular solution will be 

 

.. math:: x = \frac{1}{2} b \sigma^{-1} (c_1 k - c_2) t^2 - \sigma^{-2} (a c_1 + b c_2) 

 

.. math:: y = kx + \frac{1}{2} (c_2 - c_1 k) t^2 

 

2.2. If `\sigma = a + bk = 0`, the particular solution will be 

 

.. math:: x = \frac{1}{24} b (c_2 - c_1 k) t^4 + \frac{1}{2} c_1 t^2 

 

.. math:: y = kx + \frac{1}{2} (c_2 - c_1 k) t^2 

 

""" 

x0, y0 = symbols('x0, y0') 

if r['c1']*r['d2'] - r['c2']*r['d1'] != 0: 

sol = solve((r['c1']*x0+r['d1']*y0+r['e1'], r['c2']*x0+r['d2']*y0+r['e2']), x0, y0) 

psol = [sol[x0], sol[y0]] 

elif r['c1']*r['d2'] - r['c2']*r['d1'] == 0 and (r['c1']**2 + r['d1']**2) > 0: 

k = r['c2']/r['c1'] 

sig = r['c1'] + r['d1']*k 

if sig != 0: 

psol1 = r['d1']*sig**-1*(r['e1']*k-r['e2'])*t**2/2 - \ 

sig**-2*(r['c1']*r['e1']+r['d1']*r['e2']) 

psol2 = k*psol1 + (r['e2'] - r['e1']*k)*t**2/2 

psol = [psol1, psol2] 

else: 

psol1 = r['d1']*(r['e2']-r['e1']*k)*t**4/24 + r['e1']*t**2/2 

psol2 = k*psol1 + (r['e2']-r['e1']*k)*t**2/2 

psol = [psol1, psol2] 

return psol 

 

def _linear_2eq_order2_type3(x, y, t, r, eq): 

r""" 

These type of equation is used for describing the horizontal motion of a pendulum 

taking into account the Earth rotation. 

The solution is given with `a^2 + 4b > 0`: 

 

.. math:: x = C_1 \cos(\alpha t) + C_2 \sin(\alpha t) + C_3 \cos(\beta t) + C_4 \sin(\beta t) 

 

.. math:: y = -C_1 \sin(\alpha t) + C_2 \cos(\alpha t) - C_3 \sin(\beta t) + C_4 \cos(\beta t) 

 

where `C_1,...,C_4` and 

 

.. math:: \alpha = \frac{1}{2} a + \frac{1}{2} \sqrt{a^2 + 4b}, \beta = \frac{1}{2} a - \frac{1}{2} \sqrt{a^2 + 4b} 

 

""" 

C1, C2, C3, C4 = get_numbered_constants(eq, num=4) 

if r['b1']**2 - 4*r['c1'] > 0: 

r['a'] = r['b1'] ; r['b'] = -r['c1'] 

alpha = r['a']/2 + sqrt(r['a']**2 + 4*r['b'])/2 

beta = r['a']/2 - sqrt(r['a']**2 + 4*r['b'])/2 

sol1 = C1*cos(alpha*t) + C2*sin(alpha*t) + C3*cos(beta*t) + C4*sin(beta*t) 

sol2 = -C1*sin(alpha*t) + C2*cos(alpha*t) - C3*sin(beta*t) + C4*cos(beta*t) 

return [Eq(x(t), sol1), Eq(y(t), sol2)] 

 

def _linear_2eq_order2_type4(x, y, t, r, eq): 

r""" 

These equations are found in the theory of oscillations 

 

.. math:: x'' + a_1 x' + b_1 y' + c_1 x + d_1 y = k_1 e^{i \omega t} 

 

.. math:: y'' + a_2 x' + b_2 y' + c_2 x + d_2 y = k_2 e^{i \omega t} 

 

The general solution of this linear nonhomogeneous system of constant-coefficient 

differential equations is given by the sum of its particular solution and the 

general solution of the corresponding homogeneous system (with `k_1 = k_2 = 0`) 

 

1. A particular solution is obtained by the method of undetermined coefficients: 

 

.. math:: x = A_* e^{i \omega t}, y = B_* e^{i \omega t} 

 

On substituting these expressions into the original system of differential equations, 

one arrive at a linear nonhomogeneous system of algebraic equations for the 

coefficients `A` and `B`. 

 

2. The general solution of the homogeneous system of differential equations is determined 

by a linear combination of linearly independent particular solutions determined by 

the method of undetermined coefficients in the form of exponentials: 

 

.. math:: x = A e^{\lambda t}, y = B e^{\lambda t} 

 

On substituting these expressions into the original system and collecting the 

coefficients of the unknown `A` and `B`, one obtains 

 

.. math:: (\lambda^{2} + a_1 \lambda + c_1) A + (b_1 \lambda + d_1) B = 0 

 

.. math:: (a_2 \lambda + c_2) A + (\lambda^{2} + b_2 \lambda + d_2) B = 0 

 

The determinant of this system must vanish for nontrivial solutions A, B to exist. 

This requirement results in the following characteristic equation for `\lambda` 

 

.. math:: (\lambda^2 + a_1 \lambda + c_1) (\lambda^2 + b_2 \lambda + d_2) - (b_1 \lambda + d_1) (a_2 \lambda + c_2) = 0 

 

If all roots `k_1,...,k_4` of this equation are distinct, the general solution of the original 

system of the differential equations has the form 

 

.. math:: x = C_1 (b_1 \lambda_1 + d_1) e^{\lambda_1 t} - C_2 (b_1 \lambda_2 + d_1) e^{\lambda_2 t} - C_3 (b_1 \lambda_3 + d_1) e^{\lambda_3 t} - C_4 (b_1 \lambda_4 + d_1) e^{\lambda_4 t} 

 

.. math:: y = C_1 (\lambda_1^{2} + a_1 \lambda_1 + c_1) e^{\lambda_1 t} + C_2 (\lambda_2^{2} + a_1 \lambda_2 + c_1) e^{\lambda_2 t} + C_3 (\lambda_3^{2} + a_1 \lambda_3 + c_1) e^{\lambda_3 t} + C_4 (\lambda_4^{2} + a_1 \lambda_4 + c_1) e^{\lambda_4 t} 

 

""" 

C1, C2, C3, C4 = get_numbered_constants(eq, num=4) 

k = Symbol('k') 

Ra, Ca, Rb, Cb = symbols('Ra, Ca, Rb, Cb') 

a1 = r['a1'] ; a2 = r['a2'] 

b1 = r['b1'] ; b2 = r['b2'] 

c1 = r['c1'] ; c2 = r['c2'] 

d1 = r['d1'] ; d2 = r['d2'] 

k1 = r['e1'].expand().as_independent(t)[0] 

k2 = r['e2'].expand().as_independent(t)[0] 

ew1 = r['e1'].expand().as_independent(t)[1] 

ew2 = powdenest(ew1).as_base_exp()[1] 

ew3 = collect(ew2, t).coeff(t) 

w = cancel(ew3/I) 

# The particular solution is assumed to be (Ra+I*Ca)*exp(I*w*t) and 

# (Rb+I*Cb)*exp(I*w*t) for x(t) and y(t) respectively 

peq1 = (-w**2+c1)*Ra - a1*w*Ca + d1*Rb - b1*w*Cb - k1 

peq2 = a1*w*Ra + (-w**2+c1)*Ca + b1*w*Rb + d1*Cb 

peq3 = c2*Ra - a2*w*Ca + (-w**2+d2)*Rb - b2*w*Cb - k2 

peq4 = a2*w*Ra + c2*Ca + b2*w*Rb + (-w**2+d2)*Cb 

# FIXME: solve for what in what? Ra, Rb, etc I guess 

# but then psol not used for anything? 

psol = solve([peq1, peq2, peq3, peq4]) 

 

chareq = (k**2+a1*k+c1)*(k**2+b2*k+d2) - (b1*k+d1)*(a2*k+c2) 

[k1, k2, k3, k4] = roots_quartic(Poly(chareq)) 

sol1 = -C1*(b1*k1+d1)*exp(k1*t) - C2*(b1*k2+d1)*exp(k2*t) - \ 

C3*(b1*k3+d1)*exp(k3*t) - C4*(b1*k4+d1)*exp(k4*t) + (Ra+I*Ca)*exp(I*w*t) 

 

a1_ = (a1-1) 

sol2 = C1*(k1**2+a1_*k1+c1)*exp(k1*t) + C2*(k2**2+a1_*k2+c1)*exp(k2*t) + \ 

C3*(k3**2+a1_*k3+c1)*exp(k3*t) + C4*(k4**2+a1_*k4+c1)*exp(k4*t) + (Rb+I*Cb)*exp(I*w*t) 

return [Eq(x(t), sol1), Eq(y(t), sol2)] 

 

def _linear_2eq_order2_type5(x, y, t, r, eq): 

r""" 

The equation which come under this category are 

 

.. math:: x'' = a (t y' - y) 

 

.. math:: y'' = b (t x' - x) 

 

The transformation 

 

.. math:: u = t x' - x, b = t y' - y 

 

leads to the first-order system 

 

.. math:: u' = atv, v' = btu 

 

The general solution of this system is given by 

 

If `ab > 0`: 

 

.. math:: u = C_1 a e^{\frac{1}{2} \sqrt{ab} t^2} + C_2 a e^{-\frac{1}{2} \sqrt{ab} t^2} 

 

.. math:: v = C_1 \sqrt{ab} e^{\frac{1}{2} \sqrt{ab} t^2} - C_2 \sqrt{ab} e^{-\frac{1}{2} \sqrt{ab} t^2} 

 

If `ab < 0`: 

 

.. math:: u = C_1 a \cos(\frac{1}{2} \sqrt{\left|ab\right|} t^2) + C_2 a \sin(-\frac{1}{2} \sqrt{\left|ab\right|} t^2) 

 

.. math:: v = C_1 \sqrt{\left|ab\right|} \sin(\frac{1}{2} \sqrt{\left|ab\right|} t^2) + C_2 \sqrt{\left|ab\right|} \cos(-\frac{1}{2} \sqrt{\left|ab\right|} t^2) 

 

where `C_1` and `C_2` are arbitrary constants. On substituting the value of `u` and `v` 

in above equations and integrating the resulting expressions, the general solution will become 

 

.. math:: x = C_3 t + t \int \frac{u}{t^2} \,dt, y = C_4 t + t \int \frac{u}{t^2} \,dt 

 

where `C_3` and `C_4` are arbitrary constants. 

 

""" 

C1, C2, C3, C4 = get_numbered_constants(eq, num=4) 

r['a'] = -r['d1'] ; r['b'] = -r['c2'] 

mul = sqrt(abs(r['a']*r['b'])) 

if r['a']*r['b'] > 0: 

u = C1*r['a']*exp(mul*t**2/2) + C2*r['a']*exp(-mul*t**2/2) 

v = C1*mul*exp(mul*t**2/2) - C2*mul*exp(-mul*t**2/2) 

else: 

u = C1*r['a']*cos(mul*t**2/2) + C2*r['a']*sin(mul*t**2/2) 

v = -C1*mul*sin(mul*t**2/2) + C2*mul*cos(mul*t**2/2) 

sol1 = C3*t + t*Integral(u/t**2, t) 

sol2 = C4*t + t*Integral(v/t**2, t) 

return [Eq(x(t), sol1), Eq(y(t), sol2)] 

 

def _linear_2eq_order2_type6(x, y, t, r, eq): 

r""" 

The equations are 

 

.. math:: x'' = f(t) (a_1 x + b_1 y) 

 

.. math:: y'' = f(t) (a_2 x + b_2 y) 

 

If `k_1` and `k_2` are roots of the quadratic equation 

 

.. math:: k^2 - (a_1 + b_2) k + a_1 b_2 - a_2 b_1 = 0 

 

Then by multiplying appropriate constants and adding together original equations 

we obtain two independent equations: 

 

.. math:: z_1'' = k_1 f(t) z_1, z_1 = a_2 x + (k_1 - a_1) y 

 

.. math:: z_2'' = k_2 f(t) z_2, z_2 = a_2 x + (k_2 - a_1) y 

 

Solving the equations will give the values of `x` and `y` after obtaining the value 

of `z_1` and `z_2` by solving the differential equation and substituting the result. 

 

""" 

C1, C2, C3, C4 = get_numbered_constants(eq, num=4) 

k = Symbol('k') 

z = Function('z') 

num, den = cancel( 

(r['c1']*x(t) + r['d1']*y(t))/ 

(r['c2']*x(t) + r['d2']*y(t))).as_numer_denom() 

f = r['c1']/num.coeff(x(t)) 

a1 = num.coeff(x(t)) 

b1 = num.coeff(y(t)) 

a2 = den.coeff(x(t)) 

b2 = den.coeff(y(t)) 

chareq = k**2 - (a1 + b2)*k + a1*b2 - a2*b1 

k1, k2 = [rootof(chareq, k) for k in range(Poly(chareq).degree())] 

z1 = dsolve(diff(z(t),t,t) - k1*f*z(t)).rhs 

z2 = dsolve(diff(z(t),t,t) - k2*f*z(t)).rhs 

sol1 = (k1*z2 - k2*z1 + a1*(z1 - z2))/(a2*(k1-k2)) 

sol2 = (z1 - z2)/(k1 - k2) 

return [Eq(x(t), sol1), Eq(y(t), sol2)] 

 

def _linear_2eq_order2_type7(x, y, t, r, eq): 

r""" 

The equations are given as 

 

.. math:: x'' = f(t) (a_1 x' + b_1 y') 

 

.. math:: y'' = f(t) (a_2 x' + b_2 y') 

 

If `k_1` and 'k_2` are roots of the quadratic equation 

 

.. math:: k^2 - (a_1 + b_2) k + a_1 b_2 - a_2 b_1 = 0 

 

Then the system can be reduced by adding together the two equations multiplied 

by appropriate constants give following two independent equations: 

 

.. math:: z_1'' = k_1 f(t) z_1', z_1 = a_2 x + (k_1 - a_1) y 

 

.. math:: z_2'' = k_2 f(t) z_2', z_2 = a_2 x + (k_2 - a_1) y 

 

Integrating these and returning to the original variables, one arrives at a linear 

algebraic system for the unknowns `x` and `y`: 

 

.. math:: a_2 x + (k_1 - a_1) y = C_1 \int e^{k_1 F(t)} \,dt + C_2 

 

.. math:: a_2 x + (k_2 - a_1) y = C_3 \int e^{k_2 F(t)} \,dt + C_4 

 

where `C_1,...,C_4` are arbitrary constants and `F(t) = \int f(t) \,dt` 

 

""" 

C1, C2, C3, C4 = get_numbered_constants(eq, num=4) 

k = Symbol('k') 

num, den = cancel( 

(r['a1']*x(t) + r['b1']*y(t))/ 

(r['a2']*x(t) + r['b2']*y(t))).as_numer_denom() 

f = r['a1']/num.coeff(x(t)) 

a1 = num.coeff(x(t)) 

b1 = num.coeff(y(t)) 

a2 = den.coeff(x(t)) 

b2 = den.coeff(y(t)) 

chareq = k**2 - (a1 + b2)*k + a1*b2 - a2*b1 

[k1, k2] = [rootof(chareq, k) for k in range(Poly(chareq).degree())] 

F = Integral(f, t) 

z1 = C1*Integral(exp(k1*F), t) + C2 

z2 = C3*Integral(exp(k2*F), t) + C4 

sol1 = (k1*z2 - k2*z1 + a1*(z1 - z2))/(a2*(k1-k2)) 

sol2 = (z1 - z2)/(k1 - k2) 

return [Eq(x(t), sol1), Eq(y(t), sol2)] 

 

def _linear_2eq_order2_type8(x, y, t, r, eq): 

r""" 

The equation of this category are 

 

.. math:: x'' = a f(t) (t y' - y) 

 

.. math:: y'' = b f(t) (t x' - x) 

 

The transformation 

 

.. math:: u = t x' - x, v = t y' - y 

 

leads to the system of first-order equations 

 

.. math:: u' = a t f(t) v, v' = b t f(t) u 

 

The general solution of this system has the form 

 

If `ab > 0`: 

 

.. math:: u = C_1 a e^{\sqrt{ab} \int t f(t) \,dt} + C_2 a e^{-\sqrt{ab} \int t f(t) \,dt} 

 

.. math:: v = C_1 \sqrt{ab} e^{\sqrt{ab} \int t f(t) \,dt} - C_2 \sqrt{ab} e^{-\sqrt{ab} \int t f(t) \,dt} 

 

If `ab < 0`: 

 

.. math:: u = C_1 a \cos(\sqrt{\left|ab\right|} \int t f(t) \,dt) + C_2 a \sin(-\sqrt{\left|ab\right|} \int t f(t) \,dt) 

 

.. math:: v = C_1 \sqrt{\left|ab\right|} \sin(\sqrt{\left|ab\right|} \int t f(t) \,dt) + C_2 \sqrt{\left|ab\right|} \cos(-\sqrt{\left|ab\right|} \int t f(t) \,dt) 

 

where `C_1` and `C_2` are arbitrary constants. On substituting the value of `u` and `v` 

in above equations and integrating the resulting expressions, the general solution will become 

 

.. math:: x = C_3 t + t \int \frac{u}{t^2} \,dt, y = C_4 t + t \int \frac{u}{t^2} \,dt 

 

where `C_3` and `C_4` are arbitrary constants. 

 

""" 

C1, C2, C3, C4 = get_numbered_constants(eq, num=4) 

num, den = cancel(r['d1']/r['c2']).as_numer_denom() 

f = -r['d1']/num 

a = num 

b = den 

mul = sqrt(abs(a*b)) 

Igral = Integral(t*f, t) 

if a*b > 0: 

u = C1*a*exp(mul*Igral) + C2*a*exp(-mul*Igral) 

v = C1*mul*exp(mul*Igral) - C2*mul*exp(-mul*Igral) 

else: 

u = C1*a*cos(mul*Igral) + C2*a*sin(mul*Igral) 

v = -C1*mul*sin(mul*Igral) + C2*mul*cos(mul*Igral) 

sol1 = C3*t + t*Integral(u/t**2, t) 

sol2 = C4*t + t*Integral(v/t**2, t) 

return [Eq(x(t), sol1), Eq(y(t), sol2)] 

 

def _linear_2eq_order2_type9(x, y, t, r, eq): 

r""" 

.. math:: t^2 x'' + a_1 t x' + b_1 t y' + c_1 x + d_1 y = 0 

 

.. math:: t^2 y'' + a_2 t x' + b_2 t y' + c_2 x + d_2 y = 0 

 

These system of equations are euler type. 

 

The substitution of `t = \sigma e^{\tau} (\sigma \neq 0)` leads to the system of constant 

coefficient linear differential equations 

 

.. math:: x'' + (a_1 - 1) x' + b_1 y' + c_1 x + d_1 y = 0 

 

.. math:: y'' + a_2 x' + (b_2 - 1) y' + c_2 x + d_2 y = 0 

 

The general solution of the homogeneous system of differential equations is determined 

by a linear combination of linearly independent particular solutions determined by 

the method of undetermined coefficients in the form of exponentials 

 

.. math:: x = A e^{\lambda t}, y = B e^{\lambda t} 

 

On substituting these expressions into the original system and collecting the 

coefficients of the unknown `A` and `B`, one obtains 

 

.. math:: (\lambda^{2} + (a_1 - 1) \lambda + c_1) A + (b_1 \lambda + d_1) B = 0 

 

.. math:: (a_2 \lambda + c_2) A + (\lambda^{2} + (b_2 - 1) \lambda + d_2) B = 0 

 

The determinant of this system must vanish for nontrivial solutions A, B to exist. 

This requirement results in the following characteristic equation for `\lambda` 

 

.. math:: (\lambda^2 + (a_1 - 1) \lambda + c_1) (\lambda^2 + (b_2 - 1) \lambda + d_2) - (b_1 \lambda + d_1) (a_2 \lambda + c_2) = 0 

 

If all roots `k_1,...,k_4` of this equation are distinct, the general solution of the original 

system of the differential equations has the form 

 

.. math:: x = C_1 (b_1 \lambda_1 + d_1) e^{\lambda_1 t} - C_2 (b_1 \lambda_2 + d_1) e^{\lambda_2 t} - C_3 (b_1 \lambda_3 + d_1) e^{\lambda_3 t} - C_4 (b_1 \lambda_4 + d_1) e^{\lambda_4 t} 

 

.. math:: y = C_1 (\lambda_1^{2} + (a_1 - 1) \lambda_1 + c_1) e^{\lambda_1 t} + C_2 (\lambda_2^{2} + (a_1 - 1) \lambda_2 + c_1) e^{\lambda_2 t} + C_3 (\lambda_3^{2} + (a_1 - 1) \lambda_3 + c_1) e^{\lambda_3 t} + C_4 (\lambda_4^{2} + (a_1 - 1) \lambda_4 + c_1) e^{\lambda_4 t} 

 

""" 

C1, C2, C3, C4 = get_numbered_constants(eq, num=4) 

k = Symbol('k') 

a1 = -r['a1']*t; a2 = -r['a2']*t 

b1 = -r['b1']*t; b2 = -r['b2']*t 

c1 = -r['c1']*t**2; c2 = -r['c2']*t**2 

d1 = -r['d1']*t**2; d2 = -r['d2']*t**2 

eq = (k**2+(a1-1)*k+c1)*(k**2+(b2-1)*k+d2)-(b1*k+d1)*(a2*k+c2) 

[k1, k2, k3, k4] = roots_quartic(Poly(eq)) 

sol1 = -C1*(b1*k1+d1)*exp(k1*log(t)) - C2*(b1*k2+d1)*exp(k2*log(t)) - \ 

C3*(b1*k3+d1)*exp(k3*log(t)) - C4*(b1*k4+d1)*exp(k4*log(t)) 

 

a1_ = (a1-1) 

sol2 = C1*(k1**2+a1_*k1+c1)*exp(k1*log(t)) + C2*(k2**2+a1_*k2+c1)*exp(k2*log(t)) \ 

+ C3*(k3**2+a1_*k3+c1)*exp(k3*log(t)) + C4*(k4**2+a1_*k4+c1)*exp(k4*log(t)) 

 

return [Eq(x(t), sol1), Eq(y(t), sol2)] 

 

def _linear_2eq_order2_type10(x, y, t, r, eq): 

r""" 

The equation of this category are 

 

.. math:: (\alpha t^2 + \beta t + \gamma)^{2} x'' = ax + by 

 

.. math:: (\alpha t^2 + \beta t + \gamma)^{2} y'' = cx + dy 

 

The transformation 

 

.. math:: \tau = \int \frac{1}{\alpha t^2 + \beta t + \gamma} \,dt , u = \frac{x}{\sqrt{\left|\alpha t^2 + \beta t + \gamma\right|}} , v = \frac{y}{\sqrt{\left|\alpha t^2 + \beta t + \gamma\right|}} 

 

leads to a constant coefficient linear system of equations 

 

.. math:: u'' = (a - \alpha \gamma + \frac{1}{4} \beta^{2}) u + b v 

 

.. math:: v'' = c u + (d - \alpha \gamma + \frac{1}{4} \beta^{2}) v 

 

These system of equations obtained can be solved by type1 of System of two 

constant-coefficient second-order linear homogeneous differential equations. 

 

""" 

C1, C2, C3, C4 = get_numbered_constants(eq, num=4) 

u, v = symbols('u, v', function=True) 

T = Symbol('T') 

p = Wild('p', exclude=[t, t**2]) 

q = Wild('q', exclude=[t, t**2]) 

s = Wild('s', exclude=[t, t**2]) 

n = Wild('n', exclude=[t, t**2]) 

num, den = r['c1'].as_numer_denom() 

dic = den.match((n*(p*t**2+q*t+s)**2).expand()) 

eqz = dic[p]*t**2 + dic[q]*t + dic[s] 

a = num/dic[n] 

b = cancel(r['d1']*eqz**2) 

c = cancel(r['c2']*eqz**2) 

d = cancel(r['d2']*eqz**2) 

[msol1, msol2] = dsolve([Eq(diff(u(t), t, t), (a - dic[p]*dic[s] + dic[q]**2/4)*u(t) \ 

+ b*v(t)), Eq(diff(v(t),t,t), c*u(t) + (d - dic[p]*dic[s] + dic[q]**2/4)*v(t))]) 

sol1 = (msol1.rhs*sqrt(abs(eqz))).subs(t, Integral(1/eqz, t)) 

sol2 = (msol2.rhs*sqrt(abs(eqz))).subs(t, Integral(1/eqz, t)) 

return [Eq(x(t), sol1), Eq(y(t), sol2)] 

 

def _linear_2eq_order2_type11(x, y, t, r, eq): 

r""" 

The equations which comes under this type are 

 

.. math:: x'' = f(t) (t x' - x) + g(t) (t y' - y) 

 

.. math:: y'' = h(t) (t x' - x) + p(t) (t y' - y) 

 

The transformation 

 

.. math:: u = t x' - x, v = t y' - y 

 

leads to the linear system of first-order equations 

 

.. math:: u' = t f(t) u + t g(t) v, v' = t h(t) u + t p(t) v 

 

On substituting the value of `u` and `v` in transformed equation gives value of `x` and `y` as 

 

.. math:: x = C_3 t + t \int \frac{u}{t^2} \,dt , y = C_4 t + t \int \frac{v}{t^2} \,dt. 

 

where `C_3` and `C_4` are arbitrary constants. 

 

""" 

C1, C2, C3, C4 = get_numbered_constants(eq, num=4) 

u, v = symbols('u, v', function=True) 

f = -r['c1'] ; g = -r['d1'] 

h = -r['c2'] ; p = -r['d2'] 

[msol1, msol2] = dsolve([Eq(diff(u(t),t), t*f*u(t) + t*g*v(t)), Eq(diff(v(t),t), t*h*u(t) + t*p*v(t))]) 

sol1 = C3*t + t*Integral(msol1.rhs/t**2, t) 

sol2 = C4*t + t*Integral(msol2.rhs/t**2, t) 

return [Eq(x(t), sol1), Eq(y(t), sol2)] 

 

def sysode_linear_3eq_order1(match_): 

x = match_['func'][0].func 

y = match_['func'][1].func 

z = match_['func'][2].func 

func = match_['func'] 

fc = match_['func_coeff'] 

eq = match_['eq'] 

C1, C2, C3, C4 = get_numbered_constants(eq, num=4) 

r = dict() 

t = list(list(eq[0].atoms(Derivative))[0].atoms(Symbol))[0] 

for i in range(3): 

eqs = 0 

for terms in Add.make_args(eq[i]): 

eqs += terms/fc[i,func[i],1] 

eq[i] = eqs 

# for equations: 

# Eq(g1*diff(x(t),t), a1*x(t)+b1*y(t)+c1*z(t)+d1), 

# Eq(g2*diff(y(t),t), a2*x(t)+b2*y(t)+c2*z(t)+d2), and 

# Eq(g3*diff(z(t),t), a3*x(t)+b3*y(t)+c3*z(t)+d3) 

r['a1'] = fc[0,x(t),0]/fc[0,x(t),1]; r['a2'] = fc[1,x(t),0]/fc[1,y(t),1]; 

r['a3'] = fc[2,x(t),0]/fc[2,z(t),1] 

r['b1'] = fc[0,y(t),0]/fc[0,x(t),1]; r['b2'] = fc[1,y(t),0]/fc[1,y(t),1]; 

r['b3'] = fc[2,y(t),0]/fc[2,z(t),1] 

r['c1'] = fc[0,z(t),0]/fc[0,x(t),1]; r['c2'] = fc[1,z(t),0]/fc[1,y(t),1]; 

r['c3'] = fc[2,z(t),0]/fc[2,z(t),1] 

for i in range(3): 

for j in Add.make_args(eq[i]): 

if not j.has(x(t), y(t), z(t)): 

raise NotImplementedError("Only homogeneous problems are supported, non-homogenous are not supported currently.") 

if match_['type_of_equation'] == 'type1': 

sol = _linear_3eq_order1_type1(x, y, z, t, r, eq) 

if match_['type_of_equation'] == 'type2': 

sol = _linear_3eq_order1_type2(x, y, z, t, r, eq) 

if match_['type_of_equation'] == 'type3': 

sol = _linear_3eq_order1_type3(x, y, z, t, r, eq) 

if match_['type_of_equation'] == 'type4': 

sol = _linear_3eq_order1_type4(x, y, z, t, r, eq) 

if match_['type_of_equation'] == 'type6': 

sol = _linear_neq_order1_type1(match_) 

return sol 

 

def _linear_3eq_order1_type1(x, y, z, t, r, eq): 

r""" 

.. math:: x' = ax 

 

.. math:: y' = bx + cy 

 

.. math:: z' = dx + ky + pz 

 

Solution of such equations are forward substitution. Solving first equations 

gives the value of `x`, substituting it in second and third equation and 

solving second equation gives `y` and similarly substituting `y` in third 

equation give `z`. 

 

.. math:: x = C_1 e^{at} 

 

.. math:: y = \frac{b C_1}{a - c} e^{at} + C_2 e^{ct} 

 

.. math:: z = \frac{C_1}{a - p} (d + \frac{bk}{a - c}) e^{at} + \frac{k C_2}{c - p} e^{ct} + C_3 e^{pt} 

 

where `C_1, C_2` and `C_3` are arbitrary constants. 

 

""" 

C1, C2, C3, C4 = get_numbered_constants(eq, num=4) 

a = -r['a1']; b = -r['a2']; c = -r['b2'] 

d = -r['a3']; k = -r['b3']; p = -r['c3'] 

sol1 = C1*exp(a*t) 

sol2 = b*C1*exp(a*t)/(a-c) + C2*exp(c*t) 

sol3 = C1*(d+b*k/(a-c))*exp(a*t)/(a-p) + k*C2*exp(c*t)/(c-p) + C3*exp(p*t) 

return [Eq(x(t), sol1), Eq(y(t), sol2), Eq(z(t), sol3)] 

 

def _linear_3eq_order1_type2(x, y, z, t, r, eq): 

r""" 

The equations of this type are 

 

.. math:: x' = cy - bz 

 

.. math:: y' = az - cx 

 

.. math:: z' = bx - ay 

 

1. First integral: 

 

.. math:: ax + by + cz = A \qquad - (1) 

 

.. math:: x^2 + y^2 + z^2 = B^2 \qquad - (2) 

 

where `A` and `B` are arbitrary constants. It follows from these integrals 

that the integral lines are circles formed by the intersection of the planes 

`(1)` and sphere `(2)` 

 

2. Solution: 

 

.. math:: x = a C_0 + k C_1 \cos(kt) + (c C_2 - b C_3) \sin(kt) 

 

.. math:: y = b C_0 + k C_2 \cos(kt) + (a C_2 - c C_3) \sin(kt) 

 

.. math:: z = c C_0 + k C_3 \cos(kt) + (b C_2 - a C_3) \sin(kt) 

 

where `k = \sqrt{a^2 + b^2 + c^2}` and the four constants of integration, 

`C_1,...,C_4` are constrained by a single relation, 

 

.. math:: a C_1 + b C_2 + c C_3 = 0 

 

""" 

C0, C1, C2, C3 = get_numbered_constants(eq, num=4, start=0) 

a = -r['c2']; b = -r['a3']; c = -r['b1'] 

k = sqrt(a**2 + b**2 + c**2) 

C3 = (-a*C1 - b*C2)/c 

sol1 = a*C0 + k*C1*cos(k*t) + (c*C2-b*C3)*sin(k*t) 

sol2 = b*C0 + k*C2*cos(k*t) + (a*C3-c*C1)*sin(k*t) 

sol3 = c*C0 + k*C3*cos(k*t) + (b*C1-a*C2)*sin(k*t) 

return [Eq(x(t), sol1), Eq(y(t), sol2), Eq(z(t), sol3)] 

 

def _linear_3eq_order1_type3(x, y, z, t, r, eq): 

r""" 

Equations of this system of ODEs 

 

.. math:: a x' = bc (y - z) 

 

.. math:: b y' = ac (z - x) 

 

.. math:: c z' = ab (x - y) 

 

1. First integral: 

 

.. math:: a^2 x + b^2 y + c^2 z = A 

 

where A is an arbitrary constant. It follows that the integral lines are plane curves. 

 

2. Solution: 

 

.. math:: x = C_0 + k C_1 \cos(kt) + a^{-1} bc (C_2 - C_3) \sin(kt) 

 

.. math:: y = C_0 + k C_2 \cos(kt) + a b^{-1} c (C_3 - C_1) \sin(kt) 

 

.. math:: z = C_0 + k C_3 \cos(kt) + ab c^{-1} (C_1 - C_2) \sin(kt) 

 

where `k = \sqrt{a^2 + b^2 + c^2}` and the four constants of integration, 

`C_1,...,C_4` are constrained by a single relation 

 

.. math:: a^2 C_1 + b^2 C_2 + c^2 C_3 = 0 

 

""" 

C0, C1, C2, C3 = get_numbered_constants(eq, num=4, start=0) 

c = sqrt(r['b1']*r['c2']) 

b = sqrt(r['b1']*r['a3']) 

a = sqrt(r['c2']*r['a3']) 

C3 = (-a**2*C1-b**2*C2)/c**2 

k = sqrt(a**2 + b**2 + c**2) 

sol1 = C0 + k*C1*cos(k*t) + a**-1*b*c*(C2-C3)*sin(k*t) 

sol2 = C0 + k*C2*cos(k*t) + a*b**-1*c*(C3-C1)*sin(k*t) 

sol3 = C0 + k*C3*cos(k*t) + a*b*c**-1*(C1-C2)*sin(k*t) 

return [Eq(x(t), sol1), Eq(y(t), sol2), Eq(z(t), sol3)] 

 

def _linear_3eq_order1_type4(x, y, z, t, r, eq): 

r""" 

Equations: 

 

.. math:: x' = (a_1 f(t) + g(t)) x + a_2 f(t) y + a_3 f(t) z 

 

.. math:: y' = b_1 f(t) x + (b_2 f(t) + g(t)) y + b_3 f(t) z 

 

.. math:: z' = c_1 f(t) x + c_2 f(t) y + (c_3 f(t) + g(t)) z 

 

The transformation 

 

.. math:: x = e^{\int g(t) \,dt} u, y = e^{\int g(t) \,dt} v, z = e^{\int g(t) \,dt} w, \tau = \int f(t) \,dt 

 

leads to the system of constant coefficient linear differential equations 

 

.. math:: u' = a_1 u + a_2 v + a_3 w 

 

.. math:: v' = b_1 u + b_2 v + b_3 w 

 

.. math:: w' = c_1 u + c_2 v + c_3 w 

 

These system of equations are solved by homogeneous linear system of constant 

coefficients of `n` equations of first order. Then substituting the value of 

`u, v` and `w` in transformed equation gives value of `x, y` and `z`. 

 

""" 

u, v, w = symbols('u, v, w', function=True) 

a2, a3 = cancel(r['b1']/r['c1']).as_numer_denom() 

f = cancel(r['b1']/a2) 

b1 = cancel(r['a2']/f); b3 = cancel(r['c2']/f) 

c1 = cancel(r['a3']/f); c2 = cancel(r['b3']/f) 

a1, g = div(r['a1'],f) 

b2 = div(r['b2'],f)[0] 

c3 = div(r['c3'],f)[0] 

trans_eq = (diff(u(t),t)-a1*u(t)-a2*v(t)-a3*w(t), diff(v(t),t)-b1*u(t)-\ 

b2*v(t)-b3*w(t), diff(w(t),t)-c1*u(t)-c2*v(t)-c3*w(t)) 

sol = dsolve(trans_eq) 

sol1 = exp(Integral(g,t))*((sol[0].rhs).subs(t, Integral(f,t))) 

sol2 = exp(Integral(g,t))*((sol[1].rhs).subs(t, Integral(f,t))) 

sol3 = exp(Integral(g,t))*((sol[2].rhs).subs(t, Integral(f,t))) 

return [Eq(x(t), sol1), Eq(y(t), sol2), Eq(z(t), sol3)] 

 

def sysode_linear_neq_order1(match_): 

sol = _linear_neq_order1_type1(match_) 

 

def _linear_neq_order1_type1(match_): 

r""" 

System of n first-order constant-coefficient linear nonhomogeneous differential equation 

 

.. math:: y'_k = a_{k1} y_1 + a_{k2} y_2 +...+ a_{kn} y_n; k = 1,2,...,n 

 

or that can be written as `\vec{y'} = A . \vec{y}` 

where `\vec{y}` is matrix of `y_k` for `k = 1,2,...n` and `A` is a `n \times n` matrix. 

 

Since these equations are equivalent to a first order homogeneous linear 

differential equation. So the general solution will contain `n` linearly 

independent parts and solution will consist some type of exponential 

functions. Assuming `y = \vec{v} e^{rt}` is a solution of the system where 

`\vec{v}` is a vector of coefficients of `y_1,...,y_n`. Substituting `y` and 

`y' = r v e^{r t}` into the equation `\vec{y'} = A . \vec{y}`, we get 

 

.. math:: r \vec{v} e^{rt} = A \vec{v} e^{rt} 

 

.. math:: r \vec{v} = A \vec{v} 

 

where `r` comes out to be eigenvalue of `A` and vector `\vec{v}` is the eigenvector 

of `A` corresponding to `r`. There are three possibilities of eigenvalues of `A` 

 

- `n` distinct real eigenvalues 

- complex conjugate eigenvalues 

- eigenvalues with multiplicity `k` 

 

1. When all eigenvalues `r_1,..,r_n` are distinct with `n` different eigenvectors 

`v_1,...v_n` then the solution is given by 

 

.. math:: \vec{y} = C_1 e^{r_1 t} \vec{v_1} + C_2 e^{r_2 t} \vec{v_2} +...+ C_n e^{r_n t} \vec{v_n} 

 

where `C_1,C_2,...,C_n` are arbitrary constants. 

 

2. When some eigenvalues are complex then in order to make the solution real, 

we take a linear combination: if `r = a + bi` has an eigenvector 

`\vec{v} = \vec{w_1} + i \vec{w_2}` then to obtain real-valued solutions to 

the system, replace the complex-valued solutions `e^{rx} \vec{v}` 

with real-valued solution `e^{ax} (\vec{w_1} \cos(bx) - \vec{w_2} \sin(bx))` 

and for `r = a - bi` replace the solution `e^{-r x} \vec{v}` with 

`e^{ax} (\vec{w_1} \sin(bx) + \vec{w_2} \cos(bx))` 

 

3. If some eigenvalues are repeated. Then we get fewer than `n` linearly 

independent eigenvectors, we miss some of the solutions and need to 

construct the missing ones. We do this via generalized eigenvectors, vectors 

which are not eigenvectors but are close enough that we can use to write 

down the remaining solutions. For a eigenvalue `r` with eigenvector `\vec{w}` 

we obtain `\vec{w_2},...,\vec{w_k}` using 

 

.. math:: (A - r I) . \vec{w_2} = \vec{w} 

 

.. math:: (A - r I) . \vec{w_3} = \vec{w_2} 

 

.. math:: \vdots 

 

.. math:: (A - r I) . \vec{w_k} = \vec{w_{k-1}} 

 

Then the solutions to the system for the eigenspace are `e^{rt} [\vec{w}], 

e^{rt} [t \vec{w} + \vec{w_2}], e^{rt} [\frac{t^2}{2} \vec{w} + t \vec{w_2} + \vec{w_3}], 

...,e^{rt} [\frac{t^{k-1}}{(k-1)!} \vec{w} + \frac{t^{k-2}}{(k-2)!} \vec{w_2} +...+ t \vec{w_{k-1}} 

+ \vec{w_k}]` 

 

So, If `\vec{y_1},...,\vec{y_n}` are `n` solution of obtained from three 

categories of `A`, then general solution to the system `\vec{y'} = A . \vec{y}` 

 

.. math:: \vec{y} = C_1 \vec{y_1} + C_2 \vec{y_2} + \cdots + C_n \vec{y_n} 

 

""" 

eq = match_['eq'] 

func = match_['func'] 

fc = match_['func_coeff'] 

n = len(eq) 

t = list(list(eq[0].atoms(Derivative))[0].atoms(Symbol))[0] 

constants = numbered_symbols(prefix='C', cls=Symbol, start=1) 

M = Matrix(n,n,lambda i,j:-fc[i,func[j],0]) 

evector = M.eigenvects(simplify=True) 

def is_complex(mat, root): 

return Matrix(n, 1, lambda i,j: re(mat[i])*cos(im(root)*t) - im(mat[i])*sin(im(root)*t)) 

def is_complex_conjugate(mat, root): 

return Matrix(n, 1, lambda i,j: re(mat[i])*sin(abs(im(root))*t) + im(mat[i])*cos(im(root)*t)*abs(im(root))/im(root)) 

conjugate_root = [] 

e_vector = zeros(n,1) 

for evects in evector: 

if evects[0] not in conjugate_root: 

# If number of column of an eigenvector is not equal to the multiplicity 

# of its eigenvalue then the legt eigenvectors are calculated 

if len(evects[2])!=evects[1]: 

var_mat = Matrix(n, 1, lambda i,j: Symbol('x'+str(i))) 

Mnew = (M - evects[0]*eye(evects[2][-1].rows))*var_mat 

w = [0 for i in range(evects[1])] 

w[0] = evects[2][-1] 

for r in range(1, evects[1]): 

w_ = Mnew - w[r-1] 

sol_dict = solve(list(w_), var_mat[1:]) 

sol_dict[var_mat[0]] = var_mat[0] 

for key, value in sol_dict.items(): 

sol_dict[key] = value.subs(var_mat[0],1) 

w[r] = Matrix(n, 1, lambda i,j: sol_dict[var_mat[i]]) 

evects[2].append(w[r]) 

for i in range(evects[1]): 

C = next(constants) 

for j in range(i+1): 

if evects[0].has(I): 

evects[2][j] = simplify(evects[2][j]) 

e_vector += C*is_complex(evects[2][j], evects[0])*t**(i-j)*exp(re(evects[0])*t)/factorial(i-j) 

C = next(constants) 

e_vector += C*is_complex_conjugate(evects[2][j], evects[0])*t**(i-j)*exp(re(evects[0])*t)/factorial(i-j) 

else: 

e_vector += C*evects[2][j]*t**(i-j)*exp(evects[0]*t)/factorial(i-j) 

if evects[0].has(I): 

conjugate_root.append(conjugate(evects[0])) 

sol = [] 

for i in range(len(eq)): 

sol.append(Eq(func[i],e_vector[i])) 

return sol 

 

def sysode_nonlinear_2eq_order1(match_): 

func = match_['func'] 

eq = match_['eq'] 

fc = match_['func_coeff'] 

t = list(list(eq[0].atoms(Derivative))[0].atoms(Symbol))[0] 

if match_['type_of_equation'] == 'type5': 

sol = _nonlinear_2eq_order1_type5(func, t, eq) 

return sol 

x = func[0].func 

y = func[1].func 

for i in range(2): 

eqs = 0 

for terms in Add.make_args(eq[i]): 

eqs += terms/fc[i,func[i],1] 

eq[i] = eqs 

if match_['type_of_equation'] == 'type1': 

sol = _nonlinear_2eq_order1_type1(x, y, t, eq) 

elif match_['type_of_equation'] == 'type2': 

sol = _nonlinear_2eq_order1_type2(x, y, t, eq) 

elif match_['type_of_equation'] == 'type3': 

sol = _nonlinear_2eq_order1_type3(x, y, t, eq) 

elif match_['type_of_equation'] == 'type4': 

sol = _nonlinear_2eq_order1_type4(x, y, t, eq) 

return sol 

 

def _nonlinear_2eq_order1_type1(x, y, t, eq): 

r""" 

Equations: 

 

.. math:: x' = x^n F(x,y) 

 

.. math:: y' = g(y) F(x,y) 

 

Solution: 

 

.. math:: x = \varphi(y), \int \frac{1}{g(y) F(\varphi(y),y)} \,dy = t + C_2 

 

where 

 

if `n \neq 1` 

 

.. math:: \varphi = [C_1 + (1-n) \int \frac{1}{g(y)} \,dy]^{\frac{1}{1-n}} 

 

if `n = 1` 

 

.. math:: \varphi = C_1 e^{\int \frac{1}{g(y)} \,dy} 

 

where `C_1` and `C_2` are arbitrary constants. 

 

""" 

C1, C2 = get_numbered_constants(eq, num=2) 

n = Wild('n', exclude=[x(t),y(t)]) 

f = Wild('f') 

u, v, phi = symbols('u, v, phi', function=True) 

r = eq[0].match(diff(x(t),t) - x(t)**n*f) 

g = ((diff(y(t),t) - eq[1])/r[f]).subs(y(t),v) 

F = r[f].subs(x(t),u).subs(y(t),v) 

n = r[n] 

if n!=1: 

phi = (C1 + (1-n)*Integral(1/g, v))**(1/(1-n)) 

else: 

phi = C1*exp(Integral(1/g, v)) 

phi = phi.doit() 

sol2 = solve(Integral(1/(g*F.subs(u,phi)), v).doit() - t - C2, v) 

sol = [] 

for sols in sol2: 

sol.append(Eq(x(t),phi.subs(v, sols))) 

sol.append(Eq(y(t), sols)) 

return sol 

 

def _nonlinear_2eq_order1_type2(x, y, t, eq): 

r""" 

Equations: 

 

.. math:: x' = e^{\lambda x} F(x,y) 

 

.. math:: y' = g(y) F(x,y) 

 

Solution: 

 

.. math:: x = \varphi(y), \int \frac{1}{g(y) F(\varphi(y),y)} \,dy = t + C_2 

 

where 

 

if `\lambda \neq 0` 

 

.. math:: \varphi = -\frac{1}{\lambda} log(C_1 - \lambda \int \frac{1}{g(y)} \,dy) 

 

if `\lambda = 0` 

 

.. math:: \varphi = C_1 + \int \frac{1}{g(y)} \,dy 

 

where `C_1` and `C_2` are arbitrary constants. 

 

""" 

C1, C2 = get_numbered_constants(eq, num=2) 

n = Wild('n', exclude=[x(t),y(t)]) 

f = Wild('f') 

u, v, phi = symbols('u, v, phi', function=True) 

r = eq[0].match(diff(x(t),t) - exp(n*x(t))*f) 

g = ((diff(y(t),t) - eq[1])/r[f]).subs(y(t),v) 

F = r[f].subs(x(t),u).subs(y(t),v) 

n = r[n] 

if n: 

phi = -1/n*log(C1 - n*Integral(1/g, v)) 

else: 

phi = C1 + Integral(1/g, v) 

phi = phi.doit() 

sol2 = solve(Integral(1/(g*F.subs(u,phi)), v).doit() - t - C2, v) 

sol = [] 

for sols in sol2: 

sol.append(Eq(x(t),phi.subs(v, sols))) 

sol.append(Eq(y(t), sols)) 

return sol 

 

def _nonlinear_2eq_order1_type3(x, y, t, eq): 

r""" 

Autonomous system of general form 

 

.. math:: x' = F(x,y) 

 

.. math:: y' = G(x,y) 

 

Assuming `y = y(x, C_1)` where `C_1` is an arbitrary constant is the general 

solution of the first-order equation 

 

.. math:: F(x,y) y'_x = G(x,y) 

 

Then the general solution of the original system of equations has the form 

 

.. math:: \int \frac{1}{F(x,y(x,C_1))} \,dx = t + C_1 

 

""" 

C1, C2, C3, C4 = get_numbered_constants(eq, num=4) 

u, v = symbols('u, v', function=True) 

f = Wild('f') 

g = Wild('g') 

r1 = eq[0].match(diff(x(t),t) - f) 

r2 = eq[1].match(diff(y(t),t) - g) 

F = r1[f].subs(x(t),u).subs(y(t),v) 

G = r2[g].subs(x(t),u).subs(y(t),v) 

sol2r = dsolve(Eq(diff(v(u),u), G.subs(v,v(u))/F.subs(v,v(u)))) 

for sol2s in sol2r: 

sol1 = solve(Integral(1/F.subs(v, sol2s.rhs), u).doit() - t - C2, u) 

sol = [] 

for sols in sol1: 

sol.append(Eq(x(t), sols)) 

sol.append(Eq(y(t), (sol2s.rhs).subs(u, sols))) 

return sol 

 

def _nonlinear_2eq_order1_type4(x, y, t, eq): 

r""" 

Equation: 

 

.. math:: x' = f_1(x) g_1(y) \phi(x,y,t) 

 

.. math:: y' = f_2(x) g_2(y) \phi(x,y,t) 

 

First integral: 

 

.. math:: \int \frac{f_2(x)}{f_1(x)} \,dx - \int \frac{g_1(y)}{g_2(y)} \,dy = C 

 

where `C` is an arbitrary constant. 

 

On solving the first integral for `x` (resp., `y` ) and on substituting the 

resulting expression into either equation of the original solution, one 

arrives at a first-order equation for determining `y` (resp., `x` ). 

 

""" 

C1, C2 = get_numbered_constants(eq, num=2) 

u, v = symbols('u, v') 

f = Wild('f') 

g = Wild('g') 

f1 = Wild('f1', exclude=[v,t]) 

f2 = Wild('f2', exclude=[v,t]) 

g1 = Wild('g1', exclude=[u,t]) 

g2 = Wild('g2', exclude=[u,t]) 

r1 = eq[0].match(diff(x(t),t) - f) 

r2 = eq[1].match(diff(y(t),t) - g) 

num, den = ( 

(r1[f].subs(x(t),u).subs(y(t),v))/ 

(r2[g].subs(x(t),u).subs(y(t),v))).as_numer_denom() 

R1 = num.match(f1*g1) 

R2 = den.match(f2*g2) 

phi = (r1[f].subs(x(t),u).subs(y(t),v))/num 

F1 = R1[f1]; F2 = R2[f2] 

G1 = R1[g1]; G2 = R2[g2] 

sol1r = solve(Integral(F2/F1, u).doit() - Integral(G1/G2,v).doit() - C1, u) 

sol2r = solve(Integral(F2/F1, u).doit() - Integral(G1/G2,v).doit() - C1, v) 

sol = [] 

for sols in sol1r: 

sol.append(Eq(y(t), dsolve(diff(v(t),t) - F2.subs(u,sols).subs(v,v(t))*G2.subs(v,v(t))*phi.subs(u,sols).subs(v,v(t))).rhs)) 

for sols in sol2r: 

sol.append(Eq(x(t), dsolve(diff(u(t),t) - F1.subs(u,u(t))*G1.subs(v,sols).subs(u,u(t))*phi.subs(v,sols).subs(u,u(t))).rhs)) 

return set(sol) 

 

def _nonlinear_2eq_order1_type5(func, t, eq): 

r""" 

Clairaut system of ODEs 

 

.. math:: x = t x' + F(x',y') 

 

.. math:: y = t y' + G(x',y') 

 

The following are solutions of the system 

 

`(i)` straight lines: 

 

.. math:: x = C_1 t + F(C_1, C_2), y = C_2 t + G(C_1, C_2) 

 

where `C_1` and `C_2` are arbitrary constants; 

 

`(ii)` envelopes of the above lines; 

 

`(iii)` continuously differentiable lines made up from segments of the lines 

`(i)` and `(ii)`. 

 

""" 

C1, C2 = get_numbered_constants(eq, num=2) 

f = Wild('f') 

g = Wild('g') 

def check_type(x, y): 

r1 = eq[0].match(t*diff(x(t),t) - x(t) + f) 

r2 = eq[1].match(t*diff(y(t),t) - y(t) + g) 

if not (r1 and r2): 

r1 = eq[0].match(diff(x(t),t) - x(t)/t + f/t) 

r2 = eq[1].match(diff(y(t),t) - y(t)/t + g/t) 

if not (r1 and r2): 

r1 = (-eq[0]).match(t*diff(x(t),t) - x(t) + f) 

r2 = (-eq[1]).match(t*diff(y(t),t) - y(t) + g) 

if not (r1 and r2): 

r1 = (-eq[0]).match(diff(x(t),t) - x(t)/t + f/t) 

r2 = (-eq[1]).match(diff(y(t),t) - y(t)/t + g/t) 

return [r1, r2] 

for func_ in func: 

if isinstance(func_, list): 

x = func[0][0].func 

y = func[0][1].func 

[r1, r2] = check_type(x, y) 

if not (r1 and r2): 

[r1, r2] = check_type(y, x) 

x, y = y, x 

x1 = diff(x(t),t); y1 = diff(y(t),t) 

return {Eq(x(t), C1*t + r1[f].subs(x1,C1).subs(y1,C2)), Eq(y(t), C2*t + r2[g].subs(x1,C1).subs(y1,C2))} 

 

def sysode_nonlinear_3eq_order1(match_): 

x = match_['func'][0].func 

y = match_['func'][1].func 

z = match_['func'][2].func 

eq = match_['eq'] 

fc = match_['func_coeff'] 

func = match_['func'] 

t = list(list(eq[0].atoms(Derivative))[0].atoms(Symbol))[0] 

if match_['type_of_equation'] == 'type1': 

sol = _nonlinear_3eq_order1_type1(x, y, z, t, eq) 

if match_['type_of_equation'] == 'type2': 

sol = _nonlinear_3eq_order1_type2(x, y, z, t, eq) 

if match_['type_of_equation'] == 'type3': 

sol = _nonlinear_3eq_order1_type3(x, y, z, t, eq) 

if match_['type_of_equation'] == 'type4': 

sol = _nonlinear_3eq_order1_type4(x, y, z, t, eq) 

if match_['type_of_equation'] == 'type5': 

sol = _nonlinear_3eq_order1_type5(x, y, z, t, eq) 

return sol 

 

def _nonlinear_3eq_order1_type1(x, y, z, t, eq): 

r""" 

Equations: 

 

.. math:: a x' = (b - c) y z, \enspace b y' = (c - a) z x, \enspace c z' = (a - b) x y 

 

First Integrals: 

 

.. math:: a x^{2} + b y^{2} + c z^{2} = C_1 

 

.. math:: a^{2} x^{2} + b^{2} y^{2} + c^{2} z^{2} = C_2 

 

where `C_1` and `C_2` are arbitrary constants. On solving the integrals for `y` and 

`z` and on substituting the resulting expressions into the first equation of the 

system, we arrives at a separable first-order equation on `x`. Similarly doing that 

for other two equations, we will arrive at first order equation on `y` and `z` too. 

 

References 

========== 

-http://eqworld.ipmnet.ru/en/solutions/sysode/sode0401.pdf 

 

""" 

C1, C2 = get_numbered_constants(eq, num=2) 

u, v, w = symbols('u, v, w') 

p = Wild('p', exclude=[x(t), y(t), z(t), t]) 

q = Wild('q', exclude=[x(t), y(t), z(t), t]) 

s = Wild('s', exclude=[x(t), y(t), z(t), t]) 

r = (diff(x(t),t) - eq[0]).match(p*y(t)*z(t)) 

r.update((diff(y(t),t) - eq[1]).match(q*z(t)*x(t))) 

r.update((diff(z(t),t) - eq[2]).match(s*x(t)*y(t))) 

n1, d1 = r[p].as_numer_denom() 

n2, d2 = r[q].as_numer_denom() 

n3, d3 = r[s].as_numer_denom() 

val = solve([n1*u-d1*v+d1*w, d2*u+n2*v-d2*w, d3*u-d3*v-n3*w],[u,v]) 

vals = [val[v], val[u]] 

c = lcm(vals[0].as_numer_denom()[1], vals[1].as_numer_denom()[1]) 

b = vals[0].subs(w,c) 

a = vals[1].subs(w,c) 

y_x = sqrt(((c*C1-C2) - a*(c-a)*x(t)**2)/(b*(c-b))) 

z_x = sqrt(((b*C1-C2) - a*(b-a)*x(t)**2)/(c*(b-c))) 

z_y = sqrt(((a*C1-C2) - b*(a-b)*y(t)**2)/(c*(a-c))) 

x_y = sqrt(((c*C1-C2) - b*(c-b)*y(t)**2)/(a*(c-a))) 

x_z = sqrt(((b*C1-C2) - c*(b-c)*z(t)**2)/(a*(b-a))) 

y_z = sqrt(((a*C1-C2) - c*(a-c)*z(t)**2)/(b*(a-b))) 

sol1 = dsolve(a*diff(x(t),t) - (b-c)*y_x*z_x) 

sol2 = dsolve(b*diff(y(t),t) - (c-a)*z_y*x_y) 

sol3 = dsolve(c*diff(z(t),t) - (a-b)*x_z*y_z) 

return [sol1, sol2, sol3] 

 

 

def _nonlinear_3eq_order1_type2(x, y, z, t, eq): 

r""" 

Equations: 

 

.. math:: a x' = (b - c) y z f(x, y, z, t) 

 

.. math:: b y' = (c - a) z x f(x, y, z, t) 

 

.. math:: c z' = (a - b) x y f(x, y, z, t) 

 

First Integrals: 

 

.. math:: a x^{2} + b y^{2} + c z^{2} = C_1 

 

.. math:: a^{2} x^{2} + b^{2} y^{2} + c^{2} z^{2} = C_2 

 

where `C_1` and `C_2` are arbitrary constants. On solving the integrals for `y` and 

`z` and on substituting the resulting expressions into the first equation of the 

system, we arrives at a first-order differential equations on `x`. Similarly doing 

that for other two equations we will arrive at first order equation on `y` and `z`. 

 

References 

========== 

-http://eqworld.ipmnet.ru/en/solutions/sysode/sode0402.pdf 

 

""" 

C1, C2 = get_numbered_constants(eq, num=2) 

u, v, w = symbols('u, v, w') 

p = Wild('p', exclude=[x(t), y(t), z(t), t]) 

q = Wild('q', exclude=[x(t), y(t), z(t), t]) 

s = Wild('s', exclude=[x(t), y(t), z(t), t]) 

f = Wild('f') 

r1 = (diff(x(t),t) - eq[0]).match(y(t)*z(t)*f) 

r = collect_const(r1[f]).match(p*f) 

r.update(((diff(y(t),t) - eq[1])/r[f]).match(q*z(t)*x(t))) 

r.update(((diff(z(t),t) - eq[2])/r[f]).match(s*x(t)*y(t))) 

n1, d1 = r[p].as_numer_denom() 

n2, d2 = r[q].as_numer_denom() 

n3, d3 = r[s].as_numer_denom() 

val = solve([n1*u-d1*v+d1*w, d2*u+n2*v-d2*w, -d3*u+d3*v+n3*w],[u,v]) 

vals = [val[v], val[u]] 

c = lcm(vals[0].as_numer_denom()[1], vals[1].as_numer_denom()[1]) 

a = vals[0].subs(w,c) 

b = vals[1].subs(w,c) 

y_x = sqrt(((c*C1-C2) - a*(c-a)*x(t)**2)/(b*(c-b))) 

z_x = sqrt(((b*C1-C2) - a*(b-a)*x(t)**2)/(c*(b-c))) 

z_y = sqrt(((a*C1-C2) - b*(a-b)*y(t)**2)/(c*(a-c))) 

x_y = sqrt(((c*C1-C2) - b*(c-b)*y(t)**2)/(a*(c-a))) 

x_z = sqrt(((b*C1-C2) - c*(b-c)*z(t)**2)/(a*(b-a))) 

y_z = sqrt(((a*C1-C2) - c*(a-c)*z(t)**2)/(b*(a-b))) 

sol1 = dsolve(a*diff(x(t),t) - (b-c)*y_x*z_x*r[f]) 

sol2 = dsolve(b*diff(y(t),t) - (c-a)*z_y*x_y*r[f]) 

sol3 = dsolve(c*diff(z(t),t) - (a-b)*x_z*y_z*r[f]) 

return [sol1, sol2, sol3] 

 

def _nonlinear_3eq_order1_type3(x, y, z, t, eq): 

r""" 

Equations: 

 

.. math:: x' = c F_2 - b F_3, \enspace y' = a F_3 - c F_1, \enspace z' = b F_1 - a F_2 

 

where `F_n = F_n(x, y, z, t)`. 

 

1. First Integral: 

 

.. math:: a x + b y + c z = C_1, 

 

where C is an arbitrary constant. 

 

2. If we assume function `F_n` to be independent of `t`,i.e, `F_n` = `F_n (x, y, z)` 

Then, on eliminating `t` and `z` from the first two equation of the system, one 

arrives at the first-order equation 

 

.. math:: \frac{dy}{dx} = \frac{a F_3 (x, y, z) - c F_1 (x, y, z)}{c F_2 (x, y, z) - 

b F_3 (x, y, z)} 

 

where `z = \frac{1}{c} (C_1 - a x - b y)` 

 

References 

========== 

-http://eqworld.ipmnet.ru/en/solutions/sysode/sode0404.pdf 

 

""" 

C1 = get_numbered_constants(eq, num=1) 

u, v, w = symbols('u, v, w') 

p = Wild('p', exclude=[x(t), y(t), z(t), t]) 

q = Wild('q', exclude=[x(t), y(t), z(t), t]) 

s = Wild('s', exclude=[x(t), y(t), z(t), t]) 

F1, F2, F3 = symbols('F1, F2, F3', cls=Wild) 

r1 = (diff(x(t),t) - eq[0]).match(F2-F3) 

r = collect_const(r1[F2]).match(s*F2) 

r.update(collect_const(r1[F3]).match(q*F3)) 

if eq[1].has(r[F2]) and not eq[1].has(r[F3]): 

r[F2], r[F3] = r[F3], r[F2] 

r[s], r[q] = -r[q], -r[s] 

r.update((diff(y(t),t) - eq[1]).match(p*r[F3] - r[s]*F1)) 

a = r[p]; b = r[q]; c = r[s] 

F1 = r[F1].subs(x(t),u).subs(y(t),v).subs(z(t),w) 

F2 = r[F2].subs(x(t),u).subs(y(t),v).subs(z(t),w) 

F3 = r[F3].subs(x(t),u).subs(y(t),v).subs(z(t),w) 

z_xy = (C1-a*u-b*v)/c 

y_zx = (C1-a*u-c*w)/b 

x_yz = (C1-b*v-c*w)/a 

y_x = dsolve(diff(v(u),u) - ((a*F3-c*F1)/(c*F2-b*F3)).subs(w,z_xy).subs(v,v(u))).rhs 

z_x = dsolve(diff(w(u),u) - ((b*F1-a*F2)/(c*F2-b*F3)).subs(v,y_zx).subs(w,w(u))).rhs 

z_y = dsolve(diff(w(v),v) - ((b*F1-a*F2)/(a*F3-c*F1)).subs(u,x_yz).subs(w,w(v))).rhs 

x_y = dsolve(diff(u(v),v) - ((c*F2-b*F3)/(a*F3-c*F1)).subs(w,z_xy).subs(u,u(v))).rhs 

y_z = dsolve(diff(v(w),w) - ((a*F3-c*F1)/(b*F1-a*F2)).subs(u,x_yz).subs(v,v(w))).rhs 

x_z = dsolve(diff(u(w),w) - ((c*F2-b*F3)/(b*F1-a*F2)).subs(v,y_zx).subs(u,u(w))).rhs 

sol1 = dsolve(diff(u(t),t) - (c*F2 - b*F3).subs(v,y_x).subs(w,z_x).subs(u,u(t))).rhs 

sol2 = dsolve(diff(v(t),t) - (a*F3 - c*F1).subs(u,x_y).subs(w,z_y).subs(v,v(t))).rhs 

sol3 = dsolve(diff(w(t),t) - (b*F1 - a*F2).subs(u,x_z).subs(v,y_z).subs(w,w(t))).rhs 

return [sol1, sol2, sol3] 

 

def _nonlinear_3eq_order1_type4(x, y, z, t, eq): 

r""" 

Equations: 

 

.. math:: x' = c z F_2 - b y F_3, \enspace y' = a x F_3 - c z F_1, \enspace z' = b y F_1 - a x F_2 

 

where `F_n = F_n (x, y, z, t)` 

 

1. First integral: 

 

.. math:: a x^{2} + b y^{2} + c z^{2} = C_1 

 

where `C` is an arbitrary constant. 

 

2. Assuming the function `F_n` is independent of `t`: `F_n = F_n (x, y, z)`. Then on 

eliminating `t` and `z` from the first two equations of the system, one arrives at 

the first-order equation 

 

.. math:: \frac{dy}{dx} = \frac{a x F_3 (x, y, z) - c z F_1 (x, y, z)} 

{c z F_2 (x, y, z) - b y F_3 (x, y, z)} 

 

where `z = \pm \sqrt{\frac{1}{c} (C_1 - a x^{2} - b y^{2})}` 

 

References 

========== 

-http://eqworld.ipmnet.ru/en/solutions/sysode/sode0405.pdf 

 

""" 

C1 = get_numbered_constants(eq, num=1) 

u, v, w = symbols('u, v, w') 

p = Wild('p', exclude=[x(t), y(t), z(t), t]) 

q = Wild('q', exclude=[x(t), y(t), z(t), t]) 

s = Wild('s', exclude=[x(t), y(t), z(t), t]) 

F1, F2, F3 = symbols('F1, F2, F3', cls=Wild) 

r1 = eq[0].match(diff(x(t),t) - z(t)*F2 + y(t)*F3) 

r = collect_const(r1[F2]).match(s*F2) 

r.update(collect_const(r1[F3]).match(q*F3)) 

if eq[1].has(r[F2]) and not eq[1].has(r[F3]): 

r[F2], r[F3] = r[F3], r[F2] 

r[s], r[q] = -r[q], -r[s] 

r.update((diff(y(t),t) - eq[1]).match(p*x(t)*r[F3] - r[s]*z(t)*F1)) 

a = r[p]; b = r[q]; c = r[s] 

F1 = r[F1].subs(x(t),u).subs(y(t),v).subs(z(t),w) 

F2 = r[F2].subs(x(t),u).subs(y(t),v).subs(z(t),w) 

F3 = r[F3].subs(x(t),u).subs(y(t),v).subs(z(t),w) 

x_yz = sqrt((C1 - b*v**2 - c*w**2)/a) 

y_zx = sqrt((C1 - c*w**2 - a*u**2)/b) 

z_xy = sqrt((C1 - a*u**2 - b*v**2)/c) 

y_x = dsolve(diff(v(u),u) - ((a*u*F3-c*w*F1)/(c*w*F2-b*v*F3)).subs(w,z_xy).subs(v,v(u))).rhs 

z_x = dsolve(diff(w(u),u) - ((b*v*F1-a*u*F2)/(c*w*F2-b*v*F3)).subs(v,y_zx).subs(w,w(u))).rhs 

z_y = dsolve(diff(w(v),v) - ((b*v*F1-a*u*F2)/(a*u*F3-c*w*F1)).subs(u,x_yz).subs(w,w(v))).rhs 

x_y = dsolve(diff(u(v),v) - ((c*w*F2-b*v*F3)/(a*u*F3-c*w*F1)).subs(w,z_xy).subs(u,u(v))).rhs 

y_z = dsolve(diff(v(w),w) - ((a*u*F3-c*w*F1)/(b*v*F1-a*u*F2)).subs(u,x_yz).subs(v,v(w))).rhs 

x_z = dsolve(diff(u(w),w) - ((c*w*F2-b*v*F3)/(b*v*F1-a*u*F2)).subs(v,y_zx).subs(u,u(w))).rhs 

sol1 = dsolve(diff(u(t),t) - (c*w*F2 - b*v*F3).subs(v,y_x).subs(w,z_x).subs(u,u(t))).rhs 

sol2 = dsolve(diff(v(t),t) - (a*u*F3 - c*w*F1).subs(u,x_y).subs(w,z_y).subs(v,v(t))).rhs 

sol3 = dsolve(diff(w(t),t) - (b*v*F1 - a*u*F2).subs(u,x_z).subs(v,y_z).subs(w,w(t))).rhs 

return [sol1, sol2, sol3] 

 

def _nonlinear_3eq_order1_type5(x, y, t, eq): 

r""" 

.. math:: x' = x (c F_2 - b F_3), \enspace y' = y (a F_3 - c F_1), \enspace z' = z (b F_1 - a F_2) 

 

where `F_n = F_n (x, y, z, t)` and are arbitrary functions. 

 

First Integral: 

 

.. math:: \left|x\right|^{a} \left|y\right|^{b} \left|z\right|^{c} = C_1 

 

where `C` is an arbitrary constant. If the function `F_n` is independent of `t`, 

then, by eliminating `t` and `z` from the first two equations of the system, one 

arrives at a first-order equation. 

 

References 

========== 

-http://eqworld.ipmnet.ru/en/solutions/sysode/sode0406.pdf 

 

""" 

C1 = get_numbered_constants(eq, num=1) 

u, v, w = symbols('u, v, w') 

p = Wild('p', exclude=[x(t), y(t), z(t), t]) 

q = Wild('q', exclude=[x(t), y(t), z(t), t]) 

s = Wild('s', exclude=[x(t), y(t), z(t), t]) 

F1, F2, F3 = symbols('F1, F2, F3', cls=Wild) 

r1 = eq[0].match(diff(x(t),t) - x(t)*(F2 - F3)) 

r = collect_const(r1[F2]).match(s*F2) 

r.update(collect_const(r1[F3]).match(q*F3)) 

if eq[1].has(r[F2]) and not eq[1].has(r[F3]): 

r[F2], r[F3] = r[F3], r[F2] 

r[s], r[q] = -r[q], -r[s] 

r.update((diff(y(t),t) - eq[1]).match(y(t)*(a*r[F3] - r[c]*F1))) 

a = r[p]; b = r[q]; c = r[s] 

F1 = r[F1].subs(x(t),u).subs(y(t),v).subs(z(t),w) 

F2 = r[F2].subs(x(t),u).subs(y(t),v).subs(z(t),w) 

F3 = r[F3].subs(x(t),u).subs(y(t),v).subs(z(t),w) 

x_yz = (C1*v**-b*w**-c)**-a 

y_zx = (C1*w**-c*u**-a)**-b 

z_xy = (C1*u**-a*v**-b)**-c 

y_x = dsolve(diff(v(u),u) - ((v*(a*F3-c*F1))/(u*(c*F2-b*F3))).subs(w,z_xy).subs(v,v(u))).rhs 

z_x = dsolve(diff(w(u),u) - ((w*(b*F1-a*F2))/(u*(c*F2-b*F3))).subs(v,y_zx).subs(w,w(u))).rhs 

z_y = dsolve(diff(w(v),v) - ((w*(b*F1-a*F2))/(v*(a*F3-c*F1))).subs(u,x_yz).subs(w,w(v))).rhs 

x_y = dsolve(diff(u(v),v) - ((u*(c*F2-b*F3))/(v*(a*F3-c*F1))).subs(w,z_xy).subs(u,u(v))).rhs 

y_z = dsolve(diff(v(w),w) - ((v*(a*F3-c*F1))/(w*(b*F1-a*F2))).subs(u,x_yz).subs(v,v(w))).rhs 

x_z = dsolve(diff(u(w),w) - ((u*(c*F2-b*F3))/(w*(b*F1-a*F2))).subs(v,y_zx).subs(u,u(w))).rhs 

sol1 = dsolve(diff(u(t),t) - (u*(c*F2-b*F3)).subs(v,y_x).subs(w,z_x).subs(u,u(t))).rhs 

sol2 = dsolve(diff(v(t),t) - (v*(a*F3-c*F1)).subs(u,x_y).subs(w,z_y).subs(v,v(t))).rhs 

sol3 = dsolve(diff(w(t),t) - (w*(b*F1-a*F2)).subs(u,x_z).subs(v,y_z).subs(w,w(t))).rhs 

return [sol1, sol2, sol3]