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 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
use crate::allocator::Allocator; use crate::storage::Storage; use crate::{DefaultAllocator, Dim, Matrix, RowVectorN, Scalar, VectorN, VectorSliceN, U1}; use num::Zero; use simba::scalar::{ClosedAdd, Field, SupersetOf}; /// # Folding on columns and rows impl<N: Scalar, R: Dim, C: Dim, S: Storage<N, R, C>> Matrix<N, R, C, S> { /// Returns a row vector where each element is the result of the application of `f` on the /// corresponding column of the original matrix. #[inline] pub fn compress_rows( &self, f: impl Fn(VectorSliceN<N, R, S::RStride, S::CStride>) -> N, ) -> RowVectorN<N, C> where DefaultAllocator: Allocator<N, U1, C>, { let ncols = self.data.shape().1; let mut res = unsafe { RowVectorN::new_uninitialized_generic(U1, ncols) }; for i in 0..ncols.value() { // TODO: avoid bound checking of column. unsafe { *res.get_unchecked_mut((0, i)) = f(self.column(i)); } } res } /// Returns a column vector where each element is the result of the application of `f` on the /// corresponding column of the original matrix. /// /// This is the same as `self.compress_rows(f).transpose()`. #[inline] pub fn compress_rows_tr( &self, f: impl Fn(VectorSliceN<N, R, S::RStride, S::CStride>) -> N, ) -> VectorN<N, C> where DefaultAllocator: Allocator<N, C>, { let ncols = self.data.shape().1; let mut res = unsafe { VectorN::new_uninitialized_generic(ncols, U1) }; for i in 0..ncols.value() { // TODO: avoid bound checking of column. unsafe { *res.vget_unchecked_mut(i) = f(self.column(i)); } } res } /// Returns a column vector resulting from the folding of `f` on each column of this matrix. #[inline] pub fn compress_columns( &self, init: VectorN<N, R>, f: impl Fn(&mut VectorN<N, R>, VectorSliceN<N, R, S::RStride, S::CStride>), ) -> VectorN<N, R> where DefaultAllocator: Allocator<N, R>, { let mut res = init; for i in 0..self.ncols() { f(&mut res, self.column(i)) } res } } /// # Common statistics operations impl<N: Scalar, R: Dim, C: Dim, S: Storage<N, R, C>> Matrix<N, R, C, S> { /* * * Sum computation. * */ /// The sum of all the elements of this matrix. /// /// # Example /// /// ``` /// # use nalgebra::Matrix2x3; /// /// let m = Matrix2x3::new(1.0, 2.0, 3.0, /// 4.0, 5.0, 6.0); /// assert_eq!(m.sum(), 21.0); /// ``` #[inline] pub fn sum(&self) -> N where N: ClosedAdd + Zero, { self.iter().cloned().fold(N::zero(), |a, b| a + b) } /// The sum of all the rows of this matrix. /// /// Use `.row_variance_tr` if you need the result in a column vector instead. /// /// # Example /// /// ``` /// # use nalgebra::{Matrix2x3, Matrix3x2}; /// # use nalgebra::{RowVector2, RowVector3}; /// /// let m = Matrix2x3::new(1.0, 2.0, 3.0, /// 4.0, 5.0, 6.0); /// assert_eq!(m.row_sum(), RowVector3::new(5.0, 7.0, 9.0)); /// /// let mint = Matrix3x2::new(1,2,3,4,5,6); /// assert_eq!(mint.row_sum(), RowVector2::new(9,12)); /// ``` #[inline] pub fn row_sum(&self) -> RowVectorN<N, C> where N: ClosedAdd + Zero, DefaultAllocator: Allocator<N, U1, C>, { self.compress_rows(|col| col.sum()) } /// The sum of all the rows of this matrix. The result is transposed and returned as a column vector. /// /// # Example /// /// ``` /// # use nalgebra::{Matrix2x3, Matrix3x2}; /// # use nalgebra::{Vector2, Vector3}; /// /// let m = Matrix2x3::new(1.0, 2.0, 3.0, /// 4.0, 5.0, 6.0); /// assert_eq!(m.row_sum_tr(), Vector3::new(5.0, 7.0, 9.0)); /// /// let mint = Matrix3x2::new(1,2,3,4,5,6); /// assert_eq!(mint.row_sum_tr(), Vector2::new(9,12)); /// ``` #[inline] pub fn row_sum_tr(&self) -> VectorN<N, C> where N: ClosedAdd + Zero, DefaultAllocator: Allocator<N, C>, { self.compress_rows_tr(|col| col.sum()) } /// The sum of all the columns of this matrix. /// /// # Example /// /// ``` /// # use nalgebra::{Matrix2x3, Matrix3x2}; /// # use nalgebra::{Vector2, Vector3}; /// /// let m = Matrix2x3::new(1.0, 2.0, 3.0, /// 4.0, 5.0, 6.0); /// assert_eq!(m.column_sum(), Vector2::new(6.0, 15.0)); /// /// let mint = Matrix3x2::new(1,2,3,4,5,6); /// assert_eq!(mint.column_sum(), Vector3::new(3,7,11)); /// ``` #[inline] pub fn column_sum(&self) -> VectorN<N, R> where N: ClosedAdd + Zero, DefaultAllocator: Allocator<N, R>, { let nrows = self.data.shape().0; self.compress_columns(VectorN::zeros_generic(nrows, U1), |out, col| { *out += col; }) } /* * * Variance computation. * */ /// The variance of all the elements of this matrix. /// /// # Example /// /// ``` /// # #[macro_use] extern crate approx; /// # use nalgebra::Matrix2x3; /// /// let m = Matrix2x3::new(1.0, 2.0, 3.0, /// 4.0, 5.0, 6.0); /// assert_relative_eq!(m.variance(), 35.0 / 12.0, epsilon = 1.0e-8); /// ``` #[inline] pub fn variance(&self) -> N where N: Field + SupersetOf<f64>, { if self.is_empty() { N::zero() } else { let val = self.iter().cloned().fold((N::zero(), N::zero()), |a, b| { (a.0 + b.inlined_clone() * b.inlined_clone(), a.1 + b) }); let denom = N::one() / crate::convert::<_, N>(self.len() as f64); let vd = val.1 * denom.inlined_clone(); val.0 * denom - vd.inlined_clone() * vd } } /// The variance of all the rows of this matrix. /// /// Use `.row_variance_tr` if you need the result in a column vector instead. /// # Example /// /// ``` /// # use nalgebra::{Matrix2x3, RowVector3}; /// /// let m = Matrix2x3::new(1.0, 2.0, 3.0, /// 4.0, 5.0, 6.0); /// assert_eq!(m.row_variance(), RowVector3::new(2.25, 2.25, 2.25)); /// ``` #[inline] pub fn row_variance(&self) -> RowVectorN<N, C> where N: Field + SupersetOf<f64>, DefaultAllocator: Allocator<N, U1, C>, { self.compress_rows(|col| col.variance()) } /// The variance of all the rows of this matrix. The result is transposed and returned as a column vector. /// /// # Example /// /// ``` /// # use nalgebra::{Matrix2x3, Vector3}; /// /// let m = Matrix2x3::new(1.0, 2.0, 3.0, /// 4.0, 5.0, 6.0); /// assert_eq!(m.row_variance_tr(), Vector3::new(2.25, 2.25, 2.25)); /// ``` #[inline] pub fn row_variance_tr(&self) -> VectorN<N, C> where N: Field + SupersetOf<f64>, DefaultAllocator: Allocator<N, C>, { self.compress_rows_tr(|col| col.variance()) } /// The variance of all the columns of this matrix. /// /// # Example /// /// ``` /// # #[macro_use] extern crate approx; /// # use nalgebra::{Matrix2x3, Vector2}; /// /// let m = Matrix2x3::new(1.0, 2.0, 3.0, /// 4.0, 5.0, 6.0); /// assert_relative_eq!(m.column_variance(), Vector2::new(2.0 / 3.0, 2.0 / 3.0), epsilon = 1.0e-8); /// ``` #[inline] pub fn column_variance(&self) -> VectorN<N, R> where N: Field + SupersetOf<f64>, DefaultAllocator: Allocator<N, R>, { let (nrows, ncols) = self.data.shape(); let mut mean = self.column_mean(); mean.apply(|e| -(e.inlined_clone() * e)); let denom = N::one() / crate::convert::<_, N>(ncols.value() as f64); self.compress_columns(mean, |out, col| { for i in 0..nrows.value() { unsafe { let val = col.vget_unchecked(i); *out.vget_unchecked_mut(i) += denom.inlined_clone() * val.inlined_clone() * val.inlined_clone() } } }) } /* * * Mean computation. * */ /// The mean of all the elements of this matrix. /// /// # Example /// /// ``` /// # use nalgebra::Matrix2x3; /// /// let m = Matrix2x3::new(1.0, 2.0, 3.0, /// 4.0, 5.0, 6.0); /// assert_eq!(m.mean(), 3.5); /// ``` #[inline] pub fn mean(&self) -> N where N: Field + SupersetOf<f64>, { if self.is_empty() { N::zero() } else { self.sum() / crate::convert(self.len() as f64) } } /// The mean of all the rows of this matrix. /// /// Use `.row_mean_tr` if you need the result in a column vector instead. /// /// # Example /// /// ``` /// # use nalgebra::{Matrix2x3, RowVector3}; /// /// let m = Matrix2x3::new(1.0, 2.0, 3.0, /// 4.0, 5.0, 6.0); /// assert_eq!(m.row_mean(), RowVector3::new(2.5, 3.5, 4.5)); /// ``` #[inline] pub fn row_mean(&self) -> RowVectorN<N, C> where N: Field + SupersetOf<f64>, DefaultAllocator: Allocator<N, U1, C>, { self.compress_rows(|col| col.mean()) } /// The mean of all the rows of this matrix. The result is transposed and returned as a column vector. /// /// # Example /// /// ``` /// # use nalgebra::{Matrix2x3, Vector3}; /// /// let m = Matrix2x3::new(1.0, 2.0, 3.0, /// 4.0, 5.0, 6.0); /// assert_eq!(m.row_mean_tr(), Vector3::new(2.5, 3.5, 4.5)); /// ``` #[inline] pub fn row_mean_tr(&self) -> VectorN<N, C> where N: Field + SupersetOf<f64>, DefaultAllocator: Allocator<N, C>, { self.compress_rows_tr(|col| col.mean()) } /// The mean of all the columns of this matrix. /// /// # Example /// /// ``` /// # use nalgebra::{Matrix2x3, Vector2}; /// /// let m = Matrix2x3::new(1.0, 2.0, 3.0, /// 4.0, 5.0, 6.0); /// assert_eq!(m.column_mean(), Vector2::new(2.0, 5.0)); /// ``` #[inline] pub fn column_mean(&self) -> VectorN<N, R> where N: Field + SupersetOf<f64>, DefaultAllocator: Allocator<N, R>, { let (nrows, ncols) = self.data.shape(); let denom = N::one() / crate::convert::<_, N>(ncols.value() as f64); self.compress_columns(VectorN::zeros_generic(nrows, U1), |out, col| { out.axpy(denom.inlined_clone(), &col, N::one()) }) } }