ndarray icon indicating copy to clipboard operation
ndarray copied to clipboard

Broadcast Illustration

Open bluss opened this issue 9 years ago • 8 comments

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

bluss avatar Jan 20 '17 19:01 bluss

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 │
└───┘┄┄┄ ┄┄┄╯   ╰┄┄┄ ┄┄┄ ┄┄┄╯   └───┴───┴───┘

bluss avatar Jan 20 '17 20:01 bluss

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?

sglyon avatar Feb 12 '17 03:02 sglyon

yes, ndarray does not implement the third case.

bluss avatar Feb 12 '17 11:02 bluss

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?

sglyon avatar Feb 13 '17 16:02 sglyon

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.

bluss avatar Feb 13 '17 20:02 bluss

OK that makes sense -- thanks for the clarification.

sglyon avatar Feb 16 '17 18:02 sglyon

@bluss, may thisi ssue be closed due to it being practically resolved in PR #565 ?

thecooldrop avatar Mar 07 '21 13:03 thecooldrop

We never ended up adding broadcasting illustrations for #565 so I guess we're still waiting for that.

bluss avatar Mar 07 '21 14:03 bluss