pykep-core 0.1.3

Native Rust astrodynamics core for the pykep-rust port.
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
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
// Copyright (c) 2023-2026 Dario Izzo (dario.izzo@gmail.com)
//                          Advanced Concepts Team, European Space Agency (ESA)
// Copyright (c) 2026 pykep-rust contributors
// SPDX-License-Identifier: MPL-2.0

//! Generic zero-order-hold low-thrust leg.
//!
//! Adapted from `src/leg/zoh.cpp` in pinned pykep/kep3 3.0.1.

use core::marker::PhantomData;

use crate::dynamics::zoh::{
    ControlSchedule, ZeroOrderHoldModel, ZohCr3bpDynamics, ZohEquinoctialDynamics,
    ZohKeplerDynamics, ZohSolarSailDynamics,
};
use crate::error::ensure_finite;
use crate::integration::{
    Dop853, DynamicsModel, InitialValueProblem, IntegratorOptions, SensitivityProblem,
};
use crate::{PykepError, Result};

/// Mismatch derivatives in output-by-input row order.
///
/// These derivatives compose segment sensitivities whose built-in model
/// Jacobians use centered differences. Validation against the pinned oracle
/// therefore uses scaled tolerances up to `3e-5`, independently of the
/// integrator tolerance.
#[derive(Clone, Debug, PartialEq)]
pub struct ZohLegMismatchJacobian<const N: usize> {
    /// Derivative with respect to the initial state, shape `N × N`.
    pub initial_state: [[f64; N]; N],
    /// Derivative with respect to the final state, shape `N × N`.
    pub final_state: [[f64; N]; N],
    /// Derivative with respect to chronological flattened controls, shape
    /// `N × (control_dimension * segment_count)`.
    pub controls: Vec<Vec<f64>>,
    /// Derivative with respect to every grid node, shape
    /// `N × (segment_count + 1)`.
    pub time_grid: Vec<Vec<f64>>,
}

/// Per-segment state samples from both sides of a ZOH leg.
///
/// Backward segments appear in propagation order: final segment first,
/// followed by successively earlier segments.
#[derive(Clone, Debug, PartialEq)]
pub struct ZohLegHistory<const N: usize> {
    /// Forward segment samples in chronological order.
    pub forward: Vec<Vec<[f64; N]>>,
    /// Backward segment samples in reverse chronological order.
    pub backward: Vec<Vec<[f64; N]>>,
}

/// Validated generic zero-order-hold low-thrust leg.
///
/// `W` is the local variational width and must equal `N + C`. The public
/// aliases for the four built-in ZOH models select the correct dimensions.
///
/// ```
/// use pykep_core::dynamics::zoh::ZohKeplerDynamics;
/// use pykep_core::integration::IntegratorOptions;
/// use pykep_core::leg::ZohKeplerLeg;
///
/// let state = [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0];
/// let leg = ZohKeplerLeg::new(
///     ZohKeplerDynamics,
///     state,
///     vec![[0.0; 4]],
///     state,
///     vec![0.0, 0.1],
///     [0.0],
///     0.5,
///     IntegratorOptions::default(),
/// )?;
/// assert!(leg.mismatch_constraints()?.iter().all(|value| value.is_finite()));
/// # Ok::<(), pykep_core::PykepError>(())
/// ```
#[derive(Clone, Debug, PartialEq)]
pub struct ZohLeg<M, const N: usize, const C: usize, const K: usize, const P: usize, const W: usize>
{
    model: M,
    initial_state: [f64; N],
    final_state: [f64; N],
    schedule: ControlSchedule<C>,
    constants: [f64; K],
    cut: f64,
    forward_segments: usize,
    options: IntegratorOptions,
    parameter_dimension: PhantomData<[(); P]>,
}

/// Seven-state, four-control normalized Kepler ZOH leg.
pub type ZohKeplerLeg = ZohLeg<ZohKeplerDynamics, 7, 4, 1, 5, 11>;
/// Seven-state, four-control CR3BP ZOH leg.
pub type ZohCr3bpLeg = ZohLeg<ZohCr3bpDynamics, 7, 4, 2, 6, 11>;
/// Seven-state, four-control modified-equinoctial ZOH leg.
pub type ZohEquinoctialLeg = ZohLeg<ZohEquinoctialDynamics, 7, 4, 1, 5, 11>;
/// Six-state, two-control ideal solar-sail ZOH leg.
pub type ZohSolarSailLeg = ZohLeg<ZohSolarSailDynamics, 6, 2, 1, 3, 8>;

struct ReverseTimeModel<'a, M> {
    model: &'a M,
    origin: f64,
}

impl<M, const N: usize, const P: usize> DynamicsModel<N, P> for ReverseTimeModel<'_, M>
where
    M: DynamicsModel<N, P>,
{
    const NAME: &'static str = M::NAME;

    fn validate(&self, time: f64, state: &[f64; N], parameters: &[f64; P]) -> Result<()> {
        self.model.validate(self.origin - time, state, parameters)
    }

    fn rhs(
        &self,
        time: f64,
        state: &[f64; N],
        parameters: &[f64; P],
        derivative: &mut [f64; N],
    ) -> Result<()> {
        self.model
            .rhs(self.origin - time, state, parameters, derivative)?;
        for value in derivative {
            *value = -*value;
        }
        Ok(())
    }
}

impl<M, const N: usize, const C: usize, const K: usize, const P: usize, const W: usize>
    ZohLeg<M, N, C, K, P, W>
where
    M: ZeroOrderHoldModel<N, C, K, P>,
{
    /// Constructs and validates a ZOH leg.
    ///
    /// Controls and grid nodes are chronological. The number propagated
    /// forward is `floor(segment_count * cut)`.
    ///
    /// # Errors
    ///
    /// Returns an error for invalid dimensions, values, model endpoints,
    /// integration settings, or cuts outside `[0,1]`.
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        model: M,
        initial_state: [f64; N],
        controls: Vec<[f64; C]>,
        final_state: [f64; N],
        time_grid: Vec<f64>,
        constants: [f64; K],
        cut: f64,
        options: IntegratorOptions,
    ) -> Result<Self> {
        if W != N + C {
            return Err(PykepError::InvalidInput {
                parameter: "variational_width",
                reason: format!("must equal state + control dimensions ({})", N + C),
            });
        }
        ensure_finite("cut", cut)?;
        if !(0.0..=1.0).contains(&cut) {
            return Err(PykepError::InvalidInput {
                parameter: "cut",
                reason: "must lie in 0..=1".into(),
            });
        }
        options.validate()?;
        let schedule = ControlSchedule::new(time_grid, controls)?;
        model.validate(
            schedule.initial_time(),
            &initial_state,
            &M::parameters(schedule.controls()[0], constants),
        )?;
        model.validate(
            schedule.final_time(),
            &final_state,
            &M::parameters(schedule.controls()[schedule.len() - 1], constants),
        )?;
        let forward_segments = (schedule.len() as f64 * cut) as usize;
        Ok(Self {
            model,
            initial_state,
            final_state,
            schedule,
            constants,
            cut,
            forward_segments,
            options,
            parameter_dimension: PhantomData,
        })
    }

    /// Initial endpoint state.
    pub const fn initial_state(&self) -> [f64; N] {
        self.initial_state
    }

    /// Final endpoint state.
    pub const fn final_state(&self) -> [f64; N] {
        self.final_state
    }

    /// Chronological control schedule.
    pub const fn schedule(&self) -> &ControlSchedule<C> {
        &self.schedule
    }

    /// Constant model parameters in model-specific order.
    pub const fn constants(&self) -> [f64; K] {
        self.constants
    }

    /// Fractional cut in `[0,1]`.
    pub const fn cut(&self) -> f64 {
        self.cut
    }

    /// Integration settings applied independently to every segment.
    pub const fn integrator_options(&self) -> IntegratorOptions {
        self.options
    }

    /// Total number of segments.
    pub fn segment_count(&self) -> usize {
        self.schedule.len()
    }

    /// Number of segments propagated from the initial state.
    pub const fn forward_segment_count(&self) -> usize {
        self.forward_segments
    }

    /// Number of segments propagated backward from the final state.
    pub fn backward_segment_count(&self) -> usize {
        self.segment_count() - self.forward_segments
    }

    /// Evaluates `forward_state - backward_state` at the cut.
    ///
    /// # Errors
    ///
    /// Returns an integration error containing direction, segment index, and
    /// segment time interval.
    pub fn mismatch_constraints(&self) -> Result<[f64; N]> {
        let forward = self.propagate_nominal_half(true)?;
        let backward = self.propagate_nominal_half(false)?;
        Ok(core::array::from_fn(|row| forward[row] - backward[row]))
    }

    /// Evaluates all four first-order mismatch derivative groups.
    ///
    /// The matrices use output-by-input rows and chronological control
    /// blocks, matching the upstream ZOH-leg contract. Built-in model
    /// Jacobians are centered finite differences with a relative step of
    /// `3e-6`; the validation ceiling is a scaled `3e-5`, even when the
    /// propagation tolerance is tighter.
    ///
    /// # Errors
    ///
    /// Returns an integration error containing direction, segment index, and
    /// segment time interval.
    pub fn mismatch_jacobian(&self) -> Result<ZohLegMismatchJacobian<N>> {
        let segment_count = self.segment_count();
        let mut initial_state = identity();
        let mut final_state = identity();
        let mut controls = vec![vec![0.0; C * segment_count]; N];
        let mut time_grid = vec![vec![0.0; segment_count + 1]; N];

        let forward = self.segment_variations(true)?;
        let forward_maps = forward
            .iter()
            .map(|segment| segment.transition)
            .collect::<Vec<_>>();
        let forward_suffix = suffix_products(&forward_maps);
        if !forward.is_empty() {
            initial_state = forward_suffix[0];
            for (index, segment) in forward.iter().enumerate() {
                let block = matrix_times_columns(&forward_suffix[index + 1], &segment.control);
                write_control_block(&mut controls, index, &block, 1.0);
            }
            add_column(
                &mut time_grid,
                0,
                &matrix_times_vector(&forward_suffix[1], &forward[0].dynamics),
                -1.0,
            );
            let last = forward.len() - 1;
            add_column(
                &mut time_grid,
                forward.len(),
                &matrix_times_vector(&forward_suffix[forward.len()], &forward[last].dynamics),
                1.0,
            );
            for index in 1..forward.len() {
                let previous_at_next_start =
                    matrix_times_vector(&forward[index].transition, &forward[index - 1].dynamics);
                let jump = core::array::from_fn(|row| {
                    previous_at_next_start[row] - forward[index].dynamics[row]
                });
                let derivative = matrix_times_vector(&forward_suffix[index + 1], &jump);
                add_column(&mut time_grid, index, &derivative, 1.0);
            }
        }

        let backward = self.segment_variations(false)?;
        let backward_maps = backward
            .iter()
            .map(|segment| segment.transition)
            .collect::<Vec<_>>();
        let backward_suffix = suffix_products(&backward_maps);
        if !backward.is_empty() {
            final_state = backward_suffix[0].map(|row| row.map(|value| -value));
            for (index, segment) in backward.iter().enumerate() {
                let block = matrix_times_columns(&backward_suffix[index + 1], &segment.control);
                let chronological = segment_count - 1 - index;
                write_control_block(&mut controls, chronological, &block, -1.0);
            }
            add_column(
                &mut time_grid,
                segment_count,
                &matrix_times_vector(&backward_suffix[1], &backward[0].dynamics),
                1.0,
            );
            let last = backward.len() - 1;
            add_column(
                &mut time_grid,
                segment_count - backward.len(),
                &matrix_times_vector(&backward_suffix[backward.len()], &backward[last].dynamics),
                -1.0,
            );
            for index in 1..backward.len() {
                let previous_at_next_start =
                    matrix_times_vector(&backward[index].transition, &backward[index - 1].dynamics);
                let jump = core::array::from_fn(|row| {
                    previous_at_next_start[row] - backward[index].dynamics[row]
                });
                let derivative = matrix_times_vector(&backward_suffix[index + 1], &jump);
                add_column(&mut time_grid, segment_count - index, &derivative, -1.0);
            }
        } else {
            final_state = final_state.map(|row| row.map(|value| -value));
        }

        validate_rows("ZOH leg control Jacobian", &controls)?;
        validate_rows("ZOH leg time-grid Jacobian", &time_grid)?;
        Ok(ZohLegMismatchJacobian {
            initial_state,
            final_state,
            controls,
            time_grid,
        })
    }

    /// Samples every propagated segment at `samples_per_segment` uniformly
    /// spaced times, including both endpoints.
    ///
    /// # Errors
    ///
    /// Returns an error when fewer than two samples are requested or when a
    /// segment cannot be integrated.
    pub fn state_history(&self, samples_per_segment: usize) -> Result<ZohLegHistory<N>> {
        if samples_per_segment < 2 {
            return Err(PykepError::InvalidInput {
                parameter: "samples_per_segment",
                reason: "must be at least two".into(),
            });
        }
        Ok(ZohLegHistory {
            forward: self.sample_half(true, samples_per_segment)?,
            backward: self.sample_half(false, samples_per_segment)?,
        })
    }

    fn propagate_nominal_half(&self, forward: bool) -> Result<[f64; N]> {
        let mut state = if forward {
            self.initial_state
        } else {
            self.final_state
        };
        if forward {
            for index in 0..self.forward_segments {
                state = self.propagate_segment(index, state, true)?;
            }
        } else {
            for index in (self.forward_segments..self.segment_count()).rev() {
                state = self.propagate_segment(index, state, false)?;
            }
        }
        Ok(state)
    }

    fn propagate_segment(&self, index: usize, state: [f64; N], forward: bool) -> Result<[f64; N]> {
        let (start, end) = self.segment_interval(index, forward);
        Dop853
            .propagate(
                &self.model,
                InitialValueProblem::new(
                    start,
                    state,
                    end,
                    M::parameters(self.schedule.controls()[index], self.constants),
                ),
                self.options,
            )
            .map(|result| result.state)
            .map_err(|error| segment_error::<M, N, P>(forward, index, start, end, error))
    }

    fn segment_variations(&self, forward: bool) -> Result<Vec<SegmentVariation<N, C>>> {
        let mut state = if forward {
            self.initial_state
        } else {
            self.final_state
        };
        let indices: Box<dyn Iterator<Item = usize>> = if forward {
            Box::new(0..self.forward_segments)
        } else {
            Box::new((self.forward_segments..self.segment_count()).rev())
        };
        let mut output = Vec::with_capacity(if forward {
            self.forward_segment_count()
        } else {
            self.backward_segment_count()
        });
        for index in indices {
            let (start, end) = self.segment_interval(index, forward);
            let mut initial_sensitivities = [[0.0; W]; N];
            for (row, values) in initial_sensitivities.iter_mut().enumerate() {
                values[row] = 1.0;
            }
            let mut control_seeds = [[0.0; W]; C];
            for (row, values) in control_seeds.iter_mut().enumerate() {
                values[N + row] = 1.0;
            }
            let parameters = M::parameters(self.schedule.controls()[index], self.constants);
            let result = Dop853
                .propagate_with_sensitivities(
                    &self.model,
                    SensitivityProblem {
                        nominal: InitialValueProblem::new(start, state, end, parameters),
                        initial_sensitivities,
                        parameter_seeds: M::parameter_seeds(control_seeds, [[0.0; W]; K]),
                    },
                    self.options,
                )
                .map_err(|error| segment_error::<M, N, P>(forward, index, start, end, error))?;
            state = result.state;
            let transition = core::array::from_fn(|row| {
                core::array::from_fn(|column| result.sensitivities[row][column])
            });
            let control = core::array::from_fn(|row| {
                core::array::from_fn(|column| result.sensitivities[row][N + column])
            });
            let mut dynamics = [0.0; N];
            self.model
                .rhs(end, &state, &parameters, &mut dynamics)
                .map_err(|error| segment_error::<M, N, P>(forward, index, start, end, error))?;
            output.push(SegmentVariation {
                transition,
                control,
                dynamics,
            });
        }
        Ok(output)
    }

    fn sample_half(&self, forward: bool, samples: usize) -> Result<Vec<Vec<[f64; N]>>> {
        let mut state = if forward {
            self.initial_state
        } else {
            self.final_state
        };
        let indices: Box<dyn Iterator<Item = usize>> = if forward {
            Box::new(0..self.forward_segments)
        } else {
            Box::new((self.forward_segments..self.segment_count()).rev())
        };
        let mut result = Vec::new();
        for index in indices {
            let (start, end) = self.segment_interval(index, forward);
            let times = (0..samples)
                .map(|sample| {
                    start + (end - start) * sample as f64 / (samples.saturating_sub(1)) as f64
                })
                .collect::<Vec<_>>();
            let parameters = M::parameters(self.schedule.controls()[index], self.constants);
            let sample_states = if forward {
                Dop853
                    .propagate_dense(
                        &self.model,
                        InitialValueProblem::new(start, state, end, parameters),
                        &times,
                        self.options,
                    )
                    .map_err(|error| segment_error::<M, N, P>(forward, index, start, end, error))?
                    .states
            } else {
                // differential-equations 0.6.1 can panic when a decreasing
                // dense-output request rounds just outside the completed step,
                // so integrate the equivalent dx/dtau = -f(start - tau, x)
                // problem on an increasing interval.
                let evaluation_times = times
                    .iter()
                    .map(|&sample_time| start - sample_time)
                    .collect::<Vec<_>>();
                Dop853
                    .propagate_dense(
                        &ReverseTimeModel {
                            model: &self.model,
                            origin: start,
                        },
                        InitialValueProblem::new(0.0, state, start - end, parameters),
                        &evaluation_times,
                        self.options,
                    )
                    .map_err(|error| segment_error::<M, N, P>(forward, index, start, end, error))?
                    .states
            };
            state = sample_states.last().copied().ok_or_else(|| {
                segment_error::<M, N, P>(
                    forward,
                    index,
                    start,
                    end,
                    PykepError::IntegrationFailure {
                        model: M::NAME,
                        reason: "dense propagation returned no samples".into(),
                    },
                )
            })?;
            result.push(sample_states);
        }
        Ok(result)
    }

    fn segment_interval(&self, index: usize, forward: bool) -> (f64, f64) {
        if forward {
            (
                self.schedule.boundaries()[index],
                self.schedule.boundaries()[index + 1],
            )
        } else {
            (
                self.schedule.boundaries()[index + 1],
                self.schedule.boundaries()[index],
            )
        }
    }
}

/// Evaluates a batch of already validated ZOH legs in input order.
///
/// # Errors
///
/// Returns the first segment-contextual integration error.
pub fn evaluate_zoh_mismatch_batch<
    M,
    const N: usize,
    const C: usize,
    const K: usize,
    const P: usize,
    const W: usize,
>(
    legs: &[ZohLeg<M, N, C, K, P, W>],
) -> Result<Vec<[f64; N]>>
where
    M: ZeroOrderHoldModel<N, C, K, P>,
{
    legs.iter().map(ZohLeg::mismatch_constraints).collect()
}

/// Evaluates independent generic ZOH-leg mismatches in parallel.
///
/// Output order matches input order. Zero workers uses the shared global
/// pool, one executes serially, and larger values use exactly that many cached
/// worker threads.
///
/// # Errors
///
/// Returns an invalid worker count or the first leg evaluation error in input
/// order.
pub fn evaluate_zoh_mismatch_batch_parallel<
    M,
    const N: usize,
    const C: usize,
    const K: usize,
    const P: usize,
    const W: usize,
>(
    legs: &[ZohLeg<M, N, C, K, P, W>],
    workers: usize,
) -> Result<Vec<[f64; N]>>
where
    M: ZeroOrderHoldModel<N, C, K, P> + Sync,
{
    crate::batch::try_map(legs, workers, ZohLeg::mismatch_constraints)
}

#[derive(Clone, Copy)]
struct SegmentVariation<const N: usize, const C: usize> {
    transition: [[f64; N]; N],
    control: [[f64; C]; N],
    dynamics: [f64; N],
}

fn identity<const N: usize>() -> [[f64; N]; N] {
    core::array::from_fn(|row| core::array::from_fn(|column| f64::from(row == column)))
}

fn matrix_product<const N: usize>(left: &[[f64; N]; N], right: &[[f64; N]; N]) -> [[f64; N]; N] {
    core::array::from_fn(|row| {
        core::array::from_fn(|column| {
            (0..N)
                .map(|inner| left[row][inner] * right[inner][column])
                .sum()
        })
    })
}

fn suffix_products<const N: usize>(matrices: &[[[f64; N]; N]]) -> Vec<[[f64; N]; N]> {
    let mut suffix = vec![identity(); matrices.len() + 1];
    for index in (0..matrices.len()).rev() {
        suffix[index] = matrix_product(&suffix[index + 1], &matrices[index]);
    }
    suffix
}

fn matrix_times_columns<const N: usize, const C: usize>(
    matrix: &[[f64; N]; N],
    columns: &[[f64; C]; N],
) -> [[f64; C]; N] {
    core::array::from_fn(|row| {
        core::array::from_fn(|column| {
            (0..N)
                .map(|inner| matrix[row][inner] * columns[inner][column])
                .sum()
        })
    })
}

fn matrix_times_vector<const N: usize>(matrix: &[[f64; N]; N], vector: &[f64; N]) -> [f64; N] {
    core::array::from_fn(|row| {
        (0..N)
            .map(|column| matrix[row][column] * vector[column])
            .sum()
    })
}

fn write_control_block<const N: usize, const C: usize>(
    destination: &mut [Vec<f64>],
    segment: usize,
    block: &[[f64; C]; N],
    scale: f64,
) {
    for row in 0..N {
        for column in 0..C {
            destination[row][C * segment + column] = scale * block[row][column];
        }
    }
}

fn add_column<const N: usize>(
    destination: &mut [Vec<f64>],
    column: usize,
    values: &[f64; N],
    scale: f64,
) {
    for row in 0..N {
        destination[row][column] += scale * values[row];
    }
}

fn validate_rows(operation: &'static str, rows: &[Vec<f64>]) -> Result<()> {
    if rows.iter().flatten().all(|value| value.is_finite()) {
        Ok(())
    } else {
        Err(PykepError::NumericalOverflow { operation })
    }
}

fn segment_error<M, const N: usize, const P: usize>(
    forward: bool,
    index: usize,
    start: f64,
    end: f64,
    error: PykepError,
) -> PykepError
where
    M: DynamicsModel<N, P>,
{
    let direction = if forward { "forward" } else { "backward" };
    PykepError::IntegrationFailure {
        model: M::NAME,
        reason: format!("{direction} segment {index} [{start}, {end}]: {error}"),
    }
}

#[cfg(test)]
mod tests {
    use super::{ZohKeplerDynamics, ZohLeg};
    use crate::integration::IntegratorOptions;

    #[test]
    fn cut_boundaries_have_exact_segment_counts() {
        let make = |cut| {
            ZohLeg::<ZohKeplerDynamics, 7, 4, 1, 5, 11>::new(
                ZohKeplerDynamics,
                [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0],
                vec![[0.0; 4]; 5],
                [0.5, 0.8, 0.0, -0.8, 0.5, 0.0, 1.0],
                vec![0.0, 0.2, 0.4, 0.6, 0.8, 1.0],
                [0.1],
                cut,
                IntegratorOptions::default(),
            )
            .unwrap()
        };
        assert_eq!(make(0.0).forward_segment_count(), 0);
        assert_eq!(make(0.5).forward_segment_count(), 2);
        assert_eq!(make(1.0).forward_segment_count(), 5);
    }
}