Skip to content
Snippets Groups Projects
Commit bfdc604c authored by Paul Nitzke's avatar Paul Nitzke
Browse files

Merge branch 'matrix-improvements' into 'master'

Various matrix improvements

See merge request !42
parents 9d71cce5 d84855e3
No related branches found
No related tags found
1 merge request!42Various matrix improvements
using System.Text;
namespace SparseTransform.DataStructures namespace SparseTransform.DataStructures
{ {
/// <summary> /// <summary>
...@@ -41,7 +43,7 @@ namespace SparseTransform.DataStructures ...@@ -41,7 +43,7 @@ namespace SparseTransform.DataStructures
/// <returns>MatrixMarket String</returns> /// <returns>MatrixMarket String</returns>
public String ToMatrixMarketString() public String ToMatrixMarketString()
{ {
String MMString; StringBuilder MMString = new StringBuilder();
String type = "real"; String type = "real";
String symmetry; String symmetry;
if (Symmetric) if (Symmetric)
...@@ -53,42 +55,19 @@ namespace SparseTransform.DataStructures ...@@ -53,42 +55,19 @@ namespace SparseTransform.DataStructures
symmetry = "general"; symmetry = "general";
} }
// Build header // Build header
MMString = $"%%MatrixMarket matrix coordinate {type} {symmetry}\n"; MMString.Append($"%%MatrixMarket matrix coordinate {type} {symmetry}\n");
// Comments may be added here // Comments may be added here
// Add Dimensions and non-zero count // 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 i = 0; i < RowCount; i++)
{ {
for (int j = 0; j < ColumnCount; j++) for (int j = 0; j < ColumnCount; j++)
if (this[i, j] != 0) if (this[i, j] != 0)
{ {
MMString += $"{i} {j} {this[i, j]}\n"; MMString.Append($"{i} {j} {this[i, j]}\n");
}
} }
return MMString;
} }
return MMString.ToString();
/// <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;
} }
/// <summary> /// <summary>
......
using System.Text;
namespace SparseTransform.DataStructures namespace SparseTransform.DataStructures
{ {
/// <summary> /// <summary>
...@@ -41,7 +43,7 @@ namespace SparseTransform.DataStructures ...@@ -41,7 +43,7 @@ namespace SparseTransform.DataStructures
/// <returns>MatrixMarket String</returns> /// <returns>MatrixMarket String</returns>
public String ToMatrixMarketString() public String ToMatrixMarketString()
{ {
String MMString; StringBuilder MMString = new StringBuilder();
String type = "integer"; String type = "integer";
String symmetry; String symmetry;
if (Symmetric) if (Symmetric)
...@@ -53,44 +55,19 @@ namespace SparseTransform.DataStructures ...@@ -53,44 +55,19 @@ namespace SparseTransform.DataStructures
symmetry = "general"; symmetry = "general";
} }
// Build header // Build header
MMString = $"%%MatrixMarket matrix coordinate {type} {symmetry}\n"; MMString.Append($"%%MatrixMarket matrix coordinate {type} {symmetry}\n");
// Comments may be added here // Comments may be added here
// Add Dimensions and non-zero count // 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 i = 0; i < RowCount; i++)
{ {
for (int j = 0; j < ColumnCount; j++) for (int j = 0; j < ColumnCount; j++)
{
if (this[i, j] != 0) 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 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++)
{
s += this[i, k] * other[k, j];
}
result[i, j] = s;
} }
} }
return result; return MMString.ToString();
} }
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment