/// <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); }
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)); }
/// <summary> /// Gets modules of deviations from the mean. /// </summary> /// <returns>Modules of deviations from the mean.</returns> public T[] GetModulesDevMean() { T[] arr = new T[Matrix.Rows]; T[] xi = Matrix[GetIndexColumn(TableVariations.Xi), State.Column]; T mean = GetSampleMeanByTable(TableVariations.Xi); for (int i = 0; i < arr.Length; i++) { arr[i] = MathGeneric <T> .Abs(MathUnsafe <T> .Sub(xi[i], mean)); } return(arr); }
/// <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])); } } } } }
public static void EigenVectorQrIterative <T>(this Matrix <T> matrix, double accuracy, int maxIterations, out Matrix <T> iter, out Matrix <T> qIter) where T : unmanaged { iter = matrix.Clone() as Matrix <T>; qIter = null; for (int i = 0; i < maxIterations; i++) { iter.QrDecomposition(out var q, out var r); iter = r * q; if (qIter is null) { qIter = q; } else { var qNew = qIter * q; bool isAchieved = true; // checks accuracy for (int j = 0; j < q.Columns; j++) { for (int k = 0; k < q.Rows; k++) { var sub = MathUnsafe <T> .Sub(MathGeneric <T> .Abs(qNew[j, k]), MathGeneric <T> .Abs(qIter[j, k])); if (accuracy.CompareTo(MathGeneric <T> .Abs(sub)) <= 0) { continue; } isAchieved = false; break; } if (!isAchieved) { break; } } qIter = qNew; if (isAchieved) { break; } } } }
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)); }