Skip to content

Commit 17a8d25

Browse files
author
Nil Goyette
authored
Merge pull request #1246 from Ph03nixStyle/master
Update README-quick-start.md
2 parents c5bb8b6 + a26a17e commit 17a8d25

File tree

1 file changed

+25
-38
lines changed

1 file changed

+25
-38
lines changed

README-quick-start.md

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ You can use [play.integer32.com](https://play.integer32.com/) to immediately try
77

88
## The Basics
99

10-
Just create your first 2x3 floating-point ndarray
10+
You can create your first 2x3 floating-point ndarray as such:
1111
```rust
1212
use ndarray::prelude::*;
1313

@@ -24,7 +24,7 @@ fn main() {
2424
println!("{:?}", a);
2525
}
2626
```
27-
This code will create a simple array and output to stdout:
27+
This code will create a simple array, then print it to stdout as such:
2828
```
2929
[[1.0, 2.0, 3.0],
3030
[4.0, 5.0, 6.0]], shape=[2, 3], strides=[3, 1], layout=C (0x1), const ndim=2
@@ -34,8 +34,7 @@ This code will create a simple array and output to stdout:
3434

3535
### Element type and dimensionality
3636

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:
3938
```rust
4039
use ndarray::prelude::*;
4140
use ndarray::Array;
@@ -44,13 +43,13 @@ fn main() {
4443
println!("{:?}", a);
4544
}
4645
```
47-
gives
46+
Unfortunately, this code does not compile.
4847
```
4948
| let a = Array::zeros((3, 2, 4).f());
5049
| - ^^^^^^^^^^^^ cannot infer type for type parameter `A`
5150
```
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:
5453

5554
```rust
5655
use ndarray::prelude::*;
@@ -60,7 +59,7 @@ fn main() {
6059
println!("{:?}", a);
6160
}
6261
```
63-
and now it works:
62+
This code now compiles to what we wanted:
6463
```
6564
[[[0.0, 0.0, 0.0, 0.0],
6665
[0.0, 0.0, 0.0, 0.0]],
@@ -72,24 +71,11 @@ and now it works:
7271
[0.0, 0.0, 0.0, 0.0]]], shape=[3, 2, 4], strides=[1, 3, 6], layout=F (0x2), const ndim=3
7372
```
7473

75-
We can also specify its dimensionality
74+
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.
7675

77-
```rust
78-
use ndarray::prelude::*;
79-
use ndarray::{Array, Ix3};
80-
fn main() {
81-
let a = 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
9177

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:
9379

9480
```rust
9581
use ndarray::{Array, Ix3};
@@ -99,7 +85,7 @@ fn main() {
9985
}
10086
```
10187

102-
### Some common create helper functions
88+
### Some common array initializing helper functions
10389
`linspace` - Create a 1-D array with 11 elements with values 0., …, 5.
10490
```rust
10591
use ndarray::prelude::*;
@@ -114,10 +100,11 @@ The output is:
114100
[0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0], shape=[11], strides=[1], layout=C | F (0x3), const ndim=1
115101
```
116102

117-
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)...
118104

119105
## Basic operations
120106

107+
Basic operations on arrays are all element-wise; you need to use specific methods for operations such as matrix multiplication (see later section).
121108
```rust
122109
use ndarray::prelude::*;
123110
use ndarray::Array;
@@ -136,16 +123,19 @@ fn main() {
136123
}
137124
```
138125

139-
Try remove all the `&` sign in front of `a` and `b`, does it still compile? Why?
140126

141-
Note that
127+
Note that (for any binary operator `@`):
142128
* `&A @ &A` produces a new `Array`
143129
* `B @ A` consumes `B`, updates it with the result, and returns it
144130
* `B @ &A` consumes `B`, updates it with the result, and returns it
145131
* `C @= &A` performs an arithmetic operation in place
146132

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+
147135
For more info checkout https://docs.rs/ndarray/latest/ndarray/struct.ArrayBase.html#arithmetic-operations
148136

137+
138+
149139
Some operations have `_axis` appended to the function name: they generally take in a parameter of type `Axis` as one of their inputs,
150140
such as `sum_axis`:
151141

@@ -237,7 +227,7 @@ The output is:
237227

238228
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)
239229

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:
241231
```rust
242232
use ndarray::prelude::*;
243233

@@ -250,8 +240,8 @@ fn main() {
250240
[110,112,113]]
251241
];
252242

253-
let a = a.mapv(|a: isize| a.pow(1)); // numpy equivlant of `a ** 1`;
254-
// This line does nothing but illustrate mapv with isize type
243+
let a = a.mapv(|a: isize| a.pow(1)); // numpy equivalent of `a ** 1`;
244+
// This line does nothing except illustrating mapv with isize type
255245
println!("a -> \n{}\n", a);
256246

257247
println!("`a.slice(s![1, .., ..])` -> \n{}\n", a.slice(s![1, .., ..]));
@@ -461,11 +451,8 @@ s2 =
461451

462452
## Copies and Views
463453
### View, Ref or Shallow Copy
464-
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.
468454

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.
469456
```rust
470457
use ndarray::prelude::*;
471458
use ndarray::{Array, Axis};
@@ -566,9 +553,9 @@ b clone of a =
566553
[2, 3]]
567554
```
568555

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.
570557

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).
572559

573560
## Broadcasting
574561

@@ -600,7 +587,7 @@ fn main() {
600587

601588
See [.broadcast()](https://docs.rs/ndarray/latest/ndarray/struct.ArrayBase.html#method.broadcast) for a more detailed description.
602589

603-
And there is a short example of it:
590+
And here is a short example of it:
604591
```rust
605592
use ndarray::prelude::*;
606593

0 commit comments

Comments
 (0)