Class Math2

java.lang.Object
org.djunits.util.Math2

public final class Math2 extends Object
Math2 contains a few utility methods that are missing in the Math package.

Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See for project information https://djunits.org. The DJUNITS project is distributed under a three-clause BSD-style license.

Author:
Alexander Verbraeck
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    max(double... dn)
    Return the maximum value within an array of values.
    static double
    maxAbs(double... dn)
    Return the maximum absolute value within an array of values.
    static double
    median(double... d)
    Computes the median of the provided values, skipping NaN entries.
    static double
    min(double... dn)
    Return the minimum value within an array of values.
    static double
    minAbs(double... dn)
    Return the mimimum absolute value within an array of values.
    static int
    pow(int base, int exp)
    Computes base^exp for integers.
    static double
    sum(double... dn)
    Return the sum of the values of an array of values.
    static double
    sumAbs(double... dn)
    Return the sum of the absolute values of an array of values.
    static double
    sumSqr(double... dn)
    Return the sum of the squares of the values of an array of values.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • max

      public static double max(double... dn)
      Return the maximum value within an array of values.
      Parameters:
      dn - zero or more values
      Returns:
      the maximum value of the arguments
    • min

      public static double min(double... dn)
      Return the minimum value within an array of values.
      Parameters:
      dn - zero or more values
      Returns:
      the minimum value of the arguments
    • maxAbs

      public static double maxAbs(double... dn)
      Return the maximum absolute value within an array of values.
      Parameters:
      dn - zero or more values
      Returns:
      the maximum absolute value of the arguments
    • minAbs

      public static double minAbs(double... dn)
      Return the mimimum absolute value within an array of values.
      Parameters:
      dn - zero or more values
      Returns:
      the minimum absolute value of the arguments
    • sum

      public static double sum(double... dn)
      Return the sum of the values of an array of values.
      Parameters:
      dn - zero or more values
      Returns:
      the sum of the values of the arguments
    • sumAbs

      public static double sumAbs(double... dn)
      Return the sum of the absolute values of an array of values.
      Parameters:
      dn - zero or more values
      Returns:
      the sum of the absolute values of the arguments
    • sumSqr

      public static double sumSqr(double... dn)
      Return the sum of the squares of the values of an array of values.
      Parameters:
      dn - zero or more values
      Returns:
      the sum of the squares of the values of the arguments
    • median

      public static double median(double... d)
      Computes the median of the provided values, skipping NaN entries.

      Rules:

      • NaN values are ignored.
      • If there are 0 valid (non-NaN) values, Double.NaN is returned.
      • For an odd count, returns the middle value after sorting.
      • For an even count, returns the arithmetic mean of the two middle values.
      Parameters:
      d - values to consider; NaNs are skipped
      Returns:
      the median as defined above, or Double.NaN if no valid values
    • pow

      public static int pow(int base, int exp)
      Computes base^exp for integers.
      Parameters:
      base - the integer base
      exp - the non-negative exponent
      Returns:
      base raised to the power exp