linalg-traits

Crates.iolinalg-traits
lib.rslinalg-traits
version0.1.2
sourcesrc
created_at2024-11-11 00:23:41.809194
updated_at2024-11-11 01:18:05.705331
descriptionTraits for generic linear algebra.
homepage
repositoryhttps://github.com/tamaskis/linalg_traits
max_upload_size
id1443319
size28,555
Tamas Kis (tamaskis)

documentation

https://docs.rs/linalg-traits

README

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 [Vector] trait, we can write it in a way that makes it independent of what struct we use to represent a vector.

use linalg_traits::Vector;
use numtest::*;

// Define the function for repeating the elements.
fn repeat_elements<T: Vector>(v: &T) -> T {
    // Create a new vector of the same type but with twice the length.
    let mut v_repeated = T::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: Vec<f64> = vec![1.0, 2.0, 3.0];

// Repeat the elements.
let v_repeated: Vec<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.
Commit count: 9

cargo fmt