From 9df3c132955bfef5247b038ef3ec2b8a15595f9b Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sat, 4 Aug 2012 22:09:08 +0200 Subject: [PATCH] Tests: replace matrix structural tests with theory (part 7) - Apped, Stack --- .../LinearAlgebra/Complex/DiagonalMatrix.cs | 2 +- .../LinearAlgebra/Complex32/DiagonalMatrix.cs | 2 +- .../LinearAlgebra/Double/DiagonalMatrix.cs | 2 +- src/Numerics/LinearAlgebra/Generic/Matrix.cs | 51 +--- .../LinearAlgebra/Single/DiagonalMatrix.cs | 2 +- .../Complex/DiagonalMatrixTests.cs | 32 --- .../LinearAlgebraTests/Complex/MatrixTests.cs | 236 ------------------ .../Complex32/DiagonalMatrixTests.cs | 32 --- .../Complex32/MatrixTests.cs | 236 ------------------ .../Double/DiagonalMatrixTests.cs | 32 --- .../LinearAlgebraTests/Double/MatrixTests.cs | 236 ------------------ .../MatrixStructureTheory.Reform.cs | 157 +++++++++++- .../Single/DiagonalMatrixTests.cs | 32 --- .../LinearAlgebraTests/Single/MatrixTests.cs | 236 ------------------ 14 files changed, 166 insertions(+), 1122 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs index 268cf260..82582656 100644 --- a/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs @@ -1372,7 +1372,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex } // Copy the lower matrix into the result matrix. - CommonParallel.For(0, lower.RowCount, i => CommonParallel.For(0, lower.ColumnCount, j => result.At(i + RowCount, j + ColumnCount, lower.At(i, j)))); + result.SetSubMatrix(RowCount, lower.RowCount, ColumnCount, lower.ColumnCount, lower); } /// diff --git a/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs index 02f0ea4b..6fb1ab91 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs @@ -1372,7 +1372,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 } // Copy the lower matrix into the result matrix. - CommonParallel.For(0, lower.RowCount, i => CommonParallel.For(0, lower.ColumnCount, j => result.At(i + RowCount, j + ColumnCount, lower.At(i, j)))); + result.SetSubMatrix(RowCount, lower.RowCount, ColumnCount, lower.ColumnCount, lower); } /// diff --git a/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs index d2583b3c..5e1b9c8d 100644 --- a/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs @@ -1366,7 +1366,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double } // Copy the lower matrix into the result matrix. - CommonParallel.For(0, lower.RowCount, i => CommonParallel.For(0, lower.ColumnCount, j => result.At(i + RowCount, j + ColumnCount, lower.At(i, j)))); + result.SetSubMatrix(RowCount, lower.RowCount, ColumnCount, lower.ColumnCount, lower); } /// diff --git a/src/Numerics/LinearAlgebra/Generic/Matrix.cs b/src/Numerics/LinearAlgebra/Generic/Matrix.cs index 72cdaddc..0bbe4999 100644 --- a/src/Numerics/LinearAlgebra/Generic/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Generic/Matrix.cs @@ -1577,21 +1577,8 @@ namespace MathNet.Numerics.LinearAlgebra.Generic throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension); } - for (var i = 0; i < RowCount; i++) - { - for (var j = 0; j < ColumnCount; j++) - { - result.At(i, j, At(i, j)); - } - } - - for (var i = 0; i < RowCount; i++) - { - for (var j = 0; j < right.ColumnCount; j++) - { - result.At(i, j + ColumnCount, right.At(i, j)); - } - } + result.SetSubMatrix(0, RowCount, 0, ColumnCount, this); + result.SetSubMatrix(0, right.RowCount, ColumnCount, right.ColumnCount, right); } /// @@ -1647,21 +1634,8 @@ namespace MathNet.Numerics.LinearAlgebra.Generic throw DimensionsDontMatch(this, result, "result"); } - for (var i = 0; i < RowCount; i++) - { - for (var j = 0; j < ColumnCount; j++) - { - result.At(i, j, At(i, j)); - } - } - - for (var i = 0; i < lower.RowCount; i++) - { - for (var j = 0; j < ColumnCount; j++) - { - result.At(i + RowCount, j, lower.At(i, j)); - } - } + result.SetSubMatrix(0, RowCount, 0, ColumnCount, this); + result.SetSubMatrix(RowCount, lower.RowCount, 0, lower.ColumnCount, lower); } /// @@ -1709,21 +1683,8 @@ namespace MathNet.Numerics.LinearAlgebra.Generic throw DimensionsDontMatch(this, result, "result"); } - for (var i = 0; i < RowCount; i++) - { - for (var j = 0; j < ColumnCount; j++) - { - result.At(i, j, At(i, j)); - } - } - - for (var i = 0; i < lower.RowCount; i++) - { - for (var j = 0; j < lower.ColumnCount; j++) - { - result.At(i + RowCount, j + ColumnCount, lower.At(i, j)); - } - } + result.SetSubMatrix(0, RowCount, 0, ColumnCount, this); + result.SetSubMatrix(RowCount, lower.RowCount, ColumnCount, lower.ColumnCount, lower); } /// Calculates the L1 norm. diff --git a/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs index 57ff3b21..92ea1134 100644 --- a/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs @@ -1366,7 +1366,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single } // Copy the lower matrix into the result matrix. - CommonParallel.For(0, lower.RowCount, i => CommonParallel.For(0, lower.ColumnCount, j => result.At(i + RowCount, j + ColumnCount, lower.At(i, j)))); + result.SetSubMatrix(RowCount, lower.RowCount, ColumnCount, lower.ColumnCount, lower); } /// diff --git a/src/UnitTests/LinearAlgebraTests/Complex/DiagonalMatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Complex/DiagonalMatrixTests.cs index 92be7284..5fb1bc73 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex/DiagonalMatrixTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex/DiagonalMatrixTests.cs @@ -202,38 +202,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex Assert.Throws(() => DiagonalMatrix.Identity(order)); } - /// - /// Can diagonally stack matrices into a result matrix. - /// - public override void CanDiagonallyStackMatricesIntoResult() - { - var top = TestMatrices["Tall3x2"]; - var bottom = TestMatrices["Wide2x3"]; - var result = new SparseMatrix(top.RowCount + bottom.RowCount, top.ColumnCount + bottom.ColumnCount); - top.DiagonalStack(bottom, result); - Assert.AreEqual(top.RowCount + bottom.RowCount, result.RowCount); - Assert.AreEqual(top.ColumnCount + bottom.ColumnCount, result.ColumnCount); - - for (var i = 0; i < result.RowCount; i++) - { - for (var j = 0; j < result.ColumnCount; j++) - { - if (i < top.RowCount && j < top.ColumnCount) - { - Assert.AreEqual(top[i, j], result[i, j]); - } - else if (i >= top.RowCount && j >= top.ColumnCount) - { - Assert.AreEqual(bottom[i - top.RowCount, j - top.ColumnCount], result[i, j]); - } - else - { - Assert.AreEqual(Complex.Zero, result[i, j]); - } - } - } - } - /// /// Can multiply a matrix with matrix. /// diff --git a/src/UnitTests/LinearAlgebraTests/Complex/MatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Complex/MatrixTests.cs index 9d2a096f..9971b7e2 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex/MatrixTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex/MatrixTests.cs @@ -152,242 +152,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex } } - /// - /// Can append matrices. - /// - [Test] - public void CanAppendMatrices() - { - var left = CreateMatrix(TestData2D["Singular3x3"]); - var right = CreateMatrix(TestData2D["Tall3x2"]); - var result = left.Append(right); - Assert.AreEqual(left.ColumnCount + right.ColumnCount, result.ColumnCount); - Assert.AreEqual(left.RowCount, right.RowCount); - - for (var i = 0; i < result.RowCount; i++) - { - for (var j = 0; j < result.ColumnCount; j++) - { - Assert.AreEqual(j < left.ColumnCount ? left[i, j] : right[i, j - left.ColumnCount], result[i, j]); - } - } - } - - /// - /// Append with right null throws ArgumentNullException. - /// - [Test] - public void AppendWithRightNullThrowsArgumentNullException() - { - var left = TestMatrices["Square3x3"]; - Matrix right = null; - Assert.Throws(() => left.Append(right)); - } - - /// - /// Append with result null throws ArgumentNullException. - /// - [Test] - public void AppendWithResultNullThrowsArgumentNullException() - { - var left = TestMatrices["Square3x3"]; - var right = TestMatrices["Tall3x2"]; - Matrix result = null; - Assert.Throws(() => left.Append(right, result)); - } - - /// - /// Appending two matrices with different row count throws ArgumentException. - /// - [Test] - public void AppendWithDifferentRowCountThrowsArgumentException() - { - var left = TestMatrices["Square3x3"]; - var right = TestMatrices["Wide2x3"]; - Assert.Throws(() => { var result = left.Append(right); }); - } - - /// - /// Appending with invalid result matrix columns throws ArgumentException. - /// - [Test] - public void AppendWithInvalidResultMatrixColumnsThrowsArgumentException() - { - var left = TestMatrices["Square3x3"]; - var right = TestMatrices["Tall3x2"]; - var result = CreateMatrix(3, 2); - Assert.Throws(() => left.Append(right, result)); - } - - /// - /// Can stack matrices. - /// - [Test] - public void CanStackMatrices() - { - var top = TestMatrices["Square3x3"]; - var bottom = TestMatrices["Wide2x3"]; - var result = top.Stack(bottom); - Assert.AreEqual(top.RowCount + bottom.RowCount, result.RowCount); - Assert.AreEqual(top.ColumnCount, result.ColumnCount); - - for (var i = 0; i < result.RowCount; i++) - { - for (var j = 0; j < result.ColumnCount; j++) - { - Assert.AreEqual(result[i, j], i < top.RowCount ? top[i, j] : bottom[i - top.RowCount, j]); - } - } - } - - /// - /// Stacking with a bottom null throws ArgumentNullException. - /// - [Test] - public void StackWithBottomNullThrowsArgumentNullException() - { - var top = TestMatrices["Square3x3"]; - Matrix bottom = null; - var result = CreateMatrix(top.RowCount + top.RowCount, top.ColumnCount); - Assert.Throws(() => top.Stack(bottom, result)); - } - - /// - /// Stacking with a result null throws ArgumentNullException. - /// - [Test] - public void StackWithResultNullThrowsArgumentNullException() - { - var top = TestMatrices["Square3x3"]; - var bottom = TestMatrices["Square3x3"]; - Matrix result = null; - Assert.Throws(() => top.Stack(bottom, result)); - } - - /// - /// Stacking matrices with different columns throws ArgumentException. - /// - [Test] - public void StackMatricesWithDifferentColumnsThrowsArgumentException() - { - var top = TestMatrices["Square3x3"]; - var lower = TestMatrices["Tall3x2"]; - var result = CreateMatrix(top.RowCount + lower.RowCount, top.ColumnCount); - Assert.Throws(() => top.Stack(lower, result)); - } - - /// - /// Stacking with invalid result matrix rows throws ArgumentException. - /// - [Test] - public void StackWithInvalidResultMatrixRowsThrowsArgumentException() - { - var top = TestMatrices["Square3x3"]; - var bottom = TestMatrices["Wide2x3"]; - var result = CreateMatrix(1, 3); - Assert.Throws(() => top.Stack(bottom, result)); - } - - /// - /// Can diagonally stack matrices. - /// - [Test] - public void CanDiagonallyStackMatrices() - { - var top = TestMatrices["Tall3x2"]; - var bottom = TestMatrices["Wide2x3"]; - var result = top.DiagonalStack(bottom); - Assert.AreEqual(top.RowCount + bottom.RowCount, result.RowCount); - Assert.AreEqual(top.ColumnCount + bottom.ColumnCount, result.ColumnCount); - - for (var i = 0; i < result.RowCount; i++) - { - for (var j = 0; j < result.ColumnCount; j++) - { - if (i < top.RowCount && j < top.ColumnCount) - { - Assert.AreEqual(top[i, j], result[i, j]); - } - else if (i >= top.RowCount && j >= top.ColumnCount) - { - Assert.AreEqual(bottom[i - top.RowCount, j - top.ColumnCount], result[i, j]); - } - else - { - Assert.AreEqual(Complex.Zero, result[i, j]); - } - } - } - } - - /// - /// Diagonal stack with lower null throws ArgumentNullException. - /// - [Test] - public void DiagonalStackWithLowerNullThrowsArgumentNullException() - { - var top = TestMatrices["Square3x3"]; - Matrix lower = null; - Assert.Throws(() => top.DiagonalStack(lower)); - } - - /// - /// Can diagonally stack matrices into a result matrix. - /// - [Test] - public virtual void CanDiagonallyStackMatricesIntoResult() - { - var top = TestMatrices["Tall3x2"]; - var bottom = TestMatrices["Wide2x3"]; - var result = CreateMatrix(top.RowCount + bottom.RowCount, top.ColumnCount + bottom.ColumnCount); - top.DiagonalStack(bottom, result); - Assert.AreEqual(top.RowCount + bottom.RowCount, result.RowCount); - Assert.AreEqual(top.ColumnCount + bottom.ColumnCount, result.ColumnCount); - - for (var i = 0; i < result.RowCount; i++) - { - for (var j = 0; j < result.ColumnCount; j++) - { - if (i < top.RowCount && j < top.ColumnCount) - { - Assert.AreEqual(top[i, j], result[i, j]); - } - else if (i >= top.RowCount && j >= top.ColumnCount) - { - Assert.AreEqual(bottom[i - top.RowCount, j - top.ColumnCount], result[i, j]); - } - else - { - Assert.AreEqual(Complex.Zero, result[i, j]); - } - } - } - } - - /// - /// Diagonal stack into null result throws ArgumentNullException. - /// - [Test] - public void DiagonalStackIntoResultNullThrowsArgumentNullException() - { - var top = TestMatrices["Square3x3"]; - var lower = TestMatrices["Wide2x3"]; - Matrix result = null; - Assert.Throws(() => top.DiagonalStack(lower, result)); - } - - /// - /// Diagonal stack into invalid result matrix throws ArgumentException. - /// - [Test] - public void DiagonalStackIntoInvalidResultMatrixThrowsArgumentException() - { - var top = TestMatrices["Square3x3"]; - var lower = TestMatrices["Wide2x3"]; - var result = CreateMatrix(top.RowCount + lower.RowCount + 2, top.ColumnCount + lower.ColumnCount); - Assert.Throws(() => top.DiagonalStack(lower, result)); - } - /// /// Can compute Frobenius norm. /// diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/DiagonalMatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Complex32/DiagonalMatrixTests.cs index 3321b0a4..4d2f2943 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex32/DiagonalMatrixTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex32/DiagonalMatrixTests.cs @@ -202,38 +202,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 Assert.Throws(() => DiagonalMatrix.Identity(order)); } - /// - /// Can diagonally stack matrices into a result matrix. - /// - public override void CanDiagonallyStackMatricesIntoResult() - { - var top = TestMatrices["Tall3x2"]; - var bottom = TestMatrices["Wide2x3"]; - var result = new SparseMatrix(top.RowCount + bottom.RowCount, top.ColumnCount + bottom.ColumnCount); - top.DiagonalStack(bottom, result); - Assert.AreEqual(top.RowCount + bottom.RowCount, result.RowCount); - Assert.AreEqual(top.ColumnCount + bottom.ColumnCount, result.ColumnCount); - - for (var i = 0; i < result.RowCount; i++) - { - for (var j = 0; j < result.ColumnCount; j++) - { - if (i < top.RowCount && j < top.ColumnCount) - { - Assert.AreEqual(top[i, j], result[i, j]); - } - else if (i >= top.RowCount && j >= top.ColumnCount) - { - Assert.AreEqual(bottom[i - top.RowCount, j - top.ColumnCount], result[i, j]); - } - else - { - Assert.AreEqual(Complex32.Zero, result[i, j]); - } - } - } - } - /// /// Can multiply a matrix with matrix. /// diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/MatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Complex32/MatrixTests.cs index b8bb356e..e58c3c74 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex32/MatrixTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex32/MatrixTests.cs @@ -152,242 +152,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 } } - /// - /// Can append matrices. - /// - [Test] - public void CanAppendMatrices() - { - var left = CreateMatrix(TestData2D["Singular3x3"]); - var right = CreateMatrix(TestData2D["Tall3x2"]); - var result = left.Append(right); - Assert.AreEqual(left.ColumnCount + right.ColumnCount, result.ColumnCount); - Assert.AreEqual(left.RowCount, right.RowCount); - - for (var i = 0; i < result.RowCount; i++) - { - for (var j = 0; j < result.ColumnCount; j++) - { - Assert.AreEqual(j < left.ColumnCount ? left[i, j] : right[i, j - left.ColumnCount], result[i, j]); - } - } - } - - /// - /// Append with right null throws ArgumentNullException. - /// - [Test] - public void AppendWithRightNullThrowsArgumentNullException() - { - var left = TestMatrices["Square3x3"]; - Matrix right = null; - Assert.Throws(() => left.Append(right)); - } - - /// - /// Append with result null throws ArgumentNullException. - /// - [Test] - public void AppendWithResultNullThrowsArgumentNullException() - { - var left = TestMatrices["Square3x3"]; - var right = TestMatrices["Tall3x2"]; - Matrix result = null; - Assert.Throws(() => left.Append(right, result)); - } - - /// - /// Appending two matrices with different row count throws ArgumentException. - /// - [Test] - public void AppendWithDifferentRowCountThrowsArgumentException() - { - var left = TestMatrices["Square3x3"]; - var right = TestMatrices["Wide2x3"]; - Assert.Throws(() => { var result = left.Append(right); }); - } - - /// - /// Appending with invalid result matrix columns throws ArgumentException. - /// - [Test] - public void AppendWithInvalidResultMatrixColumnsThrowsArgumentException() - { - var left = TestMatrices["Square3x3"]; - var right = TestMatrices["Tall3x2"]; - var result = CreateMatrix(3, 2); - Assert.Throws(() => left.Append(right, result)); - } - - /// - /// Can stack matrices. - /// - [Test] - public void CanStackMatrices() - { - var top = TestMatrices["Square3x3"]; - var bottom = TestMatrices["Wide2x3"]; - var result = top.Stack(bottom); - Assert.AreEqual(top.RowCount + bottom.RowCount, result.RowCount); - Assert.AreEqual(top.ColumnCount, result.ColumnCount); - - for (var i = 0; i < result.RowCount; i++) - { - for (var j = 0; j < result.ColumnCount; j++) - { - Assert.AreEqual(result[i, j], i < top.RowCount ? top[i, j] : bottom[i - top.RowCount, j]); - } - } - } - - /// - /// Stacking with a bottom null throws ArgumentNullException. - /// - [Test] - public void StackWithBottomNullThrowsArgumentNullException() - { - var top = TestMatrices["Square3x3"]; - Matrix bottom = null; - var result = CreateMatrix(top.RowCount + top.RowCount, top.ColumnCount); - Assert.Throws(() => top.Stack(bottom, result)); - } - - /// - /// Stacking with a result null throws ArgumentNullException. - /// - [Test] - public void StackWithResultNullThrowsArgumentNullException() - { - var top = TestMatrices["Square3x3"]; - var bottom = TestMatrices["Square3x3"]; - Matrix result = null; - Assert.Throws(() => top.Stack(bottom, result)); - } - - /// - /// Stacking matrices with different columns throws ArgumentException. - /// - [Test] - public void StackMatricesWithDifferentColumnsThrowsArgumentException() - { - var top = TestMatrices["Square3x3"]; - var lower = TestMatrices["Tall3x2"]; - var result = CreateMatrix(top.RowCount + lower.RowCount, top.ColumnCount); - Assert.Throws(() => top.Stack(lower, result)); - } - - /// - /// Stacking with invalid result matrix rows throws ArgumentException. - /// - [Test] - public void StackWithInvalidResultMatrixRowsThrowsArgumentException() - { - var top = TestMatrices["Square3x3"]; - var bottom = TestMatrices["Wide2x3"]; - var result = CreateMatrix(1, 3); - Assert.Throws(() => top.Stack(bottom, result)); - } - - /// - /// Can diagonally stack matrices. - /// - [Test] - public void CanDiagonallyStackMatrices() - { - var top = TestMatrices["Tall3x2"]; - var bottom = TestMatrices["Wide2x3"]; - var result = top.DiagonalStack(bottom); - Assert.AreEqual(top.RowCount + bottom.RowCount, result.RowCount); - Assert.AreEqual(top.ColumnCount + bottom.ColumnCount, result.ColumnCount); - - for (var i = 0; i < result.RowCount; i++) - { - for (var j = 0; j < result.ColumnCount; j++) - { - if (i < top.RowCount && j < top.ColumnCount) - { - Assert.AreEqual(top[i, j], result[i, j]); - } - else if (i >= top.RowCount && j >= top.ColumnCount) - { - Assert.AreEqual(bottom[i - top.RowCount, j - top.ColumnCount], result[i, j]); - } - else - { - Assert.AreEqual(Complex32.Zero, result[i, j]); - } - } - } - } - - /// - /// Diagonal stack with lower null throws ArgumentNullException. - /// - [Test] - public void DiagonalStackWithLowerNullThrowsArgumentNullException() - { - var top = TestMatrices["Square3x3"]; - Matrix lower = null; - Assert.Throws(() => top.DiagonalStack(lower)); - } - - /// - /// Can diagonally stack matrices into a result matrix. - /// - [Test] - public virtual void CanDiagonallyStackMatricesIntoResult() - { - var top = TestMatrices["Tall3x2"]; - var bottom = TestMatrices["Wide2x3"]; - var result = CreateMatrix(top.RowCount + bottom.RowCount, top.ColumnCount + bottom.ColumnCount); - top.DiagonalStack(bottom, result); - Assert.AreEqual(top.RowCount + bottom.RowCount, result.RowCount); - Assert.AreEqual(top.ColumnCount + bottom.ColumnCount, result.ColumnCount); - - for (var i = 0; i < result.RowCount; i++) - { - for (var j = 0; j < result.ColumnCount; j++) - { - if (i < top.RowCount && j < top.ColumnCount) - { - Assert.AreEqual(top[i, j], result[i, j]); - } - else if (i >= top.RowCount && j >= top.ColumnCount) - { - Assert.AreEqual(bottom[i - top.RowCount, j - top.ColumnCount], result[i, j]); - } - else - { - Assert.AreEqual(Complex32.Zero, result[i, j]); - } - } - } - } - - /// - /// Diagonal stack into null result throws ArgumentNullException. - /// - [Test] - public void DiagonalStackIntoResultNullThrowsArgumentNullException() - { - var top = TestMatrices["Square3x3"]; - var lower = TestMatrices["Wide2x3"]; - Matrix result = null; - Assert.Throws(() => top.DiagonalStack(lower, result)); - } - - /// - /// Diagonal stack into invalid result matrix throws ArgumentException. - /// - [Test] - public void DiagonalStackIntoInvalidResultMatrixThrowsArgumentException() - { - var top = TestMatrices["Square3x3"]; - var lower = TestMatrices["Wide2x3"]; - var result = CreateMatrix(top.RowCount + lower.RowCount + 2, top.ColumnCount + lower.ColumnCount); - Assert.Throws(() => top.DiagonalStack(lower, result)); - } - /// /// Can compute Frobenius norm. /// diff --git a/src/UnitTests/LinearAlgebraTests/Double/DiagonalMatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Double/DiagonalMatrixTests.cs index 86cbef04..ee875b20 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/DiagonalMatrixTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/DiagonalMatrixTests.cs @@ -202,38 +202,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double Assert.Throws(() => DiagonalMatrix.Identity(order)); } - /// - /// Can diagonally stack matrices into a result matrix. - /// - public override void CanDiagonallyStackMatricesIntoResult() - { - var top = TestMatrices["Tall3x2"]; - var bottom = TestMatrices["Wide2x3"]; - var result = new SparseMatrix(top.RowCount + bottom.RowCount, top.ColumnCount + bottom.ColumnCount); - top.DiagonalStack(bottom, result); - Assert.AreEqual(top.RowCount + bottom.RowCount, result.RowCount); - Assert.AreEqual(top.ColumnCount + bottom.ColumnCount, result.ColumnCount); - - for (var i = 0; i < result.RowCount; i++) - { - for (var j = 0; j < result.ColumnCount; j++) - { - if (i < top.RowCount && j < top.ColumnCount) - { - Assert.AreEqual(top[i, j], result[i, j]); - } - else if (i >= top.RowCount && j >= top.ColumnCount) - { - Assert.AreEqual(bottom[i - top.RowCount, j - top.ColumnCount], result[i, j]); - } - else - { - Assert.AreEqual(0, result[i, j]); - } - } - } - } - /// /// Can multiply a matrix with matrix. /// diff --git a/src/UnitTests/LinearAlgebraTests/Double/MatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Double/MatrixTests.cs index add92612..b19da409 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/MatrixTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/MatrixTests.cs @@ -125,242 +125,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double } } - /// - /// Can append matrices. - /// - [Test] - public void CanAppendMatrices() - { - var left = CreateMatrix(TestData2D["Singular3x3"]); - var right = CreateMatrix(TestData2D["Tall3x2"]); - var result = left.Append(right); - Assert.AreEqual(left.ColumnCount + right.ColumnCount, result.ColumnCount); - Assert.AreEqual(left.RowCount, right.RowCount); - - for (var i = 0; i < result.RowCount; i++) - { - for (var j = 0; j < result.ColumnCount; j++) - { - Assert.AreEqual(j < left.ColumnCount ? left[i, j] : right[i, j - left.ColumnCount], result[i, j]); - } - } - } - - /// - /// Append with right null throws ArgumentNullException. - /// - [Test] - public void AppendWithRightNullThrowsArgumentNullException() - { - var left = TestMatrices["Square3x3"]; - Matrix right = null; - Assert.Throws(() => left.Append(right)); - } - - /// - /// Append with result null throws ArgumentNullException. - /// - [Test] - public void AppendWithResultNullThrowsArgumentNullException() - { - var left = TestMatrices["Square3x3"]; - var right = TestMatrices["Tall3x2"]; - Matrix result = null; - Assert.Throws(() => left.Append(right, result)); - } - - /// - /// Appending two matrices with different row count throws ArgumentException. - /// - [Test] - public void AppendWithDifferentRowCountThrowsArgumentException() - { - var left = TestMatrices["Square3x3"]; - var right = TestMatrices["Wide2x3"]; - Assert.Throws(() => { var result = left.Append(right); }); - } - - /// - /// Appending with invalid result matrix columns throws ArgumentException. - /// - [Test] - public void AppendWithInvalidResultMatrixColumnsThrowsArgumentException() - { - var left = TestMatrices["Square3x3"]; - var right = TestMatrices["Tall3x2"]; - var result = CreateMatrix(3, 2); - Assert.Throws(() => left.Append(right, result)); - } - - /// - /// Can stack matrices. - /// - [Test] - public void CanStackMatrices() - { - var top = TestMatrices["Square3x3"]; - var bottom = TestMatrices["Wide2x3"]; - var result = top.Stack(bottom); - Assert.AreEqual(top.RowCount + bottom.RowCount, result.RowCount); - Assert.AreEqual(top.ColumnCount, result.ColumnCount); - - for (var i = 0; i < result.RowCount; i++) - { - for (var j = 0; j < result.ColumnCount; j++) - { - Assert.AreEqual(result[i, j], i < top.RowCount ? top[i, j] : bottom[i - top.RowCount, j]); - } - } - } - - /// - /// Stacking with a bottom null throws ArgumentNullException. - /// - [Test] - public void StackWithBottomNullThrowsArgumentNullException() - { - var top = TestMatrices["Square3x3"]; - Matrix bottom = null; - var result = CreateMatrix(top.RowCount + top.RowCount, top.ColumnCount); - Assert.Throws(() => top.Stack(bottom, result)); - } - - /// - /// Stacking with a result null throws ArgumentNullException. - /// - [Test] - public void StackWithResultNullThrowsArgumentNullException() - { - var top = TestMatrices["Square3x3"]; - var bottom = TestMatrices["Square3x3"]; - Matrix result = null; - Assert.Throws(() => top.Stack(bottom, result)); - } - - /// - /// Stacking matrices with different columns throws ArgumentException. - /// - [Test] - public void StackMatricesWithDifferentColumnsThrowsArgumentException() - { - var top = TestMatrices["Square3x3"]; - var lower = TestMatrices["Tall3x2"]; - var result = CreateMatrix(top.RowCount + lower.RowCount, top.ColumnCount); - Assert.Throws(() => top.Stack(lower, result)); - } - - /// - /// Stacking with invalid result matrix rows throws ArgumentException. - /// - [Test] - public void StackWithInvalidResultMatrixRowsThrowsArgumentException() - { - var top = TestMatrices["Square3x3"]; - var bottom = TestMatrices["Wide2x3"]; - var result = CreateMatrix(1, 3); - Assert.Throws(() => top.Stack(bottom, result)); - } - - /// - /// Can diagonally stack matrices. - /// - [Test] - public void CanDiagonallyStackMatrices() - { - var top = TestMatrices["Tall3x2"]; - var bottom = TestMatrices["Wide2x3"]; - var result = top.DiagonalStack(bottom); - Assert.AreEqual(top.RowCount + bottom.RowCount, result.RowCount); - Assert.AreEqual(top.ColumnCount + bottom.ColumnCount, result.ColumnCount); - - for (var i = 0; i < result.RowCount; i++) - { - for (var j = 0; j < result.ColumnCount; j++) - { - if (i < top.RowCount && j < top.ColumnCount) - { - Assert.AreEqual(top[i, j], result[i, j]); - } - else if (i >= top.RowCount && j >= top.ColumnCount) - { - Assert.AreEqual(bottom[i - top.RowCount, j - top.ColumnCount], result[i, j]); - } - else - { - Assert.AreEqual(0, result[i, j]); - } - } - } - } - - /// - /// Diagonal stack with lower null throws ArgumentNullException. - /// - [Test] - public void DiagonalStackWithLowerNullThrowsArgumentNullException() - { - var top = TestMatrices["Square3x3"]; - Matrix lower = null; - Assert.Throws(() => top.DiagonalStack(lower)); - } - - /// - /// Can diagonally stack matrices into a result matrix. - /// - [Test] - public virtual void CanDiagonallyStackMatricesIntoResult() - { - var top = TestMatrices["Tall3x2"]; - var bottom = TestMatrices["Wide2x3"]; - var result = CreateMatrix(top.RowCount + bottom.RowCount, top.ColumnCount + bottom.ColumnCount); - top.DiagonalStack(bottom, result); - Assert.AreEqual(top.RowCount + bottom.RowCount, result.RowCount); - Assert.AreEqual(top.ColumnCount + bottom.ColumnCount, result.ColumnCount); - - for (var i = 0; i < result.RowCount; i++) - { - for (var j = 0; j < result.ColumnCount; j++) - { - if (i < top.RowCount && j < top.ColumnCount) - { - Assert.AreEqual(top[i, j], result[i, j]); - } - else if (i >= top.RowCount && j >= top.ColumnCount) - { - Assert.AreEqual(bottom[i - top.RowCount, j - top.ColumnCount], result[i, j]); - } - else - { - Assert.AreEqual(0, result[i, j]); - } - } - } - } - - /// - /// Diagonal stack into null result throws ArgumentNullException. - /// - [Test] - public void DiagonalStackIntoResultNullThrowsArgumentNullException() - { - var top = TestMatrices["Square3x3"]; - var lower = TestMatrices["Wide2x3"]; - Matrix result = null; - Assert.Throws(() => top.DiagonalStack(lower, result)); - } - - /// - /// Diagonal stack into invalid result matrix throws ArgumentException. - /// - [Test] - public void DiagonalStackIntoInvalidResultMatrixThrowsArgumentException() - { - var top = TestMatrices["Square3x3"]; - var lower = TestMatrices["Wide2x3"]; - var result = CreateMatrix(top.RowCount + lower.RowCount + 2, top.ColumnCount + lower.ColumnCount); - Assert.Throws(() => top.DiagonalStack(lower, result)); - } - /// /// Can compute Frobenius norm. /// diff --git a/src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.Reform.cs b/src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.Reform.cs index 81f5d28b..c941c918 100644 --- a/src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.Reform.cs +++ b/src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.Reform.cs @@ -18,7 +18,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests { m.PermuteRows(permutation); } - catch(InvalidOperationException) + catch (InvalidOperationException) { Assert.Ignore("Matrix type {0} does not support permutations", matrix.GetType().FullName); } @@ -136,5 +136,160 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests Assert.That(() => matrix.InsertColumn(0, CreateVector(matrix.RowCount - 1)), Throws.ArgumentException); Assert.That(() => matrix.InsertColumn(0, CreateVector(matrix.RowCount + 1)), Throws.ArgumentException); } + + [Theory, Timeout(200)] + public void CanAppend(Matrix left, Matrix right) + { + // IF + Assume.That(left.RowCount, Is.EqualTo(right.RowCount)); + + // THEN + var result = left.Append(right); + + Assert.That(result.ColumnCount, Is.EqualTo(left.ColumnCount + right.ColumnCount)); + for (var i = 0; i < result.RowCount; i++) + { + for (var j = 0; j < result.ColumnCount; j++) + { + Assert.That(result[i, j], Is.EqualTo(j < left.ColumnCount ? left[i, j] : right[i, j - left.ColumnCount])); + } + } + + // Invalid + Assert.That(() => left.Append(default(Matrix)), Throws.InstanceOf()); + } + + [Theory, Timeout(200)] + public void CanAppendIntoResult(Matrix left, Matrix right) + { + // IF + Assume.That(left.RowCount, Is.EqualTo(right.RowCount)); + + // THEN + var result = CreateDense(left.RowCount, left.ColumnCount + right.ColumnCount); + left.Append(right, result); + + Assert.That(result.ColumnCount, Is.EqualTo(left.ColumnCount + right.ColumnCount)); + for (var i = 0; i < result.RowCount; i++) + { + for (var j = 0; j < result.ColumnCount; j++) + { + Assert.That(result[i, j], Is.EqualTo(j < left.ColumnCount ? left[i, j] : right[i, j - left.ColumnCount])); + } + } + + // Invalid + Assert.That(() => left.Append(right, default(Matrix)), Throws.InstanceOf()); + Assert.That(() => left.Append(right, CreateDense(left.RowCount + 1, left.ColumnCount + right.ColumnCount)), Throws.ArgumentException); + Assert.That(() => left.Append(right, CreateDense(left.RowCount - 1, left.ColumnCount + right.ColumnCount)), Throws.ArgumentException); + Assert.That(() => left.Append(right, CreateDense(left.RowCount, left.ColumnCount + right.ColumnCount + 1)), Throws.ArgumentException); + Assert.That(() => left.Append(right, CreateDense(left.RowCount, left.ColumnCount + right.ColumnCount - 1)), Throws.ArgumentException); + } + + [Theory, Timeout(200)] + public void CanStack(Matrix top, Matrix bottom) + { + // IF + Assume.That(top.ColumnCount, Is.EqualTo(bottom.ColumnCount)); + + // THEN + var result = top.Stack(bottom); + + Assert.That(result.RowCount, Is.EqualTo(top.RowCount + bottom.RowCount)); + for (var i = 0; i < result.RowCount; i++) + { + for (var j = 0; j < result.ColumnCount; j++) + { + Assert.That(result[i, j], Is.EqualTo(i < top.RowCount ? top[i, j] : bottom[i - top.RowCount, j])); + } + } + + // Invalid + Assert.That(() => top.Stack(default(Matrix)), Throws.InstanceOf()); + } + + [Theory, Timeout(200)] + public void CanStackIntoResult(Matrix top, Matrix bottom) + { + // IF + Assume.That(top.ColumnCount, Is.EqualTo(bottom.ColumnCount)); + + // THEN + var result = CreateDense(top.RowCount + bottom.RowCount, top.ColumnCount); + top.Stack(bottom, result); + + Assert.That(result.RowCount, Is.EqualTo(top.RowCount + bottom.RowCount)); + for (var i = 0; i < result.RowCount; i++) + { + for (var j = 0; j < result.ColumnCount; j++) + { + Assert.That(result[i, j], Is.EqualTo(i < top.RowCount ? top[i, j] : bottom[i - top.RowCount, j])); + } + } + + // Invalid + Assert.That(() => top.Stack(bottom, default(Matrix)), Throws.InstanceOf()); + Assert.That(() => top.Stack(bottom, CreateDense(top.RowCount + bottom.RowCount + 1, top.ColumnCount)), Throws.ArgumentException); + Assert.That(() => top.Stack(bottom, CreateDense(top.RowCount + bottom.RowCount - 1, top.ColumnCount)), Throws.ArgumentException); + Assert.That(() => top.Stack(bottom, CreateDense(top.RowCount + bottom.RowCount, top.ColumnCount + 1)), Throws.ArgumentException); + Assert.That(() => top.Stack(bottom, CreateDense(top.RowCount + bottom.RowCount, top.ColumnCount - 1)), Throws.ArgumentException); + } + + [Theory, Timeout(200)] + public void CanDiagonalStack(Matrix left, Matrix right) + { + var result = left.DiagonalStack(right); + + Assert.That(result.RowCount, Is.EqualTo(left.RowCount + right.RowCount)); + Assert.That(result.ColumnCount, Is.EqualTo(left.ColumnCount + right.ColumnCount)); + for (var i = 0; i < left.RowCount; i++) + { + for (var j = 0; j < left.ColumnCount; j++) + { + Assert.That(result[i, j], Is.EqualTo(left[i, j]), "{0}+{1}->{2}", left.GetType(), right.GetType(), result.GetType()); + } + } + for (var i = 0; i < right.RowCount; i++) + { + for (var j = 0; j < right.ColumnCount; j++) + { + Assert.That(result[left.RowCount + i, left.ColumnCount + j], Is.EqualTo(right[i, j]), "{0}+{1}->{2}", left.GetType(), right.GetType(), result.GetType()); + } + } + + // Invalid + Assert.That(() => left.DiagonalStack(default(Matrix)), Throws.InstanceOf(), "{0}+{1}->{2}", left.GetType(), right.GetType(), result.GetType()); + } + + [Theory, Timeout(200)] + public void CanDiagonalStackIntoResult(Matrix left, Matrix right) + { + var result = CreateDense(left.RowCount + right.RowCount, left.ColumnCount + right.ColumnCount); + left.DiagonalStack(right, result); + + Assert.That(result.RowCount, Is.EqualTo(left.RowCount + right.RowCount)); + Assert.That(result.ColumnCount, Is.EqualTo(left.ColumnCount + right.ColumnCount)); + for (var i = 0; i < left.RowCount; i++) + { + for (var j = 0; j < left.ColumnCount; j++) + { + Assert.That(result[i, j], Is.EqualTo(left[i, j])); + } + } + for (var i = 0; i < right.RowCount; i++) + { + for (var j = 0; j < right.ColumnCount; j++) + { + Assert.That(result[left.RowCount + i, left.ColumnCount + j], Is.EqualTo(right[i, j])); + } + } + + // Invalid + Assert.That(() => left.DiagonalStack(right, default(Matrix)), Throws.InstanceOf()); + Assert.That(() => left.DiagonalStack(right, CreateDense(left.RowCount + right.RowCount + 1, left.ColumnCount + right.ColumnCount)), Throws.ArgumentException); + Assert.That(() => left.DiagonalStack(right, CreateDense(left.RowCount + right.RowCount - 1, left.ColumnCount + right.ColumnCount)), Throws.ArgumentException); + Assert.That(() => left.DiagonalStack(right, CreateDense(left.RowCount + right.RowCount, left.ColumnCount + right.ColumnCount + 1)), Throws.ArgumentException); + Assert.That(() => left.DiagonalStack(right, CreateDense(left.RowCount + right.RowCount, left.ColumnCount + right.ColumnCount - 1)), Throws.ArgumentException); + } } } diff --git a/src/UnitTests/LinearAlgebraTests/Single/DiagonalMatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Single/DiagonalMatrixTests.cs index ae3b1ef2..20a89063 100644 --- a/src/UnitTests/LinearAlgebraTests/Single/DiagonalMatrixTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Single/DiagonalMatrixTests.cs @@ -201,38 +201,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single Assert.Throws(() => DiagonalMatrix.Identity(order)); } - /// - /// Can diagonally stack matrices into a result matrix. - /// - public override void CanDiagonallyStackMatricesIntoResult() - { - var top = TestMatrices["Tall3x2"]; - var bottom = TestMatrices["Wide2x3"]; - var result = new SparseMatrix(top.RowCount + bottom.RowCount, top.ColumnCount + bottom.ColumnCount); - top.DiagonalStack(bottom, result); - Assert.AreEqual(top.RowCount + bottom.RowCount, result.RowCount); - Assert.AreEqual(top.ColumnCount + bottom.ColumnCount, result.ColumnCount); - - for (var i = 0; i < result.RowCount; i++) - { - for (var j = 0; j < result.ColumnCount; j++) - { - if (i < top.RowCount && j < top.ColumnCount) - { - Assert.AreEqual(top[i, j], result[i, j]); - } - else if (i >= top.RowCount && j >= top.ColumnCount) - { - Assert.AreEqual(bottom[i - top.RowCount, j - top.ColumnCount], result[i, j]); - } - else - { - Assert.AreEqual(0, result[i, j]); - } - } - } - } - /// /// Can multiply a matrix with matrix. /// diff --git a/src/UnitTests/LinearAlgebraTests/Single/MatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Single/MatrixTests.cs index 56e98ad9..27d63e7c 100644 --- a/src/UnitTests/LinearAlgebraTests/Single/MatrixTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Single/MatrixTests.cs @@ -125,242 +125,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single } } - /// - /// Can append matrices. - /// - [Test] - public void CanAppendMatrices() - { - var left = CreateMatrix(TestData2D["Singular3x3"]); - var right = CreateMatrix(TestData2D["Tall3x2"]); - var result = left.Append(right); - Assert.AreEqual(left.ColumnCount + right.ColumnCount, result.ColumnCount); - Assert.AreEqual(left.RowCount, right.RowCount); - - for (var i = 0; i < result.RowCount; i++) - { - for (var j = 0; j < result.ColumnCount; j++) - { - Assert.AreEqual(j < left.ColumnCount ? left[i, j] : right[i, j - left.ColumnCount], result[i, j]); - } - } - } - - /// - /// Append with right null throws ArgumentNullException. - /// - [Test] - public void AppendWithRightNullThrowsArgumentNullException() - { - var left = TestMatrices["Square3x3"]; - Matrix right = null; - Assert.Throws(() => left.Append(right)); - } - - /// - /// Append with result null throws ArgumentNullException. - /// - [Test] - public void AppendWithResultNullThrowsArgumentNullException() - { - var left = TestMatrices["Square3x3"]; - var right = TestMatrices["Tall3x2"]; - Matrix result = null; - Assert.Throws(() => left.Append(right, result)); - } - - /// - /// Appending two matrices with different row count throws ArgumentException. - /// - [Test] - public void AppendWithDifferentRowCountThrowsArgumentException() - { - var left = TestMatrices["Square3x3"]; - var right = TestMatrices["Wide2x3"]; - Assert.Throws(() => { var result = left.Append(right); }); - } - - /// - /// Appending with invalid result matrix columns throws ArgumentException. - /// - [Test] - public void AppendWithInvalidResultMatrixColumnsThrowsArgumentException() - { - var left = TestMatrices["Square3x3"]; - var right = TestMatrices["Tall3x2"]; - var result = CreateMatrix(3, 2); - Assert.Throws(() => left.Append(right, result)); - } - - /// - /// Can stack matrices. - /// - [Test] - public void CanStackMatrices() - { - var top = TestMatrices["Square3x3"]; - var bottom = TestMatrices["Wide2x3"]; - var result = top.Stack(bottom); - Assert.AreEqual(top.RowCount + bottom.RowCount, result.RowCount); - Assert.AreEqual(top.ColumnCount, result.ColumnCount); - - for (var i = 0; i < result.RowCount; i++) - { - for (var j = 0; j < result.ColumnCount; j++) - { - Assert.AreEqual(result[i, j], i < top.RowCount ? top[i, j] : bottom[i - top.RowCount, j]); - } - } - } - - /// - /// Stacking with a bottom null throws ArgumentNullException. - /// - [Test] - public void StackWithBottomNullThrowsArgumentNullException() - { - var top = TestMatrices["Square3x3"]; - Matrix bottom = null; - var result = CreateMatrix(top.RowCount + top.RowCount, top.ColumnCount); - Assert.Throws(() => top.Stack(bottom, result)); - } - - /// - /// Stacking with a result null throws ArgumentNullException. - /// - [Test] - public void StackWithResultNullThrowsArgumentNullException() - { - var top = TestMatrices["Square3x3"]; - var bottom = TestMatrices["Square3x3"]; - Matrix result = null; - Assert.Throws(() => top.Stack(bottom, result)); - } - - /// - /// Stacking matrices with different columns throws ArgumentException. - /// - [Test] - public void StackMatricesWithDifferentColumnsThrowsArgumentException() - { - var top = TestMatrices["Square3x3"]; - var lower = TestMatrices["Tall3x2"]; - var result = CreateMatrix(top.RowCount + lower.RowCount, top.ColumnCount); - Assert.Throws(() => top.Stack(lower, result)); - } - - /// - /// Stacking with invalid result matrix rows throws ArgumentException. - /// - [Test] - public void StackWithInvalidResultMatrixRowsThrowsArgumentException() - { - var top = TestMatrices["Square3x3"]; - var bottom = TestMatrices["Wide2x3"]; - var result = CreateMatrix(1, 3); - Assert.Throws(() => top.Stack(bottom, result)); - } - - /// - /// Can diagonally stack matrices. - /// - [Test] - public void CanDiagonallyStackMatrices() - { - var top = TestMatrices["Tall3x2"]; - var bottom = TestMatrices["Wide2x3"]; - var result = top.DiagonalStack(bottom); - Assert.AreEqual(top.RowCount + bottom.RowCount, result.RowCount); - Assert.AreEqual(top.ColumnCount + bottom.ColumnCount, result.ColumnCount); - - for (var i = 0; i < result.RowCount; i++) - { - for (var j = 0; j < result.ColumnCount; j++) - { - if (i < top.RowCount && j < top.ColumnCount) - { - Assert.AreEqual(top[i, j], result[i, j]); - } - else if (i >= top.RowCount && j >= top.ColumnCount) - { - Assert.AreEqual(bottom[i - top.RowCount, j - top.ColumnCount], result[i, j]); - } - else - { - Assert.AreEqual(0, result[i, j]); - } - } - } - } - - /// - /// Diagonal stack with lower null throws ArgumentNullException. - /// - [Test] - public void DiagonalStackWithLowerNullThrowsArgumentNullException() - { - var top = TestMatrices["Square3x3"]; - Matrix lower = null; - Assert.Throws(() => top.DiagonalStack(lower)); - } - - /// - /// Can diagonally stack matrices into a result matrix. - /// - [Test] - public virtual void CanDiagonallyStackMatricesIntoResult() - { - var top = TestMatrices["Tall3x2"]; - var bottom = TestMatrices["Wide2x3"]; - var result = CreateMatrix(top.RowCount + bottom.RowCount, top.ColumnCount + bottom.ColumnCount); - top.DiagonalStack(bottom, result); - Assert.AreEqual(top.RowCount + bottom.RowCount, result.RowCount); - Assert.AreEqual(top.ColumnCount + bottom.ColumnCount, result.ColumnCount); - - for (var i = 0; i < result.RowCount; i++) - { - for (var j = 0; j < result.ColumnCount; j++) - { - if (i < top.RowCount && j < top.ColumnCount) - { - Assert.AreEqual(top[i, j], result[i, j]); - } - else if (i >= top.RowCount && j >= top.ColumnCount) - { - Assert.AreEqual(bottom[i - top.RowCount, j - top.ColumnCount], result[i, j]); - } - else - { - Assert.AreEqual(0, result[i, j]); - } - } - } - } - - /// - /// Diagonal stack into null result throws ArgumentNullException. - /// - [Test] - public void DiagonalStackIntoResultNullThrowsArgumentNullException() - { - var top = TestMatrices["Square3x3"]; - var lower = TestMatrices["Wide2x3"]; - Matrix result = null; - Assert.Throws(() => top.DiagonalStack(lower, result)); - } - - /// - /// Diagonal stack into invalid result matrix throws ArgumentException. - /// - [Test] - public void DiagonalStackIntoInvalidResultMatrixThrowsArgumentException() - { - var top = TestMatrices["Square3x3"]; - var lower = TestMatrices["Wide2x3"]; - var result = CreateMatrix(top.RowCount + lower.RowCount + 2, top.ColumnCount + lower.ColumnCount); - Assert.Throws(() => top.DiagonalStack(lower, result)); - } - /// /// Can compute Frobenius norm. ///