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)); } }
/// <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(IArraySlice values, Shape shape = default, char order = 'C') : this(values.TypeCode) { if (order != 'C') shape.ChangeTensorLayout(order); if (shape.IsEmpty) shape = Shape.Vector((int) values.Count); //TODO! when long index, remove cast int Storage.Allocate(values, shape); }
/// <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); }
/// <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(IArraySlice values, Shape shape = default, char order = 'C') : this(values.TypeCode) { if (order != 'C') shape.ChangeTensorLayout(order); if (shape.IsEmpty) shape = Shape.Vector(values.Count); Storage.Allocate(values, shape); }
public void FromJaggedArray(Array dotNetArray) { if (!dotNetArray.GetType().GetElementType().IsArray) { throw new Exception("Multi dim arrays are not allowed here!"); } List <int> dimList = new List <int>(); dimList.Add(dotNetArray.Length); object currentArr = dotNetArray; while (currentArr.GetType().GetElementType().IsArray) { Array child = (Array)((Array)currentArr).GetValue(0); dimList.Add(child.Length); currentArr = child; } Type elementType = currentArr.GetType().GetElementType(); int[] dims = dimList.ToArray(); Shape shape = new Shape(dims); shape.ChangeTensorLayout(); NDArray nd = new NDArray(elementType, shape); Array ndStrg = nd.Storage.GetData(); if (dims.Length == 1) { throw new NotImplementedException("FromJaggedArray dims.Length == 1"); } else if (dims.Length == 2) { switch (dotNetArray) { case double[][] array: for (int i = 0; i < dims[0]; i++) { for (int j = 0; j < dims[1]; j++) { nd[i, j] = array[i][j]; } } break; } } this.Storage = nd.Storage; }
/// <summary> /// /// </summary> /// <param name="shape"></param> /// <param name="order"> /// C: row major /// F: column major /// </param> /// <returns></returns> public NDArray reshape(Shape shape, string order = "C") { // have to update Array storage if (ndim > 1 && order != this.order && shape != this.shape) { shape.ChangeTensorLayout(order); if (ndim == 2) { var nd = flatten(order); switch (dtype.Name) { case "Int32": return(new NDArray(nd.Data <int>(), shape: shape, order: order)); } } } return(new NDArray(Array, shape: shape, order: order)); }
public Array ToMuliDimArray <T>() { Array dotNetArray = Arrays.Create(typeof(T), this.shape); var pufferShape = new Shape(shape); pufferShape.ChangeTensorLayout(); int[] indexes = null; object idxValue = null; T[] array = Storage.GetData <T>(); for (int idx = 0; idx < this.size; idx++) { indexes = pufferShape.GetDimIndexOutShape(idx); idxValue = array[Storage.Shape.GetIndexInShape(slice, indexes)]; dotNetArray.SetValue(idxValue, indexes); } return(dotNetArray); }
/// <summary> /// Return a copy of the array collapsed into one dimension. /// /// A 1-D array, containing the elements of the input, is returned.A copy is made only if needed. /// </summary> public NDArray flatten(string order = "C") { var nd = new NDArray(dtype, size); var s = new Shape(Storage.Shape.Dimensions); s.ChangeTensorLayout(order); switch (dtype.Name) { case "Int32": var values1 = Array as int[]; var values2 = new int[size]; for (int i = 0; i < size; i++) { // Data<int>(s.GetDimIndexOutShape(i)) values2[i] = values1[Storage.Shape.GetIndexInShape(s.GetDimIndexOutShape(i))]; } nd.Array = values2; break; } return(nd); }