Esempio n. 1
0
        public void ExceptionIncompatibleMatrices2()
        {
            IMathematicalMatrix matrix1 = new Matrix(2, 3);
            matrix1[0, 0] = 1;
            matrix1[0, 1] = 2;
            matrix1[0, 2] = -4;
            matrix1[1, 0] = 0;
            matrix1[1, 1] = 3;
            matrix1[1, 2] = -1;

            IMathematicalMatrix matrix2 = MatrixTest.GetTestMatrix();

            matrix1.Subtract(matrix2);
        }
Esempio n. 2
0
        public void SubtractRaiseExceptionIfDifferentRowSize()
        {
            Matrix matrix1 = new Matrix(new double[][] { new double[] { 1.0, 2.0 }, new double[] { 3.0, 4.0 } });
            Matrix matrix2 = new Matrix(new double[][] { new double[] { 5.0, 6.0 }, new double[] { 7.0, 8.0 }, new double[] { 9.0, 10.0 } });

            try
            {
                matrix1.Subtract(matrix2);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(InvalidOperationException));
                Assert.AreEqual("Matrices have different sizes", ex.Message);
            }
        }
Esempio n. 3
0
        public void Subtract()
        {
            Matrix matrix1 = new Matrix(new double[][] { new double[] { 1.0, 2.0 }, new double[] { 3.0, 4.0 } });
            Matrix matrix2 = new Matrix(new double[][] { new double[] { 5.0, 6.0 }, new double[] { 7.0, 8.0 } });

            Matrix matrix = matrix1.Subtract(matrix2);

            Assert.AreEqual(4, matrix.Size);

            var elements = matrix.Elements;

            Assert.AreEqual(1.0 - 5.0, elements[0][0]);
            Assert.AreEqual(2.0 - 6.0, elements[0][1]);
            Assert.AreEqual(3.0 - 7.0, elements[1][0]);
            Assert.AreEqual(4.0 - 8.0, elements[1][1]);
        }
        public Matrix KLTransform(Matrix matrix)
        {
            matrix = (Matrix)matrix.Transpose();

            int columnNumber = matrix.ColumnCount;

            Matrix mean = GetMean(matrix);

            //Don't print it becouse is a lot of values
          //  PrintMatrix(mean, null, " mean matrix"); - 

            float[,] oneD = new float[1, columnNumber];
            for (int i = 0; i < columnNumber; i++)
            {
                oneD[0, i] = 1;
            }

            //so called edinichna matrica
            Matrix onesMatrix = DenseMatrix.OfArray(oneD);

            // center the data
            Matrix xm = (DenseMatrix)matrix.Subtract(mean.Multiply(onesMatrix));
           // PrintMatrix(null, xm.ToArray(), "center the data");

            // Calculate covariance matrix

            DenseMatrix cov = (DenseMatrix)(xm.Multiply(xm.Transpose())).Multiply(1.0f / columnNumber);
          //  PrintMatrix(cov, null, "Calculate covariance matrix");
            PrintText(cov.ColumnCount.ToString() + " Calculate covariance  ColumnCount");
            PrintText(cov.RowCount.ToString() + " Calculate covariance  RowCount");

            //this is from another libraly accord .net 
            EigenvalueDecomposition v = new EigenvalueDecomposition(FromFloatMatrixToDouble(cov.ToArray()));

            double[,] eigVectors = v.Eigenvectors;
            double[,] eidDiagonal = v.DiagonalMatrix;

            Matrix eigVectorsTransposedMatrix = (DenseMatrix)DenseMatrix.OfArray(FromDoubleMatrixToFlaot(eigVectors)).Transpose();
            Matrix eidDiagonalMatrix = DenseMatrix.OfArray(FromDoubleMatrixToFlaot(eidDiagonal));

            int rowsCountDiagonals = eidDiagonal.GetLength(0);
            int maxLambdaIndex = 0;
            for (int i = 0; i < rowsCountDiagonals - 1; i++)
            {
                if (eidDiagonal[i, i] <= eidDiagonal[i + 1, i + 1])
                {
                    maxLambdaIndex = i + 1;
                }
            }

            double maxAlpha = eidDiagonal[maxLambdaIndex, maxLambdaIndex];

            double sumAlpha = 0;
            for (int i = 0; i < rowsCountDiagonals; i++)
            {
                sumAlpha += eidDiagonal[i, i];
            }

            CalculateAndPrintError(maxAlpha, sumAlpha);
           PrintText("Max lambda index " + maxLambdaIndex);

           // PrintMatrix(eidDiagonalMatrix, null, "Eign vals");

            // //PCA

            float[,] arr = new float[1, eigVectorsTransposedMatrix.ColumnCount];
            for (int i = 0; i < eigVectorsTransposedMatrix.ColumnCount; i++)
            {
                arr[0, i] = eigVectorsTransposedMatrix[maxLambdaIndex, i];
            }

            Matrix mainComponentMatrix = DenseMatrix.OfArray(arr);
          //  PrintMatrix(mainComponentMatrix, null, "Main Component");


            Matrix pca = (DenseMatrix)mainComponentMatrix.Multiply(xm);
          //  PrintMatrix(pca, null, "PCA");

            return pca;

        }