Esempio n. 1
0
        /// <summary>
        /// Gets vector direct cosines
        /// </summary>
        /// <param name="va">vector A</param>
        /// <typeparam name="T">unmanaged type</typeparam>
        /// <returns>direct cos's</returns>
        /// <exception cref="MatrixDotNetException">
        /// throw if data type is not floating type
        /// </exception>
        public static T[] GetDirectCos <T>(Vector <T> va)
            where T : unmanaged
        {
            if (!MathGeneric.IsFloatingPoint <T>())
            {
                throw new NotSupportedException();
            }

            int length = va.Length;

            T[] cos = new T[length];
            T   mod = va.GetLengthVec();

            Array.Fill(cos, mod);

            int i              = 0;
            int size           = System.Numerics.Vector <T> .Count;
            int lastIndexBlock = length - length % size;

            for (; i < lastIndexBlock; i += size)
            {
                var vt = new System.Numerics.Vector <T>(va.Array, i);
                var vf = new System.Numerics.Vector <T>(cos);
                var vc = Vector.Divide(vt, vf);
                vc.CopyTo(cos, i);
            }

            for (; i < length; i++)
            {
                cos[i] = MathUnsafe <T> .Div(va[i], cos[i]);
            }

            return(cos);
        }
Esempio n. 2
0
        public static T GetKleinSum <T>(this Matrix <T> matrix, int dimension, State state = State.Row)
            where T : unmanaged
        {
            if (!MathGeneric.IsFloatingPoint <T>())
            {
                throw new MatrixDotNetException($"{typeof(T)} is not supported type must be floating type");
            }

            return(state == State.Row ? GetKleinSumByRows(matrix, dimension) : GetKleinSumByColumns(matrix, dimension));
        }
Esempio n. 3
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));
        }
Esempio n. 4
0
        /// <summary>
        /// Initialize
        /// </summary>
        /// <param name="matrix">the matrix.</param>
        /// <exception cref="ArgumentException"></exception>
        protected Setup(Matrix <T> matrix)
        {
            if (!MathGeneric.IsFloatingPoint <T>())
            {
                throw new ArgumentException("Matrix must be floating type.");
            }

            ColumnNames  = new string[matrix.Columns];
            ColumnNumber = new int[matrix.Columns];

            Matrix = matrix;
        }
Esempio n. 5
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));
        }
Esempio n. 6
0
        public static Matrix <T> ProcessGrammShmidtByColumns <T>(this Matrix <T> matrix) where T : unmanaged
        {
            if (!MathGeneric.IsFloatingPoint <T>())
            {
                throw new NotSupportedTypeException(ExceptionArgument.NotSupportedTypeFloatType);
            }

            if (!matrix.IsSquare)
            {
                throw new MatrixNotSquareException();
            }

            int m = matrix.Rows;
            int n = matrix.Columns;

            Matrix <T> b = new Matrix <T>(m, n)
            {
Esempio n. 7
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);
        }
Esempio n. 8
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);
        }
Esempio n. 9
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);
        }