You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -34,8 +34,7 @@ This code will create a simple array and output to stdout:
34
34
35
35
### Element type and dimensionality
36
36
37
-
Now let's create more arrays. How about try make a zero array with dimension of (3, 2, 4)?
38
-
37
+
Now let's create more arrays. A common operation on matrices is to create a matrix full of 0's of certain dimensions. Let's try to do that with dimensions (3, 2, 4) using the `Array::zeros` function:
39
38
```rust
40
39
usendarray::prelude::*;
41
40
usendarray::Array;
@@ -44,13 +43,13 @@ fn main() {
44
43
println!("{:?}", a);
45
44
}
46
45
```
47
-
gives
46
+
Unfortunately, this code does not compile.
48
47
```
49
48
| let a = Array::zeros((3, 2, 4).f());
50
49
| - ^^^^^^^^^^^^ cannot infer type for type parameter `A`
51
50
```
52
-
Note that the compiler needs to infer the element type and dimensionality from context. In this
53
-
case the compiler failed to do that. Now we give it the type and let it infer dimensionality
51
+
Indeed, note that the compiler needs to infer the element type and dimensionality from context only. In this
52
+
case the compiler does not have enough information. To fix the code, we can explicitly give the element type through turbofish syntax, and let it infer the dimensionality type:
We could also specify its dimensionality explicitly `Array::<f64, Ix3>::zeros(...)`, with`Ix3` standing for 3D array type. Phew! We achieved type safety. If you tried changing the code above to `Array::<f64, Ix3>::zeros((3, 2, 4, 5).f());`, which is not of dimension 3 anymore, Rust's type system would gracefully prevent you from compiling the code.
76
75
77
-
```rust
78
-
usendarray::prelude::*;
79
-
usendarray::{Array, Ix3};
80
-
fnmain() {
81
-
leta=Array::<f64, Ix3>::zeros((3, 2, 4).f());
82
-
println!("{:?}", a);
83
-
}
84
-
```
85
-
`Ix3` stands for 3D array.
86
-
87
-
And now we are type checked. Try change the code above to `Array::<f64, Ix3>::zeros((3, 2, 4, 5).f());`
88
-
and compile, see what happens.
89
-
90
-
### How about create array of different type and having different initial values?
76
+
### Creating arrays with different initial values and/or different types
91
77
92
-
The [`from_elem`](http://docs.rs/ndarray/latest/ndarray/struct.ArrayBase.html#method.from_elem) method can be handy here:
78
+
The [`from_elem`](http://docs.rs/ndarray/latest/ndarray/struct.ArrayBase.html#method.from_elem) method allows initializing an array of given dimension to a specific value of any type:
93
79
94
80
```rust
95
81
usendarray::{Array, Ix3};
@@ -99,7 +85,7 @@ fn main() {
99
85
}
100
86
```
101
87
102
-
### Some common create helper functions
88
+
### Some common array initializing helper functions
103
89
`linspace` - Create a 1-D array with 11 elements with values 0., …, 5.
And there are also `range`, `logspace`, `ones`, `eye` and so on you can choose to use.
103
+
Common array initializing methods include [`range`](https://docs.rs/ndarray/0.13.0/ndarray/struct.ArrayBase.html#method.range), [`logspace`](https://docs.rs/ndarray/0.13.0/ndarray/struct.ArrayBase.html#method.logspace), [`eye`](https://docs.rs/ndarray/0.13.0/ndarray/struct.ArrayBase.html#method.eye), [`ones`](https://docs.rs/ndarray/0.13.0/ndarray/struct.ArrayBase.html#method.ones)...
118
104
119
105
## Basic operations
120
106
107
+
Basic operations on arrays are all element-wise; you need to use specific methods for operations such as matrix multiplication (see later section).
121
108
```rust
122
109
usendarray::prelude::*;
123
110
usendarray::Array;
@@ -136,16 +123,19 @@ fn main() {
136
123
}
137
124
```
138
125
139
-
Try remove all the `&` sign in front of `a` and `b`, does it still compile? Why?
140
126
141
-
Note that
127
+
Note that (for any binary operator `@`):
142
128
*`&A @ &A` produces a new `Array`
143
129
*`B @ A` consumes `B`, updates it with the result, and returns it
144
130
*`B @ &A` consumes `B`, updates it with the result, and returns it
145
131
*`C @= &A` performs an arithmetic operation in place
146
132
133
+
Try removing all the `&` sign in front of `a` and `b` in the last example: it will not compile anymore because of those rules.
134
+
147
135
For more info checkout https://docs.rs/ndarray/latest/ndarray/struct.ArrayBase.html#arithmetic-operations
148
136
137
+
138
+
149
139
Some operations have `_axis` appended to the function name: they generally take in a parameter of type `Axis` as one of their inputs,
150
140
such as `sum_axis`:
151
141
@@ -237,7 +227,7 @@ The output is:
237
227
238
228
For more info about iteration see [Loops, Producers, and Iterators](https://docs.rs/ndarray/0.13.0/ndarray/struct.ArrayBase.html#loops-producers-and-iterators)
239
229
240
-
Let's try a 3D array with elements of type `isize`. This is how you index it:
230
+
Let's try a iterating over a 3D array with elements of type `isize`. This is how you index it:
241
231
```rust
242
232
usendarray::prelude::*;
243
233
@@ -250,8 +240,8 @@ fn main() {
250
240
[110,112,113]]
251
241
];
252
242
253
-
leta=a.mapv(|a:isize|a.pow(1)); // numpy equivlant of `a ** 1`;
254
-
// This line does nothing but illustrate mapv with isize type
243
+
leta=a.mapv(|a:isize|a.pow(1)); // numpy equivalent of `a ** 1`;
244
+
// This line does nothing except illustrating mapv with isize type
As in Rust we have owner ship, so we cannot simply
465
-
update an element of an array while we have a
466
-
shared view of it. This will help us write more
467
-
robust code.
468
454
455
+
Rust has ownership, so we cannot simply update an element of an array while we have a shared view of it. This brings guarantees & helps having more robust code.
469
456
```rust
470
457
usendarray::prelude::*;
471
458
usendarray::{Array, Axis};
@@ -566,9 +553,9 @@ b clone of a =
566
553
[2, 3]]
567
554
```
568
555
569
-
Noticing that using `clone()` (or cloning) an `Array` type also copies the array's elements. It creates an independently owned array of the same type.
556
+
Notice that using `clone()` (or cloning) an `Array` type also copies the array's elements. It creates an independently owned array of the same type.
570
557
571
-
Cloning an `ArrayView` does not clone or copy the underlying elements - it just clones the view reference (as it happens in Rust when cloning a `&` reference).
558
+
Cloning an `ArrayView` does not clone or copy the underlying elements - it only clones the view reference (as it happens in Rust when cloning a `&` reference).
572
559
573
560
## Broadcasting
574
561
@@ -600,7 +587,7 @@ fn main() {
600
587
601
588
See [.broadcast()](https://docs.rs/ndarray/latest/ndarray/struct.ArrayBase.html#method.broadcast) for a more detailed description.
Copy file name to clipboardExpand all lines: src/doc/ndarray_for_numpy_users/mod.rs
+3Lines changed: 3 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -258,6 +258,7 @@
258
258
//! `a[-5:]` or `a[-5:, :]` | [`a.slice(s![-5.., ..])`][.slice()] or [`a.slice_axis(Axis(0), Slice::from(-5..))`][.slice_axis()] | get the last 5 rows of a 2-D array
259
259
//! `a[:3, 4:9]` | [`a.slice(s![..3, 4..9])`][.slice()] | columns 4, 5, 6, 7, and 8 of the first 3 rows
260
260
//! `a[1:4:2, ::-1]` | [`a.slice(s![1..4;2, ..;-1])`][.slice()] | rows 1 and 3 with the columns in reverse order
261
+
//! `a.take([4, 2])` | `a.select(Axis(0), &[4, 2])` | rows 4 and 2 of the array
261
262
//!
262
263
//! ## Shape and strides
263
264
//!
@@ -531,6 +532,8 @@
531
532
//! ------|-----------|------
532
533
//! `a[:] = 3.` | [`a.fill(3.)`][.fill()] | set all array elements to the same scalar value
533
534
//! `a[:] = b` | [`a.assign(&b)`][.assign()] | copy the data from array `b` into array `a`
535
+
//! `a[:5, 2] = 3.` | [`a.slice_mut(s![..5, 2]).fill(3.)`][.fill()] | set a portion of the array to the same scalar value
536
+
//! `a[:5, 2] = b` | [`a.slice_mut(s![..5, 2]).assign(&b)`][.assign()] | copy the data from array `b` into part of array `a`
534
537
//! `np.concatenate((a,b), axis=1)` | [`concatenate![Axis(1), a, b]`][concatenate!] or [`concatenate(Axis(1), &[a.view(), b.view()])`][concatenate()] | concatenate arrays `a` and `b` along axis 1
535
538
//! `np.stack((a,b), axis=1)` | [`stack![Axis(1), a, b]`][stack!] or [`stack(Axis(1), vec![a.view(), b.view()])`][stack()] | stack arrays `a` and `b` along axis 1
536
539
//! `a[:,np.newaxis]` or `np.expand_dims(a, axis=1)` | [`a.slice(s![.., NewAxis])`][.slice()] or [`a.insert_axis(Axis(1))`][.insert_axis()] | create an view of 1-D array `a`, inserting a new axis 1
0 commit comments