Browse Source

LA: Add a scalar to a matrix

pull/163/head
Christoph Ruegg 13 years ago
parent
commit
90ea9c62e6
  1. 26
      src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs
  2. 16
      src/Numerics/LinearAlgebra/Complex/Matrix.cs
  3. 26
      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. 54
      src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs
  8. 38
      src/Numerics/LinearAlgebra/Generic/Matrix.Operators.cs
  9. 24
      src/Numerics/LinearAlgebra/Single/DenseMatrix.cs
  10. 16
      src/Numerics/LinearAlgebra/Single/Matrix.cs

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

@ -28,6 +28,8 @@
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using MathNet.Numerics.Threading;
namespace MathNet.Numerics.LinearAlgebra.Complex
{
using Algorithms.LinearAlgebra;
@ -584,6 +586,30 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
}
}
/// <summary>
/// Add a scalar to each element of the matrix and stores the result in the result vector.
/// </summary>
/// <param name="scalar">The scalar to add.</param>
/// <param name="result">The matrix to store the result of the addition.</param>
protected override void DoAdd(Complex scalar, Matrix<Complex> 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;
}
});
}
/// <summary>
/// Adds another matrix to this matrix.
/// </summary>

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

@ -129,6 +129,22 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return norm;
}
/// <summary>
/// Add a scalar to each element of the matrix and stores the result in the result vector.
/// </summary>
/// <param name="scalar">The scalar to add.</param>
/// <param name="result">The matrix to store the result of the addition.</param>
protected override void DoAdd(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>
/// Adds another matrix to this matrix.
/// </summary>

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

@ -28,6 +28,8 @@
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using MathNet.Numerics.Threading;
namespace MathNet.Numerics.LinearAlgebra.Complex32
{
using Algorithms.LinearAlgebra;
@ -579,6 +581,30 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
}
}
/// <summary>
/// Add a scalar to each element of the matrix and stores the result in the result vector.
/// </summary>
/// <param name="scalar">The scalar to add.</param>
/// <param name="result">The matrix to store the result of the addition.</param>
protected override void DoAdd(Complex32 scalar, Matrix<Complex32> 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;
}
});
}
/// <summary>
/// Adds another matrix to this matrix.
/// </summary>

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

@ -124,6 +124,22 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return norm;
}
/// <summary>
/// Add a scalar to each element of the matrix and stores the result in the result vector.
/// </summary>
/// <param name="scalar">The scalar to add.</param>
/// <param name="result">The matrix to store the result of the addition.</param>
protected override void DoAdd(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>
/// Adds another matrix to this matrix.
/// </summary>

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

@ -348,6 +348,30 @@ namespace MathNet.Numerics.LinearAlgebra.Double
#endregion
/// <summary>
/// Add a scalar to each element of the matrix and stores the result in the result vector.
/// </summary>
/// <param name="scalar">The scalar to add.</param>
/// <param name="result">The matrix to store the result of the addition.</param>
protected override void DoAdd(double scalar, Matrix<double> 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;
}
});
}
/// <summary>
/// Adds another matrix to this matrix.
/// </summary>

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

@ -114,6 +114,22 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return norm;
}
/// <summary>
/// Add a scalar to each element of the matrix and stores the result in the result vector.
/// </summary>
/// <param name="scalar">The scalar to add.</param>
/// <param name="result">The matrix to store the result of the addition.</param>
protected override void DoAdd(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>
/// Adds another matrix to this matrix.
/// </summary>

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

@ -61,6 +61,13 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// <param name="result">The result of the conjugation.</param>
protected abstract void DoConjugate(Matrix<T> result);
/// <summary>
/// Add a scalar to each element of the matrix and stores the result in the result vector.
/// </summary>
/// <param name="scalar">The scalar to add.</param>
/// <param name="result">The matrix to store the result of the addition.</param>
protected abstract void DoAdd(T scalar, Matrix<T> result);
/// <summary>
/// Adds another matrix to this matrix.
/// </summary>
@ -147,6 +154,53 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// <param name="result">The matrix to store the result of the pointwise division.</param>
protected abstract void DoPointwiseDivide(Matrix<T> other, Matrix<T> result);
/// <summary>
/// Adds a scalar to each element of the matrix.
/// </summary>
/// <param name="scalar">The scalar to add.</param>
/// <returns>The result of the addition.</returns>
/// <exception cref="ArgumentNullException">If the other matrix is <see langword="null"/>.</exception>
/// <exception cref="ArgumentOutOfRangeException">If the two matrices don't have the same dimensions.</exception>
public Matrix<T> Add(T scalar)
{
if (scalar.Equals(Zero))
{
return Clone();
}
var result = CreateMatrix(RowCount, ColumnCount);
DoAdd(scalar, result);
return result;
}
/// <summary>
/// Adds a scalar to 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 addition.</param>
/// <exception cref="ArgumentNullException">If the other matrix is <see langword="null"/>.</exception>
/// <exception cref="ArgumentOutOfRangeException">If the two matrices don't have the same dimensions.</exception>
public void Add(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;
}
DoAdd(scalar, result);
}
/// <summary>
/// Adds another matrix to this matrix.
/// </summary>

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

@ -91,6 +91,44 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
return leftSide.Add(rightSide);
}
/// <summary>
/// Adds a scalar to each element of the 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 add.</param>
/// <param name="rightSide">The scalar value to add.</param>
/// <returns>The result of the addition.</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.Add(rightSide);
}
/// <summary>
/// Adds a scalar to each element of the 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 scalar value to add.</param>
/// <param name="rightSide">The right matrix to add.</param>
/// <returns>The result of the addition.</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.Add(leftSide);
}
/// <summary>
/// Subtracts two matrices together and returns the results.
/// </summary>

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

@ -348,6 +348,30 @@ namespace MathNet.Numerics.LinearAlgebra.Single
#endregion
/// <summary>
/// Add a scalar to each element of the matrix and stores the result in the result vector.
/// </summary>
/// <param name="scalar">The scalar to add.</param>
/// <param name="result">The matrix to store the result of the addition.</param>
protected override void DoAdd(float scalar, Matrix<float> 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;
}
});
}
/// <summary>
/// Adds another matrix to this matrix.
/// </summary>

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

@ -114,6 +114,22 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return norm;
}
/// <summary>
/// Add a scalar to each element of the matrix and stores the result in the result vector.
/// </summary>
/// <param name="scalar">The scalar to add.</param>
/// <param name="result">The matrix to store the result of the addition.</param>
protected override void DoAdd(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>
/// Adds another matrix to this matrix.
/// </summary>

Loading…
Cancel
Save