pictorus-blocks 0.0.0

Implementations of Pictorus blocks.
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
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
use crate::nalgebra_interop::MatrixExt;
use pictorus_block_data::{BlockData as OldBlockData, FromPass};
use pictorus_traits::{Matrix, Pass, PassBy, ProcessBlock, Scalar};

/// Sums (adds or subtracts) all inputs together.
pub struct SumBlock<T: Summable>
where
    pictorus_block_data::BlockData: FromPass<<T as Summable>::Output>,
{
    store: Option<T::Output>,
    pub data: OldBlockData,
}

impl<T: Summable> Default for SumBlock<T>
where
    pictorus_block_data::BlockData: FromPass<<T as Summable>::Output>,
{
    fn default() -> Self {
        Self {
            store: None,
            data: <OldBlockData as FromPass<T::Output>>::from_pass(T::Output::default().as_by()),
        }
    }
}

impl<T> ProcessBlock for SumBlock<T>
where
    T: Summable,
    OldBlockData: FromPass<T::Output>,
{
    type Inputs = T;
    type Output = T::Output;
    type Parameters = T::Parameters;

    fn process(
        &mut self,
        parameters: &Self::Parameters,
        _context: &dyn pictorus_traits::Context,
        input: PassBy<Self::Inputs>,
    ) -> PassBy<Self::Output> {
        self.store = None;
        let result = T::get_sum(input, *parameters, &mut self.store);
        self.data = OldBlockData::from_pass(result);
        result
    }
}

trait SumScalar:
    Scalar
    + nalgebra::Scalar
    + core::ops::Neg<Output = Self>
    + core::ops::Add<Output = Self>
    + core::ops::Sub<Output = Self>
    + core::ops::AddAssign
    + core::ops::SubAssign
{
}
impl SumScalar for f32 {}
impl SumScalar for f64 {}

/// This trait is used to determine the output type of a sum operation
/// between two types, most importantly it can be used recursively. To get the output type for
/// a tuple of inputs. For an input of all scalars the output is scalar. For all inputs being a
/// single size of matrix, or a mix of scalars and a single size of matrix the output is a matrix
/// of that size.
pub trait TypePromotion<RHS> {
    type Output: Pass + Default;
}

/// A Scalar and a scalar outputs a scalar
impl<S: SumScalar> TypePromotion<S> for S {
    type Output = S;
}

/// A Scalar and a Matrix outputs a Matrix
impl<const R: usize, const C: usize, S: SumScalar> TypePromotion<S> for Matrix<R, C, S> {
    type Output = Matrix<R, C, S>;
}

/// A Matrix and a Scalar outputs a Matrix
impl<const R: usize, const C: usize, S: SumScalar> TypePromotion<Matrix<R, C, S>> for S {
    type Output = Matrix<R, C, S>;
}

/// A Matrix and a Matrix outputs a Matrix
impl<const R: usize, const C: usize, S: SumScalar> TypePromotion<Matrix<R, C, S>>
    for Matrix<R, C, S>
{
    type Output = Matrix<R, C, S>;
}

/// Recursive definition for 3 inputs
impl<A, B, C> TypePromotion<(B, C)> for A
where
    B: TypePromotion<C>,
    A: TypePromotion<<B as TypePromotion<C>>::Output>,
{
    type Output = <A as TypePromotion<B::Output>>::Output;
}

/// Recursive definition for 4 inputs
impl<A, B, C, D> TypePromotion<(B, C, D)> for A
where
    B: TypePromotion<(C, D)>,
    A: TypePromotion<B::Output>,
{
    type Output = <A as TypePromotion<B::Output>>::Output;
}

/// Recursive definition for 5 inputs
impl<A, B, C, D, E> TypePromotion<(B, C, D, E)> for A
where
    B: TypePromotion<(C, D, E)>,
    A: TypePromotion<B::Output>,
{
    type Output = <A as TypePromotion<B::Output>>::Output;
}

/// Recursive definition for 6 inputs
impl<A, B, C, D, E, F> TypePromotion<(B, C, D, E, F)> for A
where
    B: TypePromotion<(C, D, E, F)>,
    A: TypePromotion<B::Output>,
{
    type Output = <A as TypePromotion<B::Output>>::Output;
}

/// Recursive definition for 7 inputs
impl<A, B, C, D, E, F, G> TypePromotion<(B, C, D, E, F, G)> for A
where
    B: TypePromotion<(C, D, E, F, G)>,
    A: TypePromotion<B::Output>,
{
    type Output = <A as TypePromotion<B::Output>>::Output;
}

/// Recursive definition for 8 inputs
impl<A, B, C, D, E, F, G, H> TypePromotion<(B, C, D, E, F, G, H)> for A
where
    B: TypePromotion<(C, D, E, F, G, H)>,
    A: TypePromotion<B::Output>,
{
    type Output = <A as TypePromotion<B::Output>>::Output;
}

/// This trait allow the implementor to be "summed into" a destination type
/// A matrix can only be summed into a matrix of the same size, a scalar can be summed into
/// a matrix or another scalar
pub trait SumInto<DEST: Pass>: Pass {
    fn sum_into<'a>(
        input: PassBy<Self>,
        sum_type: SumType,
        dest: &'a mut Option<DEST>,
    ) -> PassBy<'a, DEST>;
}

/// Scalar summing into a scalar
impl<S: SumScalar> SumInto<S> for S {
    fn sum_into<'a>(
        input: PassBy<Self>,
        sum_type: SumType,
        dest: &'a mut Option<S>,
    ) -> PassBy<'a, S> {
        let dest = dest.get_or_insert(S::default());
        match sum_type {
            SumType::Addition => {
                *dest += input;
            }
            SumType::Subtraction => {
                *dest -= input;
            }
        }
        *dest
    }
}

/// Matrix summing into a matrix
impl<const R: usize, const C: usize, S: SumScalar> SumInto<Matrix<R, C, S>> for Matrix<R, C, S> {
    fn sum_into<'a>(
        input: PassBy<Self>,
        sum_type: SumType,
        dest: &'a mut Option<Matrix<R, C, S>>,
    ) -> PassBy<'a, Matrix<R, C, S>> {
        let dest = dest.get_or_insert(Matrix::<R, C, S>::zeroed());
        let orig_dest = dest.as_view().clone_owned();
        match sum_type {
            SumType::Addition => {
                orig_dest.add_to(&input.as_view(), &mut dest.as_view_mut());
            }
            SumType::Subtraction => {
                orig_dest.sub_to(&input.as_view(), &mut dest.as_view_mut());
            }
        }
        dest
    }
}

/// Scalar summing into a matrix
impl<const R: usize, const C: usize, S: SumScalar> SumInto<Matrix<R, C, S>> for S {
    fn sum_into<'a>(
        input: PassBy<Self>,
        sum_type: SumType,
        dest: &'a mut Option<Matrix<R, C, S>>,
    ) -> PassBy<'a, Matrix<R, C, S>> {
        let dest = dest.get_or_insert(Matrix::<R, C, S>::zeroed());
        let mut orig_dest = dest.as_view().clone_owned();
        match sum_type {
            SumType::Addition => {
                orig_dest = orig_dest.add_scalar(input);
            }
            SumType::Subtraction => {
                orig_dest = orig_dest.add_scalar(-input);
            }
        }
        dest.as_view_mut().copy_from(&orig_dest);
        dest
    }
}

/// This trait makes use of the two above , `SumInto` and `TypePromotion` to sum a tuple of inputs (or a single input)
pub trait Summable: Pass {
    type Output: Pass + Default;
    type Parameters: Copy;

    fn get_sum<'a>(
        input: PassBy<Self>,
        parameters: Self::Parameters,
        dest: &'a mut Option<Self::Output>,
    ) -> PassBy<'a, Self::Output>;
}

/// Single scalar input
impl<S: SumScalar> Summable for S {
    type Output = S;
    type Parameters = Parameters<1>;

    fn get_sum<'a>(
        input: PassBy<Self>,
        parameters: Self::Parameters,
        dest: &'a mut Option<Self::Output>,
    ) -> PassBy<'a, Self::Output> {
        Self::sum_into(input, parameters.operations[0], dest);
        dest.unwrap()
    }
}

/// Single matrix input
impl<const R: usize, const C: usize, S: SumScalar> Summable for Matrix<R, C, S> {
    type Output = Matrix<R, C, S>;
    type Parameters = Parameters<1>;

    fn get_sum<'a>(
        input: PassBy<Self>,
        parameters: Self::Parameters,
        dest: &'a mut Option<Self::Output>,
    ) -> PassBy<'a, Self::Output> {
        Self::sum_into(input, parameters.operations[0], dest);
        dest.as_ref().unwrap()
    }
}

impl<A, B> Summable for (A, B)
where
    A: TypePromotion<B>,
    A: SumInto<A::Output>,
    B: SumInto<A::Output>,
{
    type Output = A::Output;
    type Parameters = Parameters<2>;

    fn get_sum<'a>(
        input: PassBy<Self>,
        parameters: Self::Parameters,
        dest: &'a mut Option<Self::Output>,
    ) -> PassBy<'a, Self::Output> {
        let (a, b) = input;
        A::sum_into(a, parameters.operations[0], dest);
        B::sum_into(b, parameters.operations[1], dest)
    }
}

impl<A, B, C> Summable for (A, B, C)
where
    A: TypePromotion<(B, C)>,
    A: SumInto<A::Output>,
    B: SumInto<A::Output>,
    C: SumInto<A::Output>,
{
    type Output = A::Output;
    type Parameters = Parameters<3>;

    fn get_sum<'a>(
        input: PassBy<Self>,
        parameters: Self::Parameters,
        dest: &'a mut Option<Self::Output>,
    ) -> PassBy<'a, Self::Output> {
        let (a, b, c) = input;
        A::sum_into(a, parameters.operations[0], dest);
        B::sum_into(b, parameters.operations[1], dest);
        C::sum_into(c, parameters.operations[2], dest)
    }
}

impl<A, B, C, D> Summable for (A, B, C, D)
where
    A: TypePromotion<(B, C, D)>,
    A: SumInto<A::Output>,
    B: SumInto<A::Output>,
    C: SumInto<A::Output>,
    D: SumInto<A::Output>,
{
    type Output = A::Output;
    type Parameters = Parameters<4>;

    fn get_sum<'a>(
        input: PassBy<Self>,
        parameters: Self::Parameters,
        dest: &'a mut Option<Self::Output>,
    ) -> PassBy<'a, Self::Output> {
        let (a, b, c, d) = input;
        A::sum_into(a, parameters.operations[0], dest);
        B::sum_into(b, parameters.operations[1], dest);
        C::sum_into(c, parameters.operations[2], dest);
        D::sum_into(d, parameters.operations[3], dest)
    }
}

impl<A, B, C, D, E> Summable for (A, B, C, D, E)
where
    A: TypePromotion<(B, C, D, E)>,
    A: SumInto<A::Output>,
    B: SumInto<A::Output>,
    C: SumInto<A::Output>,
    D: SumInto<A::Output>,
    E: SumInto<A::Output>,
{
    type Output = A::Output;
    type Parameters = Parameters<5>;

    fn get_sum<'a>(
        input: PassBy<Self>,
        parameters: Self::Parameters,
        dest: &'a mut Option<Self::Output>,
    ) -> PassBy<'a, Self::Output> {
        let (a, b, c, d, e) = input;
        A::sum_into(a, parameters.operations[0], dest);
        B::sum_into(b, parameters.operations[1], dest);
        C::sum_into(c, parameters.operations[2], dest);
        D::sum_into(d, parameters.operations[3], dest);
        E::sum_into(e, parameters.operations[4], dest)
    }
}

impl<A, B, C, D, E, F> Summable for (A, B, C, D, E, F)
where
    A: TypePromotion<(B, C, D, E, F)>,
    A: SumInto<A::Output>,
    B: SumInto<A::Output>,
    C: SumInto<A::Output>,
    D: SumInto<A::Output>,
    E: SumInto<A::Output>,
    F: SumInto<A::Output>,
{
    type Output = A::Output;
    type Parameters = Parameters<6>;

    fn get_sum<'a>(
        input: PassBy<Self>,
        parameters: Self::Parameters,
        dest: &'a mut Option<Self::Output>,
    ) -> PassBy<'a, Self::Output> {
        let (a, b, c, d, e, f) = input;
        A::sum_into(a, parameters.operations[0], dest);
        B::sum_into(b, parameters.operations[1], dest);
        C::sum_into(c, parameters.operations[2], dest);
        D::sum_into(d, parameters.operations[3], dest);
        E::sum_into(e, parameters.operations[4], dest);
        F::sum_into(f, parameters.operations[5], dest)
    }
}

impl<A, B, C, D, E, F, G> Summable for (A, B, C, D, E, F, G)
where
    A: TypePromotion<(B, C, D, E, F, G)>,
    A: SumInto<A::Output>,
    B: SumInto<A::Output>,
    C: SumInto<A::Output>,
    D: SumInto<A::Output>,
    E: SumInto<A::Output>,
    F: SumInto<A::Output>,
    G: SumInto<A::Output>,
{
    type Output = A::Output;
    type Parameters = Parameters<7>;

    fn get_sum<'a>(
        input: PassBy<Self>,
        parameters: Self::Parameters,
        dest: &'a mut Option<Self::Output>,
    ) -> PassBy<'a, Self::Output> {
        let (a, b, c, d, e, f, g) = input;
        A::sum_into(a, parameters.operations[0], dest);
        B::sum_into(b, parameters.operations[1], dest);
        C::sum_into(c, parameters.operations[2], dest);
        D::sum_into(d, parameters.operations[3], dest);
        E::sum_into(e, parameters.operations[4], dest);
        F::sum_into(f, parameters.operations[5], dest);
        G::sum_into(g, parameters.operations[6], dest)
    }
}

impl<A, B, C, D, E, F, G, H> Summable for (A, B, C, D, E, F, G, H)
where
    A: TypePromotion<(B, C, D, E, F, G, H)>,
    A: SumInto<A::Output>,
    B: SumInto<A::Output>,
    C: SumInto<A::Output>,
    D: SumInto<A::Output>,
    E: SumInto<A::Output>,
    F: SumInto<A::Output>,
    G: SumInto<A::Output>,
    H: SumInto<A::Output>,
{
    type Output = A::Output;
    type Parameters = Parameters<8>;

    fn get_sum<'a>(
        input: PassBy<Self>,
        parameters: Self::Parameters,
        dest: &'a mut Option<Self::Output>,
    ) -> PassBy<'a, Self::Output> {
        let (a, b, c, d, e, f, g, h) = input;
        A::sum_into(a, parameters.operations[0], dest);
        B::sum_into(b, parameters.operations[1], dest);
        C::sum_into(c, parameters.operations[2], dest);
        D::sum_into(d, parameters.operations[3], dest);
        E::sum_into(e, parameters.operations[4], dest);
        F::sum_into(f, parameters.operations[5], dest);
        G::sum_into(g, parameters.operations[6], dest);
        H::sum_into(h, parameters.operations[7], dest)
    }
}

/// The type of sum to perform
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum SumType {
    Addition,
    Subtraction,
}

/// The parameters for the sum block
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Parameters<const NUM_INPUTS: usize> {
    pub operations: [SumType; NUM_INPUTS],
}

impl<const NUM_INPUTS: usize> Parameters<NUM_INPUTS> {
    /// This new function accepts a fixed size arrays of f64 because that is what codgen hands it currently
    /// It should be revisited when we tackle codegen changes
    pub fn new(input: [f64; NUM_INPUTS]) -> Self {
        let mut operations = [SumType::Addition; NUM_INPUTS];
        for (i, &val) in input.iter().enumerate() {
            if val < 0.0 {
                operations[i] = SumType::Subtraction;
            }
        }
        Self { operations }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::testing::StubContext;
    use approx::assert_relative_eq;

    #[test]
    fn test_one_scalar() {
        let mut block = SumBlock::<f64>::default();
        let input = 3.0;
        let stub_context = StubContext::default();
        let parameters = Parameters {
            operations: [SumType::Addition],
        };
        let result = block.process(&parameters, &stub_context, input);
        assert_relative_eq!(result, 3.0);
    }

    #[test]
    fn test_one_matrix() {
        let mut block = SumBlock::<Matrix<2, 2, f64>>::default();
        let input = Matrix {
            data: [[1.0, 2.0], [3.0, 4.0]],
        };
        let stub_context = StubContext::default();
        let parameters = Parameters {
            operations: [SumType::Addition],
        };
        let result = block.process(&parameters, &stub_context, &input);
        assert_relative_eq!(
            result.data.as_flattened(),
            [[1.0, 2.0], [3.0, 4.0]].as_flattened()
        );
    }

    #[test]
    fn test_multiple_scalars() {
        let stub_context = StubContext::default();

        // Two Inputs
        let mut two_block = SumBlock::<(f64, f64)>::default();
        let input = (3.0, 4.0);
        let parameters = Parameters {
            operations: [SumType::Addition, SumType::Addition],
        };
        let result = two_block.process(&parameters, &stub_context, input);
        assert_relative_eq!(result, 7.0);

        let parameters = Parameters {
            operations: [SumType::Addition, SumType::Subtraction],
        };
        let result = two_block.process(&parameters, &stub_context, input);
        assert_relative_eq!(result, -1.0);

        let parameters = Parameters {
            operations: [SumType::Subtraction, SumType::Addition],
        };
        let result = two_block.process(&parameters, &stub_context, input);
        assert_relative_eq!(result, 1.0);

        let parameters = Parameters {
            operations: [SumType::Subtraction, SumType::Subtraction],
        };
        let result = two_block.process(&parameters, &stub_context, input);
        assert_relative_eq!(result, -7.0);

        // Three Inputs
        let mut three_block = SumBlock::<(f64, f64, f64)>::default();
        let input = (3.0, 4.0, 5.0);
        let parameters = Parameters {
            operations: [SumType::Addition, SumType::Addition, SumType::Addition],
        };
        let result = three_block.process(&parameters, &stub_context, input);
        assert_relative_eq!(result, 12.0);

        let parameters = Parameters {
            operations: [SumType::Addition, SumType::Addition, SumType::Subtraction],
        };
        let result = three_block.process(&parameters, &stub_context, input);
        assert_relative_eq!(result, 2.0);

        // Four Inputs
        let mut four_block = SumBlock::<(f64, f64, f64, f64)>::default();
        let input = (3.0, 4.0, 5.0, 6.0);
        let parameters = Parameters {
            operations: [
                SumType::Addition,
                SumType::Addition,
                SumType::Addition,
                SumType::Addition,
            ],
        };
        let result = four_block.process(&parameters, &stub_context, input);
        assert_relative_eq!(result, 18.0);

        // Five Inputs
        let mut five_block = SumBlock::<(f64, f64, f64, f64, f64)>::default();
        let input = (3.0, 4.0, 5.0, 6.0, 7.0);
        let parameters = Parameters {
            operations: [
                SumType::Addition,
                SumType::Addition,
                SumType::Addition,
                SumType::Addition,
                SumType::Addition,
            ],
        };
        let result = five_block.process(&parameters, &stub_context, input);
        assert_relative_eq!(result, 25.0);

        // Six Inputs
        let mut six_block = SumBlock::<(f64, f64, f64, f64, f64, f64)>::default();
        let input = (3.0, 4.0, 5.0, 6.0, 7.0, 8.0);
        let parameters = Parameters {
            operations: [
                SumType::Addition,
                SumType::Addition,
                SumType::Addition,
                SumType::Addition,
                SumType::Addition,
                SumType::Addition,
            ],
        };
        let result = six_block.process(&parameters, &stub_context, input);
        assert_relative_eq!(result, 33.0);

        // Seven Inputs
        let mut seven_block = SumBlock::<(f64, f64, f64, f64, f64, f64, f64)>::default();
        let input = (3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0);
        let parameters = Parameters {
            operations: [
                SumType::Addition,
                SumType::Addition,
                SumType::Addition,
                SumType::Addition,
                SumType::Addition,
                SumType::Addition,
                SumType::Addition,
            ],
        };
        let result = seven_block.process(&parameters, &stub_context, input);
        assert_relative_eq!(result, 42.0);

        // Eight Inputs
        let mut eight_block = SumBlock::<(f64, f64, f64, f64, f64, f64, f64, f64)>::default();
        let input = (3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0);
        let parameters = Parameters {
            operations: [
                SumType::Addition,
                SumType::Addition,
                SumType::Addition,
                SumType::Addition,
                SumType::Addition,
                SumType::Addition,
                SumType::Addition,
                SumType::Addition,
            ],
        };
        let result = eight_block.process(&parameters, &stub_context, input);
        assert_relative_eq!(result, 52.0);
    }

    #[test]
    fn test_multiple_matrices() {
        let stub_context = StubContext::default();

        // Two Inputs
        let mut two_block = SumBlock::<(Matrix<2, 2, f64>, Matrix<2, 2, f64>)>::default();
        let input = (
            &Matrix {
                data: [[1.0, 2.0], [3.0, 4.0]],
            },
            &Matrix {
                data: [[5.0, 6.0], [7.0, 8.0]],
            },
        );
        let parameters = Parameters {
            operations: [SumType::Addition, SumType::Addition],
        };
        let result = two_block.process(&parameters, &stub_context, input);
        assert_relative_eq!(
            result.data.as_flattened(),
            [[6.0, 8.0], [10.0, 12.0]].as_flattened()
        );

        let parameters = Parameters {
            operations: [SumType::Addition, SumType::Subtraction],
        };
        let result = two_block.process(&parameters, &stub_context, input);
        assert_relative_eq!(
            result.data.as_flattened(),
            [[-4.0, -4.0], [-4.0, -4.0]].as_flattened()
        );

        let parameters = Parameters {
            operations: [SumType::Subtraction, SumType::Addition],
        };
        let result = two_block.process(&parameters, &stub_context, input);
        assert_relative_eq!(
            result.data.as_flattened(),
            [[4.0, 4.0], [4.0, 4.0]].as_flattened()
        );

        let parameters = Parameters {
            operations: [SumType::Subtraction, SumType::Subtraction],
        };
        let result = two_block.process(&parameters, &stub_context, input);
        assert_relative_eq!(
            result.data.as_flattened(),
            [[-6.0, -8.0], [-10.0, -12.0]].as_flattened()
        );

        // Three Inputs
        let mut three_block =
            SumBlock::<(Matrix<2, 2, f64>, Matrix<2, 2, f64>, Matrix<2, 2, f64>)>::default();
        let input = (
            &Matrix {
                data: [[1.0, 2.0], [3.0, 4.0]],
            },
            &Matrix {
                data: [[5.0, 6.0], [7.0, 8.0]],
            },
            &Matrix {
                data: [[9.0, 10.0], [11.0, 12.0]],
            },
        );
        let parameters = Parameters {
            operations: [SumType::Addition, SumType::Addition, SumType::Addition],
        };
        let result = three_block.process(&parameters, &stub_context, input);
        assert_relative_eq!(
            result.data.as_flattened(),
            [[15.0, 18.0], [21.0, 24.0]].as_flattened()
        );

        // Four Inputs
        let mut four_block = SumBlock::<(
            Matrix<2, 2, f64>,
            Matrix<2, 2, f64>,
            Matrix<2, 2, f64>,
            Matrix<2, 2, f64>,
        )>::default();
        let input = (
            &Matrix {
                data: [[1.0, 2.0], [3.0, 4.0]],
            },
            &Matrix {
                data: [[5.0, 6.0], [7.0, 8.0]],
            },
            &Matrix {
                data: [[9.0, 10.0], [11.0, 12.0]],
            },
            &Matrix {
                data: [[13.0, 14.0], [15.0, 16.0]],
            },
        );
        let parameters = Parameters {
            operations: [
                SumType::Addition,
                SumType::Addition,
                SumType::Addition,
                SumType::Addition,
            ],
        };
        let result = four_block.process(&parameters, &stub_context, input);
        assert_relative_eq!(
            result.data.as_flattened(),
            [[28.0, 32.0], [36.0, 40.0]].as_flattened()
        );
    }

    #[test]
    fn test_mixed_scalars_and_matrices() {
        let stub_context = StubContext::default();

        // Two Inputs
        let mut two_block = SumBlock::<(f64, Matrix<2, 2, f64>)>::default();
        let input = (
            3.0,
            &Matrix {
                data: [[1.0, 2.0], [3.0, 4.0]],
            },
        );
        let parameters = Parameters {
            operations: [SumType::Addition, SumType::Addition],
        };
        let result = two_block.process(&parameters, &stub_context, input);
        assert_relative_eq!(
            result.data.as_flattened(),
            [[4.0, 5.0], [6.0, 7.0]].as_flattened()
        );

        // Three Inputs
        let mut three_block_1 = SumBlock::<(f64, Matrix<2, 2, f64>, f64)>::default();
        let input = (
            3.0,
            &Matrix {
                data: [[1.0, 2.0], [3.0, 4.0]],
            },
            5.0,
        );
        let parameters = Parameters {
            operations: [SumType::Addition, SumType::Addition, SumType::Addition],
        };
        let result = three_block_1.process(&parameters, &stub_context, input);
        assert_relative_eq!(
            result.data.as_flattened(),
            [[9.0, 10.0], [11.0, 12.0]].as_flattened()
        );

        let mut three_block_2 = SumBlock::<(Matrix<2, 2, f64>, f64, Matrix<2, 2, f64>)>::default();
        let input = (
            &Matrix {
                data: [[1.0, 2.0], [3.0, 4.0]],
            },
            5.0,
            &Matrix {
                data: [[5.0, 6.0], [7.0, 8.0]],
            },
        );
        let parameters = Parameters {
            operations: [SumType::Addition, SumType::Addition, SumType::Addition],
        };
        let result = three_block_2.process(&parameters, &stub_context, input);
        assert_relative_eq!(
            result.data.as_flattened(),
            [[11.0, 13.0], [15.0, 17.0]].as_flattened()
        );
    }
}