-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3fcb08b
commit d7c2911
Showing
7 changed files
with
92 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
# Linear Algebra | ||
|
||
{{#include linear_algebra/vector-sum.md}} | ||
{{#include linear_algebra/vector-norm.md}} | ||
{{#include linear_algebra/add-matrices.md}} | ||
{{#include linear_algebra/multiply-matrices.md}} | ||
{{#include linear_algebra/multiply-scalar-vector-matrix.md}} | ||
{{#include linear_algebra/vector-comparison.md}} | ||
{{#include linear_algebra/vector-norm.md}} | ||
{{#include linear_algebra/invert-matrix.md}} | ||
|
||
{{#include ../../links.md}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/science/mathematics/linear_algebra/vector-comparison.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
## Vector comparison | ||
[![ndarray-badge]][ndarray] | ||
|
||
The [ndarray] crate supports a number of ways to create arrays -- this recipe creates | ||
[`ndarray::Array`]s from `std::Vec` using `from`. Then, it sums the arrays element-wise. | ||
|
||
This recipe contains an example of comparing two floating-point vectors element-wise. | ||
Floating-point numbers are often stored inexactly, making exact comparisons difficult. | ||
However, the [`assert_abs_diff_eq!`] macro from the [`approx`] crate allows for convenient | ||
element-wise comparisons. To use the `approx` crate with `ndarray`, the `approx` | ||
feature must be added to the `ndarray` dependency in `Cargo.toml`. For example, | ||
`ndarray = { version = "0.13", features = ["approx"] }`. | ||
|
||
This recipe also contains additional ownership examples. Here, `let z = a + b` consumes | ||
`a` and `b`, updates `a` with the result, then moves ownership to `z`. Alternatively, | ||
`let w = &c + &d` creates a new vector without consuming `c` or `d`, allowing | ||
their modification later. See [Binary Operators With Two Arrays] for additional detail. | ||
|
||
```rust | ||
#[macro_use(assert_abs_diff_eq)] | ||
extern crate approx; | ||
extern crate ndarray; | ||
|
||
use ndarray::Array; | ||
|
||
fn main() { | ||
let a = Array::from(vec![1., 2., 3., 4., 5.]); | ||
let b = Array::from(vec![5., 4., 3., 2., 1.]); | ||
let mut c = Array::from(vec![1., 2., 3., 4., 5.]); | ||
let mut d = Array::from(vec![5., 4., 3., 2., 1.]); | ||
|
||
let z = a + b; | ||
let w = &c + &d; | ||
|
||
assert_abs_diff_eq!(z, Array::from(vec![6., 6., 6., 6., 6.])); | ||
|
||
println!("c = {}", c); | ||
c[0] = 10.; | ||
d[1] = 10.; | ||
|
||
assert_abs_diff_eq!(w, Array::from(vec![6., 6., 6., 6., 6.])); | ||
|
||
} | ||
``` | ||
|
||
[`approx`]: https://docs.rs/approx/*/approx/index.html | ||
[`assert_abs_diff_eq!`]: https://docs.rs/approx/*/approx/macro.assert_abs_diff_eq.html | ||
[Binary Operators With Two Arrays]: https://docs.rs/ndarray/*/ndarray/struct.ArrayBase.html#binary-operators-with-two-arrays | ||
[ndarray]: https://docs.rs/crate/ndarray/* | ||
[`ndarray::Array`]: https://docs.rs/ndarray/*/ndarray/struct.ArrayBase.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.