Skip to content

Commit 4b36f97

Browse files
committed
Add data_transpose
1 parent 2040369 commit 4b36f97

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/layout.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl Layout {
6262
}
6363
}
6464

65-
pub fn t(&self) -> Self {
65+
pub fn toggle_order(&self) -> Self {
6666
match *self {
6767
Layout::C((row, col)) => Layout::F((col, row)),
6868
Layout::F((col, row)) => Layout::C((row, col)),
@@ -121,8 +121,26 @@ impl<A, S> AllocatedArrayMut for ArrayBase<S, Ix2>
121121
}
122122
}
123123

124+
pub fn into_col_vec<S>(a: ArrayBase<S, Ix1>) -> ArrayBase<S, Ix2>
125+
where S: Data
126+
{
127+
let n = a.len();
128+
a.into_shape((n, 1)).unwrap()
129+
}
124130

131+
pub fn into_row_vec<S>(a: ArrayBase<S, Ix1>) -> ArrayBase<S, Ix2>
132+
where S: Data
133+
{
134+
let n = a.len();
135+
a.into_shape((1, n)).unwrap()
136+
}
125137

138+
pub fn into_vec<S>(a: ArrayBase<S, Ix2>) -> ArrayBase<S, Ix1>
139+
where S: Data
140+
{
141+
let n = a.len();
142+
a.into_shape((n)).unwrap()
143+
}
126144

127145
pub fn reconstruct<A, S>(l: Layout, a: Vec<A>) -> Result<ArrayBase<S, Ix2>>
128146
where S: DataOwned<Elem = A>
@@ -157,3 +175,13 @@ pub fn clone_with_layout<A, Si, So>(l: Layout, a: &ArrayBase<Si, Ix2>) -> ArrayB
157175
b.assign(a);
158176
b
159177
}
178+
179+
pub fn data_transpose<A, S>(a: &mut ArrayBase<S, Ix2>) -> Result<&mut ArrayBase<S, Ix2>>
180+
where A: Copy,
181+
S: DataOwned<Elem = A> + DataMut
182+
{
183+
let l = a.layout()?.toggle_order();
184+
let new = clone_with_layout(l, a);
185+
::std::mem::replace(a, new);
186+
Ok(a)
187+
}

0 commit comments

Comments
 (0)