private void _inferMissingDimension(ref Shape shape) { var indexOfNegOne = -1; int product = 1; for (int i = 0; i < shape.NDim; i++) { if (shape[i] == -1) { if (indexOfNegOne != -1) { throw new ArgumentException("Only allowed to pass one shape dimension as -1"); } indexOfNegOne = i; } else { product *= shape[i]; } } if (indexOfNegOne == -1) { return; } if (this.IsBroadcasted) { /* //TODO: the following case needs to be handled. * a = np.arange(4).reshape(1,2,2) * print(a.strides) * * b = np.broadcast_to(a, (2,2,2)) * print(b.strides) * * c = np.reshape(b, (2, -1)) * print(c.strides) * * c = np.reshape(b, (-1, 2)) * print(c.strides) * * (16, 8, 4) * (0, 8, 4) * (0, 4) //here it handles broadcast * (8, 4) //here can be seen that numpy performs a copy */ //var originalReshaped = np.broadcast_to(this.BroadcastInfo.OriginalShape.Reshape(new int[] { this.BroadcastInfo.OriginalShape.size }), (Shape) new int[] { this.size }); throw new NotSupportedException("Reshaping a broadcasted array with a -1 (unknown) dimension is not supported."); } int missingValue = this.size / product; if (missingValue * product != this.size) { throw new ArgumentException("Bad shape: missing dimension would have to be non-integer"); } shape.dimensions[indexOfNegOne] = missingValue; var strides = shape.strides; var dimensions = shape.dimensions; if (indexOfNegOne == strides.Length - 1) { strides[strides.Length - 1] = 1; } for (int idx = indexOfNegOne; idx >= 1; idx--) { strides[idx - 1] = strides[idx] * dimensions[idx]; } shape.ComputeHashcode(); }