diff --git a/src/Numerics/LinearAlgebra/Complex/Matrix.cs b/src/Numerics/LinearAlgebra/Complex/Matrix.cs index 1e34ced4..29493d45 100644 --- a/src/Numerics/LinearAlgebra/Complex/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/Matrix.cs @@ -499,6 +499,48 @@ namespace MathNet.Numerics.LinearAlgebra.Complex return sum; } + protected override void DoPointwiseMinimum(Complex scalar, Matrix result) + { + throw new NotSupportedException(); + } + + protected override void DoPointwiseMaximum(Complex scalar, Matrix result) + { + throw new NotSupportedException(); + } + + protected override void DoPointwiseAbsoluteMinimum(Complex scalar, Matrix result) + { + double absolute = scalar.Magnitude; + Map(x => Math.Min(absolute, x.Magnitude), result, Zeros.AllowSkip); + } + + protected override void DoPointwiseAbsoluteMaximum(Complex scalar, Matrix result) + { + double absolute = scalar.Magnitude; + Map(x => Math.Max(absolute, x.Magnitude), result, Zeros.Include); + } + + protected override void DoPointwiseMinimum(Matrix other, Matrix result) + { + throw new NotSupportedException(); + } + + protected override void DoPointwiseMaximum(Matrix other, Matrix result) + { + throw new NotSupportedException(); + } + + protected override void DoPointwiseAbsoluteMinimum(Matrix other, Matrix result) + { + Map2((x, y) => Math.Min(x.Magnitude, y.Magnitude), other, result, Zeros.AllowSkip); + } + + protected override void DoPointwiseAbsoluteMaximum(Matrix other, Matrix result) + { + Map2((x, y) => Math.Max(x.Magnitude, y.Magnitude), other, result, Zeros.AllowSkip); + } + /// Calculates the induced L1 norm of this matrix. /// The maximum absolute column sum of the matrix. public override double L1Norm() diff --git a/src/Numerics/LinearAlgebra/Complex/Vector.cs b/src/Numerics/LinearAlgebra/Complex/Vector.cs index d21f3790..675cbea8 100644 --- a/src/Numerics/LinearAlgebra/Complex/Vector.cs +++ b/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 result) + { + throw new NotSupportedException(); + } + + protected override void DoPointwiseMaximum(Complex scalar, Vector result) + { + throw new NotSupportedException(); + } + + protected override void DoPointwiseAbsoluteMinimum(Complex scalar, Vector result) + { + double absolute = scalar.Magnitude; + Map(x => Math.Min(absolute, x.Magnitude), result, Zeros.AllowSkip); + } + + protected override void DoPointwiseAbsoluteMaximum(Complex scalar, Vector result) + { + double absolute = scalar.Magnitude; + Map(x => Math.Max(absolute, x.Magnitude), result, Zeros.Include); + } + + protected override void DoPointwiseMinimum(Vector other, Vector result) + { + throw new NotSupportedException(); + } + + protected override void DoPointwiseMaximum(Vector other, Vector result) + { + throw new NotSupportedException(); + } + + protected override void DoPointwiseAbsoluteMinimum(Vector other, Vector result) + { + Map2((x, y) => Math.Min(x.Magnitude, y.Magnitude), other, result, Zeros.AllowSkip); + } + + protected override void DoPointwiseAbsoluteMaximum(Vector other, Vector result) + { + Map2((x, y) => Math.Max(x.Magnitude, y.Magnitude), other, result, Zeros.AllowSkip); + } + /// /// Returns the value of the absolute minimum element. /// diff --git a/src/Numerics/LinearAlgebra/Complex32/Matrix.cs b/src/Numerics/LinearAlgebra/Complex32/Matrix.cs index b0490b77..60cdf11b 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Matrix.cs @@ -473,6 +473,69 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return (svd.U * w * svd.VT).Transpose(); } + /// + /// Computes the trace of this matrix. + /// + /// The trace of this matrix + /// If the matrix is not square + 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 result) + { + throw new NotSupportedException(); + } + + protected override void DoPointwiseMaximum(Complex32 scalar, Matrix result) + { + throw new NotSupportedException(); + } + + protected override void DoPointwiseAbsoluteMinimum(Complex32 scalar, Matrix result) + { + float absolute = scalar.Magnitude; + Map(x => Math.Min(absolute, x.Magnitude), result, Zeros.AllowSkip); + } + + protected override void DoPointwiseAbsoluteMaximum(Complex32 scalar, Matrix result) + { + float absolute = scalar.Magnitude; + Map(x => Math.Max(absolute, x.Magnitude), result, Zeros.Include); + } + + protected override void DoPointwiseMinimum(Matrix other, Matrix result) + { + throw new NotSupportedException(); + } + + protected override void DoPointwiseMaximum(Matrix other, Matrix result) + { + throw new NotSupportedException(); + } + + protected override void DoPointwiseAbsoluteMinimum(Matrix other, Matrix result) + { + Map2((x, y) => Math.Min(x.Magnitude, y.Magnitude), other, result, Zeros.AllowSkip); + } + + protected override void DoPointwiseAbsoluteMaximum(Matrix other, Matrix result) + { + Map2((x, y) => Math.Max(x.Magnitude, y.Magnitude), other, result, Zeros.AllowSkip); + } + /// Calculates the induced L1 norm of this matrix. /// The maximum absolute column sum of the matrix. public override double L1Norm() @@ -659,27 +722,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return Vector.Build.Dense(ret); } - /// - /// Computes the trace of this matrix. - /// - /// The trace of this matrix - /// If the matrix is not square - 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; - } - /// /// Evaluates whether this matrix is hermitian (conjugate symmetric). /// diff --git a/src/Numerics/LinearAlgebra/Complex32/Vector.cs b/src/Numerics/LinearAlgebra/Complex32/Vector.cs index 224e0e02..6ce516ee 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Vector.cs +++ b/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 result) + { + throw new NotSupportedException(); + } + + protected override void DoPointwiseMaximum(Complex32 scalar, Vector result) + { + throw new NotSupportedException(); + } + + protected override void DoPointwiseAbsoluteMinimum(Complex32 scalar, Vector result) + { + float absolute = scalar.Magnitude; + Map(x => Math.Min(absolute, x.Magnitude), result, Zeros.AllowSkip); + } + + protected override void DoPointwiseAbsoluteMaximum(Complex32 scalar, Vector result) + { + float absolute = scalar.Magnitude; + Map(x => Math.Max(absolute, x.Magnitude), result, Zeros.Include); + } + + protected override void DoPointwiseMinimum(Vector other, Vector result) + { + throw new NotSupportedException(); + } + + protected override void DoPointwiseMaximum(Vector other, Vector result) + { + throw new NotSupportedException(); + } + + protected override void DoPointwiseAbsoluteMinimum(Vector other, Vector result) + { + Map2((x, y) => Math.Min(x.Magnitude, y.Magnitude), other, result, Zeros.AllowSkip); + } + + protected override void DoPointwiseAbsoluteMaximum(Vector other, Vector result) + { + Map2((x, y) => Math.Max(x.Magnitude, y.Magnitude), other, result, Zeros.AllowSkip); + } + /// /// Returns the value of the absolute minimum element. /// diff --git a/src/Numerics/LinearAlgebra/Double/Matrix.cs b/src/Numerics/LinearAlgebra/Double/Matrix.cs index 1b8c11bb..daae0822 100644 --- a/src/Numerics/LinearAlgebra/Double/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Double/Matrix.cs @@ -464,6 +464,48 @@ namespace MathNet.Numerics.LinearAlgebra.Double return sum; } + protected override void DoPointwiseMinimum(double scalar, Matrix result) + { + Map(x => Math.Min(scalar, x), result, scalar >= 0d ? Zeros.AllowSkip : Zeros.Include); + } + + protected override void DoPointwiseMaximum(double scalar, Matrix result) + { + Map(x => Math.Max(scalar, x), result, scalar <= 0d ? Zeros.AllowSkip : Zeros.Include); + } + + protected override void DoPointwiseAbsoluteMinimum(double scalar, Matrix result) + { + double absolute = Math.Abs(scalar); + Map(x => Math.Min(absolute, Math.Abs(x)), result, Zeros.AllowSkip); + } + + protected override void DoPointwiseAbsoluteMaximum(double scalar, Matrix result) + { + double absolute = Math.Abs(scalar); + Map(x => Math.Max(absolute, Math.Abs(x)), result, Zeros.Include); + } + + protected override void DoPointwiseMinimum(Matrix other, Matrix result) + { + Map2(Math.Min, other, result, Zeros.AllowSkip); + } + + protected override void DoPointwiseMaximum(Matrix other, Matrix result) + { + Map2(Math.Max, other, result, Zeros.AllowSkip); + } + + protected override void DoPointwiseAbsoluteMinimum(Matrix other, Matrix result) + { + Map2((x, y) => Math.Min(Math.Abs(x), Math.Abs(y)), other, result, Zeros.AllowSkip); + } + + protected override void DoPointwiseAbsoluteMaximum(Matrix other, Matrix result) + { + Map2((x, y) => Math.Max(Math.Abs(x), Math.Abs(y)), other, result, Zeros.AllowSkip); + } + /// Calculates the induced L1 norm of this matrix. /// The maximum absolute column sum of the matrix. public override double L1Norm() diff --git a/src/Numerics/LinearAlgebra/Double/Vector.cs b/src/Numerics/LinearAlgebra/Double/Vector.cs index 7f80555e..787bd57b 100644 --- a/src/Numerics/LinearAlgebra/Double/Vector.cs +++ b/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 result) + { + Map(x => Math.Min(scalar, x), result, scalar >= 0d ? Zeros.AllowSkip : Zeros.Include); + } + + protected override void DoPointwiseMaximum(double scalar, Vector result) + { + Map(x => Math.Max(scalar, x), result, scalar <= 0d ? Zeros.AllowSkip : Zeros.Include); + } + + protected override void DoPointwiseAbsoluteMinimum(double scalar, Vector result) + { + double absolute = Math.Abs(scalar); + Map(x => Math.Min(absolute, Math.Abs(x)), result, Zeros.AllowSkip); + } + + protected override void DoPointwiseAbsoluteMaximum(double scalar, Vector result) + { + double absolute = Math.Abs(scalar); + Map(x => Math.Max(absolute, Math.Abs(x)), result, Zeros.Include); + } + + protected override void DoPointwiseMinimum(Vector other, Vector result) + { + Map2(Math.Min, other, result, Zeros.AllowSkip); + } + + protected override void DoPointwiseMaximum(Vector other, Vector result) + { + Map2(Math.Max, other, result, Zeros.AllowSkip); + } + + protected override void DoPointwiseAbsoluteMinimum(Vector other, Vector result) + { + Map2((x, y) => Math.Min(Math.Abs(x), Math.Abs(y)), other, result, Zeros.AllowSkip); + } + + protected override void DoPointwiseAbsoluteMaximum(Vector other, Vector result) + { + Map2((x, y) => Math.Max(Math.Abs(x), Math.Abs(y)), other, result, Zeros.AllowSkip); + } + /// /// Returns the value of the absolute minimum element. /// diff --git a/src/Numerics/LinearAlgebra/Matrix.Arithmetic.cs b/src/Numerics/LinearAlgebra/Matrix.Arithmetic.cs index e6b66f0d..91b26527 100644 --- a/src/Numerics/LinearAlgebra/Matrix.Arithmetic.cs +++ b/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 /// The matrix to store the result. protected abstract void DoPointwiseLog(Matrix result); + protected abstract void DoPointwiseMinimum(T scalar, Matrix result); + protected abstract void DoPointwiseMinimum(Matrix other, Matrix result); + protected abstract void DoPointwiseMaximum(T scalar, Matrix result); + protected abstract void DoPointwiseMaximum(Matrix other, Matrix result); + protected abstract void DoPointwiseAbsoluteMinimum(T scalar, Matrix result); + protected abstract void DoPointwiseAbsoluteMinimum(Matrix other, Matrix result); + protected abstract void DoPointwiseAbsoluteMaximum(T scalar, Matrix result); + protected abstract void DoPointwiseAbsoluteMaximum(Matrix other, Matrix result); + /// /// Adds a scalar to each element of the matrix. /// @@ -1668,6 +1677,222 @@ namespace MathNet.Numerics.LinearAlgebra } } + /// + /// Pointwise applies the minimum with a scalar to each value. + /// + /// The scalar value to compare to. + public Matrix PointwiseMinimum(T scalar) + { + var result = Build.SameAs(this); + DoPointwiseMinimum(scalar, result); + return result; + } + + /// + /// Pointwise applies the minimum with a scalar to each value. + /// + /// The scalar value to compare to. + /// The vector to store the result. + /// If this vector and are not the same size. + public void PointwiseMinimum(T scalar, Matrix result) + { + if (ColumnCount != result.ColumnCount || RowCount != result.RowCount) + { + throw DimensionsDontMatch(this, result); + } + + DoPointwiseMinimum(scalar, result); + } + + /// + /// Pointwise applies the maximum with a scalar to each value. + /// + /// The scalar value to compare to. + public Matrix PointwiseMaximum(T scalar) + { + var result = Build.SameAs(this); + DoPointwiseMaximum(scalar, result); + return result; + } + + /// + /// Pointwise applies the maximum with a scalar to each value. + /// + /// The scalar value to compare to. + /// The matrix to store the result. + /// If this matrix and are not the same size. + public void PointwiseMaximum(T scalar, Matrix result) + { + if (ColumnCount != result.ColumnCount || RowCount != result.RowCount) + { + throw DimensionsDontMatch(this, result); + } + + DoPointwiseMaximum(scalar, result); + } + + /// + /// Pointwise applies the absolute minimum with a scalar to each value. + /// + /// The scalar value to compare to. + public Matrix PointwiseAbsoluteMinimum(T scalar) + { + var result = Build.SameAs(this); + DoPointwiseAbsoluteMinimum(scalar, result); + return result; + } + + /// + /// Pointwise applies the absolute minimum with a scalar to each value. + /// + /// The scalar value to compare to. + /// The matrix to store the result. + /// If this matrix and are not the same size. + public void PointwiseAbsoluteMinimum(T scalar, Matrix result) + { + if (ColumnCount != result.ColumnCount || RowCount != result.RowCount) + { + throw DimensionsDontMatch(this, result); + } + + DoPointwiseAbsoluteMinimum(scalar, result); + } + + /// + /// Pointwise applies the absolute maximum with a scalar to each value. + /// + /// The scalar value to compare to. + public Matrix PointwiseAbsoluteMaximum(T scalar) + { + var result = Build.SameAs(this); + DoPointwiseAbsoluteMaximum(scalar, result); + return result; + } + + /// + /// Pointwise applies the absolute maximum with a scalar to each value. + /// + /// The scalar value to compare to. + /// The matrix to store the result. + /// If this matrix and are not the same size. + public void PointwiseAbsoluteMaximum(T scalar, Matrix result) + { + if (ColumnCount != result.ColumnCount || RowCount != result.RowCount) + { + throw DimensionsDontMatch(this, result); + } + + DoPointwiseAbsoluteMaximum(scalar, result); + } + + /// + /// Pointwise applies the minimum with the values of another matrix to each value. + /// + /// The matrix with the values to compare to. + public Matrix PointwiseMinimum(Matrix other) + { + var result = Build.SameAs(this); + DoPointwiseMinimum(other, result); + return result; + } + + /// + /// Pointwise applies the minimum with the values of another matrix to each value. + /// + /// The matrix with the values to compare to. + /// The matrix to store the result. + /// If this matrix and are not the same size. + public void PointwiseMinimum(Matrix other, Matrix result) + { + if (ColumnCount != result.ColumnCount || RowCount != result.RowCount || ColumnCount != other.ColumnCount || RowCount != other.RowCount) + { + throw DimensionsDontMatch(this, other, result); + } + + DoPointwiseMinimum(other, result); + } + + /// + /// Pointwise applies the maximum with the values of another matrix to each value. + /// + /// The matrix with the values to compare to. + public Matrix PointwiseMaximum(Matrix other) + { + var result = Build.SameAs(this); + DoPointwiseMaximum(other, result); + return result; + } + + /// + /// Pointwise applies the maximum with the values of another matrix to each value. + /// + /// The matrix with the values to compare to. + /// The matrix to store the result. + /// If this matrix and are not the same size. + public void PointwiseMaximum(Matrix other, Matrix result) + { + if (ColumnCount != result.ColumnCount || RowCount != result.RowCount || ColumnCount != other.ColumnCount || RowCount != other.RowCount) + { + throw DimensionsDontMatch(this, other, result); + } + + DoPointwiseMaximum(other, result); + } + + /// + /// Pointwise applies the absolute minimum with the values of another matrix to each value. + /// + /// The matrix with the values to compare to. + public Matrix PointwiseAbsoluteMinimum(Matrix other) + { + var result = Build.SameAs(this); + DoPointwiseAbsoluteMinimum(other, result); + return result; + } + + /// + /// Pointwise applies the absolute minimum with the values of another matrix to each value. + /// + /// The matrix with the values to compare to. + /// The matrix to store the result. + /// If this matrix and are not the same size. + public void PointwiseAbsoluteMinimum(Matrix other, Matrix result) + { + if (ColumnCount != result.ColumnCount || RowCount != result.RowCount || ColumnCount != other.ColumnCount || RowCount != other.RowCount) + { + throw DimensionsDontMatch(this, other, result); + } + + DoPointwiseAbsoluteMinimum(other, result); + } + + /// + /// Pointwise applies the absolute maximum with the values of another matrix to each value. + /// + /// The matrix with the values to compare to. + public Matrix PointwiseAbsoluteMaximum(Matrix other) + { + var result = Build.SameAs(this); + DoPointwiseAbsoluteMaximum(other, result); + return result; + } + + /// + /// Pointwise applies the absolute maximum with the values of another matrix to each value. + /// + /// The matrix with the values to compare to. + /// The matrix to store the result. + /// If this matrix and are not the same size. + public void PointwiseAbsoluteMaximum(Matrix other, Matrix result) + { + if (ColumnCount != result.ColumnCount || RowCount != result.RowCount || ColumnCount != other.ColumnCount || RowCount != other.RowCount) + { + throw DimensionsDontMatch(this, other, result); + } + + DoPointwiseAbsoluteMaximum(other, result); + } + /// Calculates the induced L1 norm of this matrix. /// The maximum absolute column sum of the matrix. public abstract double L1Norm(); diff --git a/src/Numerics/LinearAlgebra/Single/Matrix.cs b/src/Numerics/LinearAlgebra/Single/Matrix.cs index 354dcb50..29179f33 100644 --- a/src/Numerics/LinearAlgebra/Single/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Single/Matrix.cs @@ -464,6 +464,48 @@ namespace MathNet.Numerics.LinearAlgebra.Single return sum; } + protected override void DoPointwiseMinimum(float scalar, Matrix result) + { + Map(x => Math.Min(scalar, x), result, scalar >= 0d ? Zeros.AllowSkip : Zeros.Include); + } + + protected override void DoPointwiseMaximum(float scalar, Matrix result) + { + Map(x => Math.Max(scalar, x), result, scalar <= 0d ? Zeros.AllowSkip : Zeros.Include); + } + + protected override void DoPointwiseAbsoluteMinimum(float scalar, Matrix result) + { + float absolute = Math.Abs(scalar); + Map(x => Math.Min(absolute, Math.Abs(x)), result, Zeros.AllowSkip); + } + + protected override void DoPointwiseAbsoluteMaximum(float scalar, Matrix result) + { + float absolute = Math.Abs(scalar); + Map(x => Math.Max(absolute, Math.Abs(x)), result, Zeros.Include); + } + + protected override void DoPointwiseMinimum(Matrix other, Matrix result) + { + Map2(Math.Min, other, result, Zeros.AllowSkip); + } + + protected override void DoPointwiseMaximum(Matrix other, Matrix result) + { + Map2(Math.Max, other, result, Zeros.AllowSkip); + } + + protected override void DoPointwiseAbsoluteMinimum(Matrix other, Matrix result) + { + Map2((x, y) => Math.Min(Math.Abs(x), Math.Abs(y)), other, result, Zeros.AllowSkip); + } + + protected override void DoPointwiseAbsoluteMaximum(Matrix other, Matrix result) + { + Map2((x, y) => Math.Max(Math.Abs(x), Math.Abs(y)), other, result, Zeros.AllowSkip); + } + /// Calculates the induced L1 norm of this matrix. /// The maximum absolute column sum of the matrix. public override double L1Norm() diff --git a/src/Numerics/LinearAlgebra/Single/Vector.cs b/src/Numerics/LinearAlgebra/Single/Vector.cs index 2eb4f7a1..4a860dff 100644 --- a/src/Numerics/LinearAlgebra/Single/Vector.cs +++ b/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 result) + { + Map(x => Math.Min(scalar, x), result, scalar >= 0d ? Zeros.AllowSkip : Zeros.Include); + } + + protected override void DoPointwiseMaximum(float scalar, Vector result) + { + Map(x => Math.Max(scalar, x), result, scalar <= 0d ? Zeros.AllowSkip : Zeros.Include); + } + + protected override void DoPointwiseAbsoluteMinimum(float scalar, Vector result) + { + float absolute = Math.Abs(scalar); + Map(x => Math.Min(absolute, Math.Abs(x)), result, Zeros.AllowSkip); + } + + protected override void DoPointwiseAbsoluteMaximum(float scalar, Vector result) + { + float absolute = Math.Abs(scalar); + Map(x => Math.Max(absolute, Math.Abs(x)), result, Zeros.Include); + } + + protected override void DoPointwiseMinimum(Vector other, Vector result) + { + Map2(Math.Min, other, result, Zeros.AllowSkip); + } + + protected override void DoPointwiseMaximum(Vector other, Vector result) + { + Map2(Math.Max, other, result, Zeros.AllowSkip); + } + + protected override void DoPointwiseAbsoluteMinimum(Vector other, Vector result) + { + Map2((x, y) => Math.Min(Math.Abs(x), Math.Abs(y)), other, result, Zeros.AllowSkip); + } + + protected override void DoPointwiseAbsoluteMaximum(Vector other, Vector result) + { + Map2((x, y) => Math.Max(Math.Abs(x), Math.Abs(y)), other, result, Zeros.AllowSkip); + } + /// /// Returns the value of the absolute minimum element. /// diff --git a/src/Numerics/LinearAlgebra/Vector.Arithmetic.cs b/src/Numerics/LinearAlgebra/Vector.Arithmetic.cs index eee2af89..4aea4c2b 100644 --- a/src/Numerics/LinearAlgebra/Vector.Arithmetic.cs +++ b/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 /// The vector to store the result. protected abstract void DoPointwiseLog(Vector result); + protected abstract void DoPointwiseMinimum(T scalar, Vector result); + protected abstract void DoPointwiseMinimum(Vector other, Vector result); + protected abstract void DoPointwiseMaximum(T scalar, Vector result); + protected abstract void DoPointwiseMaximum(Vector other, Vector result); + protected abstract void DoPointwiseAbsoluteMinimum(T scalar, Vector result); + protected abstract void DoPointwiseAbsoluteMinimum(Vector other, Vector result); + protected abstract void DoPointwiseAbsoluteMaximum(T scalar, Vector result); + protected abstract void DoPointwiseAbsoluteMaximum(Vector other, Vector result); + /// /// Adds a scalar to each element of the vector. /// @@ -1028,6 +1037,222 @@ namespace MathNet.Numerics.LinearAlgebra return u.OuterProduct(v); } + /// + /// Pointwise applies the minimum with a scalar to each value. + /// + /// The scalar value to compare to. + public Vector PointwiseMinimum(T scalar) + { + var result = Build.SameAs(this); + DoPointwiseMinimum(scalar, result); + return result; + } + + /// + /// Pointwise applies the minimum with a scalar to each value. + /// + /// The scalar value to compare to. + /// The vector to store the result. + /// If this vector and are not the same size. + public void PointwiseMinimum(T scalar, Vector result) + { + if (Count != result.Count) + { + throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); + } + + DoPointwiseMinimum(scalar, result); + } + + /// + /// Pointwise applies the maximum with a scalar to each value. + /// + /// The scalar value to compare to. + public Vector PointwiseMaximum(T scalar) + { + var result = Build.SameAs(this); + DoPointwiseMaximum(scalar, result); + return result; + } + + /// + /// Pointwise applies the maximum with a scalar to each value. + /// + /// The scalar value to compare to. + /// The vector to store the result. + /// If this vector and are not the same size. + public void PointwiseMaximum(T scalar, Vector result) + { + if (Count != result.Count) + { + throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); + } + + DoPointwiseMaximum(scalar, result); + } + + /// + /// Pointwise applies the absolute minimum with a scalar to each value. + /// + /// The scalar value to compare to. + public Vector PointwiseAbsoluteMinimum(T scalar) + { + var result = Build.SameAs(this); + DoPointwiseAbsoluteMinimum(scalar, result); + return result; + } + + /// + /// Pointwise applies the absolute minimum with a scalar to each value. + /// + /// The scalar value to compare to. + /// The vector to store the result. + /// If this vector and are not the same size. + public void PointwiseAbsoluteMinimum(T scalar, Vector result) + { + if (Count != result.Count) + { + throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); + } + + DoPointwiseAbsoluteMinimum(scalar, result); + } + + /// + /// Pointwise applies the absolute maximum with a scalar to each value. + /// + /// The scalar value to compare to. + public Vector PointwiseAbsoluteMaximum(T scalar) + { + var result = Build.SameAs(this); + DoPointwiseAbsoluteMaximum(scalar, result); + return result; + } + + /// + /// Pointwise applies the absolute maximum with a scalar to each value. + /// + /// The scalar value to compare to. + /// The vector to store the result. + /// If this vector and are not the same size. + public void PointwiseAbsoluteMaximum(T scalar, Vector result) + { + if (Count != result.Count) + { + throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); + } + + DoPointwiseAbsoluteMaximum(scalar, result); + } + + /// + /// Pointwise applies the minimum with the values of another vector to each value. + /// + /// The vector with the values to compare to. + public Vector PointwiseMinimum(Vector other) + { + var result = Build.SameAs(this); + DoPointwiseMinimum(other, result); + return result; + } + + /// + /// Pointwise applies the minimum with the values of another vector to each value. + /// + /// The vector with the values to compare to. + /// The vector to store the result. + /// If this vector and are not the same size. + public void PointwiseMinimum(Vector other, Vector result) + { + if (Count != result.Count) + { + throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); + } + + DoPointwiseMinimum(other, result); + } + + /// + /// Pointwise applies the maximum with the values of another vector to each value. + /// + /// The vector with the values to compare to. + public Vector PointwiseMaximum(Vector other) + { + var result = Build.SameAs(this); + DoPointwiseMaximum(other, result); + return result; + } + + /// + /// Pointwise applies the maximum with the values of another vector to each value. + /// + /// The vector with the values to compare to. + /// The vector to store the result. + /// If this vector and are not the same size. + public void PointwiseMaximum(Vector other, Vector result) + { + if (Count != result.Count) + { + throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); + } + + DoPointwiseMaximum(other, result); + } + + /// + /// Pointwise applies the absolute minimum with the values of another vector to each value. + /// + /// The vector with the values to compare to. + public Vector PointwiseAbsoluteMinimum(Vector other) + { + var result = Build.SameAs(this); + DoPointwiseAbsoluteMinimum(other, result); + return result; + } + + /// + /// Pointwise applies the absolute minimum with the values of another vector to each value. + /// + /// The vector with the values to compare to. + /// The vector to store the result. + /// If this vector and are not the same size. + public void PointwiseAbsoluteMinimum(Vector other, Vector result) + { + if (Count != result.Count) + { + throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); + } + + DoPointwiseAbsoluteMinimum(other, result); + } + + /// + /// Pointwise applies the absolute maximum with the values of another vector to each value. + /// + /// The vector with the values to compare to. + public Vector PointwiseAbsoluteMaximum(Vector other) + { + var result = Build.SameAs(this); + DoPointwiseAbsoluteMaximum(other, result); + return result; + } + + /// + /// Pointwise applies the absolute maximum with the values of another vector to each value. + /// + /// The vector with the values to compare to. + /// The vector to store the result. + /// If this vector and are not the same size. + public void PointwiseAbsoluteMaximum(Vector other, Vector result) + { + if (Count != result.Count) + { + throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); + } + + DoPointwiseAbsoluteMaximum(other, result); + } + /// /// Calculates the L1 norm of the vector, also known as Manhattan norm. /// diff --git a/src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs b/src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs index 5af35d42..e79c813b 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs @@ -339,108 +339,111 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double Assert.That(() => vector.SubVector(index, length), Throws.TypeOf()); } - /// - /// Can find absolute minimum value index. - /// [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()); } - - /// - /// Can find absolute minimum value of a vector. - /// [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()); } - /// - /// Can find absolute maximum value index. - /// [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()); } - /// - /// Can find absolute maximum value of a vector. - /// [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()); } - /// - /// Can find maximum value index. - /// [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()); } - /// - /// Can find maximum value of a vector. - /// [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); } - /// - /// Can find minimum value index. - /// [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); } - /// - /// Can find minimum value of a vector. - /// [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); } /// diff --git a/src/UnitTests/LinearAlgebraTests/Single/VectorTests.cs b/src/UnitTests/LinearAlgebraTests/Single/VectorTests.cs index 34973742..d68ec060 100644 --- a/src/UnitTests/LinearAlgebraTests/Single/VectorTests.cs +++ b/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); + } + /// /// Can compute the sum of a vector elements. ///