Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
88fe400
feat: add matrix helpers
cmccomb Sep 6, 2025
6ae7d92
Merge pull request #1 from cmccomb/codex/create-rhaimatrix-and-rhaive…
cmccomb Sep 7, 2025
4a0b449
Add vector orientation helpers and tests
cmccomb Sep 7, 2025
38ac4a5
Merge pull request #2 from cmccomb/codex/add-row-and-column-vector-co…
cmccomb Sep 7, 2025
7f09b2f
Adjust matrix operation tests for dynamic comparisons
cmccomb Sep 7, 2025
69a7d94
Expose array wrappers for matrix ops
cmccomb Sep 7, 2025
a7d2ba6
Merge pull request #4 from cmccomb/codex/update-matrix-functions-for-…
cmccomb Sep 7, 2025
c36476d
Use matrix methods for repmat
cmccomb Sep 7, 2025
26ab1cb
Merge pull request #5 from cmccomb/codex/update-matrix-functions-for-…
cmccomb Sep 7, 2025
71fcd41
test: add matrix vector orientation tests
cmccomb Sep 8, 2025
25a8c9d
Merge pull request #6 from cmccomb/codex/add-tests-for-matrix-vectors
cmccomb Sep 8, 2025
ae3959c
docs: clarify matrix orientation
cmccomb Sep 8, 2025
c7e7d41
Merge pull request #7 from cmccomb/codex/add-matrix-and-vector-conven…
cmccomb Sep 8, 2025
5b764de
docs: standardize readme and ci
cmccomb Sep 8, 2025
3c644d6
Add matrix inversion example
cmccomb Sep 8, 2025
f305361
ci: remove clippy lint
cmccomb Sep 8, 2025
7e2b46a
Merge pull request #9 from cmccomb/codex/add-examples-to-examples-dir…
cmccomb Sep 8, 2025
4562e15
Merge branch 'master' into codex/improve-consistency-and-documentation
cmccomb Sep 8, 2025
49386eb
Merge pull request #8 from cmccomb/codex/improve-consistency-and-docu…
cmccomb Sep 8, 2025
056b536
test: cover projectile motion example
cmccomb Sep 18, 2025
b0bc83d
Fix meshgrid broadcasting and add tests
cmccomb Nov 16, 2025
9e1a7d5
Fix eye single argument array handling
cmccomb Nov 16, 2025
f5479ab
Merge pull request #11 from cmccomb/codex/update-meshgrid-function-an…
cmccomb Nov 16, 2025
20ac958
Merge pull request #12 from cmccomb/codex/refactor-eye_single_input-a…
cmccomb Nov 16, 2025
cb60008
Merge pull request #10 from cmccomb/codex/add-examples-using-multiple…
cmccomb Nov 16, 2025
2babadc
Fix concatenation orientation checks
cmccomb Nov 16, 2025
776e063
Handle vector inputs in diag
cmccomb Nov 16, 2025
ad2b105
Allow vector-shaped inputs in list helpers
cmccomb Nov 16, 2025
babf752
Merge pull request #13 from cmccomb/codex/update-matrix_functions-for…
cmccomb Nov 16, 2025
7682878
Merge pull request #14 from cmccomb/codex/refactor-matrix_functions-d…
cmccomb Nov 16, 2025
ec3da26
Merge branch 'master' into codex/generalize-validation-for-numeric-lists
cmccomb Nov 16, 2025
822863d
Merge pull request #15 from cmccomb/codex/generalize-validation-for-n…
cmccomb Nov 16, 2025
3b785db
Merge remote-tracking branch 'origin/master' into fork-master
cmccomb Jun 9, 2026
f674b15
Add matrix convention helpers
cmccomb Jun 9, 2026
9a9f9a6
Tighten matrix convention docs and validation tests
cmccomb Jun 10, 2026
798f212
Reject empty matrix convention inputs
cmccomb Jun 10, 2026
5db010b
Add neural network backprop example
cmccomb Jun 10, 2026
525de5f
Fix transpose orientation for column vectors
cmccomb Jun 15, 2026
5a70722
Update rhai-sci version in README
cmccomb Jun 15, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
name: tests
name: ci

on:
push:
branches: [ master ]
branches: [master]
pull_request:
branches: [ master ]
branches: [master]

env:
CARGO_TERM_COLOR: always

jobs:
build:

test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Build
run: cargo build --verbose --all-features
- name: Run tests
run: cargo test --verbose --all-features
- uses: actions/checkout@v4
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
components: rustfmt
- uses: Swatinem/rust-cache@v2
- name: Format
run: cargo fmt --all -- --check
- name: Test
run: cargo test --no-default-features --features rand,nalgebra

108 changes: 84 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,112 @@
[![Crates.io](https://img.shields.io/crates/v/rhai-sci.svg)](https://crates.io/crates/rhai-sci)
[![docs.rs](https://img.shields.io/docsrs/rhai-sci/latest?logo=rust)](https://docs.rs/rhai-sci)

# About `rhai-sci`
# rhai-sci

This crate provides some basic scientific computing utilities for the [`Rhai`](https://rhai.rs/) scripting language,
inspired by languages like MATLAB, Octave, and R. For a complete API reference,
check [the docs](https://docs.rs/rhai-sci).
## What & Why

# Install
`rhai-sci` adds basic scientific computing utilities to the [Rhai](https://rhai.rs/) scripting language. It is inspired by tools such as MATLAB, Octave, and R.

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

Add the crate to your `Cargo.toml`:

```toml
rhai-sci = "0.2.3"
rhai-sci = "0.3.0"
```

# Usage

Using this crate is pretty simple! If you just want to evaluate a single line of [`Rhai`](https://rhai.rs/), then you
only need:
Evaluate a single line of Rhai code:

```rust
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`](https://rhai.rs/) scripting engine, then do this instead:
## Examples

Integrate the package with a persistent Rhai engine:

```rust
use rhai::{Engine, packages::Package, INT};
use rhai::{packages::Package, Engine, 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
See the `examples` directory for more:

- `matrix_inversion.rhai` demonstrates matrix inversion.
- `download_and_regress.rhai` fetches data and performs linear regression.
- `projectile_motion.rhai` uses trigonometry and array utilities to simulate a projectile trajectory.
- `neural_network_backprop.rhai` trains a tiny neural network on XOR with explicit backpropagation.

## Matrix and Vector Conventions

Rhai arrays remain the storage model. Use constructors when shape matters:

```typescript
let values = [1, 2, 3]; // plain Rhai list
let x = vec([1, 2, 3]); // column vector: [[1], [2], [3]]
let c = col([1, 2, 3]); // column vector: [[1], [2], [3]]
let r = row([1, 2, 3]); // row vector: [[1, 2, 3]]
let A = mat([[1, 2], [3, 4]]); // validated matrix
```

For compact matrix literals, use strings with whitespace, commas, and semicolons:

```typescript
let A = mat("1 2; 3 4");
let B = M("1, 2; 3, 4");
let r = R("1 2 3");
let c = C("1; 2; 3");
```

The short aliases keep linear algebra scripts readable:

```typescript
let A = mat("1 2; 3 4");
let x = col([5, 6]);
let y = row([7, 8]);

let z = dot(A, x);
let z2 = A.dot(x);
let At = T(A);
let C = hcat(A, x);
let D = vcat(A, y);
```

### Features

- **metadata** *(disabled)*: export function metadata; required for running doc-tests on Rhai examples.
- **io** *(enabled)*: provides `read_matrix` but pulls in `polars`, `url`, `temp-file`, `csv-sniffer`, and `minreq`.
- **nalgebra** *(enabled)*: enables matrix functions such as `regress`, `inv`, `mtimes`, `horzcat`, `vertcat`, `repmat`, `svd`, `hessenberg`, and `qr` via the `nalgebra` and `linregress` crates.
- **rand** *(enabled)*: adds the `rand` function for generating random values and matrices using the `rand` crate.

## CLI/API reference

The full API is documented on [docs.rs](https://docs.rs/rhai-sci).

## Development

```bash
cargo fmt --all -- --check
cargo test --no-default-features --features rand,nalgebra
```

## Troubleshooting

- Building with the `io` feature enabled pulls in heavy dependencies. Disable default features and enable only what you need if builds are slow.

## License

Licensed under either of

- [MIT license](LICENSE-MIT.txt)
- [Apache License, Version 2.0](LICENSE-APACHE.txt)

| Feature | Default | Description |
|------------|----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `metadata` | Disabled | Enables exporting function metadata and is ___necessary for running doc-tests on Rhai examples___. |
| `io` | Enabled | Enables the [`read_matrix`](#read_matrixfile_path-string---array) function but pulls in several additional dependencies (`polars`, `url`, `temp-file`, `csv-sniffer`, `minreq`). |
| `nalgebra` | Enabled | Enables several functions ([`regress`](#regressx-array-y-array---map), [`inv`](#invmatrix-array---array), [`mtimes`](#mtimesmatrix1-array-matrix2-array---array), [`horzcat`](#horzcatmatrix1-array-matrix2-array---array), [`vertcat`](#vertcatmatrix1-array-matrix2-array---array), [`repmat`](#repmatmatrix-array-nx-i64-ny-i64---array), [`svd`](#svdmatrix-array---map), [`hessenberg`](#hessenbergmatrix-array---map), and [`qr`](#qrmatrix-array---map)) but brings in the `nalgebra` and `linregress` crates. |
| `rand` | Enabled | Enables the [`rand`](#rand) function for generating random FLOAT values and random matrices, but brings in the `rand` crate. |
at your option.
5 changes: 5 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,8 @@ mod functions {

#[cfg(feature = "metadata")]
pub use functions::*;

#[cfg(feature = "metadata")]
pub mod matrix {
include!("src/matrix/mod.rs");
}
4 changes: 4 additions & 0 deletions examples/matrix_inversion.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let m = [[1, 2], [3, 4]];
let inv_m = inv(m);
print(inv_m);
inv_m
21 changes: 21 additions & 0 deletions examples/matrix_inversion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//! Demonstrates computing the inverse of a matrix using rhai-sci.

fn main() {
#[cfg(feature = "nalgebra")]
{
use rhai::{packages::Package, Engine};
use rhai_sci::SciPackage;

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

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

// Run the script that inverts a matrix
let result = engine
.run_file("examples/matrix_inversion.rhai".into())
.expect("script should run");
println!("{:?}", result);
}
}
Loading
Loading