multicalc 0.10.0

Math for real-time embedded systems, in stable no_std Rust: state estimation, control, kinematics, Lie groups, autodiff, and linear algebra — from 64-bit servers to bare-metal microcontrollers
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
//! Building a polynomial from roots, from sampled points, or from a series expansion.
//!
//! [`Polynomial::from_points`] and [`Polynomial::fit_least_squares`] both shift and stretch the
//! sample positions into the range -1 to 1 before doing any work, and undo that at the end. Without
//! it, points spread over a wide range raise the numbers in the solve to high powers and it
//! degenerates. With it, fits stay well behaved to about the eighth power in `f64`, which is past
//! anything worth fitting with a single polynomial — beyond that, use pieces.
//!
//! One thing that helps with is worth being plain about. Undoing the shift at the end puts the
//! answer back into powers of the caller's own variable, and when the samples sit far from zero
//! that step gives up digits no matter how the fit was done: a small value near zero comes out of
//! adding and subtracting much larger ones. Samples spread over 0 to 1000 with values reaching
//! `1e11` reproduce those values to rounding against `1e11`, so the small ones near zero are right
//! to about six digits rather than all sixteen. Keeping the samples near zero, or asking for the
//! value rather than reading the coefficients, avoids it.
#![deny(clippy::indexing_slicing)]

use crate::error::{LinalgError, PolynomialError};
use crate::linear_algebra::{Matrix, PivotedQr, Vector};
use crate::polynomial::Polynomial;
use crate::scalar::{Jet, Numeric};

/// Multiplies the polynomial held in `coefficients` by `(x - root)`, in place.
///
/// `raised_degree` is the degree the answer reaches, one more than what is there now.
fn multiply_in_a_root<T: Numeric>(coefficients: &mut [T], raised_degree: usize, root: T) {
    // Each coefficient becomes the one below it, less the root times itself, so a single value
    // carried along is enough to work upward without a second copy.
    let mut carried = T::ZERO;
    for power in 0..=raised_degree {
        let current = coefficients.get(power).copied().unwrap_or(T::ZERO);
        if let Some(slot) = coefficients.get_mut(power) {
            *slot = carried - root * current;
        }
        carried = current;
    }
}

/// The middle of the sample positions and half their spread, which together move any position into
/// the range -1 to 1.
///
/// A spread of zero means every position is the same, which no polynomial can be fitted through.
fn sample_range<T: Numeric>(nodes: &[T]) -> Result<(T, T), PolynomialError> {
    let mut smallest = nodes.first().copied().ok_or(PolynomialError::Empty)?;
    let mut largest = smallest;
    for node in nodes {
        smallest = smallest.min(*node);
        largest = largest.max(*node);
    }
    let half_width = (largest - smallest) * T::HALF;
    if half_width == T::ZERO {
        return Err(PolynomialError::DuplicateNode);
    }
    Ok(((smallest + largest) * T::HALF, half_width))
}

impl<const COEFFICIENT_COUNT: usize, T: Numeric> Polynomial<COEFFICIENT_COUNT, T> {
    /// The polynomial with exactly these roots, and 1 as its highest coefficient.
    ///
    /// Returns [`PolynomialError::DegreeOverflow`] when there are more roots than the polynomial
    /// has room for, and [`PolynomialError::NonFinite`] when a root is infinite or NaN.
    ///
    /// ```
    /// use multicalc::Polynomial;
    ///
    /// // (x - 1)(x - 2)(x - 3)
    /// let p = Polynomial::<4>::from_roots(&[1.0, 2.0, 3.0]).unwrap();
    /// assert_eq!(p.coefficients(), &[-6.0, 11.0, -6.0, 1.0]);
    /// ```
    pub fn from_roots(roots: &[T]) -> Result<Self, PolynomialError> {
        if roots.iter().any(|root| !root.is_finite()) {
            return Err(PolynomialError::NonFinite);
        }
        if roots.len() >= COEFFICIENT_COUNT {
            return Err(PolynomialError::DegreeOverflow);
        }

        let mut coefficients = [T::ZERO; COEFFICIENT_COUNT];
        match coefficients.get_mut(0) {
            Some(slot) => *slot = T::ONE,
            None => return Err(PolynomialError::DegreeOverflow),
        }
        for (index, root) in roots.iter().enumerate() {
            multiply_in_a_root(&mut coefficients, index + 1, *root);
        }
        Ok(Self::new(coefficients))
    }

    /// The series a [`Jet`] carries, as a polynomial.
    ///
    /// A jet's coefficients are already those of the series, so this copies them across. The result
    /// describes the function near the point the jet was expanded about, so
    /// [`shift_argument`](Self::shift_argument) is what moves that point somewhere else.
    ///
    /// Returns [`PolynomialError::DegreeOverflow`] when the jet holds more coefficients than fit.
    ///
    /// ```
    /// use multicalc::{Jet, Polynomial};
    ///
    /// // Squaring near x = 0 gives back exactly x².
    /// let squared = Jet::<f64, 3>::variable(0.0) * Jet::<f64, 3>::variable(0.0);
    /// let p = Polynomial::<3>::from_jet(&squared).unwrap();
    /// assert_eq!(p.coefficients(), &[0.0, 0.0, 1.0]);
    /// ```
    pub fn from_jet<const JET_ORDER: usize>(
        jet: &Jet<T, JET_ORDER>,
    ) -> Result<Self, PolynomialError> {
        if JET_ORDER > COEFFICIENT_COUNT {
            return Err(PolynomialError::DegreeOverflow);
        }
        let mut coefficients = [T::ZERO; COEFFICIENT_COUNT];
        for (slot, coefficient) in coefficients.iter_mut().zip(jet.coeffs.iter()) {
            *slot = *coefficient;
        }
        Ok(Self::new(coefficients))
    }

    /// Sample positions that bunch up toward the ends of the range, in increasing order.
    ///
    /// Sampling a function at these instead of at evenly spaced positions keeps a fit through them
    /// from swinging wildly near the ends, which is what evenly spaced points cause once there are
    /// more than a handful.
    ///
    /// ```
    /// use multicalc::Polynomial;
    ///
    /// let nodes = Polynomial::<4>::chebyshev_nodes(-1.0, 1.0);
    /// assert!(nodes[0] > -1.0 && nodes[3] < 1.0);
    /// assert!(nodes[0] < nodes[1] && nodes[1] < nodes[2] && nodes[2] < nodes[3]);
    /// ```
    #[must_use]
    pub fn chebyshev_nodes(lower: T, upper: T) -> [T; COEFFICIENT_COUNT] {
        let centre = (lower + upper) * T::HALF;
        let half_width = (upper - lower) * T::HALF;
        let count = T::from_usize(COEFFICIENT_COUNT);

        let mut nodes = [T::ZERO; COEFFICIENT_COUNT];
        for (index, slot) in nodes.iter_mut().enumerate() {
            // The formula runs from the top of the range downward, so count backward to hand them
            // back in increasing order.
            let step = COEFFICIENT_COUNT - 1 - index;
            let angle = (T::TWO * T::from_usize(step) + T::ONE) * T::PI / (T::TWO * count);
            *slot = centre + half_width * angle.cos();
        }
        nodes
    }

    /// The one polynomial passing through every given point.
    ///
    /// There must be exactly as many points as the polynomial has coefficients, since that is how
    /// many it takes to pin one down. Returns [`PolynomialError::DuplicateNode`] when two points
    /// share a position, and [`PolynomialError::NonFinite`] when any value is infinite or NaN.
    ///
    /// ```
    /// use multicalc::Polynomial;
    ///
    /// // Three points off 1 - 2x + 3x².
    /// let p = Polynomial::<3>::from_points(&[0.0, 1.0, 2.0], &[1.0, 2.0, 9.0]).unwrap();
    /// assert!((p.evaluate(3.0) - 22.0).abs() < 1e-10);
    /// ```
    pub fn from_points(
        nodes: &[T; COEFFICIENT_COUNT],
        values: &[T; COEFFICIENT_COUNT],
    ) -> Result<Self, PolynomialError> {
        if nodes
            .iter()
            .chain(values.iter())
            .any(|value| !value.is_finite())
        {
            return Err(PolynomialError::NonFinite);
        }
        // A single point is just a constant, and has no spread to normalize against.
        if COEFFICIENT_COUNT <= 1 {
            return Ok(Self::new(*values));
        }
        let (centre, half_width) = sample_range(nodes)?;

        let mut normalized = [T::ZERO; COEFFICIENT_COUNT];
        for (slot, node) in normalized.iter_mut().zip(nodes.iter()) {
            *slot = (*node - centre) / half_width;
        }

        // Work out how much each extra point bends the answer away from the points before it. Each
        // pass over the table replaces neighbouring differences with their own differences, and the
        // first entry of each pass is what the polynomial needs.
        let mut table = *values;
        let mut bends = [T::ZERO; COEFFICIENT_COUNT];
        if let Some(slot) = bends.get_mut(0) {
            *slot = table.first().copied().unwrap_or(T::ZERO);
        }
        for order in 1..COEFFICIENT_COUNT {
            for index in 0..COEFFICIENT_COUNT - order {
                let current = table.get(index).copied().unwrap_or(T::ZERO);
                let next = table.get(index + 1).copied().unwrap_or(T::ZERO);
                let near = normalized.get(index).copied().unwrap_or(T::ZERO);
                let far = normalized.get(index + order).copied().unwrap_or(T::ZERO);
                let gap = far - near;
                if gap == T::ZERO {
                    return Err(PolynomialError::DuplicateNode);
                }
                if let Some(slot) = table.get_mut(index) {
                    *slot = (next - current) / gap;
                }
            }
            if let Some(slot) = bends.get_mut(order) {
                *slot = table.first().copied().unwrap_or(T::ZERO);
            }
        }

        // Fold those back into ordinary coefficients, starting from the last and working down: each
        // step multiplies by the matching point's position and adds the next bend in.
        let mut coefficients = [T::ZERO; COEFFICIENT_COUNT];
        if let Some(slot) = coefficients.get_mut(0) {
            *slot = bends.last().copied().unwrap_or(T::ZERO);
        }
        for step in (0..COEFFICIENT_COUNT - 1).rev() {
            let node = normalized.get(step).copied().unwrap_or(T::ZERO);
            multiply_in_a_root(&mut coefficients, COEFFICIENT_COUNT - 1 - step, node);
            if let Some(slot) = coefficients.get_mut(0) {
                *slot += bends.get(step).copied().unwrap_or(T::ZERO);
            }
        }

        // Undo the shift and stretch that moved the positions into the range -1 to 1.
        Ok(Self::new(coefficients)
            .scale_argument(T::ONE / half_width)
            .shift_argument(-centre))
    }

    /// The polynomial of this size that comes closest to more points than it can pass through.
    ///
    /// Closest means the squared misses summed over every point are as small as they can be.
    /// Returns [`PolynomialError::TooFewSamples`] when there are fewer points than coefficients,
    /// [`PolynomialError::DuplicateNode`] when every point shares one position, and
    /// [`PolynomialError::NonFinite`] when any value is infinite or NaN.
    ///
    /// ```
    /// use multicalc::Polynomial;
    ///
    /// // Five points off 1 + 2x, fitted with a straight line.
    /// let nodes = [0.0, 1.0, 2.0, 3.0, 4.0];
    /// let values = [1.0, 3.0, 5.0, 7.0, 9.0];
    /// let p = Polynomial::<2>::fit_least_squares(&nodes, &values).unwrap();
    /// assert!((p.evaluate(10.0) - 21.0).abs() < 1e-10);
    /// ```
    pub fn fit_least_squares<const SAMPLE_COUNT: usize>(
        nodes: &[T; SAMPLE_COUNT],
        values: &[T; SAMPLE_COUNT],
    ) -> Result<Self, PolynomialError> {
        if SAMPLE_COUNT < COEFFICIENT_COUNT {
            return Err(PolynomialError::TooFewSamples);
        }
        if nodes
            .iter()
            .chain(values.iter())
            .any(|value| !value.is_finite())
        {
            return Err(PolynomialError::NonFinite);
        }
        let (centre, half_width) = sample_range(nodes)?;

        // One row per point, holding that point's position raised to each power in turn.
        let mut design = Matrix::<SAMPLE_COUNT, COEFFICIENT_COUNT, T>::zeros();
        for (row, node) in nodes.iter().enumerate() {
            let normalized = (*node - centre) / half_width;
            let mut raised = T::ONE;
            for column in 0..COEFFICIENT_COUNT {
                if let Some(slot) = design.get_mut(row, column) {
                    *slot = raised;
                }
                raised *= normalized;
            }
        }

        let solved = PivotedQr::decompose(design)?.solve_least_squares(Vector::new(*values))?;
        Ok(Self::new(solved.into_array())
            .scale_argument(T::ONE / half_width)
            .shift_argument(-centre))
    }
}

/// Turns the eight coefficients of a degree-7 piece into its value and first three derivatives at
/// each end, measured against the outer parameter rather than the piece's own 0-to-1 clock.
///
/// `span` is how much of the outer parameter the piece covers, which is what converts between the
/// two.
pub(crate) fn endpoint_mapping<T: Numeric>(span: T) -> Matrix<8, 8, T> {
    let mut mapping = Matrix::<8, 8, T>::zeros();

    // At the start of the piece only the lowest coefficients survive: the value is the first one,
    // and each derivative picks out one more.
    if let Some(slot) = mapping.get_mut(0, 0) {
        *slot = T::ONE;
    }
    let mut ways = T::ONE;
    let mut span_raised = T::ONE;
    for order in 1..4 {
        ways *= T::from_usize(order);
        span_raised *= span;
        if let Some(slot) = mapping.get_mut(order, order) {
            *slot = ways / span_raised;
        }
    }

    // At the end of the piece every coefficient contributes, since the clock reads one there.
    for column in 0..8 {
        if let Some(slot) = mapping.get_mut(4, column) {
            *slot = T::ONE;
        }
    }
    let mut span_raised = T::ONE;
    for order in 1..4 {
        span_raised *= span;
        for column in order..8 {
            // Differentiating `order` times leaves this many copies of the term behind.
            let mut ways = T::ONE;
            for step in 0..order {
                ways *= T::from_usize(column - step);
            }
            if let Some(slot) = mapping.get_mut(4 + order, column) {
                *slot = ways / span_raised;
            }
        }
    }
    mapping
}

/// The other direction: the eight coefficients that produce a wanted value and first three
/// derivatives at each end.
pub(crate) fn endpoint_mapping_inverse<T: Numeric>(
    span: T,
) -> Result<Matrix<8, 8, T>, LinalgError> {
    endpoint_mapping(span).inverse()
}

impl<T: Numeric> Polynomial<4, T> {
    /// The smooth curve matching a value and a slope at each end.
    ///
    /// The result runs on the piece's own clock, from 0 at the start to 1 at the end, and `span` is
    /// how much of the outer parameter that covers — it is what converts the caller's slopes, which
    /// are measured against the outer parameter.
    ///
    /// Returns [`PolynomialError::SpanNotPositive`] when `span` is zero or negative, and
    /// [`PolynomialError::NonFinite`] when any input is infinite or NaN.
    ///
    /// ```
    /// use multicalc::Polynomial;
    ///
    /// // From 0 to 10 over two units of the outer parameter, at rest at both ends.
    /// let p = Polynomial::<4>::from_endpoint_derivatives(0.0, 0.0, 10.0, 0.0, 2.0).unwrap();
    /// assert!(p.evaluate(0.0).abs() < 1e-12);
    /// assert!((p.evaluate(1.0) - 10.0).abs() < 1e-12);
    /// ```
    pub fn from_endpoint_derivatives(
        start_value: T,
        start_slope: T,
        end_value: T,
        end_slope: T,
        span: T,
    ) -> Result<Self, PolynomialError> {
        for value in [start_value, start_slope, end_value, end_slope, span] {
            if !value.is_finite() {
                return Err(PolynomialError::NonFinite);
            }
        }
        if span <= T::ZERO {
            return Err(PolynomialError::SpanNotPositive);
        }

        // Slopes against the outer parameter become slopes against the piece's own clock.
        let start_rise = start_slope * span;
        let end_rise = end_slope * span;
        Ok(Self::new([
            start_value,
            start_rise,
            -T::THREE * start_value - T::TWO * start_rise + T::THREE * end_value - end_rise,
            T::TWO * start_value + start_rise - T::TWO * end_value + end_rise,
        ]))
    }
}

impl<T: Numeric> Polynomial<8, T> {
    /// The curve matching a value and its first three derivatives at each end.
    ///
    /// `start` and `end` each hold `[value, velocity, acceleration, jerk]` measured against the
    /// outer parameter. As with the cubic, the result runs on the piece's own 0-to-1 clock and
    /// `span` is how much of the outer parameter that covers.
    ///
    /// Returns [`PolynomialError::SpanNotPositive`] when `span` is zero or negative,
    /// [`PolynomialError::NonFinite`] when any input is infinite or NaN, and
    /// [`PolynomialError::Linalg`] when the eight conditions cannot be solved.
    ///
    /// ```
    /// use multicalc::Polynomial;
    ///
    /// // From 0 to 10 over two units, starting and finishing at a standstill.
    /// let still = [0.0; 4];
    /// let arrived = [10.0, 0.0, 0.0, 0.0];
    /// let p = Polynomial::<8>::from_endpoint_derivatives(&still, &arrived, 2.0).unwrap();
    /// assert!(p.evaluate(0.0).abs() < 1e-10);
    /// assert!((p.evaluate(1.0) - 10.0).abs() < 1e-10);
    /// ```
    pub fn from_endpoint_derivatives(
        start: &[T; 4],
        end: &[T; 4],
        span: T,
    ) -> Result<Self, PolynomialError> {
        if !span.is_finite()
            || start
                .iter()
                .chain(end.iter())
                .any(|value| !value.is_finite())
        {
            return Err(PolynomialError::NonFinite);
        }
        if span <= T::ZERO {
            return Err(PolynomialError::SpanNotPositive);
        }

        let mut wanted = [T::ZERO; 8];
        for (slot, value) in wanted.iter_mut().zip(start.iter().chain(end.iter())) {
            *slot = *value;
        }
        let coefficients = endpoint_mapping_inverse(span)? * Vector::new(wanted);
        Ok(Self::new(coefficients.into_array()))
    }
}