Crate mocktave

source ·
Expand description

Github CI Crates.io docs.rs

Access Octave/MATLAB from Rust

As much as I hate to say it, there is a lot of useful code living in .m files. Sometimes it can be nice to access that code through Rust. There are at least two use cases I can think of:

  1. Rapid Development: There might be a simple function in Octave that would require significant development effort to replicate in Rust. This crate serves as a stopgap measure to enable further development.
  2. Robust Testing: We all know that the better option is to rewrite those nasty .m files in Rust so they’re 🚀Blazingly Fast™️🚀! This create is still useful for testing purposes, allowing direction comparison to legacy Octave/MATLAB code.

Requirements

This crate uses a disgusting hack: Octave is run in the background in Docker. For that reason, you must have a working installation of Docker.

Example Usage

Let’s say we need a function to compute prime numbers, but we’re too lazy to write one ourselves. Let’s make a thin wrapper around the Octave primes function! That function will look like this:

fn primes(less_than_n: usize) -> Vec<Vec<f64>> {
    mocktave::eval(                // Start an evaluation
            &format!(              // Format the command
                "x = primes({});", // This is where we call `primes` from Octave
                less_than_n        // Pass through the argument
            )
        )
        .get_matrix_named("x")     // Extract the results matrix. 
        .unwrap()                  // Unwrap to get the value     
}

let all_primes_less_than_100 = primes(100);

Its important to note that this function is definitely NOT 🚀Blazingly Fast™️🚀, since it starts, runs, and closes a Docker container every time its run.

Structs

  • Create a persistent interpreter that can call a single container multiple times, resulting in more efficiency code execution.
  • Contains the workspace that resulted from running the octave command in eval

Functions

  • Evaluate a few lines of Octave code and extract the results.