| Crates.io | vectra |
| lib.rs | vectra |
| version | 0.2.4 |
| created_at | 2025-09-19 07:47:44.798718+00 |
| updated_at | 2025-10-26 17:05:40.959526+00 |
| description | A multi-dimensional array library for Rust, similar to NumPy |
| homepage | |
| repository | https://github.com/passchaos/vectra |
| max_upload_size | |
| id | 1845974 |
| size | 288,133 |
Vectra is a high-performance multi-dimensional array library for Rust, inspired by NumPy and influenced by Rust's ndarray ecosystem. It provides efficient array operations, mathematical functions, and linear algebra capabilities with a focus on performance and ergonomics.
Add this to your Cargo.toml:
[dependencies]
vectra = "0.2.0"
For BLAS support (recommended for better performance):
[dependencies]
vectra = { version = "0.2.0", features = ["blas"] }
use vectra::prelude::*;
fn main() {
// Create arrays
let zeros = Array::<_, f64>::zeros([2, 3]);
let ones = Array::<_, i32>::ones([3, 3]);
let eye = Array::<_, f32>::eye(3); // Identity matrix
// Create from vector
let data = vec![1, 2, 3, 4, 5, 6];
let arr = Array::from_vec(data, [2, 3]);
// Array operations
let reshaped = arr.reshape([3, 2]);
let transposed = arr.transpose();
// Mathematical operations
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0], [2, 2]);
let b = Array::from_vec(vec![5.0, 6.0, 7.0, 8.0], [2, 2]);
let sum = &a + &b; // Element-wise addition
let product = &a * &b; // Element-wise multiplication
let dot_product = a.matmul(&b); // Matrix multiplication
// Mathematical functions
let angles = Array::from_vec(vec![0.0, std::f64::consts::PI/2.0], [2]);
let sines = angles.sin();
let exponentials = angles.exp();
// Random arrays
let random_arr = Array::<_, f64>::random([3, 3]);
let normal_arr = Array::<_, f64>::randn([2, 4]);
}
use vectra::prelude::*;
// Create arrays filled with specific values
let zeros = Array::<_, f64>::zeros([2, 3]);
let ones = Array::<_, i32>::ones([3, 3]);
let filled = Array::full([2, 2], 42);
// Create from existing data
let data = vec![1, 2, 3, 4, 5, 6];
let arr = Array::from_vec(data, [2, 3]);
// Create ranges
let range1d = Array::arange(0, 10, 1); // [0, 1, 2, ..., 9]
let range_count = Array::arange_c(0, 2, 5); // 5 elements starting from 0 with step 2
// Random arrays
let random = Array::<_, f64>::random([3, 3]); // Uniform [0, 1)
let uniform = Array::uniform([2, 2], -1.0, 1.0); // Uniform [-1, 1)
let normal = Array::<_, f64>::randn([2, 3]); // Standard normal distribution
// Identity matrix
let identity = Array::<_, f64>::eye(4);
use vectra::prelude::*;
let arr = Array::from_vec(vec![1, 2, 3, 4, 5, 6], [2, 3]);
// Reshaping and transposition
let reshaped = arr.reshape([3, 2]);
let transposed = arr.transpose(); // 2D arrays only
// Indexing
let element = arr[[0, 1]]; // Access single element
let mut arr_mut = arr.clone();
arr_mut[[1, 2]] = 42; // Modify element
// Broadcasting operations
let a = Array::from_vec(vec![1, 2, 3], [3, 1]);
let b = Array::from_vec(vec![4, 5], [1, 2]);
let broadcasted = &a + &b; // Results in [3, 2] array
// Scalar operations
let scaled = arr.mul_scalar(2);
let shifted = arr.add_scalar(10);
use vectra::prelude::*;
let arr = Array::from_vec(vec![0.0, 1.0, 2.0, 3.0], [2, 2]);
// Trigonometric functions
let sines = arr.sin();
let cosines = arr.cos();
let tangents = arr.tan();
// Inverse trigonometric functions
let arcsines = arr.asin();
let arccosines = arr.acos();
let arctangents = arr.atan();
// Hyperbolic functions
let sinh_vals = arr.sinh();
let cosh_vals = arr.cosh();
let tanh_vals = arr.tanh();
// Exponential and logarithmic functions
let exponentials = arr.exp();
let logarithms = arr.ln();
let log10_vals = arr.log10();
let log2_vals = arr.log2();
// Power and root functions
let squares = arr.pow2();
let cubes = arr.powi(3);
let square_roots = arr.sqrt();
let cube_roots = arr.cbrt();
// Other mathematical functions
let absolute = arr.abs();
let rounded = arr.round();
let floored = arr.floor();
let ceiled = arr.ceil();
use vectra::prelude::*;
let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0], [2, 2]);
let b = Array::from_vec(vec![5.0, 6.0, 7.0, 8.0], [2, 2]);
// Matrix multiplication with default policy
let result = a.matmul(&b);
// Matrix multiplication with specific backend
let result_blas = a.matmul_with_policy(&b, MatmulPolicy::Blas);
let result_faer = a.matmul_with_policy(&b, MatmulPolicy::Faer);
let result_naive = a.matmul_with_policy(&b, MatmulPolicy::Naive);
// Identity matrix
let identity = Array::<_, f64>::eye(3);
// Transpose
let transposed = a.transpose();
Vectra is designed for high performance with multiple optimization strategies:
Check out the examples/ directory for more comprehensive examples:
demo.rs: Basic usage and array operationsmatmul.rs: Matrix multiplication performance comparisonRun examples with:
cargo run --example demo
cargo run --example matmul
default: Includes BLAS supportblas: Enable BLAS backend for linear algebra operationsThis project is licensed under the MIT License - see the LICENSE file for details.
Vectra includes comprehensive API documentation that can be generated using Rust's built-in documentation system:
# Generate documentation for the library
cargo doc
# Generate and open documentation in your browser
cargo doc --open
# Generate documentation with private items (for development)
cargo doc --document-private-items
# Generate documentation for all dependencies
cargo doc --no-deps
The generated documentation includes:
The documentation includes extensive examples for common use cases:
// Array creation examples
let arr = Array::zeros([3, 3]); // Create 3x3 zero matrix
let arr = Array::ones([2, 4]); // Create 2x4 ones matrix
let arr = Array::arange(0, 10, 2); // Create [0, 2, 4, 6, 8]
// Mathematical operations examples
let result = arr.sin(); // Element-wise sine
let result = arr.matmul(&other); // Matrix multiplication
let result = arr.broadcast_to([4, 3]); // Broadcasting
// Advanced operations examples
let gathered = arr.gather(0, &indices); // Gather along axis
let reshaped = arr.reshape([2, -1]); // Reshape with inference
For development and contribution:
# Install documentation dependencies
cargo install mdbook # For additional documentation
# Generate docs with all features enabled
cargo doc --all-features
# Generate docs for specific features
cargo doc --features "blas"
# Check documentation for warnings
cargo doc 2>&1 | grep warning
Vectra follows Rust documentation best practices:
Contributions are welcome! Please feel free to submit a Pull Request.
When contributing to Vectra, please ensure:
faer, rand, and num-traits