mirror of
https://github.com/rust-lang-nursery/rust-cookbook
synced 2024-11-22 03:23:05 +00:00
parent
656e486ff6
commit
466b74ca72
2 changed files with 54 additions and 1 deletions
|
@ -7,14 +7,16 @@
|
|||
| [Mathematical functions on complex numbers][ex-mathematical-functions] | [![num-badge]][num] | [![cat-science-badge]][cat-science] |
|
||||
| [Calculating the side length of a triangle][ex-calculating-side-length-of-triangle] | [![std-badge]][std] | [![cat-science-badge]][cat-science] |
|
||||
| [Verifying tan is equal to sin divided by cos][ex-tan-equal-to-sin-divided-by-cos] | [![std-badge]][std] | [![cat-science-badge]][cat-science] |
|
||||
[ex-creating-complex-numbers]: science/mathematics/complex-numbers.html#creating-complex-numbers
|
||||
| [Vector Sum][ex-ndarray-vec-sum] | [![ndarray-badge]][ndarray] | [![cat-science-badge]][cat-science] |
|
||||
| [Distance between two points on the Earth][ex-latitude-longitude] | [![std-badge]][std] | [![cat-science-badge]][cat-science] |
|
||||
|
||||
[ex-creating-complex-numbers]: science/mathematics/complex-numbers.html#creating-complex-numbers
|
||||
[ex-adding-complex-numbers]: science/mathematics/complex-numbers.html#adding-complex-numbers
|
||||
[ex-creating-complex-numbers]: science/mathematics/complex-numbers.html#creating-complex-numbers
|
||||
[ex-latitude-longitude]: science/mathematics.html#distance-between-two-points-on-earth
|
||||
[ex-mathematical-functions]: science/mathematics/complex-numbers.html#mathematical-functions
|
||||
[ex-calculating-side-length-of-triangle]: science/mathematics/trigonometry.html#calculating-the-side-length-of-a-triangle
|
||||
[ex-tan-equal-to-sin-divided-by-cos]: science/mathematics/trigonometry.html#verifying-tan-is-equal-to-sin-divided-by-cos
|
||||
[ex-ndarray-vec-sum]: science/mathematics/linear-algebra/vector_sum.html
|
||||
|
||||
{{#include links.md}}
|
||||
|
|
51
src/science/mathematics/linear-algebra/vector_sum.md
Normal file
51
src/science/mathematics/linear-algebra/vector_sum.md
Normal file
|
@ -0,0 +1,51 @@
|
|||
## Vector Sum
|
||||
[![ndarray-badge]][ndarray]
|
||||
|
||||
The [ndarray] crate supports a number of ways to create arrays -- this recipe
|
||||
focuses on creating [`ndarray::Array`]s from `std::Vec` via [`from_vec`]. Adding two
|
||||
arrays together is no different than adding two numbers together. Using the `&`
|
||||
operand on the arrays within an arithmetic operation prevents the operation from
|
||||
consuming the arrays. Without `&`, the arrays are consumed.
|
||||
|
||||
In the first example, arrays `a` and `b` are moved in the let-statement `z = a +
|
||||
b`. In the second example, the arrays `c` and `d` are not moved and instead, a
|
||||
new array is created for `w`. Updating either of `c` or `d` after the vector sum
|
||||
has no effect the value of `w`. Additionally, while printing `c` works as
|
||||
expected, it would be an error to print `b` due to the move. See [Binary
|
||||
Operators With Two Arrays] for additional detail.
|
||||
|
||||
```rust
|
||||
extern crate ndarray;
|
||||
use ndarray::Array;
|
||||
|
||||
fn main() {
|
||||
let a = Array::from_vec(vec![1., 2., 3., 4., 5.]);
|
||||
let b = Array::from_vec(vec![5., 4., 3., 2., 1.]);
|
||||
let mut c = Array::from_vec(vec![1., 2., 3., 4., 5.]);
|
||||
let mut d = Array::from_vec(vec![5., 4., 3., 2., 1.]);
|
||||
|
||||
let z = a + b;
|
||||
let w = &c + &d;
|
||||
|
||||
let epsilon = 1e-8;
|
||||
for elem in z.iter() {
|
||||
let diff: f32 = *elem - 6.;
|
||||
assert!(diff.abs() < epsilon);
|
||||
}
|
||||
|
||||
println!("c = {}", c);
|
||||
c[0] = 10.;
|
||||
d[1] = 10.;
|
||||
|
||||
for elem in w.iter() {
|
||||
let diff: f32 = *elem - 6.;
|
||||
assert!(diff.abs() < epsilon);
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
[Binary Operators With Two Arrays]: https://docs.rs/ndarray/*/ndarray/struct.ArrayBase.html#binary-operators-with-two-arrays
|
||||
[`from_vec`]: https://docs.rs/ndarray/*/ndarray/struct.ArrayBase.html#method.from_vec
|
||||
[ndarray]: https://docs.rs/crate/ndarray/*
|
||||
[`ndarray::Array`]: https://docs.rs/ndarray/*/ndarray/struct.ArrayBase.html
|
Loading…
Reference in a new issue