From 90ea9c62e6eaf9b7bc69da024c3c41d119d37744 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Fri, 2 Aug 2013 22:45:56 +0200 Subject: [PATCH] LA: Add a scalar to a matrix --- .../LinearAlgebra/Complex/DenseMatrix.cs | 26 +++++++++ src/Numerics/LinearAlgebra/Complex/Matrix.cs | 16 ++++++ .../LinearAlgebra/Complex32/DenseMatrix.cs | 26 +++++++++ .../LinearAlgebra/Complex32/Matrix.cs | 16 ++++++ .../LinearAlgebra/Double/DenseMatrix.cs | 24 +++++++++ src/Numerics/LinearAlgebra/Double/Matrix.cs | 16 ++++++ .../Generic/Matrix.Arithmetic.cs | 54 +++++++++++++++++++ .../LinearAlgebra/Generic/Matrix.Operators.cs | 38 +++++++++++++ .../LinearAlgebra/Single/DenseMatrix.cs | 24 +++++++++ src/Numerics/LinearAlgebra/Single/Matrix.cs | 16 ++++++ 10 files changed, 256 insertions(+) diff --git a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs index 7f6fea9d..f87c232c 100644 --- a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs @@ -28,6 +28,8 @@ // OTHER DEALINGS IN THE SOFTWARE. // +using MathNet.Numerics.Threading; + namespace MathNet.Numerics.LinearAlgebra.Complex { using Algorithms.LinearAlgebra; @@ -584,6 +586,30 @@ namespace MathNet.Numerics.LinearAlgebra.Complex } } + /// + /// Add a scalar to each element of the matrix and stores the result in the result vector. + /// + /// The scalar to add. + /// The matrix to store the result of the addition. + protected override void DoAdd(Complex scalar, Matrix result) + { + var denseResult = result as DenseMatrix; + if (denseResult == null) + { + base.DoAdd(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; + } + }); + } + /// /// Adds another matrix to this matrix. /// diff --git a/src/Numerics/LinearAlgebra/Complex/Matrix.cs b/src/Numerics/LinearAlgebra/Complex/Matrix.cs index 70296b71..b0e4a676 100644 --- a/src/Numerics/LinearAlgebra/Complex/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/Matrix.cs @@ -129,6 +129,22 @@ namespace MathNet.Numerics.LinearAlgebra.Complex return norm; } + /// + /// Add a scalar to each element of the matrix and stores the result in the result vector. + /// + /// The scalar to add. + /// The matrix to store the result of the addition. + protected override void DoAdd(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); + } + } + } + /// /// Adds another matrix to this matrix. /// diff --git a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs index 3e1ed738..22e47b3e 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs @@ -28,6 +28,8 @@ // OTHER DEALINGS IN THE SOFTWARE. // +using MathNet.Numerics.Threading; + namespace MathNet.Numerics.LinearAlgebra.Complex32 { using Algorithms.LinearAlgebra; @@ -579,6 +581,30 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 } } + /// + /// Add a scalar to each element of the matrix and stores the result in the result vector. + /// + /// The scalar to add. + /// The matrix to store the result of the addition. + protected override void DoAdd(Complex32 scalar, Matrix result) + { + var denseResult = result as DenseMatrix; + if (denseResult == null) + { + base.DoAdd(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; + } + }); + } + /// /// Adds another matrix to this matrix. /// diff --git a/src/Numerics/LinearAlgebra/Complex32/Matrix.cs b/src/Numerics/LinearAlgebra/Complex32/Matrix.cs index 04a496be..f83ef515 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Matrix.cs @@ -124,6 +124,22 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return norm; } + /// + /// Add a scalar to each element of the matrix and stores the result in the result vector. + /// + /// The scalar to add. + /// The matrix to store the result of the addition. + protected override void DoAdd(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); + } + } + } + /// /// Adds another matrix to this matrix. /// diff --git a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs index c1e0d364..0bb005e5 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs @@ -348,6 +348,30 @@ namespace MathNet.Numerics.LinearAlgebra.Double #endregion + /// + /// Add a scalar to each element of the matrix and stores the result in the result vector. + /// + /// The scalar to add. + /// The matrix to store the result of the addition. + protected override void DoAdd(double scalar, Matrix result) + { + var denseResult = result as DenseMatrix; + if (denseResult == null) + { + base.DoAdd(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; + } + }); + } + /// /// Adds another matrix to this matrix. /// diff --git a/src/Numerics/LinearAlgebra/Double/Matrix.cs b/src/Numerics/LinearAlgebra/Double/Matrix.cs index 724c8638..5dc2f064 100644 --- a/src/Numerics/LinearAlgebra/Double/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Double/Matrix.cs @@ -114,6 +114,22 @@ namespace MathNet.Numerics.LinearAlgebra.Double return norm; } + /// + /// Add a scalar to each element of the matrix and stores the result in the result vector. + /// + /// The scalar to add. + /// The matrix to store the result of the addition. + protected override void DoAdd(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); + } + } + } + /// /// Adds another matrix to this matrix. /// diff --git a/src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs b/src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs index 581543ac..58a021e8 100644 --- a/src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs +++ b/src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs @@ -61,6 +61,13 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// The result of the conjugation. protected abstract void DoConjugate(Matrix result); + /// + /// Add a scalar to each element of the matrix and stores the result in the result vector. + /// + /// The scalar to add. + /// The matrix to store the result of the addition. + protected abstract void DoAdd(T scalar, Matrix result); + /// /// Adds another matrix to this matrix. /// @@ -147,6 +154,53 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// The matrix to store the result of the pointwise division. protected abstract void DoPointwiseDivide(Matrix other, Matrix result); + /// + /// Adds a scalar to each element of the matrix. + /// + /// The scalar to add. + /// The result of the addition. + /// If the other matrix is . + /// If the two matrices don't have the same dimensions. + public Matrix Add(T scalar) + { + if (scalar.Equals(Zero)) + { + return Clone(); + } + + var result = CreateMatrix(RowCount, ColumnCount); + DoAdd(scalar, result); + return result; + } + + /// + /// Adds a scalar to 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 addition. + /// If the other matrix is . + /// If the two matrices don't have the same dimensions. + public void Add(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; + } + + DoAdd(scalar, result); + } + /// /// Adds another matrix to this matrix. /// diff --git a/src/Numerics/LinearAlgebra/Generic/Matrix.Operators.cs b/src/Numerics/LinearAlgebra/Generic/Matrix.Operators.cs index 1462d071..227bbc78 100644 --- a/src/Numerics/LinearAlgebra/Generic/Matrix.Operators.cs +++ b/src/Numerics/LinearAlgebra/Generic/Matrix.Operators.cs @@ -91,6 +91,44 @@ namespace MathNet.Numerics.LinearAlgebra.Generic return leftSide.Add(rightSide); } + /// + /// Adds a scalar to each element of the matrix. + /// + /// This operator will allocate new memory for the result. It will + /// choose the representation of the provided matrix. + /// The left matrix to add. + /// The scalar value to add. + /// The result of the addition. + /// If is . + public static Matrix operator +(Matrix leftSide, T rightSide) + { + if (leftSide == null) + { + throw new ArgumentNullException("leftSide"); + } + + return leftSide.Add(rightSide); + } + + /// + /// Adds a scalar to each element of the matrix. + /// + /// This operator will allocate new memory for the result. It will + /// choose the representation of the provided matrix. + /// The scalar value to add. + /// The right matrix to add. + /// The result of the addition. + /// If is . + public static Matrix operator +(T leftSide, Matrix rightSide) + { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + return rightSide.Add(leftSide); + } + /// /// Subtracts two matrices together and returns the results. /// diff --git a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs index fbec5ab5..1c4c60cf 100644 --- a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs @@ -348,6 +348,30 @@ namespace MathNet.Numerics.LinearAlgebra.Single #endregion + /// + /// Add a scalar to each element of the matrix and stores the result in the result vector. + /// + /// The scalar to add. + /// The matrix to store the result of the addition. + protected override void DoAdd(float scalar, Matrix result) + { + var denseResult = result as DenseMatrix; + if (denseResult == null) + { + base.DoAdd(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; + } + }); + } + /// /// Adds another matrix to this matrix. /// diff --git a/src/Numerics/LinearAlgebra/Single/Matrix.cs b/src/Numerics/LinearAlgebra/Single/Matrix.cs index 9f781a28..49617a4d 100644 --- a/src/Numerics/LinearAlgebra/Single/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Single/Matrix.cs @@ -114,6 +114,22 @@ namespace MathNet.Numerics.LinearAlgebra.Single return norm; } + /// + /// Add a scalar to each element of the matrix and stores the result in the result vector. + /// + /// The scalar to add. + /// The matrix to store the result of the addition. + protected override void DoAdd(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); + } + } + } + /// /// Adds another matrix to this matrix. ///