/// <summary> /// Gets the square root of the sum of squares for all elements in a matrix. /// </summary> /// public static double Frobenius(this double[,] a) { if (a == null) throw new ArgumentNullException("a"); int rows = a.Rows(); int cols = a.Columns(); double norm = 0.0; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { double v = a[i, j]; norm += v * v; } } return System.Math.Sqrt(norm); }
public static DataTable DataPreviewTable(this Sheet sheet, int previewRowCount) { var dt = new DataTable("Preview Table"); var dtcs = sheet.Columns().Select(p => new DataColumn(p.Name)); //check for duplicates and throw error if found var bad = dtcs.GroupBy(d => d.ColumnName).Select(g => new { g.Key, count = g.Count() }).Where(g => g.count > 1); if (bad.Count() > 0) { var s = bad.Aggregate(string.Empty, (current, enumerable) => current + (enumerable.Key + "; ")); throw new InvalidDataException("Duplicate column names found. " + s, new Exception("Bad columns : " + s.Trim())); } dt.Columns.AddRange(dtcs.ToArray()); var rows = sheet.Rows().Take(previewRowCount).Skip(sheet.ColumnHeaderRow ?? 0); foreach (var rrow in rows) { dt.NewRow(); var cells = rrow.Cells(); var va = cells != null ? cells.Select(p => p.Value).ToArray() : new object[dt.Columns.Count]; dt.Rows.Add(va); } return dt; }
public static string Print(this double[,] a) { var str = new StringBuilder(); //find the max len var maxLen = 0; for (var i = 0; i < a.Rows(); i++) for (var j = 0; j < a.Columns(); j++) if (a[i, j].ToString(CultureInfo.InvariantCulture).Length > maxLen) maxLen = a[i, j].ToString(CultureInfo.InvariantCulture).Length; maxLen += 2; for (var i = 0; i < a.Rows(); i++) { for (var j = 0; j < a.Columns(); j++) { str.AppendFormat(("{0,-" + maxLen + "}"), a[i, j]); } str.AppendLine(); } return str.ToString(); }
/// <summary> /// Gets the Squared Euclidean norm vector for a matrix. /// </summary> /// public static float[] SquareEuclidean(this float[,] a, int dimension) { int rows = a.Rows(); int cols = a.Columns(); float[] norm; if (dimension == 0) { norm = new float[cols]; for (int j = 0; j < norm.Length; j++) { float sum = 0; for (int i = 0; i < rows; i++) { float v = a[i, j]; sum += v * v; } norm[j] = sum; } } else { norm = new float[rows]; for (int i = 0; i < norm.Length; i++) { float sum = 0; for (int j = 0; j < cols; j++) { float v = a[i, j]; sum += v * v; } norm[i] = sum; } } return norm; }
/// <summary> /// Creates a matrix decomposition that be used to compute the solution matrix if the /// matrix is square or the least squares solution otherwise. /// </summary> /// public static ISolverArrayDecomposition<double> Decompose(this double[][] matrix, bool leastSquares = false) { int rows = matrix.Rows(); int cols = matrix.Columns(); if (leastSquares) { return new JaggedSingularValueDecomposition(matrix, computeLeftSingularVectors: true, computeRightSingularVectors: true, autoTranspose: true); } if (rows == cols) { // Solve by LU Decomposition if matrix is square. return new JaggedLuDecomposition(matrix); } else { if (cols < rows) { // Solve by QR Decomposition if not. return new JaggedQrDecomposition(matrix); } else { return new JaggedSingularValueDecomposition(matrix, computeLeftSingularVectors: true, computeRightSingularVectors: true, autoTranspose: true); } } }
/// <summary> /// Gets the Squared Euclidean norm vector for a matrix. /// </summary> /// public static double[] SquareEuclidean(this double[][] a, int dimension) { int rows = a.Rows(); int cols = a.Columns(); double[] norm; if (dimension == 0) { norm = new double[cols]; for (int j = 0; j < norm.Length; j++) { double sum = 0.0; for (int i = 0; i < a.Length; i++) { double v = a[i][j]; sum += v * v; } norm[j] = sum; } } else { norm = new double[rows]; for (int i = 0; i < norm.Length; i++) { double sum = 0.0; for (int j = 0; j < a[i].Length; j++) { double v = a[i][j]; sum += v * v; } norm[i] = sum; } } return norm; }
/// <summary> /// Creates a matrix decomposition that be used to compute the solution matrix if the /// matrix is square or the least squares solution otherwise. /// </summary> /// public static ISolverMatrixDecomposition<decimal> Decompose(this decimal[,] matrix, bool leastSquares = false) { int rows = matrix.Rows(); int cols = matrix.Columns(); if (leastSquares) { return new SingularValueDecompositionD(matrix, computeLeftSingularVectors: true, computeRightSingularVectors: true, autoTranspose: true); } if (rows == cols) { // Solve by LU Decomposition if matrix is square. return new LuDecompositionD(matrix); } else { if (cols < rows) { // Solve by QR Decomposition if not. return new QrDecompositionD(matrix); } else { return new SingularValueDecompositionD(matrix, computeLeftSingularVectors: true, computeRightSingularVectors: true, autoTranspose: true); } } }
public static decimal[,] CumulativeSum(this decimal[,] matrix, int dimension, decimal[,] result) { int rows = matrix.Rows(); int cols = matrix.Columns(); if (dimension == 1) { result.SetColumn(0, matrix.GetRow(0)); for (int i = 1; i < rows; i++) for (int j = 0; j < cols; j++) result[i, j] = (decimal)(result[i - 1, j] + matrix[i, j]); } else if (dimension == 0) { result.SetColumn(0, matrix.GetColumn(0)); for (int i = 1; i < cols; i++) for (int j = 0; j < rows; j++) result[i, j] = (decimal)(result[i - 1, j] + matrix[j, i]); } else { throw new ArgumentException("Invalid dimension", "dimension"); } return result; }
public static decimal[,] CumulativeSum(this decimal[,] matrix, int dimension) { int rows = matrix.Rows(); int cols = matrix.Columns(); if (dimension == 1) return CumulativeSum(matrix, dimension, Matrix.Zeros<decimal>(rows, cols)); return CumulativeSum(matrix, dimension, Matrix.Zeros<decimal>(cols, rows)); }
public static long[][] CumulativeSum(this long[][] matrix, int dimension, long[][] result) { int rows = matrix.Rows(); int cols = matrix.Columns(); if (dimension == 1) { matrix.GetRow(0, result: result[0]); for (int i = 1; i < rows; i++) for (int j = 0; j < cols; j++) result[i][j] = (long)(result[i - 1][j] + matrix[i][j]); } else if (dimension == 0) { matrix.GetColumn(0, result: result[0]); for (int i = 1; i < cols; i++) for (int j = 0; j < rows; j++) result[i][j] = (long)(result[i - 1][j] + matrix[j][i]); } else { throw new ArgumentException("Invalid dimension", "dimension"); } return result; }
public static long[][] CumulativeSum(this long[][] matrix, int dimension) { int rows = matrix.Rows(); int cols = matrix.Columns(); if (dimension == 1) return CumulativeSum(matrix, dimension, Jagged.Zeros<long>(rows, cols)); return CumulativeSum(matrix, dimension, Jagged.Zeros<long>(cols, rows)); }