Ejemplo n.º 1
0
        public void Reduce_2D_to_1D_and_0D()
        {
            //>>> x = np.arange(9).reshape(3, 3)
            //>>> x
            //array([[0, 1, 2],
            //       [3, 4, 5],
            //       [6, 7, 8]])
            //>>> x[1]
            //array([3, 4, 5])
            //>>> x[:,1]
            //array([1, 4, 7])
            //>>> x[2, 2]
            //8
            var data = new UnmanagedStorage(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 });

            data.Reshape(3, 3);
            Assert.AreEqual(new Shape(3, 3), data.Shape);
            // return identical view
            var view = data.GetView("1");

            Assert.AreEqual(new Shape(3), view.Shape);
            AssertAreEqual(new int[] { 3, 4, 5 }, view.ToArray <int>());
            // return reduced view
            view = data.GetView(":,1");
            Assert.AreEqual(new Shape(3), view.Shape);
            AssertAreEqual(new int[] { 1, 4, 7 }, view.ToArray <int>());

            view = data.GetView("2,2");
            Assert.AreEqual(Shape.Scalar, view.Shape);
            AssertAreEqual(new int[] { 8 }, view.ToArray <int>());
            // recursive dimensionality reduction
            view = data.GetView("2").GetView("2");
            Assert.AreEqual(Shape.Scalar, view.Shape);
            AssertAreEqual(new int[] { 8 }, view.ToArray <int>());
        }
Ejemplo n.º 2
0
        public void SlicingWithNegativeIndex1()
        {
            var a = new UnmanagedStorage(np.arange(10).GetData(), new Shape(10));

            a.GetView("-1").ToArray <int>().Should().BeEquivalentTo(new int[] { 9 });
            a.GetView("-2").GetValue <int>(0).Should().Be(8);
            a = new UnmanagedStorage(np.arange(10).GetData(), new Shape(1, 10));
            a.GetView(":, -1").ToArray <int>().Should().BeEquivalentTo(new int[] { 9 });
            a.GetView(":, 1:").GetView("-1, -2").ToArray <int>().Should().BeEquivalentTo(new int[] { 8 });
            a.GetView(":, 1:").GetView("-1, -2").GetValue(0, 0).Should().BeEquivalentTo(8);
        }
Ejemplo n.º 3
0
        public void GetData_1D_Negative()
        {
            var data = new UnmanagedStorage(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
            // return reduced view
            var view = data.GetView("-7:");

            Assert.AreEqual(new Shape(7), view.Shape);
            AssertAreEqual(new int[] { 3, 4, 5, 6, 7, 8, 9 }, view.ToArray <int>());
            view = data.GetView(":-5");
            Assert.AreEqual(new Shape(5), view.Shape);
            AssertAreEqual(new int[] { 0, 1, 2, 3, 4 }, view.ToArray <int>());
            view = data.GetView(":-6");
            Assert.AreEqual(new Shape(4), view.Shape);
            AssertAreEqual(new int[] { 0, 1, 2, 3, }, view.ToArray <int>());
            view = data.GetView("-3:-2");
            Assert.AreEqual(new Shape(1), view.Shape);
            AssertAreEqual(new int[] { 7 }, view.ToArray <int>());
            // negative step!
            view = data.GetView("-7::-1");
            Assert.AreEqual(new Shape(4), view.Shape);
            AssertAreEqual(new int[] { 3, 2, 1, 0 }, view.ToArray <int>());
            view = data.GetView(":-5:-1");
            Assert.AreEqual(new Shape(4), view.Shape);
            AssertAreEqual(new int[] { 9, 8, 7, 6 }, view.ToArray <int>());
            view = data.GetView(":-6:-1");
            Assert.AreEqual(new Shape(5), view.Shape);
            AssertAreEqual(new int[] { 9, 8, 7, 6, 5, }, view.ToArray <int>());
            view = data.GetView("-2:-3:-1");
            Assert.AreEqual(new Shape(1), view.Shape);
            AssertAreEqual(new int[] { 8 }, view.ToArray <int>());
        }
Ejemplo n.º 4
0
        public void TheUltimateTest______SliceReshapedSlicedReshapedSlicedArray()
        {
            var t    = new UnmanagedStorage(np.arange(20).GetData(), new Shape(20));
            var view = t.GetView("::-1");

            view.Reshape(5, 4);
            var v1 = view.GetView(":, 1:-1");

            new NDArray(v1).ToString(flat: true).Should().Be("array([[18, 17], [14, 13], [10, 9], [6, 5], [2, 1]])");
            v1.Reshape(1, 2, 5);
            new NDArray(v1).ToString(flat: true).Should().Be("array([[[18, 17, 14, 13, 10], [9, 6, 5, 2, 1]]])");
            var v2 = v1.GetView(":, ::-1, ::-2");

            new NDArray(v2).ToString(flat: true).Should().Be("array([[[1, 5, 9], [10, 14, 18]]])");
            v2.Reshape(2, 3, 1);
            new NDArray(v2).ToString(flat: true).Should().Be("array([[[1], [5], [9]], [[10], [14], [18]]])");
            var v3 = v2.GetView(":,::-2, 0");

            new NDArray(v3).ToString(flat: true).Should().Be("array([[9, 1], [18, 10]])");
            v3.SetData(ArraySlice.FromArray(new int[] { 99, 11, -18, -10 }));
            new NDArray(v3).ToString(flat: true).Should().Be("array([[99, 11], [-18, -10]])");
            new NDArray(v2).ToString(flat: true).Should().Be("array([[[11], [5], [99]], [[-10], [14], [-18]]])");
            new NDArray(v1).ToString(flat: true).Should().Be("array([[[-18, 17, 14, 13, -10], [99, 6, 5, 2, 11]]])");
            new NDArray(t).ToString(flat: true).Should().Be("array([0, 11, 2, 3, 4, 5, 6, 7, 8, 99, -10, 11, 12, 13, 14, 15, 16, 17, -18, 19])");
        }
Ejemplo n.º 5
0
        public void NestedView_2D()
        {
            var data = new UnmanagedStorage(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });

            data.Reshape(2, 10);
            //>>> x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
            //>>> x = x.reshape(2, 10)
            //>>> x
            //array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
            //       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
            // return identical view
            var identical = data.GetView(":");

            Assert.AreEqual(new Shape(2, 10), identical.Shape);
            //>>> x[:, 1:9]
            //array([[1, 2, 3, 4, 5, 6, 7, 8],
            //       [1, 2, 3, 4, 5, 6, 7, 8]])
            var view1 = identical.GetView(":,1:9");

            Assert.AreEqual(new Shape(2, 8), view1.Shape);
            AssertAreEqual(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8 }, view1.ToArray <int>());
            //>>> x[:, 1:9][:,::- 2]
            //array([[8, 6, 4, 2],
            //       [8, 6, 4, 2]])
            var view2 = view1.GetView(":,::-2");

            Assert.AreEqual(new Shape(2, 4), view2.Shape);
            AssertAreEqual(new int[] { 8, 6, 4, 2, 8, 6, 4, 2 }, view2.ToArray <int>());
            //>>> x[:, 1:9][:,::- 2][:,::- 3]
            //array([[2, 8],
            //       [2, 8]])
            var view3 = view2.GetView(":,::-3");

            Assert.AreEqual(new Shape(2, 2), view3.Shape);
            AssertAreEqual(new int[] { 2, 8, 2, 8 }, view3.ToArray <int>());
            // all must see the same modifications, no matter if original or any view is modified
            // modify original
            data.SetData(ArraySlice.FromArray(new int[] { 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, 0, -1, -2, -3, -4, -5, -6, -7, -8, -9 }));
            AssertAreEqual(new int[] { -1, -2, -3, -4, -5, -6, -7, -8, -1, -2, -3, -4, -5, -6, -7, -8 }, view1.ToArray <int>());
            AssertAreEqual(new int[] { -8, -6, -4, -2, -8, -6, -4, -2 }, view2.ToArray <int>());
            AssertAreEqual(new int[] { -2, -8, -2, -8 }, view3.ToArray <int>());
            // modify views
            view1.SetValue(88, 0, 7);
            view1.SetValue(888, 1, 7);
            AssertAreEqual(new int[] { 0, -1, -2, -3, -4, -5, -6, -7, 88, -9, 0, -1, -2, -3, -4, -5, -6, -7, 888, -9 },
                           data.ToArray <int>());
            AssertAreEqual(new int[] { -1, -2, -3, -4, -5, -6, -7, 88, -1, -2, -3, -4, -5, -6, -7, 888 },
                           view1.ToArray <int>());
            AssertAreEqual(new int[] { 88, -6, -4, -2, 888, -6, -4, -2 }, view2.ToArray <int>());
            AssertAreEqual(new int[] { -2, 88, -2, 888 }, view3.ToArray <int>());
            view3.SetValue(22, 0, 0);
            view3.SetValue(222, 1, 0);
            AssertAreEqual(new int[] { 0, -1, 22, -3, -4, -5, -6, -7, 88, -9, 0, -1, 222, -3, -4, -5, -6, -7, 888, -9 },
                           data.ToArray <int>());
            AssertAreEqual(new int[] { -1, 22, -3, -4, -5, -6, -7, 88, -1, 222, -3, -4, -5, -6, -7, 888 },
                           view1.ToArray <int>());
            AssertAreEqual(new int[] { 88, -6, -4, 22, 888, -6, -4, 222 }, view2.ToArray <int>());
            AssertAreEqual(new int[] { 22, 88, 222, 888 }, view3.ToArray <int>());
        }
Ejemplo n.º 6
0
        public void DimensionalityReduction4D_to_1D()
        {
            var t    = new UnmanagedStorage(np.arange(30).GetData(), new Shape(2, 1, 3, 5));
            var view = t.GetView("0,0,:,0");

            Assert.AreEqual(new Shape(3), view.Shape);
            Assert.AreEqual(5, view.GetValue <int>(1));
            Assert.AreEqual(10, view.GetValue <int>(2));
            AssertAreEqual(new int[] { 0, 5, 10 }, view.ToArray <int>());
        }
Ejemplo n.º 7
0
        public void Reduce_1D_to_Scalar()
        {
            var data = new UnmanagedStorage(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });

            Assert.AreEqual(new Shape(10), data.Shape);
            // return scalar
            var view = data.GetView("7");

            Assert.AreEqual(Shape.Scalar, view.Shape);
            AssertAreEqual(new int[] { 7 }, view.ToArray <int>());
        }
Ejemplo n.º 8
0
        public void ReshapeSlicedArray()
        {
            var t    = new UnmanagedStorage(np.arange(20).GetData(), new Shape(2, 10));
            var view = t.GetView(":, 5:");

            Assert.AreEqual(new Shape(2, 5), view.Shape);
            AssertAreEqual(new int[] { 5, 6, 7, 8, 9, 15, 16, 17, 18, 19 }, view.ToArray <int>());
            view.Reshape(10);
            Assert.AreEqual(new Shape(10), view.Shape);
            new int[] { 5, 6, 7, 8, 9, 15, 16, 17, 18, 19 }.Should().BeEquivalentTo(view.ToArray <int>());
            new NDArray(view).ToString(flat: true).Should().Be("array([5, 6, 7, 8, 9, 15, 16, 17, 18, 19])");
        }
Ejemplo n.º 9
0
        public void ExpandDimensions()
        {
            //>>> np.arange(6)
            //array([0, 1, 2, 3, 4, 5])
            //>>> np.arange(6).reshape(2, 3)
            //array([[0, 1, 2],
            //       [3, 4, 5]])
            //>>> a=np.arange(6).reshape(2,3)
            //>>> b=a[:, 1:]
            //>>> b
            //array([[1, 2],
            //       [4, 5]])
            //>>> c=b.reshape(2,1,2,1)
            //>>> c
            //array([[[[1],
            //         [2]]],
            //
            //
            //       [[[4],
            //         [5]]]])
            var t = new UnmanagedStorage(np.arange(6).GetData(), new Shape(2, 3));

            t.Shape.IsSliced.Should().Be(false);
            var view = t.GetView(":, 1:");

            view.Shape.IsSliced.Should().Be(true);
            new NDArray(view).ToString(flat: true).Should().Be("array([[1, 2], [4, 5]])");
            view.Reshape(2, 1, 2, 1);
            view.Shape.IsSliced.Should().Be(true);
            AssertAreEqual(new int[] { 1, 2, 4, 5 }, view.ToArray <int>());

            // doing the same disecting with slicing which ToString would do
            view.GetView("0").ToArray <int>().Should().BeEquivalentTo(new int[] { 1, 2, });
            view.GetView("1").ToArray <int>().Should().BeEquivalentTo(new int[] { 4, 5, });
            view.GetView("0").GetView("0").ToArray <int>().Should().BeEquivalentTo(new int[] { 1, 2 });
            view.GetView("0").GetView("0").GetView("0").ToArray <int>().Should().BeEquivalentTo(new int[] { 1, });
            view.GetView("0").GetView("0").GetView("1").ToArray <int>().Should().BeEquivalentTo(new int[] { 2, });
            view.GetView("0").GetView("0").GetView("0").GetView("0").ToArray <int>().Should().BeEquivalentTo(new int[] { 1, });
            view.GetView("0").GetView("0").GetView("1").GetView("0").ToArray <int>().Should().BeEquivalentTo(new int[] { 2, });
            view.GetView("1").GetView("0").ToArray <int>().Should().BeEquivalentTo(new int[] { 4, 5 });
            view.GetView("1").GetView("0").GetView("0").ToArray <int>().Should().BeEquivalentTo(new int[] { 4, });
            view.GetView("1").GetView("0").GetView("1").ToArray <int>().Should().BeEquivalentTo(new int[] { 5, });
            view.GetView("1").GetView("0").GetView("0").GetView("0").ToArray <int>().Should().BeEquivalentTo(new int[] { 4, });
            view.GetView("1").GetView("0").GetView("1").GetView("0").ToArray <int>().Should().BeEquivalentTo(new int[] { 5, });

            // this is to show that ToString works in principle:
            np.arange(4).reshape(2, 1, 2, 1).ToString(flat: true).Should().Be("array([[[[0], [1]]], [[[2], [3]]]])");
            var nd = new NDArray(view);

            // and now tostring of the reshaped
            nd.ToString(flat: true).Should().Be("array([[[[1], [2]]], [[[4], [5]]]])");
        }
Ejemplo n.º 10
0
        public void NestedDimensionalityReduction()
        {
            var data = new UnmanagedStorage(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 });

            data.Reshape(3, 3);
            Assert.AreEqual(new Shape(3, 3), data.Shape);
            var view = data.GetView("2");
            //Assert.AreEqual(new Shape(3), view.Shape);
            //AssertAreEqual(new int[] { 6, 7, 8 }, view.ToArray<int>());
            var view1 = view.GetView("2");

            Assert.AreEqual(Shape.Scalar, view1.Shape);
            AssertAreEqual(new int[] { 8 }, view1.ToArray <int>());
            var view2 = view.GetView("1::-1");

            Assert.AreEqual(new Shape(2), view2.Shape);
            AssertAreEqual(new int[] { 7, 6 }, view2.ToArray <int>());
        }
Ejemplo n.º 11
0
        public void NestedView_1D()
        {
            var data = new UnmanagedStorage(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
            // return identical view
            var identical = data.GetView(":");

            Assert.AreEqual(new Shape(10), identical.Shape);
            var view1 = identical.GetView("1:9");

            Assert.AreEqual(new Shape(8), view1.Shape);
            AssertAreEqual(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, }, view1.ToArray <int>());
            var view2 = view1.GetView("::-2");

            Assert.AreEqual(new Shape(4), view2.Shape);
            AssertAreEqual(new int[] { 8, 6, 4, 2, }, view2.ToArray <int>());
            var view3 = view2.GetView("::-3");

            Assert.AreEqual(new Shape(2), view3.Shape);
            AssertAreEqual(new int[] { 2, 8 }, view3.ToArray <int>());
            // all must see the same modifications, no matter if original or any view is modified
            // modify original
            data.SetData(ArraySlice.FromArray(new int[] { 0, -1, -2, -3, -4, -5, -6, -7, -8, -9 }));
            var arr = view1.ToArray <int>();

            AssertAreEqual(new int[] { -1, -2, -3, -4, -5, -6, -7, -8, }, view1.ToArray <int>());
            AssertAreEqual(new int[] { -8, -6, -4, -2, }, view2.ToArray <int>());
            AssertAreEqual(new int[] { -2, -8 }, view3.ToArray <int>());
            // modify views
            view1.SetValue(88, 7);
            AssertAreEqual(new int[] { 0, -1, -2, -3, -4, -5, -6, -7, 88, -9 }, data.ToArray <int>());
            AssertAreEqual(new int[] { -1, -2, -3, -4, -5, -6, -7, 88, }, view1.ToArray <int>());
            AssertAreEqual(new int[] { 88, -6, -4, -2, }, view2.ToArray <int>());
            AssertAreEqual(new int[] { -2, 88 }, view3.ToArray <int>());
            view3.SetValue(22, 0);
            AssertAreEqual(new int[] { 0, -1, 22, -3, -4, -5, -6, -7, 88, -9 }, data.ToArray <int>());
            AssertAreEqual(new int[] { -1, 22, -3, -4, -5, -6, -7, 88, }, view1.ToArray <int>());
            AssertAreEqual(new int[] { 88, -6, -4, 22, }, view2.ToArray <int>());
            AssertAreEqual(new int[] { 22, 88 }, view3.ToArray <int>());
        }
Ejemplo n.º 12
0
        public void ReshapeSlicedArray1()
        {
            //>>> a
            //array([[0, 1, 2],
            //       [3, 4, 5],
            //       [6, 7, 8]])
            var t    = new UnmanagedStorage(np.arange(9).GetData(), new Shape(3, 3));
            var view = t.GetView(":, 1:");

            //>>> a[:, 1:]
            //array([[1, 2],
            //       [4, 5],
            //       [7, 8]])
            view.Should().BeOfValues(1, 2, 4, 5, 7, 8).And.BeShaped(3, 2);
            view.Reshape(2, 3);
            //>>> a[:, 1:].reshape(2,3)
            //array([[1, 2, 4],
            //       [5, 7, 8]])
            view.Should().BeOfValues(1, 2, 4, 5, 7, 8).And.BeShaped(2, 3);
            view.GetValue(0, 0).Should().Be(1);
            view.GetValue(1, 0).Should().Be(5);
            view.GetValue(1, 1).Should().Be(7);
            view.GetValue(1, 2).Should().Be(8);
        }
Ejemplo n.º 13
0
        public void GetData_2D()
        {
            //>>> x = np.arange(9).reshape(3, 3)
            //>>> x
            //array([[0, 1, 2],
            //       [3, 4, 5],
            //       [6, 7, 8]])
            //>>> x[:]
            //array([[0, 1, 2],
            //       [3, 4, 5],
            //       [6, 7, 8]])
            //>>> x[1:]
            //array([[3, 4, 5],
            //       [6, 7, 8]])
            //>>> x[1:,:]
            //array([[3, 4, 5],
            //       [6, 7, 8]])
            //>>> x[:, 1:]
            //array([[1, 2],
            //       [4, 5],
            //       [7, 8]])
            //>>> x[1:2, 0:1]
            //array([[3]])
            var data = new UnmanagedStorage(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 });

            data.Reshape(3, 3);
            Assert.AreEqual(new Shape(3, 3), data.Shape);
            // return identical view
            var view = data.GetView(":");

            Assert.AreEqual(new Shape(3, 3), view.Shape);
            AssertAreEqual(data.ToArray <int>(), view.ToArray <int>());
            // return reduced view
            view = data.GetView("1:");
            Assert.AreEqual(new Shape(2, 3), view.Shape);
            AssertAreEqual(new int[] { 3, 4, 5, 6, 7, 8 }, view.ToArray <int>());
            view = data.GetView("1:,:");
            Assert.AreEqual(new Shape(2, 3), view.Shape);
            AssertAreEqual(new int[] { 3, 4, 5, 6, 7, 8 }, view.ToArray <int>());

            view = data.GetView(":,1:");
            Assert.AreEqual(new Shape(3, 2), view.Shape);
            AssertAreEqual(new int[] { 1, 2, 4, 5, 7, 8 }, view.ToArray <int>());
            view = data.GetView("1:2, 0:1");
            Assert.AreEqual(new Shape(1, 1), view.Shape);
            AssertAreEqual(new int[] { 3 }, view.ToArray <int>());
            // return stepped view
            //>>> x
            //array([[0, 1, 2],
            //       [3, 4, 5],
            //       [6, 7, 8]])
            //>>> x[::2]
            //array([[0, 1, 2],
            //       [6, 7, 8]])
            //>>> x[::3]
            //array([[0, 1, 2]])
            //>>> x[::- 1]
            //array([[6, 7, 8],
            //       [3, 4, 5],
            //       [0, 1, 2]])
            //>>> x[::- 2]
            //array([[6, 7, 8],
            //       [0, 1, 2]])
            //>>> x[::- 3]
            //array([[6, 7, 8]])
            view = data.GetView("::2");
            Assert.AreEqual(new Shape(2, 3), view.Shape);
            AssertAreEqual(new int[] { 0, 1, 2, 6, 7, 8 }, view.ToArray <int>());
            view = data.GetView("::3");
            Assert.AreEqual(new Shape(1, 3), view.Shape);
            AssertAreEqual(new int[] { 0, 1, 2 }, view.ToArray <int>());
            view = data.GetView("::-1");
            Assert.AreEqual(new Shape(3, 3), view.Shape);
            AssertAreEqual(new int[] { 6, 7, 8, 3, 4, 5, 0, 1, 2, }, view.ToArray <int>());
            view = data.GetView("::-2");
            Assert.AreEqual(new Shape(2, 3), view.Shape);
            AssertAreEqual(new int[] { 6, 7, 8, 0, 1, 2, }, view.ToArray <int>());
            view = data.GetView("::-3");
            Assert.AreEqual(new Shape(1, 3), view.Shape);
            AssertAreEqual(new int[] { 6, 7, 8, }, view.ToArray <int>());
            // N-Dim Stepping
            //>>> x[::2,::2]
            //array([[0, 2],
            //       [6, 8]])
            //>>> x[::- 1,::- 2]
            //array([[8, 6],
            //       [5, 3],
            //       [2, 0]])
            view = data.GetView("::2, ::2");
            Assert.AreEqual(new Shape(2, 2), view.Shape);
            AssertAreEqual(new int[] { 0, 2, 6, 8 }, view.ToArray <int>());
            view = data.GetView("::-1, ::-2");
            Assert.AreEqual(new Shape(3, 2), view.Shape);
            AssertAreEqual(new int[] { 8, 6, 5, 3, 2, 0 }, view.ToArray <int>());
        }
Ejemplo n.º 14
0
        public void GetData_1D()
        {
            var data = new UnmanagedStorage(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });

            Assert.AreEqual(new Shape(10), data.Shape);
            // return identical view
            var view = data.GetView(":");

            Assert.AreEqual(new Shape(10), view.Shape);
            AssertAreEqual(data.ToArray <int>(), view.ToArray <int>());
            view = data.GetView("-77:77");
            Assert.AreEqual(new Shape(10), view.Shape);
            AssertAreEqual(data.ToArray <int>(), view.ToArray <int>());
            // return reduced view
            view = data.GetView("7:");
            Assert.AreEqual(new Shape(3), view.Shape);
            AssertAreEqual(new int[] { 7, 8, 9 }, view.ToArray <int>());
            view = data.GetView(":5");
            Assert.AreEqual(new Shape(5), view.Shape);
            AssertAreEqual(new int[] { 0, 1, 2, 3, 4 }, view.ToArray <int>());
            view = data.GetView("2:3");
            Assert.AreEqual(new Shape(1), view.Shape);
            AssertAreEqual(new int[] { 2 }, view.ToArray <int>());
            // return stepped view
            view = data.GetView("::2");
            Assert.AreEqual(new Shape(5), view.Shape);
            AssertAreEqual(new int[] { 0, 2, 4, 6, 8 }, view.ToArray <int>());
            view = data.GetView("::3");
            Assert.AreEqual(new Shape(4), view.Shape);
            AssertAreEqual(new int[] { 0, 3, 6, 9 }, view.ToArray <int>());
            view = data.GetView("::77");
            Assert.AreEqual(new Shape(1), view.Shape);
            AssertAreEqual(new[] { 0 }, view.ToArray <int>());
            view = data.GetView("-77:77:77");
            Assert.AreEqual(new Shape(1), view.Shape);
            AssertAreEqual(new[] { 0 }, view.ToArray <int>());
            // negative step!
            view = data.GetView("::-1");
            Assert.AreEqual(new Shape(10), view.Shape);
            AssertAreEqual(data.ToArray <int>().OfType <int>().Reverse().ToArray(), view.ToArray <int>());
            view = data.GetView("::-2");
            Assert.AreEqual(new Shape(5), view.Shape);
            AssertAreEqual(new int[] { 9, 7, 5, 3, 1 }, view.ToArray <int>());
            view = data.GetView("::-3");
            Assert.AreEqual(new Shape(4), view.Shape);
            AssertAreEqual(new int[] { 9, 6, 3, 0 }, view.ToArray <int>());
            view = data.GetView("::-77");
            Assert.AreEqual(new Shape(1), view.Shape);
            AssertAreEqual(new[] { 9 }, view.ToArray <int>());
            view = data.GetView("77:-77:-77");
            Assert.AreEqual(new Shape(1), view.Shape);
            AssertAreEqual(new[] { 9 }, view.ToArray <int>());
        }
Ejemplo n.º 15
0
        public void Indexing_1D()
        {
            var data = new UnmanagedStorage(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
            // return identical view
            var view = data.GetView(":");

            Assert.AreEqual(0, view.GetValue <int>(0));
            Assert.AreEqual(5, view.GetValue <int>(5));
            Assert.AreEqual(9, view.GetValue <int>(9));
            view = data.GetView("-77:77");
            Assert.AreEqual(0, view.GetValue <int>(0));
            Assert.AreEqual(5, view.GetValue <int>(5));
            Assert.AreEqual(9, view.GetValue <int>(9));
            // return reduced view
            view = data.GetView("7:");
            Assert.AreEqual(7, view.GetValue <int>(0));
            Assert.AreEqual(8, view.GetValue <int>(1));
            Assert.AreEqual(9, view.GetValue <int>(2));
            view = data.GetView(":5");
            Assert.AreEqual(0, view.GetValue <int>(0));
            Assert.AreEqual(1, view.GetValue <int>(1));
            Assert.AreEqual(2, view.GetValue <int>(2));
            Assert.AreEqual(3, view.GetValue <int>(3));
            Assert.AreEqual(4, view.GetValue <int>(4));
            view = data.GetView("2:3");
            Assert.AreEqual(2, view.GetValue <int>(0));
            // return stepped view
            view = data.GetView("::2");
            Assert.AreEqual(0, view.GetValue <int>(0));
            Assert.AreEqual(2, view.GetValue <int>(1));
            Assert.AreEqual(4, view.GetValue <int>(2));
            Assert.AreEqual(6, view.GetValue <int>(3));
            Assert.AreEqual(8, view.GetValue <int>(4));
            view = data.GetView("::3");
            Assert.AreEqual(0, view.GetValue <int>(0));
            Assert.AreEqual(3, view.GetValue <int>(1));
            Assert.AreEqual(6, view.GetValue <int>(2));
            Assert.AreEqual(9, view.GetValue <int>(3));
            view = data.GetView("-77:77:77");
            Assert.AreEqual(0, view.GetValue <int>(0));
            // negative step!
            view = data.GetView("::-1");
            Assert.AreEqual(9, view.GetValue <int>(0));
            Assert.AreEqual(4, view.GetValue <int>(5));
            Assert.AreEqual(0, view.GetValue <int>(9));
            view = data.GetView("::-2");
            Assert.AreEqual(9, view.GetValue <int>(0));
            Assert.AreEqual(7, view.GetValue <int>(1));
            Assert.AreEqual(5, view.GetValue <int>(2));
            Assert.AreEqual(3, view.GetValue <int>(3));
            Assert.AreEqual(1, view.GetValue <int>(4));
            view = data.GetView("::-3");
            Assert.AreEqual(9, view.GetValue <int>(0));
            Assert.AreEqual(6, view.GetValue <int>(1));
            Assert.AreEqual(3, view.GetValue <int>(2));
            Assert.AreEqual(0, view.GetValue <int>(3));
            view = data.GetView("77:-77:-77");
            Assert.AreEqual(9, view.GetValue <int>(0));
        }