private static void TestEquality() { // Equals(SkylineMatrix) var full1 = Matrix.CreateFromArray(SparsePosDef10by10.Matrix); var skyline1 = SkylineMatrix.CreateFromArrays(SparsePosDef10by10.Order, SparsePosDef10by10.SkylineValues, SparsePosDef10by10.SkylineDiagOffsets, true, true); Assert.True(full1.Equals(skyline1)); // Equals(CsrMatrix) var full2 = Matrix.CreateFromArray(SparseRectangular10by5.Matrix); var csr2 = CsrMatrix.CreateFromArrays(SparseRectangular10by5.NumRows, SparseRectangular10by5.NumCols, SparseRectangular10by5.CsrValues, SparseRectangular10by5.CsrColIndices, SparseRectangular10by5.CsrRowOffsets, true); Assert.True(full2.Equals(csr2)); // Equals(CscMatrix) var full3 = Matrix.CreateFromArray(SparseRectangular10by5.Matrix); var csc3 = CscMatrix.CreateFromArrays(SparseRectangular10by5.NumRows, SparseRectangular10by5.NumCols, SparseRectangular10by5.CscValues, SparseRectangular10by5.CscRowIndices, SparseRectangular10by5.CscColOffsets, true); Assert.True(full3.Equals(csc3)); }
private static void TestSkylinePosDef() { var matrix = SkylineMatrix.CreateFromArrays(SparsePosDef10by10.Order, SparsePosDef10by10.SkylineValues, SparsePosDef10by10.SkylineDiagOffsets, true, true); TestWriteOperation(matrix, SparsePosDef10by10.SkylineArraysPath); }
/// <summary> /// Reads the values and diagonal offsets arrays of a matrix in Skyline format from 2 separate files. /// </summary> /// <param name="valuesArrayPath">Each entry on a separate line. The first must be the array length.</param> /// <param name="diagOffsetsArrayPath">Each entry on a separate line. The first must be the array length.</param> /// <param name="checkSkylineArrays"> /// If true, the provided arrays will be checked to make sure they are valid Skyline arrays, which is safer. /// If false, no such check will take place, which is faster. /// </param> public SkylineMatrix ReadSkylineMatrixFromSeparateFiles(string valuesArrayPath, string diagOffsetsArrayPath, bool checkSkylineArrays) { // Values array double[] values; using (var reader = new StreamReader(valuesArrayPath)) { string line = reader.ReadLine(); int length = int.Parse(line.Trim()); values = new double[length]; for (int i = 0; i < length; ++i) { line = reader.ReadLine(); values[i] = double.Parse(line.TrimEnd()); } } // Diagonal offsets array int[] diagOffsets; using (var reader = new StreamReader(diagOffsetsArrayPath)) { string line = reader.ReadLine(); int length = int.Parse(line.Trim()); diagOffsets = new int[length]; for (int i = 0; i < length; ++i) { line = reader.ReadLine(); diagOffsets[i] = int.Parse(line.TrimEnd()); } } return(SkylineMatrix.CreateFromArrays(diagOffsets.Length - 1, values, diagOffsets, checkSkylineArrays)); }
/// <summary> /// Reads the Skyline values and diagonal offsets arrays from 2 different files. The first entry in each file must be /// the length of the corresponding array. /// </summary> /// <param name="valuesPath"> /// The absolute path of an array containing the values array of the Skyline format. The first entry must be its entry, /// which is equal to the number of nonzero entries. /// </param> /// <param name="diagonalOffsetsPath"> /// The absolute path of an array containing the diagonal offsets array of the Skyline format. The first entry must be /// its entry, which is equal to the number of rows/columns + 1. /// </param> public static SkylineMatrix ReadFromFiles(string valuesPath, string diagonalOffsetsPath) { FileStream fs = File.OpenRead(diagonalOffsetsPath); BinaryReader bw = new BinaryReader(fs); int length = 0; length = bw.ReadInt32(); int[] diagOffsets = new int[length]; for (int i = 0; i < diagOffsets.Length; i++) { diagOffsets[i] = bw.ReadInt32(); } bw.Close(); fs.Close(); fs = File.OpenRead(valuesPath); bw = new BinaryReader(fs); length = bw.ReadInt32(); double[] values = new double[length]; for (int i = 0; i < values.Length; i++) { values[i] = bw.ReadDouble(); } bw.Close(); fs.Close(); return(SkylineMatrix.CreateFromArrays(diagOffsets.Length - 1, values, diagOffsets, true, false)); }
private static void TestArrayCopy() { var skyline = SkylineMatrix.CreateFromArrays(SparsePosDef10by10.Order, SparsePosDef10by10.SkylineValues, SparsePosDef10by10.SkylineDiagOffsets, true, true); comparer.AssertEqual(SparsePosDef10by10.Matrix, skyline.CopyToArray2D()); }
private static void TestEquality() { var full = Matrix.CreateFromArray(SparsePosDef10by10.Matrix); var skyline = SkylineMatrix.CreateFromArrays(SparsePosDef10by10.Order, SparsePosDef10by10.SkylineValues, SparsePosDef10by10.SkylineDiagOffsets, true, true); Assert.True(skyline.Equals(full)); }
private static void TestClear() { var zero = Matrix.CreateZero(SparsePosDef10by10.Order, SparsePosDef10by10.Order); var skyline = SkylineMatrix.CreateFromArrays(SparsePosDef10by10.Order, SparsePosDef10by10.SkylineValues, SparsePosDef10by10.SkylineDiagOffsets, true, true); skyline.Clear(); comparer.AssertEqual(zero, skyline); }
//TODO: Move this method to DokSymmetric, so that I can optimize access to its private data internal static SkylineMatrix GetSubmatrixSymmetricSkyline(double[] skyValues, int[] skyDiagOffsets, int[] rowsColsToKeep) { //TODO: perhaps this can be combined with the CSC and full version to get all 2 submatrices needed for // Schur complements more efficiently. int subOrder = rowsColsToKeep.Length; if (subOrder == 0) { return(SkylineMatrix.CreateFromArrays(subOrder, new double[0], new int[1] { 0 }, false, false)); } var submatrix = DokSymmetric.CreateEmpty(subOrder); for (int subCol = 0; subCol < subOrder; ++subCol) { int col = rowsColsToKeep[subCol]; int diagOffset = skyDiagOffsets[col]; int colHeight = skyDiagOffsets[col + 1] - diagOffset - 1; // excluding diagonal for (int subRow = 0; subRow <= subCol; ++subRow) { int row = rowsColsToKeep[subRow]; if (row <= col) { int entryHeight = col - row; // excluding diagonal if (entryHeight <= colHeight) // inside stored non zero pattern { double val = skyValues[diagOffset + entryHeight]; if (val != 0.0) { submatrix.SetEntryUpper(subRow, subCol, val); // Skyline format stores many unnecessary zeros. } } } else // Transpose row <-> col. The cached column height and offset must be recalculated. { int transposedDiagOffset = skyDiagOffsets[row]; int transposedColHeight = skyDiagOffsets[row + 1] - transposedDiagOffset - 1; // excluding diagonal int entryHeight = row - col; // excluding diagonal if (entryHeight <= transposedColHeight) // inside stored non zero pattern { double val = skyValues[transposedDiagOffset + entryHeight]; if (val != 0.0) { submatrix[subRow, subCol] = val; // Skyline format stores many unnecessary zeros. } } } } } return(submatrix.BuildSkylineMatrix()); }
private static void TestSparsePosDef() { var matrix = SkylineMatrix.CreateFromArrays(SparsePosDef10by10.Order, SparsePosDef10by10.SkylineValues, SparsePosDef10by10.SkylineDiagOffsets, true, true); var writer = new FullMatrixWriter { NumericFormat = new FixedPointFormat { MaxIntegerDigits = 2 } }; TestWriteOperation(matrix, SparsePosDef10by10.FullFormatPath, writer); }
private static void TestGetColumn() { var matrix = SkylineMatrix.CreateFromArrays(SparsePosDef10by10.Order, SparsePosDef10by10.SkylineValues, SparsePosDef10by10.SkylineDiagOffsets, true, true); for (int j = 0; j < SparsePosDef10by10.Order; ++j) { Vector colExpected = DenseStrategies.GetColumn(matrix, j); Vector colComputed = matrix.GetColumn(j); comparer.AssertEqual(colExpected, colComputed); } }
private static void TestGetRow() { var matrix = SkylineMatrix.CreateFromArrays(SparsePosDef10by10.Order, SparsePosDef10by10.SkylineValues, SparsePosDef10by10.SkylineDiagOffsets, true, true); for (int i = 0; i < SparsePosDef10by10.Order; ++i) { Vector rowExpected = DenseStrategies.GetRow(matrix, i); Vector rowComputed = matrix.GetRow(i); comparer.AssertEqual(rowExpected, rowComputed); } }
private static void TestFactorization() { //TestSettings.RunMultiproviderTest(providers, delegate () { // var skyline = SkylineMatrix.CreateFromArrays(SparsePosDef10by10.Order, SparsePosDef10by10.SkylineValues, SparsePosDef10by10.SkylineDiagOffsets, true, true); var expectedU = Matrix.CreateFromArray(SparsePosDef10by10.CholeskyU); CholeskySkyline factorization = skyline.FactorCholesky(false); TriangularUpper computedU = factorization.GetFactorU(); comparer.AssertEqual(expectedU, computedU); //}); }
private static void TestMatrixVectorMultiplication(/*LinearAlgebraProviderChoice providers*/) { //TestSettings.RunMultiproviderTest(providers, delegate () { // var A = SkylineMatrix.CreateFromArrays(SparsePosDef10by10.Order, SparsePosDef10by10.SkylineValues, SparsePosDef10by10.SkylineDiagOffsets, true, true); var x = Vector.CreateFromArray(SparsePosDef10by10.Lhs); var bExpected = Vector.CreateFromArray(SparsePosDef10by10.Rhs); IVector bComputed = A.Multiply(x, false); comparer.AssertEqual(bExpected, bComputed); //}); }
private static void TestSystemSolution() { int order = SparsePosDef10by10.Order; var skyline = SkylineMatrix.CreateFromArrays(order, SparsePosDef10by10.SkylineValues, SparsePosDef10by10.SkylineDiagOffsets, true, true); var dok = DokSymmetric.CreateFromSparseMatrix(skyline); var b = Vector.CreateFromArray(SparsePosDef10by10.Rhs); var xExpected = Vector.CreateFromArray(SparsePosDef10by10.Lhs); (double[] cscValues, int[] cscRowIndices, int[] cscColOffsets) = dok.BuildSymmetricCscArrays(true); var factor = CholeskyCSparseNet.Factorize(order, cscValues.Length, cscValues, cscRowIndices, cscColOffsets); Vector xComputed = factor.SolveLinearSystem(b); comparer.AssertEqual(xExpected, xComputed); }
private static void TestSystemSolution(/*LinearAlgebraProviderChoice providers*/) { //TestSettings.RunMultiproviderTest(providers, delegate () { // var skyline = SkylineMatrix.CreateFromArrays(SparsePosDef10by10.Order, SparsePosDef10by10.SkylineValues, SparsePosDef10by10.SkylineDiagOffsets, true, true); var b = Vector.CreateFromArray(SparsePosDef10by10.Rhs); var xExpected = Vector.CreateFromArray(SparsePosDef10by10.Lhs); CholeskySkyline factor = skyline.FactorCholesky(false); Vector xComputed = factor.SolveLinearSystem(b); comparer.AssertEqual(xExpected, xComputed); //}); }
private static void TestMatrixVectorMultiplicationIntoResult(/*LinearAlgebraProviderChoice providers*/) { //TestSettings.RunMultiproviderTest(providers, delegate () { // var A = SkylineMatrix.CreateFromArrays(SparsePosDef10by10.Order, SparsePosDef10by10.SkylineValues, SparsePosDef10by10.SkylineDiagOffsets, true, true); var x = Vector.CreateFromArray(SparsePosDef10by10.Lhs); var bExpected = Vector.CreateFromArray(SparsePosDef10by10.Rhs); // The result vectors will first be set to some non zero values to make sure that the result overwrites // them instead of being added to them. Vector bComputed = Vector.CreateWithValue(A.NumRows, 1.0); A.MultiplyIntoResult(x, bComputed, false); comparer.AssertEqual(bExpected, bComputed); //}); }
private static void TestMultipleSystemsSolution(/*LinearAlgebraProviderChoice providers*/) { //TestSettings.RunMultiproviderTest(providers, delegate () { // var skyline = SkylineMatrix.CreateFromArrays(SparsePosDef10by10.Order, SparsePosDef10by10.SkylineValues, SparsePosDef10by10.SkylineDiagOffsets, true, true); LdlSkyline factor = skyline.FactorLdl(false); var identity = Matrix.CreateIdentity(SparsePosDef10by10.Order); var inverse = Matrix.CreateZero(SparsePosDef10by10.Order, SparsePosDef10by10.Order); factor.SolveLinearSystems(identity, inverse); var matrixTimesInverse = MatrixOperations.MatrixTimesMatrix(SparsePosDef10by10.Matrix, inverse.CopyToArray2D()); comparer.AssertEqual(identity.CopyToArray2D(), matrixTimesInverse); //}); }
public static SkylineMatrix ReadFromSimilarlyNamedFiles(string commonNamePart) { string path = Path.GetDirectoryName(commonNamePart); string nameOnly = Path.GetFileNameWithoutExtension(commonNamePart); string ext = Path.GetExtension(commonNamePart); FileStream fs = File.OpenRead(path + "\\" + nameOnly + "-Ix" + ext); BinaryReader bw = new BinaryReader(fs); int length = 0; length = bw.ReadInt32(); var diagOffsets = new int[length]; for (int i = 0; i < diagOffsets.Length; i++) { diagOffsets[i] = bw.ReadInt32(); } bw.Close(); fs.Close(); fs = File.OpenRead(path + "\\" + nameOnly + "-Data" + ext); bw = new BinaryReader(fs); length = bw.ReadInt32(); var values = new double[length]; for (int i = 0; i < values.Length; i++) { values[i] = bw.ReadDouble(); } bw.Close(); fs.Close(); return(SkylineMatrix.CreateFromArrays(diagOffsets.Length - 1, values, diagOffsets, true, false)); //string[] lines = File.ReadAllLines(path + "\\" + nameOnly + "-Ix" + ext); //rowIndex = new int[lines.Length]; //for (int i = 0; i < lines.Length; i++) rowIndex[i] = Int32.Parse(lines[i]); //lines = File.ReadAllLines(path + "\\" + nameOnly + "-Data" + ext); //data = new T[lines.Length]; //double[] mData = data as double[]; //for (int i = 0; i < lines.Length; i++) mData[i] = Convert.ToDouble(lines[i]); }
internal static SkylineMatrix GetSubmatrixSymmetricSkylineLegacy(double[] skyValues, int[] skyDiagOffsets, int[] rowsColsToKeep) { // Taken from G. Stavroulakis code in a FETI-DP solver. //TODO: It does not seem to work correctly if rowsColsToKeep is not sorted in ascending order. //TODO: compare against the version that uses DOK, in terms of a) time/memory required for creating the submatrix, // b) quality of the submatrix the DOK version may avoid some zeros. int subOrder = rowsColsToKeep.Length; int[] subDiagOffsets = new int[subOrder + 1]; int offset = 0; for (int i = 0; i < subOrder; i++) { subDiagOffsets[i] = offset; int col = rowsColsToKeep[i]; int fromRow = col - skyDiagOffsets[col + 1] + skyDiagOffsets[col] + 1; // top non zero entry of this col var newRows = rowsColsToKeep.Count(x => x >= fromRow && x <= col); offset += newRows; } subDiagOffsets[subOrder] = offset; var subValues = new double[subDiagOffsets[subDiagOffsets.Length - 1]]; offset = 0; for (int i = 0; i < subOrder; i++) { int col = rowsColsToKeep[i]; int fromRow = col - skyDiagOffsets[col + 1] + skyDiagOffsets[col] + 1; // top non zero entry of this col var newRows = rowsColsToKeep.Where(x => x >= fromRow && x <= col).OrderByDescending(x => x).ToArray <int>(); for (int j = 0; j < newRows.Length; j++) { subValues[offset] = skyValues[skyDiagOffsets[col] + col - newRows[j]]; offset++; } } return(SkylineMatrix.CreateFromArrays(subOrder, subValues, subDiagOffsets, false, false)); }
/// <summary> /// See <see cref="IIndexable2D.Equals(IIndexable2D, double)"/>. /// </summary> public bool Equals(IIndexable2D other, double tolerance = 1E-13) //TODO: what are the semantics of this? It cannot be compared to matrices. Perhaps IIndexable2D should not have Equals() => SkylineMatrix.CreateFromArrays(Order, values, diagOffsets, false, false).Equals(other, tolerance);
/// <summary> /// See <see cref="ISparseMatrix.GetSparseFormat"/>. /// </summary> public SparseFormat GetSparseFormat() => SkylineMatrix.CreateFromArrays(Order, values, diagOffsets, false, false).GetSparseFormat();