/// <summary> /// Constructor which takes .NET array /// dtype and shape is determined from array /// </summary> /// <param name="values"></param> /// <returns>Array with values</returns> public NDArray(Array values, Shape shape = null) : this(values.GetType().GetElementType()) { int[] strgDim = new int[values.Rank]; for (int idx = 0; idx < strgDim.Length; idx++) { strgDim[idx] = values.GetLength(idx); } Storage = new NDStorage(dtype); Storage.Allocate(dtype, new Shape(strgDim)); switch (values.Rank) { case 1: { Storage.SetData(values); break; } } if (!(shape is null)) { Storage.Reshape(shape); } }
public void FromMultiDimArray(Array dotNetArray) { if (dotNetArray.GetType().GetElementType().IsArray) { throw new Exception("Jagged arrays are not allowed here!"); } int[] dims = new int[dotNetArray.Rank]; for (int idx = 0; idx < dims.Length; idx++) { dims[idx] = dotNetArray.GetLength(idx); } Storage = new NDStorage(); Storage.Allocate(dotNetArray.GetType().GetElementType(), new Shape(dims)); Array internalStrg = Storage.GetData(); var pufferShape = new Shape(dims); pufferShape.ChangeTensorLayout(); int[] idxDims = null; object valueIdx = null; for (int idx = 0; idx < Storage.Shape.Size; idx++) { idxDims = pufferShape.GetDimIndexOutShape(idx); valueIdx = dotNetArray.GetValue(pufferShape.GetDimIndexOutShape(idx)); internalStrg.SetValue(valueIdx, Storage.Shape.GetIndexInShape(idxDims)); } }
public StorageTester() { strg1D = new NDStorage(np.float64); strg1D.Allocate(np.float64, new Shape(10), 1); strg1D.SetData(new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }); strg2D = new NDStorage(np.int64); strg2D.Allocate(np.int64, new Shape(3, 3), 1); strg2D.SetData(new Int64[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }); strg2DNonFull = new NDStorage(np.float32); strg2DNonFull.Allocate(np.float32, new Shape(5, 2), 1); strg2DNonFull.SetData(new float[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }); strg3D = new NDStorage(typeof(Complex)); strg3D.Allocate(typeof(Complex), new Shape(2, 2, 2)); strg3D.SetData(new Complex[] { 1, 2, 3, 4, 5, 6, 7, 8 }); strg3DNonFull = new NDStorage(typeof(Complex)); strg3DNonFull.Allocate(typeof(Complex), new Shape(2, 3, 4)); var puffer = new Complex[24]; for (int idx = 1; idx < 25; idx++) { puffer[idx - 1] = new Complex(idx, 0); } strg3DNonFull.SetData(puffer); }
//[TestMethod] public void CheckChangeTensorLayout2D() { var strg2DCpy = (NDStorage)strg2D.Clone(); Assert.IsTrue(Enumerable.SequenceEqual(strg2DCpy.Shape.Dimensions, new int[] { 3, 3 })); Assert.IsTrue(Enumerable.SequenceEqual(strg2DCpy.GetData <Int64>(), new Int64[] { 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 <Int64>(), strg2D.GetData <Int64>())); strg2DCpy = (NDStorage)strg2DNonFull.Clone(); Assert.IsTrue(Enumerable.SequenceEqual(strg2DCpy.Shape.Dimensions, new int[] { 5, 2 })); Assert.IsTrue(Enumerable.SequenceEqual(strg2DCpy.GetData <Int64>(), new Int64[] { 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 <Int64>(), strg2DNonFull.GetData <Int64>())); strg2DCpy = new NDStorage(typeof(Int64)); strg2DCpy.Allocate(new Shape(5, 2)); strg2DCpy.SetData(strg2DNonFull.GetData()); Assert.IsTrue(Enumerable.SequenceEqual(strg2DCpy.GetData <Int64>(), new Int64[] { 0, 2, 4, 6, 8, 1, 3, 5, 7, 9 })); }
/// <summary> /// Constructor which takes .NET array /// dtype and shape is determined from array /// </summary> /// <param name="values"></param> /// <returns>Array with values</returns> public NDArray(Array values, Shape shape = null) : this(values.GetType().GetElementType()) { if (shape is null) { shape = new Shape(values.Length); } Storage = new NDStorage(dtype); Storage.Allocate(new Shape(shape)); Storage.SetData(values); }
public StorageTester() { Storage1DInt = NDStorage.CreateByShapeAndType(typeof(int), new Shape(6)); Storage1DInt.SetData(new int[] { 0, 1, 2, 3, 4, 5 }); Storage1DDouble = NDStorage.CreateByShapeAndType(typeof(double), new Shape(6)); Storage1DDouble.SetData(new double[] { 0.1, 1.5, 2.2, 3.5, 4.9, 5.0 }); Storage2DInt = NDStorage.CreateByShapeAndType(typeof(int), new Shape(2, 3)); Storage2DInt.SetData(new int[] { 0, 1, 2, 3, 4, 5 }); }
/// <summary> /// Constructor which initialize elements with 0 /// type and shape are given. /// </summary> /// <param name="dtype">internal data type</param> /// <param name="shape">Shape of NDArray</param> public NDArray(Type dtype, Shape shape) { TensorEngine = BackendFactory.GetEngine(); Storage = new NDStorage(dtype); Storage.Allocate(shape); }
/*public NDArray() * { * throw new Exception("Don't use 0 parameter constructor."); * }*/ /// <summary> /// Constructor for init data type /// internal storage is 1D with 1 element /// </summary> /// <param name="dtype">Data type of elements</param> public NDArray(Type dtype) { TensorEngine = BackendFactory.GetEngine(); Storage = new NDStorage(dtype); }
public NDArray(Shape shape) : this() { Storage = new NDStorage(typeof(T)); Storage.Allocate(this.dtype, shape, 1); }
public NDArray() { Storage = new NDStorage(typeof(T)); Storage.Allocate(this.dtype, new Shape(1), 1); }
public NDArray(Shape shape) : this() { Storage = NDStorage.CreateByShapeAndType(Storage.dtype, shape); }
public NDArray() { Storage.dtype = typeof(T); Storage = NDStorage.CreateByShapeAndType(this.dtype, new Shape(1)); }
/// <summary> /// Default constructor /// Create a 1D double array with 1 element /// one element is 1 /// </summary> public NDArray() { Storage = new NDStorage(); TensorEngine = BackendFactory.GetEngine(); }