Browse Source

sparse: merge Andriy's additions

la-knuth
Marcus Cuda 17 years ago
parent
commit
a6572fa189
  1. 2
      src/Numerics/LinearAlgebra/Double/Matrix.cs
  2. 868
      src/Numerics/LinearAlgebra/Double/SparseMatrix.cs
  3. 4
      src/Numerics/LinearAlgebra/Double/SparseVector.cs
  4. 44
      src/UnitTests/LinearAlgebraTests/Double/SparseMatrixTests.cs
  5. 2
      src/UnitTests/LinearAlgebraTests/Double/SparseVectorTest.cs
  6. 4
      src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs

2
src/Numerics/LinearAlgebra/Double/Matrix.cs

@ -204,8 +204,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "target");
}
// TODO this assumes that all entries matter; if "this" is a sparse matrix,
// we might be able to optimize the copying a bit.
for (var i = 0; i < this.RowCount; i++)
{
for (var j = 0; j < this.ColumnCount; j++)

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

File diff suppressed because it is too large

4
src/Numerics/LinearAlgebra/Double/SparseVector.cs

@ -1244,7 +1244,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <exception cref="ArgumentNullException">If the length vector is non positive<see langword="null" />.</exception>
public override Vector Random(int length, IContinuousDistribution randomDistribution)
{
if (length < 0)
if (length < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "length");
}
@ -1270,7 +1270,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <exception cref="ArgumentNullException">If the n vector is non positive<see langword="null" />.</exception>
public override Vector Random(int length, IDiscreteDistribution randomDistribution)
{
if (length < 0)
if (length < 1)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "length");
}

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

@ -1,9 +1,11 @@
//<copyright file="SparseMatrixTests.cs" company="Math.NET">
// <copyright file="SparseMatrixTests.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2009-2010 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -26,10 +30,9 @@
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
{
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using MbUnit.Framework;
using LinearAlgebra.Double;
using MbUnit.Framework;
public class SparseMatrixTests : MatrixTests
{
@ -58,16 +61,16 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
{
var testData = new Dictionary<string, Matrix>
{
{ "Singular3x3", new SparseMatrix(3, 3, new double[] { 1, 1, 1, 1, 1, 1, 2, 2, 2 }) },
{ "Square3x3", new SparseMatrix(3, 3, new[] { -1.1, 0.0, -4.4, -2.2, 1.1, 5.5, -3.3, 2.2, 6.6 }) },
{ "Square4x4", new SparseMatrix(4, 4, new[] { -1.1, 0.0, 1.0, -4.4, -2.2, 1.1, 2.1, 5.5, -3.3, 2.2, 6.2, 6.6, -4.4, 3.3, 4.3, -7.7 }) },
{ "Tall3x2", new SparseMatrix(3, 2, new[] { -1.1, 0.0, -4.4, -2.2, 1.1, 5.5 }) },
{ "Singular3x3", new SparseMatrix(3, 3, new double[] { 1, 1, 1, 1, 1, 1, 2, 2, 2 }) },
{ "Square3x3", new SparseMatrix(3, 3, new[] { -1.1, 0.0, -4.4, -2.2, 1.1, 5.5, -3.3, 2.2, 6.6 }) },
{ "Square4x4", new SparseMatrix(4, 4, new[] { -1.1, 0.0, 1.0, -4.4, -2.2, 1.1, 2.1, 5.5, -3.3, 2.2, 6.2, 6.6, -4.4, 3.3, 4.3, -7.7 }) },
{ "Tall3x2", new SparseMatrix(3, 2, new[] { -1.1, 0.0, -4.4, -2.2, 1.1, 5.5 }) },
{ "Wide2x3", new SparseMatrix(2, 3, new[] { -1.1, 0.0, -2.2, 1.1, -3.3, 2.2 }) }
};
foreach (var name in testData.Keys)
{
Assert.AreEqual(this.testMatrices[name], testData[name]);
Assert.AreEqual(testMatrices[name], testData[name]);
}
}
@ -84,9 +87,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test]
public void MatrixFrom2DArrayIsCopy()
{
var matrix = new SparseMatrix(this.testData2D["Singular3x3"]);
var matrix = new SparseMatrix(testData2D["Singular3x3"]);
matrix[0, 0] = 10.0;
Assert.AreEqual(1.0, this.testData2D["Singular3x3"][0, 0]);
Assert.AreEqual(1.0, testData2D["Singular3x3"][0, 0]);
}
[Test]
@ -98,12 +101,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Row("Wide2x3")]
public void CanCreateMatrixFrom2DArray(string name)
{
var matrix = new SparseMatrix(this.testData2D[name]);
for (var i = 0; i < this.testData2D[name].GetLength(0); i++)
var matrix = new SparseMatrix(testData2D[name]);
for (var i = 0; i < testData2D[name].GetLength(0); i++)
{
for (var j = 0; j < this.testData2D[name].GetLength(1); j++)
for (var j = 0; j < testData2D[name].GetLength(1); j++)
{
Assert.AreEqual(this.testData2D[name][i, j], matrix[i, j]);
Assert.AreEqual(testData2D[name][i, j], matrix[i, j]);
}
}
}
@ -148,18 +151,17 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
{
var matrix = new SparseMatrix(500, 1000);
var nonzero = 0;
var rnd = new Random(0);
var rnd = new System.Random();
for (var i = 0; i < matrix.RowCount; i++)
for (int i = 0; i < matrix.RowCount; i++)
{
for (var j = 0; j < matrix.ColumnCount; j++)
for (int j = 0; j < matrix.ColumnCount; j++ )
{
var value = rnd.NextDouble();
var value = rnd.Next(10) * rnd.Next(10) * rnd.Next(10) * rnd.Next(10) * rnd.Next(10);
if (value != 0)
{
nonzero++;
}
matrix[i, j] = value;
}
}
@ -167,4 +169,4 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
Assert.AreEqual(matrix.NonZerosCount, nonzero);
}
}
}
}

2
src/UnitTests/LinearAlgebraTests/Double/SparseVectorTest.cs

@ -121,10 +121,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
Assert.ForAll(vector, value => value == 5);
}
// TODO: Remove [Ignore] when SparseMatrix developed
[Test]
[MultipleAsserts]
[Ignore]
public void CanCreateSparseMatrix()
{
var vector = new SparseVector(3);

4
src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs

@ -217,7 +217,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
Assert.AreElementsEqual(vector, array);
}
[Test, Ignore]
[Test]
[MultipleAsserts]
public void CanConvertVectorToColumnMatrix()
{
@ -233,7 +233,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
}
}
[Test, Ignore]
[Test]
[MultipleAsserts]
public void CanConvertVectorToRowMatrix()
{

Loading…
Cancel
Save