【PhysUnits】16.2 引入变量后的乘法实现(mul.rs)

一、源码

这段代码实现了各种基本类型之间的乘法运算,包括与Var类型的乘法。

use core::ops::{Add, Mul, Neg};
use super::basic::{Z0, P1, N1, B0, B1, Integer, NonZero};
use crate::variable::{Var,Numeric};

// ========== Basic Type Multiplication ==========
// ========== 基本类型乘法 ==========

// ========== 0 * All ==========
// ========== 零乘以任何数 ==========
impl<I: Integer> Mul<I> for Z0 {
    type Output = Self;
    #[inline(always)]
    fn mul(self, _rhs: I) -> Self::Output {
        self  // 0 * any = 0
    }
}

// ========== 1 * All ==========
// ========== 一乘以任何数 ==========
impl<I: Integer> Mul<I> for P1 {
    type Output = I;
    #[inline(always)]
    fn mul(self, rhs: I) -> Self::Output {
        rhs  // 1 * x = x
    }
}

// ========== -1 * All ==========
// ========== 负一乘以任何数 ==========
impl<I: Integer + Neg> Mul<I> for N1 {
    type Output = I::Output;
    #[inline(always)]
    fn mul(self, rhs: I) -> Self::Output {
        -rhs  // -1 * x = -x
    }
}

// ========== B0 * All ==========
// ========== 以0结尾的二进制数乘法 ==========

// B0 * Z0 = 0
// 以0结尾的数乘以零
impl<H: NonZero> Mul<Z0> for B0<H> {
    type Output = Z0;
    #[inline(always)]
    fn mul(self, _rhs: Z0) -> Self::Output {
        Z0  // x * 0 = 0
    }
}

// B0 * NonZero = B0
// 以0结尾的数乘以非零数
//
// Explanation:
//    B0

* I = (2*P)*I = 2*(P*I) = B0

// B0 * I = -B0<-N> * I = -B0<(-N)*I> = B0 // Therefore, B0 * I = B0 // // 说明: // B0

* I = (2*P)*I = 2*(P*I) = B0

// B0 * I = -B0<-N> * I = -B0<(-N)*I> = B0 // 因此,B0 * I = B0 impl<H: NonZero + Mul<I>, I: NonZero> Mul<I> for B0<H> { type Output = B0<H::Output>; #[inline(always)] fn mul(self, _rhs: I) -> Self::Output { B0::new() // 构造新的B0类型 } } // ========== B1 * All ========== // ========== 以1结尾的二进制数乘法 ========== // B1 * Z0 = 0 // 以1结尾的数乘以零 impl<H: NonZero> Mul<Z0> for B1<H> { type Output = Z0; #[inline(always)] fn mul(self, _rhs: Z0) -> Self::Output { Z0 // x * 0 = 0 } } // B1 * NonZero = I + B0 // 以1结尾的数乘以非零数 // // Explanation: // B1

* I = (1 + B0

) * I = I + B0

// B1 * I = -B1 * I = -I * ((2*!N)+1) // = -I * ((-2*(N+1))+1) = -I * ((-2*N)-1) // = I * ((2*N)+1) = I + B0 // Therefore, B1 * I = I + B0 // // 说明: // B1

* I = (1 + B0

) * I = I + B0

// B1 * I = -B1 * I = -I * ((2*!N)+1) // = -I * ((-2*(N+1))+1) = -I * ((-2*N)-1) // = I * ((2*N)+1) = I + B0 // 因此,B1 * I = I + B0 impl<H: NonZero + Mul<I>, I: NonZero + Add<B0<<H as Mul<I>>::Output>>> Mul<I> for B1<H> { type Output = I::Output; #[inline(always)] fn mul(self, i: I) -> Self::Output { i + B0::new() // I + B0 } } /// Type alias for multiplication: `Prod = >::Output` /// 乘法运算的类型别名:`Prod = >::Output` pub type Prod<A, B> = <A as Mul<B>>::Output; // ========== 与Var乘法(新增加) ========== // ========== 0 * Var ========== impl<T:Numeric> Mul<Var<T>> for Z0 { type Output = Self; #[inline(always)] fn mul(self, _rhs: Var<T>) -> Self::Output { self // 0 * any = 0 } } // ========== 1 * Var ========== impl<T: Numeric> Mul<Var<T>> for P1 { type Output = Var<T>; #[inline(always)] fn mul(self, rhs: Var<T>) -> Self::Output { rhs // 1 * x = x } } // ========== -1 * Var ========== impl<T: Numeric> Mul<Var<T>> for N1 where Var<T>: Neg, { type Output = <Var<T> as Neg>::Output; #[inline(always)] fn mul(self, rhs: Var<T>) -> Self::Output { -rhs // -1 * x = -x } } // ========== B0 * Var ========== impl<H: NonZero, T:Numeric> Mul<Var<T>> for B0<H> where B0<H>: Integer, Var<T>: Mul<Var<T>>, { type Output = Var<T>; #[inline(always)] fn mul(self, rhs: Var<T>) -> Self::Output { Var(rhs.0 * T::from(self.to_i32())) } } // ========== B1 * Var ========== impl<H: NonZero, T:Numeric> Mul<Var<T>> for B1<H> where B1<H>: Integer, Var<T>: Mul<Var<T>>, { type Output = Var<T>; #[inline(always)] fn mul(self, rhs: Var<T>) -> Self::Output { Var(rhs.0 * T::from(self.to_i32())) } } #[cfg(test)] mod tests { use super::*; #[test] fn test_basic_multiplication() { // Test Z0 (0 * anything = 0) // 测试零的乘法 let _: Z0 = Z0 * Z0; let _: Z0 = Z0 * P1; let _: Z0 = Z0 * N1; // Test P1 (1 * anything = anything) // 测试正一的乘法 let _: Z0 = P1 * Z0; let _: P1 = P1 * P1; let _: N1 = P1 * N1; // Test N1 (-1 * anything = -anything) // 测试负一的乘法 let _: Z0 = N1 * Z0; let _: N1 = N1 * P1; let _: P1 = N1 * N1; } #[test] fn test_b0_multiplication() { // B0 represents binary 10 (decimal 2) // B0 表示二进制10(十进制2) let b0_p1: B0<P1> = B0::new(); // 2 * 0 = 0 let _: Z0 = b0_p1 * Z0; // 2 * 1 = 2 (B0) let _: B0<P1> = b0_p1 * P1; // 2 * (-1) = -2 (B0) let _: B0<N1> = b0_p1 * N1; } #[test] fn test_b1_multiplication() { // B1 represents binary 11 (decimal 3) // B1 表示二进制11(十进制3) let b1_p1: B1<P1> = B1::new(); // 3 * 0 = 0 let _: Z0 = b1_p1 * Z0; // 3 * 1 = 3 (B1) // Note: This requires addition to be properly implemented // 注意:这需要加法正确实现 // let _: B1 = b1_p1 * P1; // 3 * (-1) = -3 (B1) // let _: B1 = b1_p1 * N1; } // Helper function to create values // 辅助函数创建值 fn _create_values() { let _z0 = Z0; let _p1 = P1; let _n1 = N1; let _b0_p1: B0<P1> = B0::new(); let _b1_p1: B1<P1> = B1::new(); } }

二、基本类型乘法

零乘以任何数 (Z0 * I)
impl<I: Integer> Mul<I> for Z0 {
    type Output = Self;
    fn mul(self, _rhs: I) -> Self::Output {
        self  // 0 * any = 0
    }
}
  • 零乘以任何整数类型都返回零

  • 适用于所有实现Integer trait的类型

一乘以任何数 (P1 * I)
impl<I: Integer> Mul<I> for P1 {
    type Output = I;
    fn mul(self, rhs: I) -> Self::Output {
        rhs  // 1 * x = x
    }
}
  • 一乘以任何数返回该数本身

  • 输出类型与被乘数类型相同

负一乘以任何数 (N1 * I)
impl<I: Integer + Neg> Mul<I> for N1 {
    type Output = I::Output;
    fn mul(self, rhs: I) -> Self::Output {
        -rhs  // -1 * x = -x
    }
}
  • 负一乘以任何数返回该数的相反数

  • 要求被乘数类型实现Neg trait

三、二进制数乘法

以0结尾的二进制数 (B0)
B0 * Z0
impl<H: NonZero> Mul<Z0> for B0<H> {
    type Output = Z0;
    fn mul(self, _rhs: Z0) -> Self::Output {
        Z0  // x * 0 = 0
    }
}
  • 任何数乘以零都返回零
B0 * NonZero
impl<H: NonZero + Mul<I>, I: NonZero> Mul<I> for B0<H> {
    type Output = B0<H::Output>;
    fn mul(self, _rhs: I) -> Self::Output {
        B0::new()  // B0
    }
}
  • 表示二进制数乘以非零数的运算

  • 数学原理:B0

    * I = 2PI = B0

  • 返回一个新的B0类型,其内部类型是H * I的结果

以1结尾的二进制数 (B1)
B1 * Z0
impl<H: NonZero> Mul<Z0> for B1<H> {
    type Output = Z0;
    fn mul(self, _rhs: Z0) -> Self::Output {
        Z0  // x * 0 = 0
    }
}

任何数乘以零都返回零

B1 * NonZero
impl<H: NonZero + Mul<I>, I: NonZero + Add<B0<<H as Mul<I>>::Output>>> Mul<I> for B1<H> {
    type Output = I::Output;
    fn mul(self, i: I) -> Self::Output {
        i + B0::new()  // I + B0
    }
}
  • 表示二进制数乘以非零数的运算

  • 数学原理:B1

    * I = (2P + 1)I = I + 2PI = I + B0

返回I + B0的结果

四、与 Var 的乘法

基本类型与 Var 的乘法

实现了Z0、P1、N1、B0和B1与Var的乘法运算:

impl<T:Numeric> Mul<Var<T>> for Z0 {
    type Output = Self;
    fn mul(self, _rhs: Var<T>) -> Self::Output {
        self  // 0 * Var = 0
    }
}

impl<T: Numeric> Mul<Var<T>> for P1 {
    type Output = Var<T>;
    fn mul(self, rhs: Var<T>) -> Self::Output {
        rhs  // 1 * Var = Var
    }
}

impl<T: Numeric> Mul<Var<T>> for N1 {
    type Output = <Var<T> as Neg>::Output;
    fn mul(self, rhs: Var<T>) -> Self::Output {
        -rhs  // -1 * Var = -Var
    }
}

impl<H: NonZero, T:Numeric> Mul<Var<T>> for B0<H> {
    type Output = Var<T>;
    fn mul(self, rhs: Var<T>) -> Self::Output {
        Var(rhs.0 * T::from(self.to_i32()))
    }
}

impl<H: NonZero, T:Numeric> Mul<Var<T>> for B1<H> {
    type Output = Var<T>;
    fn mul(self, rhs: Var<T>) -> Self::Output {
        Var(rhs.0 * T::from(self.to_i32()))
    }
}

五、类型别名

pub type Prod<A, B> = <A as Mul<B>>::Output;

定义了一个类型别名Prod,表示A * B的结果类型

六、测试代码

包含了各种乘法运算的测试用例,验证实现的正确性。

七、总结

这段代码实现了一个类型级别的乘法系统:

  1. 支持基本整数类型(Z0, P1, N1)的乘法

  2. 支持二进制表示的数(B0, B1)的乘法

  3. 支持与Var类型的乘法

  4. 通过Rust的类型系统在编译期完成乘法运算的类型计算

这种实现常用于类型级编程,可以在编译期进行数值计算和类型检查。

你可能感兴趣的:(我的计量单位库quantity,rust)