From dc7c8a996090dbd027125dba976b89cef82dc4f2 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Fri, 2 Aug 2013 23:00:21 +0200 Subject: [PATCH] LA: Subtract a scalar from a matrix, and a matrix from a scalar --- .../LinearAlgebra/Complex/DenseMatrix.cs | 24 +++++ src/Numerics/LinearAlgebra/Complex/Matrix.cs | 16 +++ .../LinearAlgebra/Complex32/DenseMatrix.cs | 24 +++++ .../LinearAlgebra/Complex32/Matrix.cs | 16 +++ .../LinearAlgebra/Double/DenseMatrix.cs | 24 +++++ src/Numerics/LinearAlgebra/Double/Matrix.cs | 16 +++ .../Generic/Matrix.Arithmetic.cs | 97 +++++++++++++++++++ .../LinearAlgebra/Generic/Matrix.Operators.cs | 42 +++++++- .../LinearAlgebra/Single/DenseMatrix.cs | 24 +++++ src/Numerics/LinearAlgebra/Single/Matrix.cs | 16 +++ 10 files changed, 298 insertions(+), 1 deletion(-) diff --git a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs index 23404015..b877dd94 100644 --- a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs @@ -675,6 +675,30 @@ namespace MathNet.Numerics.LinearAlgebra.Complex } } + /// + /// Subtracts a scalar from each element of the matrix and stores the result in the result vector. + /// + /// The scalar to subtract. + /// The matrix to store the result of the subtraction. + protected override void DoSubtract(Complex scalar, Matrix 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; + } + }); + } + /// /// Subtracts another matrix from this matrix. /// diff --git a/src/Numerics/LinearAlgebra/Complex/Matrix.cs b/src/Numerics/LinearAlgebra/Complex/Matrix.cs index b0e4a676..aa2ab0ca 100644 --- a/src/Numerics/LinearAlgebra/Complex/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/Matrix.cs @@ -163,6 +163,22 @@ namespace MathNet.Numerics.LinearAlgebra.Complex } } + /// + /// Subtracts a scalar from each element of the vector and stores the result in the result vector. + /// + /// The scalar to subtract. + /// The matrix to store the result of the subtraction. + protected override void DoSubtract(Complex scalar, Matrix result) + { + for (var i = 0; i < RowCount; i++) + { + for (var j = 0; j < ColumnCount; j++) + { + result.At(i, j, At(i, j) - scalar); + } + } + } + /// /// Subtracts another matrix from this matrix. /// diff --git a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs index bebc39dc..b96f8224 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs @@ -670,6 +670,30 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 } } + /// + /// Subtracts a scalar from each element of the matrix and stores the result in the result vector. + /// + /// The scalar to subtract. + /// The matrix to store the result of the subtraction. + protected override void DoSubtract(Complex32 scalar, Matrix 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; + } + }); + } + /// /// Subtracts another matrix from this matrix. /// diff --git a/src/Numerics/LinearAlgebra/Complex32/Matrix.cs b/src/Numerics/LinearAlgebra/Complex32/Matrix.cs index f83ef515..ce58a024 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Matrix.cs @@ -158,6 +158,22 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 } } + /// + /// Subtracts a scalar from each element of the vector and stores the result in the result vector. + /// + /// The scalar to subtract. + /// The matrix to store the result of the subtraction. + protected override void DoSubtract(Complex32 scalar, Matrix result) + { + for (var i = 0; i < RowCount; i++) + { + for (var j = 0; j < ColumnCount; j++) + { + result.At(i, j, At(i, j) - scalar); + } + } + } + /// /// Subtracts another matrix from this matrix. /// diff --git a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs index 53930330..cb170313 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs @@ -437,6 +437,30 @@ namespace MathNet.Numerics.LinearAlgebra.Double } } + /// + /// Subtracts a scalar from each element of the matrix and stores the result in the result vector. + /// + /// The scalar to subtract. + /// The matrix to store the result of the subtraction. + protected override void DoSubtract(double scalar, Matrix 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; + } + }); + } + /// /// Subtracts another matrix from this matrix. /// diff --git a/src/Numerics/LinearAlgebra/Double/Matrix.cs b/src/Numerics/LinearAlgebra/Double/Matrix.cs index 5dc2f064..7e95114f 100644 --- a/src/Numerics/LinearAlgebra/Double/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Double/Matrix.cs @@ -147,6 +147,22 @@ namespace MathNet.Numerics.LinearAlgebra.Double } } } + + /// + /// Subtracts a scalar from each element of the vector and stores the result in the result vector. + /// + /// The scalar to subtract. + /// The matrix to store the result of the subtraction. + protected override void DoSubtract(double scalar, Matrix result) + { + for (var i = 0; i < RowCount; i++) + { + for (var j = 0; j < ColumnCount; j++) + { + result.At(i, j, At(i, j) - scalar); + } + } + } /// /// Subtracts another matrix from this matrix. diff --git a/src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs b/src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs index 58a021e8..6fc9c39d 100644 --- a/src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs +++ b/src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs @@ -77,6 +77,24 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// If the two matrices don't have the same dimensions. protected abstract void DoAdd(Matrix other, Matrix result); + /// + /// Subtracts a scalar from each element of the matrix and stores the result in the result matrix. + /// + /// The scalar to subtract. + /// The matrix to store the result of the subtraction. + protected abstract void DoSubtract(T scalar, Matrix result); + + /// + /// Subtracts each element of the matrix from a scalar and stores the result in the result matrix. + /// + /// The scalar to subtract from. + /// The matrix to store the result of the subtraction. + protected virtual void DoSubtractFrom(T scalar, Matrix result) + { + DoNegate(result); + result.DoAdd(scalar, result); + } + /// /// Subtracts another matrix from this matrix. /// @@ -257,6 +275,85 @@ namespace MathNet.Numerics.LinearAlgebra.Generic DoAdd(other, result); } + /// + /// Subtracts a scalar from each element of the matrix. + /// + /// The scalar to subtract. + /// A new matrix containing the subtraction of this matrix and the scalar. + public Matrix Subtract(T scalar) + { + if (scalar.Equals(Zero)) + { + return Clone(); + } + + var result = CreateMatrix(RowCount, ColumnCount); + DoSubtract(scalar, result); + return result; + } + + /// + /// Subtracts a scalar from each element of the matrix and stores the result in the result matrix. + /// + /// The scalar to subtract. + /// The matrix to store the result of the subtraction. + /// If the result matrix is . + /// If this matrix and are not the same size. + public void Subtract(T scalar, Matrix result) + { + if (result == null) + { + throw new ArgumentNullException("result"); + } + + if (result.RowCount != RowCount || result.ColumnCount != ColumnCount) + { + throw DimensionsDontMatch(this, result, "result"); + } + + if (scalar.Equals(Zero)) + { + CopyTo(result); + return; + } + + DoSubtract(scalar, result); + } + + /// + /// Subtracts each element of the matrix from a scalar. + /// + /// The scalar to subtract from. + /// A new matrix containing the subtraction of the scalar and this matrix. + public Matrix SubtractFrom(T scalar) + { + var result = CreateMatrix(RowCount, ColumnCount); + DoSubtractFrom(scalar, result); + return result; + } + + /// + /// Subtracts each element of the matrix from a scalar and stores the result in the result matrix. + /// + /// The scalar to subtract from. + /// The matrix to store the result of the subtraction. + /// If the result matrix is . + /// If this matrix and are not the same size. + public void SubtractFrom(T scalar, Matrix result) + { + if (result == null) + { + throw new ArgumentNullException("result"); + } + + if (result.RowCount != RowCount || result.ColumnCount != ColumnCount) + { + throw DimensionsDontMatch(this, result, "result"); + } + + DoSubtractFrom(scalar, result); + } + /// /// Subtracts another matrix from this matrix. /// diff --git a/src/Numerics/LinearAlgebra/Generic/Matrix.Operators.cs b/src/Numerics/LinearAlgebra/Generic/Matrix.Operators.cs index 227bbc78..a59e47a9 100644 --- a/src/Numerics/LinearAlgebra/Generic/Matrix.Operators.cs +++ b/src/Numerics/LinearAlgebra/Generic/Matrix.Operators.cs @@ -137,7 +137,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// is denser. /// The left matrix to subtract. /// The right matrix to subtract. - /// The result of the addition. + /// The result of the subtraction. /// If and don't have the same dimensions. /// If or is . public static Matrix operator -(Matrix leftSide, Matrix rightSide) @@ -150,6 +150,46 @@ namespace MathNet.Numerics.LinearAlgebra.Generic return leftSide.Subtract(rightSide); } + /// + /// Subtracts a scalar from each element of a matrix. + /// + /// This operator will allocate new memory for the result. It will + /// choose the representation of the provided matrix. + /// The left matrix to subtract. + /// The scalar value to subtract. + /// The result of the subtraction. + /// If and don't have the same dimensions. + /// If or is . + public static Matrix operator -(Matrix leftSide, T rightSide) + { + if (leftSide == null) + { + throw new ArgumentNullException("leftSide"); + } + + return leftSide.Subtract(rightSide); + } + + /// + /// Substracts each element of a matrix from a scalar. + /// + /// This operator will allocate new memory for the result. It will + /// choose the representation of the provided matrix. + /// The scalar value to subtract. + /// The right matrix to subtract. + /// The result of the subtraction. + /// If and don't have the same dimensions. + /// If or is . + public static Matrix operator -(T leftSide, Matrix rightSide) + { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + return rightSide.SubtractFrom(leftSide); + } + /// /// Multiplies a Matrix by a constant and returns the result. /// diff --git a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs index 6e7150ac..9397b3a2 100644 --- a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs @@ -437,6 +437,30 @@ namespace MathNet.Numerics.LinearAlgebra.Single } } + /// + /// Subtracts a scalar from each element of the matrix and stores the result in the result vector. + /// + /// The scalar to subtract. + /// The matrix to store the result of the subtraction. + protected override void DoSubtract(float scalar, Matrix 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; + } + }); + } + /// /// Subtracts another matrix from this matrix. /// diff --git a/src/Numerics/LinearAlgebra/Single/Matrix.cs b/src/Numerics/LinearAlgebra/Single/Matrix.cs index 49617a4d..1d9a0a05 100644 --- a/src/Numerics/LinearAlgebra/Single/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Single/Matrix.cs @@ -148,6 +148,22 @@ namespace MathNet.Numerics.LinearAlgebra.Single } } + /// + /// Subtracts a scalar from each element of the vector and stores the result in the result vector. + /// + /// The scalar to subtract. + /// The matrix to store the result of the subtraction. + protected override void DoSubtract(float scalar, Matrix result) + { + for (var i = 0; i < RowCount; i++) + { + for (var j = 0; j < ColumnCount; j++) + { + result.At(i, j, At(i, j) - scalar); + } + } + } + /// /// Subtracts another matrix from this matrix. ///