Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add path for DenseMatrix multiplication with SparseVector #933

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/Benchmark/LinearAlgebra/SparseVector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// <copyright file="SparseVector.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// https://numerics.mathdotnet.com
// https://github.com/mathnet/mathnet-numerics
//
// Copyright (c) 2009-$CURRENT_YEAR$ 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
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// 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
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>

using BenchmarkDotNet.Attributes;
using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.Random;
using System.Linq;

namespace Benchmark.LinearAlgebra
{
public class SparseVector
{
private Matrix<double> matrix;
private Vector<double> vector;

[Params(100, 1000, 10000)]
public int N { get; set; }

[Params(0.01, 0.1, 0.5)]
public double SparseRatio { get; set; }

[GlobalSetup]
public void GlobalSetup()
{
const int Seed = 42;

var rnd = new SystemRandomSource(Seed, true);

matrix = Matrix<double>.Build.Random(N, N, new Normal(rnd));

vector = Vector<double>.Build.SparseOfEnumerable(rnd.NextDoubleSequence().Take(N).Select(x => x < SparseRatio ? x : 0));
}

[Benchmark]
public Vector<double> DenseMatrixSparseVector() => matrix.Multiply(vector);
}
}
1 change: 1 addition & 0 deletions src/Benchmark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public static void Main(string[] args)
typeof(Transforms.FFT),
typeof(LinearAlgebra.DenseMatrixProduct),
typeof(LinearAlgebra.DenseVector),
typeof(LinearAlgebra.SparseVector),
});

switcher.Run(args);
Expand Down
157 changes: 135 additions & 22 deletions src/Numerics.Tests/LinearAlgebraTests/Complex/MatrixTests.Arithmetic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@ namespace MathNet.Numerics.Tests.LinearAlgebraTests.Complex
/// </summary>
public abstract partial class MatrixTests
{
private static Vector<Complex> GetVector(string name, Complex[] data)
{
switch (name)
{
case "Dense": return Vector<Complex>.Build.Dense(data);
case "Sparse": return Vector<Complex>.Build.SparseOfArray(data);
case "User": return new UserDefinedVector(data);
default: throw new NotImplementedException($"{nameof(GetVector)}(string, Complex[]) for {nameof(name)}=\"{name}\"");
}
}

private static Vector<Complex> GetVector(string name, int size)
{
switch (name)
{
case "Dense": return Vector<Complex>.Build.Dense(size);
case "Sparse": return Vector<Complex>.Build.Sparse(size);
case "User": return new UserDefinedVector(size);
default: throw new NotImplementedException($"{nameof(GetVector)}(string, int) for {nameof(name)}=\"{name}\"");
}
}

/// <summary>
/// Can multiply with a complex number.
/// </summary>
Expand Down Expand Up @@ -68,10 +90,10 @@ public void CanMultiplyWithComplex(double real)
/// Can multiply with a vector.
/// </summary>
[Test]
public void CanMultiplyWithVector()
public void CanMultiplyWithVector([Values("Dense", "Sparse", "User")] string vec)
{
var matrix = TestMatrices["Singular3x3"];
var x = new DenseVector(new[] { new Complex(1, 1), new Complex(2, 1), new Complex(3, 1) });
var x = GetVector(vec, new[] { new Complex(1, 1), new Complex(2, 1), new Complex(3, 1) });
var y = matrix * x;

Assert.AreEqual(matrix.RowCount, y.Count);
Expand All @@ -88,11 +110,13 @@ public void CanMultiplyWithVector()
/// Can multiply with a vector into a result.
/// </summary>
[Test]
public void CanMultiplyWithVectorIntoResult()
public void CanMultiplyWithVectorIntoResult(
[Values("Dense", "Sparse", "User")] string vecX,
[Values("Dense", "Sparse", "User")] string vecY)
{
var matrix = TestMatrices["Singular3x3"];
var x = new DenseVector(new[] { new Complex(1, 1), new Complex(2, 1), new Complex(3, 1) });
var y = new DenseVector(3);
var x = GetVector(vecX, new[] { new Complex(1, 1), new Complex(2, 1), new Complex(3, 1) });
var y = GetVector(vecY, 3);
matrix.Multiply(x, y);

for (var i = 0; i < matrix.RowCount; i++)
Expand All @@ -107,16 +131,18 @@ public void CanMultiplyWithVectorIntoResult()
/// Can multiply with a vector into result when updating input argument.
/// </summary>
[Test]
public void CanMultiplyWithVectorIntoResultWhenUpdatingInputArgument()
public void CanMultiplyWithVectorIntoResultWhenUpdatingInputArgument(
[Values("Dense", "Sparse", "User")] string vecX,
[Values("Dense", "Sparse", "User")] string vecY)
{
var matrix = TestMatrices["Singular3x3"];
var x = new DenseVector(new[] { new Complex(1, 1), new Complex(2, 1), new Complex(3, 1) });
var x = GetVector(vecX, new[] { new Complex(1, 1), new Complex(2, 1), new Complex(3, 1) });
var y = x;
matrix.Multiply(x, x);

Assert.AreSame(y, x);

y = new DenseVector(new[] { new Complex(1, 1), new Complex(2, 1), new Complex(3, 1) });
y = GetVector(vecY, new[] { new Complex(1, 1), new Complex(2, 1), new Complex(3, 1) });
for (var i = 0; i < matrix.RowCount; i++)
{
var ar = matrix.Row(i);
Expand All @@ -129,11 +155,13 @@ public void CanMultiplyWithVectorIntoResultWhenUpdatingInputArgument()
/// Multiply with a vector into too large result throws <c>ArgumentException</c>.
/// </summary>
[Test]
public void MultiplyWithVectorIntoLargerResultThrowsArgumentException()
public void MultiplyWithVectorIntoLargerResultThrowsArgumentException(
[Values("Dense", "Sparse", "User")] string vecX,
[Values("Dense", "Sparse", "User")] string vecY)
{
var matrix = TestMatrices["Singular3x3"];
var x = new DenseVector(new[] { new Complex(1, 1), new Complex(2, 1), new Complex(3, 1) });
Vector<Complex> y = new DenseVector(4);
var x = GetVector(vecX, new[] { new Complex(1, 1), new Complex(2, 1), new Complex(3, 1) });
var y = GetVector(vecY, 4);
Assert.That(() => matrix.Multiply(x, y), Throws.ArgumentException);
}

Expand Down Expand Up @@ -614,10 +642,10 @@ public virtual void CanMultiplyMatrixWithMatrixIntoResult(string nameA, string n
/// Can multiply transposed matrix with a vector.
/// </summary>
[Test]
public void CanTransposeThisAndMultiplyWithVector()
public void CanTransposeThisAndMultiplyWithVector([Values("Dense", "Sparse", "User")] string vec)
{
var matrix = TestMatrices["Singular3x3"];
var x = new DenseVector(new[] { new Complex(1, 1), new Complex(2, 1), new Complex(3, 1) });
var x = GetVector(vec, new[] { new Complex(1, 1), new Complex(2, 1), new Complex(3, 1) });
var y = matrix.TransposeThisAndMultiply(x);

Assert.AreEqual(matrix.ColumnCount, y.Count);
Expand All @@ -634,11 +662,13 @@ public void CanTransposeThisAndMultiplyWithVector()
/// Can multiply transposed matrix with a vector into a result.
/// </summary>
[Test]
public void CanTransposeThisAndMultiplyWithVectorIntoResult()
public void CanTransposeThisAndMultiplyWithVectorIntoResult(
[Values("Dense", "Sparse", "User")] string vecX,
[Values("Dense", "Sparse", "User")] string vecY)
{
var matrix = TestMatrices["Singular3x3"];
var x = new DenseVector(new[] { new Complex(1, 1), new Complex(2, 1), new Complex(3, 1) });
var y = new DenseVector(3);
var x = GetVector(vecX, new[] { new Complex(1, 1), new Complex(2, 1), new Complex(3, 1) });
var y = GetVector(vecY, 3);
matrix.TransposeThisAndMultiply(x, y);

for (var j = 0; j < matrix.ColumnCount; j++)
Expand All @@ -653,16 +683,18 @@ public void CanTransposeThisAndMultiplyWithVectorIntoResult()
/// Can multiply transposed matrix with a vector into result when updating input argument.
/// </summary>
[Test]
public void CanTransposeThisAndMultiplyWithVectorIntoResultWhenUpdatingInputArgument()
public void CanTransposeThisAndMultiplyWithVectorIntoResultWhenUpdatingInputArgument(
[Values("Dense", "Sparse", "User")] string vecX,
[Values("Dense", "Sparse", "User")] string vecY)
{
var matrix = TestMatrices["Singular3x3"];
var x = new DenseVector(new[] { new Complex(1, 1), new Complex(2, 1), new Complex(3, 1) });
var x = GetVector(vecX, new[] { new Complex(1, 1), new Complex(2, 1), new Complex(3, 1) });
var y = x;
matrix.TransposeThisAndMultiply(x, x);

Assert.AreSame(y, x);

y = new DenseVector(new[] { new Complex(1, 1), new Complex(2, 1), new Complex(3, 1) });
y = GetVector(vecY, new[] { new Complex(1, 1), new Complex(2, 1), new Complex(3, 1) });
for (var j = 0; j < matrix.ColumnCount; j++)
{
var ar = matrix.Column(j);
Expand All @@ -675,14 +707,95 @@ public void CanTransposeThisAndMultiplyWithVectorIntoResultWhenUpdatingInputArgu
/// Multiply transposed matrix with a vector into too large result throws <c>ArgumentException</c>.
/// </summary>
[Test]
public void TransposeThisAndMultiplyWithVectorIntoLargerResultThrowsArgumentException()
public void TransposeThisAndMultiplyWithVectorIntoLargerResultThrowsArgumentException(
[Values("Dense", "Sparse", "User")] string vecX,
[Values("Dense", "Sparse", "User")] string vecY)
{
var matrix = TestMatrices["Singular3x3"];
var x = new DenseVector(new[] { new Complex(1, 1), new Complex(2, 1), new Complex(3, 1) });
Vector<Complex> y = new DenseVector(4);
var x = GetVector(vecX, new[] { new Complex(1, 1), new Complex(2, 1), new Complex(3, 1) });
var y = GetVector(vecY, 4);
Assert.That(() => matrix.TransposeThisAndMultiply(x, y), Throws.ArgumentException);
}

/// <summary>
/// Can multiply conjugate transposed matrix with a vector.
/// </summary>
[Test]
public void CanConjugateTransposeThisAndMultiplyWithVector([Values("Dense", "Sparse", "User")] string vec)
{
var matrix = TestMatrices["Singular3x3"];
var x = GetVector(vec, new[] { new Complex(1, 1), new Complex(2, 1), new Complex(3, 1) });
var y = matrix.ConjugateTransposeThisAndMultiply(x);

Assert.AreEqual(matrix.ColumnCount, y.Count);

for (var j = 0; j < matrix.ColumnCount; j++)
{
var ar = matrix.Column(j);
var dot = ar.ConjugateDotProduct(x);
Assert.AreEqual(dot, y[j]);
}
}

/// <summary>
/// Can multiply conjugate transposed matrix with a vector into a result.
/// </summary>
[Test]
public void CanConjugateTransposeThisAndMultiplyWithVectorIntoResult(
[Values("Dense", "Sparse", "User")] string vecX,
[Values("Dense", "Sparse", "User")] string vecY)
{
var matrix = TestMatrices["Singular3x3"];
var x = GetVector(vecX, new[] { new Complex(1, 1), new Complex(2, 1), new Complex(3, 1) });
var y = GetVector(vecY, 3);
matrix.ConjugateTransposeThisAndMultiply(x, y);

for (var j = 0; j < matrix.ColumnCount; j++)
{
var ar = matrix.Column(j);
var dot = ar.ConjugateDotProduct(x);
Assert.AreEqual(dot, y[j]);
}
}

/// <summary>
/// Can multiply conjugate transposed matrix with a vector into result when updating input argument.
/// </summary>
[Test]
public void CanConjugateTransposeThisAndMultiplyWithVectorIntoResultWhenUpdatingInputArgument(
[Values("Dense", "Sparse", "User")] string vecX,
[Values("Dense", "Sparse", "User")] string vecY)
{
var matrix = TestMatrices["Singular3x3"];
var x = GetVector(vecX, new[] { new Complex(1, 1), new Complex(2, 1), new Complex(3, 1) });
var y = x;
matrix.ConjugateTransposeThisAndMultiply(x, x);

Assert.AreSame(y, x);

y = GetVector(vecY, new[] { new Complex(1, 1), new Complex(2, 1), new Complex(3, 1) });
for (var j = 0; j < matrix.ColumnCount; j++)
{
var ar = matrix.Column(j);
var dot = ar.ConjugateDotProduct(y);
Assert.AreEqual(dot, x[j]);
}
}

/// <summary>
/// Multiply conjugate transposed matrix with a vector into too large result throws <c>ArgumentException</c>.
/// </summary>
[Test]
public void ConjugateTransposeThisAndMultiplyWithVectorIntoLargerResultThrowsArgumentException(
[Values("Dense", "Sparse", "User")] string vecX,
[Values("Dense", "Sparse", "User")] string vecY)
{
var matrix = TestMatrices["Singular3x3"];
var x = GetVector(vecX, new[] { new Complex(1, 1), new Complex(2, 1), new Complex(3, 1) });
var y = GetVector(vecY, 4);
Assert.That(() => matrix.ConjugateTransposeThisAndMultiply(x, y), Throws.ArgumentException);
}

/// <summary>
/// Can multiply transposed matrix with another matrix.
/// </summary>
Expand Down
Loading