java.lang.Object
org.klojang.util.MathMethods
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 TypeMethodDescriptionstatic <T extends Number>
Tabs
(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 tovalue / dividedBy
.static int
divHalfDown
(int value, int divideBy) Divides the specified value by the specified denominator, rounding down if the remainder is exactly0.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 exactly0.5
(given double-precision calculation).static int
divUp
(int value, int divideBy) Converts to arguments todouble
, then divides the first argument by the second, and then appliesMath.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 ofrows 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 ofrows 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 ofrows 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 ofrows 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 ofrows x cols
cells.static int[][][]
toGridCM
(int[] values, int rows, int cols) Converts a one-dimensional array of values into zero or more grids ofrows 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 ofrows 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 ofrows 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 ofrows x cols
cells.
-
Method Details
-
abs
Returns the absolute value of an arbitrary type of Number. Besides the primitive number wrappers, this method supportsBigInteger
,BigDecimal
,AtomicInteger
andAtomicLong
. AnIllegalArgumentException
is thrown if the argument is any other type ofNumber
.- Type Parameters:
T
- the type of theNumber
- Parameters:
number
- the number- Returns:
- the argument if it is a non-negative
Number
, else aNumber
representing the absolute value of the argument.
-
divUp
public static int divUp(int value, int divideBy) Converts to arguments todouble
, then divides the first argument by the second, and then appliesMath.ceil
. Since this is a very low-level operation, no argument-checking is performed.- Parameters:
value
- The integer to dividedivideBy
- 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 tovalue / dividedBy
. Usable a method reference. Since this is a very low-level operation, no argument-checking is performed.- Parameters:
value
- The integer to dividedivideBy
- 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 exactly0.5
(given double-precision calculation). Since this is a very low-level operation, no argument-checking is performed.- Parameters:
value
- The integer to dividedivideBy
- 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 exactly0.5
(given double-precision calculation). Since this is a very low-level operation, no argument-checking is performed.- Parameters:
value
- The integer to dividedivideBy
- 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 toreference
, thenvalue1
is returned - if
value2
is closer toreference
, thenvalue2
is returned - if they are equally close to
reference
(but possibly on opposite sides of it!), thenvalue1
is returned
- Parameters:
reference
- the reference valuevalue1
- the first value to compare to the reference valuevalue2
- 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
- if
-
getGridCount
public static int getGridCount(int itemCount, int rows, int cols) Returns the number ofrows x cols
stack needed to contain the specified number of items.- Parameters:
itemCount
- The total number of itemsrows
- 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 rowsrows
- 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 basicallygetGridCount
minus one.- Parameters:
rowCount
- the total number of rowsrows
- 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 rowsrows
- 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 rowsrows
- 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 itemrows
- 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 itemrows
- the number of rows per gridcols
- 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 itemcols
- 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 itemrows
- 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 itemrows
- the number of rows per gridcols
- 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 itemrows
- the number of rows per gridcols
- 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 itemrows
- the number of rows per gridcols
- 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 ofrows x cols
cells. Empty cells in the last grid will be padded with zeros.- Parameters:
values
- The values to rasterizerows
- the number of rows per gridcols
- 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 ofrows x cols
cells.- Parameters:
values
- The values to rasterizerows
- the number of rows per gridcols
- the number of columns per gridpadValue
- 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 ofrows x cols
cells. Empty cells in the last grid will be padded withnull
values.- Type Parameters:
T
- The type of the values to be rasterized- Parameters:
values
- The values to rasterizerows
- the number of rows per gridcols
- 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 ofrows x cols
cells.- Type Parameters:
T
- The type of the values to be rasterized- Parameters:
values
- The values to rasterizerows
- the number of rows per gridcols
- the number of columns per gridpadValue
- 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 ofrows 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 rasterizerows
- the number of rows per gridcols
- 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 ofrows x cols
cells. The values are laid out in column-major fashion.- Parameters:
values
- The values to rasterizerows
- the number of rows per gridcols
- the number of columns per gridpadValue
- 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 ofrows x cols
cells. Empty cells in the last grid will be padded withnull
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 rasterizerows
- 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 ofrows 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 rasterizerows
- the number of rows per gridcols
- the number of columns per gridpadValue
- 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
-