18 releases (6 breaking)

0.7.5 Dec 9, 2024
0.7.1 Nov 29, 2024

#210 in Math

Download history 461/week @ 2024-11-11 724/week @ 2024-11-18 437/week @ 2024-11-25 320/week @ 2024-12-02 356/week @ 2024-12-09

1,902 downloads per month
Used in numdiff

MIT/Apache

73KB
876 lines

linalg-traits

github crates.io docs.rs

Traits for generic linear algebra.

Documentation

Please see https://docs.rs/linalg-traits.

Examples

Let's define a function that takes in a vector and returns a new vector with all the elements repeated twice. Using the Scalar and Vector traits, we can write it in a way that makes it independent of what types we use to represent scalars and vectors.

use linalg_traits::{Scalar, Vector};
use ndarray::{array, Array1};
use numtest::*;

// Define the function for repeating the elements.
fn repeat_elements<S: Scalar, V: Vector<S>>(v: &V) -> V {
    // Create a new vector of the same type but with twice the length.
    let mut v_repeated = V::new_with_length(v.len() * 2);

    // Populate the vector.
    for i in 0..v.len() {
        v_repeated[2 * i] = v[i];
        v_repeated[2 * i + 1] = v[i];
    }

    v_repeated
}

// Define the vector to be repeated.
let v: Array1<f64> = array![1.0, 2.0, 3.0];

// Repeat the elements.
let v_repeated: Array1<f64> = repeat_elements(&v);

// Check that the elements were properly repeated.
assert_arrays_equal!(v_repeated, [1.0, 1.0, 2.0, 2.0, 3.0, 3.0]);

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~0.1–1.2MB
~22K SLoC