当前位置: 首页 > news >正文

【学Rust写CAD】10 加法器

源码

// src/matrix/adder.rs

/*
 * 说明:连加计算中,为提高运行期效率,用该结构增加一个Const变量,方便单独合并所有Const类型,最后一步才有可能出现Const与Val的加法计算
 */

 use std::ops::Add;

 use super::constant::{Constant, NotZeroConstant};
 use super::variable::{Variable, FloatType};
 use super::merge::Merge;
 
 /*
  * Adder 结构体:用于合并 Constant 和 Variable
  */
 #[derive(Debug, Clone, Copy, PartialEq)]
 pub struct Adder<const N: i32, T: FloatType>(pub Constant<N>, pub Variable<T>)
 where
     Constant<N>: NotZeroConstant;
 
 // Merge 实现
 impl<const N: i32, T: FloatType> Merge for Adder<N, T>
 where
     Constant<N>: NotZeroConstant,
     T: From<i32> + Add<Output = T>,
 {
     type Output = Variable<T>;
 
     #[inline]
     fn merge(self) -> Self::Output {
         Variable(T::from(N) + self.1.0)
     }
 }
 
 /*
  * 加法重载
  * 实现 Adder + (Const 或 Val)
  */
 // Adder + Const → Adder()
 impl<const N: i32, T: FloatType, const B: i32> Add<Constant<B>> for Adder<N, T>
 where
     Constant<N>: NotZeroConstant,
     Constant<{ N + B }>: Add<Variable<T>>,
 {
     type Output = <Constant<{ N + B }> as Add<Variable<T>>>::Output;
 
     #[inline]
     fn add(self, _b: Constant<B>) -> Self::Output {
         Constant::<{ N + B }> + self.1 // N+B分0、非0 2种情况,交给Const类处理
     }
 }
 
 // Adder + Variable 实现(假设 T = B)
 impl<const N: i32, T: FloatType> Add<Variable<T>> for Adder<N, T>
 where
     Constant<N>: NotZeroConstant,
     T: Add<T, Output = T>,
     Variable<T>: Add<Variable<T>, Output = Variable<T>>,
 {
     type Output = Self;
 
     #[inline]
     fn add(self, b: Variable<T>) -> Self::Output {
         Adder(self.0, self.1 + b)
     }
 }

相关文章:

  • C++ Primer 动态数组
  • React面试葵花宝典之三
  • Gpt翻译完整版
  • CentOS 7使用RPM安装MySQL
  • 区块链-未来世界的网络形态?
  • 蓝桥杯备考:记忆化搜索之function
  • 通义万相2.1:开启视频生成新时代
  • 若依ruoyi响应json参数
  • LeetCode 21. 合并两个有序链表(Python)
  • 阿里万相,正式开源
  • SLAM文献之-DROID-SLAM: Deep Visual SLAM for Monocular, Stereo, and RGB-D Cameras
  • Android中的ANR(Application Not Responding)现象
  • 计算机网络进化史:从烽火台到元宇宙的信息高速公路
  • Android中的AtomicLong:原理、使用与实战指南
  • SpringBoot 整合 Vault - 解决敏感配置信息的动态加密与权限管控问题
  • 一些有用的电子书
  • 四十二:VSCODE打开新文件覆盖上一个文件窗口问题
  • 特征工程如何让KNN准确率提升20%?标准化、筛选与编码全解析
  • 动规【力扣】72. 编辑距离
  • 【Python运维】用Python自动化AWS资源管理:利用boto3实现高效管理S3桶和EC2实例
  • 自己做的网站 怎么放大文件/30个免费货源网站
  • 做网站还需要续费/广州百度seo优化排名
  • 长沙环路建设开发有限公司网站/北京网络营销咨询公司
  • ppt模板怎么做/seo教程书籍
  • app平台运营模式/网站优化关键词
  • 医疗网站前置审批/网络营销服务的特点