|
3 | 3 | use super::*;
|
4 | 4 |
|
5 | 5 | use core::convert::TryFrom;
|
6 |
| -use nalgebra::Dynamic as Dy; |
| 6 | +use nalgebra::Dyn; |
7 | 7 |
|
8 | 8 | /// ```
|
9 |
| -/// use nshare::ToNalgebra; |
| 9 | +/// use nshare::IntoNalgebra; |
10 | 10 | ///
|
11 |
| -/// let arr = ndarray::arr1(&[0.1, 0.2, 0.3, 0.4]); |
| 11 | +/// let arr = ndarray::array![0.1, 0.2, 0.3, 0.4]; |
12 | 12 | /// let m = arr.view().into_nalgebra();
|
13 | 13 | /// assert!(m.iter().eq(&[0.1, 0.2, 0.3, 0.4]));
|
14 | 14 | /// assert_eq!(m.shape(), (4, 1));
|
15 | 15 | /// ```
|
16 |
| -impl<'a, T> ToNalgebra for ndarray::ArrayView1<'a, T> |
| 16 | +impl<'a, T> IntoNalgebra for ndarray::ArrayView1<'a, T> |
17 | 17 | where
|
18 | 18 | T: nalgebra::Scalar,
|
19 | 19 | {
|
20 |
| - type Out = nalgebra::DVectorSlice<'a, T>; |
| 20 | + type Out = nalgebra::DVectorView<'a, T>; |
21 | 21 | fn into_nalgebra(self) -> Self::Out {
|
22 |
| - let len = Dy::new(self.len()); |
| 22 | + let len = Dyn(self.len()); |
23 | 23 | let ptr = self.as_ptr();
|
24 | 24 | let stride: usize = TryFrom::try_from(self.strides()[0]).expect("Negative stride");
|
25 | 25 | let storage = unsafe {
|
26 |
| - nalgebra::SliceStorage::from_raw_parts( |
| 26 | + nalgebra::ViewStorage::from_raw_parts( |
27 | 27 | ptr,
|
28 | 28 | (len, nalgebra::Const::<1>),
|
29 |
| - (nalgebra::Const::<1>, Dy::new(stride)), |
| 29 | + (nalgebra::Const::<1>, Dyn(stride)), |
30 | 30 | )
|
31 | 31 | };
|
32 | 32 | nalgebra::Matrix::from_data(storage)
|
33 | 33 | }
|
34 | 34 | }
|
35 | 35 | /// ```
|
36 |
| -/// use nshare::ToNalgebra; |
| 36 | +/// use nshare::IntoNalgebra; |
37 | 37 | ///
|
38 |
| -/// let mut arr = ndarray::arr1(&[0.1, 0.2, 0.3, 0.4]); |
| 38 | +/// let mut arr = ndarray::array![0.1, 0.2, 0.3, 0.4]; |
39 | 39 | /// let m = arr.view_mut().into_nalgebra();
|
40 | 40 | /// assert!(m.iter().eq(&[0.1, 0.2, 0.3, 0.4]));
|
41 | 41 | /// assert_eq!(m.shape(), (4, 1));
|
42 | 42 | /// ```
|
43 |
| -impl<'a, T> ToNalgebra for ndarray::ArrayViewMut1<'a, T> |
| 43 | +impl<'a, T> IntoNalgebra for ndarray::ArrayViewMut1<'a, T> |
44 | 44 | where
|
45 | 45 | T: nalgebra::Scalar,
|
46 | 46 | {
|
47 |
| - type Out = nalgebra::DVectorSliceMut<'a, T>; |
| 47 | + type Out = nalgebra::DVectorViewMut<'a, T>; |
48 | 48 | fn into_nalgebra(mut self) -> Self::Out {
|
49 |
| - let len = Dy::new(self.len()); |
| 49 | + let len = Dyn(self.len()); |
50 | 50 | let stride: usize = TryFrom::try_from(self.strides()[0]).expect("Negative stride");
|
51 | 51 | let ptr = self.as_mut_ptr();
|
52 | 52 | let storage = unsafe {
|
53 |
| - // Drop to not have simultaneously the ndarray and nalgebra valid. |
54 |
| - drop(self); |
55 |
| - nalgebra::SliceStorageMut::from_raw_parts( |
| 53 | + nalgebra::ViewStorageMut::from_raw_parts( |
56 | 54 | ptr,
|
57 | 55 | (len, nalgebra::Const::<1>),
|
58 |
| - (nalgebra::Const::<1>, Dy::new(stride)), |
| 56 | + (nalgebra::Const::<1>, Dyn(stride)), |
59 | 57 | )
|
60 | 58 | };
|
61 | 59 | nalgebra::Matrix::from_data(storage)
|
62 | 60 | }
|
63 | 61 | }
|
64 | 62 |
|
65 | 63 | /// ```
|
66 |
| -/// use nshare::ToNalgebra; |
| 64 | +/// use nshare::IntoNalgebra; |
67 | 65 | ///
|
68 |
| -/// let arr = ndarray::arr1(&[0.1, 0.2, 0.3, 0.4]); |
| 66 | +/// let arr = ndarray::array![0.1, 0.2, 0.3, 0.4]; |
69 | 67 | /// let m = arr.into_nalgebra();
|
70 | 68 | /// assert!(m.iter().eq(&[0.1, 0.2, 0.3, 0.4]));
|
71 | 69 | /// assert_eq!(m.shape(), (4, 1));
|
72 | 70 | /// ```
|
73 |
| -impl<T> ToNalgebra for ndarray::Array1<T> |
| 71 | +impl<T> IntoNalgebra for ndarray::Array1<T> |
74 | 72 | where
|
75 | 73 | T: nalgebra::Scalar,
|
76 | 74 | {
|
77 | 75 | type Out = nalgebra::DVector<T>;
|
78 | 76 | fn into_nalgebra(self) -> Self::Out {
|
79 |
| - let len = Dy::new(self.len()); |
80 |
| - Self::Out::from_vec_generic(len, nalgebra::Const::<1>, self.into_raw_vec()) |
| 77 | + let len = Dyn(self.len()); |
| 78 | + // There is no method to give nalgebra the vector directly where it isn't allocated. If you call |
| 79 | + // from_vec_generic, it simply calls from_iterator_generic which uses Iterator::collect(). Due to this, |
| 80 | + // the simplest solution is to just pass an iterator over the values. If you come across this because you |
| 81 | + // have a performance issue, I would recommend creating the owned data using naglebra and borrowing it with |
| 82 | + // ndarray to perform operations on it instead of the other way around. |
| 83 | + Self::Out::from_iterator_generic(len, nalgebra::Const::<1>, self.iter().cloned()) |
81 | 84 | }
|
82 | 85 | }
|
83 | 86 |
|
84 | 87 | /// ```
|
85 |
| -/// use nshare::ToNalgebra; |
| 88 | +/// use nshare::IntoNalgebra; |
86 | 89 | ///
|
87 |
| -/// let arr = ndarray::arr2(&[ |
| 90 | +/// let arr = ndarray::array![ |
88 | 91 | /// [0.1, 0.2, 0.3, 0.4],
|
89 | 92 | /// [0.5, 0.6, 0.7, 0.8],
|
90 | 93 | /// [1.1, 1.2, 1.3, 1.4],
|
91 | 94 | /// [1.5, 1.6, 1.7, 1.8],
|
92 |
| -/// ]); |
| 95 | +/// ]; |
93 | 96 | /// let m = arr.view().into_nalgebra();
|
94 | 97 | /// assert!(m.row(1).iter().eq(&[0.5, 0.6, 0.7, 0.8]));
|
95 | 98 | /// assert_eq!(m.shape(), (4, 4));
|
96 | 99 | /// assert!(arr.t().into_nalgebra().column(1).iter().eq(&[0.5, 0.6, 0.7, 0.8]));
|
97 | 100 | /// ```
|
98 |
| -impl<'a, T> ToNalgebra for ndarray::ArrayView2<'a, T> |
| 101 | +impl<'a, T> IntoNalgebra for ndarray::ArrayView2<'a, T> |
99 | 102 | where
|
100 | 103 | T: nalgebra::Scalar,
|
101 | 104 | {
|
102 |
| - type Out = nalgebra::DMatrixSlice<'a, T, Dy, Dy>; |
| 105 | + type Out = nalgebra::DMatrixView<'a, T, Dyn, Dyn>; |
103 | 106 | fn into_nalgebra(self) -> Self::Out {
|
104 |
| - let nrows = Dy::new(self.nrows()); |
105 |
| - let ncols = Dy::new(self.ncols()); |
| 107 | + let nrows = Dyn(self.nrows()); |
| 108 | + let ncols = Dyn(self.ncols()); |
106 | 109 | let ptr = self.as_ptr();
|
107 |
| - let stride_row: usize = TryFrom::try_from(self.strides()[0]).expect("Negative row stride"); |
108 |
| - let stride_col: usize = |
109 |
| - TryFrom::try_from(self.strides()[1]).expect("Negative column stride"); |
| 110 | + let stride_row: usize = TryFrom::try_from(self.strides()[0]) |
| 111 | + .expect("can only convert positive row stride to nalgebra"); |
| 112 | + let stride_col: usize = TryFrom::try_from(self.strides()[1]) |
| 113 | + .expect("can only convert positive col stride to nalgebra"); |
110 | 114 | let storage = unsafe {
|
111 |
| - nalgebra::SliceStorage::from_raw_parts( |
| 115 | + nalgebra::ViewStorage::from_raw_parts( |
112 | 116 | ptr,
|
113 | 117 | (nrows, ncols),
|
114 |
| - (Dy::new(stride_row), Dy::new(stride_col)), |
| 118 | + (Dyn(stride_row), Dyn(stride_col)), |
115 | 119 | )
|
116 | 120 | };
|
117 | 121 | nalgebra::Matrix::from_data(storage)
|
118 | 122 | }
|
119 | 123 | }
|
120 | 124 |
|
121 | 125 | /// ```
|
122 |
| -/// use nshare::ToNalgebra; |
| 126 | +/// use nshare::IntoNalgebra; |
123 | 127 | ///
|
124 |
| -/// let mut arr = ndarray::arr2(&[ |
| 128 | +/// let mut arr = ndarray::array![ |
125 | 129 | /// [0.1, 0.2, 0.3, 0.4],
|
126 | 130 | /// [0.5, 0.6, 0.7, 0.8],
|
127 | 131 | /// [1.1, 1.2, 1.3, 1.4],
|
128 | 132 | /// [1.5, 1.6, 1.7, 1.8],
|
129 |
| -/// ]); |
| 133 | +/// ]; |
130 | 134 | /// let m = arr.view_mut().into_nalgebra();
|
131 | 135 | /// assert!(m.row(1).iter().eq(&[0.5, 0.6, 0.7, 0.8]));
|
132 | 136 | /// assert_eq!(m.shape(), (4, 4));
|
133 | 137 | /// assert!(arr.view_mut().reversed_axes().into_nalgebra().column(1).iter().eq(&[0.5, 0.6, 0.7, 0.8]));
|
134 | 138 | /// ```
|
135 |
| -impl<'a, T> ToNalgebra for ndarray::ArrayViewMut2<'a, T> |
| 139 | +impl<'a, T> IntoNalgebra for ndarray::ArrayViewMut2<'a, T> |
136 | 140 | where
|
137 | 141 | T: nalgebra::Scalar,
|
138 | 142 | {
|
139 |
| - type Out = nalgebra::DMatrixSliceMut<'a, T, Dy, Dy>; |
| 143 | + type Out = nalgebra::DMatrixViewMut<'a, T, Dyn, Dyn>; |
140 | 144 | fn into_nalgebra(mut self) -> Self::Out {
|
141 |
| - let nrows = Dy::new(self.nrows()); |
142 |
| - let ncols = Dy::new(self.ncols()); |
143 |
| - let stride_row: usize = TryFrom::try_from(self.strides()[0]).expect("Negative row stride"); |
144 |
| - let stride_col: usize = |
145 |
| - TryFrom::try_from(self.strides()[1]).expect("Negative column stride"); |
| 145 | + let nrows = Dyn(self.nrows()); |
| 146 | + let ncols = Dyn(self.ncols()); |
| 147 | + let stride_row: usize = TryFrom::try_from(self.strides()[0]) |
| 148 | + .expect("can only convert positive row stride to nalgebra"); |
| 149 | + let stride_col: usize = TryFrom::try_from(self.strides()[1]) |
| 150 | + .expect("can only convert positive col stride to nalgebra"); |
146 | 151 | let ptr = self.as_mut_ptr();
|
147 | 152 | let storage = unsafe {
|
148 |
| - // Drop to not have simultaneously the ndarray and nalgebra valid. |
149 |
| - drop(self); |
150 |
| - nalgebra::SliceStorageMut::from_raw_parts( |
| 153 | + nalgebra::ViewStorageMut::from_raw_parts( |
151 | 154 | ptr,
|
152 | 155 | (nrows, ncols),
|
153 |
| - (Dy::new(stride_row), Dy::new(stride_col)), |
| 156 | + (Dyn(stride_row), Dyn(stride_col)), |
154 | 157 | )
|
155 | 158 | };
|
156 | 159 | nalgebra::Matrix::from_data(storage)
|
157 | 160 | }
|
158 | 161 | }
|
159 | 162 |
|
160 | 163 | /// ```
|
161 |
| -/// use nshare::ToNalgebra; |
| 164 | +/// use nshare::IntoNalgebra; |
162 | 165 | ///
|
163 |
| -/// let mut arr = ndarray::arr2(&[ |
| 166 | +/// let mut arr = ndarray::array![ |
164 | 167 | /// [0.1, 0.2, 0.3, 0.4],
|
165 | 168 | /// [0.5, 0.6, 0.7, 0.8],
|
166 | 169 | /// [1.1, 1.2, 1.3, 1.4],
|
167 | 170 | /// [1.5, 1.6, 1.7, 1.8],
|
168 |
| -/// ]); |
| 171 | +/// ]; |
169 | 172 | /// let m = arr.clone().into_nalgebra();
|
170 | 173 | /// assert!(m.row(1).iter().eq(&[0.5, 0.6, 0.7, 0.8]));
|
171 | 174 | /// assert_eq!(m.shape(), (4, 4));
|
172 | 175 | /// assert!(arr.reversed_axes().into_nalgebra().column(1).iter().eq(&[0.5, 0.6, 0.7, 0.8]));
|
173 | 176 | /// ```
|
174 |
| -impl<T> ToNalgebra for ndarray::Array2<T> |
| 177 | +impl<T> IntoNalgebra for ndarray::Array2<T> |
175 | 178 | where
|
176 | 179 | T: nalgebra::Scalar,
|
177 | 180 | {
|
178 | 181 | type Out = nalgebra::DMatrix<T>;
|
179 | 182 | fn into_nalgebra(self) -> Self::Out {
|
180 |
| - let std_layout = self.is_standard_layout(); |
181 |
| - let nrows = Dy::new(self.nrows()); |
182 |
| - let ncols = Dy::new(self.ncols()); |
183 |
| - let mut res = Self::Out::from_vec_generic(nrows, ncols, self.into_raw_vec()); |
184 |
| - if std_layout { |
185 |
| - // This can be expensive, but we have no choice since nalgebra VecStorage is always |
186 |
| - // column-based. |
187 |
| - res.transpose_mut(); |
188 |
| - } |
189 |
| - res |
| 183 | + let nrows = Dyn(self.nrows()); |
| 184 | + let ncols = Dyn(self.ncols()); |
| 185 | + Self::Out::from_iterator_generic(nrows, ncols, self.t().iter().cloned()) |
190 | 186 | }
|
191 | 187 | }
|
0 commit comments