Austin Schuh | 173a159 | 2018-12-19 16:20:54 +1100 | [diff] [blame] | 1 | #ifndef FRC971_CONTROL_LOOPS_FIXED_QUADRATURE_H_ |
| 2 | #define FRC971_CONTROL_LOOPS_FIXED_QUADRATURE_H_ |
| 3 | |
| 4 | #include <array> |
| 5 | |
| 6 | namespace frc971 { |
| 7 | namespace control_loops { |
| 8 | |
| 9 | // Implements Gaussian Quadrature integration (5th order). fn is the function to |
| 10 | // integrate. It must take 1 argument of type T. The integration is between a |
| 11 | // and b. |
| 12 | template <typename F, typename T> |
| 13 | T GaussianQuadrature5(const F &fn, T a, T b) { |
| 14 | // Pulled from Python. |
| 15 | // numpy.set_printoptions(precision=20) |
| 16 | // scipy.special.p_roots(5) |
| 17 | const ::std::array<double, 5> x{{ |
| 18 | -9.06179845938663630633e-01, -5.38469310105682885670e-01, |
| 19 | 3.24607628916367383789e-17, 5.38469310105683218737e-01, |
| 20 | 9.06179845938663408589e-01}}; |
| 21 | |
| 22 | const ::std::array<double, 5> w{{ |
| 23 | 0.23692688505618844652, 0.4786286704993669705, 0.56888888888888811124, |
| 24 | 0.47862867049936674846, 0.23692688505618875183}}; |
| 25 | |
| 26 | double answer = 0.0; |
| 27 | for (int i = 0; i < 5; ++i) { |
| 28 | const double y = (b - a) * (x[i] + 1) / 2.0 + a; |
| 29 | answer += (b - a) / 2.0 * w[i] * fn(y); |
| 30 | } |
| 31 | return answer; |
| 32 | } |
| 33 | |
| 34 | } // namespace control_loops |
| 35 | } // namespace frc971 |
| 36 | |
| 37 | #endif // FRC971_CONTROL_LOOPS_FIXED_QUADRATURE_H_ |