Skip to content

Commit 5e88763

Browse files
committed
Fix clippy::comparison_chain
1 parent 3c1969b commit 5e88763

File tree

3 files changed

+48
-47
lines changed

3 files changed

+48
-47
lines changed

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
clippy::ptr_arg,
7070
clippy::len_without_is_empty,
7171
clippy::map_entry,
72-
clippy::comparison_chain,
7372
clippy::type_complexity,
7473
clippy::too_many_arguments,
7574
clippy::many_single_char_names

src/linalg/lu.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
//! <script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
3434
#![allow(non_snake_case)]
3535

36+
use std::cmp::Ordering;
3637
use std::fmt::Debug;
3738
use std::marker::PhantomData;
3839

@@ -78,12 +79,10 @@ impl<T: RealNumber, M: BaseMatrix<T>> LU<T, M> {
7879

7980
for i in 0..n_rows {
8081
for j in 0..n_cols {
81-
if i > j {
82-
L.set(i, j, self.LU.get(i, j));
83-
} else if i == j {
84-
L.set(i, j, T::one());
85-
} else {
86-
L.set(i, j, T::zero());
82+
match i.cmp(&j) {
83+
Ordering::Greater => L.set(i, j, self.LU.get(i, j)),
84+
Ordering::Equal => L.set(i, j, T::one()),
85+
Ordering::Less => L.set(i, j, T::zero()),
8786
}
8887
}
8988
}

src/linear/logistic_regression.rs

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
//!
5353
//! <script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
5454
//! <script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
55+
use std::cmp::Ordering;
5556
use std::fmt::Debug;
5657
use std::marker::PhantomData;
5758

@@ -231,48 +232,50 @@ impl<T: RealNumber, M: Matrix<T>> LogisticRegression<T, M> {
231232
yi[i] = classes.iter().position(|c| yc == *c).unwrap();
232233
}
233234

234-
if k < 2 {
235-
Err(Failed::fit(&format!(
235+
match k.cmp(&2) {
236+
Ordering::Less => Err(Failed::fit(&format!(
236237
"incorrect number of classes: {}. Should be >= 2.",
237238
k
238-
)))
239-
} else if k == 2 {
240-
let x0 = M::zeros(1, num_attributes + 1);
241-
242-
let objective = BinaryObjectiveFunction {
243-
x,
244-
y: yi,
245-
phantom: PhantomData,
246-
};
247-
248-
let result = LogisticRegression::minimize(x0, objective);
249-
250-
Ok(LogisticRegression {
251-
weights: result.x,
252-
classes,
253-
num_attributes,
254-
num_classes: k,
255-
})
256-
} else {
257-
let x0 = M::zeros(1, (num_attributes + 1) * k);
258-
259-
let objective = MultiClassObjectiveFunction {
260-
x,
261-
y: yi,
262-
k,
263-
phantom: PhantomData,
264-
};
265-
266-
let result = LogisticRegression::minimize(x0, objective);
267-
268-
let weights = result.x.reshape(k, num_attributes + 1);
269-
270-
Ok(LogisticRegression {
271-
weights,
272-
classes,
273-
num_attributes,
274-
num_classes: k,
275-
})
239+
))),
240+
Ordering::Greater => {
241+
let x0 = M::zeros(1, (num_attributes + 1) * k);
242+
243+
let objective = MultiClassObjectiveFunction {
244+
x,
245+
y: yi,
246+
k,
247+
phantom: PhantomData,
248+
};
249+
250+
let result = LogisticRegression::minimize(x0, objective);
251+
252+
let weights = result.x.reshape(k, num_attributes + 1);
253+
254+
Ok(LogisticRegression {
255+
weights,
256+
classes,
257+
num_attributes,
258+
num_classes: k,
259+
})
260+
}
261+
Ordering::Equal => {
262+
let x0 = M::zeros(1, num_attributes + 1);
263+
264+
let objective = BinaryObjectiveFunction {
265+
x,
266+
y: yi,
267+
phantom: PhantomData,
268+
};
269+
270+
let result = LogisticRegression::minimize(x0, objective);
271+
272+
Ok(LogisticRegression {
273+
weights: result.x,
274+
classes,
275+
num_attributes,
276+
num_classes: k,
277+
})
278+
}
276279
}
277280
}
278281

0 commit comments

Comments
 (0)