|
| 1 | + |
| 2 | +extern crate ndarray; |
| 3 | +extern crate ndarray_linalg as linalg; |
| 4 | + |
| 5 | +use ndarray::prelude::*; |
| 6 | +use linalg::Matrix; |
| 7 | + |
| 8 | +fn assert_almost_eq(a: f64, b: f64) { |
| 9 | + let rel_dev = (a - b).abs() / (a.abs() + b.abs()); |
| 10 | + if rel_dev > 1.0e-7 { |
| 11 | + panic!("a={:?}, b={:?} are not almost equal", a, b); |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +#[test] |
| 16 | +fn test_matrix_norm_3x4() { |
| 17 | + let a = arr2(&[[3.0, 1.0, 1.0, 1.0], [1.0, 3.0, 1.0, 1.0], [1.0, 1.0, 3.0, 1.0]]); |
| 18 | + assert_almost_eq(a.norm_1(), 5.0); |
| 19 | + assert_almost_eq(a.norm_i(), 6.0); |
| 20 | +} |
| 21 | + |
| 22 | +#[test] |
| 23 | +fn test_matrix_norm_3x4_t() { |
| 24 | + let a = arr2(&[[3.0, 1.0, 1.0, 1.0], [1.0, 3.0, 1.0, 1.0], [1.0, 1.0, 3.0, 1.0]]) |
| 25 | + .reversed_axes(); |
| 26 | + assert_almost_eq(a.norm_1(), 6.0); |
| 27 | + assert_almost_eq(a.norm_i(), 5.0); |
| 28 | +} |
| 29 | + |
| 30 | +#[test] |
| 31 | +fn test_matrix_norm_4x3() { |
| 32 | + let a = arr2(&[[3.0, 1.0, 1.0], [1.0, 3.0, 1.0], [1.0, 1.0, 3.0], [1.0, 1.0, 1.0]]); |
| 33 | + assert_almost_eq(a.norm_1(), 6.0); |
| 34 | + assert_almost_eq(a.norm_i(), 5.0); |
| 35 | +} |
| 36 | + |
| 37 | +#[test] |
| 38 | +fn test_matrix_norm_4x3_t() { |
| 39 | + let a = arr2(&[[3.0, 1.0, 1.0], [1.0, 3.0, 1.0], [1.0, 1.0, 3.0], [1.0, 1.0, 1.0]]) |
| 40 | + .reversed_axes(); |
| 41 | + assert_almost_eq(a.norm_1(), 5.0); |
| 42 | + assert_almost_eq(a.norm_i(), 6.0); |
| 43 | +} |
0 commit comments