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); }
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 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); }
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)); }
public NDArray() { Storage = NDStorage.CreateByShapeAndType(typeof(double), new Shape(1)); }
/// <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); }