Expand description

Github CI Crates.io docs.rs

About rhai-sci

This crate provides some basic scientific computing utilities for the Rhai scripting language, inspired by languages like MATLAB, Octave, and R. For a complete API reference, check the docs.

Install

To use the latest released version of rhai-sci, add this to your Cargo.toml:

automl = "0.1.0"

To use the bleeding edge instead, add this:

automl = { git = "https://github.com/cmccomb/rhai-sci" }

Usage

Using this crate is pretty simple! If you just want to evaluate a single line of Rhai, then you only need:

use rhai::INT;
use rhai_sci::eval;
let result = eval::<INT>("argmin([43, 42, -500])").unwrap();

If you need to use rhai-sci as part of a persistent Rhai scripting engine, then do this instead:

use rhai::{Engine, packages::Package, INT};
use rhai_sci::SciPackage;

// Create a new Rhai engine
let mut engine = Engine::new();

// Add the rhai-sci package to the new engine
engine.register_global_module(SciPackage::new().as_shared_module());

// Now run your code
let value = engine.eval::<INT>("argmin([43, 42, -500])").unwrap();

Constants

This package provides a few constants that are common and useful.

pi

The ratio of a circle’s circumference to its diameter.

pi // => 3.141592653589793

e

Euler’s number.

e // => 2.718281828459045

g

Acceleration due to gravity on Earth in m/s^2.

g // => 9.80665

Functions

This package provides a large variety of functions to help with scientific computing. Each one of these is written in Rhai itself! The source code is here.

argmax

Returns the argument of the largest element in a 1-D array.

argmax([43, 42, 500]) // => 2

argmin

Returns the argument of the smallest element in a 1-D array.

argmin([43, 42, -500]) // => 2

bounds

Returns the bounds (smallest and largest elements) of a 1-D array.

bounds([32, 15, -7, 10, 1000, 41, 42]) // => [-7, 1000]

diag

This function can be used in two distinct ways.

  1. If the argument is an 2-D array, diag returns an array containing the diagonal of the array.
  2. If the argument is a 1-D array, diag returns a matrix containing the argument along the diagonal and zeros elsewhere.
diag([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) // => [1, 5, 9]
diag([1, 2, 3]) // => [[1.0, 0.0, 0.0], [0.0, 2.0, 0.0], [0.0, 0.0, 3.0]]

eye

Create an identity matrix with ones along the diagonal and zeros elsewhere. Can be called with either one argument (creating a square matrix) or two arguments (specifying the number of rows and columns separately).

eye(3) // => [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]
eye(3, 3) // => [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]

interp1

Given reference data, perform linear interpolation.

interp1([0, 1], [1, 2], 0.5) // => 1.5

iqr

Returns the inter-quartile range for a 1-D array.

iqr([1, 1, 1, 1, 1, 1, 1, 5, 6, 9, 9, 9, 9, 9, 9, 9, 9])" // => 8.0

linspace

Returns an array containing a number of elements linearly spaced between two bounds.

linspace(1, 2, 5) // => [1.0, 1.25, 1.5, 1.75, 2.0]

logspace

Returns an array containing a number of elements logarithmically spaced between two bounds.

logspace(1, 3, 3) // => [10.0, 100.0, 1000.0]

max

Returns the highest value between a pair of numbers (if called with two arguments) or in a 1-D array (if called with a single Array-type argument).

max(41, 42) // => 42
max([41, 42, -1, 7, 2]) // => 42

min

Returns the lowest value between a pair of numbers (if called with two arguments) or in a 1-D array (if called with a single Array-type argument).

min(43, 42) // => 42
min([43, 42, 500]) // => 42

maxk

Returns the k highest values from a 1-D array.

maxk([32, 15, -7, 10, 1000, 41, 42], 3) // => [41, 42, 1000]

mean

Returns the average of a 1-D array.

mean([1, 2, 3]) // => 2.0

median

Returns the median of a 1-D array.

median([1, 1, 1, 1, 2, 5, 6, 7, 8]) // => 2.0

mink

Returns the k smallest values in a 1-D array.

mink([32, 15, -7, 10, 1000, 41, 42], 3) // => [-7, 10, 15]

mode

Returns the mode of a 1-D array.

mode([1, 2, 2, 2, 2, 3]) // => 2

ndims

Returns the number of dimensions in an array.

ndims(ones(4, 6)) // => 2

numel

Returns the number of elements in an array.

numel(ones(4, 6)) // => 24

ones

Create an matrix filled with ones. Can be called with either one argument (creating a square matrix) or two arguments (specifying the number of rows and columns separately).

ones(3) // => [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]]
ones(3, 3) // => [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]]

prctile

Returns a given percentile value for a 1-D array of data.

prctile([1, 2, 0, 3, 4], 50) // => 2.0

rand

Create a matrix filled with random values between 0 and 1. Can be called with either zero arguments (returning a single random value), one argument (creating a square matrix) or two arguments (specifying the number of rows and columns separately).

rand() // => 0.44392202188914254
rand(3) // => [[0.7333405150571339, 0.3597611759299407, 0.8809543481098305], [0.5327545327750203, 0.9185256001032435, 0.7226084132391764], [0.14803039057912748, 0.8924466624235429, 0.40943835774171167]]
rand(3, 3) // => [[0.7333405150571339, 0.3597611759299407, 0.8809543481098305], [0.5327545327750203, 0.9185256001032435, 0.7226084132391764], [0.14803039057912748, 0.8924466624235429, 0.40943835774171167]]

size

Returns the size along each dimension of an array.

size(ones(3, 5)) // => [3, 5]
size([[[1, 2]]]) // => [1, 1, 2]

std

Returns the standard deviation of a 1-D array.

std([1, 2, 3]) // => 1.0

variance

Returns the variance of a 1-D array.

variance([1, 2, 3]) // => 1.0

zeros

Create an matrix filled with ones. Can be called with either one argument (creating a square matrix) or two arguments (specifying the number of rows and columns separately).

zeros(3) // => [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]
zeros(3, 3) // => [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]

Structs

Package for scientific computing

Functions

This provides the ability to easily evaluate a line (or lines) of code without explicitly setting up a script engine