orx-v
Traits to unify all vectors!
The focus of this crate is mainly computation and algorithms. The goal is to allow for a generic algorithm implementation that can be called by many polymorphic vector types having a corresponding practical use case.
You may find an article discussing the motivation here and various examples in the examples folder of the repository.
Traits
Two vector traits are defined: one for immutable vectors and the other one extending it to mutable vectors.
Immutable Vectors: NVec<D, T>
NVec<D, T> is a D
dimensional vector where the scalar elements are of type T
.
At its core, the following define the vectors' shared behavior:
at
: efficient random access by indicescard
: complete knowledge of its sizeall
: efficient serial access over all scalar elements
Random Access
The first shared functionality is defined by the at
method. It is analogous to the index operator of a Vec, however, extended to higher dimensions.
;
Below are some examples for D1
and D2
vectors.
use *;
let v1 = vec!;
assert_eq!;
let v1 = V.d1.constant;
assert_eq!;
let v1 = V.d1.fun;
assert_eq!;
let v2 = vec!;
assert_eq!;
assert_eq!;
let mut v2 = V.d2.sparse;
v2.set;
assert_eq!;
assert_eq!;
assert_eq!;
As the examples reveal, various useful concrete types already implement the vector traits, such as:
- the standard vector, arrays, slices;
- ndarray arrays such as Array1, Array2, etc.;
- sparse vectors;
- caching or memoizing vectors;
- functional vectors, and so on.
Further, due to the abstraction through traits, we can have composed definitions. For instance
Vec<T>
andSparseVec<D1, T>
both implementNVec<D1, T>
.- Then,
Vec<Vec<T>>
andVec<SparseVec<D1, T>>
both implementNVec<D2, T>
. - Actually, any
Vec<V>
whereV: NVec<D1, T>
automatically implementsNVec<D2, T>
.
Cardinality
The second shared functionality is the knowledge of the vector's and all of its children's cardinality, all the way down to scalars. This is provided by the card
method. It is analogous to len
method of common collections; however, extended to provide complete cardinality information rather than only the number of immediate children.
;
Below, you may see examples for D1
vectors. Notice that some vectors are naturally unbounded, such as a constant vector, a functional vector or a sparse vector. The examples also illustrate how to define their bounds whenever needed.
use *;
let v1 = vec!;
assert_eq!;
// unbounded vectors
let sparse_elements: = .into_iter.collect;
let v1 = V.d1.sparse_from;
assert_eq!;
assert_eq!; // never out of bounds
assert_eq!;
assert!;
// add bound to unbounded vectors
let v1 = v1.bounded;
assert_eq!;
assert!;
assert_eq!;
The following D2
examples illustrate how card
method can be used for all lower dimensions.
use *;
let v2 = vec!;
assert_eq!;
assert_eq!;
assert_eq!;
// this is equivalent to
assert_eq!;
// unbounded vectors
let v2 = V.d2.fun;
assert_eq!;
assert!;
assert_eq!;
assert!;
// add rectangular bounds as in matrices
let v2 = V
.d2
.fun
.with_rectangular_bounds;
assert_eq!;
assert_eq!;
assert_eq!;
// add variable bounds as in jagged arrays
let num_elements = vec!; // any NVec<D1, usize>
let v2 = V
.d2
.fun
.with_variable_bounds;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
The num_elements
definition also demonstrates the benefit of abstraction. In this example, we used a Vec, but we could've used any NVec<D1, usize>
implementation. For instance, assume we have a D2
vector where each row has 1000 elements while the last element has 1. We can easily represent this with a functional vector or a sparse vector and avoid allocating the entire vector of number of elements.
Sequential Access
Not to be confused with iter()
method on collections, all
method yields the inner-most scalar elements.
In order to iterate over the immediate children, children
method can be used instead.
use *;
let vec = vec!; // V2
// inner-most elements, scalars
let mut all = vec.all;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
// immediate children belonging to previous dimension (D1 here)
let mut children = vec.children;
assert_eq!;
assert_eq!;
assert_eq!;
assert!;
Mutable Vectors: NVecMut<D, T>
As expected, NVecMut extends NVec
; i.e., NVecMut<D, T>: NVec<D, T>
.
Its core functionality is defined by the at_mut
method.
;
use *;
let mut v1 = vec!;
*v1.at_mut = 7;
v1.set; // if you prefer
let mut v2 = vec!;
*v2.at_mut = 21;
v2.set;
let mut v2 = V.d2.sparse;
*v2.at_mut = 12;
v2.set;
Trait Aliases
The following trait aliases can be used instead, to fix the first generic type parameter on dimension.
<====>
<====>
<====>
<====>
...
V for Vectors!
V is basically the entry point of builders for various vector types of multi dimensional vectors. It is followed by the dimension of the vector to be created, such as V.d1()
or V.d3()
. Next we can call methods to create special vectors such as:
- ConstantVec
V.d1().const(42)
- a vector that yields only 42 for all indices
- EmptyVec
V.d3().empty::<i32>()
- a vector with no elements, zero cardinality
- SparseVec
V.d2().sparse(1000)
- a sparse vector where all elements which are not explicitly set are equal to 1000
- FunVec
V.d2().fun(|[i, j]| euclidean(&locations[i], &locations[j]))
- a lazy vector which computes elements on the fly as requested
- CachedVec
V.d2().fun(|[i, j]| euclidean(&locations[i], &locations[j])).into_cached()
- also a lazy vector vector; however, it caches or memoizes computed elements
Practical Example
To demonstrate when and why these traits might be useful, let's assume that we are implementing the two-opt which is a local search algorithm to solve the traveling salesperson problem. The algorithm takes a tour and keeps modifying it until its distance can no longer be reduced within the two-opt neighborhood. We can have our generic implementation as follows.
use *;
This implementation is not much different than the implementation where we would use Vec<usize>
for a tour and Vec<Vec<u32>>
for a distance matrix.
However, it is much different in the caller side.
We can call this algorithm with a wide range of input types that make sense in different situations.
let n = 100;
let mut tour: = initial_tour;
// complete matrix stored as a V2
// complete matrix stored as a flattened V1
// sparse matrix
let finite_distances: = finite_distances_map;
let distances = V.d2.sparse_from;
let _improvement = two_opt;
// functional matrix
let locations: = get_locations;
let distances = V
.d2
.fun;
let _improvement = two_opt;
// functional matrix: ignore from-to depot (node 0) links
let locations: = get_locations;
let distances = V.d2.fun;
let _improvement = two_opt;
// cached matrix
let locations: = get_locations;
let distances = V
.d2
.fun
.into_cached;
let _improvement = two_opt;
// uniform distances
let distances = V.d2.constant;
let _improvement = two_opt;
Matrices
In addition to vector traits, specialized Matrix<T> and MatrixMut<T> traits are also defined to allow for polymorphic matrix types.
Their interface is naturally very similar to those of V2<T>
and V2Mut<T>
except that they require rectangular bounds.
Any V2
vector with rectangular cardinality can be converted into or viewed as a row-major or column-major matrix by calling into_matrix
or as_matrix
methods of the V2AsMatrix trait.
Further, any V1
vector can be transformed or viewed as a flattened matrix by calling v1_into_matrix
or v1_as_matrix
methods of the V1AsMatrix trait.
Features
Vector trait implementations for vectors in well known external libraries are being included in this crate via features. For instance, you may add "ndarray" feature to be able to use "ndarray::Vector1" as a "V1", or "Vector2" as a "V2", etc.
std is enabled as the default feature, please set "default-features=false" when working in no-std programs.
Contributing
Contributions, ideas and feedback are welcome!
If you notice an error, have a question or think something could be improved, or think certain data types must also implement the vector traits, please open an issue or create a PR.
License
This library is licensed under MIT license. See LICENSE for details.