From de5d235f6fd7d83c06554d85c25685aeb1b941b3 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sat, 4 Aug 2012 00:23:00 +0200 Subject: [PATCH] Tests: replace matrix structural tests with theory (part 2) - Row --- .../LinearAlgebra/Complex/DiagonalMatrix.cs | 12 ++-- .../LinearAlgebra/Complex/SparseMatrix.cs | 6 +- .../LinearAlgebra/Complex32/DiagonalMatrix.cs | 12 ++-- .../LinearAlgebra/Complex32/SparseMatrix.cs | 6 +- .../LinearAlgebra/Double/DiagonalMatrix.cs | 12 ++-- .../LinearAlgebra/Double/SparseMatrix.cs | 6 +- src/Numerics/LinearAlgebra/Generic/Matrix.cs | 16 ++--- src/Numerics/LinearAlgebra/Generic/Vector.cs | 6 +- .../LinearAlgebra/Single/DiagonalMatrix.cs | 12 ++-- .../LinearAlgebra/Single/SparseMatrix.cs | 6 +- .../LinearAlgebraTests/Complex/MatrixTests.cs | 34 --------- .../Complex32/MatrixTests.cs | 34 --------- .../LinearAlgebraTests/Double/MatrixTests.cs | 34 --------- .../MatrixStructureTheory.cs | 72 +++++++++++++++++-- .../LinearAlgebraTests/Single/MatrixTests.cs | 45 ------------ 15 files changed, 114 insertions(+), 199 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs index 3ef8717d..18f1159e 100644 --- a/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs @@ -747,7 +747,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// or greater than or equal to the number of rows. /// If + /// is greater than or equal to the number of rows. - /// If is not positive. + /// If is not positive. /// If result.Count < length. public override void Column(int columnIndex, int rowIndex, int length, Vector result) { @@ -773,12 +773,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex if (length < 1) { - throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); + throw new ArgumentOutOfRangeException("length", Resources.ArgumentMustBePositive); } if (result.Count < length) { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); + throw new ArgumentOutOfRangeException("result", Resources.ArgumentVectorsSameLength); } // Clear the result and copy the diagonal entry. @@ -803,7 +803,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// or greater than or equal to the number of rows. /// If + /// is greater than or equal to the number of rows. - /// If is not positive. + /// If is not positive. /// If result.Count < length. public override void Row(int rowIndex, int columnIndex, int length, Vector result) { @@ -829,12 +829,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex if (length < 1) { - throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); + throw new ArgumentOutOfRangeException("length", Resources.ArgumentMustBePositive); } if (result.Count < length) { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); + throw new ArgumentOutOfRangeException("result", Resources.ArgumentVectorsSameLength); } // Clear the result and copy the diagonal entry. diff --git a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs index 600c3b0e..97e6faf9 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs @@ -756,7 +756,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// or greater than or equal to the number of rows. /// If + /// is greater than or equal to the number of rows. - /// If is not positive. + /// If is not positive. /// If result.Count < length. public override void Row(int rowIndex, int columnIndex, int length, Vector result) { @@ -782,12 +782,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex if (length < 1) { - throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); + throw new ArgumentOutOfRangeException("length", Resources.ArgumentMustBePositive); } if (result.Count < length) { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); + throw new ArgumentOutOfRangeException("result", Resources.ArgumentVectorsSameLength); } var rowPointers = _storage.RowPointers; diff --git a/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs index 44b9573e..96fa37e1 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs @@ -747,7 +747,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// or greater than or equal to the number of rows. /// If + /// is greater than or equal to the number of rows. - /// If is not positive. + /// If is not positive. /// If result.Count < length. public override void Column(int columnIndex, int rowIndex, int length, Vector result) { @@ -773,12 +773,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 if (length < 1) { - throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); + throw new ArgumentOutOfRangeException("length", Resources.ArgumentMustBePositive); } if (result.Count < length) { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); + throw new ArgumentOutOfRangeException("result", Resources.ArgumentVectorsSameLength); } // Clear the result and copy the diagonal entry. @@ -803,7 +803,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// or greater than or equal to the number of rows. /// If + /// is greater than or equal to the number of rows. - /// If is not positive. + /// If is not positive. /// If result.Count < length. public override void Row(int rowIndex, int columnIndex, int length, Vector result) { @@ -829,12 +829,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 if (length < 1) { - throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); + throw new ArgumentOutOfRangeException("length", Resources.ArgumentMustBePositive); } if (result.Count < length) { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); + throw new ArgumentOutOfRangeException("result", Resources.ArgumentVectorsSameLength); } // Clear the result and copy the diagonal entry. diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs index bb5448b5..b6674171 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs @@ -756,7 +756,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// or greater than or equal to the number of rows. /// If + /// is greater than or equal to the number of rows. - /// If is not positive. + /// If is not positive. /// If result.Count < length. public override void Row(int rowIndex, int columnIndex, int length, Vector result) { @@ -782,12 +782,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 if (length < 1) { - throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); + throw new ArgumentOutOfRangeException("length", Resources.ArgumentMustBePositive); } if (result.Count < length) { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); + throw new ArgumentOutOfRangeException("result", Resources.ArgumentVectorsSameLength); } var rowPointers = _storage.RowPointers; diff --git a/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs index eefc3637..bc90bc25 100644 --- a/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs @@ -741,7 +741,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// or greater than or equal to the number of rows. /// If + /// is greater than or equal to the number of rows. - /// If is not positive. + /// If is not positive. /// If result.Count < length. public override void Column(int columnIndex, int rowIndex, int length, Vector result) { @@ -767,12 +767,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double if (length < 1) { - throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); + throw new ArgumentOutOfRangeException("length", Resources.ArgumentMustBePositive); } if (result.Count < length) { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); + throw new ArgumentOutOfRangeException("result", Resources.ArgumentVectorsSameLength); } // Clear the result and copy the diagonal entry. @@ -797,7 +797,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// or greater than or equal to the number of rows. /// If + /// is greater than or equal to the number of rows. - /// If is not positive. + /// If is not positive. /// If result.Count < length. public override void Row(int rowIndex, int columnIndex, int length, Vector result) { @@ -823,12 +823,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double if (length < 1) { - throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); + throw new ArgumentOutOfRangeException("length", Resources.ArgumentMustBePositive); } if (result.Count < length) { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); + throw new ArgumentOutOfRangeException("result", Resources.ArgumentVectorsSameLength); } // Clear the result and copy the diagonal entry. diff --git a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs index 858e2642..c9bb7a29 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs @@ -754,7 +754,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// or greater than or equal to the number of rows. /// If + /// is greater than or equal to the number of rows. - /// If is not positive. + /// If is not positive. /// If result.Count < length. public override void Row(int rowIndex, int columnIndex, int length, Vector result) { @@ -780,12 +780,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double if (length < 1) { - throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); + throw new ArgumentOutOfRangeException("length", Resources.ArgumentMustBePositive); } if (result.Count < length) { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); + throw new ArgumentOutOfRangeException("result", Resources.ArgumentVectorsSameLength); } var rowPointers = _storage.RowPointers; diff --git a/src/Numerics/LinearAlgebra/Generic/Matrix.cs b/src/Numerics/LinearAlgebra/Generic/Matrix.cs index cef3cd26..2d81173c 100644 --- a/src/Numerics/LinearAlgebra/Generic/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Generic/Matrix.cs @@ -339,7 +339,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// is negative, /// or greater than or equal to the number of columns. /// (columnIndex + length) >= Columns. - /// If is not positive. + /// If is not positive. public virtual Vector Row(int rowIndex, int columnIndex, int length) { var ret = CreateVector(length); @@ -361,7 +361,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// or greater than or equal to the number of rows. /// If + /// is greater than or equal to the number of rows. - /// If is not positive. + /// If is not positive. /// If result.Count < length. public virtual void Row(int rowIndex, int columnIndex, int length, Vector result) { @@ -387,12 +387,12 @@ namespace MathNet.Numerics.LinearAlgebra.Generic if (length < 1) { - throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); + throw new ArgumentOutOfRangeException("length", Resources.ArgumentMustBePositive); } if (result.Count < length) { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); + throw new ArgumentOutOfRangeException("result", Resources.ArgumentVectorsSameLength); } for (int i = columnIndex, j = 0; i < columnIndex + length; i++, j++) @@ -443,7 +443,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// or greater than or equal to the number of rows. /// (rowIndex + length) >= Rows. /// - /// If is not positive. + /// If is not positive. public virtual Vector Column(int columnIndex, int rowIndex, int length) { var result = CreateVector(length); @@ -465,7 +465,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// or greater than or equal to the number of rows. /// If + /// is greater than or equal to the number of rows. - /// If is not positive. + /// If is not positive. /// If result.Count < length. public virtual void Column(int columnIndex, int rowIndex, int length, Vector result) { @@ -491,12 +491,12 @@ namespace MathNet.Numerics.LinearAlgebra.Generic if (length < 1) { - throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); + throw new ArgumentOutOfRangeException("length", Resources.ArgumentMustBePositive); } if (result.Count < length) { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); + throw new ArgumentOutOfRangeException("result", Resources.ArgumentVectorsSameLength); } for (int i = rowIndex, j = 0; i < rowIndex + length; i++, j++) diff --git a/src/Numerics/LinearAlgebra/Generic/Vector.cs b/src/Numerics/LinearAlgebra/Generic/Vector.cs index 9a005a19..0ab5100b 100644 --- a/src/Numerics/LinearAlgebra/Generic/Vector.cs +++ b/src/Numerics/LinearAlgebra/Generic/Vector.cs @@ -66,14 +66,14 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// /// The size of the Vector to construct. /// - /// + /// /// If is less than one. /// protected Vector(int size) { if (size < 1) { - throw new ArgumentException(Resources.ArgumentMustBePositive, "size"); + throw new ArgumentOutOfRangeException("size", Resources.ArgumentMustBePositive); } Count = size; @@ -91,7 +91,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// Gets or sets the value at the given . /// The index of the value to get or set. /// The value of the vector at the given . - /// If is negative or + /// If is negative or /// greater than the size of the vector. public virtual T this[int index] { diff --git a/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs index 2ae82101..2722ed91 100644 --- a/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs @@ -741,7 +741,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// or greater than or equal to the number of rows. /// If + /// is greater than or equal to the number of rows. - /// If is not positive. + /// If is not positive. /// If result.Count < length. public override void Column(int columnIndex, int rowIndex, int length, Vector result) { @@ -767,12 +767,12 @@ namespace MathNet.Numerics.LinearAlgebra.Single if (length < 1) { - throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); + throw new ArgumentOutOfRangeException("length", Resources.ArgumentMustBePositive); } if (result.Count < length) { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); + throw new ArgumentOutOfRangeException("result", Resources.ArgumentVectorsSameLength); } // Clear the result and copy the diagonal entry. @@ -797,7 +797,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// or greater than or equal to the number of rows. /// If + /// is greater than or equal to the number of rows. - /// If is not positive. + /// If is not positive. /// If result.Count < length. public override void Row(int rowIndex, int columnIndex, int length, Vector result) { @@ -823,12 +823,12 @@ namespace MathNet.Numerics.LinearAlgebra.Single if (length < 1) { - throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); + throw new ArgumentOutOfRangeException("length", Resources.ArgumentMustBePositive); } if (result.Count < length) { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); + throw new ArgumentOutOfRangeException("result", Resources.ArgumentVectorsSameLength); } // Clear the result and copy the diagonal entry. diff --git a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs index 53660deb..153ef3bc 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs @@ -754,7 +754,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// or greater than or equal to the number of rows. /// If + /// is greater than or equal to the number of rows. - /// If is not positive. + /// If is not positive. /// If result.Count < length. public override void Row(int rowIndex, int columnIndex, int length, Vector result) { @@ -780,12 +780,12 @@ namespace MathNet.Numerics.LinearAlgebra.Single if (length < 1) { - throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); + throw new ArgumentOutOfRangeException("length", Resources.ArgumentMustBePositive); } if (result.Count < length) { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); + throw new ArgumentOutOfRangeException("result", Resources.ArgumentVectorsSameLength); } var rowPointers = _storage.RowPointers; diff --git a/src/UnitTests/LinearAlgebraTests/Complex/MatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Complex/MatrixTests.cs index 1795de5e..dc1ff902 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex/MatrixTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex/MatrixTests.cs @@ -36,40 +36,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex /// public abstract partial class MatrixTests : MatrixLoader { - /// - /// Can get a row at specific start position and length of a matrix into a vector. - /// - /// Row index. - /// Column start. - /// Row length. - /// Matrix name. - [TestCase(0, 0, 1, "Singular3x3")] - [TestCase(1, 1, 2, "Singular3x3")] - [TestCase(2, 0, 3, "Singular3x3")] - [TestCase(2, 0, 3, "Square3x3")] - public void CanGetRowWithRange(int rowIndex, int start, int length, string name) - { - var matrix = TestMatrices[name]; - var row = matrix.Row(rowIndex, start, length); - - Assert.AreEqual(length, row.Count); - for (var j = start; j < start + length; j++) - { - Assert.AreEqual(matrix[rowIndex, j], row[j - start]); - } - } - - /// - /// Get a row of a matrix at specific start position and zero length throws ArgumentException. - /// - [Test] - public void GetRowWithRangeIntoResultWhenLengthIsZeroThrowsArgumentException() - { - var matrix = TestMatrices["Singular3x3"]; - var result = CreateVector(matrix.ColumnCount); - Assert.Throws(() => matrix.Row(0, 0, 0, result)); - } - /// /// Get a row at specific start position and length of a matrix into a too small vector throws ArgumentException. /// diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/MatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Complex32/MatrixTests.cs index f5989d88..e5855c87 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex32/MatrixTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex32/MatrixTests.cs @@ -36,40 +36,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 /// public abstract partial class MatrixTests : MatrixLoader { - /// - /// Can get a row at specific start position and length of a matrix into a vector. - /// - /// Row index. - /// Column start. - /// Row length. - /// Matrix name. - [TestCase(0, 0, 1, "Singular3x3")] - [TestCase(1, 1, 2, "Singular3x3")] - [TestCase(2, 0, 3, "Singular3x3")] - [TestCase(2, 0, 3, "Square3x3")] - public void CanGetRowWithRange(int rowIndex, int start, int length, string name) - { - var matrix = TestMatrices[name]; - var row = matrix.Row(rowIndex, start, length); - - Assert.AreEqual(length, row.Count); - for (var j = start; j < start + length; j++) - { - Assert.AreEqual(matrix[rowIndex, j], row[j - start]); - } - } - - /// - /// Get a row of a matrix at specific start position and zero length throws ArgumentException. - /// - [Test] - public void GetRowWithRangeIntoResultWhenLengthIsZeroThrowsArgumentException() - { - var matrix = TestMatrices["Singular3x3"]; - var result = CreateVector(matrix.ColumnCount); - Assert.Throws(() => matrix.Row(0, 0, 0, result)); - } - /// /// Get a row at specific start position and length of a matrix into a too small vector throws ArgumentException. /// diff --git a/src/UnitTests/LinearAlgebraTests/Double/MatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Double/MatrixTests.cs index 92b4c458..19a6e83a 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/MatrixTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/MatrixTests.cs @@ -35,40 +35,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double /// public abstract partial class MatrixTests : MatrixLoader { - /// - /// Can get a row at specific start position and length of a matrix into a vector. - /// - /// Row index. - /// Column start. - /// Row length. - /// Matrix name. - [TestCase(0, 0, 1, "Singular3x3")] - [TestCase(1, 1, 2, "Singular3x3")] - [TestCase(2, 0, 3, "Singular3x3")] - [TestCase(2, 0, 3, "Square3x3")] - public void CanGetRowWithRange(int rowIndex, int start, int length, string name) - { - var matrix = TestMatrices[name]; - var row = matrix.Row(rowIndex, start, length); - - Assert.AreEqual(length, row.Count); - for (var j = start; j < start + length; j++) - { - Assert.AreEqual(matrix[rowIndex, j], row[j - start]); - } - } - - /// - /// Get a row of a matrix at specific start position and zero length throws ArgumentException. - /// - [Test] - public void GetRowWithRangeIntoResultWhenLengthIsZeroThrowsArgumentException() - { - var matrix = TestMatrices["Singular3x3"]; - var result = CreateVector(matrix.ColumnCount); - Assert.Throws(() => matrix.Row(0, 0, 0, result)); - } - /// /// Get a row at specific start position and length of a matrix into a too small vector throws ArgumentException. /// diff --git a/src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.cs b/src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.cs index 1f81a120..30effde4 100644 --- a/src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.cs +++ b/src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.cs @@ -126,16 +126,25 @@ } [Theory, Timeout(200)] - public void CanGetRow(Matrix matrix, [Values(0, 1, 1000)]int rowIndex) + public void CanGetRow(Matrix matrix) { - var actualRow = Math.Min(rowIndex, matrix.RowCount - 1); - var row = matrix.Row(actualRow); - Assert.That(row.Count, Is.EqualTo(matrix.ColumnCount)); + // First Row + var firstrow = matrix.Row(0); + Assert.That(firstrow.Count, Is.EqualTo(matrix.ColumnCount)); for (var j = 0; j < matrix.ColumnCount; j++) { - Assert.AreEqual(matrix[actualRow, j], row[j]); + Assert.AreEqual(matrix[0, j], firstrow[j]); } + // Last Row + var lastrow = matrix.Row(matrix.RowCount - 1); + Assert.That(lastrow.Count, Is.EqualTo(matrix.ColumnCount)); + for (var j = 0; j < matrix.ColumnCount; j++) + { + Assert.AreEqual(matrix[matrix.RowCount - 1, j], lastrow[j]); + } + + // Invalid Rows Assert.That(() => { matrix.Row(-1); }, Throws.InstanceOf()); Assert.That(() => { matrix.Row(matrix.RowCount); }, Throws.InstanceOf()); } @@ -155,5 +164,58 @@ Assert.That(() => matrix.Row(-1, row), Throws.InstanceOf()); Assert.That(() => matrix.Row(matrix.RowCount, row), Throws.InstanceOf()); } + + [Theory, Timeout(200)] + public virtual void CanGetRowWithRange(Matrix matrix) + { + // First Row, Columns 0..1 + var firstrow = matrix.Row(0, 0, 2); + Assert.That(firstrow.Count, Is.EqualTo(2)); + for (var j = 0; j < 2; j++) + { + Assert.AreEqual(matrix[0, j], firstrow[j]); + } + + // Second Row, Full Columns + var secondrow = matrix.Row(1, 0, matrix.ColumnCount); + Assert.That(secondrow.Count, Is.EqualTo(matrix.ColumnCount)); + for (var j = 0; j < matrix.ColumnCount; j++) + { + Assert.AreEqual(matrix[1, j], secondrow[j]); + } + + // Last Row, Columns 1 + var lastrow = matrix.Row(matrix.RowCount - 1, 1, 1); + Assert.That(lastrow.Count, Is.EqualTo(1)); + for (var j = 0; j < 1; j++) + { + Assert.AreEqual(matrix[matrix.RowCount - 1, j + 1], lastrow[j]); + } + + // Invalid Rows + Assert.That(() => { matrix.Row(-1, 0, 2); }, Throws.InstanceOf()); + Assert.That(() => { matrix.Row(matrix.RowCount, 0, 1); }, Throws.InstanceOf()); + Assert.That(() => { matrix.Row(0, -1, 1); }, Throws.InstanceOf()); + Assert.That(() => { matrix.Row(0, 1, 0); }, Throws.InstanceOf()); + Assert.That(() => { matrix.Row(0, 0, matrix.ColumnCount + 1); }, Throws.InstanceOf()); + } + + [Theory, Timeout(200)] + public void CanGetRowWithRangeIntoResult(Matrix matrix) + { + var row = CreateVector(matrix.ColumnCount - 1); + matrix.Row(0, 1, matrix.ColumnCount - 1, row); + + for (var j = 0; j < matrix.ColumnCount - 1; j++) + { + Assert.AreEqual(matrix[0, j + 1], row[j]); + } + + Assert.That(() => matrix.Row(0, 0, matrix.ColumnCount - 1, null), Throws.InstanceOf()); + Assert.That(() => matrix.Row(-1, 0, matrix.ColumnCount - 1, row), Throws.InstanceOf()); + Assert.That(() => matrix.Row(matrix.RowCount, 0, matrix.ColumnCount - 1, row), Throws.InstanceOf()); + Assert.That(() => matrix.Row(0, 0, matrix.ColumnCount, row), Throws.InstanceOf()); + } + } } diff --git a/src/UnitTests/LinearAlgebraTests/Single/MatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Single/MatrixTests.cs index 9843faf4..a1c91ddd 100644 --- a/src/UnitTests/LinearAlgebraTests/Single/MatrixTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Single/MatrixTests.cs @@ -35,51 +35,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single /// public abstract partial class MatrixTests : MatrixLoader { - /// - /// Can get a row at specific start position and length of a matrix into a vector. - /// - /// Row index. - /// Column start. - /// Row length. - /// Matrix name. - [TestCase(0, 0, 1, "Singular3x3")] - [TestCase(1, 1, 2, "Singular3x3")] - [TestCase(2, 0, 3, "Singular3x3")] - [TestCase(2, 0, 3, "Square3x3")] - public void CanGetRowWithRange(int rowIndex, int start, int length, string name) - { - var matrix = TestMatrices[name]; - var row = matrix.Row(rowIndex, start, length); - - Assert.AreEqual(length, row.Count); - for (var j = start; j < start + length; j++) - { - Assert.AreEqual(matrix[rowIndex, j], row[j - start]); - } - } - - /// - /// Get a row of a matrix at specific start position and zero length throws ArgumentException. - /// - [Test] - public void GetRowWithRangeIntoResultWhenLengthIsZeroThrowsArgumentException() - { - var matrix = TestMatrices["Singular3x3"]; - var result = CreateVector(matrix.ColumnCount); - Assert.Throws(() => matrix.Row(0, 0, 0, result)); - } - - /// - /// Get a row at specific start position and length of a matrix into a too small vector throws ArgumentException. - /// - [Test] - public void GetRowWithRangeIntoTooSmallResultVectorThrowsArgumentException() - { - var matrix = TestMatrices["Singular3x3"]; - var result = CreateVector(matrix.ColumnCount - 1); - Assert.Throws(() => matrix.Row(0, 0, 0, result)); - } - /// /// Can get a column of a matrix. ///