Cangjie Scientific Library
Introduction
The Cangjie Scientific Library provides a comprehensive scientific computing toolkit for the Cangjie language, featuring core modules for linear algebra, statistical analysis, and data visualization. The project draws design inspiration from established libraries in the Python ecosystem, such as NumPy, SciPy, and Matplotlib, while leveraging Cangjie's type system to deliver enhanced type safety at compile time.
Core Features
📊 Functional Modules
- Linear Algebra: Offers vector, matrix, and tensor operations, including array creation, reshaping, concatenation, decomposition, and various matrix computations, with interfaces compatible with NumPy/SciPy.
- Statistical Analysis: Includes approximately 80 continuous and discrete probability distributions, covering common statistical functions, hypothesis testing, correlation analysis, and regression models.
- Data Visualization: Wraps the C++ library matplot++ to support 55 types of plots, such as line charts, 3D graphs, histograms, and surface plots, providing a familiar Matplotlib-like interface.
🛡️ Type-Safe Numerical Computing
All numerical types (e.g., Real, Float, Complex) and multidimensional arrays (e.g., Vector, Matrix, Tensor) are built upon Cangjie's strong type system. Compile-time checks ensure type compatibility and dimensional consistency, preventing common runtime array operation errors.
⚡ Optimized Computational Performance
Leveraging OpenBLAS for acceleration, the library achieves performance comparable to mainstream scientific computing libraries in dense operations such as matrix multiplication and linear solving.
🔄 Seamless Ecosystem Migration
The interface design follows naming conventions and usage patterns from the Python/MATLAB scientific computing stack, enabling developers familiar with these ecosystems to quickly adapt while benefiting from the robustness of a statically typed language.
How to Build (Linux)
Prerequisites
- Cangjie version 1.0.0 or above (download from https://cangjie-lang.cn/download/1.0.0)
- cmake version at least 3.15, e.g.
sudo apt install cmake - Fortran compiler, e.g.
sudo apt install gfortran - For viewing plots, make sure gnuplot is installed, e.g.
sudo apt install gnuplot
Build steps
-
make allfor all C dependencies. -
Add
.sopath for third-party libraries to your environment.export LD_LIBRARY_PATH={{path to scientific}}/src/thirdParty/solib:$LD_LIBRARY_PATH -
./run.shfor matplot++ and OpenBlas dependencies. -
cjpm buildbuild entire module.
If the compilation is successful, the generated executable file is located at
./target/release/bin/main. For example, to run benchmarks on multiplication,
use
./target/release/bin/main --test mult --size 1000 --seed 1
Unit tests
To run unit tests, use the same build step as above, but running
cjpm test at the end.
How to Build (Windows)
Prerequisites
Download and install Cangjie from https://cangjie-lang.cn/download/1.0.0.
We suggest using MSYS2 (download from https://www.msys2.org/, then install to e.g. C:\msys64).
After installing MSYS2, update package database and install MinGW-w64 toolchain (run the following in MSYS2 MSYS):
pacman -Syu # close terminal when prompted, then reopen
pacman -S --needed base-devel mingw-w64-x86_64-toolchain # install all by default
pacman -S mingw-w64-x86_64-cmake
pacman -S mingw-w64-x86_64-gnuplot # for viewing plots
Then add C:\msys64\mingw64\bin to PATH (important: add it as first entry of PATH,
so its libraries are looked up first).
Add {{path to scientific}}/src/thirdParty/solib to PATH.
Install openblas directly using MSYS2 (run the following in MSYS MINGW64):
pacman -S mingw-w64-x86_64-openblas
Then compile the remaining dependencies (run the following in MSYS MINGW64):
./run_win.sh
make all
Finally, run cjpm build to build the entire module (this can be done
in Windows cmd). The remaining parts are the same as in Linux.
Using scientific in your code
In cjpm.toml, add the following to compile-option under [package]:
-Woff unused -L{path_to_scientific}/src/thirdParty/solib/ -lcdf -lbesselj -lcjmat -lasa111 -lmt19937 -lutils -lopenblas -lpocketfft
Also, add the following binary dependency at root level:
[target.x86_64-unknown-linux-gnu]
[target.x86_64-unknown-linux-gnu.bin-dependencies]
path-option = ["{path_to_scientific}/target/release/scientific"]
For example, if your scientific library is under /home, then a sample cjpm.toml file is as follows:
[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 -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"]
For Windows (suppose your scientific library is in 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"]
Then, your can import the appropriate packages under scientific. Below is a simple test program:
package test
import scientific.linear.*
main(): Int64 {
var a1: Vector<Float32> = empty(10)
println(a1.dtype())
println(a1.size())
return 0
}
The following demonstrates simple plotting (make sure gnuplot is installed):
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
}
Codebase Structure Overview
docs- docs for scientific library base on sphinx.src/numbers- numeric types definitions and their hierarchy.src/linear- Tensor definition and linear algebra functions, an encapsulation of LAPACK functions.src/stats- statistical analysis functions, include random number generation, probability distribution sampling, statistical summary, etc.src/special- compute special functions, such as Bessel functions.src/matplot- data visualization, an encapsulation of matplot++.src/thridParty- all 3rd party dependency.
Contributing
The Cangjie Scientific Library is developed under an open-source model. We welcome developers interested in scientific computing, system modeling, or high-performance computing to join the project.
Star ⭐ the repository to stay updated. Your contributions and feedback are highly appreciated!