#线性代数 #数学 #代数 #线性 #ultrametric-tree #ultrametric-matrix

ultrametric_matrix_tools

提供生成和处理超度量矩阵的函数和数据结构的工具箱

1 个不稳定版本

0.1.1 2021年12月29日

#1478数据结构

Apache-2.0

37KB
519

超度量矩阵工具

DOI

此工具箱提供用于在Rust和Python中构建和处理超度量矩阵的函数和数据结构。项目的目标是提供高效的超度量矩阵和超度量树工具。目前,项目具有以下功能。

功能

  • 生成随机超度量矩阵
  • 测试超度量矩阵属性
  • 从超度量矩阵构建超度量树
  • 获取超度量树属性
  • 超度量矩阵与向量的快速乘法

实现是用Rust编写的,可以交叉编译到Python。

目录

快速入门

快速入门 Rust

将以下内容添加到Cargo.toml文件中

[dependencies]
# TODO: replace the * by the latest version.
ultrametric_matrix_tools = "*"

使用示例

use ultrametric_matrix_tools::na::{DMatrix, DVector};
use ultrametric_matrix_tools::UltrametricTree;

fn main() {
    let matrix = DMatrix::from_vec(
        4,
        4,
        vec![
            0.0, 1.0, 3.0, 1.0, 1.0, 3.0, 1.0, 1.0, 3.0, 1.0, 5.0, 1.0, 1.0, 1.0, 1.0, 1.0,
        ],
    );
    let vector = DVector::from_vec(vec![4.0, 2.0, 7.0, 5.0]);

    let tree = UltrametricTree::from_matrix(&matrix);
    let product = tree * vector;
}

更多示例 可以在 ./examples/ 中找到。

快速入门 Python

您可以通过运行以下命令安装当前版本

pip install ultrametric_matrix_tools

超度量树构建和与其乘法的示例

from ultrametric_matrix_tools import UltrametricTree
import numpy as np

matrix = np.array([[0.0, 1.0, 3.0, 1.0], [1.0, 3.0, 1.0, 1.0], [
                  3.0, 1.0, 5.0, 1.0], [1.0, 1.0, 1.0, 1.0]])
vector = np.array([4.0, 2.0, 7.0, 5.0])

tree = UltrametricTree(matrix)
product = tree.mult(vector)

更多示例 可以在 ./examples/ 中找到。

构建

构建Rust库

通过运行以下命令构建Rust库

cargo build --release

编译的Rust库位于 ./target/release/ 并可以从那里复制。

构建Python模块

使用PyO3从Rust代码构建Python模块。要构建Python模块,您需要安装Cargo并运行

cargo build --release

编译的Python模块位于 ./target/release/ 并可以从那里复制。

要从Linux主机系统导出Python wheel,请运行以下命令

Linux(需要docker)

docker run --rm -v $(pwd):/io konstin2/maturin build --release

Windows(需要mingw32-python和mingw64-python)

make python_package_windows

目前,不支持交叉编译到macOS。

示例

Rust示例

您可以通过运行以下命令尝试Rust示例,您需要安装Cargo。您可以通过运行以下命令尝试位于 ./examples/ 的Python示例

cargo run --release --example [example_name]

例如,要运行乘法示例,请运行

cargo run --release --example multiplication

Python示例

要运行Python示例,您需要安装Cargo。您可以通过运行以下命令来尝试位于./examples/中的Python示例

make python_example name=[example_name]

例如,要运行乘法示例,请运行

make python_example name=multiplication

或者,如果您已经通过pip安装了Python包,可以直接运行示例

python [example_name].py

许可证

此项目遵循Apache-2.0许可

基准测试

基准测试使用criterioncargo进行测试,可以通过运行以下命令来安装

cargo install cargo-criterion

基准测试位于./benches,并通过以下命令运行

cargo criterion --bench [benchmark_name]

依赖项

~10MB
~189K SLoC