这是一个矩阵运算的库,能够实现一些简单的矩阵预算的功能
创建一个矩阵
create a new matrix
let m1 = new;
println!;
生成一个全0/全1矩阵 create a new matrix with all 0
let m1 = zeros;
println!;
let m1 = ones;
println!;
生成一个随机矩阵(只有f64类型) create a new matrix with random values
let m1 = random;
println!;
获取值以及设置值
get value & set value
let m1 = new;
let value = m1.get;
if let Some = value
m1.set;
let value = m1.get;
if let Some = value
矩阵加减乘的运算
Matrix addition, subtraction, and multiplication operations
// 加法运算 add
let m1 = new;
let m2 = new;
let m3 = m1 + m2;
println!;
//减法运算 sub
let m1 = new;
let m2 = new;
let m3 = m2 - m1;
println!;
//乘法运算 multiple
let m1 = new;
let m2 = new;
let m3 = m2 * m1;
println!;
矩阵乘法
Matrix product
let m1 = new;
let m2 = new;
let result = m1.product.unwrap;
println!;
矩阵的数乘
Scalar multiplication of a matrix
let m1 = new;
let m2 = m1.scale;
println!;
矩阵的操作
矩阵转置 Matrix transpose
let m1 = new;
let m2 = !m1;
println!;
let m2 = m1.T;
println!;
矩阵的行列式
matrix determinant
let m1 = new;
let m2 = m1.det;
println!;
物理单位及运算库
解决一部分物理量运算时的单位换算、物理量转换和量纲对齐的问题。
可以让代码向物理公式对齐,而不需要关心具体的单位转换细节。
Resolves unit conversion, physical quantity transformation, and dimensional alignment in physical calculations.
Allows code to align with physical formulas without worrying about unit conversion details.
单位对齐
以长度单位为例,可以通过from_m() 方法生成一个以米为单位的物理量,然后使用as_km() 方法将其转换为以千米为单位的物理量。
也可以用 as_light_year() 方法将其转换为光年的倍数。
Initialize with from_m(), from_km(), etc.
Convert flexibly via as_[unit]() methods.
目前支持的物理量模块: supported physical quantities:
物理量 | 模块名 |
---|---|
长度 | distance |
速度 | velocity |
加速度 | acceleration |
角度 | angular |
角速度 | angular_velocity |
系数 | coef |
角加速度 | angular_acceleration |
面积 | area |
磁感应强度 | magnetic_induction |
质量 | mass |
角动量 | angular_momentum |
动量 | momentum |
体积 | volume |
以后会慢慢维护,也欢迎大家提issue和pr。 |
物理量的计算
符合物理计算关系的物理量之间可以进行加减乘除运算,得到符合物理意义的物理量。
例如距离除以时间得到速度,速度除以时间得到加速度。
一旦两个物理量的量纲不匹配,就会编译报错。避免代码写错导致的bug。
Physical quantities with compatible dimensions can be safely added, subtracted, multiplied, or divided while preserving physical meaning.
Examples:
Distance ÷ Time → Velocity
Velocity ÷ Time → Acceleration
Compile-Time Safety:
Operations with dimensionally incompatible quantities will trigger compile errors, preventing invalid physics logic at the code level.
向量计算
各种物理量向量之间的转换
//位移向量除以时间变成速度向量
//Displacement vector divided by time yields velocity vector.
let a: = new;
let v = a / from_secs;
println!;
//速度向量乘以质量,变成动量向量
// Velocity vector multiplied by mass yields momentum vector.
let m = v * from_kg;
println!;
// 动量向量,乘以半径向量,变成角动量向量。
// Momentum vector crossed with radius vector yields angular momentum vector.
let am = a * m ;
println!;
各种物理量的向量彼此之间的加减乘除运算,都可以直接通过数学符号实现。
目前支持有限,以后会慢慢补充,欢迎 issue 和 PR。
Vector operations (add/subtract/multiply/divide) for all physical quantities can be directly performed using mathematical symbols.
Current support is limited - more features will be added gradually. Issues and PRs are welcome!
向量的常见方法
向量是否是 0 值
let v = new;
println!;
将向量转换成数组
let v = new;
println!;
通过数组生成向量 & 通过列矩阵生成向量
let v = from_array;
println!;
let v = from_matrix;
println!;
生成反对称矩阵
let v = new;
// 3 * 3 反对称矩阵
println!;
let v = new;
// 4 * 4 反对称矩阵
println!;
向量叉乘
let v1 = new;
let v2 = new;
// 叉乘的垂直向量
let v = v1.cross;
println!;
let v1 = new;
let v2 = new;
//叉乘的垂直单位向量
let v_unit = v1.cross_unit;
println!;
点积
let v1 = new;
let v2 = new;
let r = v1.dot;
println!;
空间几何的一些运算库
欧拉角
转换成四元数
let euler: = new;
let quat = euler.to_quaternion;
println!;
基本的三角函数(待补充完善)
let euler: = new;
let sin = euler.sin;
let cos = euler.cos;
println!;
余弦转换矩阵
获取向量
// 获取单位余弦矩阵
let cos = unit;
let x = cos.get_x_vector;
let y = cos.get_y_vector;
let z = cos.get_z_vector;
// 获取 x y z 行向量
println!;
let x_col = cos.get_col_x_vector;
let y_col = cos.get_col_y_vector;
let z_col = cos.get_col_z_vector;
//获取三个列向量
println!;
转置
let cos = unit;
let cos_t = cos.transfer;
println!;
转换到四元数
let cos = unit;
let q = cos.to_quaternion;
println!;
矩阵乘法
let cos_a = unit;
let cos_b = new;
// 两个余弦转换矩阵相乘
let cos_c = cos_a.product;
// 余弦转换矩阵乘以列向量
cos_c.product_vector;
转换到欧拉角
let cos = unit;
let euler = cos.to_pry;
// xzy 转序
println!;
// xyz 转序
let euler = cos.to_rpy;
println!;
四元数
初始化四元数
let q = new;
println!;
// 单位四元数
let q = default;
println!;
归一化
let q = new;
let q1 = q.normalize;
println!;
模长
let q = new;
println!;
共轭四元数
let q = new;
let q1 = q.conjugate;
println!;
四元数的逆
let q = new;
let q1 = q.inverse;
println!;
转换到余弦转换矩阵
let q = new;
let cos = q.to_cos_matrix;
println!;
执行线性变换
let q = new;
let m = new.unwrap;
let q1 = q.linear_transform;
四元数计算
let q1 = new;
let q2 = new;
let q_sum = q1 + q2;
println!;
let q1 = new;
let q2 = new;
let q_mul = q1 * q2 ;
println!;
let q1 = new;
let q2 = new;
let q_div = q1 / q2 ;
println!;