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
/*
* SPDX-License-Identifier: MIT
* Copyright (c) 2023 - 2026. The DeepCausality Authors and Contributors. All Rights Reserved.
*/
use crateAddGroup;
use crate;
use crate::;
/// An ordered `Field` that is also an analytic real scalar.
///
/// `RealField` is a [`Real`] that is also a [`Field`], and converts back to a primitive: it adds
/// field invertibility (a total multiplicative inverse / division) and `ToPrimitive` on top of the
/// analytic surface (ordering, `sqrt`/`exp`/`ln`/`sin`/…, constants) provided by [`Real`].
///
/// All analytic operations are declared on [`Real`] and inherited here via the
/// supertrait, so every existing `T: RealField` bound resolves the same method set
/// it always did. The split lets analytic-but-non-field types (for example dual
/// numbers, used for automatic differentiation) implement [`Real`] without falsely
/// claiming `Field`/`RealField`.
///
/// This trait abstracts over concrete floating-point types like `f32` and `f64`.
///
/// `ToPrimitive` makes the crossing back to a primitive part of the bound, matching the
/// `FromPrimitive` that [`Scalar`](crate::Scalar) already supplies in the other direction.
/// Code bounded on `RealField` converts a scalar to `f64` or to an integer without restating
/// the bound. The obligation costs nothing: the blanket implementation below requires `Float`,
/// and `Float: NumCast: ToPrimitive`, so every implementor already satisfies it.
// A number is an Abelian group under addition exactly when it has additive inverses, and
// `Neg` is the witness for those. That bound is load-bearing rather than incidental: the
// unsigned integers are a `Num` and do satisfy the commutative, associative, and distributive
// laws, but `3u64 - 5u64` has no value in `u64`, so ℕ is a commutative *monoid* under
// addition and never a group. `Neg` is exactly the property separating ℤ from ℕ, so requiring
// it here keeps the unsigned types out of `AbelianGroup`, and therefore out of `Ring`,
// `CommutativeRing`, and `Field` — none of which they satisfy.
//
// `Num` is kept in the bound, rather than the weaker `AddGroup`, because it is what makes this
// blanket disjoint from the concrete impls for `Complex<T>`, `Dual<T>`, `Quaternion<T>`, and
// the tensor types: none of those implement `Num`. `AddGroup` alone would overlap them, and it
// would not exclude the unsigned types either, since its inverse axiom rests on `Sub` merely
// existing.
// Every `Float` that carries the law markers is a `RealField`. `Invertible` is what separates ℝ
// from ℤ here: it promises that `/` really inverts, which integer division does not.