Broadcast Illustration
This one is really good, ndarray should adopt this (just need to change the code for each illu)
http://www.astroml.org/book_figures/appendix/fig_broadcast_visual.html
draft. Doesn't look good in github, fine in vim, fine in rustdoc's fonts.
array![0, 1, 2] + array![1]
shape [3] + shape [1] = shape [3]
┌───┬───┬───┐ ┌───┐┄┄┄┬┄┄┄╮ ┌───┬───┬───┐
│ 0 │ 1 │ 2 │ + │ 1 │ 1 ┊ 1 ┊ = │ 1 │ 2 │ 3 │
└───┴───┴───┘ └───┘┄┄┄┴┄┄┄╯ └───┴───┴───┘
array![[1, 1, 1],
[1, 1, 1],
[1, 1, 1]] + array![0, 1, 2]
shape [3, 3] + shape [3] = shape [3, 3]
┌───┬───┬───┐ ┌───┬───┬───┐ ┌───┬───┬───┐
│ 1 │ 1 │ 1 │ │ 0 │ 1 │ 2 │ │ 1 │ 2 │ 3 │
├───┼───┼───┤ └───┴───┴───┘ ├───┼───┼───┤
│ 1 │ 1 │ 1 │ + ┊ 0 ┊ 1 ┊ 2 ┊ = │ 1 │ 2 │ 3 │
├───┼───┼───┤ ├┄┄┄ ┄┄┄ ┄┄┄┤ ├───┼───┼───┤
│ 1 │ 1 │ 1 │ ┊ 0 ┊ 1 ┊ 2 ┊ │ 1 │ 2 │ 3 │
└───┴───┴───┘ ╰┄┄┄┴┄┄┄┴┄┄┄╯ └───┴───┴───┘
array![[0],
[1],
[2]] + array![[0, 1, 2]]
shape [3, 1] + shape [1, 3] = shape [3, 3]
┌───┐┄┄┄ ┄┄┄╮ ┌───┬───┬───┐ ┌───┬───┬───┐
│ 0 │ 0 ┊ 0 ┊ │ 0 │ 1 │ 2 │ │ 0 │ 1 │ 2 │
├───┤┄┄┄ ┄┄┄ └───┴───┴───┘ ├───┼───┼───┤
│ 1 │ 1 ┊ 1 ┊ + ┊ 0 ┊ 1 ┊ 2 ┊ = │ 1 │ 2 │ 3 │
├───┤┄┄┄ ┄┄┄ ┄┄┄ ┄┄┄ ┄┄┄ ├───┼───┼───┤
│ 2 │ 2 ┊ 2 ┊ ┊ 0 ┊ 1 ┊ 2 ┊ │ 1 │ 3 │ 4 │
└───┘┄┄┄ ┄┄┄╯ ╰┄┄┄ ┄┄┄ ┄┄┄╯ └───┴───┴───┘
I'm a long time numpy user and am I'm having a hard time understanding how broadcasting semantics work here. In fact, if I try your third example from the previous comment I get an error:
⇒ cargo run Spencers-MacBook-Pro
Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
Running `target/debug/bertola`
thread 'main' panicked at 'ndarray: could not broadcast array from shape: [1, 3] to: [3, 1]', /Users/sglyon/.cargo/registry/src/github.com-1ecc6299db9ec823/ndarray-0.7.3/src/lib.rs:573
note: Run with `RUST_BACKTRACE=1` for a backtrace.
Is this expected?
yes, ndarray does not implement the third case.
Ok, thanks for the answer.
Are there plans to implement the third case, or is this something that will not become a feature of this library?
As you know arrays in ndarray have dimensionality in the type.
If we add Array<f32, D1> + Array<f32, D2> the result needs to be a Array<f32, D3> where D3 is some sort of upper bound of the dimension types D1 and D2; so D3 needs to be the bigger of the two to have room to storing the dimension information.
As it is right now, the + always leaves the result with the dimension type D1 and that makes it simple, just broadcast the right hand side to the shape of the left hand side.
I haven't attempted implementing it; I'm in general not so keen on more generic noise than absolutely necessary. Every bit makes the library hard to understand, and many apparently already think it is hard to get into.
OK that makes sense -- thanks for the clarification.
@bluss, may thisi ssue be closed due to it being practically resolved in PR #565 ?
We never ended up adding broadcasting illustrations for #565 so I guess we're still waiting for that.