/// <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])); } } } } }
/// <summary> /// Raises a matrix to a power. /// </summary> /// <param name="matrix">the matrix</param> /// <param name="degree">the degree</param> /// <typeparam name="T">unmanaged type</typeparam> /// <returns>Pow</returns> /// <exception cref="MatrixDotNetException"></exception> public static Matrix <T> Pow <T>(this Matrix <T> matrix, uint degree) where T : unmanaged { if (degree == 0) { return(BuildMatrix.CreateIdentityMatrix <T>(matrix.Rows, matrix.Columns)); } if ((degree & 1) == 1) { return(matrix.Pow(degree - 1) * matrix); } var t = matrix.Pow(degree >> 1); return(t * t); }