Exemple #1
0
        public void NestedView_2D()
        {
            var data = new TypedArrayStorage(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 = new ViewStorage(data, ":");

            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 = new ViewStorage(identical, ":,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.GetData());
            //>>> x[:, 1:9][:,::- 2]
            //array([[8, 6, 4, 2],
            //       [8, 6, 4, 2]])
            var view2 = new ViewStorage(view1, ":,::-2");

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

            Assert.AreEqual(new Shape(2, 2), view3.Shape);
            AssertAreEqual(new int[] { 2, 8, 2, 8 }, view3.GetData());
            // all must see the same modifications, no matter if original or any view is modified
            // modify original
            data.ReplaceData(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.GetData());
            AssertAreEqual(new int[] { -8, -6, -4, -2, -8, -6, -4, -2 }, view2.GetData());
            AssertAreEqual(new int[] { -2, -8, -2, -8 }, view3.GetData());
            // modify views
            view1.SetData(88, 0, 7);
            view1.SetData(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.GetData());
            AssertAreEqual(new int[] { -1, -2, -3, -4, -5, -6, -7, 88, -1, -2, -3, -4, -5, -6, -7, 888 },
                           view1.GetData());
            AssertAreEqual(new int[] { 88, -6, -4, -2, 888, -6, -4, -2 }, view2.GetData());
            AssertAreEqual(new int[] { -2, 88, -2, 888 }, view3.GetData());
            view3.SetData(22, 0, 0);
            view3.SetData(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.GetData());
            AssertAreEqual(new int[] { -1, 22, -3, -4, -5, -6, -7, 88, -1, 222, -3, -4, -5, -6, -7, 888 },
                           view1.GetData());
            AssertAreEqual(new int[] { 88, -6, -4, 22, 888, -6, -4, 222 }, view2.GetData());
            AssertAreEqual(new int[] { 22, 88, 222, 888 }, view3.GetData());
        }
Exemple #2
0
        public void CheckChangeTensorLayout2D()
        {
            var strg2DCpy = (TypedArrayStorage)strg2D.Clone();

            Assert.IsTrue(Enumerable.SequenceEqual(strg2DCpy.Shape.Dimensions, new int[] { 3, 3 }));
            Assert.IsTrue(Enumerable.SequenceEqual(strg2DCpy.GetData <long>(), new long[] { 0, 3, 6, 1, 4, 7, 2, 5, 8 }));

            Assert.IsTrue(Enumerable.SequenceEqual(strg2DCpy.Shape.Dimensions, new int[] { 3, 3 }));
            Assert.IsTrue(Enumerable.SequenceEqual(strg2DCpy.GetData <long>(), strg2D.GetData <long>()));

            strg2DCpy = (TypedArrayStorage)strg2DNonFull.Clone();

            Assert.IsTrue(Enumerable.SequenceEqual(strg2DCpy.Shape.Dimensions, new int[] { 5, 2 }));
            Assert.IsTrue(Enumerable.SequenceEqual(strg2DCpy.GetData <long>(), new long[] { 0, 5, 1, 6, 2, 7, 3, 8, 4, 9 }));

            Assert.IsTrue(Enumerable.SequenceEqual(strg2DCpy.Shape.Dimensions, new int[] { 5, 2 }));
            Assert.IsTrue(Enumerable.SequenceEqual(strg2DCpy.GetData <long>(), strg2DNonFull.GetData <long>()));

            strg2DCpy = new TypedArrayStorage(typeof(long));
            strg2DCpy.Allocate(new Shape(5, 2));

            strg2DCpy.ReplaceData(strg2DNonFull.GetData());

            Assert.IsTrue(Enumerable.SequenceEqual(strg2DCpy.GetData <long>(), new long[] { 0, 2, 4, 6, 8, 1, 3, 5, 7, 9 }));
        }
Exemple #3
0
        public void GetData_1D()
        {
            var data = new TypedArrayStorage(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });

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

            Assert.AreEqual(new Shape(10), view.Shape);
            AssertAreEqual(data.GetData(), view.GetData());
            view = new ViewStorage(data, "-77:77");
            Assert.AreEqual(new Shape(10), view.Shape);
            AssertAreEqual(data.GetData(), view.GetData());
            // return reduced view
            view = new ViewStorage(data, "7:");
            Assert.AreEqual(new Shape(3), view.Shape);
            AssertAreEqual(new int[] { 7, 8, 9 }, view.GetData());
            view = new ViewStorage(data, ":5");
            Assert.AreEqual(new Shape(5), view.Shape);
            AssertAreEqual(new int[] { 0, 1, 2, 3, 4 }, view.GetData());
            view = new ViewStorage(data, "2:3");
            Assert.AreEqual(new Shape(1), view.Shape);
            AssertAreEqual(new int[] { 2 }, view.GetData());
            // return stepped view
            view = new ViewStorage(data, "::2");
            Assert.AreEqual(new Shape(5), view.Shape);
            AssertAreEqual(new int[] { 0, 2, 4, 6, 8 }, view.GetData());
            view = new ViewStorage(data, "::3");
            Assert.AreEqual(new Shape(4), view.Shape);
            AssertAreEqual(new int[] { 0, 3, 6, 9 }, view.GetData());
            view = new ViewStorage(data, "-77:77:77");
            Assert.AreEqual(new Shape(1), view.Shape);
            AssertAreEqual(new[] { 0 }, view.GetData());
            // negative step!
            view = new ViewStorage(data, "::-1");
            Assert.AreEqual(new Shape(10), view.Shape);
            AssertAreEqual(data.GetData().OfType <int>().Reverse().ToArray(), view.GetData());
            view = new ViewStorage(data, "::-2");
            Assert.AreEqual(new Shape(5), view.Shape);
            AssertAreEqual(new int[] { 9, 7, 5, 3, 1 }, view.GetData());
            view = new ViewStorage(data, "::-3");
            Assert.AreEqual(new Shape(4), view.Shape);
            AssertAreEqual(new int[] { 9, 6, 3, 0 }, view.GetData());
            view = new ViewStorage(data, "-77:77:-77");
            Assert.AreEqual(new Shape(1), view.Shape);
            AssertAreEqual(new[] { 9 }, view.GetData());
        }
Exemple #4
0
        public void NestedView_1D()
        {
            var data = new TypedArrayStorage(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
            // return identical view
            var identical = new ViewStorage(data, ":");

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

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

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

            Assert.AreEqual(new Shape(2), view3.Shape);
            AssertAreEqual(new int[] { 2, 8 }, view3.GetData());
            // all must see the same modifications, no matter if original or any view is modified
            // modify original
            data.ReplaceData(new int[] { 0, -1, -2, -3, -4, -5, -6, -7, -8, -9 });
            AssertAreEqual(new int[] { -1, -2, -3, -4, -5, -6, -7, -8, }, view1.GetData());
            AssertAreEqual(new int[] { -8, -6, -4, -2, }, view2.GetData());
            AssertAreEqual(new int[] { -2, -8 }, view3.GetData());
            // modify views
            view1.SetData(88, 7);
            AssertAreEqual(new int[] { 0, -1, -2, -3, -4, -5, -6, -7, 88, -9 }, data.GetData());
            AssertAreEqual(new int[] { -1, -2, -3, -4, -5, -6, -7, 88, }, view1.GetData());
            AssertAreEqual(new int[] { 88, -6, -4, -2, }, view2.GetData());
            AssertAreEqual(new int[] { -2, 88 }, view3.GetData());
            view3.SetData(22, 0);
            AssertAreEqual(new int[] { 0, -1, 22, -3, -4, -5, -6, -7, 88, -9 }, data.GetData());
            AssertAreEqual(new int[] { -1, 22, -3, -4, -5, -6, -7, 88, }, view1.GetData());
            AssertAreEqual(new int[] { 88, -6, -4, 22, }, view2.GetData());
            AssertAreEqual(new int[] { 22, 88 }, view3.GetData());
        }
Exemple #5
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 TypedArrayStorage(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 = new ViewStorage(data, ":");

            Assert.AreEqual(new Shape(3, 3), view.Shape);
            AssertAreEqual(data.GetData(), view.GetData());
            // return reduced view
            view = new ViewStorage(data, "1:");
            Assert.AreEqual(new Shape(2, 3), view.Shape);
            AssertAreEqual(new int[] { 3, 4, 5, 6, 7, 8 }, view.GetData());
            view = new ViewStorage(data, "1:,:");
            Assert.AreEqual(new Shape(2, 3), view.Shape);
            AssertAreEqual(new int[] { 3, 4, 5, 6, 7, 8 }, view.GetData());
            view = new ViewStorage(data, ":,1:");
            Assert.AreEqual(new Shape(3, 2), view.Shape);
            AssertAreEqual(new int[] { 1, 2, 4, 5, 7, 8 }, view.GetData());
            view = new ViewStorage(data, "1:2, 0:1");
            Assert.AreEqual(new Shape(1, 1), view.Shape);
            AssertAreEqual(new int[] { 3 }, view.GetData());
            // 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 = new ViewStorage(data, "::2");
            Assert.AreEqual(new Shape(2, 3), view.Shape);
            AssertAreEqual(new int[] { 0, 1, 2, 6, 7, 8 }, view.GetData());
            view = new ViewStorage(data, "::3");
            Assert.AreEqual(new Shape(1, 3), view.Shape);
            AssertAreEqual(new int[] { 0, 1, 2 }, view.GetData());
            view = new ViewStorage(data, "::-1");
            Assert.AreEqual(new Shape(3, 3), view.Shape);
            AssertAreEqual(new int[] { 6, 7, 8, 3, 4, 5, 0, 1, 2, }, view.GetData());
            view = new ViewStorage(data, "::-2");
            Assert.AreEqual(new Shape(2, 3), view.Shape);
            AssertAreEqual(new int[] { 6, 7, 8, 0, 1, 2, }, view.GetData());
            view = new ViewStorage(data, "::-3");
            Assert.AreEqual(new Shape(1, 3), view.Shape);
            AssertAreEqual(new int[] { 6, 7, 8, }, view.GetData());
            // N-Dim Stepping
            //>>> x[::2,::2]
            //array([[0, 2],
            //       [6, 8]])
            //>>> x[::- 1,::- 2]
            //array([[8, 6],
            //       [5, 3],
            //       [2, 0]])
            view = new ViewStorage(data, "::2, ::2");
            Assert.AreEqual(new Shape(2, 2), view.Shape);
            AssertAreEqual(new int[] { 0, 2, 6, 8 }, view.GetData());
            view = new ViewStorage(data, "::-1, ::-2");
            Assert.AreEqual(new Shape(3, 2), view.Shape);
            AssertAreEqual(new int[] { 8, 6, 5, 3, 2, 0 }, view.GetData());
        }
Exemple #6
0
 public void InternalArrayCheck()
 {
     Assert.IsTrue(strg1D.GetData().Length == 10);
     Assert.IsTrue(strg2D.GetData().Length == 9);
     Assert.IsTrue(strg2DNonFull.GetData().Length == 10);
 }