Browse Source

LA: Devide a matrix by a scalar, or a scalar by a matrix

v2
Christoph Ruegg 13 years ago
parent
commit
9d8b3e3342
  1. 16
      src/Numerics/LinearAlgebra/Complex/Matrix.cs
  2. 16
      src/Numerics/LinearAlgebra/Complex32/Matrix.cs
  3. 16
      src/Numerics/LinearAlgebra/Double/Matrix.cs
  4. 46
      src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs
  5. 34
      src/Numerics/LinearAlgebra/Generic/Matrix.Operators.cs
  6. 16
      src/Numerics/LinearAlgebra/Single/Matrix.cs

16
src/Numerics/LinearAlgebra/Complex/Matrix.cs

@ -264,6 +264,22 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
DoMultiply(1.0 / scalar, result);
}
/// <summary>
/// Divides a scalar by each element of the matrix and stores the result in the result matrix.
/// </summary>
/// <param name="scalar">The scalar to add.</param>
/// <param name="result">The matrix to store the result of the division.</param>
protected override void DoDivideByThis(Complex scalar, Matrix<Complex> result)
{
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j < ColumnCount; j++)
{
result.At(i, j, scalar / At(i, j));
}
}
}
/// <summary>
/// Multiplies this matrix with transpose of another matrix and places the results into the result matrix.
/// </summary>

16
src/Numerics/LinearAlgebra/Complex32/Matrix.cs

@ -237,6 +237,22 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
DoMultiply(1.0f / scalar, result);
}
/// <summary>
/// Divides a scalar by each element of the matrix and stores the result in the result matrix.
/// </summary>
/// <param name="scalar">The scalar to add.</param>
/// <param name="result">The matrix to store the result of the division.</param>
protected override void DoDivideByThis(Complex32 scalar, Matrix<Complex32> result)
{
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j < ColumnCount; j++)
{
result.At(i, j, scalar / At(i, j));
}
}
}
/// <summary>
/// Multiplies this matrix with another matrix and places the results into the result matrix.
/// </summary>

16
src/Numerics/LinearAlgebra/Double/Matrix.cs

@ -227,6 +227,22 @@ namespace MathNet.Numerics.LinearAlgebra.Double
DoMultiply(1.0 / scalar, result);
}
/// <summary>
/// Divides a scalar by each element of the matrix and stores the result in the result matrix.
/// </summary>
/// <param name="scalar">The scalar to add.</param>
/// <param name="result">The matrix to store the result of the division.</param>
protected override void DoDivideByThis(double scalar, Matrix<double> result)
{
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j < ColumnCount; j++)
{
result.At(i, j, scalar / At(i, j));
}
}
}
/// <summary>
/// Multiplies this matrix with another matrix and places the results into the result matrix.
/// </summary>

46
src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs

@ -151,6 +151,13 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// <param name="result">The matrix to store the result of the division.</param>
protected abstract void DoDivide(T scalar, Matrix<T> result);
/// <summary>
/// Divides a scalar by each element of the matrix and stores the result in the result matrix.
/// </summary>
/// <param name="scalar">The scalar to divide.</param>
/// <param name="result">The matrix to store the result of the division.</param>
protected abstract void DoDivideByThis(T scalar, Matrix<T> result);
/// <summary>
/// Computes the modulus for each element of the matrix.
/// </summary>
@ -531,6 +538,45 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
DoDivide(scalar, result);
}
/// <summary>
/// Divides a scalar by each element of the matrix.
/// </summary>
/// <param name="scalar">The scalar to divide.</param>
/// <returns>The result of the division.</returns>
public Matrix<T> DivideByThis(T scalar)
{
var result = CreateMatrix(RowCount, ColumnCount);
DoDivideByThis(scalar, result);
return result;
}
/// <summary>
/// Divides a scalar by each element of the matrix and places results into the result matrix.
/// </summary>
/// <param name="scalar">The scalar to divide.</param>
/// <param name="result">The matrix to store the result of the division.</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 void DivideByThis(T scalar, Matrix<T> result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (result.RowCount != RowCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "result");
}
if (result.ColumnCount != ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension, "result");
}
DoDivideByThis(scalar, result);
}
/// <summary>
/// Multiplies this matrix by a vector and returns the result.
/// </summary>

34
src/Numerics/LinearAlgebra/Generic/Matrix.Operators.cs

@ -279,6 +279,40 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
return rightSide.LeftMultiply(leftSide);
}
/// <summary>
/// Divides a scalar with a matrix.
/// </summary>
/// <param name="leftSide">The scalar to divide.</param>
/// <param name="rightSide">The matrix.</param>
/// <returns>The result of the division.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static Matrix<T> operator /(T leftSide, Matrix<T> rightSide)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
return rightSide.DivideByThis(leftSide);
}
/// <summary>
/// Divides a matrix with a scalar.
/// </summary>
/// <param name="leftSide">The matrix to divide.</param>
/// <param name="rightSide">The scalar value.</param>
/// <returns>The result of the division.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> is <see langword="null" />.</exception>
public static Matrix<T> operator /(Matrix<T> leftSide, T rightSide)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
return leftSide.Divide(rightSide);
}
/// <summary>
/// Multiplies a <strong>Matrix</strong> by a constant and returns the result.
/// </summary>

16
src/Numerics/LinearAlgebra/Single/Matrix.cs

@ -249,6 +249,22 @@ namespace MathNet.Numerics.LinearAlgebra.Single
DoMultiply(1.0f / scalar, result);
}
/// <summary>
/// Divides a scalar by each element of the matrix and stores the result in the result matrix.
/// </summary>
/// <param name="scalar">The scalar to add.</param>
/// <param name="result">The matrix to store the result of the division.</param>
protected override void DoDivideByThis(float scalar, Matrix<float> result)
{
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j < ColumnCount; j++)
{
result.At(i, j, scalar / At(i, j));
}
}
}
/// <summary>
/// Multiplies this matrix with transpose of another matrix and places the results into the result matrix.
/// </summary>

Loading…
Cancel
Save