Skip to content

Commit

Permalink
Merge pull request #5 from utopiabound/rust167
Browse files Browse the repository at this point in the history
Update for Rust 1.6.7
  • Loading branch information
wiebecommajonas authored Jun 19, 2024
2 parents 098e768 + 9577577 commit b4e06c3
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 19 deletions.
12 changes: 5 additions & 7 deletions src/err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ impl Display for MatrixError {
match self {
MatrixError::IndexOutOfBounds(idx) => write!(
f,
"Tried to access a matrix at index `{}`, which is out of bounds.",
idx
"Tried to access a matrix at index `{idx}`, which is out of bounds.",
)?,
}
Ok(())
Expand All @@ -33,12 +32,11 @@ impl Display for DimensionError {
DimensionError::InvalidDimensions => {
write!(f, "Dimensions with a size of less than 1 are invalid.")?
}
DimensionError::NoMatch(dims, bad_dims, op) => {write!(
DimensionError::NoMatch(dims, bad_dims, op) => write!(
f,
"Dimensions of two matrices do not match in the correct way. Cannot {} {} matrix with {} matrix.",
op, dims, bad_dims
)?}
DimensionError::InvalidInputDimensions(input_len, correct_len) => {write!(f, "Invalid input dimensions. Input has length {}, but should have length {}.", input_len, correct_len)?}
"Dimensions of two matrices do not match in the correct way. Cannot {op} {dims} matrix with {bad_dims} matrix.",
)?,
DimensionError::InvalidInputDimensions(input_len, correct_len) => write!(f, "Invalid input dimensions. Input has length {input_len}, but should have length {correct_len}.")?,
DimensionError::NoSquare => {
write!(f, "Not a square matrix. Rows and cols need to be the same.")?
}
Expand Down
3 changes: 2 additions & 1 deletion src/mat/_mat/mat_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ where
}

/// Creates a diagonal matrix with dimensions `dim x dim` and initial entries specified in `entries`.
#[allow(clippy::manual_memcpy)]
pub fn diag_with(dim: usize, entries: &[T]) -> Result<Matrix<T>, DimensionError> {
if entries.len() != dim {
return Err(DimensionError::InvalidInputDimensions(entries.len(), dim));
Expand Down Expand Up @@ -346,7 +347,7 @@ impl<T> Matrix<T> {
self.matrix[self.cols() * i.into() + j.into()].clone()
}

pub fn entry_mut<'a>(&'a mut self, i: impl Into<usize>, j: impl Into<usize>) -> &'a mut T {
pub fn entry_mut(&mut self, i: impl Into<usize>, j: impl Into<usize>) -> &mut T {
let cols = self.cols();
&mut self.matrix[cols * i.into() + j.into()]
}
Expand Down
6 changes: 3 additions & 3 deletions src/mat/_mat/mat_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ where
for j in 0..self.cols() {
let n = &self.matrix[i * self.cols() + j];
if j == self.cols() - 1 && i == self.rows() - 1 {
write!(f, "{}", n)?;
write!(f, "{n}")?;
} else if j == self.cols() - 1 {
writeln!(f, "{}", n)?;
writeln!(f, "{n}")?;
} else {
write!(f, "{}\t", n)?;
write!(f, "{n}\t")?;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/mat/smat/smat_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ where
}

/// Creates a diagonal matrix with initial entries specified in `entries`.
#[allow(clippy::manual_memcpy)]
pub fn diag_with(entries: &[T]) -> SMatrix<T, N, N>
where
T: One + Copy + Zero + std::iter::Sum,
Expand Down
2 changes: 1 addition & 1 deletion src/mat/smat/smat_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ where
while let Some(r) = rs.next() {
let mut es = r.iter().peekable();
while let Some(e) = es.next() {
write!(f, "{}", e)?;
write!(f, "{e}")?;
if rs.peek().is_some() {
write!(f, "")?;
} else if es.peek().is_some() {
Expand Down
8 changes: 2 additions & 6 deletions src/mat/vec/vec_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ where
T: AddAssign + Clone,
{
fn add_assign(&mut self, rhs: T) {
self.iter_mut()
.for_each(|a| *a += rhs.clone());
self.iter_mut().for_each(|a| *a += rhs.clone());
}
}

Expand Down Expand Up @@ -171,8 +170,7 @@ where
T: SubAssign + Clone,
{
fn sub_assign(&mut self, rhs: T) {
self.iter_mut()
.for_each(|a| *a -= rhs.clone());
self.iter_mut().for_each(|a| *a -= rhs.clone());
}
}

Expand Down Expand Up @@ -255,9 +253,7 @@ where
fn mul(self, mat: Matrix<T>) -> Self::Output {
let vector: Vector<T> = self;
let mat_v: Matrix<T> = vector.into();
println!("{}\n\n{}", mat_v, mat);
let res = (mat_v * mat)?;
println!("\n{}", res);
Ok(res.into())
}
}
Expand Down
5 changes: 4 additions & 1 deletion tests/determinant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ fn some_dets() -> Result<(), DimensionError> {
8, 6, 1, 0, 1, 9, 5, 9, 9, 9, 0, 8, 4, 3, 4, 0, 5, 6, 5, 1, 0, 9, 4, 6, 4, 9, 8, 3, 5,
1, 10, 6, 3, 10, 7, 4, 9, 2, 0, 1, 2, 1, 6, 8, 7, 3, 2, 9, 1, 7, 1, 4, 4, 9, 0, 0, 7,
6, 4, 0, 10, 4, 5, 9,
].iter().map(|x| (*x as i16).into()).collect(),
]
.iter()
.map(|x| (*x as i16).into())
.collect(),
)?;
assert_eq!(b.det()?, -15546220_f32);
Ok(())
Expand Down

0 comments on commit b4e06c3

Please sign in to comment.