Scientific computing for Rhai.

Overview

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:

rhai-sci = "0.1.7"

To use the bleeding edge instead, add this:

rhai-sci = { 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();

Features

The build size with --no-default-features is approximately 1.8 MB.

Feature Included by default? Size added to release build Description
metadata No 0.3 MB Enables exporting function metadata and is necessary for running doc-tests on Rhai examples.
io Yes 0.5 MB Enables the read_matrix function but pulls in several additional dependencies (polars, url, temp-file, csv-sniffer, minreq). Also actives the nalgebra feature.
nalgebra Yes 0.3 MB Enables several functions (regress, inv, mtimes, horzcat, vertcat, and repmat) but brings in the nalgebra and linregress libraries.
rand Yes 0.1 MB Enables the rand function for generating random FLOAT values and random matrices, but brings in the rand library.
Comments
  • Why write functions in Rhai?

    Why write functions in Rhai?

    Is there a particular purpose of writing the functions in Rhai script?

    Otherwise, this package would really be a very useful one for many people...

    But having to run scripts for every function will probably make it too slow for most applications?

    opened by schungx 7
  • Update url links and remove features to rhai.

    Update url links and remove features to rhai.

    It may not be a good idea to pull in features for rhai, since cargo will merge these features to the user's.

    metadata and serde are harmless, but will bloat the user's build. unchecked, however, will turn off the user's error checking, which may not be what he wants.

    I am not sure if there is any solid dependency on any rhai feature. It seems that unchecked is not really needed, but metadata and serde are needed only to run doc-tests.

    Nevertheless, if you move the functions into Rust, I suppose most of the dependencies will go away since the custom build step won't be necessary.

    opened by schungx 3
  • cleaning up imports

    cleaning up imports

    @cmccomb looking good! Especially making lots of dependencies option.

    I'd suggest adding a feature to gate rustyline, since it may not be always necessary unless the user is writing a REPL.

    Also, maybe a feature to gate rand -- but since this is a scientific computing package, maybe random numbers should be standard...

    Originally posted by @schungx in https://github.com/rhaiscript/rhai-sci/issues/9#issuecomment-1207113418

    opened by cmccomb 2
  • Only build documentation with flag

    Only build documentation with flag

    Right now, unfortunately due to the need to build documentation in lib.rs, the build script requires metadata on the rhai crate.

    This means that all users of the library will be forced to include metadata in all their builds, greatly bloating binary size.

    I suggest making a new feature flag, which pulls in rhai/metadata for full docs; the default without this flag simply skips the documentation building step in build.rs.

    We can put this inside Cargo.toml to make sure that crate docs have the documentation:

    [package.metadata.docs.rs]
    features = ["metadata" ]
    
    opened by schungx 2
  • Checking the first element of array

    Checking the first element of array

    In some of the array functions, I see code that checks the first element and then assumes the rest of the array to be of the same type (usually by checking whether we're working with an INT or FLOAT array):

    if arr[0].is::<INT>() {
        :
    } else if arr[0].is::<FLOAT>() {
        :
    } ...
    

    This is perhaps necessary for statically-typed languages, but Rhai is dynamic so this restriction is rarely necessary as the users may expect to be able to freely mix INT with FLOAT.

    For example, we can consider to do this instead: Assume the array is an INT if and only if all elements are INT. Otherwise, convert to FLOAT array. This is consistent with treatment of FLOAT in Rhai, which is: a + b is only INT if and only if a and b are both INT.

    let (ints, floats) = arr.iter().fold((0, 0), |(i, f), x| {
        if x.is::<INT>() {
            (i + 1, f)
        } else if x.is::<FLOAT>() {
            (i, f + 1)
        } else {
            (i, f)
        }
    });
    
    if ints + floats < arr.len() {
        // raise error as some elements are not numeric
    } else if floats == 0 {
        let i_vec = arr.iter().map(|x| x.as_int().unwrap()).collect::<Vec<_>>();
    } else {
        let f_vec = arr
            .iter()
            .map(|x| {
                if x.is::<FLOAT>() {
                    x.as_float().unwrap()
                } else {
                    x.as_int().unwrap() as FLOAT
                }
            })
            .collect::<Vec<_>>();
    }
    
    opened by schungx 2
  • Types/size checks and cleanup

    Types/size checks and cleanup

    Added types and size checks, primarily for functions that accept Array or Dynamic inputs. Also removed a substantial amount of duplication.

    Closes #5.

    opened by cmccomb 1
  • Add metadata feature.

    Add metadata feature.

    For some reason, using resolver = "2" doesn't work, probably due to the sub-dependency on rhai_codegen. Which is a pity.

    However, it is standard to have a metadata feature on packages to turn on/off docs.

    opened by schungx 0
  • i64 and f64

    i64 and f64

    In constants.rs and cumulative.rs, I see the primary numeric types used are i64 and f64. There are also a couple of checks in matrices_and_arrays.rs that checks for ::is<i64>(). I'm wondering if that's intentional. It should be better to use INT and FLOAT as much as possible, so that the user can use this library on 32-bit systems.

    opened by schungx 0
  • There is a `pprint` function in the example script, I wonder if that's a typo...

    There is a `pprint` function in the example script, I wonder if that's a typo...

    There is a pprint function in the example script, I wonder if that's a typo...

    image

    Originally posted by @schungx in https://github.com/rhaiscript/rhai-sci/pull/11#issuecomment-1207418837

    opened by cmccomb 0
Owner
Rhai - Embedded scripting language and engine for Rust
A small, fast, easy-to-use scripting language and evaluation engine that integrates tightly with Rust. Builds for most common targets including no-std and WASM.
Rhai - Embedded scripting language and engine for Rust
Provides filesystem access for the Rhai scripting language.

About rhai-fs This crate provides filesystem access for the Rhai scripting language. Usage Cargo.toml [dependencies] rhai-fs = "0.1.2" Rhai script //

Rhai - Embedded scripting language and engine for Rust 6 Dec 5, 2022
A GSL (the GNU Scientific Library) binding for Rust

rust-GSL A Rust binding for the GSL library (the GNU Scientific Library). The minimum support Rust version is 1.54. Installation This binding requires

Guillaume Gomez 157 Dec 15, 2022
Command Line Scientific Calculator. Free Forever. Made with โค๏ธ using ๐Ÿฆ€

โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•— โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•— โ–ˆโ–ˆโ•”โ•โ•โ•โ•โ•โ–ˆโ–ˆโ•”โ•โ•โ•โ•โ•โ–ˆโ–ˆโ•”โ•โ•โ•โ•โ• โ–ˆโ–ˆโ•‘ โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—โ–ˆโ–ˆโ•‘ โ–ˆโ–ˆโ•‘ โ•šโ•โ•โ•โ•โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•‘ โ•šโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•‘โ•šโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•— โ•šโ•โ•โ•โ•โ•โ•โ•šโ•โ•โ•โ•โ•โ•โ• โ•šโ•โ•โ•โ•โ•โ• -

zahash 17 Oct 28, 2023
Command line tool for computing gigantic correlation matrices

turbocor This is little program designed to compute (sparse) Pearson correlation matrices as efficiently as possible. The main ideas are thus: Pairwis

Daniel C. Jones 31 Nov 5, 2021
Scientific Computing Library in Rust

SciRust Scientific computing library written in Rust programming language. The objective is to design a generic library which can be used as a backbon

In Digits 242 Dec 16, 2022
Rhai - An embedded scripting language for Rust.

Rhai - Embedded Scripting for Rust Rhai is an embedded scripting language and evaluation engine for Rust that gives a safe and easy way to add scripti

Rhai - Embedded scripting language and engine for Rust 2.4k Dec 29, 2022
Rhai - An embedded scripting language for Rust.

Rhai is an embedded scripting language and evaluation engine for Rust that gives a safe and easy way to add scripting to any application.

Rhai - Embedded scripting language and engine for Rust 2.4k Jan 3, 2023
A plugin system for the Rhai embedded scripting language.

Rhai Dylib This crate exposes a simple API to load dylib Rust crates in a Rhai engine using Rhai modules. ?? This is a work in progress, the API is su

Rhai - Embedded scripting language and engine for Rust 6 Dec 27, 2022
Provides filesystem access for the Rhai scripting language.

About rhai-fs This crate provides filesystem access for the Rhai scripting language. Usage Cargo.toml [dependencies] rhai-fs = "0.1.2" Rhai script //

Rhai - Embedded scripting language and engine for Rust 6 Dec 5, 2022
a super fast scientific calculator with dimensional analysis support written in Rust ๐Ÿฆ€

larvae a super fast scientific calculator with dimensional analysis support written in Rust ?? ?? heavily inspired from insect Usage: Command mode: $

John McAvoy 2 Jan 10, 2022
Time related types (and conversions) for scientific and astronomical usage.

astrotime Time related types (and conversions) for scientific and astronomical usage. This library is lightweight and high performance. Features The f

Michael Dilger 3 Aug 22, 2022
A GSL (the GNU Scientific Library) binding for Rust

rust-GSL A Rust binding for the GSL library (the GNU Scientific Library). The minimum support Rust version is 1.54. Installation This binding requires

Guillaume Gomez 157 Dec 15, 2022
Command Line Scientific Calculator. Free Forever. Made with โค๏ธ using ๐Ÿฆ€

โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•— โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•— โ–ˆโ–ˆโ•”โ•โ•โ•โ•โ•โ–ˆโ–ˆโ•”โ•โ•โ•โ•โ•โ–ˆโ–ˆโ•”โ•โ•โ•โ•โ• โ–ˆโ–ˆโ•‘ โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—โ–ˆโ–ˆโ•‘ โ–ˆโ–ˆโ•‘ โ•šโ•โ•โ•โ•โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•‘ โ•šโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•‘โ•šโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•— โ•šโ•โ•โ•โ•โ•โ•โ•šโ•โ•โ•โ•โ•โ•โ• โ•šโ•โ•โ•โ•โ•โ• -

zahash 17 Oct 28, 2023
Secure and fast microVMs for serverless computing.

Our mission is to enable secure, multi-tenant, minimal-overhead execution of container and function workloads. Read more about the Firecracker Charter

firecracker-microvm 20.3k Jan 8, 2023
GraphScope: A One-Stop Large-Scale Graph Computing System from Alibaba

A One-Stop Large-Scale Graph Computing System from Alibaba GraphScope is a unified distributed graph computing platform that provides a one-stop envir

Alibaba 2.2k Jan 1, 2023
Command line tool for computing gigantic correlation matrices

turbocor This is little program designed to compute (sparse) Pearson correlation matrices as efficiently as possible. The main ideas are thus: Pairwis

Daniel C. Jones 31 Nov 5, 2021
An End-to-End Privacy Computing Protocol on Layer 2

Eigen Network Eigen Network is an end-to-end privacy computation network for a better digital economy based on hybrid privacy computation protocols an

Eigen Lab 24 Oct 13, 2022
Secure and fast microVMs for serverless computing.

Our mission is to enable secure, multi-tenant, minimal-overhead execution of container and function workloads. Read more about the Firecracker Charter

firecracker-microvm 20.3k Jan 1, 2023
Damavand is a quantum circuit simulator. It can run on laptops or High Performance Computing architectures, such CPU distributed architectures or multi GPU distributed architectures.

Damavand is a quantum circuit simulator. It can run on laptops or High Performance Computing architectures, such CPU distributed architectures or multi GPU distributed architectures.

MichelNowak 0 Mar 29, 2022