Browse Source

Matrix multiplication with scalar.

la-knuth
Jurgen Van Gael 17 years ago
parent
commit
db6092aec0
  1. 51
      src/Numerics/LinearAlgebra/Double/Matrix.Arithmetic.cs
  2. 67
      src/UnitTests/LinearAlgebraTests/Double/MatrixTests.Arithmetic.cs

51
src/Numerics/LinearAlgebra/Double/Matrix.Arithmetic.cs

@ -38,6 +38,57 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </summary>
public abstract partial class Matrix
{
/// <summary>
/// Multiplies each element of this matrix with a scalar.
/// </summary>
/// <param name="scalar">The scalar to multiply with.</param>
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);
}
});
}
/// <summary>
/// Multiplies each element of the matrix by a scalar and places results into the result matrix.
/// </summary>
/// <param name="scalar">The scalar to multiply the matrix with.</param>
/// <param name="result">The matrix to multiply.</param>
/// <exception cref="ArgumentNullException">If the result matrix is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If the result matrix's dimensions are not the same as this matrix.</exception>
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);
}
/// <summary>
/// Adds another matrix to this matrix. The result will be written into this matrix.
/// </summary>

67
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")]

Loading…
Cancel
Save