コード例 #1
0
ファイル: MatrixMxNTest.cs プロジェクト: belzecue/Common
        public void Multiply2x3by3x2()
        {
            double[,] matrix1 = new double[, ]
            {
                { 1, 2, 3 },
                { 4, 5, 6 }
            };

            double[,] matrix2 = new double[, ]
            {
                { 7, 8 },
                { 9, 10 },
                { 11, 12 }
            };

            double[,] multiplied = MatrixMxN.MultiplyMatrix(matrix1, matrix2);

            Assert.AreEqual(2, multiplied.GetLength(0));
            Assert.AreEqual(2, multiplied.GetLength(1));

            double[,] expected = new double[, ]
            {
                { 58, 64 },
                { 139, 154 }
            };

            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    Assert.AreEqual(expected[i, j], Math.Round(multiplied[i, j], 5));
                }
            }
        }
コード例 #2
0
ファイル: MatrixMxNTest.cs プロジェクト: belzecue/Common
        public void Multiply2x2by2x2()
        {
            double[,] matrix1 = new double[, ]
            {
                { 1, 2 },
                { 4, 5 }
            };

            double[,] matrix2 = new double[, ]
            {
                { 7, 8 },
                { 9, 10 }
            };

            double[,] multiplied = MatrixMxN.MultiplyMatrix(matrix1, matrix2);

            Assert.AreEqual(2, multiplied.GetLength(0));
            Assert.AreEqual(2, multiplied.GetLength(1));

            double[,] expected = new double[, ]
            {
                { 25, 28 },
                { 73, 82 }
            };

            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    Assert.AreEqual(expected[i, j], Math.Round(multiplied[i, j], 5));
                }
            }
        }
コード例 #3
0
ファイル: MatrixMxNTest.cs プロジェクト: belzecue/Common
        public void MultiplySingle1x3by3x4()
        {
            double[] matrix1 = new double[]
            {
                3, 4, 2
            };

            double[,] matrix2 = new double[, ]
            {
                { 13, 9, 7, 15 },
                { 8, 7, 4, 6 },
                { 6, 4, 0, 3 }
            };

            double[] multiplied = MatrixMxN.MultiplyMatrix(matrix1, matrix2);

            Assert.AreEqual(4, multiplied.Length);

            double[] expected = new double[]
            {
                83, 63, 37, 75
            };

            for (int i = 0; i < 4; i++)
            {
                Assert.AreEqual(expected[i], Math.Round(multiplied[i], 5));
            }
        }
コード例 #4
0
ファイル: MatrixMxNTest.cs プロジェクト: belzecue/Common
        public void Multiply1x3by3x4()
        {
            double[,] matrix1 = new double[, ]
            {
                { 3, 4, 2 }
            };

            double[,] matrix2 = new double[, ]
            {
                { 13, 9, 7, 15 },
                { 8, 7, 4, 6 },
                { 6, 4, 0, 3 }
            };

            double[,] multiplied = MatrixMxN.MultiplyMatrix(matrix1, matrix2);

            Assert.AreEqual(1, multiplied.GetLength(0));
            Assert.AreEqual(4, multiplied.GetLength(1));

            double[,] expected = new double[, ]
            {
                { 83, 63, 37, 75 }
            };

            for (int i = 0; i < 1; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    Assert.AreEqual(expected[i, j], Math.Round(multiplied[i, j], 5));
                }
            }
        }
コード例 #5
0
ファイル: Statistics.cs プロジェクト: belzecue/Common
        public static double[,] CovarianceToCorrelation(double[,] covariance)
        {
            int dimensions = covariance.GetLength(0);

            double[,] correlation = new double[dimensions, dimensions];

            for (int j = 0; j < dimensions; j++)
            {
                for (int i = 0; i < dimensions; i++)
                {
                    if (i != j)
                    {
                        continue;
                    }
                    correlation[i, j] = Math.Sqrt(covariance[i, j]);
                }
            }

            double[,] inverse = MatrixMxN.Inverse(correlation);

            return(MatrixMxN.MultiplyMatrix(MatrixMxN.MultiplyMatrix(inverse, covariance), inverse));
        }
コード例 #6
0
        public override double MahalanobisDistance(double[] x)
        {
            if (x.Length != Dimension)
            {
                throw new ArgumentException("X must be the same size as distributions dimension.");
            }

            double[] xm = new double[Dimension];
            for (int i = 0; i < Dimension; i++)
            {
                xm[i] = x[i] - Mean[i];
            }

            double[] xmTC = MatrixMxN.MultiplyMatrix(xm, Inverse);

            double dp = 0;

            for (int i = 0; i < Dimension; i++)
            {
                dp += xmTC[i] * xm[i];
            }

            return(Math.Sqrt(dp));
        }