|
39 | 39 | /// ```
|
40 | 40 | /// and similarly for ̅y.
|
41 | 41 | ///
|
42 |
| - /// **Panics** if `ddof` is greater than or equal to the number of |
43 |
| - /// observations, if `M` is emtpy or if the type cast of `n_observations` |
44 |
| - /// from `usize` to `A` fails. |
| 42 | + /// **Panics** if `ddof` is greater than or equal to the number of |
| 43 | + /// observations, if the number of observations is zero and division by |
| 44 | + /// zero panics for type `A`, or if the type cast of `n_observations` from |
| 45 | + /// `usize` to `A` fails. |
45 | 46 | ///
|
46 | 47 | /// # Example
|
47 | 48 | ///
|
@@ -133,11 +134,27 @@ mod tests {
|
133 | 134 | }
|
134 | 135 |
|
135 | 136 | #[test]
|
136 |
| - #[should_panic] |
137 |
| - fn test_empty_matrix() { |
138 |
| - let a: Array2<f32> = array![[], []]; |
139 |
| - // Negative ddof (-1 < 0) to avoid invalid-ddof panic |
140 |
| - a.cov(-1.); |
| 137 | + fn test_covariance_zero_variables() { |
| 138 | + let a = Array2::<f32>::zeros((0, 2)); |
| 139 | + let cov = a.cov(1.); |
| 140 | + assert_eq!(cov.shape(), &[0, 0]); |
| 141 | + } |
| 142 | + |
| 143 | + #[test] |
| 144 | + fn test_covariance_zero_observations() { |
| 145 | + let a = Array2::<f32>::zeros((2, 0)); |
| 146 | + // Negative ddof (-1 < 0) to avoid invalid-ddof panic |
| 147 | + let cov = a.cov(-1.); |
| 148 | + assert_eq!(cov.shape(), &[2, 2]); |
| 149 | + cov.mapv(|x| x.is_nan()); |
| 150 | + } |
| 151 | + |
| 152 | + #[test] |
| 153 | + fn test_covariance_zero_variables_zero_observations() { |
| 154 | + let a = Array2::<f32>::zeros((0, 0)); |
| 155 | + // Negative ddof (-1 < 0) to avoid invalid-ddof panic |
| 156 | + let cov = a.cov(-1.); |
| 157 | + assert_eq!(cov.shape(), &[0, 0]); |
141 | 158 | }
|
142 | 159 |
|
143 | 160 | #[test]
|
|
0 commit comments