Make flip_XX impl on Matrix

This commit is contained in:
Magnus Ulimoen 2021-03-15 19:31:41 +01:00
parent 6fc045ae17
commit e43e71a4d8
8 changed files with 54 additions and 59 deletions

View File

@ -3,7 +3,7 @@ use ndarray::s;
use num_traits::Zero; use num_traits::Zero;
pub(crate) mod constmatrix; 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")] #[cfg(feature = "fast-float")]
mod fastfloat; mod fastfloat;

View File

@ -263,70 +263,66 @@ mod approx {
} }
} }
pub(crate) const fn flip_ud<const M: usize, const N: usize>( impl<const M: usize, const N: usize> Matrix<super::Float, M, N> {
mut m: Matrix<super::Float, M, N>, pub(crate) const fn flip_ud(&self) -> Self {
) -> Matrix<super::Float, M, N> { let mut m = Self::new([[0.0; N]; M]);
let mut i = 0; let mut i = 0;
while i < M / 2 { while i < M {
let tmp = m.data[i]; m.data[M - 1 - i] = self.data[i];
m.data[i] = m.data[M - 1 - i]; i += 1;
m.data[M - 1 - i] = tmp; }
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<const M: usize, const N: usize>(
mut m: Matrix<super::Float, M, N>,
) -> Matrix<super::Float, M, N> {
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<const M: usize, const N: usize>(
mut m: Matrix<super::Float, M, N>,
) -> Matrix<super::Float, M, N> {
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 { mod flipping {
use super::*; use super::*;
#[test] #[test]
fn flip_lr_test() { fn flip_lr_test() {
let m = Matrix::new([[1.0, 2.0, 3.0, 4.0]]); 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]])); 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 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]])); assert_eq!(flipped, Matrix::new([[5.0, 4.0, 3.0, 2.0, 1.0]]));
} }
#[test] #[test]
fn flip_ud_test() { fn flip_ud_test() {
let m = Matrix::new([[1.0], [2.0], [3.0], [4.0]]); 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]])); 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 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]])); assert_eq!(flipped, Matrix::new([[5.0], [4.0], [3.0], [2.0], [1.0]]));
} }
} }

View File

@ -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] [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<Float, 4, 6> = const DIFF_BLOCKEND: super::Matrix<Float, 4, 6> =
super::flip_sign(super::flip_ud(super::flip_lr(Self::DIFF_BLOCK))); Self::DIFF_BLOCK.flip_lr().flip_ud().flip_sign();
const DIFF: BlockMatrix<Float, 4, 6, 5> = const DIFF: BlockMatrix<Float, 4, 6, 5> =
BlockMatrix::new(Self::DIFF_BLOCK, Self::DIFF_DIAG, Self::DIFF_BLOCKEND); BlockMatrix::new(Self::DIFF_BLOCK, Self::DIFF_DIAG, Self::DIFF_BLOCKEND);
@ -44,7 +44,7 @@ impl SBP4 {
const D2: BlockMatrix<Float, 4, 6, 5> = BlockMatrix::new( const D2: BlockMatrix<Float, 4, 6, 5> = BlockMatrix::new(
Self::D2BLOCK, Self::D2BLOCK,
Self::D2DIAG, Self::D2DIAG,
super::flip_ud(super::flip_lr(Self::D2BLOCK)), Self::D2BLOCK.flip_lr().flip_ud(),
); );
} }

View File

@ -30,7 +30,7 @@ impl SBP8 {
const DIFF: BlockMatrix<Float, 8, 12, 9> = BlockMatrix::new( const DIFF: BlockMatrix<Float, 8, 12, 9> = BlockMatrix::new(
Self::DIFF_BLOCK, Self::DIFF_BLOCK,
Self::DIFF_DIAG, Self::DIFF_DIAG,
super::flip_sign(super::flip_ud(super::flip_lr(Self::DIFF_BLOCK))), Self::DIFF_BLOCK.flip_lr().flip_ud().flip_sign(),
); );
} }

View File

@ -24,8 +24,7 @@ impl Upwind4 {
const DIFF_DIAG: RowVector<Float, 7> = RowVector::new([[ const DIFF_DIAG: RowVector<Float, 7> = 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 -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<Float, 4, 7> = const DIFF_BLOCKEND: Matrix<Float, 4, 7> = Self::DIFF_BLOCK.flip_lr().flip_ud().flip_sign();
super::flip_sign(super::flip_ud(super::flip_lr(Self::DIFF_BLOCK)));
const DIFF: BlockMatrix<Float, 4, 7, 7> = const DIFF: BlockMatrix<Float, 4, 7, 7> =
BlockMatrix::new(Self::DIFF_BLOCK, Self::DIFF_DIAG, Self::DIFF_BLOCKEND); BlockMatrix::new(Self::DIFF_BLOCK, Self::DIFF_DIAG, Self::DIFF_BLOCKEND);
@ -41,7 +40,7 @@ impl Upwind4 {
const DISS_DIAG: RowVector<Float, 7> = Matrix::new([[ const DISS_DIAG: RowVector<Float, 7> = 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 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<Float, 4, 7> = super::flip_ud(super::flip_lr(Self::DISS_BLOCK)); const DISS_BLOCKEND: Matrix<Float, 4, 7> = Self::DISS_BLOCK.flip_lr().flip_ud();
const DISS: BlockMatrix<Float, 4, 7, 7> = const DISS: BlockMatrix<Float, 4, 7, 7> =
BlockMatrix::new(Self::DISS_BLOCK, Self::DISS_DIAG, Self::DISS_BLOCKEND); BlockMatrix::new(Self::DISS_BLOCK, Self::DISS_DIAG, Self::DISS_BLOCKEND);

View File

@ -27,7 +27,7 @@ impl Upwind4h2 {
const DIFF: BlockMatrix<Float, 4, 7, 7> = BlockMatrix::new( const DIFF: BlockMatrix<Float, 4, 7, 7> = BlockMatrix::new(
Self::DIFF_BLOCK, Self::DIFF_BLOCK,
Self::DIFF_DIAG, 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] #[rustfmt::skip]
@ -45,7 +45,7 @@ impl Upwind4h2 {
const DISS: BlockMatrix<Float, 4, 7, 7> = BlockMatrix::new( const DISS: BlockMatrix<Float, 4, 7, 7> = BlockMatrix::new(
Self::DISS_BLOCK, Self::DISS_BLOCK,
Self::DISS_DIAG, Self::DISS_DIAG,
super::flip_ud(super::flip_lr(Self::DISS_BLOCK)), Self::DISS_BLOCK.flip_lr().flip_ud(),
); );
} }

View File

@ -31,7 +31,7 @@ impl Upwind9 {
const DIFF: BlockMatrix<Float, 8, 13, 11> = BlockMatrix::new( const DIFF: BlockMatrix<Float, 8, 13, 11> = BlockMatrix::new(
Self::DIFF_BLOCK, Self::DIFF_BLOCK,
Self::DIFF_DIAG, 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] #[rustfmt::skip]
@ -53,7 +53,7 @@ impl Upwind9 {
const DISS: BlockMatrix<Float, 8, 13, 11> = BlockMatrix::new( const DISS: BlockMatrix<Float, 8, 13, 11> = BlockMatrix::new(
Self::DISS_BLOCK, Self::DISS_BLOCK,
Self::DISS_DIAG, Self::DISS_DIAG,
super::flip_ud(super::flip_lr(Self::DISS_BLOCK)), Self::DISS_BLOCK.flip_lr().flip_ud(),
); );
} }

View File

@ -31,7 +31,7 @@ impl Upwind9h2 {
const DIFF: BlockMatrix<Float, 8, 13, 11> = BlockMatrix::new( const DIFF: BlockMatrix<Float, 8, 13, 11> = BlockMatrix::new(
Self::DIFF_BLOCK, Self::DIFF_BLOCK,
Self::DIFF_DIAG, 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] #[rustfmt::skip]
@ -53,7 +53,7 @@ impl Upwind9h2 {
const DISS: BlockMatrix<Float, 8, 13, 11> = BlockMatrix::new( const DISS: BlockMatrix<Float, 8, 13, 11> = BlockMatrix::new(
Self::DISS_BLOCK, Self::DISS_BLOCK,
Self::DISS_DIAG, Self::DISS_DIAG,
super::flip_ud(super::flip_lr(Self::DISS_BLOCK)), Self::DISS_BLOCK.flip_lr().flip_ud(),
); );
} }