Browse Source

LA: vector and matrix pointwise min/max and absmin/absmax

v3
Christoph Ruegg 10 years ago
parent
commit
58b9f63e6a
  1. 42
      src/Numerics/LinearAlgebra/Complex/Matrix.cs
  2. 42
      src/Numerics/LinearAlgebra/Complex/Vector.cs
  3. 84
      src/Numerics/LinearAlgebra/Complex32/Matrix.cs
  4. 42
      src/Numerics/LinearAlgebra/Complex32/Vector.cs
  5. 42
      src/Numerics/LinearAlgebra/Double/Matrix.cs
  6. 42
      src/Numerics/LinearAlgebra/Double/Vector.cs
  7. 227
      src/Numerics/LinearAlgebra/Matrix.Arithmetic.cs
  8. 42
      src/Numerics/LinearAlgebra/Single/Matrix.cs
  9. 42
      src/Numerics/LinearAlgebra/Single/Vector.cs
  10. 227
      src/Numerics/LinearAlgebra/Vector.Arithmetic.cs
  11. 123
      src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs
  12. 60
      src/UnitTests/LinearAlgebraTests/Single/VectorTests.cs

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

@ -499,6 +499,48 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return sum;
}
protected override void DoPointwiseMinimum(Complex scalar, Matrix<Complex> result)
{
throw new NotSupportedException();
}
protected override void DoPointwiseMaximum(Complex scalar, Matrix<Complex> result)
{
throw new NotSupportedException();
}
protected override void DoPointwiseAbsoluteMinimum(Complex scalar, Matrix<Complex> result)
{
double absolute = scalar.Magnitude;
Map(x => Math.Min(absolute, x.Magnitude), result, Zeros.AllowSkip);
}
protected override void DoPointwiseAbsoluteMaximum(Complex scalar, Matrix<Complex> result)
{
double absolute = scalar.Magnitude;
Map(x => Math.Max(absolute, x.Magnitude), result, Zeros.Include);
}
protected override void DoPointwiseMinimum(Matrix<Complex> other, Matrix<Complex> result)
{
throw new NotSupportedException();
}
protected override void DoPointwiseMaximum(Matrix<Complex> other, Matrix<Complex> result)
{
throw new NotSupportedException();
}
protected override void DoPointwiseAbsoluteMinimum(Matrix<Complex> other, Matrix<Complex> result)
{
Map2((x, y) => Math.Min(x.Magnitude, y.Magnitude), other, result, Zeros.AllowSkip);
}
protected override void DoPointwiseAbsoluteMaximum(Matrix<Complex> other, Matrix<Complex> result)
{
Map2((x, y) => Math.Max(x.Magnitude, y.Magnitude), other, result, Zeros.AllowSkip);
}
/// <summary>Calculates the induced L1 norm of this matrix.</summary>
/// <returns>The maximum absolute column sum of the matrix.</returns>
public override double L1Norm()

42
src/Numerics/LinearAlgebra/Complex/Vector.cs

@ -328,6 +328,48 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
throw new NotSupportedException();
}
protected override void DoPointwiseMinimum(Complex scalar, Vector<Complex> result)
{
throw new NotSupportedException();
}
protected override void DoPointwiseMaximum(Complex scalar, Vector<Complex> result)
{
throw new NotSupportedException();
}
protected override void DoPointwiseAbsoluteMinimum(Complex scalar, Vector<Complex> result)
{
double absolute = scalar.Magnitude;
Map(x => Math.Min(absolute, x.Magnitude), result, Zeros.AllowSkip);
}
protected override void DoPointwiseAbsoluteMaximum(Complex scalar, Vector<Complex> result)
{
double absolute = scalar.Magnitude;
Map(x => Math.Max(absolute, x.Magnitude), result, Zeros.Include);
}
protected override void DoPointwiseMinimum(Vector<Complex> other, Vector<Complex> result)
{
throw new NotSupportedException();
}
protected override void DoPointwiseMaximum(Vector<Complex> other, Vector<Complex> result)
{
throw new NotSupportedException();
}
protected override void DoPointwiseAbsoluteMinimum(Vector<Complex> other, Vector<Complex> result)
{
Map2((x, y) => Math.Min(x.Magnitude, y.Magnitude), other, result, Zeros.AllowSkip);
}
protected override void DoPointwiseAbsoluteMaximum(Vector<Complex> other, Vector<Complex> result)
{
Map2((x, y) => Math.Max(x.Magnitude, y.Magnitude), other, result, Zeros.AllowSkip);
}
/// <summary>
/// Returns the value of the absolute minimum element.
/// </summary>

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

@ -473,6 +473,69 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return (svd.U * w * svd.VT).Transpose();
}
/// <summary>
/// Computes the trace of this matrix.
/// </summary>
/// <returns>The trace of this matrix</returns>
/// <exception cref="ArgumentException">If the matrix is not square</exception>
public override Complex32 Trace()
{
if (RowCount != ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSquare);
}
var sum = Complex32.Zero;
for (var i = 0; i < RowCount; i++)
{
sum += At(i, i);
}
return sum;
}
protected override void DoPointwiseMinimum(Complex32 scalar, Matrix<Complex32> result)
{
throw new NotSupportedException();
}
protected override void DoPointwiseMaximum(Complex32 scalar, Matrix<Complex32> result)
{
throw new NotSupportedException();
}
protected override void DoPointwiseAbsoluteMinimum(Complex32 scalar, Matrix<Complex32> result)
{
float absolute = scalar.Magnitude;
Map(x => Math.Min(absolute, x.Magnitude), result, Zeros.AllowSkip);
}
protected override void DoPointwiseAbsoluteMaximum(Complex32 scalar, Matrix<Complex32> result)
{
float absolute = scalar.Magnitude;
Map(x => Math.Max(absolute, x.Magnitude), result, Zeros.Include);
}
protected override void DoPointwiseMinimum(Matrix<Complex32> other, Matrix<Complex32> result)
{
throw new NotSupportedException();
}
protected override void DoPointwiseMaximum(Matrix<Complex32> other, Matrix<Complex32> result)
{
throw new NotSupportedException();
}
protected override void DoPointwiseAbsoluteMinimum(Matrix<Complex32> other, Matrix<Complex32> result)
{
Map2((x, y) => Math.Min(x.Magnitude, y.Magnitude), other, result, Zeros.AllowSkip);
}
protected override void DoPointwiseAbsoluteMaximum(Matrix<Complex32> other, Matrix<Complex32> result)
{
Map2((x, y) => Math.Max(x.Magnitude, y.Magnitude), other, result, Zeros.AllowSkip);
}
/// <summary>Calculates the induced L1 norm of this matrix.</summary>
/// <returns>The maximum absolute column sum of the matrix.</returns>
public override double L1Norm()
@ -659,27 +722,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return Vector<Complex32>.Build.Dense(ret);
}
/// <summary>
/// Computes the trace of this matrix.
/// </summary>
/// <returns>The trace of this matrix</returns>
/// <exception cref="ArgumentException">If the matrix is not square</exception>
public override Complex32 Trace()
{
if (RowCount != ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSquare);
}
var sum = Complex32.Zero;
for (var i = 0; i < RowCount; i++)
{
sum += At(i, i);
}
return sum;
}
/// <summary>
/// Evaluates whether this matrix is hermitian (conjugate symmetric).
/// </summary>

42
src/Numerics/LinearAlgebra/Complex32/Vector.cs

@ -323,6 +323,48 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
throw new NotSupportedException();
}
protected override void DoPointwiseMinimum(Complex32 scalar, Vector<Complex32> result)
{
throw new NotSupportedException();
}
protected override void DoPointwiseMaximum(Complex32 scalar, Vector<Complex32> result)
{
throw new NotSupportedException();
}
protected override void DoPointwiseAbsoluteMinimum(Complex32 scalar, Vector<Complex32> result)
{
float absolute = scalar.Magnitude;
Map(x => Math.Min(absolute, x.Magnitude), result, Zeros.AllowSkip);
}
protected override void DoPointwiseAbsoluteMaximum(Complex32 scalar, Vector<Complex32> result)
{
float absolute = scalar.Magnitude;
Map(x => Math.Max(absolute, x.Magnitude), result, Zeros.Include);
}
protected override void DoPointwiseMinimum(Vector<Complex32> other, Vector<Complex32> result)
{
throw new NotSupportedException();
}
protected override void DoPointwiseMaximum(Vector<Complex32> other, Vector<Complex32> result)
{
throw new NotSupportedException();
}
protected override void DoPointwiseAbsoluteMinimum(Vector<Complex32> other, Vector<Complex32> result)
{
Map2((x, y) => Math.Min(x.Magnitude, y.Magnitude), other, result, Zeros.AllowSkip);
}
protected override void DoPointwiseAbsoluteMaximum(Vector<Complex32> other, Vector<Complex32> result)
{
Map2((x, y) => Math.Max(x.Magnitude, y.Magnitude), other, result, Zeros.AllowSkip);
}
/// <summary>
/// Returns the value of the absolute minimum element.
/// </summary>

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

@ -464,6 +464,48 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return sum;
}
protected override void DoPointwiseMinimum(double scalar, Matrix<double> result)
{
Map(x => Math.Min(scalar, x), result, scalar >= 0d ? Zeros.AllowSkip : Zeros.Include);
}
protected override void DoPointwiseMaximum(double scalar, Matrix<double> result)
{
Map(x => Math.Max(scalar, x), result, scalar <= 0d ? Zeros.AllowSkip : Zeros.Include);
}
protected override void DoPointwiseAbsoluteMinimum(double scalar, Matrix<double> result)
{
double absolute = Math.Abs(scalar);
Map(x => Math.Min(absolute, Math.Abs(x)), result, Zeros.AllowSkip);
}
protected override void DoPointwiseAbsoluteMaximum(double scalar, Matrix<double> result)
{
double absolute = Math.Abs(scalar);
Map(x => Math.Max(absolute, Math.Abs(x)), result, Zeros.Include);
}
protected override void DoPointwiseMinimum(Matrix<double> other, Matrix<double> result)
{
Map2(Math.Min, other, result, Zeros.AllowSkip);
}
protected override void DoPointwiseMaximum(Matrix<double> other, Matrix<double> result)
{
Map2(Math.Max, other, result, Zeros.AllowSkip);
}
protected override void DoPointwiseAbsoluteMinimum(Matrix<double> other, Matrix<double> result)
{
Map2((x, y) => Math.Min(Math.Abs(x), Math.Abs(y)), other, result, Zeros.AllowSkip);
}
protected override void DoPointwiseAbsoluteMaximum(Matrix<double> other, Matrix<double> result)
{
Map2((x, y) => Math.Max(Math.Abs(x), Math.Abs(y)), other, result, Zeros.AllowSkip);
}
/// <summary>Calculates the induced L1 norm of this matrix.</summary>
/// <returns>The maximum absolute column sum of the matrix.</returns>
public override double L1Norm()

42
src/Numerics/LinearAlgebra/Double/Vector.cs

@ -321,6 +321,48 @@ namespace MathNet.Numerics.LinearAlgebra.Double
Map(x => Euclid.Remainder(dividend, x), result, Zeros.Include);
}
protected override void DoPointwiseMinimum(double scalar, Vector<double> result)
{
Map(x => Math.Min(scalar, x), result, scalar >= 0d ? Zeros.AllowSkip : Zeros.Include);
}
protected override void DoPointwiseMaximum(double scalar, Vector<double> result)
{
Map(x => Math.Max(scalar, x), result, scalar <= 0d ? Zeros.AllowSkip : Zeros.Include);
}
protected override void DoPointwiseAbsoluteMinimum(double scalar, Vector<double> result)
{
double absolute = Math.Abs(scalar);
Map(x => Math.Min(absolute, Math.Abs(x)), result, Zeros.AllowSkip);
}
protected override void DoPointwiseAbsoluteMaximum(double scalar, Vector<double> result)
{
double absolute = Math.Abs(scalar);
Map(x => Math.Max(absolute, Math.Abs(x)), result, Zeros.Include);
}
protected override void DoPointwiseMinimum(Vector<double> other, Vector<double> result)
{
Map2(Math.Min, other, result, Zeros.AllowSkip);
}
protected override void DoPointwiseMaximum(Vector<double> other, Vector<double> result)
{
Map2(Math.Max, other, result, Zeros.AllowSkip);
}
protected override void DoPointwiseAbsoluteMinimum(Vector<double> other, Vector<double> result)
{
Map2((x, y) => Math.Min(Math.Abs(x), Math.Abs(y)), other, result, Zeros.AllowSkip);
}
protected override void DoPointwiseAbsoluteMaximum(Vector<double> other, Vector<double> result)
{
Map2((x, y) => Math.Max(Math.Abs(x), Math.Abs(y)), other, result, Zeros.AllowSkip);
}
/// <summary>
/// Returns the value of the absolute minimum element.
/// </summary>

227
src/Numerics/LinearAlgebra/Matrix.Arithmetic.cs

@ -3,7 +3,7 @@
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
//
// Copyright (c) 2009-2013 Math.NET
// Copyright (c) 2009-2016 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
@ -265,6 +265,15 @@ namespace MathNet.Numerics.LinearAlgebra
/// <param name="result">The matrix to store the result.</param>
protected abstract void DoPointwiseLog(Matrix<T> result);
protected abstract void DoPointwiseMinimum(T scalar, Matrix<T> result);
protected abstract void DoPointwiseMinimum(Matrix<T> other, Matrix<T> result);
protected abstract void DoPointwiseMaximum(T scalar, Matrix<T> result);
protected abstract void DoPointwiseMaximum(Matrix<T> other, Matrix<T> result);
protected abstract void DoPointwiseAbsoluteMinimum(T scalar, Matrix<T> result);
protected abstract void DoPointwiseAbsoluteMinimum(Matrix<T> other, Matrix<T> result);
protected abstract void DoPointwiseAbsoluteMaximum(T scalar, Matrix<T> result);
protected abstract void DoPointwiseAbsoluteMaximum(Matrix<T> other, Matrix<T> result);
/// <summary>
/// Adds a scalar to each element of the matrix.
/// </summary>
@ -1668,6 +1677,222 @@ namespace MathNet.Numerics.LinearAlgebra
}
}
/// <summary>
/// Pointwise applies the minimum with a scalar to each value.
/// </summary>
/// <param name="scalar">The scalar value to compare to.</param>
public Matrix<T> PointwiseMinimum(T scalar)
{
var result = Build.SameAs(this);
DoPointwiseMinimum(scalar, result);
return result;
}
/// <summary>
/// Pointwise applies the minimum with a scalar to each value.
/// </summary>
/// <param name="scalar">The scalar value to compare to.</param>
/// <param name="result">The vector to store the result.</param>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public void PointwiseMinimum(T scalar, Matrix<T> result)
{
if (ColumnCount != result.ColumnCount || RowCount != result.RowCount)
{
throw DimensionsDontMatch<ArgumentException>(this, result);
}
DoPointwiseMinimum(scalar, result);
}
/// <summary>
/// Pointwise applies the maximum with a scalar to each value.
/// </summary>
/// <param name="scalar">The scalar value to compare to.</param>
public Matrix<T> PointwiseMaximum(T scalar)
{
var result = Build.SameAs(this);
DoPointwiseMaximum(scalar, result);
return result;
}
/// <summary>
/// Pointwise applies the maximum with a scalar to each value.
/// </summary>
/// <param name="scalar">The scalar value to compare to.</param>
/// <param name="result">The matrix to store the result.</param>
/// <exception cref="ArgumentException">If this matrix and <paramref name="result"/> are not the same size.</exception>
public void PointwiseMaximum(T scalar, Matrix<T> result)
{
if (ColumnCount != result.ColumnCount || RowCount != result.RowCount)
{
throw DimensionsDontMatch<ArgumentException>(this, result);
}
DoPointwiseMaximum(scalar, result);
}
/// <summary>
/// Pointwise applies the absolute minimum with a scalar to each value.
/// </summary>
/// <param name="scalar">The scalar value to compare to.</param>
public Matrix<T> PointwiseAbsoluteMinimum(T scalar)
{
var result = Build.SameAs(this);
DoPointwiseAbsoluteMinimum(scalar, result);
return result;
}
/// <summary>
/// Pointwise applies the absolute minimum with a scalar to each value.
/// </summary>
/// <param name="scalar">The scalar value to compare to.</param>
/// <param name="result">The matrix to store the result.</param>
/// <exception cref="ArgumentException">If this matrix and <paramref name="result"/> are not the same size.</exception>
public void PointwiseAbsoluteMinimum(T scalar, Matrix<T> result)
{
if (ColumnCount != result.ColumnCount || RowCount != result.RowCount)
{
throw DimensionsDontMatch<ArgumentException>(this, result);
}
DoPointwiseAbsoluteMinimum(scalar, result);
}
/// <summary>
/// Pointwise applies the absolute maximum with a scalar to each value.
/// </summary>
/// <param name="scalar">The scalar value to compare to.</param>
public Matrix<T> PointwiseAbsoluteMaximum(T scalar)
{
var result = Build.SameAs(this);
DoPointwiseAbsoluteMaximum(scalar, result);
return result;
}
/// <summary>
/// Pointwise applies the absolute maximum with a scalar to each value.
/// </summary>
/// <param name="scalar">The scalar value to compare to.</param>
/// <param name="result">The matrix to store the result.</param>
/// <exception cref="ArgumentException">If this matrix and <paramref name="result"/> are not the same size.</exception>
public void PointwiseAbsoluteMaximum(T scalar, Matrix<T> result)
{
if (ColumnCount != result.ColumnCount || RowCount != result.RowCount)
{
throw DimensionsDontMatch<ArgumentException>(this, result);
}
DoPointwiseAbsoluteMaximum(scalar, result);
}
/// <summary>
/// Pointwise applies the minimum with the values of another matrix to each value.
/// </summary>
/// <param name="other">The matrix with the values to compare to.</param>
public Matrix<T> PointwiseMinimum(Matrix<T> other)
{
var result = Build.SameAs(this);
DoPointwiseMinimum(other, result);
return result;
}
/// <summary>
/// Pointwise applies the minimum with the values of another matrix to each value.
/// </summary>
/// <param name="other">The matrix with the values to compare to.</param>
/// <param name="result">The matrix to store the result.</param>
/// <exception cref="ArgumentException">If this matrix and <paramref name="result"/> are not the same size.</exception>
public void PointwiseMinimum(Matrix<T> other, Matrix<T> result)
{
if (ColumnCount != result.ColumnCount || RowCount != result.RowCount || ColumnCount != other.ColumnCount || RowCount != other.RowCount)
{
throw DimensionsDontMatch<ArgumentException>(this, other, result);
}
DoPointwiseMinimum(other, result);
}
/// <summary>
/// Pointwise applies the maximum with the values of another matrix to each value.
/// </summary>
/// <param name="other">The matrix with the values to compare to.</param>
public Matrix<T> PointwiseMaximum(Matrix<T> other)
{
var result = Build.SameAs(this);
DoPointwiseMaximum(other, result);
return result;
}
/// <summary>
/// Pointwise applies the maximum with the values of another matrix to each value.
/// </summary>
/// <param name="other">The matrix with the values to compare to.</param>
/// <param name="result">The matrix to store the result.</param>
/// <exception cref="ArgumentException">If this matrix and <paramref name="result"/> are not the same size.</exception>
public void PointwiseMaximum(Matrix<T> other, Matrix<T> result)
{
if (ColumnCount != result.ColumnCount || RowCount != result.RowCount || ColumnCount != other.ColumnCount || RowCount != other.RowCount)
{
throw DimensionsDontMatch<ArgumentException>(this, other, result);
}
DoPointwiseMaximum(other, result);
}
/// <summary>
/// Pointwise applies the absolute minimum with the values of another matrix to each value.
/// </summary>
/// <param name="other">The matrix with the values to compare to.</param>
public Matrix<T> PointwiseAbsoluteMinimum(Matrix<T> other)
{
var result = Build.SameAs(this);
DoPointwiseAbsoluteMinimum(other, result);
return result;
}
/// <summary>
/// Pointwise applies the absolute minimum with the values of another matrix to each value.
/// </summary>
/// <param name="other">The matrix with the values to compare to.</param>
/// <param name="result">The matrix to store the result.</param>
/// <exception cref="ArgumentException">If this matrix and <paramref name="result"/> are not the same size.</exception>
public void PointwiseAbsoluteMinimum(Matrix<T> other, Matrix<T> result)
{
if (ColumnCount != result.ColumnCount || RowCount != result.RowCount || ColumnCount != other.ColumnCount || RowCount != other.RowCount)
{
throw DimensionsDontMatch<ArgumentException>(this, other, result);
}
DoPointwiseAbsoluteMinimum(other, result);
}
/// <summary>
/// Pointwise applies the absolute maximum with the values of another matrix to each value.
/// </summary>
/// <param name="other">The matrix with the values to compare to.</param>
public Matrix<T> PointwiseAbsoluteMaximum(Matrix<T> other)
{
var result = Build.SameAs(this);
DoPointwiseAbsoluteMaximum(other, result);
return result;
}
/// <summary>
/// Pointwise applies the absolute maximum with the values of another matrix to each value.
/// </summary>
/// <param name="other">The matrix with the values to compare to.</param>
/// <param name="result">The matrix to store the result.</param>
/// <exception cref="ArgumentException">If this matrix and <paramref name="result"/> are not the same size.</exception>
public void PointwiseAbsoluteMaximum(Matrix<T> other, Matrix<T> result)
{
if (ColumnCount != result.ColumnCount || RowCount != result.RowCount || ColumnCount != other.ColumnCount || RowCount != other.RowCount)
{
throw DimensionsDontMatch<ArgumentException>(this, other, result);
}
DoPointwiseAbsoluteMaximum(other, result);
}
/// <summary>Calculates the induced L1 norm of this matrix.</summary>
/// <returns>The maximum absolute column sum of the matrix.</returns>
public abstract double L1Norm();

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

@ -464,6 +464,48 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return sum;
}
protected override void DoPointwiseMinimum(float scalar, Matrix<float> result)
{
Map(x => Math.Min(scalar, x), result, scalar >= 0d ? Zeros.AllowSkip : Zeros.Include);
}
protected override void DoPointwiseMaximum(float scalar, Matrix<float> result)
{
Map(x => Math.Max(scalar, x), result, scalar <= 0d ? Zeros.AllowSkip : Zeros.Include);
}
protected override void DoPointwiseAbsoluteMinimum(float scalar, Matrix<float> result)
{
float absolute = Math.Abs(scalar);
Map(x => Math.Min(absolute, Math.Abs(x)), result, Zeros.AllowSkip);
}
protected override void DoPointwiseAbsoluteMaximum(float scalar, Matrix<float> result)
{
float absolute = Math.Abs(scalar);
Map(x => Math.Max(absolute, Math.Abs(x)), result, Zeros.Include);
}
protected override void DoPointwiseMinimum(Matrix<float> other, Matrix<float> result)
{
Map2(Math.Min, other, result, Zeros.AllowSkip);
}
protected override void DoPointwiseMaximum(Matrix<float> other, Matrix<float> result)
{
Map2(Math.Max, other, result, Zeros.AllowSkip);
}
protected override void DoPointwiseAbsoluteMinimum(Matrix<float> other, Matrix<float> result)
{
Map2((x, y) => Math.Min(Math.Abs(x), Math.Abs(y)), other, result, Zeros.AllowSkip);
}
protected override void DoPointwiseAbsoluteMaximum(Matrix<float> other, Matrix<float> result)
{
Map2((x, y) => Math.Max(Math.Abs(x), Math.Abs(y)), other, result, Zeros.AllowSkip);
}
/// <summary>Calculates the induced L1 norm of this matrix.</summary>
/// <returns>The maximum absolute column sum of the matrix.</returns>
public override double L1Norm()

42
src/Numerics/LinearAlgebra/Single/Vector.cs

@ -321,6 +321,48 @@ namespace MathNet.Numerics.LinearAlgebra.Single
Map(x => Euclid.Remainder(dividend, x), result, Zeros.Include);
}
protected override void DoPointwiseMinimum(float scalar, Vector<float> result)
{
Map(x => Math.Min(scalar, x), result, scalar >= 0d ? Zeros.AllowSkip : Zeros.Include);
}
protected override void DoPointwiseMaximum(float scalar, Vector<float> result)
{
Map(x => Math.Max(scalar, x), result, scalar <= 0d ? Zeros.AllowSkip : Zeros.Include);
}
protected override void DoPointwiseAbsoluteMinimum(float scalar, Vector<float> result)
{
float absolute = Math.Abs(scalar);
Map(x => Math.Min(absolute, Math.Abs(x)), result, Zeros.AllowSkip);
}
protected override void DoPointwiseAbsoluteMaximum(float scalar, Vector<float> result)
{
float absolute = Math.Abs(scalar);
Map(x => Math.Max(absolute, Math.Abs(x)), result, Zeros.Include);
}
protected override void DoPointwiseMinimum(Vector<float> other, Vector<float> result)
{
Map2(Math.Min, other, result, Zeros.AllowSkip);
}
protected override void DoPointwiseMaximum(Vector<float> other, Vector<float> result)
{
Map2(Math.Max, other, result, Zeros.AllowSkip);
}
protected override void DoPointwiseAbsoluteMinimum(Vector<float> other, Vector<float> result)
{
Map2((x, y) => Math.Min(Math.Abs(x), Math.Abs(y)), other, result, Zeros.AllowSkip);
}
protected override void DoPointwiseAbsoluteMaximum(Vector<float> other, Vector<float> result)
{
Map2((x, y) => Math.Max(Math.Abs(x), Math.Abs(y)), other, result, Zeros.AllowSkip);
}
/// <summary>
/// Returns the value of the absolute minimum element.
/// </summary>

227
src/Numerics/LinearAlgebra/Vector.Arithmetic.cs

@ -3,7 +3,7 @@
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
//
// Copyright (c) 2009-2013 Math.NET
// Copyright (c) 2009-2016 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
@ -233,6 +233,15 @@ namespace MathNet.Numerics.LinearAlgebra
/// <param name="result">The vector to store the result.</param>
protected abstract void DoPointwiseLog(Vector<T> result);
protected abstract void DoPointwiseMinimum(T scalar, Vector<T> result);
protected abstract void DoPointwiseMinimum(Vector<T> other, Vector<T> result);
protected abstract void DoPointwiseMaximum(T scalar, Vector<T> result);
protected abstract void DoPointwiseMaximum(Vector<T> other, Vector<T> result);
protected abstract void DoPointwiseAbsoluteMinimum(T scalar, Vector<T> result);
protected abstract void DoPointwiseAbsoluteMinimum(Vector<T> other, Vector<T> result);
protected abstract void DoPointwiseAbsoluteMaximum(T scalar, Vector<T> result);
protected abstract void DoPointwiseAbsoluteMaximum(Vector<T> other, Vector<T> result);
/// <summary>
/// Adds a scalar to each element of the vector.
/// </summary>
@ -1028,6 +1037,222 @@ namespace MathNet.Numerics.LinearAlgebra
return u.OuterProduct(v);
}
/// <summary>
/// Pointwise applies the minimum with a scalar to each value.
/// </summary>
/// <param name="scalar">The scalar value to compare to.</param>
public Vector<T> PointwiseMinimum(T scalar)
{
var result = Build.SameAs(this);
DoPointwiseMinimum(scalar, result);
return result;
}
/// <summary>
/// Pointwise applies the minimum with a scalar to each value.
/// </summary>
/// <param name="scalar">The scalar value to compare to.</param>
/// <param name="result">The vector to store the result.</param>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public void PointwiseMinimum(T scalar, Vector<T> result)
{
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
DoPointwiseMinimum(scalar, result);
}
/// <summary>
/// Pointwise applies the maximum with a scalar to each value.
/// </summary>
/// <param name="scalar">The scalar value to compare to.</param>
public Vector<T> PointwiseMaximum(T scalar)
{
var result = Build.SameAs(this);
DoPointwiseMaximum(scalar, result);
return result;
}
/// <summary>
/// Pointwise applies the maximum with a scalar to each value.
/// </summary>
/// <param name="scalar">The scalar value to compare to.</param>
/// <param name="result">The vector to store the result.</param>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public void PointwiseMaximum(T scalar, Vector<T> result)
{
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
DoPointwiseMaximum(scalar, result);
}
/// <summary>
/// Pointwise applies the absolute minimum with a scalar to each value.
/// </summary>
/// <param name="scalar">The scalar value to compare to.</param>
public Vector<T> PointwiseAbsoluteMinimum(T scalar)
{
var result = Build.SameAs(this);
DoPointwiseAbsoluteMinimum(scalar, result);
return result;
}
/// <summary>
/// Pointwise applies the absolute minimum with a scalar to each value.
/// </summary>
/// <param name="scalar">The scalar value to compare to.</param>
/// <param name="result">The vector to store the result.</param>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public void PointwiseAbsoluteMinimum(T scalar, Vector<T> result)
{
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
DoPointwiseAbsoluteMinimum(scalar, result);
}
/// <summary>
/// Pointwise applies the absolute maximum with a scalar to each value.
/// </summary>
/// <param name="scalar">The scalar value to compare to.</param>
public Vector<T> PointwiseAbsoluteMaximum(T scalar)
{
var result = Build.SameAs(this);
DoPointwiseAbsoluteMaximum(scalar, result);
return result;
}
/// <summary>
/// Pointwise applies the absolute maximum with a scalar to each value.
/// </summary>
/// <param name="scalar">The scalar value to compare to.</param>
/// <param name="result">The vector to store the result.</param>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public void PointwiseAbsoluteMaximum(T scalar, Vector<T> result)
{
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
DoPointwiseAbsoluteMaximum(scalar, result);
}
/// <summary>
/// Pointwise applies the minimum with the values of another vector to each value.
/// </summary>
/// <param name="other">The vector with the values to compare to.</param>
public Vector<T> PointwiseMinimum(Vector<T> other)
{
var result = Build.SameAs(this);
DoPointwiseMinimum(other, result);
return result;
}
/// <summary>
/// Pointwise applies the minimum with the values of another vector to each value.
/// </summary>
/// <param name="other">The vector with the values to compare to.</param>
/// <param name="result">The vector to store the result.</param>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public void PointwiseMinimum(Vector<T> other, Vector<T> result)
{
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
DoPointwiseMinimum(other, result);
}
/// <summary>
/// Pointwise applies the maximum with the values of another vector to each value.
/// </summary>
/// <param name="other">The vector with the values to compare to.</param>
public Vector<T> PointwiseMaximum(Vector<T> other)
{
var result = Build.SameAs(this);
DoPointwiseMaximum(other, result);
return result;
}
/// <summary>
/// Pointwise applies the maximum with the values of another vector to each value.
/// </summary>
/// <param name="other">The vector with the values to compare to.</param>
/// <param name="result">The vector to store the result.</param>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public void PointwiseMaximum(Vector<T> other, Vector<T> result)
{
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
DoPointwiseMaximum(other, result);
}
/// <summary>
/// Pointwise applies the absolute minimum with the values of another vector to each value.
/// </summary>
/// <param name="other">The vector with the values to compare to.</param>
public Vector<T> PointwiseAbsoluteMinimum(Vector<T> other)
{
var result = Build.SameAs(this);
DoPointwiseAbsoluteMinimum(other, result);
return result;
}
/// <summary>
/// Pointwise applies the absolute minimum with the values of another vector to each value.
/// </summary>
/// <param name="other">The vector with the values to compare to.</param>
/// <param name="result">The vector to store the result.</param>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public void PointwiseAbsoluteMinimum(Vector<T> other, Vector<T> result)
{
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
DoPointwiseAbsoluteMinimum(other, result);
}
/// <summary>
/// Pointwise applies the absolute maximum with the values of another vector to each value.
/// </summary>
/// <param name="other">The vector with the values to compare to.</param>
public Vector<T> PointwiseAbsoluteMaximum(Vector<T> other)
{
var result = Build.SameAs(this);
DoPointwiseAbsoluteMaximum(other, result);
return result;
}
/// <summary>
/// Pointwise applies the absolute maximum with the values of another vector to each value.
/// </summary>
/// <param name="other">The vector with the values to compare to.</param>
/// <param name="result">The vector to store the result.</param>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public void PointwiseAbsoluteMaximum(Vector<T> other, Vector<T> result)
{
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
DoPointwiseAbsoluteMaximum(other, result);
}
/// <summary>
/// Calculates the L1 norm of the vector, also known as Manhattan norm.
/// </summary>

123
src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs

@ -339,108 +339,111 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
Assert.That(() => vector.SubVector(index, length), Throws.TypeOf<ArgumentOutOfRangeException>());
}
/// <summary>
/// Can find absolute minimum value index.
/// </summary>
[Test]
public void CanFindAbsoluteMinimumIndex()
{
var source = CreateVector(Data);
const int Expected = 0;
var actual = source.AbsoluteMinimumIndex();
Assert.AreEqual(Expected, actual);
Assert.AreEqual(0, CreateVector(Data).AbsoluteMinimumIndex());
}
/// <summary>
/// Can find absolute minimum value of a vector.
/// </summary>
[Test]
public void CanFindAbsoluteMinimum()
{
var source = CreateVector(Data);
const double Expected = 1;
var actual = source.AbsoluteMinimum();
Assert.AreEqual(Expected, actual);
Assert.AreEqual(1d, CreateVector(Data).AbsoluteMinimum());
}
/// <summary>
/// Can find absolute maximum value index.
/// </summary>
[Test]
public void CanFindAbsoluteMaximumIndex()
{
var source = CreateVector(Data);
const int Expected = 4;
var actual = source.AbsoluteMaximumIndex();
Assert.AreEqual(Expected, actual);
Assert.AreEqual(4, CreateVector(Data).AbsoluteMaximumIndex());
}
/// <summary>
/// Can find absolute maximum value of a vector.
/// </summary>
[Test]
public void CanFindAbsoluteMaximum()
{
var source = CreateVector(Data);
const double Expected = 5;
var actual = source.AbsoluteMaximum();
Assert.AreEqual(Expected, actual);
Assert.AreEqual(5d, CreateVector(Data).AbsoluteMaximum());
}
/// <summary>
/// Can find maximum value index.
/// </summary>
[Test]
public void CanFindMaximumIndex()
{
var vector = CreateVector(Data);
Assert.AreEqual(4, CreateVector(Data).MaximumIndex());
}
const int Expected = 4;
var actual = vector.MaximumIndex();
[Test]
public void CanFindMaximum()
{
Assert.AreEqual(5d, CreateVector(Data).Maximum());
}
Assert.AreEqual(Expected, actual);
[Test]
public void CanFindMinimumIndex()
{
Assert.AreEqual(0, CreateVector(Data).MinimumIndex());
}
/// <summary>
/// Can find maximum value of a vector.
/// </summary>
[Test]
public void CanFindMaximum()
public void CanFindMinimum()
{
var vector = CreateVector(Data);
Assert.AreEqual(1d, CreateVector(Data).Minimum());
}
const double Expected = 5;
var actual = vector.Maximum();
[Test]
public void PointwiseScalarMinimum()
{
double[] testData = { -20, -10, 10, 20, 30 };
Assert.That(CreateVector(testData).PointwiseMinimum(5d).ToArray(), Is.EqualTo(new double[] { -20, -10, 5, 5, 5 }).AsCollection);
}
Assert.AreEqual(Expected, actual);
[Test]
public void PointwiseScalarMaximum()
{
double[] testData = { -20, -10, 10, 20, 30 };
Assert.That(CreateVector(testData).PointwiseMaximum(5d).ToArray(), Is.EqualTo(new double[] { 5, 5, 10, 20, 30 }).AsCollection);
}
/// <summary>
/// Can find minimum value index.
/// </summary>
[Test]
public void CanFindMinimumIndex()
public void PointwiseScalarAbsoluteMinimum()
{
var vector = CreateVector(Data);
double[] testData = { -20, -10, 10, 20, 30 };
Assert.That(CreateVector(testData).PointwiseAbsoluteMinimum(15d).ToArray(), Is.EqualTo(new double[] { 15, 10, 10, 15, 15 }).AsCollection);
}
const int Expected = 0;
var actual = vector.MinimumIndex();
[Test]
public void PointwiseScalarAbsoluteMaximum()
{
double[] testData = { -20, -10, 10, 20, 30 };
Assert.That(CreateVector(testData).PointwiseAbsoluteMaximum(15d).ToArray(), Is.EqualTo(new double[] { 20, 15, 15, 20, 30 }).AsCollection);
}
Assert.AreEqual(Expected, actual);
[Test]
public void PointwiseVectorMinimum()
{
double[] testData = { -20, -10, 10, 20, 30 };
double[] otherData = { -5, 5, -5, 5, -5 };
Assert.That(CreateVector(testData).PointwiseMinimum(CreateVector(otherData)).ToArray(), Is.EqualTo(new double[] { -20, -10, -5, 5, -5 }).AsCollection);
}
/// <summary>
/// Can find minimum value of a vector.
/// </summary>
[Test]
public void CanFindMinimum()
public void PointwiseVectorMaximum()
{
var vector = CreateVector(Data);
double[] testData = { -20, -10, 10, 20, 30 };
double[] otherData = { -5, 5, -5, 5, -5 };
Assert.That(CreateVector(testData).PointwiseMaximum(CreateVector(otherData)).ToArray(), Is.EqualTo(new double[] { -5, 5, 10, 20, 30 }).AsCollection);
}
const double Expected = 1;
var actual = vector.Minimum();
[Test]
public void PointwiseVectorAbsoluteMinimum()
{
double[] testData = { -20, -10, 10, 20, 30 };
double[] otherData = { -15, 15, -15, 15, -15 };
Assert.That(CreateVector(testData).PointwiseAbsoluteMinimum(CreateVector(otherData)).ToArray(), Is.EqualTo(new double[] { 15, 10, 10, 15, 15 }).AsCollection);
}
Assert.AreEqual(Expected, actual);
[Test]
public void PointwiseVectorAbsoluteMaximum()
{
double[] testData = { -20, -10, 10, 20, 30 };
double[] otherData = { -15, 15, -15, 15, -15 };
Assert.That(CreateVector(testData).PointwiseAbsoluteMaximum(CreateVector(otherData)).ToArray(), Is.EqualTo(new double[] { 20, 15, 15, 20, 30 }).AsCollection);
}
/// <summary>

60
src/UnitTests/LinearAlgebraTests/Single/VectorTests.cs

@ -444,6 +444,66 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
Assert.AreEqual(Expected, actual);
}
[Test]
public void PointwiseScalarMinimum()
{
float[] testData = { -20, -10, 10, 20, 30 };
Assert.That(CreateVector(testData).PointwiseMinimum(5f).ToArray(), Is.EqualTo(new float[] { -20, -10, 5, 5, 5 }).AsCollection);
}
[Test]
public void PointwiseScalarMaximum()
{
float[] testData = { -20, -10, 10, 20, 30 };
Assert.That(CreateVector(testData).PointwiseMaximum(5f).ToArray(), Is.EqualTo(new float[] { 5, 5, 10, 20, 30 }).AsCollection);
}
[Test]
public void PointwiseScalarAbsoluteMinimum()
{
float[] testData = { -20, -10, 10, 20, 30 };
Assert.That(CreateVector(testData).PointwiseAbsoluteMinimum(15f).ToArray(), Is.EqualTo(new float[] { 15, 10, 10, 15, 15 }).AsCollection);
}
[Test]
public void PointwiseScalarAbsoluteMaximum()
{
float[] testData = { -20, -10, 10, 20, 30 };
Assert.That(CreateVector(testData).PointwiseAbsoluteMaximum(15f).ToArray(), Is.EqualTo(new float[] { 20, 15, 15, 20, 30 }).AsCollection);
}
[Test]
public void PointwiseVectorMinimum()
{
float[] testData = { -20, -10, 10, 20, 30 };
float[] otherData = { -5, 5, -5, 5, -5 };
Assert.That(CreateVector(testData).PointwiseMinimum(CreateVector(otherData)).ToArray(), Is.EqualTo(new float[] { -20, -10, -5, 5, -5 }).AsCollection);
}
[Test]
public void PointwiseVectorMaximum()
{
float[] testData = { -20, -10, 10, 20, 30 };
float[] otherData = { -5, 5, -5, 5, -5 };
Assert.That(CreateVector(testData).PointwiseMaximum(CreateVector(otherData)).ToArray(), Is.EqualTo(new float[] { -5, 5, 10, 20, 30 }).AsCollection);
}
[Test]
public void PointwiseVectorAbsoluteMinimum()
{
float[] testData = { -20, -10, 10, 20, 30 };
float[] otherData = { -15, 15, -15, 15, -15 };
Assert.That(CreateVector(testData).PointwiseAbsoluteMinimum(CreateVector(otherData)).ToArray(), Is.EqualTo(new float[] { 15, 10, 10, 15, 15 }).AsCollection);
}
[Test]
public void PointwiseVectorAbsoluteMaximum()
{
float[] testData = { -20, -10, 10, 20, 30 };
float[] otherData = { -15, 15, -15, 15, -15 };
Assert.That(CreateVector(testData).PointwiseAbsoluteMaximum(CreateVector(otherData)).ToArray(), Is.EqualTo(new float[] { 20, 15, 15, 20, 30 }).AsCollection);
}
/// <summary>
/// Can compute the sum of a vector elements.
/// </summary>

Loading…
Cancel
Save