diff --git a/src/SparseTransform/DataStructures/DoubleMatrix.cs b/src/SparseTransform/DataStructures/DoubleMatrix.cs index e103acfbc00e0142468a54fb1ea82745b48309c6..c26e5f2b732f89f6528fbf6549e2ad6db9cef0da 100644 --- a/src/SparseTransform/DataStructures/DoubleMatrix.cs +++ b/src/SparseTransform/DataStructures/DoubleMatrix.cs @@ -1,3 +1,5 @@ +using System.Text; + namespace SparseTransform.DataStructures { /// <summary> @@ -41,7 +43,7 @@ namespace SparseTransform.DataStructures /// <returns>MatrixMarket String</returns> public String ToMatrixMarketString() { - String MMString; + StringBuilder MMString = new StringBuilder(); String type = "real"; String symmetry; if (Symmetric) @@ -53,42 +55,19 @@ namespace SparseTransform.DataStructures symmetry = "general"; } // Build header - MMString = $"%%MatrixMarket matrix coordinate {type} {symmetry}\n"; + MMString.Append($"%%MatrixMarket matrix coordinate {type} {symmetry}\n"); // Comments may be added here // Add Dimensions and non-zero count - MMString += $"{RowCount} {ColumnCount} {NonZeros}\n"; + MMString.Append($"{RowCount} {ColumnCount} {NonZeros}\n"); for (int i = 0; i < RowCount; i++) { for (int j = 0; j < ColumnCount; j++) if (this[i, j] != 0) { - MMString += $"{i} {j} {this[i, j]}\n"; + MMString.Append($"{i} {j} {this[i, j]}\n"); } } - return MMString; - } - - /// <summary> - /// Multiply this matrix with another matrix - /// </summary> - /// <param name="other">second matrix</param> - /// <returns>product matrix</returns> - public DoubleMatrix Multiply(DoubleMatrix other) - { - DoubleMatrix result = new DoubleMatrix(this.RowCount, other.ColumnCount); - for (int i = 0; i < this.RowCount; i++) - { - for (int j = 0; j < other.ColumnCount; j++) - { - double s = 0; - for (int k = 0; k < this.ColumnCount; k++) - { - s += this[i, k] * other[k, j]; - } - result[i, j] = s; - } - } - return result; + return MMString.ToString(); } /// <summary> diff --git a/src/SparseTransform/DataStructures/IntegerMatrix.cs b/src/SparseTransform/DataStructures/IntegerMatrix.cs index 7d8fd10c7089fafad01fe9e5f9f0ee7816f8bb75..9d05433acf28ace034cbd013e07eaf7b7b433aae 100644 --- a/src/SparseTransform/DataStructures/IntegerMatrix.cs +++ b/src/SparseTransform/DataStructures/IntegerMatrix.cs @@ -1,3 +1,5 @@ +using System.Text; + namespace SparseTransform.DataStructures { /// <summary> @@ -41,7 +43,7 @@ namespace SparseTransform.DataStructures /// <returns>MatrixMarket String</returns> public String ToMatrixMarketString() { - String MMString; + StringBuilder MMString = new StringBuilder(); String type = "integer"; String symmetry; if (Symmetric) @@ -53,44 +55,19 @@ namespace SparseTransform.DataStructures symmetry = "general"; } // Build header - MMString = $"%%MatrixMarket matrix coordinate {type} {symmetry}\n"; + MMString.Append($"%%MatrixMarket matrix coordinate {type} {symmetry}\n"); // Comments may be added here // Add Dimensions and non-zero count - MMString += $"{RowCount} {ColumnCount} {NonZeros}\n"; + MMString.Append($"{RowCount} {ColumnCount} {NonZeros}\n"); for (int i = 0; i < RowCount; i++) { for (int j = 0; j < ColumnCount; j++) - { - if(this[i, j] != 0) - { - MMString += $"{i} {j} {this[i, j]}\n"; - } - } - } - return MMString; - } - - /// <summary> - /// Multiply this matrix with another matrix - /// </summary> - /// <param name="other">second matrix</param> - /// <returns>product matrix</returns> - public IntegerMatrix Multiply(IntegerMatrix other) - { - IntegerMatrix result = new IntegerMatrix(this.RowCount, other.ColumnCount); - for (int i = 0; i < this.RowCount; i++) - { - for (int j = 0; j < other.ColumnCount; j++) - { - int s = 0; - for (int k = 0; k < this.ColumnCount; k++) + if (this[i, j] != 0) { - s += this[i, k] * other[k, j]; + MMString.Append($"{i} {j} {this[i, j]}\n"); } - result[i, j] = s; - } } - return result; + return MMString.ToString(); } } } \ No newline at end of file