From daabd7e89cfcf0e253073696e8637764ea1e1fe4 Mon Sep 17 00:00:00 2001 From: towsey Date: Fri, 12 Jun 2020 14:38:56 +1000 Subject: [PATCH] Write tests for MatrixTools Issue #321 Write two tests for matrix concatenation --- src/TowseyLibrary/MatrixTools.cs | 87 ++++++++++--------- .../TowseyLibrary/MatrixToolsTests.cs | 72 +++++++++++++++ 2 files changed, 119 insertions(+), 40 deletions(-) create mode 100644 tests/Acoustics.Test/TowseyLibrary/MatrixToolsTests.cs diff --git a/src/TowseyLibrary/MatrixTools.cs b/src/TowseyLibrary/MatrixTools.cs index 2bef99b81..abc6f737a 100644 --- a/src/TowseyLibrary/MatrixTools.cs +++ b/src/TowseyLibrary/MatrixTools.cs @@ -55,18 +55,25 @@ public static class MatrixTools } /// - /// 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. /// 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]; } @@ -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]; } @@ -84,6 +91,41 @@ public static class MatrixTools return opMatrix; } + /// + /// 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. + /// + 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)) @@ -285,41 +327,6 @@ public static double[] Matrix2Array(double[,] matrix) return v; } - /// - /// 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. - /// - 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; - } - /// /// 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. diff --git a/tests/Acoustics.Test/TowseyLibrary/MatrixToolsTests.cs b/tests/Acoustics.Test/TowseyLibrary/MatrixToolsTests.cs new file mode 100644 index 000000000..7c5a6d3ab --- /dev/null +++ b/tests/Acoustics.Test/TowseyLibrary/MatrixToolsTests.cs @@ -0,0 +1,72 @@ +// +// 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). +// + +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); + } + } +} \ No newline at end of file