Browse Source

LA: Subtract a scalar from a matrix, and a matrix from a scalar

optimization-1
Christoph Ruegg 13 years ago
parent
commit
841e1f8843
  1. 24
      src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs
  2. 16
      src/Numerics/LinearAlgebra/Complex/Matrix.cs
  3. 24
      src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs
  4. 16
      src/Numerics/LinearAlgebra/Complex32/Matrix.cs
  5. 24
      src/Numerics/LinearAlgebra/Double/DenseMatrix.cs
  6. 16
      src/Numerics/LinearAlgebra/Double/Matrix.cs
  7. 97
      src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs
  8. 42
      src/Numerics/LinearAlgebra/Generic/Matrix.Operators.cs
  9. 24
      src/Numerics/LinearAlgebra/Single/DenseMatrix.cs
  10. 16
      src/Numerics/LinearAlgebra/Single/Matrix.cs

24
src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs

@ -631,6 +631,30 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
}
}
/// <summary>
/// Subtracts a scalar from each element of the matrix and stores the result in the result vector.
/// </summary>
/// <param name="scalar">The scalar to subtract.</param>
/// <param name="result">The matrix to store the result of the subtraction.</param>
protected override void DoSubtract(Complex scalar, Matrix<Complex> result)
{
var denseResult = result as DenseMatrix;
if (denseResult == null)
{
base.DoSubtract(scalar, result);
return;
}
CommonParallel.For(0, _values.Length, 4096, (a, b) =>
{
var v = denseResult._values;
for (int i = a; i < b; i++)
{
v[i] = _values[i] - scalar;
}
});
}
/// <summary>
/// Subtracts another matrix from this matrix.
/// </summary>

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

@ -163,6 +163,22 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
}
}
/// <summary>
/// Subtracts a scalar from each element of the vector and stores the result in the result vector.
/// </summary>
/// <param name="scalar">The scalar to subtract.</param>
/// <param name="result">The matrix to store the result of the subtraction.</param>
protected override void DoSubtract(Complex scalar, Matrix<Complex> result)
{
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j < ColumnCount; j++)
{
result.At(i, j, At(i, j) - scalar);
}
}
}
/// <summary>
/// Subtracts another matrix from this matrix.
/// </summary>

24
src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs

@ -626,6 +626,30 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
}
}
/// <summary>
/// Subtracts a scalar from each element of the matrix and stores the result in the result vector.
/// </summary>
/// <param name="scalar">The scalar to subtract.</param>
/// <param name="result">The matrix to store the result of the subtraction.</param>
protected override void DoSubtract(Complex32 scalar, Matrix<Complex32> result)
{
var denseResult = result as DenseMatrix;
if (denseResult == null)
{
base.DoSubtract(scalar, result);
return;
}
CommonParallel.For(0, _values.Length, 4096, (a, b) =>
{
var v = denseResult._values;
for (int i = a; i < b; i++)
{
v[i] = _values[i] - scalar;
}
});
}
/// <summary>
/// Subtracts another matrix from this matrix.
/// </summary>

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

@ -158,6 +158,22 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
}
}
/// <summary>
/// Subtracts a scalar from each element of the vector and stores the result in the result vector.
/// </summary>
/// <param name="scalar">The scalar to subtract.</param>
/// <param name="result">The matrix to store the result of the subtraction.</param>
protected override void DoSubtract(Complex32 scalar, Matrix<Complex32> result)
{
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j < ColumnCount; j++)
{
result.At(i, j, At(i, j) - scalar);
}
}
}
/// <summary>
/// Subtracts another matrix from this matrix.
/// </summary>

24
src/Numerics/LinearAlgebra/Double/DenseMatrix.cs

@ -393,6 +393,30 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
}
/// <summary>
/// Subtracts a scalar from each element of the matrix and stores the result in the result vector.
/// </summary>
/// <param name="scalar">The scalar to subtract.</param>
/// <param name="result">The matrix to store the result of the subtraction.</param>
protected override void DoSubtract(double scalar, Matrix<double> result)
{
var denseResult = result as DenseMatrix;
if (denseResult == null)
{
base.DoSubtract(scalar, result);
return;
}
CommonParallel.For(0, _values.Length, 4096, (a, b) =>
{
var v = denseResult._values;
for (int i = a; i < b; i++)
{
v[i] = _values[i] - scalar;
}
});
}
/// <summary>
/// Subtracts another matrix from this matrix.
/// </summary>

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

@ -147,6 +147,22 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
}
}
/// <summary>
/// Subtracts a scalar from each element of the vector and stores the result in the result vector.
/// </summary>
/// <param name="scalar">The scalar to subtract.</param>
/// <param name="result">The matrix to store the result of the subtraction.</param>
protected override void DoSubtract(double scalar, Matrix<double> result)
{
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j < ColumnCount; j++)
{
result.At(i, j, At(i, j) - scalar);
}
}
}
/// <summary>
/// Subtracts another matrix from this matrix.

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

@ -77,6 +77,24 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// <exception cref="ArgumentOutOfRangeException">If the two matrices don't have the same dimensions.</exception>
protected abstract void DoAdd(Matrix<T> other, Matrix<T> result);
/// <summary>
/// Subtracts a scalar from each element of the matrix and stores the result in the result matrix.
/// </summary>
/// <param name="scalar">The scalar to subtract.</param>
/// <param name="result">The matrix to store the result of the subtraction.</param>
protected abstract void DoSubtract(T scalar, Matrix<T> result);
/// <summary>
/// Subtracts each element of the matrix from a scalar and stores the result in the result matrix.
/// </summary>
/// <param name="scalar">The scalar to subtract from.</param>
/// <param name="result">The matrix to store the result of the subtraction.</param>
protected virtual void DoSubtractFrom(T scalar, Matrix<T> result)
{
DoNegate(result);
result.DoAdd(scalar, result);
}
/// <summary>
/// Subtracts another matrix from this matrix.
/// </summary>
@ -257,6 +275,85 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
DoAdd(other, result);
}
/// <summary>
/// Subtracts a scalar from each element of the matrix.
/// </summary>
/// <param name="scalar">The scalar to subtract.</param>
/// <returns>A new matrix containing the subtraction of this matrix and the scalar.</returns>
public Matrix<T> Subtract(T scalar)
{
if (scalar.Equals(Zero))
{
return Clone();
}
var result = CreateMatrix(RowCount, ColumnCount);
DoSubtract(scalar, result);
return result;
}
/// <summary>
/// Subtracts a scalar from each element of the matrix and stores the result in the result matrix.
/// </summary>
/// <param name="scalar">The scalar to subtract.</param>
/// <param name="result">The matrix to store the result of the subtraction.</param>
/// <exception cref="ArgumentNullException">If the result matrix is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">If this matrix and <paramref name="result"/> are not the same size.</exception>
public void Subtract(T scalar, Matrix<T> result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (result.RowCount != RowCount || result.ColumnCount != ColumnCount)
{
throw DimensionsDontMatch<ArgumentOutOfRangeException>(this, result, "result");
}
if (scalar.Equals(Zero))
{
CopyTo(result);
return;
}
DoSubtract(scalar, result);
}
/// <summary>
/// Subtracts each element of the matrix from a scalar.
/// </summary>
/// <param name="scalar">The scalar to subtract from.</param>
/// <returns>A new matrix containing the subtraction of the scalar and this matrix.</returns>
public Matrix<T> SubtractFrom(T scalar)
{
var result = CreateMatrix(RowCount, ColumnCount);
DoSubtractFrom(scalar, result);
return result;
}
/// <summary>
/// Subtracts each element of the matrix from a scalar and stores the result in the result matrix.
/// </summary>
/// <param name="scalar">The scalar to subtract from.</param>
/// <param name="result">The matrix to store the result of the subtraction.</param>
/// <exception cref="ArgumentNullException">If the result matrix is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">If this matrix and <paramref name="result"/> are not the same size.</exception>
public void SubtractFrom(T scalar, Matrix<T> result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (result.RowCount != RowCount || result.ColumnCount != ColumnCount)
{
throw DimensionsDontMatch<ArgumentOutOfRangeException>(this, result, "result");
}
DoSubtractFrom(scalar, result);
}
/// <summary>
/// Subtracts another matrix from this matrix.
/// </summary>

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

@ -137,7 +137,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// is denser.</remarks>
/// <param name="leftSide">The left matrix to subtract.</param>
/// <param name="rightSide">The right matrix to subtract.</param>
/// <returns>The result of the addition.</returns>
/// <returns>The result of the subtraction.</returns>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="leftSide"/> and <paramref name="rightSide"/> don't have the same dimensions.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> or <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static Matrix<T> operator -(Matrix<T> leftSide, Matrix<T> rightSide)
@ -150,6 +150,46 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
return leftSide.Subtract(rightSide);
}
/// <summary>
/// Subtracts a scalar from each element of a matrix.
/// </summary>
/// <remarks>This operator will allocate new memory for the result. It will
/// choose the representation of the provided matrix.</remarks>
/// <param name="leftSide">The left matrix to subtract.</param>
/// <param name="rightSide">The scalar value to subtract.</param>
/// <returns>The result of the subtraction.</returns>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="leftSide"/> and <paramref name="rightSide"/> don't have the same dimensions.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> or <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static Matrix<T> operator -(Matrix<T> leftSide, T rightSide)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
return leftSide.Subtract(rightSide);
}
/// <summary>
/// Substracts each element of a matrix from a scalar.
/// </summary>
/// <remarks>This operator will allocate new memory for the result. It will
/// choose the representation of the provided matrix.</remarks>
/// <param name="leftSide">The scalar value to subtract.</param>
/// <param name="rightSide">The right matrix to subtract.</param>
/// <returns>The result of the subtraction.</returns>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="leftSide"/> and <paramref name="rightSide"/> don't have the same dimensions.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> or <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.SubtractFrom(leftSide);
}
/// <summary>
/// Multiplies a <strong>Matrix</strong> by a constant and returns the result.
/// </summary>

24
src/Numerics/LinearAlgebra/Single/DenseMatrix.cs

@ -393,6 +393,30 @@ namespace MathNet.Numerics.LinearAlgebra.Single
}
}
/// <summary>
/// Subtracts a scalar from each element of the matrix and stores the result in the result vector.
/// </summary>
/// <param name="scalar">The scalar to subtract.</param>
/// <param name="result">The matrix to store the result of the subtraction.</param>
protected override void DoSubtract(float scalar, Matrix<float> result)
{
var denseResult = result as DenseMatrix;
if (denseResult == null)
{
base.DoSubtract(scalar, result);
return;
}
CommonParallel.For(0, _values.Length, 4096, (a, b) =>
{
var v = denseResult._values;
for (int i = a; i < b; i++)
{
v[i] = _values[i] - scalar;
}
});
}
/// <summary>
/// Subtracts another matrix from this matrix.
/// </summary>

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

@ -148,6 +148,22 @@ namespace MathNet.Numerics.LinearAlgebra.Single
}
}
/// <summary>
/// Subtracts a scalar from each element of the vector and stores the result in the result vector.
/// </summary>
/// <param name="scalar">The scalar to subtract.</param>
/// <param name="result">The matrix to store the result of the subtraction.</param>
protected override void DoSubtract(float scalar, Matrix<float> result)
{
for (var i = 0; i < RowCount; i++)
{
for (var j = 0; j < ColumnCount; j++)
{
result.At(i, j, At(i, j) - scalar);
}
}
}
/// <summary>
/// Subtracts another matrix from this matrix.
/// </summary>

Loading…
Cancel
Save