diff --git a/sbp/src/operators/algos.rs b/sbp/src/operators/algos.rs index d14997a..9ed593d 100644 --- a/sbp/src/operators/algos.rs +++ b/sbp/src/operators/algos.rs @@ -3,7 +3,7 @@ use ndarray::s; use num_traits::Zero; pub(crate) mod constmatrix; -pub(crate) use constmatrix::{flip_lr, flip_sign, flip_ud, ColVector, Matrix, RowVector}; +pub(crate) use constmatrix::{ColVector, Matrix, RowVector}; #[cfg(feature = "fast-float")] mod fastfloat; diff --git a/sbp/src/operators/algos/constmatrix.rs b/sbp/src/operators/algos/constmatrix.rs index b9645a5..7c457cc 100644 --- a/sbp/src/operators/algos/constmatrix.rs +++ b/sbp/src/operators/algos/constmatrix.rs @@ -263,70 +263,66 @@ mod approx { } } -pub(crate) const fn flip_ud( - mut m: Matrix, -) -> Matrix { - let mut i = 0; - while i < M / 2 { - let tmp = m.data[i]; - m.data[i] = m.data[M - 1 - i]; - m.data[M - 1 - i] = tmp; - i += 1; +impl Matrix { + pub(crate) const fn flip_ud(&self) -> Self { + let mut m = Self::new([[0.0; N]; M]); + let mut i = 0; + while i < M { + m.data[M - 1 - i] = self.data[i]; + i += 1; + } + m + } + + pub(crate) const fn flip_lr(&self) -> Self { + let mut m = Self::new([[0.0; N]; M]); + let mut i = 0; + while i < M { + let mut j = 0; + while j < N { + m.data[i][N - 1 - j] = self.data[i][j]; + j += 1; + } + i += 1; + } + m + } + + /// Flip all sign bits + pub(crate) const fn flip_sign(&self) -> Self { + let mut m = Self::new([[0.0; N]; M]); + let mut i = 0; + while i < M { + let mut j = 0; + while j < N { + m.data[i][j] = -self.data[i][j]; + j += 1; + } + i += 1; + } + m } - m } -pub(crate) const fn flip_lr( - mut m: Matrix, -) -> Matrix { - let mut i = 0; - while i < M { - let mut j = 0; - while j < N / 2 { - let tmp = m.data[i][j]; - m.data[i][j] = m.data[i][N - 1 - j]; - m.data[i][N - 1 - j] = tmp; - j += 1; - } - i += 1; - } - m -} - -/// Flip all sign bits -pub(crate) const fn flip_sign( - mut m: Matrix, -) -> Matrix { - let mut i = 0; - while i < M { - let mut j = 0; - while j < N { - m.data[i][j] = -m.data[i][j]; - j += 1; - } - i += 1; - } - m -} mod flipping { use super::*; #[test] fn flip_lr_test() { let m = Matrix::new([[1.0, 2.0, 3.0, 4.0]]); - let flipped = flip_lr(m); + let flipped = m.flip_lr(); assert_eq!(flipped, Matrix::new([[4.0, 3.0, 2.0, 1.0]])); let m = Matrix::new([[1.0, 2.0, 3.0, 4.0, 5.0]]); - let flipped = flip_lr(m); + let flipped = m.flip_lr(); assert_eq!(flipped, Matrix::new([[5.0, 4.0, 3.0, 2.0, 1.0]])); } #[test] fn flip_ud_test() { let m = Matrix::new([[1.0], [2.0], [3.0], [4.0]]); - let flipped = flip_ud(m); + let flipped = m.flip_ud(); assert_eq!(flipped, Matrix::new([[4.0], [3.0], [2.0], [1.0]])); let m = Matrix::new([[1.0], [2.0], [3.0], [4.0], [5.0]]); - let flipped = flip_ud(m); + let flipped = m.flip_ud(); assert_eq!(flipped, Matrix::new([[5.0], [4.0], [3.0], [2.0], [1.0]])); } } diff --git a/sbp/src/operators/traditional4.rs b/sbp/src/operators/traditional4.rs index e2b59df..5cac183 100644 --- a/sbp/src/operators/traditional4.rs +++ b/sbp/src/operators/traditional4.rs @@ -25,7 +25,7 @@ impl SBP4 { [3.0/98.0, 0.0, -59.0/98.0, 0.0, 32.0/49.0, -4.0/49.0] ]); const DIFF_BLOCKEND: super::Matrix = - super::flip_sign(super::flip_ud(super::flip_lr(Self::DIFF_BLOCK))); + Self::DIFF_BLOCK.flip_lr().flip_ud().flip_sign(); const DIFF: BlockMatrix = BlockMatrix::new(Self::DIFF_BLOCK, Self::DIFF_DIAG, Self::DIFF_BLOCKEND); @@ -44,7 +44,7 @@ impl SBP4 { const D2: BlockMatrix = BlockMatrix::new( Self::D2BLOCK, Self::D2DIAG, - super::flip_ud(super::flip_lr(Self::D2BLOCK)), + Self::D2BLOCK.flip_lr().flip_ud(), ); } diff --git a/sbp/src/operators/traditional8.rs b/sbp/src/operators/traditional8.rs index d1dcf4c..32a0525 100644 --- a/sbp/src/operators/traditional8.rs +++ b/sbp/src/operators/traditional8.rs @@ -30,7 +30,7 @@ impl SBP8 { const DIFF: BlockMatrix = BlockMatrix::new( Self::DIFF_BLOCK, Self::DIFF_DIAG, - super::flip_sign(super::flip_ud(super::flip_lr(Self::DIFF_BLOCK))), + Self::DIFF_BLOCK.flip_lr().flip_ud().flip_sign(), ); } diff --git a/sbp/src/operators/upwind4.rs b/sbp/src/operators/upwind4.rs index 0126a9d..a24aec7 100644 --- a/sbp/src/operators/upwind4.rs +++ b/sbp/src/operators/upwind4.rs @@ -24,8 +24,7 @@ impl Upwind4 { const DIFF_DIAG: RowVector = RowVector::new([[ -1.0 / 24.0, 1.0 / 4.0, -7.0 / 8.0, 0.0, 7.0 / 8.0, -1.0 / 4.0, 1.0 / 24.0 ]]); - const DIFF_BLOCKEND: Matrix = - super::flip_sign(super::flip_ud(super::flip_lr(Self::DIFF_BLOCK))); + const DIFF_BLOCKEND: Matrix = Self::DIFF_BLOCK.flip_lr().flip_ud().flip_sign(); const DIFF: BlockMatrix = BlockMatrix::new(Self::DIFF_BLOCK, Self::DIFF_DIAG, Self::DIFF_BLOCKEND); @@ -41,7 +40,7 @@ impl Upwind4 { const DISS_DIAG: RowVector = Matrix::new([[ 1.0 / 24.0, -1.0 / 4.0, 5.0 / 8.0, -5.0 / 6.0, 5.0 / 8.0, -1.0 / 4.0, 1.0 / 24.0 ]]); - const DISS_BLOCKEND: Matrix = super::flip_ud(super::flip_lr(Self::DISS_BLOCK)); + const DISS_BLOCKEND: Matrix = Self::DISS_BLOCK.flip_lr().flip_ud(); const DISS: BlockMatrix = BlockMatrix::new(Self::DISS_BLOCK, Self::DISS_DIAG, Self::DISS_BLOCKEND); diff --git a/sbp/src/operators/upwind4h2.rs b/sbp/src/operators/upwind4h2.rs index 2a270b3..711c9b3 100644 --- a/sbp/src/operators/upwind4h2.rs +++ b/sbp/src/operators/upwind4h2.rs @@ -27,7 +27,7 @@ impl Upwind4h2 { const DIFF: BlockMatrix = BlockMatrix::new( Self::DIFF_BLOCK, Self::DIFF_DIAG, - super::flip_sign(super::flip_ud(super::flip_lr(Self::DIFF_BLOCK))), + Self::DIFF_BLOCK.flip_lr().flip_ud().flip_sign(), ); #[rustfmt::skip] @@ -45,7 +45,7 @@ impl Upwind4h2 { const DISS: BlockMatrix = BlockMatrix::new( Self::DISS_BLOCK, Self::DISS_DIAG, - super::flip_ud(super::flip_lr(Self::DISS_BLOCK)), + Self::DISS_BLOCK.flip_lr().flip_ud(), ); } diff --git a/sbp/src/operators/upwind9.rs b/sbp/src/operators/upwind9.rs index 192b577..11f8dee 100644 --- a/sbp/src/operators/upwind9.rs +++ b/sbp/src/operators/upwind9.rs @@ -31,7 +31,7 @@ impl Upwind9 { const DIFF: BlockMatrix = BlockMatrix::new( Self::DIFF_BLOCK, Self::DIFF_DIAG, - super::flip_sign(super::flip_ud(super::flip_lr(Self::DIFF_BLOCK))), + Self::DIFF_BLOCK.flip_lr().flip_ud().flip_sign(), ); #[rustfmt::skip] @@ -53,7 +53,7 @@ impl Upwind9 { const DISS: BlockMatrix = BlockMatrix::new( Self::DISS_BLOCK, Self::DISS_DIAG, - super::flip_ud(super::flip_lr(Self::DISS_BLOCK)), + Self::DISS_BLOCK.flip_lr().flip_ud(), ); } diff --git a/sbp/src/operators/upwind9h2.rs b/sbp/src/operators/upwind9h2.rs index 9eb6830..6c1e104 100644 --- a/sbp/src/operators/upwind9h2.rs +++ b/sbp/src/operators/upwind9h2.rs @@ -31,7 +31,7 @@ impl Upwind9h2 { const DIFF: BlockMatrix = BlockMatrix::new( Self::DIFF_BLOCK, Self::DIFF_DIAG, - super::flip_sign(super::flip_ud(super::flip_lr(Self::DIFF_BLOCK))), + Self::DIFF_BLOCK.flip_lr().flip_ud().flip_sign(), ); #[rustfmt::skip] @@ -53,7 +53,7 @@ impl Upwind9h2 { const DISS: BlockMatrix = BlockMatrix::new( Self::DISS_BLOCK, Self::DISS_DIAG, - super::flip_ud(super::flip_lr(Self::DISS_BLOCK)), + Self::DISS_BLOCK.flip_lr().flip_ud(), ); }