|
|
|
@ -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); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|