@@ -4,7 +4,7 @@ use std::ptr::null_mut;
4
4
5
5
use ndarray:: { Dimension , IxDyn } ;
6
6
use pyo3:: types:: PyAnyMethods ;
7
- use pyo3:: { AsPyPointer , Bound , FromPyObject , PyNativeType , PyResult } ;
7
+ use pyo3:: { Borrowed , Bound , FromPyObject , PyNativeType , PyResult } ;
8
8
9
9
use crate :: array:: PyArray ;
10
10
use crate :: dtype:: Element ;
20
20
{
21
21
}
22
22
23
+ impl < ' py , T , D > ArrayOrScalar < ' py , T > for Bound < ' py , PyArray < T , D > >
24
+ where
25
+ T : Element ,
26
+ D : Dimension ,
27
+ {
28
+ }
29
+
23
30
impl < ' py , T > ArrayOrScalar < ' py , T > for T where T : Element + FromPyObject < ' py > { }
24
31
32
+ /// Deprecated form of [`inner_bound`]
33
+ #[ deprecated(
34
+ since = "0.21.0" ,
35
+ note = "will be replaced by `inner_bound` in the future"
36
+ ) ]
37
+ pub fn inner < ' py , T , DIN1 , DIN2 , OUT > (
38
+ array1 : & ' py PyArray < T , DIN1 > ,
39
+ array2 : & ' py PyArray < T , DIN2 > ,
40
+ ) -> PyResult < OUT >
41
+ where
42
+ T : Element ,
43
+ DIN1 : Dimension ,
44
+ DIN2 : Dimension ,
45
+ OUT : ArrayOrScalar < ' py , T > ,
46
+ {
47
+ inner_bound ( & array1. as_borrowed ( ) , & array2. as_borrowed ( ) )
48
+ }
49
+
25
50
/// Return the inner product of two arrays.
26
51
///
27
52
/// [NumPy's documentation][inner] has the details.
@@ -31,33 +56,33 @@ impl<'py, T> ArrayOrScalar<'py, T> for T where T: Element + FromPyObject<'py> {}
31
56
/// Note that this function can either return a scalar...
32
57
///
33
58
/// ```
34
- /// use pyo3::Python;
35
- /// use numpy::{inner , pyarray, PyArray0};
59
+ /// use pyo3::{ Python, PyNativeType} ;
60
+ /// use numpy::{inner_bound , pyarray, PyArray0};
36
61
///
37
62
/// Python::with_gil(|py| {
38
- /// let vector = pyarray![py, 1.0, 2.0, 3.0];
39
- /// let result: f64 = inner( vector, vector).unwrap();
63
+ /// let vector = pyarray![py, 1.0, 2.0, 3.0].as_borrowed() ;
64
+ /// let result: f64 = inner_bound(& vector, & vector).unwrap();
40
65
/// assert_eq!(result, 14.0);
41
66
/// });
42
67
/// ```
43
68
///
44
69
/// ...or an array depending on its arguments.
45
70
///
46
71
/// ```
47
- /// use pyo3::Python;
48
- /// use numpy::{inner , pyarray, PyArray0};
72
+ /// use pyo3::{ Python, Bound, PyNativeType} ;
73
+ /// use numpy::{inner_bound , pyarray, PyArray0, PyArrayMethods };
49
74
///
50
75
/// Python::with_gil(|py| {
51
- /// let vector = pyarray![py, 1, 2, 3];
52
- /// let result: & PyArray0<_> = inner( vector, vector).unwrap();
76
+ /// let vector = pyarray![py, 1, 2, 3].as_borrowed() ;
77
+ /// let result: Bound<'_, PyArray0<_>> = inner_bound(& vector, & vector).unwrap();
53
78
/// assert_eq!(result.item(), 14);
54
79
/// });
55
80
/// ```
56
81
///
57
82
/// [inner]: https://numpy.org/doc/stable/reference/generated/numpy.inner.html
58
- pub fn inner < ' py , T , DIN1 , DIN2 , OUT > (
59
- array1 : & ' py PyArray < T , DIN1 > ,
60
- array2 : & ' py PyArray < T , DIN2 > ,
83
+ pub fn inner_bound < ' py , T , DIN1 , DIN2 , OUT > (
84
+ array1 : & Bound < ' py , PyArray < T , DIN1 > > ,
85
+ array2 : & Bound < ' py , PyArray < T , DIN2 > > ,
61
86
) -> PyResult < OUT >
62
87
where
63
88
T : Element ,
73
98
obj. extract ( )
74
99
}
75
100
101
+ /// Deprecated form of [`dot_bound`]
102
+ #[ deprecated(
103
+ since = "0.21.0" ,
104
+ note = "will be replaced by `dot_bound` in the future"
105
+ ) ]
106
+ pub fn dot < ' py , T , DIN1 , DIN2 , OUT > (
107
+ array1 : & ' py PyArray < T , DIN1 > ,
108
+ array2 : & ' py PyArray < T , DIN2 > ,
109
+ ) -> PyResult < OUT >
110
+ where
111
+ T : Element ,
112
+ DIN1 : Dimension ,
113
+ DIN2 : Dimension ,
114
+ OUT : ArrayOrScalar < ' py , T > ,
115
+ {
116
+ dot_bound ( & array1. as_borrowed ( ) , & array2. as_borrowed ( ) )
117
+ }
118
+
76
119
/// Return the dot product of two arrays.
77
120
///
78
121
/// [NumPy's documentation][dot] has the details.
@@ -82,15 +125,15 @@ where
82
125
/// Note that this function can either return an array...
83
126
///
84
127
/// ```
85
- /// use pyo3::Python;
128
+ /// use pyo3::{ Python, Bound, PyNativeType} ;
86
129
/// use ndarray::array;
87
- /// use numpy::{dot , pyarray, PyArray2};
130
+ /// use numpy::{dot_bound , pyarray, PyArray2, PyArrayMethods };
88
131
///
89
132
/// Python::with_gil(|py| {
90
- /// let matrix = pyarray![py, [1, 0], [0, 1]];
91
- /// let another_matrix = pyarray![py, [4, 1], [2, 2]];
133
+ /// let matrix = pyarray![py, [1, 0], [0, 1]].as_borrowed() ;
134
+ /// let another_matrix = pyarray![py, [4, 1], [2, 2]].as_borrowed() ;
92
135
///
93
- /// let result: & PyArray2<_> = numpy::dot( matrix, another_matrix).unwrap();
136
+ /// let result: Bound<'_, PyArray2<_>> = dot_bound(& matrix, & another_matrix).unwrap();
94
137
///
95
138
/// assert_eq!(
96
139
/// result.readonly().as_array(),
@@ -102,20 +145,20 @@ where
102
145
/// ...or a scalar depending on its arguments.
103
146
///
104
147
/// ```
105
- /// use pyo3::Python;
106
- /// use numpy::{dot , pyarray, PyArray0};
148
+ /// use pyo3::{ Python, PyNativeType} ;
149
+ /// use numpy::{dot_bound , pyarray, PyArray0};
107
150
///
108
151
/// Python::with_gil(|py| {
109
- /// let vector = pyarray![py, 1.0, 2.0, 3.0];
110
- /// let result: f64 = dot( vector, vector).unwrap();
152
+ /// let vector = pyarray![py, 1.0, 2.0, 3.0].as_borrowed() ;
153
+ /// let result: f64 = dot_bound(& vector, & vector).unwrap();
111
154
/// assert_eq!(result, 14.0);
112
155
/// });
113
156
/// ```
114
157
///
115
158
/// [dot]: https://numpy.org/doc/stable/reference/generated/numpy.dot.html
116
- pub fn dot < ' py , T , DIN1 , DIN2 , OUT > (
117
- array1 : & ' py PyArray < T , DIN1 > ,
118
- array2 : & ' py PyArray < T , DIN2 > ,
159
+ pub fn dot_bound < ' py , T , DIN1 , DIN2 , OUT > (
160
+ array1 : & Bound < ' py , PyArray < T , DIN1 > > ,
161
+ array2 : & Bound < ' py , PyArray < T , DIN2 > > ,
119
162
) -> PyResult < OUT >
120
163
where
121
164
T : Element ,
@@ -131,10 +174,30 @@ where
131
174
obj. extract ( )
132
175
}
133
176
177
+ /// Deprecated form of [`einsum_bound`]
178
+ #[ deprecated(
179
+ since = "0.21.0" ,
180
+ note = "will be replaced by `einsum_bound` in the future"
181
+ ) ]
182
+ pub fn einsum < ' py , T , OUT > ( subscripts : & str , arrays : & [ & ' py PyArray < T , IxDyn > ] ) -> PyResult < OUT >
183
+ where
184
+ T : Element ,
185
+ OUT : ArrayOrScalar < ' py , T > ,
186
+ {
187
+ // Safety: &PyArray<T, IxDyn> has the same size and layout in memory as
188
+ // Borrowed<'_, '_, PyArray<T, IxDyn>>
189
+ einsum_bound ( subscripts, unsafe {
190
+ std:: slice:: from_raw_parts ( arrays. as_ptr ( ) . cast ( ) , arrays. len ( ) )
191
+ } )
192
+ }
193
+
134
194
/// Return the Einstein summation convention of given tensors.
135
195
///
136
196
/// This is usually invoked via the the [`einsum!`][crate::einsum!] macro.
137
- pub fn einsum < ' py , T , OUT > ( subscripts : & str , arrays : & [ & ' py PyArray < T , IxDyn > ] ) -> PyResult < OUT >
197
+ pub fn einsum_bound < ' py , T , OUT > (
198
+ subscripts : & str ,
199
+ arrays : & [ Borrowed < ' _ , ' py , PyArray < T , IxDyn > > ] ,
200
+ ) -> PyResult < OUT >
138
201
where
139
202
T : Element ,
140
203
OUT : ArrayOrScalar < ' py , T > ,
@@ -161,6 +224,20 @@ where
161
224
obj. extract ( )
162
225
}
163
226
227
+ /// Deprecated form of [`einsum_bound!`]
228
+ #[ deprecated(
229
+ since = "0.21.0" ,
230
+ note = "will be replaced by `einsum_bound!` in the future"
231
+ ) ]
232
+ #[ macro_export]
233
+ macro_rules! einsum {
234
+ ( $subscripts: literal $( , $array: ident) + $( , ) * ) => { {
235
+ use pyo3:: PyNativeType ;
236
+ let arrays = [ $( $array. to_dyn( ) . as_borrowed( ) , ) +] ;
237
+ $crate:: einsum_bound( concat!( $subscripts, "\0 " ) , & arrays)
238
+ } } ;
239
+ }
240
+
164
241
/// Return the Einstein summation convention of given tensors.
165
242
///
166
243
/// For more about the Einstein summation convention, please refer to
@@ -169,15 +246,15 @@ where
169
246
/// # Example
170
247
///
171
248
/// ```
172
- /// use pyo3::Python;
249
+ /// use pyo3::{ Python, Bound, PyNativeType} ;
173
250
/// use ndarray::array;
174
- /// use numpy::{einsum , pyarray, PyArray, PyArray2, PyArrayMethods};
251
+ /// use numpy::{einsum_bound , pyarray, PyArray, PyArray2, PyArrayMethods};
175
252
///
176
253
/// Python::with_gil(|py| {
177
- /// let tensor = PyArray::arange_bound(py, 0, 2 * 3 * 4, 1).reshape([2, 3, 4]).unwrap().into_gil_ref() ;
178
- /// let another_tensor = pyarray![py, [20, 30], [40, 50], [60, 70]];
254
+ /// let tensor = PyArray::arange_bound(py, 0, 2 * 3 * 4, 1).reshape([2, 3, 4]).unwrap();
255
+ /// let another_tensor = pyarray![py, [20, 30], [40, 50], [60, 70]].as_borrowed() ;
179
256
///
180
- /// let result: & PyArray2<_> = einsum !("ijk,ji->ik", tensor, another_tensor).unwrap();
257
+ /// let result: Bound<'_, PyArray2<_>> = einsum_bound !("ijk,ji->ik", tensor, another_tensor).unwrap();
181
258
///
182
259
/// assert_eq!(
183
260
/// result.readonly().as_array(),
@@ -188,9 +265,9 @@ where
188
265
///
189
266
/// [einsum]: https://numpy.org/doc/stable/reference/generated/numpy.einsum.html
190
267
#[ macro_export]
191
- macro_rules! einsum {
268
+ macro_rules! einsum_bound {
192
269
( $subscripts: literal $( , $array: ident) + $( , ) * ) => { {
193
- let arrays = [ $( $array. to_dyn( ) , ) +] ;
194
- $crate:: einsum ( concat!( $subscripts, "\0 " ) , & arrays)
270
+ let arrays = [ $( $array. to_dyn( ) . as_borrowed ( ) , ) +] ;
271
+ $crate:: einsum_bound ( concat!( $subscripts, "\0 " ) , & arrays)
195
272
} } ;
196
273
}
0 commit comments