SciSharp / NumSharp

High Performance Computation for N-D Tensors in .NET, similar API to NumPy.
https://github.com/SciSharp
Apache License 2.0
1.37k stars 192 forks source link

System.NotSupportedException: 'Unable to broadcast already broadcasted shape.' #442

Closed minhduc66532 closed 3 years ago

minhduc66532 commented 3 years ago

Here is the code

var shape = Z.shape;
var (u, v) = np.meshgrid(np.arange(shape[1]), np.arange(shape[0]));
var x = (u - Cu) * Z / f; <--- error raised here
var y = (v - Cv) * Z / f;
return np.vstack(x.ravel(), y.ravel(), Z.ravel());

image Any ideas ?

Nucs commented 3 years ago

We do not support broadcasting an already broadcasted NDArray. My guess is the meshgrid uses broadcasting. If you'll clone the NDArray (NDArray.Clone()) that'll produce a linear/contagious stored NDArray which then can be broadcasted again.

minhduc66532 commented 3 years ago

My solution currently is to use np.full like this

var shape = Z.shape;
var (u, v) = np.meshgrid(np.arange(shape[1]), np.arange(shape[0]));
var CU = np.full(Cu, shape[0], shape[1]);
var CV = np.full(Cv, shape[0], shape[1]);
var x = (u - CU) * Z / f; 
var y = (v - CV) * Z / f;
return np.vstack(x.ravel(), y.ravel(), Z.ravel());