unitrix/
variable.rs

1/*
2 * 变量结构体 Var
3 * 该结构体泛型参数 T 需满足 Numeric 约束
4 */
5
6use core::ops::{Neg, Add, Sub, Mul, Div, AddAssign, SubAssign, MulAssign};
7use crate::constant::{N1, P1, B0, B1, Integer, NonZero};
8/// 定义 Numeric trait,约束 T 必须实现基本数值运算
9/// 包括:
10/// - 一元负号运算 (Neg)
11/// - 加减乘除运算 (Add, Sub, Mul, Div)
12/// - 复合赋值运算 (AddAssign, SubAssign)
13/// - 从i32转换 (From<i32>)
14/// - 复制语义 (Copy, Clone)
15/// - 默认值 (Default)
16/// - 静态生命周期 ('static)
17pub trait Numeric:
18    Neg<Output = Self> +
19    Add<Output = Self> +
20    Sub<Output = Self> +
21    Mul<Output = Self> +
22    Div<Output = Self> +
23    AddAssign +
24    SubAssign +
25    From<i32> +
26    Copy +
27    Clone +
28    Default +
29    Sized +
30    'static
31{}
32
33// 为基本类型实现 Numeric trait
34impl Numeric for i64 {} // i64 类型实现 Numeric
35impl Numeric for f64 {} // f64 类型实现 Numeric
36
37/// 定义 Scalar trait,用于物理量取值
38/// 在 Numeric 基础上增加了 MulAssign 约束
39pub trait Scalar:
40    Neg<Output = Self> +
41    Add<Output = Self> +
42    Sub<Output = Self> +
43    Mul<Output = Self> +
44    Div<Output = Self> +
45    AddAssign +
46    SubAssign +
47    MulAssign +
48    From<i32> +
49    Copy +
50    Clone +
51    Default +
52    Sized +
53    'static
54{}
55
56// 为 Var 类型实现 Scalar trait
57impl Scalar for Var<i64> {} // Var<i64> 实现 Scalar
58impl Scalar for Var<f64> {} // Var<f64> 实现 Scalar
59
60/// 变量结构体,封装一个泛型值 T
61/// T 需要满足 Numeric 约束
62#[derive(Debug, Clone, Copy, PartialEq, Default)]
63pub struct Var<T: Numeric>(pub T);
64
65// 运算符重载实现
66// =============================================
67
68/// 实现 Var 的取反运算
69/// 用法: -V
70impl<T: Numeric> Neg for Var<T> {
71    type Output = Self;
72
73    fn neg(self) -> Self::Output {
74        Var(-self.0)   // 对内部值取反后包装为新 Var
75    }
76}
77
78/// 实现 Var 与 Var 的加法运算
79/// 用法: V + V
80impl<T: Numeric> Add for Var<T> {
81    type Output = Self;
82
83    fn add(self, b: Self) -> Self::Output {
84        Var(self.0 + b.0) // 内部值相加后包装为新 Var
85    }
86}
87
88/// 实现 Var 与 Var 的减法运算
89/// 用法: V - V
90impl<T: Numeric> Sub for Var<T> {
91    type Output = Self;
92
93    fn sub(self, b: Self) -> Self::Output {
94        Var(self.0 - b.0) // 内部值相减后包装为新 Var
95    }
96}
97
98/// 实现 Var 与 Var 的乘法运算
99/// 用法: V * V
100impl<T: Numeric> Mul<Var<T>> for Var<T> {
101    type Output = Self;
102
103    fn mul(self, b: Self) -> Self::Output {
104        Var(self.0 * b.0) // 内部值相乘后包装为新 Var
105    }
106}
107
108/// 实现 Var 与 Var 的除法运算
109/// 用法:V / V
110impl<T: Numeric> Div<Var<T>> for Var<T> {
111    type Output = Self;
112
113    fn div(self, b: Self) -> Self::Output {
114        Var(self.0 / b.0) // 内部值相除后包装为新 Var
115    }
116}
117
118/// 实现 Var 的加法赋值运算
119/// 用法: V += V
120impl<T: Numeric> AddAssign for Var<T> {
121    fn add_assign(&mut self, rhs: Self) {
122        self.0 += rhs.0;  // 直接转发到底层类型的 += 运算
123    }
124}
125
126/// 实现 Var 的减法赋值运算
127/// 用法: V -= V
128impl<T: Numeric> SubAssign for Var<T> {
129    fn sub_assign(&mut self, rhs: Self) {
130        self.0 -= rhs.0;  // 直接转发到底层类型的 -= 运算
131    }
132}
133
134// 与常量运算实现
135// =============================================
136
137/// 实现 Var 与常量的加法运算
138/// 用法: V + C
139impl<T: Numeric, C: Integer + Add<Var<T>>> Add<C> for Var<T> {
140    type Output = <C as Add<Var<T>>>::Output;
141
142    fn add(self, c:C) -> Self::Output {
143        c + self // 交换加法顺序,调用常量的 + 运算符
144    }
145}
146
147/// 实现 Var 与常量的减法运算
148/// 用法: V - C
149impl<T: Numeric, C: Integer + Neg> Sub<C> for Var<T>
150where 
151    <C as Neg>::Output: Add<Var<T>>,
152{
153    type Output = < C::Output as Add<Var<T>> >::Output;
154
155    fn sub(self, c: C) -> Self::Output {
156        -c+self // 转换为 -c + V 的形式
157    }
158}
159
160/// 实现 Var 与常量的乘法运算
161/// 用法: V * C
162impl<T: Numeric, C:Integer + Mul<Var<T>>> Mul<C> for Var<T> {
163    type Output = <C as Mul<Var<T>>>::Output;
164
165    fn mul(self, c: C) -> Self::Output {
166        c * self // 交换乘法顺序,调用常量的 * 运算符
167    }
168}
169
170/// 实现 Var 与常量的除法运算(部分实现)
171
172/// V / 0 未实现
173
174/// V / 1 = V
175impl<T: Numeric> Div<P1> for Var<T> {
176    type Output = Self;
177
178    fn div(self, _rhs: P1) -> Self::Output {
179        self
180    }
181}
182
183/// V / -1 = -V
184impl<T: Numeric> Div<N1> for Var<T> {
185    type Output = Self;
186
187    fn div(self, _rhs: N1) -> Self::Output {
188        -self
189    }
190}
191
192/// V / B0
193impl<H: NonZero, T:Numeric> Div<B0<H>> for Var<T>
194where 
195    B0<H>: Integer,
196    Var<T>: Div<Var<T>,Output = Var<T>>,
197{
198    type Output = Var<T>;
199    #[inline(always)]
200    fn div(self, _rhs: B0<H>) -> Self::Output {
201        Var(self.0 / T::from(B0::<H>::to_i32()))
202    }
203}
204
205/// V / B1
206impl<H: NonZero, T:Numeric> Div<B1<H>> for Var<T>
207where 
208    B1<H>: Integer,
209    Var<T>: Div<Var<T>,Output = Var<T>>,
210{
211    type Output = Var<T>;
212    #[inline(always)]
213    fn div(self, _rhs: B1<H>) -> Self::Output {
214        Var(self.0 / T::from(B1::<H>::to_i32()))
215    }
216}
217
218// 类型转换实现
219// =============================================
220
221/// 实现从 i32 到 Var<i64> 的转换
222impl From<i32> for Var<i64> {
223    fn from(value: i32) -> Self {
224        Var(value as i64)  // 将 i32 转换为 i64 后包装
225    }
226}
227
228/// 实现从 i32 到 Var<f64> 的转换
229impl From<i32> for Var<f64> {
230    fn from(value: i32) -> Self {
231        Var(value as f64)  // 将 i32 转换为 f64 后包装
232    }
233}
234
235/// 实现从 i64 到 Var<i64> 的转换
236impl From<i64> for Var<i64> {
237    fn from(value: i64) -> Self {
238        Var(value)   // 直接包装 i64 值
239    }
240}
241
242/// 实现从 f64 到 Var<f64> 的转换
243impl From<f64> for Var<f64> {
244    fn from(value: f64) -> Self {
245        Var(value) // 直接包装 f64 值
246    }
247}
248
249/// 实现 Var 的乘法赋值运算
250/// 用法: V *= V
251impl<T: Numeric + MulAssign> MulAssign for Var<T> {
252    fn mul_assign(&mut self, rhs: Self) {
253        self.0 *= rhs.0; // 直接转发到底层类型的 *= 运算
254    }
255}
256
257/// 实现 Var 与底层类型的乘法赋值运算
258/// 用法: V *= T
259impl<T: Numeric + MulAssign> MulAssign<T> for Var<T> {
260    fn mul_assign(&mut self, rhs: T) {
261        self.0 *= rhs; // 直接使用底层类型的 *= 运算
262    }
263}