public class DoubleFunctions 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 DoubleFunction
, binary functions of type
DoubleDoubleFunction
. All can be retrieved
via public
static final variables named after the function. Unary predicates are of
type DoubleProcedure
, binary predicates of
type DoubleDoubleProcedure
. 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(DoubleDoubleFunction,double)
and
bindArg2(DoubleDoubleFunction,double)
. The order of arguments can be
swapped so that the first argument becomes the second and vice-versa. See
method swapArgs(DoubleDoubleFunction)
. 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(DoubleFunction,DoubleFunction)
:
chain(DoubleFunction,DoubleDoubleFunction)
:
chain(DoubleDoubleFunction,DoubleFunction,DoubleFunction)
:
new DoubleDoubleFunction() { public final double apply(double a, double b) { return Math.sin(a) + Math.pow(Math.cos(b), 2); } }
For aliasing see functions
. Try this
// should yield 1.4399560356056456 in all cases double a = 0.5; double b = 0.2; double v = Math.sin(a) + Math.pow(Math.cos(b), 2); System.out.println(v); Functions F = Functions.functions; DoubleDoubleFunction f = F.chain(F.plus, F.sin, F.chain(F.square, F.cos)); System.out.println(f.apply(a, b)); DoubleDoubleFunction g = new DoubleDoubleFunction() { public double apply(double a, double 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 DoubleFunction |
abs
Function that returns Math.abs(a).
|
static DoubleFunction |
acos
Function that returns Math.acos(a).
|
static DoubleFunction |
asin
Function that returns Math.asin(a).
|
static DoubleFunction |
atan
Function that returns Math.atan(a).
|
static DoubleDoubleFunction |
atan2
Function that returns Math.atan2(a,b).
|
static DoubleFunction |
ceil
Function that returns Math.ceil(a).
|
static DoubleDoubleFunction |
compare
Function that returns a < b ? -1 : a > b ? 1 : 0.
|
static DoubleFunction |
cos
Function that returns Math.cos(a).
|
static DoubleDoubleFunction |
div
Function that returns a / b.
|
static DoubleDoubleFunction |
divNeg
Function that returns -(a / b).
|
static DoubleDoubleFunction |
equals
Function that returns a == b ? 1 : 0.
|
static DoubleFunction |
exp
Function that returns Math.exp(a).
|
static DoubleFunction |
floor
Function that returns Math.floor(a).
|
static DoubleFunctions |
functions
Little trick to allow for "aliasing", that is, renaming this class.
|
static DoubleDoubleFunction |
greater
Function that returns a > b ? 1 : 0.
|
static DoubleFunction |
identity
Function that returns its argument.
|
static DoubleDoubleFunction |
IEEEremainder
Function that returns Math.IEEEremainder(a,b).
|
static DoubleFunction |
inv
Function that returns 1.0 / a.
|
static DoubleDoubleProcedure |
isEqual
Function that returns a == b.
|
static DoubleDoubleProcedure |
isGreater
Function that returns a > b.
|
static DoubleDoubleProcedure |
isLess
Function that returns a < b.
|
static DoubleDoubleFunction |
less
Function that returns a < b ? 1 : 0.
|
static DoubleDoubleFunction |
lg
Function that returns Math.log(a) / Math.log(b).
|
static DoubleFunction |
log
Function that returns Math.log(a).
|
static DoubleFunction |
log2
Function that returns Math.log(a) / Math.log(2).
|
static DoubleDoubleFunction |
max
Function that returns Math.max(a,b).
|
static DoubleDoubleFunction |
min
Function that returns Math.min(a,b).
|
static DoubleDoubleFunction |
minus
Function that returns a - b.
|
static DoubleDoubleFunction |
mod
Function that returns a % b.
|
static DoubleDoubleFunction |
mult
Function that returns a * b.
|
static DoubleDoubleFunction |
multNeg
Function that returns -(a * b).
|
static DoubleDoubleFunction |
multSquare
Function that returns a * b^2.
|
static DoubleFunction |
neg
Function that returns -a.
|
static DoubleDoubleFunction |
plus
Function that returns a + b.
|
static DoubleDoubleFunction |
plusAbs
Function that returns Math.abs(a) + Math.abs(b).
|
static DoubleDoubleFunction |
pow
Function that returns Math.pow(a,b).
|
static DoubleFunction |
rint
Function that returns Math.rint(a).
|
static DoubleFunction |
sign
Function that returns a < 0 ? -1 : a > 0 ? 1 : 0.
|
static DoubleFunction |
sin
Function that returns Math.sin(a).
|
static DoubleFunction |
sqrt
Function that returns Math.sqrt(a).
|
static DoubleFunction |
square
Function that returns a * a.
|
static DoubleFunction |
tan
Function that returns Math.tan(a).
|
Modifier and Type | Method and Description |
---|---|
static DoubleFunction |
between(double from,
double to)
Constructs a function that returns (from<=a && a<=to) ? 1 : 0.
|
static DoubleFunction |
bindArg1(DoubleDoubleFunction function,
double c)
Constructs a unary function from a binary function with the first operand
(argument) fixed to the given constant c.
|
static DoubleFunction |
bindArg2(DoubleDoubleFunction function,
double c)
Constructs a unary function from a binary function with the second
operand (argument) fixed to the given constant c.
|
static DoubleDoubleFunction |
chain(DoubleDoubleFunction f,
DoubleFunction g,
DoubleFunction h)
Constructs the function f( g(a), h(b) ).
|
static DoubleDoubleFunction |
chain(DoubleFunction g,
DoubleDoubleFunction h)
Constructs the function g( h(a,b) ).
|
static DoubleFunction |
chain(DoubleFunction g,
DoubleFunction h)
Constructs the function g( h(a) ).
|
static DoubleFunction |
compare(double b)
Constructs a function that returns a < b ? -1 : a > b ? 1 : 0.
|
static DoubleFunction |
constant(double 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 DoubleFunction |
div(double b)
Constructs a function that returns a / b.
|
static DoubleFunction |
equals(double b)
Constructs a function that returns a == b ? 1 : 0.
|
static DoubleFunction |
greater(double b)
Constructs a function that returns a > b ? 1 : 0.
|
static DoubleFunction |
IEEEremainder(double b)
Constructs a function that returns Math.IEEEremainder(a,b).
|
static DoubleProcedure |
isBetween(double from,
double to)
Constructs a function that returns from<=a && a<=to.
|
static DoubleProcedure |
isEqual(double b)
Constructs a function that returns a == b.
|
static DoubleProcedure |
isGreater(double b)
Constructs a function that returns a > b.
|
static DoubleProcedure |
isLess(double b)
Constructs a function that returns a < b.
|
static DoubleFunction |
less(double b)
Constructs a function that returns a < b ? 1 : 0.
|
static DoubleFunction |
lg(double b)
Constructs a function that returns Math.log(a) / Math.log(b)
.
|
static DoubleFunction |
max(double b)
Constructs a function that returns Math.max(a,b).
|
static DoubleFunction |
min(double b)
Constructs a function that returns Math.min(a,b).
|
static DoubleFunction |
minus(double b)
Constructs a function that returns a - b.
|
static DoubleDoubleFunction |
minusMult(double constant)
Constructs a function that returns a - b*constant.
|
static DoubleFunction |
mod(double b)
Constructs a function that returns a % b.
|
static DoubleFunction |
mult(double b)
Constructs a function that returns a * b.
|
static DoubleDoubleFunction |
multSecond(double constant)
Constructs a function that returns b*constant.
|
static DoubleFunction |
plus(double b)
Constructs a function that returns a + b.
|
static DoubleDoubleFunction |
plusMultFirst(double constant)
Constructs a function that returns a * constant + b.
|
static DoubleDoubleFunction |
plusMultSecond(double constant)
Constructs a function that returns a + b*constant.
|
static DoubleFunction |
pow(double b)
Constructs a function that returns Math.pow(a,b).
|
static DoubleFunction |
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 DoubleFunction |
round(double precision)
Constructs a function that returns the number rounded to the given
precision; Math.rint(a/precision)*precision.
|
static DoubleDoubleFunction |
swapArgs(DoubleDoubleFunction function)
Constructs a function that returns function.apply(b,a), i.e.
|
public static final DoubleFunctions 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 DoubleFunction abs
public static final DoubleFunction acos
public static final DoubleFunction asin
public static final DoubleFunction atan
public static final DoubleFunction ceil
public static final DoubleFunction cos
public static final DoubleFunction exp
public static final DoubleFunction floor
public static final DoubleFunction identity
public static final DoubleFunction inv
public static final DoubleFunction log
public static final DoubleFunction log2
public static final DoubleFunction neg
public static final DoubleFunction rint
public static final DoubleFunction sign
public static final DoubleFunction sin
public static final DoubleFunction sqrt
public static final DoubleFunction square
public static final DoubleFunction tan
public static final DoubleDoubleFunction atan2
public static final DoubleDoubleFunction compare
public static final DoubleDoubleFunction div
public static final DoubleDoubleFunction divNeg
public static final DoubleDoubleFunction equals
public static final DoubleDoubleFunction greater
public static final DoubleDoubleFunction IEEEremainder
public static final DoubleDoubleProcedure isEqual
public static final DoubleDoubleProcedure isLess
public static final DoubleDoubleProcedure isGreater
public static final DoubleDoubleFunction less
public static final DoubleDoubleFunction lg
public static final DoubleDoubleFunction max
public static final DoubleDoubleFunction min
public static final DoubleDoubleFunction minus
public static final DoubleDoubleFunction mod
public static final DoubleDoubleFunction mult
public static final DoubleDoubleFunction multNeg
public static final DoubleDoubleFunction multSquare
public static final DoubleDoubleFunction plus
public static final DoubleDoubleFunction plusAbs
public static final DoubleDoubleFunction pow
public static DoubleFunction between(double from, double to)
public static DoubleFunction bindArg1(DoubleDoubleFunction function, double c)
function
- a binary function taking operands in the form
function.apply(c,var).public static DoubleFunction bindArg2(DoubleDoubleFunction function, double c)
function
- a binary function taking operands in the form
function.apply(var,c).public static DoubleDoubleFunction chain(DoubleDoubleFunction f, DoubleFunction g, DoubleFunction h)
f
- a binary function.g
- a unary function.h
- a unary function.public static DoubleDoubleFunction chain(DoubleFunction g, DoubleDoubleFunction h)
g
- a unary function.h
- a binary function.public static DoubleFunction chain(DoubleFunction g, DoubleFunction h)
g
- a unary function.h
- a unary function.public static DoubleFunction compare(double b)
public static DoubleFunction constant(double c)
public static void demo1()
public static void demo2(int size)
public static DoubleFunction div(double b)
public static DoubleFunction equals(double b)
public static DoubleFunction greater(double b)
public static DoubleFunction IEEEremainder(double b)
public static DoubleProcedure isBetween(double from, double to)
public static DoubleProcedure isEqual(double b)
public static DoubleProcedure isGreater(double b)
public static DoubleProcedure isLess(double b)
public static DoubleFunction less(double b)
public static DoubleFunction lg(double b)
public static DoubleFunction max(double b)
public static DoubleFunction min(double b)
public static DoubleFunction minus(double b)
public static DoubleDoubleFunction minusMult(double constant)
public static DoubleFunction mod(double b)
public static DoubleFunction mult(double b)
public static DoubleFunction plus(double b)
public static DoubleDoubleFunction multSecond(double constant)
public static DoubleDoubleFunction plusMultSecond(double constant)
public static DoubleDoubleFunction plusMultFirst(double constant)
public static DoubleFunction pow(double b)
public static DoubleFunction random()
(0.0,1.0)
(excluding 0.0 and 1.0).
Currently the engine is
DoubleMersenneTwister
and is
seeded with the current time.
Note that any random engine derived from
DoubleRandomEngine
and any random
distribution derived from
AbstractDoubleDistribution
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 DoubleFunction round(double precision)
precision = 0.01 rounds 0.012 --> 0.01, 0.018 --> 0.02 precision = 10 rounds 123 --> 120 , 127 --> 130
public static DoubleDoubleFunction swapArgs(DoubleDoubleFunction function)
function
- a function taking operands in the form
function.apply(a,b).Jump to the Parallel Colt Homepage