Download or view argmin.frink in plain text format
/** This contains functions to find the "argmin" of a function, that is,
given a single-argument function and a list of input values, returns an
array of the input value(s) where the function has the minimum value.
*/
argmin[f, data] :=
{
min = undef
results = new array
for arg = data
{
val = f[arg]
if min == undef
{
min = val
results.push[arg]
} else
{
if val < min // New minimum, replace results list
{
results = [arg]
min = val
} else
if val == min // Identical min, push arg to arg list
results.push[arg]
}
}
return results
}
/** This contains functions to find the "argmax" of a function, that is,
given a single-argument function and a list of input values, returns an
array of the input value(s) where the function has the maxmium value.
*/
argmax[f, data] :=
{
max = undef
results = new array
for arg = data
{
val = f[arg]
if max == undef
{
max = val
results.push[arg]
} else
{
if val > max // New maximum, replace results list
{
results = [arg]
max = val
} else
if val == max // Identical max, push arg to arg list
results.push[arg]
}
}
return results
}
/*
// Example usages
squareFunc = {|x| x^2}
data = [-1, 1, -2]
println["argmin: " + argmin[squareFunc, data]]
println["argmax: " + argmax[squareFunc, data]]
println[]
data = [[2,-3],[3,5],[2,5],[-2,3],[-2,-3]]
product = getFunction["product", 1]
println["argmin: " + argmin[product, data]]
println["argmax: " + argmax[product, data]]
*/
Download or view argmin.frink in plain text format
This is a program written in the programming language Frink.
For more information, view the Frink
Documentation or see More Sample Frink Programs.
Alan Eliasen was born 20218 days, 0 hours, 9 minutes ago.