simp_linalg/vector_impl/mod.rs
1mod mul_impl;
2mod add_impl;
3mod lambda;
4mod map;
5
6use crate::matrix_impl::Matrix;
7
8#[derive(Debug, PartialEq, Eq, Clone, Default)]
9/// The Vector type.
10pub struct Vector<T>
11{
12 list : Vec<T>
13}
14
15impl<T> Vector<T>
16{
17 /// Converts a Vector into a column Matrix, consuming the Vector.
18 ///
19 /// # Examples
20 /// ```
21 /// use simp_linalg::prelude::*;
22 ///
23 /// let vector = vector![1, 2, 3];
24 ///
25 /// let col_matrix = vector.into_col_matrix();
26 ///
27 /// assert_eq!(col_matrix, matrix![
28 /// [1],
29 /// [2],
30 /// [3]
31 /// ]);
32 ///
33 /// ```
34 /// The internal type of the vector does not need to implement traits
35 /// like [Copy] or [Sized] to convert.
36 /// ```
37 /// use simp_linalg::prelude::*;
38 ///
39 /// let vector = vector![vec!["woah"], vec!["what"]];
40 ///
41 /// let col_matrix = vector.into_col_matrix();
42 ///
43 /// assert_eq!(col_matrix, matrix![
44 /// [vec!["woah"]],
45 /// [vec!["what"]]
46 /// ]);
47 /// ```
48 pub fn into_col_matrix(self) -> Matrix<T> {
49 let mut matrix = Vec::with_capacity(self.len());
50
51 for param in self.list {
52 matrix.push(vec![param])
53 }
54
55 Matrix::from(matrix)
56 }
57
58 /// Converts a Vector<T> into a row Matrix<T>, consuming the Vector.
59 ///
60 /// # Examples
61 /// ```
62 /// use simp_linalg::prelude::*;
63 ///
64 /// let vector = vector![1, 2, 3];
65 ///
66 /// let col_matrix = vector.into_row_matrix();
67 ///
68 /// assert_eq!(col_matrix, matrix![
69 /// [1, 2, 3]
70 /// ]);
71 /// ```
72 /// The internal type of the vector does not need to implement traits
73 /// like [Copy] or [Sized] to convert.
74 /// ```
75 /// use simp_linalg::prelude::*;
76 ///
77 /// let vector = vector![vec!["woah"], vec!["what"]];
78 ///
79 /// let col_matrix = vector.into_row_matrix();
80 ///
81 /// assert_eq!(col_matrix, matrix![
82 /// [vec!["woah"], vec!["what"]]
83 /// ]);
84 /// ```
85 pub fn into_row_matrix(self) -> Matrix<T> {
86 Matrix::from(vec![self.list])
87 }
88
89 /// Returns the length of the Vector<T>.
90 pub fn len(&self) -> usize {
91 self.list.len()
92 }
93
94 /// Borrows the list of elements in the Vector<T>.
95 pub fn list(&self) -> &Vec<T> {
96 &self.list
97 }
98
99 /// Unwraps the vector.
100 pub fn into_inner(self) -> Vec<T> {
101 self.list
102 }
103}
104
105/// Converts a [Vec][std::vec::Vec] to a Vector.
106///
107/// This function will never fail since a Vector can
108/// be constructed with any valid [std::vec::Vec]. However,
109/// the methods available are based upon the Vector's internal
110/// type's traits, such as multiplcation and addition.
111///
112/// # Example
113/// ```
114/// use simp_linalg::vector_impl::Vector;
115/// use simp_linalg::vector;
116///
117/// let vector1 = vector![1, 2, 3, 4];
118/// let vector2 = vector![1.7, -0.03];
119///
120/// // Vectors can be constructed with any std::vec::Vec.
121/// let vector3 = vector!["Hello", "I'm in the vector!"];
122/// ```
123impl<T> From<Vec<T>> for Vector<T>
124{
125 fn from(vec: Vec<T>) -> Self {
126 Vector {
127 list : vec
128 }
129 }
130}