[][src]Crate crabsformer

Crabsformer is an easy-to-use fundamental library for scientific computing with Rust, highly inspired by NumPy.

Usage

Add this to your Cargo.toml:

[dependencies]
crabsformer = "2019.3.10"

and this to your crate root:

#[macro_use] extern crate crabsformer;
use crabsformer::prelude::*;

To get started using Crabsformer, read the quickstart tutorial below.

Quickstart Tutorial

Prerequisites

Before reading this quick tutorial you should know a bit of Rust. If you would like to refresh your memory, take a look at the Rust book.

The Basics

There are two main data structures in Crabsformer:

  1. Vector<T> is a fixed-length list of elements of the same numeric type. It has one atribute called len to represent the total number of elements.
  2. Matrix<T> is a table of elements of the same numeric type. It has one atribute called shape that represent the number of rows and the number of columns.

Vector<T> is pronounced as 'numeric vector' to avoid confussion with Rust's vector Vec<T> data structure.

Numeric Vector Creation

There are several ways to create numeric vector.

For example, you can create a numeric vector from a Rust vector using Vector::from static method. The type of the resulting numeric vector is deduced from the type of the elements in the sequences.

let x = vec![3, 1, 4, 1, 5];
let y = Vector::from(x);

The vector! macro is provided to make initialization of the numeric vector more convenient.

let v = vector![1, 10, 11, 314];

It can also initialize each element of a numeric vector with a given value.

let v = vector![0; 5]; // vector![0, 0, 0, 0, 0]

The function uniform creates a numeric vector of the given length len and populate it with random samples from a uniform distribution over the half-open interval [low, high) (includes low, but excludes high).

let v = Vector::uniform(5, 0.0, 1.0);
// Vector([0.054709196, 0.86043775, 0.21187294, 0.6413728, 0.14186311]) (Random)

To create a numeric vector of evenly spaced values, Crabformer provide range function.

let x = Vector::range(0, 10, 1);
assert_eq!(x, vector![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);

Values are generated within the half-open interval [start, stop) (in other words, the interval including start but excluding stop).

See also: vector!, zeros, zeros_like, ones, ones_like, full, full_like, range, linspace, uniform, normal.

Numeric Vector Operation

You can perform arithmetic operations on a Vector<T>. For example, if you add the Vector<T>, the arithmetic operator will work element-wise. The output will be a Vector<T> of the same length.

let x = vector![0.5, 0.6, 0.9, 1.7];
let y = vector![1.0, 0.4, 0.2, 0.1];
let z = x + y;
assert_eq!(z, vector![1.5, 1.0, 1.1, 1.8]);

If you try to add Vector<T> with a different number of elements, you will get an error. For example:

let x = vector![3, 1, 4, 1, 5] + vector![2, 10, 9];
thread 'guide::example::main' panicked at 'Vector addition with invalid length: 5 != 3' src/main.rs:12:13

You can run an arithmetic operation on the Vector<T> with a scalar value. For example, this code multiplies each element of the Vector<T> by 2.

let x = vector![3, 1, 4, 1] * 2;
assert_eq!(x, vector![6, 2, 8, 2]);

If you would like to square of the individual elements of the Vector<T>, or even higher up, use the power method. Here, each element of the Vector<T> is raised to the power 2.

let x = vector![3, 1, 4, 1];
let y = x.power(2);
assert_eq!(y, vector![9, 1, 16, 1]);

You can use filter to find the elements that match your criteria.

let x = vector![3, 1, 4, 1];
let y = x.filter(|x| x >= 2);
assert_eq!(y, vector![3, 4]);

Getting help

Feel free to start discussion at GitHub issues.

License

Crabsformer is licensed under the Apache-2.0 license.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in Crabsformer by you, as defined in the Apache-2.0 license, shall be licensed as above, without any additional terms or conditions.

Modules

prelude

Crabsformer's prelude

Macros

matrix
vector

Creates a numeric vector containing the arguments.

Structs

Matrix

Matrix elements structure

Vector

Vector elements structure