linear-algebra
本手册记录 Luna-Flow/linear-algebra 的 0.5.1 版本。
概览
Luna-Flow/linear-algebra 是 Luna Flow 的线性代数层。它以两种执行模型——面向值的(immut)和原地的(mutable)——提供稠密矩阵和向量,为浮点矩阵提供数值例程,为整数矩阵提供精确算法,并提供小型能力层(algebra、container),让泛型算法可以运行在任何实现了它们的矩阵或向量类型上。
0.5.1 版本围绕三项变化:
- 本模块使用 MoonBit 0.10 工具链及其
moon.mod/moon.pkg清单构建。 - 索引由带
#alias("_[_]")的普通方法(at、set、get)提供;只有列在各包extends.mbt中的 trait 方法才能用方法语法调用。 - 原生 OpenBLAS 后端已从本模块撤出,因为其上游绑定无法用 MoonBit 0.10 编译(见模块之外)。
安装
moon add Luna-Flow/linear-algebra@0.5.1
然后在 moon.pkg 中导入你使用的包:
///|
import {
"Luna-Flow/linear-algebra/immut",
"Luna-Flow/linear-algebra/mutable",
"Luna-Flow/linear-algebra/error" @la_error,
}
针对标量类型的泛型代码还需要上游包 Luna-Flow/luna-generic(0.4.0)和 Luna-Flow/arithmetic(0.5.1)。本模块需要 MoonBit 0.10.20 或更新的工具链(moonc ≥ 0.10.20),并且在 moon check --target all 下构建无警告。所有包都支持 wasm-gc、js、wasm 和 native 目标;mutable 为每个目标提供了一个调优的内核文件,基准测试包在 native 上运行。
页面
模块的每个包都有教程、API 参考和设计页面。src/doc_en_us 包不单独编写文档:它把本手册的每个页面链接为 .mbt.md 文件,使示例作为测试编译并运行。
| 部分 | 教程 | API | 设计 |
|---|---|---|---|
immut:不可变 Matrix、Vector,惰性 MatrixFn;精确行列式与幂 | 教程 | API | 设计 |
mutable:原地 Matrix、Vector,视图;LU、Cholesky、特征值、统计 | 教程 | API | 设计 |
error:供所有受检 API 使用的 LinearAlgebraError | 教程 | API | 设计 |
arithmetic:标量运算 trait 与重新导出 | 教程 | API | 设计 |
algebra:整体向量与矩阵的结构 trait(实验性) | 教程 | API | 设计 |
backends/default:实现 algebra trait 的稠密包装类型 | 教程 | API | 设计 |
container:与存储无关的操作字典与泛型转换(实验性) | 教程 | API | 设计 |
container/adapters:本仓库每种类型的字典 | 教程 | API | 设计 |
internal:共享的形状守卫(仅可在本模块内导入) | 教程 | API | 设计 |
consistency:跨包一致性测试(无公开项) | 教程 | API | 设计 |
perf_support:基准用例、fixture 与执行 | 教程 | API | 设计 |
perf:moon bench 入口包 | 教程 | API | 设计 |
perf_runner:单用例基准可执行程序 | 教程 | API | 设计 |
跨越多个包的指南和章节:
| 页面 | 内容 |
|---|---|
| 架构 | 各包之间的依赖关系、分层、目标与测试 |
| 约定 | 在 Luna Flow 文档标准之上的仓库规则 |
| 集成:algebra | 外部类型如何接入 algebra trait |
| 集成:container | 外部库如何发布 container 字典 |
导出项
具体类型
immut:Matrix、Vector、MatrixFn,行访问器Indexed,别名VecLib和VecCore、DeterminantAlgorithm、DeterminantScalar,以及顶层函数lin_combmutable:Matrix、Vector,视图RowView、ColView、Transpose,行访问器Lens,traitTolerance,重新导出的Sqrt,以及顶层函数identity和lin_combbackends/default:DenseMatrix、DenseVector、ImmutableDenseMatrix、ImmutableDenseVector,以及泛型辅助函数matmul、transpose、shape_of
数值例程
- 精确(
immut,用于Int、Int64、BigInt):基于 Bareiss 消元的determinant、基于反复平方的pow、trace、matmul - 浮点行列式(
immut,Float和Double):由DeterminantScalar选择部分选主元 LU,无零容差 - 浮点(
mutable,用于Float、Double):基于部分选主元 LU 的determinant、inverse、is_invertible和rank,以及reduce_row_elimination、cholesky_decomposition、is_positive_definite、对称矩阵的eigen、power_method、mean、variance、std_dev、frobenius_norm - 每个存在运行时失败情形的操作都有一个返回
Result[_, LinearAlgebraError]的受检形式,以及一个会中止的unchecked_*形式
能力层
algebra:MatrixShape,VectorShape,AdditiveVector,VecMulVector,TransposeMatrix,AdditiveMatrix,MatMulMatrixcontainer:VectorReadOps、VectorBuildOps、VectorPersistentEditOps、VectorMutableEditOps,它们对应的Matrix*版本,以及算法vector_map、vector_convert、matrix_map、matrix_convert、matrix_transposecontainer/adapters:每种类型、每种能力各一个工厂函数,例如mutable_matrix_read_ops
标量与错误
arithmetic:Abs、ApproxEq、CheckedDiv、CheckedSqrt、CheckedCompare,以及重新导出的Zero、One、Inverse、Conjugate、Sqrt和其他上游标量 trait 与类型error:LinearAlgebraError、LinearAlgebraErrorKind、构造函数和is_*谓词
数值注意事项
mutable 的数值例程以统一的绝对阈值 Tolerance::tolerance() 判断“零”,结果因此依赖输入尺度;请将数据缩放到一的量级(eigen 例外,它相对于元素做判断)。@immut.Matrix::determinant 为整数选择 Bareiss,为浮点数选择部分选主元 LU。其 LU 路径没有零容差,仍可能舍入、上溢或下溢,不是数值秩判定。mutable 设计 和 immut 设计 推导算法并说明这些边界。
接下来读什么
- 初次接触本包:若要在
Double矩阵上做数值计算,请从 mutable 教程开始;若需要值语义和精确的整数结果,请从 immut 教程开始。接着阅读错误教程,因为每个可能失败的操作都会返回LinearAlgebraError。 - 在库中使用:阅读 algebra 教程和 backends/default 教程以编写泛型代码,然后阅读 container 教程了解如何在不同表示之间搬运数据;在为自己的类型发布实现之前,请阅读集成指南(algebra、container)。把 API 页面放在手边;每一项都列出了它的错误、边界情况和开销。
- 参与贡献:阅读架构指南、约定、所修改包的设计页面,以及 internal 和 consistency 教程。基准测试见 perf 相关页面。
0.5.1 中的状态
algebra、backends/default的 trait 层以及container是实验性的;它们的签名可能变化。- 版本历史见更新日志。
模块之外
原生 OpenBLAS 后端(backends/openblas,包括 BlasMatrix、BlasVector 及其 container 适配器)不属于本版本。其上游绑定 Kaida-Amethyst/openblas 0.1.3 无法用 MoonBit 0.10 编译。迁移后的源码、它自己的文档以及针对该绑定的补丁保存在 contrib/openblas_backend 中;那里不是本模块的包,不参与构建,也不在本手册的范围内。需要该后端的用户可以继续使用 0.4.7。
验证
推荐的发布检查(在仓库根目录运行):
moon check --target all
moon test
./run_test.sh
run_test.sh 运行 immut 和 consistency,并在全部四个目标上运行 container、container/adapters、backends/default、mutable 以及文档包 doc_en_us。设置 LINEAR_ALGEBRA_TEST_BENCH=1 可加入基准测试包。