Skip to content

Commit 9e9c3f9

Browse files
committed
Uses initial check to clarify logic
1 parent 449ad0e commit 9e9c3f9

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/tri.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ where
3636
/// ```
3737
pub fn triu(&self, k: isize) -> Array<A, D>
3838
{
39-
match self.ndim() > 1 && is_layout_f(&self.dim, &self.strides) {
39+
if self.ndim() <= 1 {
40+
return self.to_owned();
41+
}
42+
match is_layout_f(&self.dim, &self.strides) {
4043
true => {
4144
let n = self.ndim();
4245
let mut x = self.view();
@@ -78,7 +81,10 @@ where
7881
/// ```
7982
pub fn tril(&self, k: isize) -> Array<A, D>
8083
{
81-
match self.ndim() > 1 && is_layout_f(&self.dim, &self.strides) {
84+
if self.ndim() <= 1 {
85+
return self.to_owned();
86+
}
87+
match is_layout_f(&self.dim, &self.strides) {
8288
true => {
8389
let n = self.ndim();
8490
let mut x = self.view();
@@ -90,12 +96,12 @@ where
9096
}
9197
false => {
9298
let mut res = Array::zeros(self.raw_dim());
99+
let ncols = self.len_of(Axis(self.ndim() - 1)) as isize;
93100
Zip::indexed(self.rows())
94101
.and(res.rows_mut())
95102
.for_each(|i, src, mut dst| {
96103
// This ncols must go inside the loop to avoid panic on 1D arrays.
97104
// Statistically-neglible difference in performance vs defining ncols at top.
98-
let ncols = src.len_of(Axis(src.ndim() - 1)) as isize;
99105
let row_num = i.into_dimension().last_elem();
100106
let upper = min(row_num as isize + k, ncols) + 1;
101107
dst.slice_mut(s![..upper]).assign(&src.slice(s![..upper]));

0 commit comments

Comments
 (0)