From b340fc958ccaa2aee30ee8503ebf95c536ac2145 Mon Sep 17 00:00:00 2001 From: Jurgen Van Gael Date: Sun, 20 Dec 2009 19:45:35 +0800 Subject: [PATCH] Fixed design of the matrix methods. --- .../LinearAlgebra/Double/DenseMatrix.cs | 56 ++++++++++++------- src/Numerics/LinearAlgebra/Double/Matrix.cs | 18 ++++-- 2 files changed, 50 insertions(+), 24 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs index 543b16ef..697f9b6a 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs @@ -198,6 +198,25 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// If the other matrix is . /// If the two matrices don't have the same dimensions. public override void Add(Matrix other) + { + DenseMatrix m = other as DenseMatrix; + if (m == null) + { + base.Add(other); + } + else + { + Add(other); + } + } + + /// + /// Adds another to this matrix. The result will be written into this matrix. + /// + /// The to add to this matrix. + /// If the other matrix is . + /// If the two matrices don't have the same dimensions. + public void Add(DenseMatrix other) { if (other == null) { @@ -209,26 +228,35 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixSameDimensions); } + Control.LinearAlgebraProvider.AddArrays(Data, other.Data, Data); + } + + /// + /// Subtracts another matrix from this matrix. The result will be written into this matrix. + /// + /// The matrix to subtract. + /// If the other matrix is . + /// If the two matrices don't have the same dimensions. + public override void Subtract(Matrix other) + { DenseMatrix m = other as DenseMatrix; if (m == null) { - Parallel.For(0, RowCount, i => - Parallel.For(0, ColumnCount, j => - At(i, j, At(i, j) + other.At(i, j)))); + base.Subtract(other); } else { - Control.LinearAlgebraProvider.AddArrays(Data, m.Data, Data); + Subtract(m); } } /// - /// Subtracts another matrix from this matrix. The result will be written into this matrix. + /// Subtracts another from this matrix. The result will be written into this matrix. /// - /// The matrix to subtract. + /// The to subtract. /// If the other matrix is . /// If the two matrices don't have the same dimensions. - public override void Subtract(Matrix other) + public void Subtract(DenseMatrix other) { if (other == null) { @@ -239,18 +267,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double { throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixSameDimensions); } - - DenseMatrix m = other as DenseMatrix; - if (m == null) - { - Parallel.For(0, RowCount, i => - Parallel.For(0, ColumnCount, j => - At(i, j, At(i, j) - other.At(i, j)))); - } - else - { - Control.LinearAlgebraProvider.SubtractArrays(Data, m.Data, Data); - } + + Control.LinearAlgebraProvider.SubtractArrays(Data, other.Data, Data); } #endregion } diff --git a/src/Numerics/LinearAlgebra/Double/Matrix.cs b/src/Numerics/LinearAlgebra/Double/Matrix.cs index f179f3d0..87dfdd5b 100644 --- a/src/Numerics/LinearAlgebra/Double/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Double/Matrix.cs @@ -446,9 +446,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixSameDimensions); } - Parallel.For(0, RowCount, i => - Parallel.For(0, ColumnCount, j => - At(i, j, At(i,j) + other.At(i,j)))); + Parallel.For(0, RowCount, i => + { + for (int j = 0; j < ColumnCount; j++) + { + At(i, j, At(i, j) + other.At(i, j)); + } + }); } /// @@ -502,8 +506,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double } Parallel.For(0, RowCount, i => - Parallel.For(0, ColumnCount, j => - At(i, j, At(i, j) - other.At(i, j)))); + { + for(int j = 0; j < ColumnCount; j++) + { + At(i, j, At(i, j) - other.At(i, j)); + } + }); } ///