A generically defined, light-weight linear algebra library for simple addition and multiplication of vectors and matrices.
Documentation: - docs.rs
Add this to your Cargo.toml
[dependencies]
simp_linalg = "0.1.4"
Multiplication and addition of vectors and matrices require that their sizes relative to each other are compatible.
&Vector<T> * T -> Vector<T>
&Matrix<T> * T -> Matrix<T>
&Matrix<T> * &Vector<T> -> Vector<T>
&Matrix<T> * &Matrix<T> -> Matrix<T>
&Vector<T> * &Vector<T> -> T
&Vector<T> + &Vector<T> -> Vector<T>
&Matrix<T> + &Matrix<T> -> Matrix<T>
Technically...
The aforementioned operator overloaded features utilize borrows frequently. This is only necessary if you intend to continue the lifetime of the variable after its use in the calculation (which is likely often).
If this is not a requirement, then borrowing is unneeded and the calculation will work as expected.
For example: ``` use simp_linalg::prelude::*;
//Create two vectors let vector1 = Vector::from(vec![1, 2, 3]);
let vector2 = Vector::from(vec![4, 5, 6]);
// Note: vector2 is dropped after this calculation, but vector1 is not. let dot_prod: i32 = &vector1 * vector2; ```
Why is it dropped?
This is due to Rust's move semantics. Rust's standard library type Vec does not implement the Copy trait, thereby moving the value into the multiplication/addition function when called, and consequently dropped when that function goes out of scope. By borrowing the value, the ownership is returned to the original scope and no value is dropped.
Why allow not borrowing?
This is because it allows for more readable source code.
For instance, suppose you have a vector vector_1
that is transformed by a matrix matrix
, whose result will be summed to another vector vector_2
.
In version 0.1.1 (old):
let result: Vector<i32> = &(&matrix * &vector_1) + &vector_2;
In version 0.1.2+:
let result: Vector<i32> = &matrix * &vector_1 + &vector_2;
Additionally, with the new feature of multiplying vectors and matrices by scalars, this saves the programmer from another unnecessary borrow. Using the example above, suppose now you want to scale vector_2
before it is summed.
In version 0.1.1 (old and hypothetically if scalar multiplication were included):
let result: Vector<i32> = &(&matrix * &vector_1) + &(&vector_2 * 4);
In version 0.1.2+:
let result: Vector<i32> = &matrix * &vector_1 + &vector_2 * 4;
Two main functions, lambda and map, can be used to manipulate the individual values within itself or mesh together two vectors or matrices, in a user-defined way.
Overall, these methods applies a user-defined function to each element of the vector or matrix.
The parameters for the custom function depends on the method called, which there are three: - lambda - Vector/Matrix: Fn(&T) -> T - Uses the corresponding element - lambdaindex - *Vector*: Fn(usize) -> T - Uses the index - *Matrix*: Fn(usize, usize) -> T - Uses the row index and column index - lambdaenumerate - Vector: Fn(usize, &T) -> T - Uses the index and the corresponding element - Matrix: Fn(usize, usize, &T) -> T - Uses the row index, column index, and the corresponding element
``` use simp_linalg::prelude::*;
// initialization
let vector: Vector
let matrix: Matrix
// squaring each element inside let newvector = vector.lambda(|val| val * val); let newmatrix = matrix.lambda(|val| val * val);
// tests asserteq!(newvector, Vector::from(vec![1, 4, 9])); asserteq!(newmatrix, Matrix::from(vec![vec![1,4], vec![9,16]])) ```
Overall, these methods combine two vectors or matrices together using a user-defined function.
The parameters for the custom function mapping depends on the mapping method called, which there are two: - map - Vector/Matrix: Fn(&T, &T) -> T - Uses the two corresponding elements - map_enumerate - Vector: Fn(usize, &T, &T) -> T - Uses the index and the two corresponding elements - Matrix: Fn(usize, usize, &T, &T) -> T - Uses the row index, column index, and the two corresponding elements
``` use simp_linalg::prelude::*;
// initialization
// Note: this process is similar to matrix
// Also: the two vectors or matrices must be the same size
let lhsvector: Vector
// multiplying each corresponding element let meshedvector = lhsvector.map(&rhsvector, |lhsval, rhsval| lhsval * rhs_val);
// test asserteq!(meshedvector, Vector::from(vec![4.5, 8.0])) ```