Browse Source

added a IsSymmetric property to the matrix interface and an optimized version for sparse matrices. work item: 5653

pull/36/head
Marcus Cuda 16 years ago
parent
commit
2dd9bb7555
  1. 5
      src/MathNet.Numerics.5.1.ReSharper
  2. 11
      src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs
  3. 65
      src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs
  4. 11
      src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs
  5. 102
      src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs
  6. 11
      src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs
  7. 65
      src/Numerics/LinearAlgebra/Double/SparseMatrix.cs
  8. 32
      src/Numerics/LinearAlgebra/Generic/Matrix.cs
  9. 11
      src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs
  10. 65
      src/Numerics/LinearAlgebra/Single/SparseMatrix.cs
  11. 10
      src/UnitTests/LinearAlgebraTests/Complex/DiagonalMatrixTests.cs
  12. 1
      src/UnitTests/LinearAlgebraTests/Complex/MatrixLoader.cs
  13. 13
      src/UnitTests/LinearAlgebraTests/Complex/MatrixTests.cs
  14. 11
      src/UnitTests/LinearAlgebraTests/Complex32/DiagonalMatrixTests.cs
  15. 3
      src/UnitTests/LinearAlgebraTests/Complex32/MatrixLoader.cs
  16. 13
      src/UnitTests/LinearAlgebraTests/Complex32/MatrixTests.cs
  17. 10
      src/UnitTests/LinearAlgebraTests/Double/DiagonalMatrixTests.cs
  18. 1
      src/UnitTests/LinearAlgebraTests/Double/MatrixLoader.cs
  19. 13
      src/UnitTests/LinearAlgebraTests/Double/MatrixTests.cs
  20. 10
      src/UnitTests/LinearAlgebraTests/Single/DiagonalMatrixTests.cs
  21. 1
      src/UnitTests/LinearAlgebraTests/Single/MatrixLoader.cs
  22. 13
      src/UnitTests/LinearAlgebraTests/Single/MatrixTests.cs

5
src/MathNet.Numerics.5.1.ReSharper

@ -75,7 +75,10 @@ Datatset
Doesnt
cosecant
Ulps
Matlab</UserWords>
Matlab
&amp;lt
&amp;gt
Wikipedia</UserWords>
</CustomDictionary>
</Dictionaries>
</CustomDictionaries>

11
src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs

@ -1537,6 +1537,17 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
throw new InvalidOperationException("Permutations in diagonal matrix are not allowed");
}
/// <summary>
/// Gets a value indicating whether this matrix is symmetric.
/// </summary>
public override bool IsSymmetric
{
get
{
return true;
}
}
#region Static constructors for special matrices.
/// <summary>

65
src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs

@ -1503,6 +1503,71 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
}
}
}
/// <summary>
/// Gets a value indicating whether this matrix is symmetric.
/// </summary>
public override bool IsSymmetric
{
get
{
if (RowCount != ColumnCount)
{
return false;
}
// todo: we might be able to speed this up by caching one half of the matrix
for (var row = 0; row < RowCount - 1; row++)
{
var start = _rowIndex[row];
var end = _rowIndex[row + 1];
if (start == end)
{
continue;
}
if (!CheckIfOppositesAreEqual(start, end, row))
{
return false;
}
}
var lastRow = _rowIndex.Length - 1;
if (_rowIndex[lastRow] < NonZerosCount)
{
if (!CheckIfOppositesAreEqual(_rowIndex[lastRow], NonZerosCount, lastRow))
{
return false;
}
}
return true;
}
}
/// <summary>
/// Checks if opposites in a range are equal.
/// </summary>
/// <param name="start">The start of the range.</param>
/// <param name="end">The end of the range.</param>
/// <param name="row">The row the row to check.</param>
/// <returns>If the values are equal or not.</returns>
private bool CheckIfOppositesAreEqual(int start, int end, int row)
{
for (var index = start; index < end; index++)
{
var column = _columnIndices[index];
var opposite = At(column, row);
if (!_nonZeroValues[index].Equals(opposite))
{
return false;
}
}
return true;
}
}
}

11
src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs

@ -1542,6 +1542,17 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
throw new InvalidOperationException("Permutations in diagonal matrix are not allowed");
}
/// <summary>
/// Gets a value indicating whether this matrix is symmetric.
/// </summary>
public override bool IsSymmetric
{
get
{
return true;
}
}
#region Static constructors for special matrices.
/// <summary>

102
src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs

@ -41,7 +41,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// A Matrix class with sparse storage. The underlying storage scheme is 3-array compressed-sparse-row (CSR) Format.
/// <a href="http://en.wikipedia.org/wiki/Sparse_matrix#Compressed_sparse_row_.28CSR_or_CRS.29">Wikipedia - CSR</a>.
/// </summary>
public class SparseMatrix : Matrix
public class SparseMatrix : Matrix
{
/// <summary>
/// The array containing the row indices of the existing rows. Element "j" of the array gives the index of the
@ -54,7 +54,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// array using the row-major storage mapping described in a compressed sparse row (CSR) format.
/// </summary>
private Complex32[] _nonZeroValues = new Complex32[0];
/// <summary>
/// Gets the number of non zero elements in the matrix.
/// </summary>
@ -64,7 +64,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
get;
private set;
}
/// <summary>
/// An array containing the column indices of the non-zero values. Element "I" of the array
/// is the number of the column in matrix that contains the I-th value in the <see cref="_nonZeroValues"/> array.
@ -80,11 +80,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="columns">
/// The number of columns.
/// </param>
public SparseMatrix(int rows, int columns) : base(rows, columns)
public SparseMatrix(int rows, int columns)
: base(rows, columns)
{
_rowIndex = new int[rows];
}
/// <summary>
/// Initializes a new instance of the <see cref="SparseMatrix"/> class. This matrix is square with a given size.
/// </summary>
@ -92,7 +93,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <exception cref="ArgumentException">
/// If <paramref name="order"/> is less than one.
/// </exception>
public SparseMatrix(int order) : this(order, order)
public SparseMatrix(int order)
: this(order, order)
{
}
@ -106,7 +108,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// The number of columns.
/// </param>
/// <param name="value">The value which we assign to each element of the matrix.</param>
public SparseMatrix(int rows, int columns, Complex32 value) : this(rows, columns)
public SparseMatrix(int rows, int columns, Complex32 value)
: this(rows, columns)
{
if (value == 0.0f)
{
@ -128,7 +131,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
_nonZeroValues[i] = value;
_columnIndices[i] = j;
}
// Set proper row pointers
for (var i = 0; i < _rowIndex.Length; i++)
{
@ -144,7 +147,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="array">The one dimensional array to create this matrix from. This array should store the matrix in column-major order. see: http://en.wikipedia.org/wiki/Column-major_order </param>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="array"/> length is less than <paramref name="rows"/> * <paramref name="columns"/>.
/// </exception>
public SparseMatrix(int rows, int columns, Complex32[] array) : this(rows, columns)
public SparseMatrix(int rows, int columns, Complex32[] array)
: this(rows, columns)
{
if (rows * columns > array.Length)
{
@ -164,7 +168,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// Initializes a new instance of the <see cref="SparseMatrix"/> class from a 2D array.
/// </summary>
/// <param name="array">The 2D array to create this matrix from.</param>
public SparseMatrix(Complex32[,] array) : this(array.GetLength(0), array.GetLength(1))
public SparseMatrix(Complex32[,] array)
: this(array.GetLength(0), array.GetLength(1))
{
var rows = array.GetLength(0);
var columns = array.GetLength(1);
@ -708,7 +713,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
_nonZeroValues[i - 1] = _nonZeroValues[i];
_columnIndices[i - 1] = _columnIndices[i];
}
// Decrease value in Row
for (var i = row + 1; i < _rowIndex.Length; i++)
{
@ -725,7 +730,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
Array.Resize(ref _columnIndices, NonZerosCount);
}
}
/// <summary>
/// Find item Index in nonZeroValues array
/// </summary>
@ -740,7 +745,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
var endIndex = row < _rowIndex.Length - 1 ? _rowIndex[row + 1] : NonZerosCount;
return Array.BinarySearch(_columnIndices, startIndex, endIndex - startIndex, column);
}
/// <summary>
/// Calculates the amount with which to grow the storage array's if they need to be
/// increased in size.
@ -820,7 +825,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
Array.Copy(_rowIndex, sparseTarget._rowIndex, RowCount);
}
}
/// <summary>
/// Returns a hash code for this instance.
/// </summary>
@ -1038,7 +1043,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
resultSparseMatrix.NonZerosCount = NonZerosCount + lowerSparseMatrix.NonZerosCount;
resultSparseMatrix._nonZeroValues = new Complex32[resultSparseMatrix.NonZerosCount];
resultSparseMatrix._columnIndices = new int[resultSparseMatrix.NonZerosCount];
Array.Copy(_nonZeroValues, 0, resultSparseMatrix._nonZeroValues, 0, NonZerosCount);
Array.Copy(lowerSparseMatrix._nonZeroValues, 0, resultSparseMatrix._nonZeroValues, NonZerosCount, lowerSparseMatrix.NonZerosCount);
@ -1313,7 +1318,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
{
// Multiply row of matrix A on column of matrix B
other.Column(column, columnVector);
var sum = Complex32.Zero;
for (var index = startIndex; index < endIndex; index++)
{
@ -1499,5 +1504,70 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
}
}
}
/// <summary>
/// Gets a value indicating whether this matrix is symmetric.
/// </summary>
public override bool IsSymmetric
{
get
{
if (RowCount != ColumnCount)
{
return false;
}
// todo: we might be able to speed this up by caching one half of the matrix
for (var row = 0; row < RowCount - 1; row++)
{
var start = _rowIndex[row];
var end = _rowIndex[row + 1];
if (start == end)
{
continue;
}
if (!CheckIfOppositesAreEqual(start, end, row))
{
return false;
}
}
var lastRow = _rowIndex.Length - 1;
if (_rowIndex[lastRow] < NonZerosCount)
{
if (!CheckIfOppositesAreEqual(_rowIndex[lastRow], NonZerosCount, lastRow))
{
return false;
}
}
return true;
}
}
/// <summary>
/// Checks if opposites in a range are equal.
/// </summary>
/// <param name="start">The start of the range.</param>
/// <param name="end">The end of the range.</param>
/// <param name="row">The row the row to check.</param>
/// <returns>If the values are equal or not.</returns>
private bool CheckIfOppositesAreEqual(int start, int end, int row)
{
for (var index = start; index < end; index++)
{
var column = _columnIndices[index];
var opposite = At(column, row);
if (!_nonZeroValues[index].Equals(opposite))
{
return false;
}
}
return true;
}
}
}

11
src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs

@ -1536,6 +1536,17 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new InvalidOperationException("Permutations in diagonal matrix are not allowed");
}
/// <summary>
/// Gets a value indicating whether this matrix is symmetric.
/// </summary>
public override bool IsSymmetric
{
get
{
return true;
}
}
#region Static constructors for special matrices.
/// <summary>

65
src/Numerics/LinearAlgebra/Double/SparseMatrix.cs

@ -1515,5 +1515,70 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
}
}
/// <summary>
/// Gets a value indicating whether this matrix is symmetric.
/// </summary>
public override bool IsSymmetric
{
get
{
if (RowCount != ColumnCount)
{
return false;
}
// todo: we might be able to speed this up by caching one half of the matrix
for (var row = 0; row < RowCount - 1; row++)
{
var start = _rowIndex[row];
var end = _rowIndex[row + 1];
if (start == end)
{
continue;
}
if (!CheckIfOppositesAreEqual(start, end, row))
{
return false;
}
}
var lastRow = _rowIndex.Length - 1;
if (_rowIndex[lastRow] < NonZerosCount)
{
if (!CheckIfOppositesAreEqual(_rowIndex[lastRow], NonZerosCount, lastRow))
{
return false;
}
}
return true;
}
}
/// <summary>
/// Checks if opposites in a range are equal.
/// </summary>
/// <param name="start">The start of the range.</param>
/// <param name="end">The end of the range.</param>
/// <param name="row">The row the row to check.</param>
/// <returns>If the values are equal or not.</returns>
private bool CheckIfOppositesAreEqual(int start, int end, int row)
{
for (var index = start; index < end; index++)
{
var column = _columnIndices[index];
var opposite = At(column, row);
if (!_nonZeroValues[index].Equals(opposite))
{
return false;
}
}
return true;
}
}
}

32
src/Numerics/LinearAlgebra/Generic/Matrix.cs

@ -1773,5 +1773,37 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
}
}
}
/// <summary>
/// Gets a value indicating whether this matrix is symmetric.
/// </summary>
public virtual bool IsSymmetric
{
get
{
if (RowCount != ColumnCount)
{
return false;
}
for (var row = 1; row < RowCount; row++)
{
for (var column = 0; column < ColumnCount; column++)
{
if (column >= row)
{
continue;
}
if (!At(row, column).Equals(At(column, row)))
{
return false;
}
}
}
return true;
}
}
}
}

11
src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs

@ -1536,6 +1536,17 @@ namespace MathNet.Numerics.LinearAlgebra.Single
throw new InvalidOperationException("Permutations in diagonal matrix are not allowed");
}
/// <summary>
/// Gets a value indicating whether this matrix is symmetric.
/// </summary>
public override bool IsSymmetric
{
get
{
return true;
}
}
#region Static constructors for special matrices.
/// <summary>

65
src/Numerics/LinearAlgebra/Single/SparseMatrix.cs

@ -1498,5 +1498,70 @@ namespace MathNet.Numerics.LinearAlgebra.Single
}
}
}
/// <summary>
/// Gets a value indicating whether this matrix is symmetric.
/// </summary>
public override bool IsSymmetric
{
get
{
if (RowCount != ColumnCount)
{
return false;
}
// todo: we might be able to speed this up by caching one half of the matrix
for (var row = 0; row < RowCount - 1; row++)
{
var start = _rowIndex[row];
var end = _rowIndex[row + 1];
if (start == end)
{
continue;
}
if (!CheckIfOppositesAreEqual(start, end, row))
{
return false;
}
}
var lastRow = _rowIndex.Length - 1;
if (_rowIndex[lastRow] < NonZerosCount)
{
if (!CheckIfOppositesAreEqual(_rowIndex[lastRow], NonZerosCount, lastRow))
{
return false;
}
}
return true;
}
}
/// <summary>
/// Checks if opposites in a range are equal.
/// </summary>
/// <param name="start">The start of the range.</param>
/// <param name="end">The end of the range.</param>
/// <param name="row">The row the row to check.</param>
/// <returns>If the values are equal or not.</returns>
private bool CheckIfOppositesAreEqual(int start, int end, int row)
{
for (var index = start; index < end; index++)
{
var column = _columnIndices[index];
var opposite = At(column, row);
if (!_nonZeroValues[index].Equals(opposite))
{
return false;
}
}
return true;
}
}
}

10
src/UnitTests/LinearAlgebraTests/Complex/DiagonalMatrixTests.cs

@ -582,5 +582,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
Assert.AreEqual(2, item.Item2);
Assert.AreEqual(new Complex(3.0, 1.0), item.Item3);
}
/// <summary>
/// Can check if a matrix is symmetric.
/// </summary>
[Test]
public override void CanCheckIfMatrixIsSymmetric()
{
var matrix = TestMatrices["Square3x3"];
Assert.IsTrue(matrix.IsSymmetric);
}
}
}

1
src/UnitTests/LinearAlgebraTests/Complex/MatrixLoader.cs

@ -101,6 +101,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
{ "Singular4x4", new[,] { { new Complex(-1.1, 1), new Complex(-2.2, 1), new Complex(-3.3, 1), new Complex(-4.4, 1) }, { new Complex(-1.1, 1), new Complex(-2.2, 1), new Complex(-3.3, 1), new Complex(-4.4, 1) }, { new Complex(-1.1, 1), new Complex(-2.2, 1), new Complex(-3.3, 1), new Complex(-4.4, 1) }, { new Complex(-1.1, 1), new Complex(-2.2, 1), new Complex(-3.3, 1), new Complex(-4.4, 1) } } },
{ "Tall3x2", new[,] { { new Complex(-1.1, 1), new Complex(-2.2, 1) }, { Complex.Zero, new Complex(1.1, 1) }, { new Complex(-4.4, 1), new Complex(5.5, 1) } } },
{ "Wide2x3", new[,] { { new Complex(-1.1, 1), new Complex(-2.2, 1), new Complex(-3.3, 1) }, { Complex.Zero, new Complex(1.1, 1), new Complex(2.2, 1) } } },
{ "Symmetric3x3", new[,] { { Complex.One, 2.0, 3.0 }, { 2.0, 2.0, 0.0 }, { 3.0, 0.0, 3.0 } } }
};
TestMatrices = new Dictionary<string, Matrix>();

13
src/UnitTests/LinearAlgebraTests/Complex/MatrixTests.cs

@ -1851,5 +1851,18 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
Assert.AreEqual(2, item.Item2);
Assert.AreEqual(new Complex(2.0, 1.0), item.Item3);
}
/// <summary>
/// Can check if a matrix is symmetric.
/// </summary>
[Test]
public virtual void CanCheckIfMatrixIsSymmetric()
{
var matrix = TestMatrices["Symmetric3x3"];
Assert.IsTrue(matrix.IsSymmetric);
matrix = TestMatrices["Square3x3"];
Assert.IsFalse(matrix.IsSymmetric);
}
}
}

11
src/UnitTests/LinearAlgebraTests/Complex32/DiagonalMatrixTests.cs

@ -582,5 +582,16 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
Assert.AreEqual(2, item.Item2);
Assert.AreEqual(new Complex32(3.0f, 1.0f), item.Item3);
}
/// <summary>
/// Can check if a matrix is symmetric.
/// </summary>
[Test]
public override void CanCheckIfMatrixIsSymmetric()
{
var matrix = TestMatrices["Square3x3"];
Assert.IsTrue(matrix.IsSymmetric);
}
}
}

3
src/UnitTests/LinearAlgebraTests/Complex32/MatrixLoader.cs

@ -100,7 +100,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
{ "Square4x4", new[,] { { new Complex32(-1.1f, 1), new Complex32(-2.2f, 1), new Complex32(-3.3f, 1), new Complex32(-4.4f, 1) }, { Complex32.Zero, new Complex32(1.1f, 1), new Complex32(2.2f, 1), new Complex32(3.3f, 1) }, { new Complex32(1.0f, 1), new Complex32(2.1f, 1), new Complex32(6.2f, 1), new Complex32(4.3f, 1) }, { new Complex32(-4.4f, 1), new Complex32(5.5f, 1), new Complex32(6.6f, 1), new Complex32(-7.7f, 1) } } },
{ "Singular4x4", new[,] { { new Complex32(-1.1f, 1), new Complex32(-2.2f, 1), new Complex32(-3.3f, 1), new Complex32(-4.4f, 1) }, { new Complex32(-1.1f, 1), new Complex32(-2.2f, 1), new Complex32(-3.3f, 1), new Complex32(-4.4f, 1) }, { new Complex32(-1.1f, 1), new Complex32(-2.2f, 1), new Complex32(-3.3f, 1), new Complex32(-4.4f, 1) }, { new Complex32(-1.1f, 1), new Complex32(-2.2f, 1), new Complex32(-3.3f, 1), new Complex32(-4.4f, 1) } } },
{ "Tall3x2", new[,] { { new Complex32(-1.1f, 1), new Complex32(-2.2f, 1) }, { Complex32.Zero, new Complex32(1.1f, 1) }, { new Complex32(-4.4f, 1), new Complex32(5.5f, 1) } } },
{ "Wide2x3", new[,] { { new Complex32(-1.1f, 1), new Complex32(-2.2f, 1), new Complex32(-3.3f, 1) }, { Complex32.Zero, new Complex32(1.1f, 1), new Complex32(2.2f, 1) } } },
{ "Wide2x3", new[,] { { new Complex32(-1.1f, 1), new Complex32(-2.2f, 1), new Complex32(-3.3f, 1) }, { Complex32.Zero, new Complex32(1.1f, 1), new Complex32(2.2f, 1) } } },
{ "Symmetric3x3", new[,] { { Complex32.One, 2.0f, 3.0f }, { 2.0f, 2.0f, 0.0f }, { 3.0f, 0.0f, 3.0f } } }
};
TestMatrices = new Dictionary<string, Matrix>();

13
src/UnitTests/LinearAlgebraTests/Complex32/MatrixTests.cs

@ -1851,5 +1851,18 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
Assert.AreEqual(2, item.Item2);
Assert.AreEqual(new Complex32(2.0f, 1.0f), item.Item3);
}
/// <summary>
/// Can check if a matrix is symmetric.
/// </summary>
[Test]
public virtual void CanCheckIfMatrixIsSymmetric()
{
var matrix = TestMatrices["Symmetric3x3"];
Assert.IsTrue(matrix.IsSymmetric);
matrix = TestMatrices["Square3x3"];
Assert.IsFalse(matrix.IsSymmetric);
}
}
}

10
src/UnitTests/LinearAlgebraTests/Double/DiagonalMatrixTests.cs

@ -582,5 +582,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
Assert.AreEqual(2, item.Item2);
Assert.AreEqual(3.0, item.Item3);
}
/// <summary>
/// Can check if a matrix is symmetric.
/// </summary>
[Test]
public override void CanCheckIfMatrixIsSymmetric()
{
var matrix = TestMatrices["Square3x3"];
Assert.IsTrue(matrix.IsSymmetric);
}
}
}

1
src/UnitTests/LinearAlgebraTests/Double/MatrixLoader.cs

@ -100,6 +100,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
{ "Singular4x4", new[,] { { -1.1, -2.2, -3.3, -4.4 }, { -1.1, -2.2, -3.3, -4.4 }, { -1.1, -2.2, -3.3, -4.4 }, { -1.1, -2.2, -3.3, -4.4 } } },
{ "Tall3x2", new[,] { { -1.1, -2.2 }, { 0.0, 1.1 }, { -4.4, 5.5 } } },
{ "Wide2x3", new[,] { { -1.1, -2.2, -3.3 }, { 0.0, 1.1, 2.2 } } },
{ "Symmetric3x3", new[,] { { 1.0, 2.0, 3.0 }, { 2.0, 2.0, 0.0 }, { 3.0, 0.0, 3.0 } } }
};
TestMatrices = new Dictionary<string, Matrix>();

13
src/UnitTests/LinearAlgebraTests/Double/MatrixTests.cs

@ -1792,5 +1792,18 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
Assert.AreEqual(2, item.Item2);
Assert.AreEqual(2.0, item.Item3);
}
/// <summary>
/// Can check if a matrix is symmetric.
/// </summary>
[Test]
public virtual void CanCheckIfMatrixIsSymmetric()
{
var matrix = TestMatrices["Symmetric3x3"];
Assert.IsTrue(matrix.IsSymmetric);
matrix = TestMatrices["Square3x3"];
Assert.IsFalse(matrix.IsSymmetric);
}
}
}

10
src/UnitTests/LinearAlgebraTests/Single/DiagonalMatrixTests.cs

@ -581,5 +581,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
Assert.AreEqual(2, item.Item2);
Assert.AreEqual(3.0f, item.Item3);
}
/// <summary>
/// Can check if a matrix is symmetric.
/// </summary>
[Test]
public override void CanCheckIfMatrixIsSymmetric()
{
var matrix = TestMatrices["Square3x3"];
Assert.IsTrue(matrix.IsSymmetric);
}
}
}

1
src/UnitTests/LinearAlgebraTests/Single/MatrixLoader.cs

@ -100,6 +100,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
{ "Singular4x4", new[,] { { -1.1f, -2.2f, -3.3f, -4.4f }, { -1.1f, -2.2f, -3.3f, -4.4f }, { -1.1f, -2.2f, -3.3f, -4.4f }, { -1.1f, -2.2f, -3.3f, -4.4f } } },
{ "Tall3x2", new[,] { { -1.1f, -2.2f }, { 0.0f, 1.1f }, { -4.4f, 5.5f } } },
{ "Wide2x3", new[,] { { -1.1f, -2.2f, -3.3f }, { 0.0f, 1.1f, 2.2f } } },
{ "Symmetric3x3", new[,] { { 1.0f, 2.0f, 3.0f }, { 2.0f, 2.0f, 0.0f }, { 3.0f, 0.0f, 3.0f } } }
};
TestMatrices = new Dictionary<string, Matrix>();

13
src/UnitTests/LinearAlgebraTests/Single/MatrixTests.cs

@ -1792,5 +1792,18 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
Assert.AreEqual(2, item.Item2);
Assert.AreEqual(2.0f, item.Item3);
}
/// <summary>
/// Can check if a matrix is symmetric.
/// </summary>
[Test]
public virtual void CanCheckIfMatrixIsSymmetric()
{
var matrix = TestMatrices["Symmetric3x3"];
Assert.IsTrue(matrix.IsSymmetric);
matrix = TestMatrices["Square3x3"];
Assert.IsFalse(matrix.IsSymmetric);
}
}
}

Loading…
Cancel
Save