Exemple #1
0
        public static T GetKahanSum <T>(this Matrix <T> matrix, int dimension, State state = State.Row)
            where T : unmanaged
        {
            T sum   = default;
            T error = default;

            if (state == State.Row)
            {
                for (int i = 0; i < matrix.Columns; i++)
                {
                    T y = MathUnsafe <T> .Sub(matrix[dimension, i], error);

                    T t = MathUnsafe <T> .Add(sum, y);

                    error = MathUnsafe <T> .Sub(MathUnsafe <T> .Sub(t, sum), matrix[dimension, i]);

                    sum = t;
                }
            }
            else
            {
                for (int i = 0; i < matrix.Rows; i++)
                {
                    T y = MathUnsafe <T> .Sub(matrix[i, dimension], error);

                    T t = MathUnsafe <T> .Add(sum, y);

                    error = MathUnsafe <T> .Sub(MathUnsafe <T> .Sub(t, sum), matrix[i, dimension]);

                    sum = t;
                }
            }

            return(sum);
        }
Exemple #2
0
        /// <summary>
        /// Gets m-norm.
        /// </summary>
        /// <param name="matrix">the matrix.</param>
        /// <typeparam name="T">unmanaged type.</typeparam>
        /// <returns>m-norm.</returns>
        /// <exception cref="NullReferenceException"></exception>
        public static T MNorm <T>(this Matrix <T> matrix) where T : unmanaged
        {
            int rows    = matrix.Rows;
            int columns = matrix.Columns;

            var array = new T[rows];

            for (int i = 0; i < rows; i++)
            {
                T sum = default;

                for (int j = 0; j < columns; j++)
                {
                    sum = MathUnsafe <T> .Add(sum, MathGeneric <T> .Abs(matrix[i, j]));
                }
                array[i] = sum;
            }

            Comparer <T> comparer = Comparer <T> .Default;
            T            max      = array[0];

            for (int i = 0; i < rows; i++)
            {
                T reg = array[i];
                if (comparer.Compare(max, array[i]) < 0)
                {
                    max = reg;
                }
            }

            return(max);
        }
Exemple #3
0
        internal static T SumVector(Vector128 <T> a)
        {
            var sum = default(T);

            for (var i = 0; i < Vector128 <T> .Count; i++)
            {
                sum = MathUnsafe <T> .Add(sum, a.GetElement(i));
            }
            return(sum);
        }
        public static T Sum <T>(this Vector256 <T> a)
            where T : unmanaged
        {
            var sum = default(T);

            for (var i = 0; i < Vector256 <T> .Count; i++)
            {
                sum = MathUnsafe <T> .Add(sum, a.GetElement(i));
            }
            return(sum);
        }
Exemple #5
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 #6
0
        /// <summary>
        /// Gets sum by column of matrix.
        /// </summary>
        /// <param name="matrix">the matrix.</param>
        /// <param name="dimension">column index.</param>
        /// <typeparam name="T">unmanaged type.</typeparam>
        /// <returns>Sum column by index</returns>
        /// <exception cref="NullReferenceException"></exception>
        public static T SumByColumn <T>(this Matrix <T> matrix, int dimension)
            where T : unmanaged
        {
            T sum = default;

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

            return(sum);
        }
Exemple #7
0
        /// <summary>
        /// Gets sum by diagonal.
        /// </summary>
        /// <param name="matrix"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        /// <exception cref="NullReferenceException"></exception>
        public static T SumByDiagonal <T>(this Matrix <T> matrix)
            where T : unmanaged
        {
            T sum = default;

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

            return(sum);
        }
Exemple #8
0
        public static T GetKleinSum <T>(this Matrix <T> matrix)
            where T : unmanaged
        {
            if (!MathGeneric.IsFloatingPoint <T>())
            {
                throw new MatrixDotNetException($"{typeof(T)} is not supported type must be floating type");
            }

            T sum = default;
            T cs  = default;
            T ccs = default;

            var comparer = Comparer <T> .Default;

            for (int i = 0; i < matrix.Rows; i++)
            {
                for (int j = 0; j < matrix.Columns; j++)
                {
                    T t = MathUnsafe <T> .Add(sum, matrix[i, j]);

                    T error;

                    if (comparer.Compare(MathGeneric <T> .Abs(sum), matrix[i, j]) >= 0)
                    {
                        error = MathUnsafe <T> .Add(MathUnsafe <T> .Sub(sum, t), matrix[i, j]);
                    }
                    else
                    {
                        error = MathUnsafe <T> .Add(MathUnsafe <T> .Sub(matrix[i, j], t), sum);
                    }

                    sum = t;
                    t   = MathUnsafe <T> .Add(cs, cs);

                    T error2;

                    if (comparer.Compare(MathGeneric <T> .Abs(cs), error) >= 0)
                    {
                        error2 = MathUnsafe <T> .Add(MathUnsafe <T> .Sub(error, t), cs);
                    }
                    else
                    {
                        error2 = MathUnsafe <T> .Add(MathUnsafe <T> .Sub(cs, t), error);
                    }

                    cs  = t;
                    ccs = MathUnsafe <T> .Add(ccs, error2);
                }
            }
            return(MathUnsafe <T> .Add(MathUnsafe <T> .Add(sum, cs), ccs));
        }
Exemple #9
0
        public static T NormFrobenius <T>(this Matrix <T> matrix) where T : unmanaged
        {
            T result = default;

            for (int i = 0; i < matrix.Rows; i++)
            {
                for (int j = 0; j < matrix.Columns; j++)
                {
                    result = MathUnsafe <T> .Add(result,
                                                 MathUnsafe <T> .Mul(matrix[i, j], matrix[i, j]));
                }
            }
            return(MathGeneric <T> .Sqrt(result));
        }
Exemple #10
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 #11
0
        /// <summary>
        /// The trace <c>Tr</c> of a square matrix A is defined to be the sum of elements on the main diagonal.
        /// </summary>
        /// <param name="matrix">the matrix.</param>
        /// <typeparam name="T">unmanaged type.</typeparam>
        /// <returns>Traceless of matrix.</returns>
        public static T Traceless <T>(this Matrix <T> matrix) where T : unmanaged
        {
            if (!matrix.IsSquare)
            {
                throw new MatrixNotSquareException();
            }

            T sum = default;

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

            return(sum);
        }
Exemple #12
0
        /// <summary>
        /// Gets array of sum columns.
        /// </summary>
        /// <param name="matrix">the matrix.</param>
        /// <typeparam name="T">unmanaged type.</typeparam>
        /// <returns></returns>
        /// <exception cref="NullReferenceException"></exception>
        public static T[] SumByColumns <T>(this Matrix <T> matrix)
            where T : unmanaged
        {
            var array = new T[matrix.Columns];

            for (int i = 0; i < matrix.Columns; i++)
            {
                T sum = default;

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

                array[i] = sum;
            }

            return(array);
        }
Exemple #13
0
        private static T GetKleinSumByColumns <T>(this Matrix <T> matrix, int dimension)
            where T : unmanaged
        {
            T sum = default;
            T cs  = default;
            T ccs = default;

            var comparer = Comparer <T> .Default;

            for (int j = 0; j < matrix.Rows; j++)
            {
                T t = MathUnsafe <T> .Add(sum, matrix[j, dimension]);

                T error;

                if (comparer.Compare(MathGeneric <T> .Abs(sum), matrix[j, dimension]) >= 0)
                {
                    error = MathUnsafe <T> .Add(MathUnsafe <T> .Sub(sum, t), matrix[j, dimension]);
                }
                else
                {
                    error = MathUnsafe <T> .Add(MathUnsafe <T> .Sub(matrix[j, dimension], t), sum);
                }

                sum = t;
                t   = MathUnsafe <T> .Add(cs, cs);

                T error2;

                if (comparer.Compare(MathGeneric <T> .Abs(cs), error) >= 0)
                {
                    error2 = MathUnsafe <T> .Add(MathUnsafe <T> .Sub(error, t), cs);
                }
                else
                {
                    error2 = MathUnsafe <T> .Add(MathUnsafe <T> .Sub(cs, t), error);
                }

                cs  = t;
                ccs = MathUnsafe <T> .Add(ccs, error2);
            }
            return(MathUnsafe <T> .Add(MathUnsafe <T> .Add(sum, cs), ccs));
        }
Exemple #14
0
        public static T GetKahanSum <T>(this Matrix <T> matrix)
            where T : unmanaged
        {
            T sum   = default;
            T error = default;

            for (int i = 0; i < matrix.Rows; i++)
            {
                for (int j = 0; j < matrix.Columns; j++)
                {
                    T y = MathUnsafe <T> .Sub(matrix[i, j], error);

                    T t = MathUnsafe <T> .Add(sum, y);

                    error = MathUnsafe <T> .Sub(MathUnsafe <T> .Sub(t, sum), matrix[i, j]);

                    sum = t;
                }
            }

            return(sum);
        }
Exemple #15
0
 public static Point <T> operator +(Point <T> first, Point <T> second)
 {
     return(new Point <T>(MathUnsafe.Add(first.X, second.X), MathUnsafe.Add(first.Y, second.Y)));
 }
 public static Point <T> operator +(Point <T> pos1, Point <T> pos2)
 {
     return(new Point <T>(MathUnsafe.Add(pos1.X, pos2.X), MathUnsafe.Add(pos1.Y, pos2.Y)));
 }