Beispiel #1
0
        /// <summary>
        /// Filter specific elements through select.
        /// </summary>
        /// <param name="select"></param>
        /// <returns>Return a new NDArray with filterd elements.</returns>
        public NDArrayGeneric <T> this[IList <int> select]
        {
            get
            {
                var n = new NDArrayGeneric <T>();
                if (NDim == 1)
                {
                    n.Data  = new T[select.Count];
                    n.Shape = new Shape(select.Count);
                    for (int i = 0; i < select.Count; i++)
                    {
                        n[i] = this[select[i]];
                    }
                }
                else if (NDim == 2)
                {
                    n.Data  = new T[select.Count * Shape[1]];
                    n.Shape = new Shape(select.Count, Shape[1]);
                    for (int i = 0; i < select.Count; i++)
                    {
                        for (int j = 0; j < Shape[1]; j++)
                        {
                            n[i, j] = this[select[i], j];
                        }
                    }
                }
                else
                {
                    throw new NotImplementedException();
                }

                return(n);
            }
        }
Beispiel #2
0
        public NDArrayGeneric <T> this[Shape select]
        {
            get
            {
                if (select.NDim == NDim)
                {
                    throw new Exception("Please use NDArray[m, n] to access element.");
                }
                else
                {
                    int start  = GetIndexInShape(select.Shapes.ToArray());
                    int length = Shape.DimOffset[select.NDim - 1];

                    var      n    = new NDArrayGeneric <T>();
                    Span <T> data = Data;
                    n.Data = data.Slice(start, length).ToArray();
                    int[] shape = new int[Shape.NDim - select.NDim];
                    for (int i = select.NDim; i < Shape.NDim; i++)
                    {
                        shape[i - select.NDim] = Shape[i];
                    }
                    n.Shape = new Shape(shape);
                    // n.Shape = new Shape(Shape.Shapes.ToArray().AsSpan().Slice(select.Length).ToArray());
                    return(n);
                }
            }
        }
Beispiel #3
0
        public NDArrayGeneric <T> arange(int start, int stop, int step = 1)
        {
            if (start > stop)
            {
                throw new Exception("parameters invalid");
            }

            switch (typeof(T).Name)
            {
            case "Int32":
            {
                var n = new NDArrayGeneric <int>();
                n.arange(stop, start, step);
                return(n as NDArrayGeneric <T>);
            }

            case "Double":
            {
                var n = new NDArrayGeneric <double>();
                n.arange(stop, start, step);
                return(n as NDArrayGeneric <T>);
            }

            default:
                throw new NotImplementedException();
            }
        }
Beispiel #4
0
    public static dynamic arange(object start, object stop, object step)
    {
        dynamic returnValue = null;

        switch (start)
        {
        case int startCast:
        {
            int stopCast = (int)stop;
            int stepCast = (int)step;

            returnValue = new NumSharp.Core.NDArrayGeneric <int>().arange(stopCast, startCast, stepCast);

            break;
        }

        case float startCast_:
        {
            int startCast = (int)start;
            int stopCast  = (int)stop;
            int stepCast  = (int)step;

            returnValue = new NumSharp.Core.NDArrayGeneric <float>().arange(stopCast, startCast, stepCast);

            break;
        }
        }
        return(returnValue);
    }
Beispiel #5
0
        public static NDArrayGeneric <T> sin <T>(this NumPyGeneric <T> np, NDArrayGeneric <T> nd)
        {
            NDArrayGeneric <T> sinArray = new NDArrayGeneric <T>();

            sinArray.Data  = new T[nd.Size];
            sinArray.Shape = new Shape(nd.Shape.Shapes);

            for (int idx = 0; idx < nd.Size; idx++)
            {
                switch (nd[idx])
                {
                case double d:
                    sinArray[idx] = (T)(object)Math.Sin(d);
                    break;

                case float d:
                    sinArray[idx] = (T)(object)Math.Sin(d);
                    break;

                case Complex d:
                    sinArray[idx] = (T)(object)Complex.Sin(d);
                    break;
                }
            }

            return(sinArray);
        }
Beispiel #6
0
        /// <summary>
        /// Stack arrays in sequence vertically (row wise).
        /// </summary>
        /// <param name="nps"></param>
        /// <returns></returns>
        public static NDArrayGeneric <T> VStack <T>(this NDArrayGeneric <T> np1, params NDArrayGeneric <T>[] nps)
        {
            if (nps == null || nps.Length == 0)
            {
                throw new Exception("Input arrays can not be empty");
            }
            List <T>           list = new List <T>();
            NDArrayGeneric <T> np   = new NDArrayGeneric <T>();

            foreach (NDArrayGeneric <T> ele in nps)
            {
                if (nps[0].Shape != ele.Shape)
                {
                    throw new Exception("Arrays mush have same shapes");
                }
                list.AddRange(ele.Data);
            }
            np.Data = list.ToArray();
            if (nps[0].NDim == 1)
            {
                np.Shape = new Shape(new int[] { nps.Length, nps[0].Shape.Shapes[0] });
            }
            else
            {
                int[] shapes = nps[0].Shape.Shapes.ToArray();
                shapes[0] *= nps.Length;
                np.Shape   = new Shape(shapes);
            }
            return(np);
        }
Beispiel #7
0
        public NDArrayGeneric <T> power(T exponent)
        {
            NDArrayGeneric <T> sinArray = new NDArrayGeneric <T>();

            sinArray.Data  = new T[this.Size];
            sinArray.Shape = new Shape(this.Shape.Shapes);

            switch (Data)
            {
            case double[] sinData:
            {
                for (int idx = 0; idx < sinData.Length; idx++)
                {
                    sinArray[idx] = (T)(object)Math.Pow(sinData[idx], (double)(object)exponent);
                }
                break;
            }

            default:
            {
                throw new Exception("The operation is not implemented for the");
            }
            }
            return(sinArray);
        }
Beispiel #8
0
        public NDArrayGeneric <T> inv()
        {
            NDArrayGeneric <T> npInv = new NDArrayGeneric <T>();

            npInv.Shape = new Shape(this.Shape.Shapes);
            npInv.Data  = new T[this.Data.Length];

            switch (this)
            {
            case NDArrayGeneric <double> np:
            {
                NDArrayGeneric <double> npInvDouble = npInv as NDArrayGeneric <double>;
                double[][] matrix = np.ToDotNetArray <double[][]>();

                double[][] matrixInv = MatrixInv.InverseMatrix(matrix);

                for (int idx = 0; idx < npInv.Shape.Shapes[0]; idx++)
                {
                    for (int jdx = 0; jdx < npInv.Shape.Shapes[1]; jdx++)
                    {
                        npInvDouble[idx, jdx] = matrixInv[idx][jdx];
                    }
                }
                break;
            }

            default:
            {
                throw new Exception("This method was not implemented for this Type : " + typeof(T).Name);
            }
            }

            return(npInv);
        }
Beispiel #9
0
        public static NDArrayGeneric <T> array <T>(this NumPyGeneric <T> np, IEnumerable <T> array, int ndim = 1)
        {
            var nd = new NDArrayGeneric <T>();

            nd.Data  = array.ToArray();
            nd.Shape = new Shape(new int[] { nd.Data.Length });

            return(nd);
        }
Beispiel #10
0
        public NDArrayGeneric <T> array(T[] data)
        {
            var n = new NDArrayGeneric <T>();

            n.Data  = data;
            n.Shape = new Shape(new int[] { data.Length });

            return(n);
        }
Beispiel #11
0
        public NDArrayGeneric <T> log()
        {
            NDArrayGeneric <T> logArray = new NDArrayGeneric <T>();

            logArray.Data  = new T[this.Data.Length];
            logArray.Shape = new Shape(this.Shape.Shapes);

            switch (logArray.Data)
            {
            case double[] logData:
            {
                double[] npData = this.Data as double[];

                for (int idx = 0; idx < npData.Length; idx++)
                {
                    logData[idx] = Math.Log(npData[idx]);
                }
                break;
            }

            case float[] logData:
            {
                double[] npData = this.Data as double[];

                for (int idx = 0; idx < npData.Length; idx++)
                {
                    // boxing necessary because Math.Sin just for double
                    logData[idx] = (float)Math.Log(npData[idx]);
                }
                break;
            }

            case Complex[] logData:
            {
                Complex[] npData = this.Data as Complex[];

                for (int idx = 0; idx < npData.Length; idx++)
                {
                    // boxing necessary because Math.Sin just for double
                    logData[idx] = Complex.Log(npData[idx]);
                }
                break;
            }

            default:
            {
                throw new Exception("The operation is not implemented for the");
            }
            }
            return(logArray);
        }
Beispiel #12
0
        public static NDArrayGeneric <Byte> array <T>(this NumPyGeneric <T> np, System.Drawing.Bitmap image)
        {
            NDArrayGeneric <Byte> imageArray = new NDArrayGeneric <Byte>();

            var bmpd     = image.LockBits(new System.Drawing.Rectangle(0, 0, image.Width, image.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, image.PixelFormat);
            var dataSize = bmpd.Stride * bmpd.Height;

            imageArray.Data = new byte[dataSize];
            System.Runtime.InteropServices.Marshal.Copy(bmpd.Scan0, imageArray.Data, 0, imageArray.Data.Length);
            image.UnlockBits(bmpd);

            imageArray.Shape = new Shape(new int[] { bmpd.Height, bmpd.Width, System.Drawing.Image.GetPixelFormatSize(image.PixelFormat) / 8 });

            return(imageArray);
        }
Beispiel #13
0
        /// <summary>
        /// Return a new array of given shape and type, filled with ones.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="np"></param>
        /// <param name="shape"></param>
        /// <returns></returns>
        public static NDArrayGeneric <T> ones <T>(this NumPyGeneric <T> np, Shape shape)
        {
            var nd = new NDArrayGeneric <T>();

            nd.Shape = shape;

            switch (default(T))
            {
            case int data:
                nd.Data = Enumerable.Range(0, nd.Size).Select(x => (T)(object)1).ToArray();
                break;

            case double data:
                nd.Data = Enumerable.Range(0, nd.Size).Select(x => (T)(object)1.0).ToArray();
                break;
            }

            return(nd);
        }
Beispiel #14
0
        public NDArrayGeneric <T> zeros(params int[] shape)
        {
            switch (typeof(T).Name)
            {
            case "Int32":
            {
                var n = new NDArrayGeneric <int>();
                n.Zeros(shape);
                return(n as NDArrayGeneric <T>);
            }

            case "Double":
            {
                var n = new NDArrayGeneric <double>();
                n.Zeros(shape);
                return(n as NDArrayGeneric <T>);
            }

            default:
                throw new NotImplementedException();
            }
        }
Beispiel #15
0
        public NDArrayGeneric <T> array(T[][] data)
        {
            int size = data.Length * data[0].Length;
            var all  = new T[size];

            int idx = 0;

            for (int row = 0; row < data.Length; row++)
            {
                for (int col = 0; col < data[row].Length; col++)
                {
                    all[idx] = data[row][col];
                    idx++;
                }
            }

            var n = new NDArrayGeneric <T>();

            n.Data  = all;
            n.Shape = new Shape(new int[] { data.Length, data[0].Length });

            return(n);
        }
Beispiel #16
0
        public static NDArrayGeneric <NDArrayGeneric <T> > sin <T>(this NumPyGeneric <T> np, NDArrayGeneric <NDArrayGeneric <T> > nd)
        {
            var sinArray = new NDArrayGeneric <NDArrayGeneric <T> >();

            sinArray.Data  = new NDArrayGeneric <T> [nd.Size];
            sinArray.Shape = new Shape(nd.Shape.Shapes);

            for (int idx = 0; idx < nd.Size; idx++)
            {
                switch (default(T))
                {
                case double d:
                    sinArray[idx] = new NDArrayGeneric <T>
                    {
                        Data  = new T[] { (T)(object)Math.Sin(d) },
                        Shape = new Shape(1)
                    };
                    break;
                }
            }

            return(sinArray);
        }
Beispiel #17
0
        /// <summary>
        /// Draw random samples from a normal (Gaussian) distribution.
        /// </summary>
        /// <param name="loc">Mean of the distribution</param>
        /// <param name="scale">Standard deviation of the distribution</param>
        /// <param name="size"></param>
        /// <returns></returns>
        public NDArrayGeneric <double> normal(double loc, double scale, params int[] size)
        {
            if (size.Length == 0)
            {
                throw new Exception("d cannot be empty.");
            }
            NDArrayGeneric <double> array = new NDArrayGeneric <double>();
            Random rand = new Random(); //reuse this if you are generating many

            array.Shape = new Shape(size);
            array.Data  = new double[array.Shape.Size];

            for (int i = 0; i < array.Shape.Size; i++)
            {
                double u1            = 1.0 - rand.NextDouble(); //uniform(0,1] random doubles
                double u2            = 1.0 - rand.NextDouble();
                double randStdNormal = Math.Sqrt(-2.0 * Math.Log(u1)) *
                                       Math.Sin(2.0 * Math.PI * u2); //random normal(0,1)
                double randNormal = loc + scale * randStdNormal;     //random normal(mean,stdDev^2)
                array.Data[i] = randNormal;
            }
            return(array);
        }
Beispiel #18
0
        public NDArrayGeneric <NDArrayGeneric <T> > this[Slice select]
        {
            get
            {
                var result = new NDArrayGeneric <NDArrayGeneric <T> >();
                result.Data  = new NDArrayGeneric <T> [select.Length];
                result.shape = new Shape(select.Length);

                int[] shape = new int[Shape.Length];
                for (int i = 0; i < Shape.Length; i++)
                {
                    if (i == 0)
                    {
                        shape[i] = select.Step;
                    }
                    else
                    {
                        shape[i] = Shape[i];
                    }
                }

                int index = 0;
                var list  = new NDArrayGeneric <T>();
                for (int s = select.Start; s < select.Stop; s += select.Step)
                {
                    var      n    = new NDArrayGeneric <T>();
                    Span <T> data = Data;
                    n.Data  = data.Slice(s, select.Step).ToArray();
                    n.Shape = new Shape(shape);

                    result.Data[index] = n;
                    index++;
                }

                return(result);
            }
        }
        public NDArrayGeneric <T> transpose()
        {
            var np = new NDArrayGeneric <T>();

            np.Data = new T[this.Data.Length];

            if (NDim == 1)
            {
                np.Shape = new Shape(1, Shape.Shapes[0]);
            }
            else
            {
                np.Shape = new Shape(this.Shape.Shapes.Reverse().ToArray());
                for (int idx = 0; idx < np.shape.Shapes[0]; idx++)
                {
                    for (int jdx = 0; jdx < np.shape.Shapes[1]; jdx++)
                    {
                        np[idx, jdx] = this[jdx, idx];
                    }
                }
            }

            return(np);
        }
Beispiel #20
0
 public NDArrayGeneric <double> amin(NDArrayGeneric <double> np, int?axis = null)
 {
     return(np.AMin(axis));
 }
Beispiel #21
0
 /// <summary>
 /// Overload
 /// </summary>
 /// <param name="select"></param>
 /// <returns></returns>
 public NDArrayGeneric <T> this[NDArrayGeneric <int> select] => this[select.Data.ToList()];
Beispiel #22
0
 public NDArrayGeneric <double> max(NDArrayGeneric <double> nd)
 {
     return(nd.Max());
 }
Beispiel #23
0
 public NDArrayGeneric <double> power(NDArrayGeneric <double> nd, double exponent)
 {
     return(nd.power(exponent));
 }
Beispiel #24
0
 public NDArrayGeneric <double> absolute(NDArrayGeneric <double> np)
 {
     return(np.Absolute());
 }
Beispiel #25
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="np1"></param>
        /// <param name="np2"></param>
        /// <typeparam name="TData"></typeparam>
        /// <returns></returns>
        public NDArrayGeneric <T> dot(NDArrayGeneric <T> np2)
        {
            if ((this.Shape.Length == 1) & (np2.Shape.Length == 1))
            {
                if (this.Shape.Shapes[0] != np2.Shape.Shapes[0])
                {
                    throw new Exception("The Dot method does not work with this shape or was not already implemented.");
                }
                else
                {
                    np2.Shape  = new Shape(np2.Data.Length, 1);
                    this.Shape = new Shape(1, this.Data.Length);
                }
            }
            else
            if (this.Shape.Shapes[1] != np2.Shape.Shapes[0])
            {
                throw new Exception("The Dot method does not work with this shape or was not already implemented.");
            }

            int iterator = this.Shape.Shapes[1];
            int dim0     = this.Shape.Shapes[0];
            int dim1     = np2.Shape.Shapes[1];

            NDArrayGeneric <T> prod = new NDArrayGeneric <T>();

            prod.Shape = new Shape(dim0, dim1);
            prod.Data  = new T[prod.Shape.Size];

            switch (this.Data)
            {
            case double[] np1Array:
            {
                double[] result   = prod.Data as double[];
                double[] np2Array = np2.Data as double[];

                for (int idx = 0; idx < prod.Data.Length; idx++)
                {
                    int puffer1 = idx / dim1;
                    int puffer2 = idx % dim1;
                    int puffer3 = puffer1 * iterator;
                    for (int kdx = 0; kdx < iterator; kdx++)
                    {
                        result[idx] += np1Array[puffer3 + kdx] * np2Array[dim1 * kdx + puffer2];
                    }
                }
                break;
            }

            case float[] np1Array:
            {
                float[] result   = prod.Data as float[];
                float[] np2Array = np2.Data as float[];

                for (int idx = 0; idx < prod.Data.Length; idx++)
                {
                    int puffer1 = idx / dim1;
                    int puffer2 = idx % dim1;
                    int puffer3 = puffer1 * iterator;
                    for (int kdx = 0; kdx < iterator; kdx++)
                    {
                        result[idx] += np1Array[puffer3 + kdx] * np2Array[dim1 * kdx + puffer2];
                    }
                }

                break;
            }

            case Complex[] np1Array:
            {
                Complex[] result   = prod.Data as Complex[];
                Complex[] np2Array = np2.Data as Complex[];

                for (int idx = 0; idx < prod.Data.Length; idx++)
                {
                    int puffer1 = idx / dim1;
                    int puffer2 = idx % dim1;
                    int puffer3 = puffer1 * iterator;
                    for (int kdx = 0; kdx < iterator; kdx++)
                    {
                        result[idx] += np1Array[puffer3 + kdx] * np2Array[dim1 * kdx + puffer2];
                    }
                }
                break;
            }

            case Quaternion[] np1Array:
            {
                Quaternion[] result   = prod.Data as Quaternion[];
                Quaternion[] np2Array = np2.Data as Quaternion[];

                for (int idx = 0; idx < prod.Data.Length; idx++)
                {
                    int puffer1 = idx / dim1;
                    int puffer2 = idx % dim1;
                    int puffer3 = puffer1 * iterator;
                    for (int kdx = 0; kdx < iterator; kdx++)
                    {
                        result[idx] += np1Array[puffer3 + kdx] * np2Array[dim1 * kdx + puffer2];
                    }
                }
                break;
            }

            default:
            {
                throw new Exception("The Dot method is not implemented for the " + typeof(T).Name);
            }
            }
            if ((this.Shape.Length == 1) & (np2.Shape.Length == 1))
            {
                this.Shape = new Shape(this.Data.Length);
                np2.Shape  = new Shape(np2.Data.Length);
                prod.Shape = new Shape(1);
            }

            return(prod);
        }
Beispiel #26
0
        public NDArrayGeneric <double> hstack(params NDArrayGeneric <double>[] nps)
        {
            var n = new NDArrayGeneric <double>();

            return(n.HStack(nps));
        }
Beispiel #27
0
        public NDArrayGeneric <int> reshape(NDArrayGeneric <int> np, params int[] shape)
        {
            np.Shape = new Shape(shape);

            return(np);
        }
Beispiel #28
0
        public static NDArrayGeneric <T> operator +(NDArrayGeneric <T> np1, T scalar)
        {
            NDArrayGeneric <T> sum = new NDArrayGeneric <T>();

            sum.Shape = np1.Shape;
            sum.Data  = new T[np1.Data.Length];

            switch (scalar)
            {
            case double scalarDouble:
            {
                double[] np1Array = np1.Data as double[];
                double[] sumArray = sum.Data as double[];
                // for is faster than linq
                for (int idx = 0; idx < sumArray.Length; idx++)
                {
                    sumArray[idx] = np1Array[idx] + scalarDouble;
                }
                break;
            }

            case float scalarFloat:
            {
                float[] np1Array = np1.Data as float[];
                float[] sumArray = sum.Data as float[];
                // for is faster than linq
                for (int idx = 0; idx < sumArray.Length; idx++)
                {
                    sumArray[idx] = np1Array[idx] + scalarFloat;
                }
                break;
            }

            case Complex scalarComplex:
            {
                Complex[] np1Array = np1.Data as Complex[];
                Complex[] sumArray = sum.Data as Complex[];
                // for is faster than linq
                for (int idx = 0; idx < sumArray.Length; idx++)
                {
                    sumArray[idx] = np1Array[idx] + scalarComplex;
                }
                break;
            }

            case Quaternion scalarQuaternion:
            {
                Quaternion[] np1Array = np1.Data as Quaternion[];
                Quaternion[] sumArray = sum.Data as Quaternion[];
                // for is faster than linq
                for (int idx = 0; idx < sumArray.Length; idx++)
                {
                    sumArray[idx] = np1Array[idx] + scalarQuaternion;
                }
                break;
            }

            default:
            {
                throw new Exception("The operation is not implemented for the " + typeof(T).Name);
            }
            }

            return((NDArrayGeneric <T>)sum);
        }
Beispiel #29
0
 public static NDArrayGeneric <T> ones_like <T>(this NumPyGeneric <T> np, NDArrayGeneric <T> nd, string order = "C")
 {
     return(np.ones(new Shape(nd.Shape.Shapes)));
 }