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

【PhysUnits】17.7 readme.md更新

physunits · 物理单位库

Type-safe physical quantities with dimensional analysis
带量纲分析的类型安全物理量库

A Rust library for safe unit operations /
Rust实现的类型安全单位计算库

Key Advantages / 核心优势

  1. No dependencies - Pure Rust implementation without external dependencies
    无依赖库 - 纯Rust实现,不依赖任何外部库

  2. Typenum-like constant calculation - Full compile-time dimensional analysis with constant evaluation capabilities
    类Typenum的常量计算 - 完整的编译期量纲分析与常量计算能力

  3. Var structure bridge - Seamless integration between variable and constant calculations
    Var结构桥接 - 完美衔接变量与常量的混合计算

  4. Complete operator overloading - Supports all arithmetic operations with dimensional correctness
    完整运算符重载 - 支持所有算术运算并保持量纲正确性

Core Architecture / 核心架构

1. Constant System / 常量系统

// 基础常量类型
pub struct B0<H>(PhantomData<H>);  // 二进制0节点
pub struct B1<H>(PhantomData<H>);  // 二进制1节点
pub struct Z0;                     // 零值
pub struct P1;                     // +1
pub struct N1;                     // -1// 常量运算特性
pub trait Integer: Default + Sealed + Copy {fn to_i32() -> i32;  // 常量值转换
}// 示例实现
impl Integer for P1 {fn to_i32() -> i32 { 1 }
}### 2. Var Structure / 变量结构
```rust
/// 变量结构体,桥接常量与变量计算
#[derive(Debug, Clone, Copy)]
pub struct Var<T: Numeric>(pub T);// 支持的运算类型
impl<T: Numeric> Add for Var<T> {type Output = Self;fn add(self, b: Self) -> Self {Var(self.0 + b.0)}
}// 与常量的混合运算
impl<T: Numeric, C: Integer> Add<C> for Var<T> {type Output = C::Output;fn add(self, c: C) -> Self::Output {c + self  // 调用常量的加法实现}
}

3. Dimension System / 量纲系统

/// 国际单位制7大量纲
pub struct Dimension<METER: Integer,     // 长度KILOGRAM: Integer,  // 质量SECOND: Integer,    // 时间AMPERE: Integer,    // 电流KELVIN: Integer,    // 温度MOLE: Integer,      // 物质的量CANDELA: Integer    // 发光强度
>(PhantomData<(METER, KILOGRAM, SECOND, AMPERE, KELVIN, MOLE, CANDELA)>);// 量纲运算示例
impl<M1, M2, KG1, KG2> Mul<Dimension<M2, KG2>> for Dimension<M1, KG1> {type Output = Dimension<Sum<M1, M2>, Sum<KG1, KG2>>;fn mul(self, _: Dimension<M2, KG2>) -> Self::Output {Dimension::new()  // 量纲指数相加}
}

4. Unit System / 单位系统

/// SI基础单位结构
pub struct Si<Value: Scalar, D: Dimensional, Pr: Prefixed>(pub Value,PhantomData<(D, Pr)>
);/// 复合单位结构
pub struct Unit<S: Sied, R: Scaled>(pub S, PhantomData<R>);// 单位运算示例
impl<V, D1, D2> Mul<Si<V, D2>> for Si<V, D1> {type Output = Si<V, Prod<D1, D2>>;fn mul(self, rhs: Si<V, D2>) -> Self::Output {Si(self.0 * rhs.0, PhantomData)}
}

Features / 特性

Feature功能描述
📏 Compile-time dimensional safety编译期量纲安全
⚡ Zero runtime overhead零运行时开销
🔢 Integer & float support支持整数和浮点数
🔄 Automatic unit conversion自动单位转换
🏷️ Runtime prefix handling运行时词头处理
🧮 Typenum-like constant math类Typenum的常量计算
🌉 Var-based mixed calculation基于Var的混合计算
🔧 Full operator overloading完整运算符重载

Usage Examples / 使用示例

Basic Operations / 基础运算

use physunits::{m, kg, s, N};// 常量计算
let force = const5* kg * const(9) * m / (s * s);
println!("Force: {} N", force.value());// 变量计算
let mass = Var(5.0) * N;
let acceleration = Var(9.0);
let force = mass * acceleration;

Mixed Calculation / 混合计算

use physunits::{consts::*, Var};// 编译时常量与运行时变量混合运算
let G = Const(6) * m3 / (kg * s * s);
let m1 = Var(5.972e24);  // 地球质量 (kg)
let m2 = Var(7.342e22);  // 月球质量 (kg)
let r = Var(3.844e8);    // 地月距离 (m)let f = G * m1 * m2 / (r * r);
println!("Gravitational force: {} N", f.value());

Temperature Conversion / 温度转换

use physunits::{Celsius, Fahrenheit};let boiling = quantity::Si::<f64, Celsius>::new(100.0);
let fahr = boiling.convert::<Fahrenheit>();
println!("Water boils at {} °F", fahr.value()); 

Unit Math / 单位运算

let speed = 60.0 * km / h;
let time = 30.0 * min;
let distance = speed * time;  // 自动推导为km单位

Advanced Features / 高级特性

  1. 常量计算系统
  • 二进制编码的常量类型 (B0, B1)

  • 基础常量值 (Z0, P1, N1)

  • 支持所有算术运算的trait实现

  • 常量到运行时的值转换 (to_i32())

  1. Var结构桥接
  • 同时支持变量与常量的运算

  • 自动类型转换系统

  • 完整的运算符重载 (+, -, *, /, +=, -=, *=, /=)

  • 支持i64和f64基础类型

  1. 量纲系统
  • 7大基本量纲的编译期检查

  • 量纲运算自动推导

  • 支持幂运算 (pow()方法)

  • 零开销抽象

Comparison / 对比

FeaturePhysUnitsuomnalgebra
Dim Safety
Integer Support⚠️
Runtime Prefix
No Deps
Const Math⚠️
Var Bridge
特性PhysUnitsuomnalgebra
量纲安全
整数支持⚠️
运行时词头
无依赖
常量计算⚠️
变量桥接

Installation / 安装

[dependencies]
physunits = "0.0.4"

Contributing / 贡献指南

We welcome issues and PRs! / 欢迎提交 Issue 和 PR!

Key needs: / 重点需求:

  • More unit definitions / 更多单位定义

  • Real-world physics test cases / 实际物理测试案例

  • Better error messages / 更好的错误提示

  • Constant calculation optimization / 常量计算优化

  • WASM compatibility / WASM兼容性

相关文章:

  • 仓库物资出入库管理系统源码+uniapp小程序
  • 大模型笔记_检索增强生成(RAG)
  • 在 Azure 机器学习中注册 MLflow 模型
  • 汉诺塔 (easy)
  • Spark提交流程
  • JAVA毕业设计227—基于SpringBoot+hadoop+spark+Vue的大数据房屋维修系统(源代码+数据库)
  • PHP:互联网时代的常青编程语言
  • C++ 中的 iostream 库:cin/cout 基本用法
  • 【JavaEE】-- HTTPS
  • windows电脑解决笔记本搜索不到wifi问题
  • 侃侃AI编程
  • javaee初阶-多线程
  • 电机专用32位MCU PY32MD310,QFN32封装,内置多功能栅极驱动器
  • 曼昆《经济学原理》第九版 第十六章垄断竞争
  • 免部署的数字人 API 调用教程:基于 wav2lip模型训练的开放API,附 PHP 代码示例
  • 大模型与人工智能
  • React Vue 编译/运行流程
  • 11. 线性表的顺序表示和实现(3)
  • 新闻类鸿蒙应用全链路测试实践:性能、兼容性与体验的深度优化
  • 【多模态/T5】[特殊字符] 为什么视频生成模型还在用T5?聊聊模型选择的学问
  • app建设网站公司/百度广告怎么做
  • 网站的分页做不好会影响主页/湖南正规seo公司
  • 织梦cms瀑布流极品美女图片网站源码/搜索大全
  • 做物流网站模块/今日新闻7月1日
  • 河南安阳网站建设/上海服务政策调整
  • 郑州做网站公司/百度搜索网址