public void basics_broadcasting_narrays() { Shape[] ret; ret = DefaultEngine.Broadcast(new Shape[] { new Shape(5, 5), Shape.Scalar }); ret[0].dimensions.Should().ContainInOrder(5, 5); ret[1].dimensions.Should().ContainInOrder(5, 5); ret[1].strides.Should().AllBeEquivalentTo(0); indexes2(5, 5).Select(i => ret[1].GetOffset(i.i1, i.i2)).Should().AllBeEquivalentTo(0); indexes2(5, 5).Select(i => ret[0].GetOffset(i.i1, i.i2)) .Should().BeInAscendingOrder().And.Subject.First().Should().Be(0); ret = DefaultEngine.Broadcast(new Shape[] { Shape.Scalar, new Shape(5, 5) }); ret[0].dimensions.Should().ContainInOrder(5, 5); ret[1].dimensions.Should().ContainInOrder(5, 5); ret[0].strides.Should().AllBeEquivalentTo(0); indexes2(5, 5).Select(i => ret[0].GetOffset(i.i1, i.i2)).Should().AllBeEquivalentTo(0); indexes2(5, 5).Select(i => ret[1].GetOffset(i.i1, i.i2)) .Should().BeInAscendingOrder().And.Subject.First().Should().Be(0); ret = DefaultEngine.Broadcast(new Shape[] { new Shape(5, 5), new Shape(5, 5) }); ret[0].dimensions.Should().ContainInOrder(5, 5); ret[1].dimensions.Should().ContainInOrder(5, 5); ret[0].strides.Should().ContainInOrder(5, 1); ret[1].strides.Should().ContainInOrder(5, 1); ret = DefaultEngine.Broadcast(new Shape[] { new Shape(5, 5, 3), new Shape(5, 3) }); ret[0].dimensions.Should().ContainInOrder(5, 5, 3); ret[1].dimensions.Should().ContainInOrder(5, 5, 3); ret = DefaultEngine.Broadcast(new Shape[] { new Shape(256, 256, 3), new Shape(3) }); ret[0].dimensions.Should().ContainInOrder(256, 256, 3); ret[1].dimensions.Should().ContainInOrder(256, 256, 3); ret = DefaultEngine.Broadcast(new Shape[] { new Shape(8, 1, 6, 1), new Shape(7, 1, 5) }); ret[0].dimensions.Should().ContainInOrder(8, 7, 6, 5); ret[1].dimensions.Should().ContainInOrder(8, 7, 6, 5); ret = DefaultEngine.Broadcast(new Shape[] { new Shape(5, 4), new Shape(1) }); ret[0].dimensions.Should().ContainInOrder(5, 4); ret[1].dimensions.Should().ContainInOrder(5, 4); indexes2(5, 4).Select(i => ret[0].GetOffset(i.i1, i.i2)) .Should().BeInAscendingOrder().And.Subject.First().Should().Be(0); indexes2(5, 4).Select(i => ret[1].GetOffset(i.i1, i.i2)).Should().AllBeEquivalentTo(0); ret = DefaultEngine.Broadcast(new Shape[] { new Shape(5, 4), new Shape(4) }); ret[0].dimensions.Should().ContainInOrder(5, 4); ret[1].dimensions.Should().ContainInOrder(5, 4); ret = DefaultEngine.Broadcast(new Shape[] { new Shape(15, 3, 5), new Shape(15, 1, 5) }); ret[0].dimensions.Should().ContainInOrder(15, 3, 5); ret[1].dimensions.Should().ContainInOrder(15, 3, 5); ret = DefaultEngine.Broadcast(new Shape[] { new Shape(15, 3, 5), new Shape(3, 1) }); ret[0].dimensions.Should().ContainInOrder(15, 3, 5); ret[1].dimensions.Should().ContainInOrder(15, 3, 5); new Action(() => DefaultEngine.ResolveReturnShape(new Shape(3), new Shape(4))).Should().Throw <Exception>(); new Action(() => DefaultEngine.ResolveReturnShape(new Shape(2, 1), new Shape(8, 4, 3))).Should().Throw <Exception>(); }
public void basics_ResolveReturnShape() { Shape arrOne; arrOne = DefaultEngine.ResolveReturnShape(new Shape(5, 5), Shape.Scalar); arrOne.dimensions.Should().ContainInOrder(5, 5); arrOne = DefaultEngine.ResolveReturnShape(Shape.Scalar, new Shape(5, 5)); arrOne.dimensions.Should().ContainInOrder(5, 5); arrOne = DefaultEngine.ResolveReturnShape(new Shape(5, 5), new Shape(5, 5)); arrOne.dimensions.Should().ContainInOrder(5, 5); arrOne = DefaultEngine.ResolveReturnShape(new Shape(256, 256, 3), new Shape(3)); arrOne.dimensions.Should().ContainInOrder(256, 256, 3); arrOne = DefaultEngine.ResolveReturnShape(new Shape(8, 1, 6, 1), new Shape(7, 1, 5)); arrOne.dimensions.Should().ContainInOrder(8, 7, 6, 5); arrOne = DefaultEngine.ResolveReturnShape(new Shape(5, 4), new Shape(1)); arrOne.dimensions.Should().ContainInOrder(5, 4); arrOne = DefaultEngine.ResolveReturnShape(new Shape(5, 4), new Shape(4)); arrOne.dimensions.Should().ContainInOrder(5, 4); arrOne = DefaultEngine.ResolveReturnShape(new Shape(15, 3, 5), new Shape(15, 1, 5)); arrOne.dimensions.Should().ContainInOrder(15, 3, 5); arrOne = DefaultEngine.ResolveReturnShape(new Shape(15, 3, 5), new Shape(3, 1)); arrOne.dimensions.Should().ContainInOrder(15, 3, 5); new Action(() => DefaultEngine.ResolveReturnShape(new Shape(3), new Shape(4))).Should().Throw <Exception>(); new Action(() => DefaultEngine.ResolveReturnShape(new Shape(2, 1), new Shape(8, 4, 3))).Should().Throw <Exception>(); }
/// <summary> /// Produce an object that mimics broadcasting. /// </summary> /// <returns>Broadcast the input parameters against one another, and return an object that encapsulates the result. Amongst others, it has shape and nd properties, and may be used as an iterator.</returns> /// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.broadcast.html</remarks> public static Broadcast broadcast(NDArray nd1, NDArray nd2) { return(new Broadcast { shape = DefaultEngine.ResolveReturnShape(nd1.Shape, nd2.Shape) }); }
/// <summary> /// Produce an object that mimics broadcasting. /// </summary> /// <returns>Broadcast the input parameters against one another, and return an object that encapsulates the result. Amongst others, it has shape and nd properties, and may be used as an iterator.</returns> /// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.broadcast.html</remarks> public static Broadcast broadcast(NDArray nd1, NDArray nd2) { return(new Broadcast { shape = DefaultEngine.ResolveReturnShape(nd1.Shape, nd2.Shape), iters = new[] { nd1.AsIterator(), nd2.AsIterator() } }); }