From 5f2459ded2b51dc6fb1218531e99f62b528a6bde Mon Sep 17 00:00:00 2001 From: Luc Maisonobe <luc@apache.org> Date: Sun, 15 Nov 2015 14:54:49 +0100 Subject: [PATCH] Added min/max utility methods for RealField elements. --- .../apache/commons/math3/util/MathUtils.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/main/java/org/apache/commons/math3/util/MathUtils.java b/src/main/java/org/apache/commons/math3/util/MathUtils.java index 0b5fc5690..1ca92841c 100644 --- a/src/main/java/org/apache/commons/math3/util/MathUtils.java +++ b/src/main/java/org/apache/commons/math3/util/MathUtils.java @@ -19,6 +19,7 @@ package org.apache.commons.math3.util; import java.util.Arrays; +import org.apache.commons.math3.RealFieldElement; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.NotFiniteNumberException; import org.apache.commons.math3.exception.NullArgumentException; @@ -109,6 +110,28 @@ public final class MathUtils { return a - TWO_PI * FastMath.floor((a + FastMath.PI - center) / TWO_PI); } + /** Find the maximum of two field elements. + * @param <T> the type of the field elements + * @param e1 first element + * @param e2 second element + * @return max(a1, e2) + * @since 3.6 + */ + public static <T extends RealFieldElement<T>> T max(final T e1, final T e2) { + return e1.subtract(e2).getReal() >= 0 ? e1 : e2; + } + + /** Find the minimum of two field elements. + * @param <T> the type of the field elements + * @param e1 first element + * @param e2 second element + * @return min(a1, e2) + * @since 3.6 + */ + public static <T extends RealFieldElement<T>> T min(final T e1, final T e2) { + return e1.subtract(e2).getReal() >= 0 ? e2 : e1; + } + /** * <p>Reduce {@code |a - offset|} to the primary interval * {@code [0, |period|)}.</p>