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])"); }
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>()); }
public static NDArray array <T>(T[,,,] data) where T : unmanaged { if (data == null) { throw new ArgumentNullException(nameof(data)); } return(new NDArray(ArraySlice.FromArray(data.Cast <T>().ToArray()), new Shape(data.GetLength(0), data.GetLength(1), data.GetLength(2), data.GetLength(3)))); }
/// <summary> /// Constructor which takes .NET array /// dtype and shape is determined from array /// </summary> /// <param name="values"></param> /// <param name="shape"></param> /// <param name="order"></param> /// <returns>Array with values</returns> /// <remarks>This constructor calls <see cref="IStorage.Allocate(NumSharp.Shape,System.Type)"/></remarks> public NDArray(Array values, Shape shape = default, char order = 'C') : this(values.GetType().GetElementType()) { if (order != 'C') shape.ChangeTensorLayout(order); if (shape.IsEmpty) shape = Shape.ExtractShape(values); Storage.Allocate(values.ResolveRank() != 1 ? ArraySlice.FromArray(Arrays.Flatten(values), false) : ArraySlice.FromArray(values, false), shape); }
public void Setup() { var arraySize = 100000; A1 = ArraySlice.FromArray(new double[arraySize].Select((x, idx) => x + idx).ToArray()); ACopy1 = ArraySlice.FromArray(new double[arraySize].Select((x, idx) => x + idx).ToArray()); ASlice1 = ArraySlice.FromArray(new double[arraySize - 2].Select((x, idx) => x + idx).ToArray()); ASliceStep2 = ArraySlice.FromArray(new double[((arraySize - 2) + 1) / 2].Select((x, idx) => x + idx).ToArray()); M1 = new Memory <double>(A1.ToArray()); M2 = new Memory <double>(A1.ToArray(), 2, A1.Count - 2); ND1 = new NDArray(new double[arraySize].Select((x, idx) => x + idx).ToArray()); NDSlice1 = ND1["1:" + (ND1.size - 2)]; NDSliceStep2 = ND1["1:" + (ND1.size - 2) + ":2"]; NDDouble1 = new NDArray(new double[arraySize].Select((x, idx) => x + idx).ToArray()).MakeGeneric <double>(); NDDoubleSlice1 = NDDouble1["1:" + (NDDouble1.size - 2)]; NDDoubleSliceStep2 = NDDouble1["1:" + (NDDouble1.size - 2) + ":2"]; }
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>()); }
public static NDArray array <T>(params T[] data) where T : unmanaged => new NDArray(ArraySlice.FromArray(data), Shape.Vector(data.Length));
/// <summary> /// Creates a Vector <see cref="NDArray"/> from given <paramref name="data"/>. /// </summary> /// <typeparam name="T">The type of given array, must be compliant to numpy's supported dtypes.</typeparam> /// <param name="data">The enumeration of data to create <see cref="NDArray"/> from.</param> /// <returns>An <see cref="NDArray"/> with the data and shape of the given array.</returns> /// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html <br></br>Always performs a copy.</remarks> public static NDArray array <T>(IEnumerable <T> data) where T : unmanaged { var slice = ArraySlice.FromArray(data.ToArray(), false); return(new NDArray(slice, Shape.Vector(slice.Count))); }
public static NDArray array <T>(params T[] data) where T : unmanaged { return(new NDArray(ArraySlice.FromArray(data), new Shape(data.Length))); }