Skip to content

Commit

Permalink
add ignoreNonNumerics param to min functions
Browse files Browse the repository at this point in the history
  • Loading branch information
perNyfelt committed Jul 17, 2023
1 parent db37aaf commit c1f9001
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions src/main/groovy/se/alipsa/groovy/matrix/Stat.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -295,32 +295,40 @@ class Stat {
return [v[q1], v[q3]] as T[]
}

static <T extends Comparable> T min(List<T> list) {
static <T extends Comparable> T min(List<T> list, boolean ignoreNonNumerics = false) {
def minVal = null
for (value in list) {
if (value instanceof Comparable ) {
if (minVal == null || value < minVal) {
boolean skip = false
if (ignoreNonNumerics && !(value instanceof Number)) {
skip = true
}
if (!skip && (minVal == null || value < minVal)) {
minVal = value
}
}
}
return minVal
}

static <T extends Comparable> T[] min(List<List<T>> matrix, Integer colNum) {
return min(matrix, [colNum])
static <T extends Comparable> T[] min(List<List<T>> matrix, Integer colNum, boolean ignoreNonNumerics = false) {
return min(matrix, [colNum], ignoreNonNumerics)
}

static <T extends Comparable> T[] min(List<List<T>> matrix, List<Integer> colNums) {
static <T extends Comparable> T[] min(List<List<T>> matrix, List<Integer> colNums, boolean ignoreNonNumerics = false) {
def value
def minVal
def minVals = new ArrayList<T>(colNums.size())
def idx
for (row in matrix) {
idx = 0
for (colNum in colNums) {
boolean skip = false
value = row[colNum]
if (value instanceof Number) {
if (ignoreNonNumerics && !(value instanceof Number)) {
skip = true
}
if (value instanceof Number && !skip) {
minVal = minVals[idx]
if (minVal == null || value < minVal) {
minVals[idx] = value
Expand All @@ -332,14 +340,18 @@ class Stat {
return minVals
}

static <T extends Comparable> T[] min(Matrix table, List<String> colNames) {
return min(table.rows(), table.columnIndexes(colNames))
static <T extends Comparable> T[] min(Matrix table, List<String> colNames, boolean ignoreNonNumerics = false) {
return min(table.rows(), table.columnIndexes(colNames), ignoreNonNumerics)
}

static <T> T max(List<T> list) {
static <T> T max(List<T> list, boolean ignoreNonNumerics = false) {
def maxVal = null
for (value in list) {
if (value instanceof Comparable ) {
boolean skip = false
if (ignoreNonNumerics && !(value instanceof Number)) {
skip = true
}
if (value instanceof Comparable && !skip) {
if (maxVal == null || value > maxVal) {
maxVal = value
}
Expand All @@ -348,8 +360,8 @@ class Stat {
return maxVal
}

static <T extends Comparable> T[] max(List<List<T>> matrix, Integer colNum) {
return max(matrix, [colNum])
static <T extends Comparable> T[] max(List<List<T>> matrix, Integer colNum, boolean ignoreNonNumerics = false) {
return max(matrix, [colNum], ignoreNonNumerics)
}

static <T extends Comparable> T[] max(List<List<T>> matrix, List<Integer> colNums, boolean ignoreNonNumerics = false) {
Expand Down

0 comments on commit c1f9001

Please sign in to comment.