Skip to content

Commit 6de9acc

Browse files
authored
Merge pull request #287 from rust-ndarray/use-build-uninit
Port usage of uninitialized to build_uninit
2 parents a561e5a + b0d8529 commit 6de9acc

File tree

3 files changed

+25
-30
lines changed

3 files changed

+25
-30
lines changed

ndarray-linalg/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ rand = "0.8.3"
3636
thiserror = "1.0.24"
3737

3838
[dependencies.ndarray]
39-
version = "0.15.1"
39+
version = "0.15.2"
4040
features = ["blas", "approx", "std"]
4141
default-features = false
4242

ndarray-linalg/src/convert.rs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,31 +46,19 @@ where
4646
}
4747
}
4848

49-
fn uninitialized<A, S>(l: MatrixLayout) -> ArrayBase<S, Ix2>
50-
where
51-
A: Copy,
52-
S: DataOwned<Elem = A>,
53-
{
54-
match l {
55-
MatrixLayout::C { row, lda } => unsafe {
56-
ArrayBase::uninitialized((row as usize, lda as usize))
57-
},
58-
MatrixLayout::F { col, lda } => unsafe {
59-
ArrayBase::uninitialized((lda as usize, col as usize).f())
60-
},
61-
}
62-
}
63-
6449
pub fn replicate<A, Sv, So, D>(a: &ArrayBase<Sv, D>) -> ArrayBase<So, D>
6550
where
6651
A: Copy,
6752
Sv: Data<Elem = A>,
6853
So: DataOwned<Elem = A> + DataMut,
6954
D: Dimension,
7055
{
71-
let mut b = unsafe { ArrayBase::uninitialized(a.dim()) };
72-
b.assign(a);
73-
b
56+
unsafe {
57+
let ret = ArrayBase::<So, D>::build_uninit(a.dim(), |view| {
58+
a.assign_to(view);
59+
});
60+
ret.assume_init()
61+
}
7462
}
7563

7664
fn clone_with_layout<A, Si, So>(l: MatrixLayout, a: &ArrayBase<Si, Ix2>) -> ArrayBase<So, Ix2>
@@ -79,9 +67,16 @@ where
7967
Si: Data<Elem = A>,
8068
So: DataOwned<Elem = A> + DataMut,
8169
{
82-
let mut b = uninitialized(l);
83-
b.assign(a);
84-
b
70+
let shape_builder = match l {
71+
MatrixLayout::C { row, lda } => (row as usize, lda as usize).set_f(false),
72+
MatrixLayout::F { col, lda } => (lda as usize, col as usize).set_f(true),
73+
};
74+
unsafe {
75+
let ret = ArrayBase::<So, _>::build_uninit(shape_builder, |view| {
76+
a.assign_to(view);
77+
});
78+
ret.assume_init()
79+
}
8580
}
8681

8782
pub fn transpose_data<A, S>(a: &mut ArrayBase<S, Ix2>) -> Result<&mut ArrayBase<S, Ix2>>

ndarray-linalg/src/qr.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,7 @@ where
135135
S2: DataMut<Elem = A> + DataOwned,
136136
{
137137
let av = a.slice(s![..n as isize, ..m as isize]);
138-
let mut a = unsafe { ArrayBase::uninitialized((n, m)) };
139-
a.assign(&av);
140-
a
138+
replicate(&av)
141139
}
142140

143141
fn take_slice_upper<A, S1, S2>(a: &ArrayBase<S1, Ix2>, n: usize, m: usize) -> ArrayBase<S2, Ix2>
@@ -146,10 +144,12 @@ where
146144
S1: Data<Elem = A>,
147145
S2: DataMut<Elem = A> + DataOwned,
148146
{
149-
let av = a.slice(s![..n as isize, ..m as isize]);
150-
let mut a = unsafe { ArrayBase::uninitialized((n, m)) };
151-
for ((i, j), val) in a.indexed_iter_mut() {
152-
*val = if i <= j { av[(i, j)] } else { A::zero() };
153-
}
147+
let av = a.slice(s![..n, ..m]);
148+
let mut a = replicate(&av);
149+
Zip::indexed(&mut a).for_each(|(i, j), elt| {
150+
if i > j {
151+
*elt = A::zero()
152+
}
153+
});
154154
a
155155
}

0 commit comments

Comments
 (0)