@@ -10,7 +10,7 @@ use std::ptr::null_mut;
10
10
use super :: error:: ArrayCastError ;
11
11
use super :: * ;
12
12
13
- /// Untyped safe interface for NumPy ndarray.
13
+ /// Interface for [ NumPy ndarray](https://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html) .
14
14
pub struct PyArray < T > ( PyObject , PhantomData < T > ) ;
15
15
16
16
pyobject_native_type_convert ! (
@@ -53,16 +53,18 @@ impl<T> IntoPyObject for PyArray<T> {
53
53
}
54
54
55
55
impl < T > PyArray < T > {
56
- /// Get raw pointer for PyArrayObject
56
+ /// Gets a raw ` PyArrayObject` pointer.
57
57
pub fn as_array_ptr ( & self ) -> * mut npyffi:: PyArrayObject {
58
58
self . as_ptr ( ) as _
59
59
}
60
60
61
+ /// Constructs `PyArray` from raw python object without incrementing reference counts.
61
62
pub unsafe fn from_owned_ptr ( py : Python , ptr : * mut pyo3:: ffi:: PyObject ) -> Self {
62
63
let obj = PyObject :: from_owned_ptr ( py, ptr) ;
63
64
PyArray ( obj, PhantomData )
64
65
}
65
66
67
+ /// Constructs PyArray from raw python object and increments reference counts.
66
68
pub unsafe fn from_borrowed_ptr ( py : Python , ptr : * mut pyo3:: ffi:: PyObject ) -> Self {
67
69
let obj = PyObject :: from_borrowed_ptr ( py, ptr) ;
68
70
PyArray ( obj, PhantomData )
@@ -135,8 +137,6 @@ impl<T> PyArray<T> {
135
137
}
136
138
137
139
/// Same as [shape](./struct.PyArray.html#method.shape)
138
- ///
139
- /// Reserved for backward compatibility.
140
140
#[ inline]
141
141
pub fn dims ( & self ) -> & [ usize ] {
142
142
self . shape ( )
@@ -294,6 +294,7 @@ impl<T: TypeNum> PyArray<T> {
294
294
IntoPyArray :: into_pyarray ( arr, py, np)
295
295
}
296
296
297
+ /// Returns the pointer to the first element of the inner array.
297
298
unsafe fn data ( & self ) -> * mut T {
298
299
let ptr = self . as_array_ptr ( ) ;
299
300
( * ptr) . data as * mut T
@@ -310,13 +311,14 @@ impl<T: TypeNum> PyArray<T> {
310
311
shape. strides ( Dim ( st) )
311
312
}
312
313
313
- pub fn typenum ( & self ) -> i32 {
314
+ fn typenum ( & self ) -> i32 {
314
315
unsafe {
315
316
let descr = ( * self . as_array_ptr ( ) ) . descr ;
316
317
( * descr) . type_num
317
318
}
318
319
}
319
320
321
+ /// Returns the scalar type of the array.
320
322
pub fn data_type ( & self ) -> NpyDataType {
321
323
NpyDataType :: from_i32 ( self . typenum ( ) )
322
324
}
@@ -360,6 +362,9 @@ impl<T: TypeNum> PyArray<T> {
360
362
unsafe { Ok ( :: std:: slice:: from_raw_parts_mut ( self . data ( ) , self . len ( ) ) ) }
361
363
}
362
364
365
+ /// Construct a new PyArray given a raw pointer and dimensions.
366
+ ///
367
+ /// Please use `new` or from methods instead.
363
368
pub unsafe fn new_ (
364
369
py : Python ,
365
370
np : & PyArrayModule ,
@@ -382,27 +387,69 @@ impl<T: TypeNum> PyArray<T> {
382
387
Self :: from_owned_ptr ( py, ptr)
383
388
}
384
389
385
- /// a wrapper of [PyArray_SimpleNew](https://docs.scipy.org/doc/numpy/reference/c-api.array.html#c.PyArray_SimpleNew)
390
+ /// Creates a new uninitialized array.
391
+ ///
392
+ /// See also [PyArray_SimpleNew](https://docs.scipy.org/doc/numpy/reference/c-api.array.html#c.PyArray_SimpleNew).
393
+ ///
394
+ /// # Example
395
+ /// ```
396
+ /// # extern crate pyo3; extern crate numpy; #[macro_use] extern crate ndarray; fn main() {
397
+ /// use numpy::{PyArray, PyArrayModule};
398
+ /// let gil = pyo3::Python::acquire_gil();
399
+ /// let np = PyArrayModule::import(gil.python()).unwrap();
400
+ /// let pyarray = PyArray::new(gil.python(), &np, &[2, 2]);
401
+ /// assert_eq!(pyarray.as_array().unwrap(), array![[0, 0], [0, 0]].into_dyn());
402
+ /// # }
403
+ /// ```
386
404
pub fn new ( py : Python , np : & PyArrayModule , dims : & [ usize ] ) -> Self {
387
405
unsafe { Self :: new_ ( py, np, dims, null_mut ( ) , null_mut ( ) ) }
388
406
}
389
407
390
- /// a wrapper of [PyArray_ZEROS](https://docs.scipy.org/doc/numpy/reference/c-api.array.html#c.PyArray_ZEROS)
391
- pub fn zeros ( py : Python , np : & PyArrayModule , dims : & [ usize ] , order : NPY_ORDER ) -> Self {
408
+ /// Construct a new nd-dimensional array filled with 0. If `is_fortran` is true, then
409
+ /// a fortran order array is created, otherwise a C-order array is created.
410
+ ///
411
+ /// See also [PyArray_Zeros](https://docs.scipy.org/doc/numpy/reference/c-api.array.html#c.PyArray_Zeros)
412
+ ///
413
+ /// # Example
414
+ /// ```
415
+ /// # extern crate pyo3; extern crate numpy; #[macro_use] extern crate ndarray; fn main() {
416
+ /// use numpy::{PyArray, PyArrayModule};
417
+ /// let gil = pyo3::Python::acquire_gil();
418
+ /// let np = PyArrayModule::import(gil.python()).unwrap();
419
+ /// let pyarray = PyArray::zeros(gil.python(), &np, &[2, 2], false);
420
+ /// assert_eq!(pyarray.as_array().unwrap(), array![[0, 0], [0, 0]].into_dyn());
421
+ /// # }
422
+ /// ```
423
+ pub fn zeros ( py : Python , np : & PyArrayModule , dims : & [ usize ] , is_fortran : bool ) -> Self {
392
424
let dims: Vec < npy_intp > = dims. iter ( ) . map ( |d| * d as npy_intp ) . collect ( ) ;
393
425
unsafe {
394
426
let descr = np. PyArray_DescrFromType ( T :: typenum ( ) ) ;
395
427
let ptr = np. PyArray_Zeros (
396
428
dims. len ( ) as i32 ,
397
429
dims. as_ptr ( ) as * mut npy_intp ,
398
430
descr,
399
- order as i32 ,
431
+ if is_fortran { - 1 } else { 0 } ,
400
432
) ;
401
433
Self :: from_owned_ptr ( py, ptr)
402
434
}
403
435
}
404
436
405
- /// a wrapper of [PyArray_Arange](https://docs.scipy.org/doc/numpy/reference/c-api.array.html#c.PyArray_Arange)
437
+ /// Return evenly spaced values within a given interval.
438
+ /// Same as [numpy.arange](https://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html).
439
+ ///
440
+ /// See also [PyArray_Arange](https://docs.scipy.org/doc/numpy/reference/c-api.array.html#c.PyArray_Arange).
441
+ ///
442
+ /// # Example
443
+ /// ```
444
+ /// # extern crate pyo3; extern crate numpy; fn main() {
445
+ /// use numpy::{PyArray, PyArrayModule, IntoPyArray};
446
+ /// let gil = pyo3::Python::acquire_gil();
447
+ /// let np = PyArrayModule::import(gil.python()).unwrap();
448
+ /// let pyarray = PyArray::<f64>::arange(gil.python(), &np, 2.0, 4.0, 0.5);
449
+ /// assert_eq!(pyarray.as_slice().unwrap(), &[2.0, 2.5, 3.0, 3.5]);
450
+ /// let pyarray = PyArray::<i32>::arange(gil.python(), &np, -2.0, 4.0, 3.0);
451
+ /// assert_eq!(pyarray.as_slice().unwrap(), &[-2, 1]);
452
+ /// # }
406
453
pub fn arange ( py : Python , np : & PyArrayModule , start : f64 , stop : f64 , step : f64 ) -> Self {
407
454
unsafe {
408
455
let ptr = np. PyArray_Arange ( start, stop, step, T :: typenum ( ) ) ;
0 commit comments