Exemple #1
0
        /// <summary>
        /// Gets mean value by column.
        /// </summary>
        /// <param name="matrix">the matrix.</param>
        /// <param name="index">the column index.</param>
        /// <typeparam name="T">unmanaged type.</typeparam>
        /// <returns>mean value by column.</returns>
        public static T MeanByColumn <T>(this Matrix <T> matrix, int index)
            where T : unmanaged
        {
            if (!MathGeneric.IsFloatingPoint <T>())
            {
                throw new NotSupportedTypeException(ExceptionArgument.NotSupportedTypeFloatType);
            }

            return(MathGeneric <T, int, T> .Divide(matrix.SumByColumn(index), matrix.Rows));
        }
Exemple #2
0
        /// <summary>
        /// Gets mean linear deviation.
        /// </summary>
        /// <returns>mean linear deviation.</returns>
        public T GetMeanLinearDeviation()
        {
            T sum = default;

            T[] arr = GetModulesDevMean();
            for (int i = 0; i < Matrix.Rows; i++)
            {
                sum = MathUnsafe <T> .Add(sum, arr[i]);
            }

            return(MathGeneric <T, int, T> .Divide(sum, Matrix.Rows));
        }
Exemple #3
0
        /// <summary>
        /// Gets sample dispersion of matrix.
        /// </summary>
        /// <returns></returns>
        public T GetSampleDispersion()
        {
            T mean = GetSampleMeanByTable(TableVariations.Xi);

            T[] xi  = Matrix[GetIndexColumn(TableVariations.Xi), State.Column];
            T   sum = default;

            for (int i = 0; i < Matrix.Rows; i++)
            {
                var operation = MathUnsafe <T> .Sub(xi[i], mean);

                sum = MathUnsafe <T> .Add(sum, MathUnsafe <T> .Mul(operation, operation));
            }

            return(MathGeneric <T, int, T> .Divide(sum, Matrix.Rows));
        }
Exemple #4
0
        /// <summary>
        /// Gets lower upper permutation with matrix C which calculate by formula:
        /// <c>C=L+U-E</c>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static void GetLowerUpperPermutation <T>(this Matrix <T> matrix, out Matrix <T> matrixC, out Matrix <T> matrixP) where T : unmanaged
        {
            int n = matrix.Rows;

            matrixC = matrix.Clone() as Matrix <T>;

            if (matrixC is null)
            {
                throw new NullReferenceException();
            }

            // load to P identity matrix.
            matrixP = BuildMatrix.CreateIdentityMatrix <T>(matrix.Rows, matrix.Columns);

            var comparer = Comparer <T> .Default;

            for (int i = 0; i < n; i++)
            {
                T   pivotValue = default;
                int pivot      = -1;
                for (int j = i; j < n; j++)
                {
                    if (comparer.Compare(MathGeneric <T> .Abs(matrixC[j, i]), pivotValue) > 0)
                    {
                        pivotValue = MathGeneric <T> .Abs(matrixC[j, i]);

                        pivot = j;
                    }
                }

                if (pivot != 0)
                {
                    matrixP.SwapRows(pivot, i);
                    matrixC.SwapRows(pivot, i);
                    for (int j = i + 1; j < n; j++)
                    {
                        matrixC[j, i] = MathGeneric <T> .Divide(matrixC[j, i], matrixC[i, i]);

                        for (int k = i + 1; k < n; k++)
                        {
                            matrixC[j, k] = MathUnsafe <T> .Sub(matrixC[j, k],
                                                                MathUnsafe <T> .Mul(matrixC[j, i], matrix[i, k]));
                        }
                    }
                }
            }
        }
Exemple #5
0
        /// <summary>
        /// Gets mean value by each row.
        /// </summary>
        /// <param name="matrix">the matrix.</param>
        /// <typeparam name="T">unmanaged type.</typeparam>
        /// <returns>mean value by each row.</returns>
        public static T[] MeanByColumns <T>(this Matrix <T> matrix)
            where T : unmanaged
        {
            if (!MathGeneric.IsFloatingPoint <T>())
            {
                throw new NotSupportedTypeException(ExceptionArgument.NotSupportedTypeFloatType);
            }

            var rows    = matrix.Rows;
            var columns = matrix.Columns;
            var arr     = new T[columns];

            for (int i = 0; i < columns; i++)
            {
                arr[i] = MathGeneric <T, int, T> .Divide(matrix.SumByColumn(i), rows);
            }

            return(arr);
        }
Exemple #6
0
        /// <summary>
        /// Gets mean value by each row.
        /// </summary>
        /// <param name="matrix">the matrix.</param>
        /// <typeparam name="T">unmanaged type.</typeparam>
        /// <returns>mean value by each row.</returns>
        public static T[] MeanByRows <T>(this Matrix <T> matrix)
            where T : unmanaged
        {
            if (!MathGeneric.IsFloatingPoint <T>())
            {
                throw new NotSupportedException();
            }

            var rows    = matrix.Rows;
            var columns = matrix.Columns;
            var arr     = new T[rows];

            for (int i = 0; i < rows; i++)
            {
                arr[i] = MathGeneric <T, int, T> .Divide(matrix.SumByRow(i), columns);
            }

            return(arr);
        }
Exemple #7
0
        public static Matrix <T> ProcessGrammShmidtByRows <T>(this Matrix <T> matrix) where T : unmanaged
        {
            if (!MathGeneric.IsFloatingPoint <T>())
            {
                throw new NotSupportedTypeException(ExceptionArgument.NotSupportedTypeFloatType);
            }

            if (!matrix.IsSquare)
            {
                throw new MatrixDotNetException("matrix is not square");
            }

            int m = matrix.Rows;

            Matrix <T> b = new Matrix <T>(m, matrix.Columns)
            {
                [0] = matrix[0]
            };

            for (int i = 1; i < m; i++)
            {
                Vectorization.Vector <T> ai  = matrix[i];
                Vectorization.Vector <T> sum = new T[m];
                for (int j = 0; j < i; j++)
                {
                    Vectorization.Vector <T> bi = b[j];
                    T scalarProduct             = ai * bi;
                    T biMul = bi * bi;
                    T ci    = MathGeneric <T> .Divide(scalarProduct, biMul);

                    sum += ci * bi;
                }
                var res = ai - sum;
                b[i] = res.Array;
            }

            return(b);
        }