Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        public static NDArrayGeneric <double> Eye(this NDArrayGeneric <double> np, int dim, int diagonalIndex = 0)
        {
            int noOfDiagElement = dim - Math.Abs(diagonalIndex);

            if ((np.NDim == 1) && (dim != 1))
            {
                np.Zeros(dim, dim);
            }
            else
            {
                np.Zeros(np.Shape.Shapes.ToArray());
            }

            np.Data = new double[np.Size];

            if (diagonalIndex >= 0)
            {
                for (int idx = 0; idx < noOfDiagElement; idx++)
                {
                    np.Data[diagonalIndex + idx + idx * np.Shape.Shapes[1]] = 1;
                }
            }
            else
            {
                for (int idx = 0; idx < noOfDiagElement; idx++)
                {
                    np.Data[(-1) * diagonalIndex * dim + idx + idx * np.Shape.Shapes[1]] = 1;
                }
            }

            return(np);
        }
Ejemplo n.º 3
0
        public void MatrixMultiplyComplex()
        {
            NDArrayGeneric <Complex> matrix1 = new NDArrayGeneric <Complex>();

            matrix1.Data  = new Complex[] { new Complex(1, -1), new Complex(2, -2), new Complex(3, 0), new Complex(4, 0), 5, 6 };
            matrix1.Shape = new Shape(new int[] { 3, 2 });

            NDArrayGeneric <Complex> matrix2 = new NDArrayGeneric <Complex>();

            matrix2.Data  = new Complex[] { 7, 8, 9, new Complex(10, -10), 11, new Complex(12, -12) };
            matrix2.Shape = new Shape(new int[] { 2, 3 });

            var matrix3 = matrix1.dot(matrix2);

            var matrix4 = new Complex[9];

            matrix4[0] = new Complex(7, -47);
            matrix4[1] = new Complex(30, -30);
            matrix4[2] = new Complex(9, -57);
            matrix4[3] = new Complex(61, -40);
            matrix4[4] = new Complex(68, 0);
            matrix4[5] = new Complex(75, -48);
            matrix4[6] = new Complex(95, -60);
            matrix4[7] = new Complex(106, 0);
            matrix4[8] = new Complex(117, -72);

            Assert.IsTrue(Enumerable.SequenceEqual(matrix4, matrix3.Data));
        }
Ejemplo n.º 4
0
        public void PerformaceBitmapSimulation()
        {
            var npRealWorldBitmap = new NDArrayGeneric <byte>();

            //npRealWorldBitmap.ARange(2081 * 2531);
            npRealWorldBitmap.reshape(2531, 2081);
        }
Ejemplo n.º 5
0
        public static NDArrayGeneric <double> Max(this NDArrayGeneric <double> np)
        {
            if (np.NDim == 1)
            {
                var max = new NDArrayGeneric <double>().Zeros(np.Size);
                max.Shape = new Shape(1);
                max.Data  = new double[] { np.Data.Max() };

                return(max);
            }
            else if (np.NDim == 2)
            {
                var max = new NDArrayGeneric <double>().Zeros(np.Shape.Shapes[1]);

                for (int col = 0; col < np.Shape.Shapes[1]; col++)
                {
                    max[col] = np[0, col];
                    for (int row = 0; row < np.Shape.Shapes[0]; row++)
                    {
                        if (np[row, col] > max[col])
                        {
                            max[col] = np[row, col];
                        }
                    }
                }

                return(max);
            }
            else
            {
                throw new NotImplementedException();
            }
        }
Ejemplo n.º 6
0
        public void amin0axis()
        {
            var nd2 = new NDArrayGeneric <double>();

            nd2 = np.arange(1000 * 8 * 8 * 8).reshape(1000, 8, 8, 8);
            //var nd3 = nd2.AMin(0);
        }
Ejemplo n.º 7
0
        public void Simple3x3()
        {
            NDArrayGeneric <double> np1 = new NDArrayGeneric <double>().Zeros(3, 3);

            np1[0, 0] = 5;
            np1[0, 1] = 1;
            np1[0, 2] = 2;
            np1[1, 0] = 1;
            np1[1, 1] = 0;
            np1[1, 2] = 1;
            np1[2, 0] = 1;
            np1[2, 1] = 1;
            np1[2, 2] = 0;

            NDArrayGeneric <double> np1Inv = np1.inv();

            var OncesMatrix = np1.dot(np1Inv);

            Assert.IsTrue(Math.Abs(OncesMatrix[0, 0]) < 1.000001);
            Assert.IsTrue(Math.Abs(OncesMatrix[1, 1]) < 1.000001);
            Assert.IsTrue(Math.Abs(OncesMatrix[2, 2]) < 1.000001);

            Assert.IsTrue(Math.Abs(OncesMatrix[0, 1]) < 0.000001);
            Assert.IsTrue(Math.Abs(OncesMatrix[0, 2]) < 0.000001);
            Assert.IsTrue(Math.Abs(OncesMatrix[1, 0]) < 0.000001);
            Assert.IsTrue(Math.Abs(OncesMatrix[1, 2]) < 0.000001);
            Assert.IsTrue(Math.Abs(OncesMatrix[2, 0]) < 0.000001);
            Assert.IsTrue(Math.Abs(OncesMatrix[2, 1]) < 0.000001);
        }
Ejemplo n.º 8
0
        public void Setup()
        {
            // first array
            np1       = new NDArrayGeneric <double>();
            np1.Data  = new double[100000].Select((x, idx) => x + idx).ToArray();
            np1.Shape = new Shape(np1.Data.Length);

            np1Double = np1.Data;

            // second array
            np2       = new NDArrayGeneric <double>();
            np2.Data  = new double[100000].Select((x, idx) => x + idx).ToArray();
            np2.Shape = new Shape(np2.Data.Length);

            np2Double = np2.Data;

            // result array
            np3      = new NDArrayGeneric <double>();
            np3.Data = new double[100000];

            np3Double = np3.Data;

            // result array
            np3      = new NDArrayGeneric <double>();
            np3.Data = new double[100000];

            np3Double = np3.Data;
        }
Ejemplo n.º 9
0
        public static NDArrayGeneric <T> delete <T>(this NumPyGeneric <T> np, NDArrayGeneric <T> nd, IEnumerable <T> delete)
        {
            var nd1 = np.array(nd.Data.Where(x => !delete.Contains(x)));

            nd1.Shape = new Shape(nd1.Data.Length);

            return(nd1);
        }
Ejemplo n.º 10
0
        public void Double1DPlusOffset_NDArrayAddition()
        {
            var np1 = new NDArrayGeneric <double>().arange(4, 1);

            var np3 = np1 + 2;

            Assert.IsTrue(Enumerable.SequenceEqual(new double[] { 3, 4, 5 }, np3.Data));
        }
Ejemplo n.º 11
0
        public void StdTest()
        {
            var np = new NDArrayGeneric <double>().arange(4).reshape(2, 2);

            // Assert.IsTrue(Enumerable.SequenceEqual(np.Std().Data, new double[] { 1.1180339887498949 }));
            // Assert.IsTrue(Enumerable.SequenceEqual(np.Std(0).Data, new double[] { 1, 1 }));
            // Assert.IsTrue(Enumerable.SequenceEqual(np.Std(1).Data, new double[] { 0.5, 3.5 }));
        }
Ejemplo n.º 12
0
        public void ToDotNetArray1D()
        {
            var np1 = new NDArrayGeneric <double>().arange(9);

            double[] np1_ = np1.ToDotNetArray <double[]>();

            Assert.IsTrue(Enumerable.SequenceEqual(np1_, np1.Data));
        }
Ejemplo n.º 13
0
        public void DoubleTwo1D_NDArrayMultiplication()
        {
            var np1 = new NDArrayGeneric <double>().arange(4, 1, 1).reshape(1, 3);
            var np2 = new NDArrayGeneric <double>().arange(5, 2, 1).reshape(1, 3);

            var np3 = np1 * np2;

            Assert.IsTrue(Enumerable.SequenceEqual(new double[] { 2, 6, 12 }, np3.Data));
        }
Ejemplo n.º 14
0
        public static NDArrayGeneric <Complex> Sqrt(this NDArrayGeneric <Complex> np)
        {
            for (int i = 0; i < np.Data.Length; i++)
            {
                np[i] = Complex.Sqrt(np[i]);
            }

            return(np);
        }
Ejemplo n.º 15
0
        public static NDArrayGeneric <T> Unique <T>(this NDArrayGeneric <T> np)
        {
            var np2 = new NDArrayGeneric <T>();

            np2.Data  = np.Data.Distinct().ToArray();
            np2.Shape = new Shape(new int[] { np2.Data.Length });

            return(np2);
        }
Ejemplo n.º 16
0
        public static NDArrayGeneric <double> Sqrt(this NDArrayGeneric <double> np)
        {
            for (int i = 0; i < np.Data.Length; i++)
            {
                np[i] = Math.Sqrt(np[i]);
            }

            return(np);
        }
Ejemplo n.º 17
0
        public void DoubleTwo1D_NDArrayAddition()
        {
            var np1 = new NDArrayGeneric <double>().arange(4, 1);
            var np2 = new NDArrayGeneric <double>().arange(5, 2);

            var np3 = np1 + np2;

            Assert.IsTrue(Enumerable.SequenceEqual(new double[] { 3, 5, 7 }, np3.Data));
        }
Ejemplo n.º 18
0
        public void DoubleSqrtTest()
        {
            var np = new NDArrayGeneric <double>().arange(3);

            np[0] = 1;
            np[1] = 4;
            np[2] = 9;
            Assert.IsTrue(Enumerable.SequenceEqual(np.Sqrt().Data, new double[] { 1, 2, 3 }));
        }
Ejemplo n.º 19
0
        public void Dump()
        {
            var np = new NDArrayGeneric <double>();

            np.Data  = new double[] { 1, 2, 3, 4, 5 };
            np.Shape = new Shape(np.Data.Length);

            Assert.IsTrue(np.NDim == 1);
        }
Ejemplo n.º 20
0
        public void FromNumpyDocs()
        {
            var np1 = new NDArrayGeneric <double>().linspace(2.0, 3.0, 5);

            Assert.IsTrue(Enumerable.SequenceEqual(np1.Data, new double[] { 2.0, 2.25, 2.5, 2.75, 3.0 }));

            var np2 = new NDArrayGeneric <double>().linspace(2.0, 3.0, 5, false);

            Assert.IsTrue(Enumerable.SequenceEqual(np2.Data, new double[] { 2.0, 2.20, 2.4, 2.6, 2.8 }));
        }
Ejemplo n.º 21
0
        public void Complex1DPlusOffset_NDArrayMultiplication()
        {
            var np1 = new NDArrayGeneric <Complex>();

            np1.Data = new Complex[] { new Complex(1, 2), new Complex(3, 4) };

            var np2 = np1 * new Complex(1, 2);

            Assert.IsTrue(Enumerable.SequenceEqual(new Complex[] { new Complex(-3, 4), new Complex(-5, 10) }, np2.Data));
        }
Ejemplo n.º 22
0
        public void MatrixMutliplyDifferentDataLength()
        {
            var A = new NDArrayGeneric <double>().arange(6).reshape(3, 2);

            var B = new NDArrayGeneric <double>().arange(14).reshape(2, 7);

            var C = A.dot(B);

            Assert.IsTrue(Enumerable.SequenceEqual(new double[] { 7, 8, 9, 10, 11, 12, 13, 21, 26, 31, 36, 41, 46, 51, 35, 44, 53, 62, 71, 80, 89 }, C.Data));
        }
Ejemplo n.º 23
0
        public void Setup()
        {
            np     = new NumPyGeneric <int>();
            nd     = new NDArrayGeneric <int>();
            start  = 0;
            step   = 1;
            length = 100 * 100;

            np2 = new NumPy();
        }
Ejemplo n.º 24
0
        public void DoubleMatrix2DiagonalRight()
        {
            var np = new NDArrayGeneric <double>().Eye(5, 2);

            Assert.IsTrue(np[0, 2] == 1);
            Assert.IsTrue(np[1, 3] == 1);
            Assert.IsTrue(np[2, 4] == 1);

            Assert.IsTrue(np.Data.Where(x => x == 0).ToArray().Length == 22);
        }
Ejemplo n.º 25
0
        public void DoubleMatrix2DiagonalLeft()
        {
            var np = new NDArrayGeneric <double>().Eye(5, -2);

            Assert.IsTrue(np[2, 0] == 1);
            Assert.IsTrue(np[3, 1] == 1);
            Assert.IsTrue(np[4, 2] == 1);

            Assert.IsTrue(np.Data.Where(x => x == 0).ToArray().Length == 22);
        }
Ejemplo n.º 26
0
        public void Double1DPlusOffset_NDArrayMultiplication()
        {
            var np1 = new NDArrayGeneric <double>();

            np1.Data = new double[] { 1, 2, 3 };

            var np3 = np1 * 2;

            Assert.IsTrue(Enumerable.SequenceEqual(new double[] { 2, 4, 6 }, np3.Data));
        }
Ejemplo n.º 27
0
        /// <summary>
        /// Stack arrays in sequence horizontally
        /// </summary>
        /// <param name="nps"></param>
        /// <returns></returns>
        public static NDArrayGeneric <T> HStack <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");
                }
            }
            int total    = nps[0].NDim == 1 ? 1 : nps[0].Shape.Shapes[0];
            int pageSize = nps[0].NDim == 1 ? nps[0].Shape.Shapes[0] : nps[0].Shape.DimOffset[0];

            //int i = 0;
            //Enumerable.Range(0, total)
            //                    .Select(x =>
            //                    {
            //                        foreach (NDArrayGeneric<T> ele in nps)
            //                        {
            //                            for (int j = i * pageSize; j < (i + 1) * pageSize; j++)
            //                                list.Add(ele.Data[j]);
            //                        }
            //                        return i++;
            //                    })
            //                    .ToArray();

            for (int i = 0; i < total; i++)
            {
                for (int k = 0; k < nps.Length; k++)
                {
                    for (int j = i * pageSize; j < (i + 1) * pageSize; j++)
                    {
                        list.Add(nps[k].Data[j]);
                    }
                }
            }
            np.Data = list.ToArray();
            int[] shapes = nps[0].Shape.Shapes.ToArray();
            if (shapes.Length == 1)
            {
                shapes[0] *= nps.Length;
            }
            else
            {
                shapes[1] *= nps.Length;
            }
            np.Shape = new Shape(shapes);
            return(np);
        }
Ejemplo n.º 28
0
        public void Complex1DPlusOffset_NDArrayAddition()
        {
            var np1 = new NDArrayGeneric <Complex>();

            np1.Data  = new Complex[] { new Complex(1, 2), new Complex(3, 4) };
            np1.Shape = new Shape(new int[] { 2 });

            var np2 = np1 + new Complex(1, 2);

            Assert.IsTrue(Enumerable.SequenceEqual(new Complex[] { new Complex(2, 4), new Complex(4, 6) }, np2.Data));
        }
Ejemplo n.º 29
0
        public void Setup()
        {
            // first array
            np1Matrix = new NDArrayGeneric <double>().arange(100 * 100 + 2, 2).reshape(100, 100);

            np1DoubleMatrix = np1Matrix.ToDotNetArray <double[][]>();

            // second array
            np2Matrix = new NDArrayGeneric <double>().arange(100 * 100 + 1, 1).reshape(100, 100);;

            np2DoubleMatrix = np2Matrix.ToDotNetArray <double[][]>();
        }
Ejemplo n.º 30
0
        public void DimOrder()
        {
            NDArrayGeneric <double> np1 = new NDArrayGeneric <double>().Zeros(2, 2);

            np1[0, 0] = 0;
            np1[1, 0] = 10;
            np1[0, 1] = 1;
            np1[1, 1] = 11;

            // columns first than rows
            Assert.IsTrue(Enumerable.SequenceEqual(new double[] { 0, 1, 10, 11 }, np1.Data));
        }