Esempio n. 1
0
        /// <summary>
        /// Gets determinant matrix by Kramer algorithm.
        /// </summary>
        /// <param name="matrix">matrix.</param>
        /// <param name="arr">array.</param>
        /// <typeparam name="T">unmanaged type.</typeparam>
        /// <returns>Gets array x.</returns>
        /// <exception cref="MatrixDotNetException">
        /// array length not equal matrix rows.
        /// </exception>
        public static Vectorization.Vector <T> KramerSolve <T>(this Matrix <T> matrix, Vectorization.Vector <T> arr) where T : unmanaged
        {
            if (matrix.Rows != arr.Length)
            {
                throw new SizeNotEqualException(ExceptionArgument.RowSizeOfMatrixIsNotEqualSizeOfVector);
            }

            var det = matrix.GetDeterminant();
            var vr  = new Vectorization.Vector <T>(matrix.Columns);

            if (!(matrix.Clone() is Matrix <T> temp))
            {
                throw new NullReferenceException();
            }

            for (int i = 0; i < matrix.Columns; i++)
            {
                for (int j = 0; j < matrix.Rows; j++)
                {
                    temp[j, i] = arr[j];
                }

                vr[i] = MathUnsafe <T> .Div(temp.GetDeterminant(), det);
            }

            return(vr);
        }
Esempio n. 2
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);
        }