From 7820018d3b9f82650c04c1b06f906ed32c523521 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Fri, 25 Apr 2014 03:15:48 +0200 Subject: [PATCH] LA: restrict modified sparse*diagonal matrix product to square diagonal matrices only --- src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs | 10 ++++++++-- src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs | 2 +- src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs | 10 ++++++++-- src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs | 2 +- src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs | 10 ++++++++-- src/Numerics/LinearAlgebra/Double/SparseMatrix.cs | 2 +- src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs | 10 ++++++++-- src/Numerics/LinearAlgebra/Single/SparseMatrix.cs | 2 +- 8 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs index 7c346957..f796e6e0 100644 --- a/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs @@ -435,8 +435,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex return; } - // TODO: Map is rather generic, we can do better - other.MapIndexed((i, j, x) => _data[i]*x, result, false); + if (RowCount == ColumnCount) + { + // TODO: Map is rather generic, we can do better + other.MapIndexed((i, j, x) => _data[i]*x, result, false); + return; + } + + base.DoMultiply(other, result); } /// diff --git a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs index b115a61c..0be638d2 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs @@ -932,7 +932,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex } var diagonalOther = other as DiagonalMatrix; - if (diagonalOther != null && sparseResult != null) + if (diagonalOther != null && sparseResult != null && other.RowCount == other.ColumnCount) { var diagonal = ((DiagonalMatrixStorage)other.Storage).Data; // TODO: Map is rather generic, we can do better diff --git a/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs index b9778364..2340a8d3 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs @@ -429,8 +429,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return; } - // TODO: Map is rather generic, we can do better - other.MapIndexed((i, j, x) => _data[i]*x, result, false); + if (RowCount == ColumnCount) + { + // TODO: Map is rather generic, we can do better + other.MapIndexed((i, j, x) => _data[i]*x, result, false); + return; + } + + base.DoMultiply(other, result); } /// diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs index 2270099c..5b7f6442 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs @@ -926,7 +926,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 } var diagonalOther = other as DiagonalMatrix; - if (diagonalOther != null && sparseResult != null) + if (diagonalOther != null && sparseResult != null && other.RowCount == other.ColumnCount) { var diagonal = ((DiagonalMatrixStorage)other.Storage).Data; // TODO: Map is rather generic, we can do better diff --git a/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs index 536809eb..dc23fc26 100644 --- a/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs @@ -409,8 +409,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double return; } - // TODO: Map is rather generic, we can do better - other.MapIndexed((i, j, x) => _data[i]*x, result, false); + if (RowCount == ColumnCount) + { + // TODO: Map is rather generic, we can do better + other.MapIndexed((i, j, x) => _data[i]*x, result, false); + return; + } + + base.DoMultiply(other, result); } /// diff --git a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs index 5275fc14..e8137235 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs @@ -928,7 +928,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double } var diagonalOther = other as DiagonalMatrix; - if (diagonalOther != null && sparseResult != null) + if (diagonalOther != null && sparseResult != null && other.RowCount == other.ColumnCount) { var diagonal = ((DiagonalMatrixStorage)other.Storage).Data; // TODO: Map is rather generic, we can do better diff --git a/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs index 807899e7..0492dd1b 100644 --- a/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs @@ -409,8 +409,14 @@ namespace MathNet.Numerics.LinearAlgebra.Single return; } - // TODO: Map is rather generic, we can do better - other.MapIndexed((i, j, x) => _data[i]*x, result, false); + if (RowCount == ColumnCount) + { + // TODO: Map is rather generic, we can do better + other.MapIndexed((i, j, x) => _data[i]*x, result, false); + return; + } + + base.DoMultiply(other, result); } /// diff --git a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs index 280c2971..c3c80fe5 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs @@ -933,7 +933,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single } var diagonalOther = other as DiagonalMatrix; - if (diagonalOther != null && sparseResult != null) + if (diagonalOther != null && sparseResult != null && other.RowCount == other.ColumnCount) { var diagonal = ((DiagonalMatrixStorage)other.Storage).Data; // TODO: Map is rather generic, we can do better