public class FloatFunctions extends Object
Math
as function objects, as well as a few more basic
functions.
Function objects conveniently allow to express arbitrary functions in a generic manner. Essentially, a function object is an object that can perform a function on some arguments. It has a minimal interface: a method apply that takes the arguments, computes something and returns some result value. Function objects are comparable to function pointers in C used for call-backs.
Unary functions are of type FloatFunction
,
binary functions of type FloatFloatFunction
. All can be retrieved via public
static final variables named after the function. Unary predicates are of
type FloatProcedure
, binary predicates of
type FloatFloatProcedure
. All can be
retrieved via public
static final variables named isXXX.
Binary functions and predicates also exist as unary functions with the second argument being fixed to a constant. These are generated and retrieved via factory methods (again with the same name as the function). Example:
bindArg1(FloatFloatFunction,float)
and
bindArg2(FloatFloatFunction,float)
. The order of arguments can be
swapped so that the first argument becomes the second and vice-versa. See
method swapArgs(FloatFloatFunction)
. Example:
Even more general, functions can be chained (composed, assembled). Assume we
have two unary functions g and h. The unary function
g(h(a)) applying both in sequence can be generated via
chain(FloatFunction,FloatFunction)
:
chain(FloatFunction,FloatFloatFunction)
:
chain(FloatFloatFunction,FloatFunction,FloatFunction)
:
new FloatFloatFunction() { public final float apply(float a, float b) { return Math.sin(a) + Math.pow(Math.cos(b), 2); } }
For aliasing see functions
. Try this
// should yield 1.4399560356056456 in all cases float a = 0.5; float b = 0.2; float v = Math.sin(a) + Math.pow(Math.cos(b), 2); System.out.println(v); Functions F = Functions.functions; FloatFloatFunction f = F.chain(F.plus, F.sin, F.chain(F.square, F.cos)); System.out.println(f.apply(a, b)); FloatFloatFunction g = new FloatFloatFunction() { public float apply(float a, float b) { return Math.sin(a) + Math.pow(Math.cos(b), 2); } }; System.out.println(g.apply(a, b)); |
Iteration Performance [million
function evaluations per second] Pentium Pro 200 Mhz, SunJDK 1.2.2, NT, java -classic, |
||||||
30000000 iterations |
3000000 iterations (10 times less) | |||||
F.plus | a+b | F.chain(F.abs,F.chain(F.plus,F.sin,F.chain(F.square,F.cos))) | Math.abs(Math.sin(a) + Math.pow(Math.cos(b),2)) | |||
10.8 | 29.6 | 0.43 | 0.35 |
Modifier and Type | Field and Description |
---|---|
static FloatFunction |
abs
Function that returns Math.abs(a).
|
static FloatFunction |
acos
Function that returns Math.acos(a).
|
static FloatFunction |
asin
Function that returns Math.asin(a).
|
static FloatFunction |
atan
Function that returns Math.atan(a).
|
static FloatFloatFunction |
atan2
Function that returns Math.atan2(a,b).
|
static FloatFunction |
ceil
Function that returns Math.ceil(a).
|
static FloatFloatFunction |
compare
Function that returns a < b ? -1 : a > b ? 1 : 0.
|
static FloatFunction |
cos
Function that returns Math.cos(a).
|
static FloatFloatFunction |
div
Function that returns a / b.
|
static FloatFloatFunction |
divNeg
Function that returns -(a / b).
|
static FloatFloatFunction |
equals
Function that returns a == b ? 1 : 0.
|
static FloatFunction |
exp
Function that returns Math.exp(a).
|
static FloatFunction |
floor
Function that returns Math.floor(a).
|
static FloatFunctions |
functions
Little trick to allow for "aliasing", that is, renaming this class.
|
static FloatFloatFunction |
greater
Function that returns a > b ? 1 : 0.
|
static FloatFunction |
identity
Function that returns its argument.
|
static FloatFloatFunction |
IEEEremainder
Function that returns Math.IEEEremainder(a,b).
|
static FloatFunction |
inv
Function that returns 1.0 / a.
|
static FloatFloatProcedure |
isEqual
Function that returns a == b.
|
static FloatFloatProcedure |
isGreater
Function that returns a > b.
|
static FloatFloatProcedure |
isLess
Function that returns a < b.
|
static FloatFloatFunction |
less
Function that returns a < b ? 1 : 0.
|
static FloatFloatFunction |
lg
Function that returns Math.log(a) / Math.log(b).
|
static FloatFunction |
log
Function that returns Math.log(a).
|
static FloatFunction |
log2
Function that returns Math.log(a) / Math.log(2).
|
static FloatFloatFunction |
max
Function that returns Math.max(a,b).
|
static FloatFloatFunction |
min
Function that returns Math.min(a,b).
|
static FloatFloatFunction |
minus
Function that returns a - b.
|
static FloatFloatFunction |
mod
Function that returns a % b.
|
static FloatFloatFunction |
mult
Function that returns a * b.
|
static FloatFloatFunction |
multNeg
Function that returns -(a * b).
|
static FloatFloatFunction |
multSquare
Function that returns a * b^2.
|
static FloatFunction |
neg
Function that returns -a.
|
static FloatFloatFunction |
plus
Function that returns a + b.
|
static FloatFloatFunction |
plusAbs
Function that returns Math.abs(a) + Math.abs(b).
|
static FloatFloatFunction |
pow
Function that returns Math.pow(a,b).
|
static FloatFunction |
rint
Function that returns Math.rint(a).
|
static FloatFunction |
sign
Function that returns a < 0 ? -1 : a > 0 ? 1 : 0.
|
static FloatFunction |
sin
Function that returns Math.sin(a).
|
static FloatFunction |
sqrt
Function that returns Math.sqrt(a).
|
static FloatFunction |
square
Function that returns a * a.
|
static FloatFunction |
tan
Function that returns Math.tan(a).
|
Modifier and Type | Method and Description |
---|---|
static FloatFunction |
between(float from,
float to)
Constructs a function that returns (from<=a && a<=to) ? 1 : 0.
|
static FloatFunction |
bindArg1(FloatFloatFunction function,
float c)
Constructs a unary function from a binary function with the first operand
(argument) fixed to the given constant c.
|
static FloatFunction |
bindArg2(FloatFloatFunction function,
float c)
Constructs a unary function from a binary function with the second
operand (argument) fixed to the given constant c.
|
static FloatFloatFunction |
chain(FloatFloatFunction f,
FloatFunction g,
FloatFunction h)
Constructs the function f( g(a), h(b) ).
|
static FloatFloatFunction |
chain(FloatFunction g,
FloatFloatFunction h)
Constructs the function g( h(a,b) ).
|
static FloatFunction |
chain(FloatFunction g,
FloatFunction h)
Constructs the function g( h(a) ).
|
static FloatFunction |
compare(float b)
Constructs a function that returns a < b ? -1 : a > b ? 1 : 0.
|
static FloatFunction |
constant(float c)
Constructs a function that returns the constant c.
|
static void |
demo1()
Demonstrates usage of this class.
|
static void |
demo2(int size)
Benchmarks and demonstrates usage of trivial and complex functions.
|
static FloatFunction |
div(float b)
Constructs a function that returns a / b.
|
static FloatFunction |
equals(float b)
Constructs a function that returns a == b ? 1 : 0.
|
static FloatFunction |
greater(float b)
Constructs a function that returns a > b ? 1 : 0.
|
static FloatFunction |
IEEEremainder(float b)
Constructs a function that returns Math.IEEEremainder(a,b).
|
static FloatProcedure |
isBetween(float from,
float to)
Constructs a function that returns from<=a && a<=to.
|
static FloatProcedure |
isEqual(float b)
Constructs a function that returns a == b.
|
static FloatProcedure |
isGreater(float b)
Constructs a function that returns a > b.
|
static FloatProcedure |
isLess(float b)
Constructs a function that returns a < b.
|
static FloatFunction |
less(float b)
Constructs a function that returns a < b ? 1 : 0.
|
static FloatFunction |
lg(float b)
Constructs a function that returns Math.log(a) / Math.log(b)
.
|
static FloatFunction |
max(float b)
Constructs a function that returns Math.max(a,b).
|
static FloatFunction |
min(float b)
Constructs a function that returns Math.min(a,b).
|
static FloatFunction |
minus(float b)
Constructs a function that returns a - b.
|
static FloatFloatFunction |
minusMult(float constant)
Constructs a function that returns a - b*constant.
|
static FloatFunction |
mod(float b)
Constructs a function that returns a % b.
|
static FloatFunction |
mult(float b)
Constructs a function that returns a * b.
|
static FloatFloatFunction |
multSecond(float constant) |
static FloatFunction |
plus(float b)
Constructs a function that returns a + b.
|
static FloatFloatFunction |
plusMultFirst(float constant)
Constructs a function that returns a * constant + b.
|
static FloatFloatFunction |
plusMultSecond(float constant)
Constructs a function that returns a + b*constant.
|
static FloatFunction |
pow(float b)
Constructs a function that returns Math.pow(a,b).
|
static FloatFunction |
random()
Constructs a function that returns a new uniform random number in the
open unit interval
(0.0,1.0) (excluding 0.0 and 1.0). |
static FloatFunction |
round(float precision)
Constructs a function that returns the number rounded to the given
precision; Math.rint(a/precision)*precision.
|
static FloatFloatFunction |
swapArgs(FloatFloatFunction function)
Constructs a function that returns function.apply(b,a), i.e.
|
public static final FloatFunctions functions
Functions.chain(Functions.plus,Functions.sin,Functions.chain(Functions.square,Functions.cos));
is a bit awkward, to say the least. Using the aliasing you can instead write
Functions F = Functions.functions;
F.chain(F.plus,F.sin,F.chain(F.square,F.cos));
public static final FloatFunction abs
public static final FloatFunction acos
public static final FloatFunction asin
public static final FloatFunction atan
public static final FloatFunction ceil
public static final FloatFunction cos
public static final FloatFunction exp
public static final FloatFunction floor
public static final FloatFunction identity
public static final FloatFunction inv
public static final FloatFunction log
public static final FloatFunction log2
public static final FloatFunction neg
public static final FloatFunction rint
public static final FloatFunction sign
public static final FloatFunction sin
public static final FloatFunction sqrt
public static final FloatFunction square
public static final FloatFunction tan
public static final FloatFloatFunction atan2
public static final FloatFloatFunction compare
public static final FloatFloatFunction div
public static final FloatFloatFunction divNeg
public static final FloatFloatFunction equals
public static final FloatFloatFunction greater
public static final FloatFloatFunction IEEEremainder
public static final FloatFloatProcedure isEqual
public static final FloatFloatProcedure isLess
public static final FloatFloatProcedure isGreater
public static final FloatFloatFunction less
public static final FloatFloatFunction lg
public static final FloatFloatFunction max
public static final FloatFloatFunction min
public static final FloatFloatFunction minus
public static final FloatFloatFunction mod
public static final FloatFloatFunction mult
public static final FloatFloatFunction multNeg
public static final FloatFloatFunction multSquare
public static final FloatFloatFunction plus
public static final FloatFloatFunction plusAbs
public static final FloatFloatFunction pow
public static FloatFunction between(float from, float to)
public static FloatFunction bindArg1(FloatFloatFunction function, float c)
function
- a binary function taking operands in the form
function.apply(c,var).public static FloatFunction bindArg2(FloatFloatFunction function, float c)
function
- a binary function taking operands in the form
function.apply(var,c).public static FloatFloatFunction chain(FloatFloatFunction f, FloatFunction g, FloatFunction h)
f
- a binary function.g
- a unary function.h
- a unary function.public static FloatFloatFunction chain(FloatFunction g, FloatFloatFunction h)
g
- a unary function.h
- a binary function.public static FloatFunction chain(FloatFunction g, FloatFunction h)
g
- a unary function.h
- a unary function.public static FloatFunction compare(float b)
public static FloatFunction constant(float c)
public static void demo1()
public static void demo2(int size)
public static FloatFunction div(float b)
public static FloatFunction equals(float b)
public static FloatFunction greater(float b)
public static FloatFunction IEEEremainder(float b)
public static FloatProcedure isBetween(float from, float to)
public static FloatProcedure isEqual(float b)
public static FloatProcedure isGreater(float b)
public static FloatProcedure isLess(float b)
public static FloatFunction less(float b)
public static FloatFunction lg(float b)
public static FloatFunction max(float b)
public static FloatFunction min(float b)
public static FloatFunction minus(float b)
public static FloatFloatFunction minusMult(float constant)
public static FloatFunction mod(float b)
public static FloatFunction mult(float b)
public static FloatFunction plus(float b)
public static FloatFloatFunction multSecond(float constant)
public static FloatFloatFunction plusMultSecond(float constant)
public static FloatFloatFunction plusMultFirst(float constant)
public static FloatFunction pow(float b)
public static FloatFunction random()
(0.0,1.0)
(excluding 0.0 and 1.0).
Currently the engine is
FloatMersenneTwister
and is seeded
with the current time.
Note that any random engine derived from
FloatRandomEngine
and any random
distribution derived from
AbstractFloatDistribution
are function
objects, because they implement the proper interfaces. Thus, if you are
not happy with the default, just pass your favourite random generator to
function evaluating methods.
public static FloatFunction round(float precision)
precision = 0.01 rounds 0.012 --> 0.01, 0.018 --> 0.02 precision = 10 rounds 123 --> 120 , 127 --> 130
public static FloatFloatFunction swapArgs(FloatFloatFunction function)
function
- a function taking operands in the form
function.apply(a,b).Jump to the Parallel Colt Homepage