Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could not broadcast array from shape: [2] to: [2, 1] #76

Closed
SuperFluffy opened this issue Feb 5, 2016 · 6 comments
Closed

Could not broadcast array from shape: [2] to: [2, 1] #76

SuperFluffy opened this issue Feb 5, 2016 · 6 comments

Comments

@SuperFluffy
Copy link
Contributor

Using slices, I want to set the columns or rows of the 2×2 matrix (i.e. Array) a to the vectors u or v, respectively. This works when slicing along the first axis (getting row-slices), but not along the second:

#[macro_use(s)]
extern crate ndarray;

use ndarray::OwnedArray;

fn main() {
    let u: Vec<f64> = vec![1.,2.];
    let v: Vec<f64> = vec![3.,4.];

    let mut a = OwnedArray::from_elem((2,2), 0f64);

    // Works
    a.slice_mut(s![0..1, ..]).assign(&OwnedArray::from_vec(u.clone()));
    a.slice_mut(s![1..2, ..]).assign(&OwnedArray::from_vec(v.clone()));

    // Could not broadcast array from shape: [2] to: [2, 1]
    a.slice_mut(s![.., 0..1]).assign(&OwnedArray::from_vec(u.clone()));
    a.slice_mut(s![.., 1..2]).assign(&OwnedArray::from_vec(v.clone()));
}

I suppose the problem is that ndarray can broadcast from [2] to [1,2], but not to the reverse.

@bluss
Copy link
Member

bluss commented Feb 5, 2016

That's right. The broadcast method has some explanation. In all the arithmetic operations right now, self @ rhs will only try to broadcast rhs into the shape of self, and assign works the same way.

@bluss
Copy link
Member

bluss commented Feb 5, 2016

By the way, you can use subview mut or the method col_mut to get one dimensional array views that you can assign to.

@bluss
Copy link
Member

bluss commented Feb 5, 2016

I'd do this

a.column_mut(0).assign(&aview1(&u));
a.column_mut(1).assign(&aview1(&u));

Creating an array view from the Vec is a much simpler operation, no need to allocate a new owned array..

Edited: Fixed method name, it's column_mut.

@bluss
Copy link
Member

bluss commented Feb 5, 2016

co-broadcasting (issue #11) does not apply here, so there is no intention to fix this; it works as designed.

Co broadcasting applies when we can allocate a new result, for example something like this dimensional equation, where the result doesn't have the shape of either operand.

Array(2, 1) + Array(2) => Array(2, 2) (this is not yet implemented).

Today only shape preserving of the left hand side is allowed, for example:

Array(4, 2) + Array(2) => Array(4, 2) (this is implemented).

@bluss bluss closed this as completed Feb 5, 2016
@sglyon
Copy link

sglyon commented Aug 28, 2016

I think this is a related issue. I'm trying this code:

extern crate ndarray;
use ndarray::prelude::*;

fn main() {
    let x = Array::linspace(0.5, 1.5, 5);
    let y = arr2(&[[0.1, 0.2, 0.3]]);

    let z = y + x;
}

But it fails with

   Compiling ndarray_broadcast_error v0.1.0 (file:///Users/sglyon/Desktop/temp/rust/ndarray_broadcast_error)
src/main.rs:8:9: 8:10 warning: unused variable: `z`, #[warn(unused_variables)] on by default
src/main.rs:8     let z = y + x;
                      ^
     Running `target/debug/ndarray_broadcast_error`
thread '<main>' panicked at 'ndarray: could not broadcast array from shape: [5] to: [1, 3]', /Users/sglyon/.cargo/registry/src/github.com-88ac128001ac3a9a/ndarray-0.6.2/src/lib.rs:485
note: Run with `RUST_BACKTRACE=1` for a backtrace.
error: Process didn't exit successfully: `target/debug/ndarray_broadcast_error` (exit code: 101)

I would have expected this to work as it does in numpy:

In [1]: import numpy as np

In [2]: x = np.linspace(0.5, 1.5, 5);

In [3]: y = np.array([[0.5], [0.2], [0.3]])

In [4]: x + y
Out[4]:
array([[ 1.  ,  1.25,  1.5 ,  1.75,  2.  ],
       [ 0.7 ,  0.95,  1.2 ,  1.45,  1.7 ],
       [ 0.8 ,  1.05,  1.3 ,  1.55,  1.8 ]])

Any suggestions for how I can do this the right way here?

@bluss
Copy link
Member

bluss commented Aug 29, 2016

numpy has implemented broadcasting a step further than we have. (Some kind of co-broadcasting where both sides are adjusted). It would do fine as a feature request issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants