Skip to content

Commit

Permalink
Write tests for MatrixTools
Browse files Browse the repository at this point in the history
Issue #321 Write two tests for matrix concatenation
  • Loading branch information
towsey authored and atruskie committed Jun 12, 2020
1 parent 4433ec4 commit daabd7e
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 40 deletions.
87 changes: 47 additions & 40 deletions src/TowseyLibrary/MatrixTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,25 @@ public static class MatrixTools
}

/// <summary>
/// Method assumes that the column count for two matrices is the same.
/// Concatenates two matrices that have the same column count.
/// That is, each row of the output matrix is the join of the equivalent two rows of the input matrices.
/// </summary>
public static double[,] ConcatenateMatrixRows(double[,] m1, double[,] m2)
{
int colCount = m1.GetLength(1);
int m1ColCount = m1.GetLength(1);
int m2ColCount = m2.GetLength(1);
int m1RowCount = m1.GetLength(0);
int m2RowCount = m2.GetLength(0);

var opMatrix = new double[m1RowCount + m2RowCount, colCount];
if (m1ColCount != m2ColCount)
{
throw new ArgumentException($"Cannot join these matrices. They do not have the same column count. {m1ColCount} != {m2ColCount}.");
}

var opMatrix = new double[m1RowCount + m2RowCount, m1ColCount];
for (int r = 0; r < m1RowCount; r++)
{
for (int c = 0; c < colCount; c++)
for (int c = 0; c < m1ColCount; c++)
{
opMatrix[r, c] = m1[r, c];
}
Expand All @@ -75,7 +82,7 @@ public static class MatrixTools
for (int r = 0; r < m2RowCount; r++)
{
int row = m1RowCount + r;
for (int c = 0; c < colCount; c++)
for (int c = 0; c < m1ColCount; c++)
{
opMatrix[row, c] = m2[r, c];
}
Expand All @@ -84,6 +91,41 @@ public static class MatrixTools
return opMatrix;
}

/// <summary>
/// Concatenates two matrices that have the same row count.
/// That is, each row of the output matrix is the join of the equivalent two rows of the input matrices.
/// WARNING: If the two matrices do not have the same number of rows, an exception is thrown.
/// </summary>
public static double[,] ConcatenateTwoMatrices(double[,] matrix1, double[,] matrix2)
{
int rowCount1 = matrix1.GetLength(0);
int colCount1 = matrix1.GetLength(1);
int rowCount2 = matrix2.GetLength(0);
int colCount2 = matrix2.GetLength(1);

if (rowCount1 != rowCount2)
{
throw new ArgumentException($"Cannot join these matrices. They do not have the same row count. {rowCount1} != {rowCount2}.");
}

double[,] opMatrix = new double[rowCount1, colCount1 + colCount2];

for (int row = 0; row < rowCount1; row++)
{
for (int col = 0; col < colCount1; col++)
{
opMatrix[row, col] = matrix1[row, col];
}

for (int col = 0; col < colCount2; col++)
{
opMatrix[row, colCount1 + col] = matrix2[row, col];
}
}

return opMatrix;
}

public static bool MatrixDimensionsAreEqual(double[,] m1, double[,] m2)
{
if (m1.GetLength(0) == m2.GetLength(0) && m1.GetLength(1) == m2.GetLength(1))
Expand Down Expand Up @@ -285,41 +327,6 @@ public static double[] Matrix2Array(double[,] matrix)
return v;
}

/// <summary>
/// Concatenate two matrices.
/// WARNING: This method requires that the two matrices have the same number of rows.
/// They will be joined by adding the columns of M2 to the columns of M1.
/// </summary>
public static double[,] ConcatenateTwoMatrices(double[,] matrix1, double[,] matrix2)
{
int rowCount1 = matrix1.GetLength(0);
int colCount1 = matrix1.GetLength(1);
int rowCount2 = matrix2.GetLength(0);
int colCount2 = matrix2.GetLength(1);

if(rowCount1 != rowCount2)
{
throw new ArgumentException($"Cannot join these matrices. They do not have the same row count. {rowCount1} != {rowCount2}.");
}

double[,] opMatrix = new double[rowCount1, colCount1 + colCount2];

for (int row = 0; row < rowCount1; row++)
{
for (int col = 0; col < colCount1; col++)
{
opMatrix[row, col] = matrix1[row, col];
}

for (int col = 0; col < colCount2; col++)
{
opMatrix[row, colCount1 + col] = matrix1[row, col];
}
}

return opMatrix;
}

/// <summary>
/// converts a vector to a matrix in the direction of column.
/// For example, the "Matrix2Array" method in MatrixTools.cs builds the vector by concatenating the columns.
Expand Down
72 changes: 72 additions & 0 deletions tests/Acoustics.Test/TowseyLibrary/MatrixToolsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// <copyright file="MatrixToolsTests.cs" company="QutEcoacoustics">
// All code in this file and all associated files are the copyright and property of the QUT Ecoacoustics Research Group (formerly MQUTeR, and formerly QUT Bioacoustics Research Group).
// </copyright>

namespace Acoustics.Test.TowseyLibrary
{
using System.Collections.Generic;
using global::TowseyLibrary;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class MatrixToolsTests
{
[TestMethod]
public void ConcatenateMatrixRows()
{
double[,] matrix1 =
{
{ 1.0, 4.0 },
{ 2.0, 5.0 },
{ 3.0, 6.0 },
};

double[,] matrix2 =
{
{ 7.0, 10.0 },
{ 8.0, 11.0 },
{ 9.0, 12.0 },
};

double[,] expected =
{
{ 1.0, 4.0 },
{ 2.0, 5.0 },
{ 3.0, 6.0 },
{ 7.0, 10.0 },
{ 8.0, 11.0 },
{ 9.0, 12.0 },
};

var actual = MatrixTools.ConcatenateMatrixRows(matrix1, matrix2);

CollectionAssert.AreEqual(expected, actual);
}

[TestMethod]
public void ConcatenateTwoMatrices()
{
double[,] matrix1 =
{
{ 1.0, 3.0, 5.0 },
{ 2.0, 4.0, 6.0 },
};

double[,] matrix2 =
{
{ 7.0, 9.0 },
{ 8.0, 10.0 },
};

double[,] expected =
{
{ 1.0, 3.0, 5.0, 7.0, 9.0 },
{ 2.0, 4.0, 6.0, 8.0, 10.0 },
};

var actual = MatrixTools.ConcatenateTwoMatrices(matrix1, matrix2);

CollectionAssert.AreEqual(expected, actual);
}
}
}

0 comments on commit daabd7e

Please sign in to comment.