public NDArray transpose() { var nd = new NDArray(dtype, new Shape(this.Storage.Shape.Shapes.Reverse().ToArray())); if (ndim == 1) { nd.Storage = NDStorage.CreateByShapeAndType(dtype, new Shape(1, shape.Shapes[0])); } else if (ndim == 2) { for (int idx = 0; idx < nd.shape.Shapes[0]; idx++) { for (int jdx = 0; jdx < nd.shape.Shapes[1]; jdx++) { nd[idx, jdx] = this[jdx, idx]; } } } else { throw new NotImplementedException(); } return(nd); }
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), 1); Array internalStrg = Storage.GetData(); var pufferShape = new Shape(dims); pufferShape.ChangeTensorLayout(2); 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 static NDStorage CreateByArray(Array values) { Type dtype = null; if (!values.GetType().GetElementType().IsArray) { dtype = values.GetType().GetElementType(); } else { throw new IncorrectShapeException(); } int[] dims = new int[values.Rank]; for (int idx = 0; idx < dims.Length; idx++) { dims[idx] = values.GetLength(idx); } var storage = NDStorage.CreateByShapeAndType(dtype, new Shape(dims)); storage.values = Array.CreateInstance(dtype, values.Length); storage.SetData(values); return(storage); }
/// <summary> /// Create a NDStorage by data type and array shape /// </summary> /// <param name="dtype">The type of arrays elements</param> /// <param name="shape">The shape of array/param> /// <returns>The constructed NDStorage</returns> public static NDStorage CreateByShapeAndType(Type dtype, Shape shape) { var storage = new NDStorage(dtype); storage.Shape = shape; storage.Allocate(shape.Size); return(storage); }
public NDArray(Type dtype) { this.dtype = dtype; // set default shape as 1 dim and 0 elements. Shape = new Shape(new int[] { 0 }); Storage = new NDStorage(dtype); }
public object Clone() { var puffer = new NDStorage(); puffer.Allocate(_DType, new Shape(_Shape.Dimensions), _TensorLayout); puffer.SetData((Array)_values.Clone()); return(puffer); }
public NDArray array(Array array, Type dtype = null, int ndim = 1) { dtype = (dtype == null) ? array.GetType().GetElementType() : dtype; var nd = new NDArray(dtype); if ((array.Rank == 1) && (!array.GetType().GetElementType().IsArray)) { nd.Storage = NDStorage.CreateByShapeAndType(dtype, new Shape(new int[] { array.Length })); nd.Storage.SetData(array); } else { throw new Exception("Method is not implemeneted for multidimensional arrays or jagged arrays."); } return(nd); }
public new matrix transpose() { Storage.Shape = new Shape(this.Storage.Shape.Shapes.Reverse().ToArray()); var nd = new NDArray(this.dtype, this.shape); switch (this.dtype.Name) { case "Double": nd.Set(this.float64); break; case "Int32": nd.Set(this.int32); break; } if (ndim == 1) { Storage = NDStorage.CreateByShapeAndType(dtype, new Shape(1, shape.Shapes[0])); Storage.SetData(nd.float64); } else if (ndim == 2) { for (int idx = 0; idx < shape.Shapes[0]; idx++) { for (int jdx = 0; jdx < shape.Shapes[1]; jdx++) { this[idx, jdx] = nd[jdx, idx]; } } } else { throw new NotImplementedException(); } return(this); }
public NDArray ones(Type dtype = null, params int[] shapes) { dtype = (dtype == null) ? typeof(double) : dtype; int dataLength = 1; for (int idx = 0; idx < shapes.Length; idx++) { dataLength *= shapes[idx]; } Array dataArray = Array.CreateInstance(dtype, dataLength); for (int idx = 0; idx < dataLength; idx++) { dataArray.SetValue(1, idx); } this.Storage = NDStorage.CreateByShapeAndType(dtype, new Shape(shapes)); this.Storage.SetData(dataArray); return(this); }
/// <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) { Storage = new NDStorage(dtype); }
/// <summary> /// Default constructor /// Create a 1D double array with 1 element /// one element is 1 /// </summary> public NDArray() { Storage = new NDStorage(); }
/// <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) { Storage = new NDStorage(); Storage.Allocate(dtype, shape, 1); }
public NDArray() { Storage = NDStorage.CreateByShapeAndType(typeof(double), new Shape(1)); }
public NDArray(Type dtype, params int[] shapes) { Storage = NDStorage.CreateByShapeAndType(dtype, new Shape(shapes)); }
public NDArray(Type dtype, Shape shape) { Storage = NDStorage.CreateByShapeAndType(dtype, shape); }
public NDArray(Type dtype) { Storage = NDStorage.CreateByShapeAndType(dtype, new Shape(1)); }
/// <summary> /// Return the maximum of an array or minimum along an axis /// </summary> /// <param name="np"></param> /// <param name="axis"></param> /// <returns></returns> public NDArray amin(int?axis = null) { var res = new NDArray(dtype); double[] npArr = this.Storage.GetData <double>(); if (axis == null) { double min = npArr[0]; for (int i = 0; i < npArr.Length; i++) { min = Math.Min(min, npArr[i]); } res.Storage = NDStorage.CreateByArray(new double[1] { min }); } else { if (axis < 0 || axis >= this.ndim) { throw new Exception("Invalid input: axis"); } int[] resShapes = new int[this.shape.Shapes.Count - 1]; int index = 0; //index for result shape set //axis departs the shape into three parts: prev, cur and post. They are all product of shapes int prev = 1; int cur = 1; int post = 1; int size = 1; //total number of the elements for result //Calculate new Shape for (int i = 0; i < this.shape.Shapes.Count; i++) { if (i == axis) { cur = this.shape.Shapes[i]; } else { resShapes[index++] = this.shape.Shapes[i]; size *= this.shape.Shapes[i]; if (i < axis) { prev *= this.shape.Shapes[i]; } else { post *= this.shape.Shapes[i]; } } } res.Storage.Shape = new Shape(resShapes); //Fill in data index = 0; //index for result data set int sameSetOffset = this.shape.DimOffset[axis.Value]; int increments = cur * post; double[] resData = new double[size]; //res.Data = new double[size]; int start = 0; double min = 0; for (int i = 0; i < this.size; i += increments) { for (int j = i; j < i + post; j++) { start = j; min = npArr[start]; for (int k = 0; k < cur; k++) { min = Math.Min(min, npArr[start]); start += sameSetOffset; } resData[index++] = min; } } res.Storage = NDStorage.CreateByArray(resData); res.Storage.Shape = new Shape(resShapes); } return(res); }
/// <summary> /// Convolution of 2 series /// </summary> /// <param name="numSharpArray1"></param> /// <param name="numSharpArray2"></param> /// <param name="mode"></param> /// <returns></returns> public NDArray Convolve(NDArray numSharpArray2, string mode = "full") { int nf = this.shape.Shapes[0]; int ng = numSharpArray2.shape.Shapes[0]; if (shape.NDim > 1) { throw new IncorrectShapeException(); } var numSharpReturn = new NDArray(typeof(double)); double[] np1 = this.Storage.GetData <double>(); double[] np2 = numSharpArray2.Storage.GetData <double>(); switch (mode) { case "full": { int n = nf + ng - 1; var outArray = new double[n]; for (int idx = 0; idx < n; ++idx) { int jmn = (idx >= ng - 1) ? (idx - (ng - 1)) : 0; int jmx = (idx < nf - 1) ? idx : nf - 1; for (int jdx = jmn; jdx <= jmx; ++jdx) { outArray[idx] += (np1[jdx] * np2[idx - jdx]); } } numSharpReturn.Storage = NDStorage.CreateByShapeAndType(numSharpReturn.dtype, new Shape(outArray.Length)); numSharpReturn.Storage.SetData(outArray); break; } case "valid": { var min_v = (nf < ng) ? np1 : np2; var max_v = (nf < ng) ? np2 : np1; int n = Math.Max(nf, ng) - Math.Min(nf, ng) + 1; double[] outArray = new double[n]; for (int idx = 0; idx < n; ++idx) { int kdx = idx; for (int jdx = (min_v.Length - 1); jdx >= 0; --jdx) { outArray[idx] += min_v[jdx] * max_v[kdx]; ++kdx; } } numSharpReturn.Storage = NDStorage.CreateByShapeAndType(numSharpReturn.dtype, new Shape(outArray.Length)); numSharpReturn.Storage.SetData(outArray); break; } case "same": { // followed the discussion on // https://stackoverflow.com/questions/38194270/matlab-convolution-same-to-numpy-convolve // implemented numpy convolve because we follow numpy var npad = numSharpArray2.shape.Shapes[0] - 1; double[] np1New = null; if (npad % 2 == 1) { npad = (int)Math.Floor(((double)npad) / 2.0); np1New = (double[])np1.Clone(); np1New.ToList().AddRange(new double[npad + 1]); var puffer = (new double[npad]).ToList(); puffer.AddRange(np1New); np1New = puffer.ToArray(); } else { npad = npad / 2; np1New = (double[])np1.Clone(); var puffer = np1New.ToList(); puffer.AddRange(new double[npad]); np1New = puffer.ToArray(); puffer = (new double[npad]).ToList(); puffer.AddRange(np1New); np1New = puffer.ToArray(); } var numSharpNew = new NumPy().array(np1New, dtype); numSharpReturn = numSharpNew.Convolve(numSharpArray2, "valid"); break; } } return(numSharpReturn); }