Statically sized matrix using a definition with const generics

Overview

matrix

status

Statically sized matrix using a definition with const generics (only for nightly)

Getting Started

  1. docker compose up --detach
  2. docker container exec --interactive --tty nightly bash

Test

use cargo nextest run instead of cargo test

Examples

#![allow(incomplete_features)]
#![feature(generic_const_exprs)]

use matrix::heap::*;
use matrix::matrix::*;
// use matrix::multi_dim::*;

fn main() {
    // A + B in stack
    let left = Matrix::<2, 3, _, i32>::new([
        1, 2, 3, //
        4, 5, 6,
    ]);
    let right = Matrix::<2, 3, _, i32>::new([
        1, 2, 3, //
        4, 5, 6,
    ]);
    assert_eq!(
        left + right,
        Matrix::<2, 3, _, i32>::new([
            2, 4, 6, //
            8, 10, 12
        ])
    );

    // A + B in heap
    let left = Matrix::<2, 3, _, _>::new(Heaped::<2, 3, i32>::new(Box::new([
        1, 2, 3, //
        4, 5, 6,
    ])));
    let right = Matrix::<2, 3, _, _>::new(Heaped::<2, 3, i32>::new(Box::new([
        1, 2, 3, //
        4, 5, 6,
    ])));
    assert_eq!(
        left + right,
        Matrix::<2, 3, _, i32>::new([
            2, 4, 6, //
            8, 10, 12
        ])
    );

    // A in heap + B in stack
    let left = Matrix::<2, 3, _, _>::new(Heaped::<2, 3, i32>::new(Box::new([
        1, 2, 3, //
        4, 5, 6,
    ])));
    let right = Matrix::<2, 3, _, i32>::new([
        1, 2, 3, //
        4, 5, 6,
    ]);
    assert_eq!(
        left + right,
        Matrix::<2, 3, _, i32>::new([
            2, 4, 6, //
            8, 10, 12
        ])
    );

    // A * B
    let left = Matrix::<2, 3, _, u32>::new([
        3, 7, 2, //
        2, 4, 3,
    ]);
    let right = Matrix::<3, 3, _, u32>::new([
        2, 1, 4, //
        9, 2, 7, //
        8, 3, 2,
    ]);
    assert_eq!(
        left * right,
        Matrix::<2, 3, _, u32>::new([
            85, 23, 65, //
            64, 19, 42
        ])
    );

    // LU decomposition
    let matrix = Matrix::<10, 10, _, f64>::new([
        3.4, 5.3, 2.4, 4.7, 7.89, 3.2, 3.5, 2.1324, 3.0, 3.4, //
        1.4, 5.4, 2.4, 4.7, 7.89, 3.2, 4.5, 2.1324, 3.0, 3.4, //
        2.4, 5.5, 2.4, 4.7, 7.89, 3.2, 2.5, 2.1324, 3.0, 3.4, //
        3.4, 5.6, 2.4, 4.7, 7.89, 3.2, 4.5, 2.1324, 3.0, 3.4, //
        3.4, 5.9, 2.4, 4.7, 7.89, 3.2, 5.5, 2.1324, 3.0, 3.4, //
        5.4, 4.3, 2.4, 4.7, 7.89, 3.2, 4.5, 2.1324, 3.0, 3.4, //
        6.4, 3.3, 2.4, 4.7, 7.89, 3.2, 7.5, 2.1324, 3.0, 3.4, //
        7.4, 1.3, 2.4, 4.7, 7.89, 3.2, 9.5, 2.1324, 3.0, 3.4, //
        8.4, 2.3, 2.4, 4.7, 7.89, 3.2, 4.5, 2.1324, 3.0, 3.4, //
        9.4, 3.3, 2.4, 4.7, 7.89, 3.2, 1.5, 2.1324, 3.0, 3.4, //
    ]);
    let (l, u) = matrix.lu_decomposition();
    let diff = matrix - l * u;
    diff.map::<_, _, [(); 10 * 10]>(|e| assert!(e.abs() < 1e-10));

    // Solve Ax = b with LU Decomposition
    //
    // 2a + 2b - 4c + 5d = 16
    //  a +  b +  c +  d = 10
    // -a + 2b - 3c -  d = -2
    //  a + 2b + 3c - 4d = -2
    //
    // (a, b, c, d) = (1, 2, 3, 4)
    let a = Matrix::<4, 4, _, f64>::new([
        2.0, 3.0, -4.0, 5.0, //
        1.0, 1.0, 1.0, 1.0, //
        -1.0, 2.0, -3.0, 1.0, //
        1.0, 2.0, 3.0, -4.0,
    ]);
    let b = Matrix::<4, 1, _, f64>::new([
        16.0, //
        10.0, //
        -2.0, //
        -2.0,
    ]);
    let x = solve_eqn(a, b);
    assert!((1.0 - x.0[0]).abs() < 1e-10);
    assert!((2.0 - x.0[1]).abs() < 1e-10);
    assert!((3.0 - x.0[2]).abs() < 1e-10);
    assert!((4.0 - x.0[3]).abs() < 1e-10);
}
You might also like...
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

Using OpenAI Codex's "davinci-edit" Model for Gradual Type Inference

OpenTau: Using OpenAI Codex for Gradual Type Inference Current implementation is focused on TypeScript Python implementation comes next Requirements r

Find faces in photo using Rustface.
Find faces in photo using Rustface.

facegrep I want to learn Rust and found about Rustface (which is a port of C++ SeetaFace Engine). It detects faces in a picture. What interesting is i

Simulation of sand falling down in a cave built using nannou (Rust)
Simulation of sand falling down in a cave built using nannou (Rust)

nannou-sand-simulation Learning nannou, an open-source creative-coding toolkit for Rust, by implementing a visualization for a simulation of sand fall

A repo for learning how to parallelize computations in the GPU using Apple's Metal, in Rust.

Metal playground in rust Made for learning how to parallelize computations in the GPU using Apple's Metal, in Rust, via the metal crate. Overview The

A browser AI agent, using GPT-4

A browser AI agent, using GPT-4 This project provides a bridge between GPT-4 and a headless Chromium browser, allowing you to automate actions simply

Sample Python extension using Rust/PyO3/tch to interact with PyTorch

Python extensions using tch to interact with PyTorch This sample crate shows how to use tch to write a Python extension that manipulates PyTorch tenso

A Voice Activity Detector rust library using the Silero VAD model.

Voice Activity Detector Provides a model and extensions for detecting speech in audio. Standalone Voice Activity Detector This crate provides a standa

Rust Uint crate using const-generics

Rust uint crate using const-generics Implements [UintBITS, LIMBS], the ring of numbers modulo $2^{\mathtt{BITS}}$ . It requires two generic argument

An alternative to `qcell` and `ghost-cell` that instead uses const generics

Purpose This crate is another attempt at the ghost-cell / qcell saga of cell crates. This provides an alternative to std::cell::RefCell that can allow

A statically-typed, interpreted programming language, with generics and type inference

Glide A programming language. Currently, this includes: Static typing Generics, with monomorphization Type inference on function calls func identityT

Const equivalents of many [`bytemuck`] functions, and a few additional const functions.

Const equivalents of many bytemuck functions, and a few additional const functions. constmuck uses bytemuck's traits, so any type that implements thos

fast rust implementation of online nonnegative matrix factorization as laid out in the paper "detect and track latent factors with online nonnegative matrix factorization"

ONMF status: early work in progress. still figuring this out. code still somewhat messy. api still in flux. fast rust implementation of online nonnega

This crate allows you to safely initialize Dynamically Sized Types (DST) using only safe Rust.

This crate allows you to safely initialize Dynamically Sized Types (DST) using only safe Rust.

This library implements a type macro for a zero-sized type that is Serde deserializable only from one specific value.

Monostate This library implements a type macro for a zero-sized type that is Serde deserializable only from one specific value. [dependencies] monosta

replaces fixed-sized string prefixes & whole sections in binaries for fast, debuggable, reproducible builds

Replacing fixed-sized string prefixes in binaries to refix them to their build context Here's the long story about what refix does and why you'd want

Owned container for dynamically-sized types backed by inline memory

sized-dst This crate provides Dst, an owned container for dynamically-sized types (DSTs) that's backed by inline memory. The main use-case is owned tr

Utf8 to utf16 conversion functions for use in const contexts

const-utf16 utf8 to utf16 conversion functions useable in const contexts. Use const HELLO_WORLD_UTF16: &[u16]= const_utf16::encode!("Hello, world!");

const panic with formatting

For panicking with formatting in const contexts. This library exists because the panic macro was stabilized for use in const contexts in Rust 1.57.0,

Comments
  • Compute Gaussian elimination on the stack

    Compute Gaussian elimination on the stack

    Use stack instead of heap to speed up calcutaion.

    Now, we use heap in forward_erase for easy swapping. But we can use stack by managing indeces separetely.

    enhancement 
    opened by den-taku 1
Owner
DenTaku
Hello, world!!
DenTaku
TP - Optimization of the energy consumption of a Matrix Multiplication

Optimization de la consommation: Multiplication de matrices Nous allons travailler sur la multiplication de matrices denses. C’est un algorithme class

Bynawers 0 Mar 11, 2022
🧮 alphatensor matrix breakthrough algorithms + simd + rust.

simd-alphatensor-rs tldr; alphatensor matrix breakthrough algorithims + simd + rust. This repo contains the cutting edge matrix multiplication algorit

drbh 50 Feb 11, 2023
General matrix multiplication with custom configuration in Rust. Supports no_std and no_alloc environments.

microgemm General matrix multiplication with custom configuration in Rust. Supports no_std and no_alloc environments. The implementation is based on t

null 4 Nov 6, 2023
Msgpack serialization/deserialization library for Python, written in Rust using PyO3, and rust-msgpack. Reboot of orjson. msgpack.org[Python]

ormsgpack ormsgpack is a fast msgpack library for Python. It is a fork/reboot of orjson It serializes faster than msgpack-python and deserializes a bi

Aviram Hassan 139 Dec 30, 2022
A real-time implementation of "Ray Tracing in One Weekend" using nannou and rust-gpu.

Real-time Ray Tracing with nannou & rust-gpu An attempt at a real-time implementation of "Ray Tracing in One Weekend" by Peter Shirley. This was a per

null 89 Dec 23, 2022
HNSW ANN from the paper "Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs"

hnsw Hierarchical Navigable Small World Graph for fast ANN search Enable the serde feature to serialize and deserialize HNSW. Tips A good default for

Rust Computer Vision 93 Dec 30, 2022
An example of using TensorFlow rust bindings to serve trained machine learning models via Actix Web

Serving TensorFlow with Actix-Web This repository gives an example of training a machine learning model using TensorFlow2.0 Keras in python, exporting

Kyle Kosic 39 Dec 12, 2022
SelfOrgMap 5 Nov 4, 2020
This repository features a simple Kalman filter and RTS smoother (KFS) implementation in Rust by using the ndarray library.

Kalman filter and RTS smoother in Rust (ndarray) This repository features a simple Kalman filter and RTS smoother (KFS) implementation in Rust by usin

SPDEs 3 Dec 1, 2022
Toy library for neural networks in Rust using Vulkan compute shaders

descent Toy library for neural networks in Rust using Vulkan compute shaders. Features Multi-dimensional arrays backed by Vulkan device memory Use Rus

Simon Brown 71 Dec 16, 2022