differential-equations 0.5.3

A Rust library for solving differential equations.
Documentation
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
//! Classic or typical Runge-Kutta methods without unique properties

use crate::{tableau::ButcherTableau, traits::Real};

// -- Explicit Runge-Kutta methods (ERK) --

impl<T: Real> ButcherTableau<T, 4> {
    /// Classic Runge-Kutta 4th order method (RK4).
    ///
    /// # Overview
    /// This provides a 4-stage, explicit Runge-Kutta method with:
    /// - Primary order: 4
    /// - Embedded order: None (this implementation does not include an embedded error estimate)
    /// - Number of stages: 4
    ///
    /// # Interpolation
    /// - This standard RK4 implementation does not provide coefficients for dense output (interpolation).
    ///
    /// # Notes
    /// - RK4 is a widely used, general-purpose method known for its balance of accuracy and
    ///   computational efficiency for many problems.
    /// - It is a fixed-step method as presented here, lacking an embedded formula for adaptive
    ///   step-size control. For adaptive step sizes, a method with an error estimate (e.g., RKF45)
    ///   would be required.
    ///
    /// # Butcher Tableau
    /// ```text
    /// 0   |
    /// 1/2 | 1/2
    /// 1/2 | 0   1/2
    /// 1   | 0   0   1
    /// ----|--------------------
    ///     | 1/6 1/3 1/3 1/6
    /// ```
    ///
    /// # References
    /// - Kutta, W. (1901). "Beitrag zur näherungsweisen Integration totaler Differentialgleichungen". *Zeitschrift für Mathematik und Physik*, 46, 435-453.
    /// - Runge, C. (1895). "Über die numerische Auflösung von Differentialgleichungen". *Mathematische Annalen*, 46, 167-178.
    pub fn rk4() -> Self {
        let mut c = [0.0; 4];
        let mut a = [[0.0; 4]; 4];
        let mut b = [0.0; 4];

        c[0] = 0.0;
        c[1] = 0.5;
        c[2] = 0.5;
        c[3] = 1.0;

        a[1][0] = 0.5;
        a[2][1] = 0.5;
        a[3][2] = 1.0;

        b[0] = 1.0 / 6.0;
        b[1] = 1.0 / 3.0;
        b[2] = 1.0 / 3.0;
        b[3] = 1.0 / 6.0;

        let a = a.map(|row| row.map(|x| T::from_f64(x).unwrap()));
        let b = b.map(|x| T::from_f64(x).unwrap());
        let c = c.map(|x| T::from_f64(x).unwrap());

        Self {
            c,
            a,
            b,
            bh: None,
            bi: None,
            er: None,
        }
    }

    /// Three-Eighths Rule 4th order method.
    ///
    /// # Overview
    /// This provides a 4-stage, explicit Runge-Kutta method with:
    /// - Primary order: 4
    /// - Embedded order: None (no embedded error estimate)
    /// - Number of stages: 4
    ///
    /// # Notes
    /// - The primary advantage of this method is that almost all of the error coefficients
    ///   are smaller than in the classic RK4 method, but it requires slightly more FLOPs
    ///   (floating-point operations) per time step.
    ///
    /// # Butcher Tableau
    /// ```text
    /// 0   |
    /// 1/3 | 1/3
    /// 2/3 | -1/3 1
    /// 1   | 1   -1   1
    /// ----|--------------------
    ///     | 1/8 3/8 3/8 1/8
    /// ```
    ///
    /// # References
    /// - Butcher, J.C. (2008). "Numerical Methods for Ordinary Differential Equations".
    pub fn three_eighths() -> Self {
        let mut c = [0.0; 4];
        let mut a = [[0.0; 4]; 4];
        let mut b = [0.0; 4];

        c[0] = 0.0;
        c[1] = 1.0 / 3.0;
        c[2] = 2.0 / 3.0;
        c[3] = 1.0;

        a[1][0] = 1.0 / 3.0;
        a[2][0] = -1.0 / 3.0;
        a[2][1] = 1.0;
        a[3][0] = 1.0;
        a[3][1] = -1.0;
        a[3][2] = 1.0;

        b[0] = 1.0 / 8.0;
        b[1] = 3.0 / 8.0;
        b[2] = 3.0 / 8.0;
        b[3] = 1.0 / 8.0;

        let a = a.map(|row| row.map(|x| T::from_f64(x).unwrap()));
        let b = b.map(|x| T::from_f64(x).unwrap());
        let c = c.map(|x| T::from_f64(x).unwrap());

        Self {
            c,
            a,
            b,
            bh: None,
            bi: None,
            er: None,
        }
    }
}

impl<T: Real> ButcherTableau<T, 2> {
    /// Midpoint method (2nd order Runge-Kutta).
    ///
    /// # Overview
    /// This provides a 2-stage, explicit Runge-Kutta method with:
    /// - Primary order: 2
    /// - Embedded order: None
    /// - Number of stages: 2
    ///
    /// # Butcher Tableau
    /// ```text
    /// 0   |
    /// 1/2 | 1/2
    /// ----|--------
    ///     | 0   1
    /// ```
    ///
    /// # References
    /// - Butcher, J.C. (2008). "Numerical Methods for Ordinary Differential Equations".
    pub fn midpoint() -> Self {
        let mut c = [0.0; 2];
        let mut a = [[0.0; 2]; 2];
        let mut b = [0.0; 2];

        c[0] = 0.0;
        c[1] = 0.5;

        a[1][0] = 0.5;

        b[0] = 0.0;
        b[1] = 1.0;

        let a = a.map(|row| row.map(|x| T::from_f64(x).unwrap()));
        let b = b.map(|x| T::from_f64(x).unwrap());
        let c = c.map(|x| T::from_f64(x).unwrap());

        Self {
            c,
            a,
            b,
            bh: None,
            bi: None,
            er: None,
        }
    }

    /// Heun's method (2nd order Runge-Kutta).
    ///
    /// # Overview
    /// This provides a 2-stage, explicit Runge-Kutta method with:
    /// - Primary order: 2
    /// - Embedded order: None
    /// - Number of stages: 2
    ///
    /// # Butcher Tableau
    /// ```text
    /// 0   |
    /// 1   | 1
    /// ----|--------
    ///     | 1/2 1/2
    /// ```
    ///
    /// # References
    /// - Heun, K. (1900). "Neue Methoden zur approximativen Integration der Differentialgleichungen einer unabhängigen Veränderlichen".
    pub fn heun() -> Self {
        let mut c = [0.0; 2];
        let mut a = [[0.0; 2]; 2];
        let mut b = [0.0; 2];

        c[0] = 0.0;
        c[1] = 1.0;

        a[1][0] = 1.0;

        b[0] = 0.5;
        b[1] = 0.5;

        let a = a.map(|row| row.map(|x| T::from_f64(x).unwrap()));
        let b = b.map(|x| T::from_f64(x).unwrap());
        let c = c.map(|x| T::from_f64(x).unwrap());

        Self {
            c,
            a,
            b,
            bh: None,
            bi: None,
            er: None,
        }
    }

    /// Ralston's method (2nd order Runge-Kutta).
    ///
    /// # Overview
    /// This provides a 2-stage, explicit Runge-Kutta method with:
    /// - Primary order: 2
    /// - Embedded order: None
    /// - Number of stages: 2
    ///
    /// # Butcher Tableau
    /// ```text
    /// 0   |
    /// 2/3 | 2/3
    /// ----|--------
    ///     | 1/4 3/4
    /// ```
    ///
    /// # References
    /// - Ralston, A. (1962). "Runge-Kutta Methods with Minimum Error Bounds".
    pub fn ralston() -> Self {
        let mut c = [0.0; 2];
        let mut a = [[0.0; 2]; 2];
        let mut b = [0.0; 2];

        c[0] = 0.0;
        c[1] = 2.0 / 3.0;

        a[1][0] = 2.0 / 3.0;

        b[0] = 1.0 / 4.0;
        b[1] = 3.0 / 4.0;

        let a = a.map(|row| row.map(|x| T::from_f64(x).unwrap()));
        let b = b.map(|x| T::from_f64(x).unwrap());
        let c = c.map(|x| T::from_f64(x).unwrap());

        Self {
            c,
            a,
            b,
            bh: None,
            bi: None,
            er: None,
        }
    }
}

impl<T: Real> ButcherTableau<T, 1> {
    /// Euler's method (1st order Runge-Kutta).
    ///
    /// # Overview
    /// This provides a 1-stage, explicit Runge-Kutta method with:
    /// - Primary order: 1
    /// - Embedded order: None
    /// - Number of stages: 1
    ///
    /// # Butcher Tableau
    /// ```text
    /// 0 |
    /// --|--
    ///   | 1
    /// ```
    ///
    /// # References
    /// - Euler, L. (1768). "Institutionum calculi integralis".
    pub fn euler() -> Self {
        let c = [T::zero()];
        let a = [[T::zero()]];
        let b = [T::one()];

        Self {
            c,
            a,
            b,
            bh: None,
            bi: None,
            er: None,
        }
    }
}

// Implementations for methods with embedded error estimators (adaptive methods)
impl<T: Real> ButcherTableau<T, 6> {
    /// Runge-Kutta-Fehlberg 4(5) method (RKF45).
    ///
    /// # Overview
    /// This provides a 6-stage, explicit Runge-Kutta method with:
    /// - Primary order: 5
    /// - Embedded order: 4 (for error estimation)
    /// - Number of stages: 6
    ///
    /// # Notes
    /// - RKF45 is a widely used adaptive step size method that provides a good balance
    ///   between accuracy and computational efficiency.
    /// - It uses the difference between 4th and 5th order approximations to estimate error.
    ///
    /// # Butcher Tableau
    /// ```text
    /// 0      |
    /// 1/4    | 1/4
    /// 3/8    | 3/32         9/32
    /// 12/13  | 1932/2197    -7200/2197  7296/2197
    /// 1      | 439/216      -8          3680/513    -845/4104
    /// 1/2    | -8/27        2           -3544/2565  1859/4104   -11/40
    /// -------|---------------------------------------------------------------
    ///        | 16/135       0           6656/12825  28561/56430 -9/50  2/55  (5th)
    ///        | 25/216       0           1408/2565   2197/4104   -1/5   0     (4th)
    /// ```
    ///
    /// # References
    /// - Fehlberg, E. (1969). "Low-order classical Runge-Kutta formulas with step size control and their application to some heat transfer problems".
    pub fn rkf45() -> Self {
        let mut c = [0.0; 6];
        let mut a = [[0.0; 6]; 6];
        let mut b = [0.0; 6];
        let mut bh = [0.0; 6];

        c[0] = 0.0;
        c[1] = 1.0 / 4.0;
        c[2] = 3.0 / 8.0;
        c[3] = 12.0 / 13.0;
        c[4] = 1.0;
        c[5] = 1.0 / 2.0;

        a[1][0] = 1.0 / 4.0;
        a[2][0] = 3.0 / 32.0;
        a[2][1] = 9.0 / 32.0;
        a[3][0] = 1932.0 / 2197.0;
        a[3][1] = -7200.0 / 2197.0;
        a[3][2] = 7296.0 / 2197.0;
        a[4][0] = 439.0 / 216.0;
        a[4][1] = -8.0;
        a[4][2] = 3680.0 / 513.0;
        a[4][3] = -845.0 / 4104.0;
        a[5][0] = -8.0 / 27.0;
        a[5][1] = 2.0;
        a[5][2] = -3544.0 / 2565.0;
        a[5][3] = 1859.0 / 4104.0;
        a[5][4] = -11.0 / 40.0;

        b[0] = 16.0 / 135.0;
        b[1] = 0.0;
        b[2] = 6656.0 / 12825.0;
        b[3] = 28561.0 / 56430.0;
        b[4] = -9.0 / 50.0;
        b[5] = 2.0 / 55.0;

        bh[0] = 25.0 / 216.0;
        bh[1] = 0.0;
        bh[2] = 1408.0 / 2565.0;
        bh[3] = 2197.0 / 4104.0;
        bh[4] = -1.0 / 5.0;
        bh[5] = 0.0;

        let a = a.map(|row| row.map(|x| T::from_f64(x).unwrap()));
        let b = b.map(|x| T::from_f64(x).unwrap());
        let bh = bh.map(|x| T::from_f64(x).unwrap());
        let c = c.map(|x| T::from_f64(x).unwrap());

        Self {
            c,
            a,
            b,
            bh: Some(bh),
            bi: None,
            er: None,
        }
    }

    /// Cash-Karp 4(5) method.
    ///
    /// # Overview
    /// This provides a 6-stage, explicit Runge-Kutta method with:
    /// - Primary order: 5
    /// - Embedded order: 4 (for error estimation)
    /// - Number of stages: 6
    ///
    /// # Notes
    /// - The Cash-Karp method is a variant of Runge-Kutta methods with embedded error estimation
    ///   that often provides better accuracy than RKF45 for some problem types.
    ///
    /// # Butcher Tableau
    /// ```text
    /// 0      |
    /// 1/5    | 1/5
    /// 3/10   | 3/40         9/40
    /// 3/5    | 3/10         -9/10       6/5
    /// 1      | -11/54       5/2         -70/27      35/27
    /// 7/8    | 1631/55296   175/512     575/13824   44275/110592 253/4096
    /// -------|---------------------------------------------------------------
    ///        | 37/378       0           250/621     125/594     0      512/1771  (5th)
    ///        | 2825/27648   0           18575/48384 13525/55296 277/14336 1/4    (4th)
    /// ```
    ///
    /// # References
    /// - Cash, J.R., Karp, A.H. (1990). "A Variable Order Runge-Kutta Method for Initial Value Problems with Rapidly Varying Right-Hand Sides".
    pub fn cash_karp() -> Self {
        let mut c = [0.0; 6];
        let mut a = [[0.0; 6]; 6];
        let mut b = [0.0; 6];
        let mut bh = [0.0; 6];

        c[0] = 0.0;
        c[1] = 1.0 / 5.0;
        c[2] = 3.0 / 10.0;
        c[3] = 3.0 / 5.0;
        c[4] = 1.0;
        c[5] = 7.0 / 8.0;

        a[1][0] = 1.0 / 5.0;
        a[2][0] = 3.0 / 40.0;
        a[2][1] = 9.0 / 40.0;
        a[3][0] = 3.0 / 10.0;
        a[3][1] = -9.0 / 10.0;
        a[3][2] = 6.0 / 5.0;
        a[4][0] = -11.0 / 54.0;
        a[4][1] = 5.0 / 2.0;
        a[4][2] = -70.0 / 27.0;
        a[4][3] = 35.0 / 27.0;
        a[5][0] = 1631.0 / 55296.0;
        a[5][1] = 175.0 / 512.0;
        a[5][2] = 575.0 / 13824.0;
        a[5][3] = 44275.0 / 110592.0;
        a[5][4] = 253.0 / 4096.0;

        b[0] = 37.0 / 378.0;
        b[1] = 0.0;
        b[2] = 250.0 / 621.0;
        b[3] = 125.0 / 594.0;
        b[4] = 0.0;
        b[5] = 512.0 / 1771.0;

        bh[0] = 2825.0 / 27648.0;
        bh[1] = 0.0;
        bh[2] = 18575.0 / 48384.0;
        bh[3] = 13525.0 / 55296.0;
        bh[4] = 277.0 / 14336.0;
        bh[5] = 1.0 / 4.0;

        let a = a.map(|row| row.map(|x| T::from_f64(x).unwrap()));
        let b = b.map(|x| T::from_f64(x).unwrap());
        let bh = bh.map(|x| T::from_f64(x).unwrap());
        let c = c.map(|x| T::from_f64(x).unwrap());

        Self {
            c,
            a,
            b,
            bh: Some(bh),
            bi: None,
            er: None,
        }
    }
}

// --- Implicit Runge-Kutta methods (IRK) ---

impl<T: Real> ButcherTableau<T, 1> {
    pub fn backward_euler() -> Self {
        let mut c = [0.0; 1];
        let mut a = [[0.0; 1]; 1];
        let mut b = [0.0; 1];

        c[0] = 1.0;
        a[0][0] = 1.0;
        b[0] = 1.0;

        let a = a.map(|row| row.map(|x| T::from_f64(x).unwrap()));
        let b = b.map(|x| T::from_f64(x).unwrap());
        let c = c.map(|x| T::from_f64(x).unwrap());

        Self {
            c,
            a,
            b,
            bh: None,
            bi: None,
            er: None,
        }
    }
}

impl<T: Real> ButcherTableau<T, 2> {
    pub fn trapezoidal() -> Self {
        let mut c = [0.0; 2];
        let mut a = [[0.0; 2]; 2];
        let mut b = [0.0; 2];

        c[0] = 0.0;
        c[1] = 1.0;

        a[1][0] = 1.0;

        b[0] = 0.5;
        b[1] = 0.5;

        let a = a.map(|row| row.map(|x| T::from_f64(x).unwrap()));
        let b = b.map(|x| T::from_f64(x).unwrap());
        let c = c.map(|x| T::from_f64(x).unwrap());

        Self {
            c,
            a,
            b,
            bh: None,
            bi: None,
            er: None,
        }
    }

    pub fn crank_nicolson() -> Self {
        let mut c = [0.0; 2];
        let mut a = [[0.0; 2]; 2];
        let mut b = [0.0; 2];

        c[0] = 0.0;
        c[1] = 1.0;

        a[1][0] = 1.0;

        b[0] = 0.5;
        b[1] = 0.5;

        let a = a.map(|row| row.map(|x| T::from_f64(x).unwrap()));
        let b = b.map(|x| T::from_f64(x).unwrap());
        let c = c.map(|x| T::from_f64(x).unwrap());

        Self {
            c,
            a,
            b,
            bh: None,
            bi: None,
            er: None,
        }
    }
}