conspire 0.7.6

The Rust interface to conspire.
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
#[cfg(test)]
mod test;

pub(crate) mod sparse_vec;
pub(crate) mod sparse_vec_2d;
pub(crate) mod vec;

use super::{
    Differentiate, Erase, Hessian, Jacobian, Solution, SquareMatrix, Tensor, TensorArray, Vector,
    rank_0::TensorRank0,
};
use crate::math::{TensorList, assert::FiniteDifference};
use crate::units::{Dimensionless, UnitDiv, UnitHalves, UnitInv, UnitMul};
use std::{
    cmp::Ordering,
    fmt::{self, Display, Formatter},
    marker::PhantomData,
    ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign},
};

/// Implemented only where the two types are the same, so that a unit may be
/// named where it is discarded without allowing a different one.
pub trait Is<T> {}

impl<T> Is<T> for T {}

/// A scalar carrying a physical unit.
#[repr(transparent)]
pub struct Quantity<U = Dimensionless>(TensorRank0, PhantomData<U>);

impl<U> Quantity<U> {
    /// Associated function for const type conversion.
    pub(crate) const fn new(value: TensorRank0) -> Self {
        Self(value, PhantomData)
    }
    /// Returns the value with its unit discarded.
    pub const fn value(&self) -> TensorRank0 {
        self.0
    }
    /// Returns the value, stating the unit being discarded.
    pub const fn value_as<V>(&self) -> TensorRank0
    where
        U: Is<V>,
    {
        self.0
    }
}

impl<U> Quantity<U> {
    /// Returns the absolute value, which leaves the unit alone.
    pub fn abs(self) -> Self {
        Self::new(self.0.abs())
    }
    /// Returns whether the value is not a number.
    pub fn is_nan(&self) -> bool {
        self.0.is_nan()
    }
    /// Returns whether two quantities differ by more than the epsilon
    /// relatively, at least one of them being large enough for that ratio to
    /// mean anything.
    pub fn differs(self, quantity: Self, epsilon: TensorRank0) -> bool {
        ((self.0 / quantity.0 - 1.0).abs() >= epsilon
            && (self.0.abs() >= epsilon || quantity.0.abs() >= epsilon))
            || self.is_nan()
            || quantity.is_nan()
    }
    /// Returns whether two quantities [differ](Self::differs) absolutely as
    /// well as relatively.
    pub fn differs_severely(self, quantity: Self, epsilon: TensorRank0) -> bool {
        ((self.0 / quantity.0 - 1.0).abs() >= epsilon
            && (self.0 - quantity.0).abs() >= epsilon
            && (self.0.abs() >= epsilon || quantity.0.abs() >= epsilon))
            || self.is_nan()
            || quantity.is_nan()
    }
    /// Returns how two quantities of the same unit order, totally.
    pub fn total_cmp(&self, quantity: &Self) -> Ordering {
        self.0.total_cmp(&quantity.0)
    }
    /// Returns the lesser of two quantities of the same unit.
    pub fn min(self, quantity: Self) -> Self {
        Self::new(self.0.min(quantity.0))
    }
    /// Returns the greater of two quantities of the same unit.
    pub fn max(self, quantity: Self) -> Self {
        Self::new(self.0.max(quantity.0))
    }
}

impl<U> Quantity<U>
where
    U: UnitHalves,
{
    /// Returns the quantity twice over, as the units the halves of a tuple take
    /// from it.
    pub const fn halves(
        self,
    ) -> (
        Quantity<<U as UnitHalves>::First>,
        Quantity<<U as UnitHalves>::Second>,
    ) {
        (Quantity::new(self.0), Quantity::new(self.0))
    }
}

impl Quantity<Dimensionless> {
    /// Returns the smallest integer greater than or equal to the value.
    pub fn ceil(self) -> Self {
        Self::new(self.0.ceil())
    }
    /// Returns the largest integer less than or equal to the value.
    pub fn floor(self) -> Self {
        Self::new(self.0.floor())
    }
    /// Raises to an integer power.
    pub fn powi(self, n: i32) -> Self {
        Self::new(self.0.powi(n))
    }
    /// Raises to a power.
    pub fn powf(self, n: TensorRank0) -> Self {
        Self::new(self.0.powf(n))
    }
    /// Returns the square root.
    pub fn sqrt(self) -> Self {
        Self::new(self.0.sqrt())
    }
    /// Returns the natural logarithm.
    pub fn ln(self) -> Self {
        Self::new(self.0.ln())
    }
    /// Returns the base-2 logarithm.
    pub fn log2(self) -> Self {
        Self::new(self.0.log2())
    }
    /// Returns the exponential.
    pub fn exp(self) -> Self {
        Self::new(self.0.exp())
    }
    /// Returns the sine.
    pub fn sin(self) -> Self {
        Self::new(self.0.sin())
    }
    /// Returns the cosine.
    pub fn cos(self) -> Self {
        Self::new(self.0.cos())
    }
}

impl<U> Clone for Quantity<U> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<U> Copy for Quantity<U> {}

impl<U> fmt::Debug for Quantity<U> {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        fmt::Debug::fmt(&self.0, f)
    }
}

impl<U> Display for Quantity<U> {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        Display::fmt(&self.0, f)
    }
}

impl<U> PartialEq for Quantity<U> {
    fn eq(&self, other: &Self) -> bool {
        self.0 == other.0
    }
}

impl<U> PartialOrd for Quantity<U> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        self.0.partial_cmp(&other.0)
    }
}

impl<U> Neg for Quantity<U> {
    type Output = Self;
    fn neg(self) -> Self::Output {
        Self::new(-self.0)
    }
}

impl<U> Default for Quantity<U> {
    fn default() -> Self {
        Self::new(0.0)
    }
}

impl<U> Add for Quantity<U> {
    type Output = Self;
    fn add(self, quantity: Self) -> Self::Output {
        Self::new(self.0 + quantity.0)
    }
}

impl<U> Add<&Self> for Quantity<U> {
    type Output = Self;
    fn add(self, quantity: &Self) -> Self::Output {
        Self::new(self.0 + quantity.0)
    }
}

impl<U> AddAssign<&Self> for Quantity<U> {
    fn add_assign(&mut self, quantity: &Self) {
        self.0 += quantity.0
    }
}

impl<U> Sub<&Self> for Quantity<U> {
    type Output = Self;
    fn sub(self, quantity: &Self) -> Self::Output {
        Self::new(self.0 - quantity.0)
    }
}

impl<U> SubAssign<&Self> for Quantity<U> {
    fn sub_assign(&mut self, quantity: &Self) {
        self.0 -= quantity.0
    }
}

impl<U> Add for &Quantity<U> {
    type Output = Quantity<U>;
    fn add(self, quantity: Self) -> Self::Output {
        Quantity::new(self.0 + quantity.0)
    }
}

impl<U> Add<Quantity<U>> for &Quantity<U> {
    type Output = Quantity<U>;
    fn add(self, quantity: Quantity<U>) -> Self::Output {
        Quantity::new(self.0 + quantity.0)
    }
}

impl<U> Sub<Quantity<U>> for &Quantity<U> {
    type Output = Quantity<U>;
    fn sub(self, quantity: Quantity<U>) -> Self::Output {
        Quantity::new(self.0 - quantity.0)
    }
}

impl<U> Div<TensorRank0> for &Quantity<U> {
    type Output = Quantity<U>;
    fn div(self, tensor_rank_0: TensorRank0) -> Self::Output {
        Quantity::new(self.0 / tensor_rank_0)
    }
}

impl<U> Div<&TensorRank0> for &Quantity<U> {
    type Output = Quantity<U>;
    fn div(self, tensor_rank_0: &TensorRank0) -> Self::Output {
        Quantity::new(self.0 / tensor_rank_0)
    }
}

impl<U> Neg for &Quantity<U> {
    type Output = Quantity<U>;
    fn neg(self) -> Self::Output {
        Quantity::new(-self.0)
    }
}

impl<U> Sub for &Quantity<U> {
    type Output = Quantity<U>;
    fn sub(self, quantity: Self) -> Self::Output {
        Quantity::new(self.0 - quantity.0)
    }
}

impl<U> Mul<TensorRank0> for &Quantity<U> {
    type Output = Quantity<U>;
    fn mul(self, tensor_rank_0: TensorRank0) -> Self::Output {
        Quantity::new(self.0 * tensor_rank_0)
    }
}

impl<U> Mul<&TensorRank0> for &Quantity<U> {
    type Output = Quantity<U>;
    fn mul(self, tensor_rank_0: &TensorRank0) -> Self::Output {
        Quantity::new(self.0 * tensor_rank_0)
    }
}

impl<U> MulAssign<&TensorRank0> for Quantity<U> {
    fn mul_assign(&mut self, tensor_rank_0: &TensorRank0) {
        self.0 *= tensor_rank_0
    }
}

impl<U> DivAssign<&TensorRank0> for Quantity<U> {
    fn div_assign(&mut self, tensor_rank_0: &TensorRank0) {
        self.0 /= tensor_rank_0
    }
}

impl<U> AddAssign for Quantity<U> {
    fn add_assign(&mut self, quantity: Self) {
        self.0 += quantity.0
    }
}

impl<U> Sub for Quantity<U> {
    type Output = Self;
    fn sub(self, quantity: Self) -> Self::Output {
        Self::new(self.0 - quantity.0)
    }
}

impl<U> SubAssign for Quantity<U> {
    fn sub_assign(&mut self, quantity: Self) {
        self.0 -= quantity.0
    }
}

impl<U> Mul<TensorRank0> for Quantity<U> {
    type Output = Self;
    fn mul(self, tensor_rank_0: TensorRank0) -> Self::Output {
        Self::new(self.0 * tensor_rank_0)
    }
}

impl<U> Mul<&TensorRank0> for Quantity<U> {
    type Output = Self;
    fn mul(self, tensor_rank_0: &TensorRank0) -> Self::Output {
        Self::new(self.0 * tensor_rank_0)
    }
}

impl<U> MulAssign<TensorRank0> for Quantity<U> {
    fn mul_assign(&mut self, tensor_rank_0: TensorRank0) {
        self.0 *= tensor_rank_0
    }
}

impl<U> Div<TensorRank0> for Quantity<U> {
    type Output = Self;
    fn div(self, tensor_rank_0: TensorRank0) -> Self::Output {
        Self::new(self.0 / tensor_rank_0)
    }
}

impl<U> DivAssign<TensorRank0> for Quantity<U> {
    fn div_assign(&mut self, tensor_rank_0: TensorRank0) {
        self.0 /= tensor_rank_0
    }
}

impl<U> Mul<Quantity<U>> for TensorRank0 {
    type Output = Quantity<U>;
    fn mul(self, quantity: Quantity<U>) -> Self::Output {
        Quantity::new(self * quantity.0)
    }
}

/// Scaling a bare scalar by a dimensionless quantity leaves it bare.
impl Mul<Quantity<Dimensionless>> for &TensorRank0 {
    type Output = TensorRank0;
    fn mul(self, quantity: Quantity<Dimensionless>) -> Self::Output {
        self * quantity.0
    }
}

impl<U, V> Mul<Quantity<V>> for Quantity<U>
where
    U: UnitMul<V>,
{
    type Output = Quantity<<U as UnitMul<V>>::Output>;
    fn mul(self, quantity: Quantity<V>) -> Self::Output {
        Quantity::new(self.0 * quantity.0)
    }
}

impl<U, V> Mul<Quantity<V>> for &Quantity<U>
where
    U: UnitMul<V>,
{
    type Output = Quantity<<U as UnitMul<V>>::Output>;
    fn mul(self, quantity: Quantity<V>) -> Self::Output {
        Quantity::new(self.0 * quantity.0)
    }
}

impl<U, V> Div<Quantity<V>> for &Quantity<U>
where
    U: UnitDiv<V>,
{
    type Output = Quantity<<U as UnitDiv<V>>::Output>;
    fn div(self, quantity: Quantity<V>) -> Self::Output {
        Quantity::new(self.0 / quantity.value())
    }
}

impl<U, V> Div<Quantity<V>> for Quantity<U>
where
    U: UnitDiv<V>,
{
    type Output = Quantity<<U as UnitDiv<V>>::Output>;
    fn div(self, quantity: Quantity<V>) -> Self::Output {
        Quantity::new(self.0 / quantity.0)
    }
}

impl<U, V> Mul<&Quantity<V>> for Quantity<U>
where
    U: UnitMul<V>,
{
    type Output = Quantity<<U as UnitMul<V>>::Output>;
    fn mul(self, quantity: &Quantity<V>) -> Self::Output {
        self * *quantity
    }
}

impl<U, V> Mul<&Quantity<V>> for &Quantity<U>
where
    U: UnitMul<V>,
{
    type Output = Quantity<<U as UnitMul<V>>::Output>;
    fn mul(self, quantity: &Quantity<V>) -> Self::Output {
        *self * *quantity
    }
}

impl<V> Mul<&Quantity<V>> for TensorRank0 {
    type Output = Quantity<V>;
    fn mul(self, quantity: &Quantity<V>) -> Self::Output {
        Quantity::new(self * quantity.0)
    }
}

impl<V> Mul<&Quantity<V>> for &TensorRank0 {
    type Output = Quantity<V>;
    fn mul(self, quantity: &Quantity<V>) -> Self::Output {
        Quantity::new(self * quantity.0)
    }
}

impl Add<TensorRank0> for Quantity<Dimensionless> {
    type Output = Self;
    fn add(self, tensor_rank_0: TensorRank0) -> Self::Output {
        Self::new(self.0 + tensor_rank_0)
    }
}

impl Add<Quantity<Dimensionless>> for TensorRank0 {
    type Output = Quantity<Dimensionless>;
    fn add(self, quantity: Quantity<Dimensionless>) -> Self::Output {
        Quantity::new(self + quantity.0)
    }
}

impl Sub<TensorRank0> for Quantity<Dimensionless> {
    type Output = Self;
    fn sub(self, tensor_rank_0: TensorRank0) -> Self::Output {
        Self::new(self.0 - tensor_rank_0)
    }
}

impl Sub<Quantity<Dimensionless>> for TensorRank0 {
    type Output = Quantity<Dimensionless>;
    fn sub(self, quantity: Quantity<Dimensionless>) -> Self::Output {
        Quantity::new(self - quantity.0)
    }
}

impl PartialEq<TensorRank0> for Quantity<Dimensionless> {
    fn eq(&self, tensor_rank_0: &TensorRank0) -> bool {
        &self.0 == tensor_rank_0
    }
}

impl PartialEq<Quantity<Dimensionless>> for TensorRank0 {
    fn eq(&self, quantity: &Quantity<Dimensionless>) -> bool {
        self == &quantity.0
    }
}

impl PartialOrd<TensorRank0> for Quantity<Dimensionless> {
    fn partial_cmp(&self, tensor_rank_0: &TensorRank0) -> Option<std::cmp::Ordering> {
        self.0.partial_cmp(tensor_rank_0)
    }
}

impl PartialOrd<Quantity<Dimensionless>> for TensorRank0 {
    fn partial_cmp(&self, quantity: &Quantity<Dimensionless>) -> Option<std::cmp::Ordering> {
        self.partial_cmp(&quantity.0)
    }
}

impl<U> Div<Quantity<U>> for TensorRank0
where
    U: UnitInv,
{
    type Output = Quantity<<U as UnitInv>::Output>;
    fn div(self, quantity: Quantity<U>) -> Self::Output {
        Quantity::new(self / quantity.0)
    }
}

impl<U> std::iter::Sum for Quantity<U> {
    fn sum<I>(iter: I) -> Self
    where
        I: Iterator<Item = Self>,
    {
        Self::new(iter.map(|quantity| quantity.0).sum())
    }
}

impl<'a, U> std::iter::Sum<&'a Quantity<U>> for Quantity<U> {
    fn sum<I>(iter: I) -> Self
    where
        I: Iterator<Item = &'a Quantity<U>>,
    {
        Self::new(iter.map(|quantity| quantity.0).sum())
    }
}

impl<U> Erase for Quantity<U> {
    type Erased = TensorRank0;
    fn erase(&self) -> &Self::Erased {
        &self.0
    }
}

impl<U> Tensor for Quantity<U> {
    type Item = Self;
    type Unit = U;
    fn error_count_zero(&self, tol_abs: TensorRank0, tol_rel: TensorRank0) -> Option<usize> {
        self.0.error_count_zero(tol_abs, tol_rel)
    }
    fn error_count(
        &self,
        other: &Self,
        tol_abs: TensorRank0,
        tol_rel: TensorRank0,
    ) -> Option<usize> {
        self.0.error_count(&other.0, tol_abs, tol_rel)
    }
    fn full_contraction(&self, quantity: &Self) -> TensorRank0 {
        self.0 * quantity.0
    }
    fn is_zero(&self) -> bool {
        self.0 == 0.0
    }
    fn iter(&self) -> impl Iterator<Item = &Self::Item> {
        std::slice::from_ref(self).iter()
    }
    fn iter_mut(&mut self) -> impl Iterator<Item = &mut Self::Item> {
        std::slice::from_mut(self).iter_mut()
    }
    fn len(&self) -> usize {
        1
    }
    fn norm_inf(&self) -> Quantity<U> {
        Self::new(self.0.abs())
    }
    fn norm_l1(&self) -> Quantity<U> {
        Self::new(self.0.abs())
    }
    fn norm_p_sum(&self, p: TensorRank0) -> TensorRank0 {
        self.0.abs().powf(p)
    }
    fn size(&self) -> usize {
        1
    }
    fn sub_abs(&self, other: &Self) -> Self {
        Self::new((self.0 - other.0).abs())
    }
    fn sub_rel(&self, other: &Self) -> Self {
        Self::new(self.0.sub_rel(&other.0))
    }
}

impl<U> TensorArray for Quantity<U> {
    type Array = TensorRank0;
    type Item = Self;
    fn as_array(&self) -> Self::Array {
        self.0
    }
    fn identity() -> Self {
        Self::new(1.0)
    }
    fn zero() -> Self {
        Self::new(0.0)
    }
}

impl<U> FiniteDifference for Quantity<U> {
    fn error_fd(&self, comparator: &Self, epsilon: TensorRank0) -> Option<(bool, usize)> {
        self.0.error_fd(&comparator.0, epsilon)
    }
}

impl<U, const N: usize> FiniteDifference for TensorList<Quantity<U>, N> {
    fn error_fd(&self, comparator: &Self, epsilon: TensorRank0) -> Option<(bool, usize)> {
        error_fd_over(self.iter().zip(comparator.iter()), epsilon)
    }
}

impl<U, const M: usize, const N: usize> FiniteDifference
    for TensorList<TensorList<Quantity<U>, N>, M>
{
    fn error_fd(&self, comparator: &Self, epsilon: TensorRank0) -> Option<(bool, usize)> {
        error_fd_over(
            self.iter()
                .zip(comparator.iter())
                .flat_map(|(entry, comparator_entry)| entry.iter().zip(comparator_entry.iter())),
            epsilon,
        )
    }
}

fn error_fd_over<'a, U: 'a>(
    entries: impl Iterator<Item = (&'a Quantity<U>, &'a Quantity<U>)>,
    epsilon: TensorRank0,
) -> Option<(bool, usize)> {
    let error_count = entries
        .filter_map(|(entry, comparator_entry)| entry.error_fd(comparator_entry, epsilon))
        .map(|(_, count)| count)
        .sum();
    if error_count > 0 {
        Some((true, error_count))
    } else {
        None
    }
}

impl<U> Solution for Quantity<U> {
    fn decrement_from(&mut self, _other: &Vector) {
        unimplemented!()
    }
    fn decrement_from_chained(&mut self, _other: &mut Vector, _vector: &Vector) {
        unimplemented!()
    }
}

impl<U> Hessian for Quantity<U> {
    fn quadratic_form(&self, vector: &Vector) -> TensorRank0 {
        self.0 * vector[0] * vector[0]
    }
    fn entry(&self, _row: usize, _column: usize) -> TensorRank0 {
        unimplemented!()
    }
    fn fill_into(self, _square_matrix: &mut SquareMatrix) {
        unimplemented!()
    }
}

impl<U> Jacobian for Quantity<U> {
    fn fill_into(&self, _vector: &mut Vector) {
        unimplemented!()
    }
    fn fill_into_chained(self, _other: Vector, _vector: &mut Vector) {
        unimplemented!()
    }
}

impl<U> Sub<Vector> for Quantity<U> {
    type Output = Self;
    fn sub(self, _vector: Vector) -> Self::Output {
        unimplemented!()
    }
}

impl<U> Sub<&Vector> for Quantity<U> {
    type Output = Self;
    fn sub(self, _vector: &Vector) -> Self::Output {
        unimplemented!()
    }
}

impl<U> From<Vector> for Quantity<U> {
    fn from(_vector: Vector) -> Self {
        unimplemented!()
    }
}

impl<U, T> Differentiate<T> for Quantity<U>
where
    U: UnitDiv<T>,
{
    type Derivative = Quantity<<U as UnitDiv<T>>::Output>;
}