scientific 是仓颉编程语言中用于统计分析、线性代数求解和数据可视化的库。
仓颉 Scientific 科学计算库
简介
仓颉 Scientific 库为仓颉语言提供了一套完整的科学计算工具集,包含线性代数、统计分析、数据可视化等核心模块。该项目在设计上借鉴了 Python 生态中 NumPy、SciPy、Matplotlib 等成熟库的接口风格,同时充分发挥仓颉语言的类型系统特性,在编译期提供更强的类型安全保证。
核心特性
📊 功能模块
- 线性代数:提供向量、矩阵与张量操作,支持数组创建、变形、拼接、分解及各类矩阵运算,接口与 NumPy/SciPy 保持兼容。
- 统计分析:内置约 80 种连续与离散概率分布,涵盖常用统计函数、假设检验、相关性分析与回归模型。
- 数据可视化:基于 matplot++ 封装,支持线图、3D 图、直方图、曲面图等 55 种图形类型,提供与 Matplotlib 相似的绘图接口。
🛡️ 类型安全的数值计算
库中所有数值类型(如 Real、Float、Complex)和多维数组(如 Vector、Matrix)都基于仓颉的强类型系统实现。编译期会检查类型匹配与维度一致性,避免常见的运行时数组操作错误。
⚡ 优化的计算性能
底层计算通过 OpenBLAS 加速,在矩阵乘法、线性求解等密集运算中能达到与主流科学计算库相当的性能水平。
🔄 平滑的生态迁移
接口设计参考了 Python/MATLAB 科学计算栈的命名习惯和使用模式,让熟悉这些生态的开发者能够快速上手,同时获得静态类型语言在代码健壮性上的优势。
构建指南(Linux)
前提条件
- 仓颉版本至少1.0.0(从 https://cangjie-lang.cn/download/1.0.0 )下载
- cmake 版本至少为 3.15,例如
sudo apt install cmake - Fortran 编译器,例如
sudo apt install gfortran - 如需查看图表,请确保安装 gnuplot,例如
sudo apt install gnuplot
构建步骤
-
make all编译所有 C 依赖项 -
将第三方库的
.so路径添加到环境变量中export LD_LIBRARY_PATH={{path to scientific}}/src/thirdParty/solib:$LD_LIBRARY_PATH -
./run.sh安装 matplot++ 和 openBlas 依赖 -
cjpm build构建整个模块
如果编译成功,生成的可执行文件位于 ./target/release/bin/main。例如,要运行乘法基准测试,请使用:
./target/release/bin/main --test mult --size 1000 --seed 1
单元测试
运行单元测试时使用相同的构建步骤,但最后执行 cjpm test。
构建指南(Windows)
环境准备
请从 https://cangjie-lang.cn/download/1.0.0 下载并安装仓颉语言。
推荐使用 MSYS2(从 https://www.msys2.org/ 下载,假设安装至 C:\msys64 目录)。
安装完成后,更新软件包数据库并安装 MinGW-w64 工具链(在 MSYS2 MSYS 环境中执行以下命令):
pacman -Syu # 根据提示关闭终端后重新启动
pacman -S --needed base-devel mingw-w64-x86_64-toolchain # 默认安装全部组件
pacman -S mingw-w64-x86_64-cmake
pacman -S mingw-w64-x86_64-gnuplot # 用于图表可视化
将 C:\msys64\mingw64\bin 添加至 PATH 环境变量(重要:需设为 PATH 的首个条目,确保优先加载其库文件)。
将 {{path to scientific}}/src/thirdParty/solib 添加至 PATH 环境变量。
通过 MSYS2 直接安装 OpenBlas(在 MSYS MINGW64 环境中执行以下命令):
pacman -S mingw-w64-x86_64-openblas
随后编译剩余依赖项(在 MSYS MINGW64 环境中执行以下命令):
./run_win.sh
make all
最后执行 cjpm build 完成整体模块构建(此步骤可在 Windows cmd 中完成)。后续流程与 Linux 环境一致。
在代码中使用 scientific 库
在 cjpm.toml 文件中,在 [package] 下的 compile-option 添加以下内容:
-Woff unused -L{path_to_scientific}/src/thirdParty/solib/ -lcdf -lbesselj -lcjmat -lasa111 -lmt19937 -lutils -lopenblas -lpocketfft"
同时,在根级别添加以下二进制依赖:
[target.x86_64-unknown-linux-gnu]
[target.x86_64-unknown-linux-gnu.bin-dependencies]
path-option = ["{path_to_scientific}/target/release/scientific"]
例如,如果您的 scientific 库位于 /home 下,则示例 cjpm.toml 文件如下:
[package]
cjc-version = "0.60.5"
name = "test"
description = "nothing here"
version = "1.0.0"
target-dir = ""
src-dir = ""
output-type = "executable"
compile-option = "-Woff unused -L/home/scientific/src/thirdParty/solib/ -lcdf -lbesselj -lcjmat -lasa111 -lmt19937 -lutils -lopenblas -lpocketfft"
override-compile-option = ""
link-option = ""
package-configuration = {}
[target.x86_64-unknown-linux-gnu]
[target.x86_64-unknown-linux-gnu.bin-dependencies]
path-option = ["/home/scientific/target/release/scientific"]
Windows 的案例如下 (假设 scientific 库位于 D:\):
[package]
cjc-version = "1.0.0"
name = "test"
description = "nothing here"
version = "1.0.0"
target-dir = ""
src-dir = ""
output-type = "executable"
compile-option = "-Woff unused -LD:\\scientific\\src\\thirdParty\\solib\\ -lcdf -lbesselj -lcjmat -lasa111 -lmt19937 -lutils -lopenblas -lpocketfft"
override-compile-option = ""
link-option = ""
package-configuration = {}
[target.x86_64-w64-mingw32]
[target.x86_64-w64-mingw32.bin-dependencies]
path-option = ["D:\\scientific\\target\\release\\scientific"]
然后,您可以导入 scientific 下的相应包。以下是一个简单的测试程序:
package test
import scientific.linear.*
main(): Int64 {
var a1: Vector<Float32> = empty(10)
println(a1.dtype())
println(a1.size())
return 0
}
以下演示简单的绘图功能(确保已安装 gnuplot):
package test
import std.math.*
import scientific.linear.*
import scientific.matplot.*
main(): Int64 {
let x = linspace<Float64>(0.0, 2.0 * Float64.getPI())
let y = x.apply({t => sin(t)})
plot(x, y, line_spec:"-o")
show()
return 0
}
代码库结构概览
docs- 基于 sphinx 的库文档src/numbers- 数值类型定义及其层次结构src/linear- 张量定义和线性代数函数,封装了 LAPACK 函数src/stats- 统计分析函数,包括随机数生成、概率分布抽样、统计摘要等src/special- 特殊函数计算,如贝塞尔函数src/matplot- 数据可视化,封装了 matplot++src/thridParty- 所有第三方依赖项
参与贡献
仓颉 Scientific 库采用开源开发模式,欢迎对科学计算、系统建模或高性能计算感兴趣的开发者加入项目。
欢迎 Star ⭐ 关注项目,期待你的贡献与反馈!