Class MathMethods

java.lang.Object
org.klojang.util.MathMethods

public final class MathMethods extends Object
Math-related methods. Quite a few methods in this class are about laying out a one-dimensional array of values across one or more stack with a certain number of rows and columns. However, for "grid" one might just as well read "matrix", or "table", or "grid", or "HTML table", or "HTML grid" - whatever makes sense given the context in which you use the methods.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T extends Number>
    T
    abs(T number)
    Returns the absolute value of an arbitrary type of Number.
    static int
    countEmptyRowsOnLastGrid(int rowCount, int rows)
    Returns the number of trailing, empty rows in the last grid.
    static int
    countRowsOnLastGrid(int rowCount, int rows)
    Returns the number of populated rows in the last grid (with the very last row possibly only partly populated).
    static int
    divDown(int value, int divideBy)
    Equivalent to value / dividedBy.
    static int
    divHalfDown(int value, int divideBy)
    Divides the specified value by the specified denominator, rounding down if the remainder is exactly 0.5 (given double-precision calculation).
    static int
    divHalfUp(int value, int divideBy)
    Divides the specified value by the specified denominator, rounding up if the remainder is exactly 0.5 (given double-precision calculation).
    static int
    divUp(int value, int divideBy)
    Converts to arguments to double, then divides the first argument by the second, and then applies Math.ceil.
    static int
    getColumn(int itemIndex, int cols)
    Returns the column index of an item with the specified array index, given the specified number of columns per grid.
    static int
    getColumnCM(int itemIndex, int rows, int cols)
    Returns the column index of an item in a column-major layout.
    static int
    getGrid(int itemIndex, int rows, int cols)
    Returns the array index of the grid within an array of stack that will contain the item with the specified index in a one-dimensional array.
    static int
    getGridCount(int rowCount, int rows)
    Returns the number of grids needed to contain the specified number of rows.
    static int
    getGridCount(int itemCount, int rows, int cols)
    Returns the number of rows x cols stack needed to contain the specified number of items.
    static int[]
    getGridRowColumn(int itemIndex, int rows, int cols)
    Returns grid, row, and column index of an item with the specified array index.
    static int[]
    getGridRowColumnCM(int itemIndex, int rows, int cols)
    Returns the grid, row, and column index of an item in a column-major layout.
    static int
    getRow(int itemIndex, int rows, int cols)
    Returns the row index of an item, relative to the grid that it finds itself on.
    static int
    getRowCM(int itemIndex, int rows)
    Returns the row index of an item in a column-major layout.
    static int
    indexOfLastGrid(int rowCount, int rows)
    Returns the array index of the last grid within an array of stack containing the specified number rows.
    static int
    nearest(int reference, int value1, int value2)
    Returns the value that is closest to the reference value.
    static int[][][]
    toGrid(int[] values, int rows, int cols)
    Converts a one-dimensional array of values into zero or more grids of rows x cols cells.
    static int[][][]
    toGrid(int[] values, int rows, int cols, int padValue)
    Converts a one-dimensional array of values into zero or more grids of rows x cols cells.
    static <T> T[][][]
    toGrid(T[] values, int rows, int cols)
    Converts a one-dimensional array of values into zero or more grids of rows x cols cells.
    static <T> T[][][]
    toGrid(T[] values, int rows, int cols, T padValue)
    Converts a one-dimensional array of values into zero or more grids of rows x cols cells.
    static int[][][]
    toGridCM(int[] values, int rows, int cols)
    Converts a one-dimensional array of values into zero or more grids of rows x cols cells.
    static int[][][]
    toGridCM(int[] values, int rows, int cols, int padValue)
    Converts a one-dimensional array of values into zero or more grids of rows x cols cells.
    static <T> T[][][]
    toGridCM(T[] values, int rows, int cols)
    Converts a one-dimensional array of values into zero or more grids of rows x cols cells.
    static <T> T[][][]
    toGridCM(T[] values, int rows, int cols, T padValue)
    Converts a one-dimensional array of values into zero or more grids of rows x cols cells.

    Methods inherited from class java.lang.Object

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

    • abs

      public static <T extends Number> T abs(T number)
      Returns the absolute value of an arbitrary type of Number. Besides the primitive number wrappers, this method supports BigInteger, BigDecimal, AtomicInteger and AtomicLong. An IllegalArgumentException is thrown if the argument is any other type of Number.
      Type Parameters:
      T - the type of the Number
      Parameters:
      number - the number
      Returns:
      the argument if it is a non-negative Number, else a Number representing the absolute value of the argument.
    • divUp

      public static int divUp(int value, int divideBy)
      Converts to arguments to double, then divides the first argument by the second, and then applies Math.ceil. Since this is a very low-level operation, no argument-checking is performed.
      Parameters:
      value - The integer to divide
      divideBy - The integer to divide it by
      Returns:
      the result of the division, rounded to the next integer
    • divDown

      public static int divDown(int value, int divideBy)
      Equivalent to value / dividedBy. Usable a method reference. Since this is a very low-level operation, no argument-checking is performed.
      Parameters:
      value - The integer to divide
      divideBy - The integer to divide it by
      Returns:
      the result of the division, rounded to the preceding integer
    • divHalfUp

      public static int divHalfUp(int value, int divideBy)
      Divides the specified value by the specified denominator, rounding up if the remainder is exactly 0.5 (given double-precision calculation). Since this is a very low-level operation, no argument-checking is performed.
      Parameters:
      value - The integer to divide
      divideBy - The integer to divide it by
      Returns:
      the result of the division, rounded up if the remainder is exactly 0.5
    • divHalfDown

      public static int divHalfDown(int value, int divideBy)
      Divides the specified value by the specified denominator, rounding down if the remainder is exactly 0.5 (given double-precision calculation). Since this is a very low-level operation, no argument-checking is performed.
      Parameters:
      value - The integer to divide
      divideBy - The integer to divide it by
      Returns:
      the result of the division, rounded down if the remainder is exactly 0.5
    • nearest

      public static int nearest(int reference, int value1, int value2)

      Returns the value that is closest to the reference value. More precisely:

      • if value1 is closer to reference, then value1 is returned
      • if value2 is closer to reference, then value2 is returned
      • if they are equally close to reference (but possibly on opposite sides of it!), then value1 is returned
      Parameters:
      reference - the reference value
      value1 - the first value to compare to the reference value
      value2 - then second value to compare to the reference value
      Returns:
      the second value if it is closer to the reference value, else the first value
    • getGridCount

      public static int getGridCount(int itemCount, int rows, int cols)
      Returns the number of rows x cols stack needed to contain the specified number of items.
      Parameters:
      itemCount - The total number of items
      rows - the number of rows per grid (or table, or matrix, or grid)
      cols - the number of columns per grid (or table, or matrix, or grid)
      Returns:
      the number of stack needed to contain the specified number of items
    • getGridCount

      public static int getGridCount(int rowCount, int rows)
      Returns the number of grids needed to contain the specified number of rows.
      Parameters:
      rowCount - the total number of rows
      rows - The number of rows per grid
      Returns:
      the number of stack needed to contain the specified number of rows
    • indexOfLastGrid

      public static int indexOfLastGrid(int rowCount, int rows)
      Returns the array index of the last grid within an array of stack containing the specified number rows. So basically getGridCount minus one.
      Parameters:
      rowCount - the total number of rows
      rows - The number of rows per grid
      Returns:
      the index of the last grid within an array of stack
    • countRowsOnLastGrid

      public static int countRowsOnLastGrid(int rowCount, int rows)
      Returns the number of populated rows in the last grid (with the very last row possibly only partly populated).
      Parameters:
      rowCount - the total number of rows
      rows - the number of rows per grid
      Returns:
      the number of (partly) populated rows on the last grid
    • countEmptyRowsOnLastGrid

      public static int countEmptyRowsOnLastGrid(int rowCount, int rows)
      Returns the number of trailing, empty rows in the last grid.
      Parameters:
      rowCount - the total number of rows
      rows - the number of rows per grid
      Returns:
      the number of trailing, empty rows in the last grid
    • getGrid

      public static int getGrid(int itemIndex, int rows, int cols)
      Returns the array index of the grid within an array of stack that will contain the item with the specified index in a one-dimensional array. In other words, this method maps an index in a one-dimensional array (T[x]) to an index in the first component of a three-dimensional array (T[y][][].
      Parameters:
      itemIndex - the array index of the item
      rows - the number of rows per grid (or table, or matrix, or grid)
      cols - the number of columns per grid (or table, or matrix, or grid)
      Returns:
      the index of the
    • getRow

      public static int getRow(int itemIndex, int rows, int cols)
      Returns the row index of an item, relative to the grid that it finds itself on. In other words, this method maps an index in a one-dimensional array (T[x]) to an index in the second component of a three-dimensional array (T[][y][].
      Parameters:
      itemIndex - the array index of the item
      rows - the number of rows per grid
      cols - the number of columns per grid
      Returns:
      the row index of an item
    • getColumn

      public static int getColumn(int itemIndex, int cols)
      Returns the column index of an item with the specified array index, given the specified number of columns per grid. In other words, this method maps an index in a one-dimensional array (T[x]) to an index in the third component of a three-dimensional array (T[][][y].
      Parameters:
      itemIndex - the array index of the item
      cols - the number of columns per grid
      Returns:
      the column index of an item with the specified array index
    • getRowCM

      public static int getRowCM(int itemIndex, int rows)
      Returns the row index of an item in a column-major layout. That is: the items are laid out in columns (top-to-bottom first, left-to-right second).
      Parameters:
      itemIndex - the array index of the item
      rows - the number of rows per grid
      Returns:
      the row index of an item in a column-major layout
    • getColumnCM

      public static int getColumnCM(int itemIndex, int rows, int cols)
      Returns the column index of an item in a column-major layout.
      Parameters:
      itemIndex - the array index of the item
      rows - the number of rows per grid
      cols - the number of columns per grid
      Returns:
      the column number of an item in a column-major layout
    • getGridRowColumn

      public static int[] getGridRowColumn(int itemIndex, int rows, int cols)
      Returns grid, row, and column index of an item with the specified array index.
      Parameters:
      itemIndex - the array index of the item
      rows - the number of rows per grid
      cols - the number of columns per grid
      Returns:
      the grid, row, and column index of an item with the specified array index
    • getGridRowColumnCM

      public static int[] getGridRowColumnCM(int itemIndex, int rows, int cols)
      Returns the grid, row, and column index of an item in a column-major layout.
      Parameters:
      itemIndex - the array index of the item
      rows - the number of rows per grid
      cols - the number of columns per grid
      Returns:
      the grid, row, and column number of an item in a column-major layout
    • toGrid

      public static int[][][] toGrid(int[] values, int rows, int cols)
      Converts a one-dimensional array of values into zero or more grids of rows x cols cells. Empty cells in the last grid will be padded with zeros.
      Parameters:
      values - The values to rasterize
      rows - the number of rows per grid
      cols - the number of columns per grid
      Returns:
      Zero or more grids containing the values in the array, laid out in rows containing cols cells
    • toGrid

      public static int[][][] toGrid(int[] values, int rows, int cols, int padValue)
      Converts a one-dimensional array of values into zero or more grids of rows x cols cells.
      Parameters:
      values - The values to rasterize
      rows - the number of rows per grid
      cols - the number of columns per grid
      padValue - The value to pad the empty cells in the last grid with
      Returns:
      Zero or more grids containing the values in the array, laid out in rows containing cols cells
    • toGrid

      public static <T> T[][][] toGrid(T[] values, int rows, int cols)
      Converts a one-dimensional array of values into zero or more grids of rows x cols cells. Empty cells in the last grid will be padded with null values.
      Type Parameters:
      T - The type of the values to be rasterized
      Parameters:
      values - The values to rasterize
      rows - the number of rows per grid
      cols - the number of columns per grid
      Returns:
      Zero or more grids containing the values in the array, laid out in rows containing cols cells
    • toGrid

      public static <T> T[][][] toGrid(T[] values, int rows, int cols, T padValue)
      Converts a one-dimensional array of values into zero or more grids of rows x cols cells.
      Type Parameters:
      T - The type of the values to be rasterized
      Parameters:
      values - The values to rasterize
      rows - the number of rows per grid
      cols - the number of columns per grid
      padValue - The value to pad the empty cells in the last grid with
      Returns:
      Zero or more grids containing the values in the array, laid out in rows containing cols cells
    • toGridCM

      public static int[][][] toGridCM(int[] values, int rows, int cols)
      Converts a one-dimensional array of values into zero or more grids of rows x cols cells. Empty cells in the last grid will be padded with zeros. The values are laid out in column-major fashion.
      Parameters:
      values - The values to rasterize
      rows - the number of rows per grid
      cols - the number of columns per grid
      Returns:
      Zero or more grids containing the values in the array, laid out in columns containing cols cells
    • toGridCM

      public static int[][][] toGridCM(int[] values, int rows, int cols, int padValue)
      Converts a one-dimensional array of values into zero or more grids of rows x cols cells. The values are laid out in column-major fashion.
      Parameters:
      values - The values to rasterize
      rows - the number of rows per grid
      cols - the number of columns per grid
      padValue - The value to pad the empty cells in the last grid with
      Returns:
      Zero or more grids containing the values in the array, laid out in columns containing cols cells
    • toGridCM

      public static <T> T[][][] toGridCM(T[] values, int rows, int cols)
      Converts a one-dimensional array of values into zero or more grids of rows x cols cells. Empty cells in the last grid will be padded with null values. The values are laid out in column-major fashion.
      Type Parameters:
      T - The type of the values to be rasterized
      Parameters:
      values - The values to rasterize
      rows - the number of rows per raster (or table, or matrix)
      cols - the number of columns per raster (or table, or matrix)
      Returns:
      Zero or more grids containing the values in the array, laid out in columns containing cols cells
    • toGridCM

      public static <T> T[][][] toGridCM(T[] values, int rows, int cols, T padValue)
      Converts a one-dimensional array of values into zero or more grids of rows x cols cells. The values are laid out in column-major fashion.
      Type Parameters:
      T - The type of the values to be rasterized
      Parameters:
      values - The values to rasterize
      rows - the number of rows per grid
      cols - the number of columns per grid
      padValue - The value to pad the empty cells in the last grid with
      Returns:
      Zero or more grids containing the values in the array, laid out in columns containing cols cells