Browse Source

added support for large sparse matrices with less than int.MaxValue non-zero elelements, work items: 5688

la-knuth
Marcus Cuda 16 years ago
parent
commit
820cba83cd
  1. 13
      src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs
  2. 13
      src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs
  3. 13
      src/Numerics/LinearAlgebra/Double/SparseMatrix.cs
  4. 13
      src/Numerics/LinearAlgebra/Single/SparseMatrix.cs
  5. 11
      src/Numerics/Properties/Resources.Designer.cs
  6. 3
      src/Numerics/Properties/Resources.resx
  7. 13
      src/UnitTests/LinearAlgebraTests/Complex/SparseMatrixTests.cs
  8. 13
      src/UnitTests/LinearAlgebraTests/Complex32/SparseMatrixTests.cs
  9. 13
      src/UnitTests/LinearAlgebraTests/Double/SparseMatrixTests.cs
  10. 13
      src/UnitTests/LinearAlgebraTests/Single/SparseMatrixTests.cs

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

@ -665,13 +665,18 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
index = ~index;
// Check if the storage needs to be increased
if ((NonZerosCount == _nonZeroValues.Length) && (NonZerosCount < (RowCount * ColumnCount)))
if ((NonZerosCount == _nonZeroValues.Length) && (NonZerosCount < ((long)RowCount * ColumnCount)))
{
// Value array is completely full so we increase the size
// Determine the increase in size. We will not grow beyond the size of the matrix
var size = Math.Min(_nonZeroValues.Length + GrowthSize(), RowCount * ColumnCount);
Array.Resize(ref _nonZeroValues, size);
Array.Resize(ref _columnIndices, size);
var size = Math.Min(_nonZeroValues.Length + GrowthSize(), (long)RowCount * ColumnCount);
if (size > int.MaxValue)
{
throw new NotSupportedException(Resources.TooManyElements);
}
Array.Resize(ref _nonZeroValues, (int)size);
Array.Resize(ref _columnIndices, (int)size);
}
// Move all values (with an position larger than index) in the value array to the next position

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

@ -666,13 +666,18 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
index = ~index;
// Check if the storage needs to be increased
if ((NonZerosCount == _nonZeroValues.Length) && (NonZerosCount < (RowCount * ColumnCount)))
if ((NonZerosCount == _nonZeroValues.Length) && (NonZerosCount < ((long)RowCount * ColumnCount)))
{
// Value array is completely full so we increase the size
// Determine the increase in size. We will not grow beyond the size of the matrix
var size = Math.Min(_nonZeroValues.Length + GrowthSize(), RowCount * ColumnCount);
Array.Resize(ref _nonZeroValues, size);
Array.Resize(ref _columnIndices, size);
var size = Math.Min(_nonZeroValues.Length + GrowthSize(), (long)RowCount * ColumnCount);
if (size > int.MaxValue)
{
throw new NotSupportedException(Resources.TooManyElements);
}
Array.Resize(ref _nonZeroValues, (int)size);
Array.Resize(ref _columnIndices, (int)size);
}
// Move all values (with an position larger than index) in the value array to the next position

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

@ -677,13 +677,18 @@ namespace MathNet.Numerics.LinearAlgebra.Double
index = ~index;
// Check if the storage needs to be increased
if ((NonZerosCount == _nonZeroValues.Length) && (NonZerosCount < (RowCount * ColumnCount)))
if ((NonZerosCount == _nonZeroValues.Length) && (NonZerosCount < ((long)RowCount * ColumnCount)))
{
// Value array is completely full so we increase the size
// Determine the increase in size. We will not grow beyond the size of the matrix
var size = Math.Min(_nonZeroValues.Length + GrowthSize(), RowCount * ColumnCount);
Array.Resize(ref _nonZeroValues, size);
Array.Resize(ref _columnIndices, size);
var size = Math.Min(_nonZeroValues.Length + GrowthSize(), (long)RowCount * ColumnCount);
if (size > int.MaxValue)
{
throw new NotSupportedException(Resources.TooManyElements);
}
Array.Resize(ref _nonZeroValues, (int)size);
Array.Resize(ref _columnIndices, (int)size);
}
// Move all values (with an position larger than index) in the value array to the next position

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

@ -660,13 +660,18 @@ namespace MathNet.Numerics.LinearAlgebra.Single
index = ~index;
// Check if the storage needs to be increased
if ((NonZerosCount == _nonZeroValues.Length) && (NonZerosCount < (RowCount * ColumnCount)))
if ((NonZerosCount == _nonZeroValues.Length) && (NonZerosCount < ((long)RowCount * ColumnCount)))
{
// Value array is completely full so we increase the size
// Determine the increase in size. We will not grow beyond the size of the matrix
var size = Math.Min(_nonZeroValues.Length + GrowthSize(), RowCount * ColumnCount);
Array.Resize(ref _nonZeroValues, size);
Array.Resize(ref _columnIndices, size);
var size = Math.Min(_nonZeroValues.Length + GrowthSize(), (long)RowCount * ColumnCount);
if (size > int.MaxValue)
{
throw new NotSupportedException(Resources.TooManyElements);
}
Array.Resize(ref _nonZeroValues, (int)size);
Array.Resize(ref _columnIndices, (int)size);
}
// Move all values (with an position larger than index) in the value array to the next position

11
src/Numerics/Properties/Resources.Designer.cs

@ -1,7 +1,7 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.1
// Runtime Version:4.0.30319.225
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
@ -753,6 +753,15 @@ namespace MathNet.Numerics.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to We only support sparse matrix with less than int.MaxValue elements..
/// </summary>
internal static string TooManyElements {
get {
return ResourceManager.GetString("TooManyElements", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to The moment of the distribution is undefined..
/// </summary>

3
src/Numerics/Properties/Resources.resx

@ -357,4 +357,7 @@
<data name="RowsLessThanColumns" xml:space="preserve">
<value>The number of rows must greater than or equal to the number of columns.</value>
</data>
<data name="TooManyElements" xml:space="preserve">
<value>We only support sparse matrix with less than int.MaxValue elements.</value>
</data>
</root>

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

@ -297,5 +297,18 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
Assert.IsTrue(diff3.Equals(m3.Negate()));
Assert.IsTrue(diff3.Equals(diff4.Negate()));
}
/// <summary>
/// Test whether we can create a large sparse matrix
/// </summary>
[Test]
public void CanCreateLargeMatrix()
{
const int order = 1000000;
var matrix = new SparseMatrix(order);
Assert.AreEqual(order, matrix.RowCount);
Assert.AreEqual(order, matrix.ColumnCount);
Assert.DoesNotThrow(() => matrix[0, 0] = 1);
}
}
}

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

@ -297,5 +297,18 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
Assert.IsTrue(diff3.Equals(m3.Negate()));
Assert.IsTrue(diff3.Equals(diff4.Negate()));
}
/// <summary>
/// Test whether we can create a large sparse matrix
/// </summary>
[Test]
public void CanCreateLargeMatrix()
{
const int order = 1000000;
var matrix = new SparseMatrix(order);
Assert.AreEqual(order, matrix.RowCount);
Assert.AreEqual(order, matrix.ColumnCount);
Assert.DoesNotThrow(() => matrix[0, 0] = 1);
}
}
}

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

@ -296,5 +296,18 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
Assert.IsTrue(diff3.Equals(m3.Negate()));
Assert.IsTrue(diff3.Equals(diff4.Negate()));
}
/// <summary>
/// Test whether we can create a large sparse matrix
/// </summary>
[Test]
public void CanCreateLargeMatrix()
{
const int order = 1000000;
var matrix = new SparseMatrix(order);
Assert.AreEqual(order, matrix.RowCount);
Assert.AreEqual(order, matrix.ColumnCount);
Assert.DoesNotThrow(() => matrix[0, 0] = 1);
}
}
}

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

@ -296,5 +296,18 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
Assert.IsTrue(diff3.Equals(m3.Negate()));
Assert.IsTrue(diff3.Equals(diff4.Negate()));
}
/// <summary>
/// Test whether we can create a large sparse matrix
/// </summary>
[Test]
public void CanCreateLargeMatrix()
{
const int order = 1000000;
var matrix = new SparseMatrix(order);
Assert.AreEqual(order, matrix.RowCount);
Assert.AreEqual(order, matrix.ColumnCount);
Assert.DoesNotThrow(() => matrix[0, 0] = 1);
}
}
}

Loading…
Cancel
Save