From 02526e814d571ba45899ca75d16167989eea7c1f Mon Sep 17 00:00:00 2001 From: Alexander Karatarakis Date: Sat, 24 Nov 2012 20:54:24 +0200 Subject: [PATCH] Fixes from Unit Tests Signed-off-by: Alexander Karatarakis --- .../Double/SymmetricDenseMatrix.cs | 55 +++++++------------ .../Storage/SymmetricMatrixStorage.cs | 12 ++++ .../Double/SymmetricDenseMatrixTests.cs | 33 ++++++++++- 3 files changed, 61 insertions(+), 39 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Double/SymmetricDenseMatrix.cs b/src/Numerics/LinearAlgebra/Double/SymmetricDenseMatrix.cs index e34219f5..2dc98577 100644 --- a/src/Numerics/LinearAlgebra/Double/SymmetricDenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/SymmetricDenseMatrix.cs @@ -46,20 +46,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double { readonly DenseColumnMajorSymmetricMatrixStorage _storage; - /// - /// Number of rows. - /// - /// Using this instead of the RowCount property to speed up calculating - /// a matrix index in the data array. - readonly int _rowCount; - - /// - /// Number of columns. - /// - /// Using this instead of the ColumnCount property to speed up calculating - /// a matrix index in the data array. - readonly int _columnCount; - /// /// Gets the matrix's data. /// @@ -70,8 +56,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double : base(storage) { _storage = storage; - _rowCount = _storage.RowCount; - _columnCount = _storage.ColumnCount; _data = _storage.Data; } @@ -99,9 +83,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double public SymmetricDenseMatrix(int order, double value) : this(order) { - for (var i = 0; i < Data.Length; i++) + for (var i = 0; i < _data.Length; i++) { - Data[i] = value; + _data[i] = value; } } @@ -145,7 +129,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double { for (var column = row; column < Order; column++) { - Data[indexer.Of(row, column)] = array[row, column]; + _data[indexer.Of(row, column)] = array[row, column]; } } } @@ -178,7 +162,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double { for (var column = row; column < Order; column++) { - Data[indexer.Of(row, column)] = matrix[row, column]; + _data[indexer.Of(row, column)] = matrix[row, column]; } } } @@ -189,13 +173,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double } /// - /// Gets the matrix's data in array format. + /// Gets the matrix's data. /// - /// The matrix's raw data. + /// The matrix's data. public double[] Data { - get; - private set; + get { return _data; } } /// @@ -279,7 +262,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double } else { - Control.LinearAlgebraProvider.AddArrays(Data, denseOther.Data, denseResult.Data); + Control.LinearAlgebraProvider.AddArrays(_data, denseOther._data, denseResult._data); } } @@ -298,7 +281,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double } else { - Control.LinearAlgebraProvider.SubtractArrays(Data, denseOther.Data, denseResult.Data); + Control.LinearAlgebraProvider.SubtractArrays(_data, denseOther._data, denseResult._data); } } @@ -316,7 +299,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double } else { - Control.LinearAlgebraProvider.ScaleArray(scalar, Data, denseResult.Data); + Control.LinearAlgebraProvider.ScaleArray(scalar, _data, denseResult._data); } } @@ -397,7 +380,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double } else { - Control.LinearAlgebraProvider.ScaleArray(-1, Data, denseResult.Data); + Control.LinearAlgebraProvider.ScaleArray(-1, _data, denseResult._data); } } @@ -417,7 +400,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double } else { - Control.LinearAlgebraProvider.PointWiseMultiplyArrays(Data, denseOther.Data, denseResult.Data); + Control.LinearAlgebraProvider.PointWiseMultiplyArrays(_data, denseOther._data, denseResult._data); } } @@ -437,7 +420,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double } else { - Control.LinearAlgebraProvider.PointWiseDivideArrays(Data, denseOther.Data, denseResult.Data); + Control.LinearAlgebraProvider.PointWiseDivideArrays(_data, denseOther._data, denseResult._data); } } @@ -537,8 +520,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double CommonParallel.For( 0, - Data.Length, - index => denseResult.Data[index] %= divisor); + _data.Length, + index => denseResult._data[index] %= divisor); } } @@ -578,9 +561,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double } else { - for (var i = 0; i < denseMatrix.Data.Length; i++) + for (var i = 0; i < denseMatrix._data.Length; i++) { - denseMatrix.Data[i] = distribution.Sample(); + denseMatrix._data[i] = distribution.Sample(); } } } @@ -600,9 +583,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double } else { - for (var i = 0; i < denseMatrix.Data.Length; i++) + for (var i = 0; i < denseMatrix._data.Length; i++) { - denseMatrix.Data[i] = distribution.Sample(); + denseMatrix._data[i] = distribution.Sample(); } } } diff --git a/src/Numerics/LinearAlgebra/Storage/SymmetricMatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/SymmetricMatrixStorage.cs index 8ff1a641..744c286b 100644 --- a/src/Numerics/LinearAlgebra/Storage/SymmetricMatrixStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/SymmetricMatrixStorage.cs @@ -48,5 +48,17 @@ namespace MathNet.Numerics.LinearAlgebra.Storage } } } + + /// Parameters assumed to be validated already. + public override void CopyTo(MatrixStorage target, bool skipClearing = false) + { + for (int j = 0; j < ColumnCount; j++) + { + for (int i = 0; i <= j; i++) + { + target.At(i, j, At(i, j)); + } + } + } } } diff --git a/src/UnitTests/LinearAlgebraTests/Double/SymmetricDenseMatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Double/SymmetricDenseMatrixTests.cs index 53896f67..50bd316b 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/SymmetricDenseMatrixTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/SymmetricDenseMatrixTests.cs @@ -26,6 +26,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double { + using System.Collections.Generic; + using MathNet.Numerics.LinearAlgebra.Double; using NUnit.Framework; @@ -63,9 +65,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double /// protected override Matrix CreateMatrix(double[,] data) { - return SymmetricMatrix.CheckIfSymmetric(data) - ? (Matrix)new SymmetricDenseMatrix(data) - : new DenseMatrix(data); + if (SymmetricMatrix.CheckIfSymmetric(data)) + { + return new SymmetricDenseMatrix(data); + } + + return new DenseMatrix(data); } /// @@ -95,5 +100,27 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double { return new DenseVector(data); } + + /// + /// Can create a matrix form array. + /// + [Test] + public void CanCreateMatrixFrom2DArray() + { + var testData = new Dictionary + { + { "Singular3x3", new SymmetricDenseMatrix(new[,] { { 1.0, 2.0, 3.0 }, { 2.0, 0.0, 0.0 }, { 3.0, 0.0, 0.0 } }) }, + { "Square3x3", new SymmetricDenseMatrix(new[,] { { -1.1, 2.0, 3.0 }, { 2.0, 1.1, 0.0 }, { 3.0, 0.0, 6.6 } }) }, + { "Square4x4", new SymmetricDenseMatrix(new[,] { { 1.1, 2.0, -3.0, 4.4 }, { 2.0, 5.0, -6.0, 7.0 }, { -3.0, -6.0, 8.0, 9.0 }, { 4.4, 7.0, 9.0, 10.0 } }) }, + { "Singular4x4", new SymmetricDenseMatrix(new[,] { { 1.0, 2.0, 0.0, 4.0 }, { 2.0, 5.0, 0.0, 7.0 }, { 0.0, 0.0, 0.0, 0.0 }, { 4.0, 7.0, 0.0, 10.0 } }) }, + { "Symmetric3x3", new SymmetricDenseMatrix(new[,] { { 1.0, 2.0, 3.0 }, { 2.0, 2.0, 0.0 }, { 3.0, 0.0, 3.0 } }) }, + { "IndexTester4x4", new SymmetricDenseMatrix(new double[,] { { 0, 1, 3, 6 }, { 1, 2, 4, 7 }, { 3, 4, 5, 8 }, { 6, 7, 8, 9 } }) } + }; + + foreach (var name in testData.Keys) + { + Assert.AreEqual(TestMatrices[name], testData[name]); + } + } } } \ No newline at end of file