/// <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); }
/// <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); }
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)); }
/// <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)); }
/// <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)); }
/// <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; }
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); }
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)); }
/// <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)); }
/// <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 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) {
/// <summary> /// Gets algebraic complement. /// </summary> /// <param name="matrix">the matrix.</param> /// <typeparam name="T">unmanaged type.</typeparam> /// <returns></returns> public static Matrix <T> AlgebraicComplement <T>(this Matrix <T> matrix) where T : unmanaged { var res = matrix.GetMinorMatrix(); for (int i = 0; i < matrix.Rows; i++) { for (int j = 0; j < matrix.Columns; j++) { if ((i + j & 0b01) != 0) { res[i, j] = MathGeneric <T> .Negate(res[i, j]); } } } return(res); }
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; } } } }
/// <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); }
/// <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); }
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)); }
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); }
/// <summary> /// Gets corrected dispersion. /// </summary> /// <remarks> /// Finds by formula: s^2 = (n / (n - 1)) * Dispersion(sample) /// </remarks> public T GetCorrectedDispersion() { var n = Matrix.Rows; return(MathGeneric <T, double, T> .Multiply(GetSampleDispersion(), n - 1 / n)); }