1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
//! Example of simple math operations on 2-D arrays. //! //! <table> //! //! <tr> //! <th> //! //! NumPy //! //! </th> //! <th> //! //! `ndarray` //! //! </th> //! </tr> //! <tr> //! <td> //! //! ```python //! import numpy as np //! //! //! //! //! a = np.full((5, 4), 3.) //! //! //! a[::2, :] = 2. //! //! //! a[:, 1] = np.sin(a[:, 1]) + 1. //! //! //! a[a < 1.5] = 4. //! //! //! odd_sum = a[:, 1::2].sum() //! //! //! b = np.exp(np.arange(4)) //! //! //! c = a + b //! //! //! d = c.T.dot(a) //! ``` //! //! </td> //! <td> //! //! ``` //! use ndarray::prelude::*; //! //! # fn main() { //! // Create a 5×4 array of threes. //! let mut a = Array2::<f64>::from_elem((5, 4), 3.); //! //! // Fill the even-index rows with twos. //! a.slice_mut(s![..;2, ..]).fill(2.); //! //! // Change column 1 to sin(x) + 1. //! a.column_mut(1).mapv_inplace(|x| x.sin() + 1.); //! //! // Change values less than 1.5 to 4. //! a.mapv_inplace(|x| if x < 1.5 { 4. } else { x }); //! //! // Compute the sum of the odd-index columns. //! let odd_sum = a.slice(s![.., 1..;2]).sum(); //! //! // Create a 1-D array of exp(index). //! let b = Array::from_shape_fn(4, |i| (i as f64).exp()); //! //! // Add b to a (broadcasting to rows). //! let c = a + &b; //! //! // Matrix product of c transpose with c. //! let d = c.t().dot(&c); //! # } //! ``` //! //! </td> //! </tr> //! </table>