Static Linear Algebra System

Overview

SLAS

Static Linear Algebra System

Crates.io GitHub Workflow Status Coverage Status Docs Donate on paypal

Provides statically allocated vector, matrix and tensor types, for interfacing with blas/blis, in a performant manor, using copy-on-write (aka cow) behavior by default.

What is BLAS?

Example

use slas::prelude::*;
let a = moo![f32: 1, 2, 3.2];
let b = moo![f32: 3, 0.4, 5];
println!("Dot product of {a:?} and {b:?} is {:?}", a.dot(&b));

You can also choose a static backend yourself

use slas::prelude::*;
let a = moo![on slas_backend::Rust:f32: 1, 2, 3.2];
// This will only use rust code for all operations on a
use slas::prelude::*;
let a = moo![on slas_backend::Blas:f32: 1, 2, 3.2];
// This will always use blas for all operations on a

The StaticCowVec dereferences to StaticVecUnion, which in turn dereferences to [T; LEN], so any method implemented for [T;LEN] can also be called on StaticCowVec and StaticVecUnion.

More example code here.

What is a cow and when is it useful?

The copy-on-write functionality is inspired by std::borrow::cow. The idea is simply that allocations (and time) can be saved, by figuring out when to copy at runtime instead of at compiletime. This can be memory inefficient at times (as an enum takes the size of its largest field + tag size), which is why you can optionally use StaticVecUnions and StaticVecs instead. You can call moo, moo_ref and mut_moo_ref on any type that implements StaticVec to cast it to a appropriate type for it's use-case, with zero overhead.

moo_ref returns a StaticVecRef, which is just a type alias for a reference to a StaticVecUnion. This is most efficient when you know you don't need mutable access or ownership of a vector.

mut_moo_ref returns a MutStaticVecRef. This is a lot like moo_ref, but is useful when you want to mutate your data in place (fx if you wan't to normalize a vector). You should only use this if you want mutable access to a vector WITH side effects.

moo returns a StaticCowVec that references self. This is useful if you don't know if you need mutable access to you vector and you don't want side effects. If you want to copy data into a StaticCowVec then StaticCowVec::from is what you need.

moo_owned will just return a StaticVecUnion. This is useful when you really just want a [T; LEN], but you need methods only implemented for a StaticVecUnion.

Example of cow behavior

use slas::prelude::*;

let source: Vec<f32> = vec![1., 2., 3.];
let mut v = source.moo();

// Here we mutate v,
// so the content of source will be copied into v before the mutation occours.
v[0] = 0.;

assert_eq!(**v, [0., 2., 3.]);
assert_eq!(source, vec![1., 2., 3.]);

The borrow checker won't allow mutating source after v is created, because assignment to borrowed values is not allowed. This can be a problem in some situations.

use slas::prelude::*;

let mut source: Vec<f32> = vec![1., 2., 3.];
let mut v = unsafe { StaticCowVec::<f32, 3>::from_ptr(source.as_ptr()) };

// Here we can mutate source, because v was created from a raw pointer.
source[1] = 3.;
v[0] = 0.;
source[2] = 4.;

assert_eq!(**v, [0., 3., 3.]);
assert_eq!(source, vec![1., 3., 4.]);

In the example above, you can see v changed value the first time source was mutated, but not the second time. This is because v was copied when it was mutated.

Matrix example

use slas::prelude::*;
use slas_backend::*;

let a = moo![f32: 1..=6].matrix::<Blas, 2, 3>();
let b = moo![f32: 1..=6].matrix::<Blas, 3, 2>();
let c = a.matrix_mul(&b);

assert_eq!(c, [22., 28., 49., 64.]);

println!("{a:.0?} * {b:.0?} = {:.0?}", c.matrix::<Blas, 2, 2>());

Indexing into matricies can be done both with columns and rows first. When indexing with [usize; 2] it will take columns first, where as using m! will be rows first.

use slas::prelude::*;
use slas_backend::*;

let a = moo![f32: 1..=6].matrix::<Blas, 2, 3>();

assert_eq!(a[[0, 1]], a[m![1, 0]]);

Tensor example

At the moment tensors can't do much

use slas::prelude::*;
let t = moo![f32: 0..27].reshape(&[3, 3, 3], slas_backend::Rust);
assert_eq!(t[[0, 0, 1]], 9.);

let mut s = t.index_slice(1);

assert_eq!(s[m![0, 0]], 9.);

That's pretty much it for now...

Why not just use ndarray (or alike)?

Slas can be faster than ndarray in some specific use cases, like when having to do a lot of allocations, or when using referenced data in vector operations. Besides slas should always be atleast as fast as ndarray, so it can't hurt.

Ndarray will always use the backend you choose in your Cargo.toml. With slas you can choose a backend in code and even create your own backend that fits your needs.

Static allocation and the way slas cow behavior works with the borrow checker, also means that you might catch a lot of bugs at compiletime, where ndarray most of the time will let you get away with pretty much anything. For example taking the dot product of two vectors with different sizes, will cause a panic in ndarray and a compiletime error in slas.

Installation

By default slas will assume you have blis installed on your system. If you want tos choose your own blas provider please set dependencies.slas.default-features = false in your Cargo.toml, and refer to blas-src for further instructions. Remember to add extern crate blas_src; if you use blas-src as a blas provider.

On the crates.io version of slas (v0.1.0 and 0.1.1) blis is compiled automatically.

For now, if you want to use the newest version of slas, you need to install blis/blas on your system.

  • On Arch linux blis-cblas v0.7.0 from the AUR has been tested and works fine.
  • On Debian you can simply run apt install libblis-dev.
  • On Windows openblas-src has been tested. This mean you will need to disable slas default features, follow the installation instructions in the openblas readme and add extern crate openblas_src to your main file.

Misc

TODO

Progress and todos are now on trello!

License: Apache-2.0

You might also like...
Linear algebra crate for Rust.

cayley - generic, stack-allocated linear algebra in Rust cayley is a crate that fills a small niche: it provides a generic matrix type that allocates

Docker images for compiling static Rust binaries using musl-libc and musl-gcc, with static versions of useful C libraries. Supports openssl and diesel crates.

rust-musl-builder: Docker container for easily building static Rust binaries Source on GitHub Changelog UPDATED: Major updates in this release which m

Hot reload static web server for deploying mutiple static web site with version control.

SPA-SERVER It is to provide a static web http server with cache and hot reload. 中文 README Feature Built with Hyper and Warp, fast and small! SSL with

Static Web Server - a very small and fast production-ready web server suitable to serve static web files or assets
Static Web Server - a very small and fast production-ready web server suitable to serve static web files or assets

Static Web Server (or SWS abbreviated) is a very small and fast production-ready web server suitable to serve static web files or assets.

serve a static site, single page application or just a static file with Rust
serve a static site, single page application or just a static file with Rust

cargo-server tl;dr: Does the same as "python -m http.server" or "npx serve" but for Rust ecosystem. cargo-server helps you serve a static site, single

Serve a static site, single page application or just a static file with Rust
Serve a static site, single page application or just a static file with Rust

cargo-server tl;dr: Does the same as "python -m http.server" or "npx serve" but for Rust ecosystem. cargo-server helps you serve a static site, single

Experimental type-safe geometric algebra for Rust

Projective Geometric Algebra This library is a Rust code generator, generating the mathematics you need for a geometric algebra library. I made it mos

Rust Statistics and Vector Algebra Library

Rstats Usage Insert rstats = "^1" in the Cargo.toml file, under [dependencies]. Use in source files any of the following structs, as needed: use rstat

Simple type-safe relational algebra evaluator built entirely in Rust

ra-evaluator A simple type-safe relational algebra evaluator. Relational algebra provides the theoretical foundation for relational databases and the

Yet Another Kalman Filter Implementation. As well as Lie Theory (Lie group and algebra) on SE(3). [no_std] is supported by default.

yakf - Yet Another Kalman Filter Yet Another Kalman Filter Implementation, as well as, Lie Theory (Lie group, algebra, vector) on SO(3), SE(3), SO(2),

An implementation of regular expressions for Rust. This implementation uses finite automata and guarantees linear time matching on all inputs.

regex A Rust library for parsing, compiling, and executing regular expressions. Its syntax is similar to Perl-style regular expressions, but lacks a f

Linear Programming for Rust, with an user-friendly API. This crate allows modeling LP problems, and let's you solve them with various solvers.

good_lp A Linear Programming modeler that is easy to use, performant with large problems, and well-typed. use good_lp::{variables, variable, coin_cbc,

Non-Linear Ray Casting

linon Non-Linear Ray Casting References E. Gröller, “Nonlinear Ray Tracing: Visualizing Strange Worlds,” The Visual Computer, vol. 11, no. 5, pp. 263–

Fast conversion between linear float and 8-bit sRGB

fast-srgb8 Small crate implementing fast conversion between linear float and 8-bit sRGB. Includes API for performing 4 simultaneous conversions, which

A neural network model that can approximate any non-linear function by using the random search algorithm for the optimization of the loss function.

random_search A neural network model that can approximate any non-linear function by using the random search algorithm for the optimization of the los

A Rust-powered linear programming library for Python.
A Rust-powered linear programming library for Python.

Dantzig: A Rust-powered LP library for Python Dantzig is a lightweight and concise linear programming solver suitable for small and large-scale proble

A lightweight microkernel/IPC based operating system built with Rust which is not a clone of any existing operating system
A lightweight microkernel/IPC based operating system built with Rust which is not a clone of any existing operating system

Noble Operating System Noble is a lightweight microkernel and IPC based operating system built with Rust which is not a clone of any existing operatin

Elemental System Designs is an open source project to document system architecture design of popular apps and open source projects that we want to study
Elemental System Designs is an open source project to document system architecture design of popular apps and open source projects that we want to study

Elemental System Designs is an open source project to document system architecture design of popular apps and open source projects that we want to study

Experimental package manager/system configurator for system hoppers

mascara An experimental package manager/config initializer tool for system hoppers. mascara.toml [mascara] feature = "Debian" logs = { stdout = "blue"

Comments
  • Matrix::from([f32; 9]) not working.

    Matrix::from([f32; 9]) not working.

    use slas::matrix::*;
    use slas::prelude::*;
    use slas::traits;
    
    fn main() {
        let m1 = Matrix::<i32, 3, 3>::from([1, 2, 3, 4, 5, 6, 7, 8, 9]);
    }
    

    With error: Because std::traits is a private module

    bug 
    opened by Bechiscul 1
Owner
Aksel?
blib blob mit navn er bob
Aksel?
A linear algebra library with excellent type safety

Static L.A. (Linear Algebra) A fast minimal ultra type safe linear algebra library. While ndarray offers no compile time type checking on dimensionali

Jonathan Woollett-Light 2 Feb 21, 2022
Linear algebra crate for Rust.

cayley - generic, stack-allocated linear algebra in Rust cayley is a crate that fills a small niche: it provides a generic matrix type that allocates

Simeon Duwel 6 Apr 3, 2023
Simple type-safe relational algebra evaluator built entirely in Rust

ra-evaluator A simple type-safe relational algebra evaluator. Relational algebra provides the theoretical foundation for relational databases and the

Vincent Wong 4 Aug 8, 2022
A neural network model that can approximate any non-linear function by using the random search algorithm for the optimization of the loss function.

random_search A neural network model that can approximate any non-linear function by using the random search algorithm for the optimization of the los

ph04 2 Apr 1, 2022
Kuintessence - Open Source HPC Computing Orchestration System

Kuintessence is an advanced computing orchestration system designed to revolutionize HPC workload and cluster management, to empower HPC users by allowing them to concentrate on their scientific ideas rather than getting bogged down by HPC environment complications, and enhance research productivity to its fullest potential.

National Supercomputing Center in Jinan 4 Aug 31, 2023
sparse linear algebra library for rust

sprs, sparse matrices for Rust sprs implements some sparse matrix data structures and linear algebra algorithms in pure Rust. The API is a work in pro

Vincent Barrielle 311 Dec 18, 2022
A linear algebra and mathematics library for computer graphics.

cgmath-rs A linear algebra and mathematics library for computer graphics. The library provides: vectors: Vector2, Vector3, Vector4 square matrices: Ma

rustgd 998 Jan 2, 2023
A linear algebra library written in Rust

rulinalg This library is no longer actively maintained The crate is currently on version 0.4.2. Read the API Documentation to learn more. Summary Ruli

James Lucas 264 Jan 2, 2023
A simple and fast linear algebra library for games and graphics

glam A simple and fast 3D math library for games and graphics. Development status glam is in beta stage. Base functionality has been implemented and t

Cameron Hart 953 Jan 3, 2023
A linear algebra library with excellent type safety

Static L.A. (Linear Algebra) A fast minimal ultra type safe linear algebra library. While ndarray offers no compile time type checking on dimensionali

Jonathan Woollett-Light 2 Feb 21, 2022