1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use ndarray::*;
use super::error::*;
use super::layout::*;
pub fn into_col<S>(a: ArrayBase<S, Ix1>) -> ArrayBase<S, Ix2>
where
S: Data,
{
let n = a.len();
a.into_shape((n, 1)).unwrap()
}
pub fn into_row<S>(a: ArrayBase<S, Ix1>) -> ArrayBase<S, Ix2>
where
S: Data,
{
let n = a.len();
a.into_shape((1, n)).unwrap()
}
pub fn flatten<S>(a: ArrayBase<S, Ix2>) -> ArrayBase<S, Ix1>
where
S: Data,
{
let n = a.len();
a.into_shape((n)).unwrap()
}
pub fn into_matrix<A, S>(l: MatrixLayout, a: Vec<A>) -> Result<ArrayBase<S, Ix2>>
where
S: DataOwned<Elem = A>,
{
Ok(ArrayBase::from_shape_vec(l.as_shape(), a)?)
}
fn uninitialized<A, S>(l: MatrixLayout) -> ArrayBase<S, Ix2>
where
A: Copy,
S: DataOwned<Elem = A>,
{
unsafe { ArrayBase::uninitialized(l.as_shape()) }
}
pub fn replicate<A, Sv, So, D>(a: &ArrayBase<Sv, D>) -> ArrayBase<So, D>
where
A: Copy,
Sv: Data<Elem = A>,
So: DataOwned<Elem = A> + DataMut,
D: Dimension,
{
let mut b = unsafe { ArrayBase::uninitialized(a.dim()) };
b.assign(a);
b
}
fn clone_with_layout<A, Si, So>(l: MatrixLayout, a: &ArrayBase<Si, Ix2>) -> ArrayBase<So, Ix2>
where
A: Copy,
Si: Data<Elem = A>,
So: DataOwned<Elem = A> + DataMut,
{
let mut b = uninitialized(l);
b.assign(a);
b
}
pub fn transpose_data<A, S>(a: &mut ArrayBase<S, Ix2>) -> Result<&mut ArrayBase<S, Ix2>>
where
A: Copy,
S: DataOwned<Elem = A> + DataMut,
{
let l = a.layout()?.toggle_order();
let new = clone_with_layout(l, a);
::std::mem::replace(a, new);
Ok(a)
}
pub fn generalize<A, S, D>(a: Array<A, D>) -> ArrayBase<S, D>
where
S: DataOwned<Elem = A>,
D: Dimension,
{
let strides: Vec<isize> = a.strides().to_vec();
let new = if a.is_standard_layout() {
ArrayBase::from_shape_vec(a.dim(), a.into_raw_vec()).unwrap()
} else {
ArrayBase::from_shape_vec(a.dim().f(), a.into_raw_vec()).unwrap()
};
assert_eq!(
new.strides(),
strides.as_slice(),
"Custom stride is not supported"
);
new
}