Browse Source

Fixes from Unit Tests

Signed-off-by: Alexander Karatarakis <alex@karatarakis.com>
la-symmetric^2
Alexander Karatarakis 14 years ago
parent
commit
02526e814d
  1. 55
      src/Numerics/LinearAlgebra/Double/SymmetricDenseMatrix.cs
  2. 12
      src/Numerics/LinearAlgebra/Storage/SymmetricMatrixStorage.cs
  3. 33
      src/UnitTests/LinearAlgebraTests/Double/SymmetricDenseMatrixTests.cs

55
src/Numerics/LinearAlgebra/Double/SymmetricDenseMatrix.cs

@ -46,20 +46,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{ {
readonly DenseColumnMajorSymmetricMatrixStorage<double> _storage; readonly DenseColumnMajorSymmetricMatrixStorage<double> _storage;
/// <summary>
/// Number of rows.
/// </summary>
/// <remarks>Using this instead of the RowCount property to speed up calculating
/// a matrix index in the data array.</remarks>
readonly int _rowCount;
/// <summary>
/// Number of columns.
/// </summary>
/// <remarks>Using this instead of the ColumnCount property to speed up calculating
/// a matrix index in the data array.</remarks>
readonly int _columnCount;
/// <summary> /// <summary>
/// Gets the matrix's data. /// Gets the matrix's data.
/// </summary> /// </summary>
@ -70,8 +56,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double
: base(storage) : base(storage)
{ {
_storage = storage; _storage = storage;
_rowCount = _storage.RowCount;
_columnCount = _storage.ColumnCount;
_data = _storage.Data; _data = _storage.Data;
} }
@ -99,9 +83,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public SymmetricDenseMatrix(int order, double value) public SymmetricDenseMatrix(int order, double value)
: this(order) : 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++) 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++) 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
} }
/// <summary> /// <summary>
/// Gets the matrix's data in array format. /// Gets the matrix's data.
/// </summary> /// </summary>
/// <value>The matrix's raw data.</value> /// <value>The matrix's data.</value>
public double[] Data public double[] Data
{ {
get; get { return _data; }
private set;
} }
/// <summary> /// <summary>
@ -279,7 +262,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
} }
else 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 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 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 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 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 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( CommonParallel.For(
0, 0,
Data.Length, _data.Length,
index => denseResult.Data[index] %= divisor); index => denseResult._data[index] %= divisor);
} }
} }
@ -578,9 +561,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double
} }
else 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 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();
} }
} }
} }

12
src/Numerics/LinearAlgebra/Storage/SymmetricMatrixStorage.cs

@ -48,5 +48,17 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
} }
} }
} }
/// <remarks>Parameters assumed to be validated already.</remarks>
public override void CopyTo(MatrixStorage<T> 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));
}
}
}
} }
} }

33
src/UnitTests/LinearAlgebraTests/Double/SymmetricDenseMatrixTests.cs

@ -26,6 +26,8 @@
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
{ {
using System.Collections.Generic;
using MathNet.Numerics.LinearAlgebra.Double; using MathNet.Numerics.LinearAlgebra.Double;
using NUnit.Framework; using NUnit.Framework;
@ -63,9 +65,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
/// </returns> /// </returns>
protected override Matrix CreateMatrix(double[,] data) protected override Matrix CreateMatrix(double[,] data)
{ {
return SymmetricMatrix.CheckIfSymmetric(data) if (SymmetricMatrix.CheckIfSymmetric(data))
? (Matrix)new SymmetricDenseMatrix(data) {
: new DenseMatrix(data); return new SymmetricDenseMatrix(data);
}
return new DenseMatrix(data);
} }
/// <summary> /// <summary>
@ -95,5 +100,27 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
{ {
return new DenseVector(data); return new DenseVector(data);
} }
/// <summary>
/// Can create a matrix form array.
/// </summary>
[Test]
public void CanCreateMatrixFrom2DArray()
{
var testData = new Dictionary<string, Matrix>
{
{ "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]);
}
}
} }
} }
Loading…
Cancel
Save