diff --git a/src/Numerics/LinearAlgebra/Double/Matrix.Arithmetic.cs b/src/Numerics/LinearAlgebra/Double/Matrix.Arithmetic.cs index 5891f66b..fc37d6e2 100644 --- a/src/Numerics/LinearAlgebra/Double/Matrix.Arithmetic.cs +++ b/src/Numerics/LinearAlgebra/Double/Matrix.Arithmetic.cs @@ -38,6 +38,57 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// public abstract partial class Matrix { + /// + /// Multiplies each element of this matrix with a scalar. + /// + /// The scalar to multiply with. + public virtual void Multiply(double scalar) + { + if (Precision.AlmostEqualInDecimalPlaces(1.0, scalar, 15)) + { + return; + } + + Parallel.For( + 0, + RowCount, + i => + { + for (int j = 0; j < ColumnCount; j++) + { + At(i, j, At(i, j) * scalar); + } + }); + } + + /// + /// Multiplies each element of the matrix by a scalar and places results into the result matrix. + /// + /// The scalar to multiply the matrix with. + /// The matrix to multiply. + /// If the result matrix is . + /// If the result matrix's dimensions are not the same as this matrix. + public virtual void Multiply(double scalar, Matrix result) + { + if (result == null) + { + throw new ArgumentNullException("result"); + } + + if (result.RowCount != RowCount) + { + throw new ArgumentException("result", Resources.ArgumentMatrixSameRowDimension); + } + + if (result.ColumnCount != ColumnCount) + { + throw new ArgumentException("result", Resources.ArgumentMatrixSameColumnDimension); + } + + CopyTo(result); + result.Multiply(scalar); + } + /// /// Adds another matrix to this matrix. The result will be written into this matrix. /// diff --git a/src/UnitTests/LinearAlgebraTests/Double/MatrixTests.Arithmetic.cs b/src/UnitTests/LinearAlgebraTests/Double/MatrixTests.Arithmetic.cs index 019a6669..b05a3fee 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/MatrixTests.Arithmetic.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/MatrixTests.Arithmetic.cs @@ -8,6 +8,73 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double { public abstract partial class MatrixTests { + [Test] + [Row(0)] + [Row(1)] + [Row(2.2)] + [MultipleAsserts] + public void CanMultiplyWithScalar(double scalar) + { + var matrix = testMatrices["Singular3x3"]; + var clone = matrix.Clone(); + clone.Multiply(scalar); + + for (int i = 0; i < matrix.RowCount; i++) + { + for (int j = 0; j < matrix.ColumnCount; j++) + { + Assert.AreEqual(matrix[i, j] * scalar, clone[i, j]); + } + } + } + + [Test] + [Row(0)] + [Row(1)] + [Row(2.2)] + [MultipleAsserts] + public void CanMultiplyWithScalarIntoResult(double scalar) + { + var matrix = testMatrices["Singular3x3"]; + var result = matrix.Clone(); + matrix.Multiply(scalar, result); + + for (int i = 0; i < matrix.RowCount; i++) + { + for (int j = 0; j < matrix.ColumnCount; j++) + { + Assert.AreEqual(matrix[i, j] * scalar, result[i, j]); + } + } + } + + [Test] + [ExpectedException(typeof(ArgumentNullException))] + public void MultiplyWithScalarIntoResultFailsWhenResultIsNull() + { + var matrix = testMatrices["Singular3x3"]; + Matrix result = null; + matrix.Multiply(2.3, result); + } + + [Test] + [ExpectedArgumentException] + public void MultiplyWithScalarFailsWhenResultHasMoreRows() + { + var matrix = testMatrices["Singular3x3"]; + Matrix result = CreateMatrix(matrix.RowCount + 1, matrix.ColumnCount); + matrix.Multiply(2.3, result); + } + + [Test] + [ExpectedArgumentException] + public void MultiplyWithScalarFailsWhenResultHasMoreColumns() + { + var matrix = testMatrices["Singular3x3"]; + Matrix result = CreateMatrix(matrix.RowCount, matrix.ColumnCount + 1); + matrix.Multiply(2.3, result); + } + [Test] [Row("Singular3x3", "Square3x3")] [Row("Singular4x4", "Square4x4")]