From 9d8b3e3342b20fa1b2d7d131e3f17efb62df23ef Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sat, 3 Aug 2013 11:16:46 +0200 Subject: [PATCH] LA: Devide a matrix by a scalar, or a scalar by a matrix --- src/Numerics/LinearAlgebra/Complex/Matrix.cs | 16 +++++++ .../LinearAlgebra/Complex32/Matrix.cs | 16 +++++++ src/Numerics/LinearAlgebra/Double/Matrix.cs | 16 +++++++ .../Generic/Matrix.Arithmetic.cs | 46 +++++++++++++++++++ .../LinearAlgebra/Generic/Matrix.Operators.cs | 34 ++++++++++++++ src/Numerics/LinearAlgebra/Single/Matrix.cs | 16 +++++++ 6 files changed, 144 insertions(+) diff --git a/src/Numerics/LinearAlgebra/Complex/Matrix.cs b/src/Numerics/LinearAlgebra/Complex/Matrix.cs index aa2ab0ca..d01ed63c 100644 --- a/src/Numerics/LinearAlgebra/Complex/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/Matrix.cs @@ -264,6 +264,22 @@ namespace MathNet.Numerics.LinearAlgebra.Complex DoMultiply(1.0 / scalar, result); } + /// + /// Divides a scalar by each element of the matrix and stores the result in the result matrix. + /// + /// The scalar to add. + /// The matrix to store the result of the division. + protected override void DoDivideByThis(Complex scalar, Matrix result) + { + for (var i = 0; i < RowCount; i++) + { + for (var j = 0; j < ColumnCount; j++) + { + result.At(i, j, scalar / At(i, j)); + } + } + } + /// /// Multiplies this matrix with transpose of another matrix and places the results into the result matrix. /// diff --git a/src/Numerics/LinearAlgebra/Complex32/Matrix.cs b/src/Numerics/LinearAlgebra/Complex32/Matrix.cs index ce58a024..80196c32 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Matrix.cs @@ -237,6 +237,22 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 DoMultiply(1.0f / scalar, result); } + /// + /// Divides a scalar by each element of the matrix and stores the result in the result matrix. + /// + /// The scalar to add. + /// The matrix to store the result of the division. + protected override void DoDivideByThis(Complex32 scalar, Matrix result) + { + for (var i = 0; i < RowCount; i++) + { + for (var j = 0; j < ColumnCount; j++) + { + result.At(i, j, scalar / At(i, j)); + } + } + } + /// /// Multiplies this matrix with another matrix and places the results into the result matrix. /// diff --git a/src/Numerics/LinearAlgebra/Double/Matrix.cs b/src/Numerics/LinearAlgebra/Double/Matrix.cs index 7e95114f..328aaa3e 100644 --- a/src/Numerics/LinearAlgebra/Double/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Double/Matrix.cs @@ -227,6 +227,22 @@ namespace MathNet.Numerics.LinearAlgebra.Double DoMultiply(1.0 / scalar, result); } + /// + /// Divides a scalar by each element of the matrix and stores the result in the result matrix. + /// + /// The scalar to add. + /// The matrix to store the result of the division. + protected override void DoDivideByThis(double scalar, Matrix result) + { + for (var i = 0; i < RowCount; i++) + { + for (var j = 0; j < ColumnCount; j++) + { + result.At(i, j, scalar / At(i, j)); + } + } + } + /// /// Multiplies this matrix with another matrix and places the results into the result matrix. /// diff --git a/src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs b/src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs index 6fc9c39d..1d7a99a2 100644 --- a/src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs +++ b/src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs @@ -151,6 +151,13 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// The matrix to store the result of the division. protected abstract void DoDivide(T scalar, Matrix result); + /// + /// Divides a scalar by each element of the matrix and stores the result in the result matrix. + /// + /// The scalar to divide. + /// The matrix to store the result of the division. + protected abstract void DoDivideByThis(T scalar, Matrix result); + /// /// Computes the modulus for each element of the matrix. /// @@ -531,6 +538,45 @@ namespace MathNet.Numerics.LinearAlgebra.Generic DoDivide(scalar, result); } + /// + /// Divides a scalar by each element of the matrix. + /// + /// The scalar to divide. + /// The result of the division. + public Matrix DivideByThis(T scalar) + { + var result = CreateMatrix(RowCount, ColumnCount); + DoDivideByThis(scalar, result); + return result; + } + + /// + /// Divides a scalar by each element of the matrix and places results into the result matrix. + /// + /// The scalar to divide. + /// The matrix to store the result of the division. + /// If the result matrix is . + /// If the result matrix's dimensions are not the same as this matrix. + public void DivideByThis(T scalar, Matrix 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); + } + /// /// Multiplies this matrix by a vector and returns the result. /// diff --git a/src/Numerics/LinearAlgebra/Generic/Matrix.Operators.cs b/src/Numerics/LinearAlgebra/Generic/Matrix.Operators.cs index a59e47a9..0c6b7049 100644 --- a/src/Numerics/LinearAlgebra/Generic/Matrix.Operators.cs +++ b/src/Numerics/LinearAlgebra/Generic/Matrix.Operators.cs @@ -279,6 +279,40 @@ namespace MathNet.Numerics.LinearAlgebra.Generic return rightSide.LeftMultiply(leftSide); } + /// + /// Divides a scalar with a matrix. + /// + /// The scalar to divide. + /// The matrix. + /// The result of the division. + /// If is . + public static Matrix operator /(T leftSide, Matrix rightSide) + { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + return rightSide.DivideByThis(leftSide); + } + + /// + /// Divides a matrix with a scalar. + /// + /// The matrix to divide. + /// The scalar value. + /// The result of the division. + /// If is . + public static Matrix operator /(Matrix leftSide, T rightSide) + { + if (leftSide == null) + { + throw new ArgumentNullException("leftSide"); + } + + return leftSide.Divide(rightSide); + } + /// /// Multiplies a Matrix by a constant and returns the result. /// diff --git a/src/Numerics/LinearAlgebra/Single/Matrix.cs b/src/Numerics/LinearAlgebra/Single/Matrix.cs index 1d9a0a05..d101691c 100644 --- a/src/Numerics/LinearAlgebra/Single/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Single/Matrix.cs @@ -249,6 +249,22 @@ namespace MathNet.Numerics.LinearAlgebra.Single DoMultiply(1.0f / scalar, result); } + /// + /// Divides a scalar by each element of the matrix and stores the result in the result matrix. + /// + /// The scalar to add. + /// The matrix to store the result of the division. + protected override void DoDivideByThis(float scalar, Matrix result) + { + for (var i = 0; i < RowCount; i++) + { + for (var j = 0; j < ColumnCount; j++) + { + result.At(i, j, scalar / At(i, j)); + } + } + } + /// /// Multiplies this matrix with transpose of another matrix and places the results into the result matrix. ///