Vectra - Rust Multi-dimensional Array Library
A feature-rich multi-dimensional array library, similar to Python's NumPy, designed specifically for the Rust language.
Design Philosophy
Vectra is designed with simplicity and ease of use as the primary goals. Unlike other Rust libraries that prioritize maximum performance or compile-time safety, Vectra focuses on providing an intuitive and accessible API that makes multi-dimensional array operations straightforward for developers of all skill levels.
Key Design Principles:
- šÆ Simplicity First: Clean, intuitive API that's easy to learn and use
- š Developer Experience: Familiar NumPy-like syntax for smooth transition
- š Accessibility: Comprehensive examples and clear documentation
- š§ Practicality: Focus on common use cases rather than edge case optimization
While performance and safety are important, they are secondary to creating a library that developers actually enjoy using for rapid prototyping, data analysis, and scientific computing tasks.
Features
- š Multi-dimensional Arrays: Support for arbitrary dimensional array operations
- š§® Mathematical Operations: Complete mathematical operation support (addition, subtraction, multiplication, division, matrix multiplication, etc.)
- š Trigonometric Functions: Complete set of trigonometric and hyperbolic functions
- š Logarithmic & Exponential Functions: Natural log, base-10/2 log, exponential functions
- š” Broadcasting: Automatic shape alignment for operations between arrays of different shapes
- š Aggregation Functions: Sum, mean, max, min, etc.
- š Shape Operations: Reshape, transpose and other array transformations
- š ļø Ease of Use: Intuitive API design, similar to NumPy experience
Quick Start
Installation
Add to your Cargo.toml:
[]
= "0.1.0"
Basic Usage
use Array;
Core Features
1. Array Creation
// Create zero array
let zeros = zeros;
// Create ones array
let ones = ones;
// Create identity matrix
let identity = eye;
// Create from vector
let arr = from_vec.unwrap;
// Random array creation
let random = random; // Random numbers between 0-1
let randint = randint; // Random integers between 1-9
let randn = randn; // Normal distribution random numbers
let uniform = uniform; // Uniform distribution random numbers
2. Array Operations
let arr = from_vec.unwrap;
// Reshape
let reshaped = arr.reshape.unwrap;
// Transpose (2D array)
let transposed = arr.transpose.unwrap;
// Index access
let element = arr;
3. Mathematical Operations
let a = from_vec.unwrap;
let b = from_vec.unwrap;
// Element-wise operations
let sum = a.clone + b.clone;
let diff = a.clone - b.clone;
let product = a.clone * b.clone;
let quotient = a.clone / b.clone;
// Scalar operations
let scaled = a.mul_scalar;
let shifted = a.add_scalar;
// Matrix multiplication
let dot_product = a.dot.unwrap;
// Broadcasting examples
let matrix = from_vec.unwrap;
let scalar = from_vec.unwrap;
let broadcast_result = matrix + scalar; // Broadcasts scalar to matrix shape
println!;
// Vector and matrix broadcasting
let vector = from_vec.unwrap;
let column = from_vec.unwrap;
let result = vector + column; // Results in 2x2 matrix
println!;
// Trigonometric functions
use PI;
let angles = from_vec.unwrap;
let sin_values = angles.sin;
let cos_values = angles.cos;
let tan_values = angles.tan;
println!;
println!;
// Hyperbolic functions
let values = from_vec.unwrap;
let sinh_values = values.sinh;
let tanh_values = values.tanh;
println!;
// Angle conversion
let degrees = from_vec.unwrap;
let radians = degrees.to_radians;
println!;
// Logarithmic and exponential functions
use E;
let values = from_vec.unwrap;
let ln_values = values.ln;
println!; // [0, 1, 2]
// Base-specific logarithms
let powers_of_10 = from_vec.unwrap;
let log10_values = powers_of_10.log10;
println!; // [0, 1, 2]
let powers_of_2 = from_vec.unwrap;
let log2_values = powers_of_2.log2;
println!; // [0, 1, 2]
// Exponential functions
let exp_input = from_vec.unwrap;
let exp_values = exp_input.exp;
println!; // [1, e, e²]
let exp2_values = exp_input.exp2;
println!; // [1, 2, 4]
4. Aggregation Functions
let arr = from_vec.unwrap;
// Global aggregation
let sum = arr.sum;
let mean = arr.mean_int; // Integer mean
let max_val = arr.max;
let min_val = arr.min;
// Axis aggregation
let sum_axis0 = arr.sum_axis.unwrap;
let sum_axis1 = arr.sum_axis.unwrap;
5. Function Mapping
let arr = from_vec.unwrap;
// Apply function to each element
let squared = arr.map;
let doubled = arr.map;
Array Properties
let arr = from_vec.unwrap;
// Get shape
let shape = arr.shape; // &[2, 3]
// Get number of dimensions
let ndim = arr.ndim; // 2
// Get total number of elements
let size = arr.size; // 6
Indexing Operations
let mut arr = from_vec.unwrap;
// Read element
let element = arr;
// Modify element
arr = 10;
// Support multiple indexing methods
let val1 = arr; // 2D indexing
let val2 = arr; // Slice indexing
Error Handling
Most operations in the library return Result type, providing clear error messages:
// Shape mismatch error
let result = from_vec;
match result
// Matrix multiplication dimension mismatch
let a = from_vec.unwrap;
let b = from_vec.unwrap;
let result = a.dot; // Returns error
Running Examples
# Run demo program
# Run tests
Comparison with NumPy
| Feature | NumPy | Vectra |
|---|---|---|
| Array creation | np.zeros(), np.ones() |
Array::zeros(), Array::ones() |
| Shape operations | arr.reshape(), arr.T |
arr.reshape(), arr.transpose() |
| Mathematical operations | +, -, *, @ |
+, -, *, dot() |
| Aggregation functions | arr.sum(), arr.mean() |
arr.sum(), arr.mean_int() |
| Indexing | arr[0, 1] |
arr[[0, 1]] |