From 41ab904c7f9351aacb8053b34eb29a93880837bb Mon Sep 17 00:00:00 2001 From: Jurgen Van Gael Date: Fri, 18 Dec 2009 00:28:04 +0800 Subject: [PATCH] Added Matrix.Clear method. --- src/Numerics/LinearAlgebra/Double/DenseMatrix.cs | 8 ++++++++ src/Numerics/LinearAlgebra/Double/Matrix.cs | 14 ++++++++++++++ .../LinearAlgebraTests/Double/MatrixTests.cs | 14 ++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs index 04aba072..543b16ef 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs @@ -182,6 +182,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double Data[column * RowCount + row] = value; } + /// + /// Sets all values to zero. + /// + public override void Clear() + { + Array.Clear(Data, 0, Data.Length); + } + #region Elementary operations /// /// Adds another matrix to this matrix. The result will be written into this matrix. diff --git a/src/Numerics/LinearAlgebra/Double/Matrix.cs b/src/Numerics/LinearAlgebra/Double/Matrix.cs index e4bcd869..f179f3d0 100644 --- a/src/Numerics/LinearAlgebra/Double/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Double/Matrix.cs @@ -412,6 +412,20 @@ namespace MathNet.Numerics.LinearAlgebra.Double } #endregion + + /// + /// Sets all values to zero. + /// + public virtual void Clear() + { + for (int i = 0; i < RowCount; i++) + { + for (int j = 0; j < ColumnCount; j++) + { + At(i, j, 0); + } + } + } #region Elementary operations /// diff --git a/src/UnitTests/LinearAlgebraTests/Double/MatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Double/MatrixTests.cs index 0b81e099..f57667ba 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/MatrixTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/MatrixTests.cs @@ -162,6 +162,20 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double { } + [Test] + public void CanClearMatrix() + { + Matrix matrix = (Matrix) testMatrices["Singular3x3"].Clone(); + matrix.Clear(); + for (int i = 0; i < matrix.RowCount; i++) + { + for (int j = 0; j < matrix.ColumnCount; j++) + { + Assert.AreEqual(0, matrix[i, j]); + } + } + } + #region Elementary operations [Test] [Row("Singular3x3", "Square3x3")]