1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
use crate::size::Size; use crate::Matrix; use std::io; use num_traits::Num; // Quick private macro to create a new Matrix class macro_rules! new { ($height:ident, $width:ident, $body:expr) => { Matrix { height: $height, width: $width, data: $body, } }; } impl<T: Num + Clone + Copy> Matrix<T> { /// Creates a new identity matrix of size `N * N` /// ``` /// use mtrs::Matrix; /// /// let matrix: Matrix<i32> = Matrix::identity(2); /// /// assert_eq!(matrix.as_slice(), &[1, 0, 0, 1]); /// assert_eq!(matrix.size(), (2, 2)); /// ``` pub fn identity(size: usize) -> Self { Self::diag(vec![T::one(); size]) } /// Creates a new matrix from a pre-given size, passing a 2d `Vec<T>` /// ``` /// use mtrs::Matrix; /// /// let matrix = Matrix::from_vec((2, 2), vec![1, 2, 7, 6]); /// /// assert_eq!(matrix.as_slice(), &[1, 2, 7, 6]); /// ``` pub fn from_vec<S: Size>(size: S, body: Vec<T>) -> Self { let (height, width) = size.dim(); new!(height, width, body) } /// Creates a new matrix from a slice /// ``` /// use mtrs::Matrix; /// /// let matrix = Matrix::from_slice((2, 2), &[1, 2, 7, 6]); /// /// assert_eq!(matrix.as_slice(), &[1, 2, 7, 6]); /// assert_eq!(matrix[(1, 0)], 7); /// ``` pub fn from_slice<S: Size>(size: S, body: &[T]) -> Self { let (height, width) = size.dim(); new!(height, width, body.to_vec()) } /// Create a `Matrix<T>` of size `M * N` filled with `0`s /// ``` /// use mtrs::Matrix; /// /// let matrix = Matrix::zeros(2); /// /// assert_eq!(matrix.as_slice(), &[0, 0, 0, 0]); /// assert_eq!(matrix, Matrix::from_vec(2, vec![0, 0, 0, 0])); /// ``` pub fn zeros<S: Size>(size: S) -> Self { let (height, width) = size.dim(); new!(height, width, vec![T::zero(); width * height]) } /// Create a `Matrix<T>` of size `M * N` filled with `1`s /// ``` /// use mtrs::Matrix; /// /// let matrix = Matrix::ones(2); /// /// assert_eq!(matrix.as_slice(), &[1, 1, 1, 1]); /// assert_eq!(matrix, Matrix::from_vec(2, vec![1, 1, 1, 1])); /// ``` pub fn ones<S: Size>(size: S) -> Self { let (height, width) = size.dim(); new!(height, width, vec![T::one(); width * height]) } /// Create a square matrix with a diagonal (all other values initialized at 0) /// ``` /// use mtrs::Matrix; /// /// let matrix = Matrix::diag(vec![1, 2, 3]); /// /// assert_eq!(matrix.as_slice(), &[1, 0, 0, 0, 2, 0, 0, 0, 3]); /// assert_eq!(matrix.size(), (3, 3)); /// ``` pub fn diag(diagonal: Vec<T>) -> Self { let mut m = Self::zeros(diagonal.len()); for (i, val) in diagonal.iter().enumerate() { m.set(i, *val).expect("Something went wrong"); } m } /// Returns a tuple representing the dimensions (`(height, width)`) /// ``` /// use mtrs::Matrix; /// /// let matrix: Matrix<i32> = Matrix::ones((2, 3)); /// assert_eq!(matrix.size(), (2, 3)); /// ``` pub fn size(&self) -> (usize, usize) { (self.height, self.width) } /// Wrapper function for `self.data.as_slice()` pub fn as_slice(&self) -> &[T] { self.data.as_slice() } /// Wrapper function for `self.data.as_mut_slice()` pub fn as_mut_slice(&mut self) -> &mut [T] { self.data.as_mut_slice() } /// Wrapper function for `self.data.as_ptr()` pub fn as_ptr(&self) -> *const T { self.data.as_ptr() } /// Wrapper function for `self.data.as_mut_ptr()` pub fn as_mut_ptr(&mut self) -> *mut T { self.data.as_mut_ptr() } /// Zero out the matrix // Safety: Only use when the values inside the matrix can be safely zeroed #[inline] pub unsafe fn erase(&mut self) { std::ptr::write_bytes(self.as_mut_ptr(), 0, self.data.len()); } /// Return a `Vec` representation of the Matrix /// ``` /// use mtrs::Matrix; /// /// let matrix = Matrix::from_vec(2, vec![2, 1, 4, 3]); /// assert_eq!(matrix.as_vec(), vec![vec![2, 1], vec![4, 3]]); /// ``` pub fn as_vec(&self) -> Vec<Vec<T>> { let mut body = Vec::new(); let sliced = self.as_slice(); for row in 0..self.height { let mut row_vec = Vec::new(); for col in 0..self.width { row_vec.push(sliced[row * self.width + col]); } body.push(row_vec); } body } /// Returns a single column of the Matrix /// Returns a `Vec` of all the columns /// ``` /// extern crate mtrs; /// use mtrs::matrix; /// /// let mat = matrix![i32; (3, 2); 1, 2; 3, 4; 5, 6]; /// /// assert_eq!(mat.get_col(0), Some(vec![1, 3, 5])); /// assert_eq!(mat.get_col(3), None) /// ``` pub fn get_col(&self, index: usize) -> Option<Vec<T>> { if index >= self.width { None } else { let mut body = Vec::new(); let sliced = self.as_slice(); for row in 0..self.height { body.push(sliced[row * self.width + index]); } Some(body) } } /// Returns a `Vec` of all the columns /// ``` /// extern crate mtrs; /// use mtrs::matrix; /// /// let mat = matrix![i32; (3, 2); 1, 2; 3, 4; 5, 6]; /// /// assert_eq!(mat.cols(), vec![vec![1, 3, 5], vec![2, 4, 6]]); /// ``` pub fn cols(&self) -> Vec<Vec<T>> { let mut body = Vec::new(); for i in 0..self.width { // We can call `unwrap` here as it is guaranteed to be within bounds body.push(self.get_col(i).unwrap()); } body } /// Returns an entry in the Matrix safely, that is: /// ``` /// extern crate mtrs; /// use mtrs::matrix; /// /// let mat = matrix![i32; (2, 2); 1, 2; 3, 4]; /// /// assert_eq!(mat.get((0, 1)), Some(&2)); /// ``` /// # Failure /// Fails if the location is out of bounds pub fn get<S: Size>(&self, loc: S) -> Option<&T> { let (h, w) = loc.dim(); self.data.get(h * self.width + w) } /// Sets an entry in the Matrix /// ``` /// #[macro_use] extern crate mtrs; /// use mtrs::Matrix; /// /// let mut mat = matrix![(2, 3); 1, 2, 3; 4, 5, 6]; /// mat.set(1, 13); /// assert_eq!(mat.as_slice(), &[1, 2, 3, 4, 13, 6]); /// ``` /// # Failure /// Fails if you attempt to set a value that is out of bounds pub fn set<S: Size>(&mut self, loc: S, val: T) -> io::Result<()> { let (h, w) = loc.dim(); if h >= self.height || w >= self.width { Err(io::Error::new(io::ErrorKind::InvalidData, "Invalid bounds")) } else { self.data[h * self.width + w] = val; Ok(()) } } /// Resizes the Matrix to any size, with all new values initialized to `0` /// ``` /// #[macro_use] extern crate mtrs; /// /// let mut mat = matrix![(2, 3); 1, 2, 3; 4, 5, 6]; /// mat.resize((2, 2)); /// assert_eq!(mat.as_slice(), &[1, 2, 4, 5]); /// ``` pub fn resize<S: Size>(&mut self, size: S) { let (height, width) = size.dim(); if self.height != height { self.data.resize(self.height * self.width + (height - self.height) * self.width, T::zero()); self.height = height; } if self.width != width { let mut new_dat = self.as_vec(); for entry in &mut new_dat { let len = entry.len(); entry.resize(len + width - self.width, T::zero()); } self.width = width; self.data = new_dat.iter().flat_map(|entry| entry.iter().copied()).collect(); } } } #[cfg(test)] mod matrix_tests { use super::Matrix; #[test] fn test_cols() { let matrix1: Matrix<i32> = Matrix::identity(1); let matrix2: Matrix<i32> = Matrix::identity(2); let matrix3: Matrix<i32> = Matrix::identity(3); assert_eq!(matrix1.get_col(0), Some(vec![1])); assert_eq!(matrix2.get_col(0), Some(vec![1, 0])); assert_eq!(matrix2.get_col(1), Some(vec![0, 1])); assert_eq!(matrix3.get_col(0), Some(vec![1, 0, 0])); assert_eq!(matrix3.get_col(1), Some(vec![0, 1, 0])); assert_eq!(matrix3.get_col(2), Some(vec![0, 0, 1])); assert_eq!(matrix1.cols(), vec![vec![1]]); assert_eq!(matrix2.cols(), vec![vec![1, 0], vec![0, 1]]); assert_eq!( matrix3.cols(), vec![vec![1, 0, 0], vec![0, 1, 0], vec![0, 0, 1]] ); } #[test] fn test_empty_matrix() { let matrix0: Matrix<i32> = Matrix::zeros(0); assert_eq!(matrix0.size(), (0, 0)); assert_eq!(matrix0.get_col(0), None); assert_eq!(matrix0.as_slice(), &[]); assert_eq!(matrix0.scalar_add(0), matrix0); } #[test] fn test_erase() { let mut matrix: Matrix<i32> = Matrix::ones(2); unsafe { matrix.erase(); } assert_eq!(matrix, Matrix::<i32>::zeros(2)); } #[test] fn test_diagonal() { let matrix = Matrix::diag(vec![3, 7, 8, 1]); assert_eq!(matrix.get_col(1), Some(vec![0, 7, 0, 0])); assert_eq!(matrix.size(), (4, 4)); assert_eq!(matrix.get(2), Some(&8)); } #[test] fn test_resize() { let mut matrix: Matrix<i32> = Matrix::identity(2); assert_eq!(matrix.size(), (2, 2)); matrix.resize((3, 4)); assert_eq!(matrix.size(), (3, 4)); assert_eq!(matrix.as_slice(), &[1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0]); } }