public final class Functions 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(org.apache.mahout.math.function.DoubleDoubleFunction ,double)
and bindArg2(org.apache.mahout.math.function.DoubleDoubleFunction ,double)
. The order of arguments can
be swapped so that the first argument becomes the
second and vice-versa. See method swapArgs(org.apache.mahout.math.function.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(org.apache.mahout.math.function.DoubleFunction , org.apache.mahout.math.function.DoubleFunction)
:
chain(org.apache.mahout.math.function.DoubleFunction , org.apache.mahout.math.function.DoubleDoubleFunction)
:
chain(org.apache.mahout.math.function.DoubleDoubleFunction , org.apache.mahout.math.function.DoubleFunction ,
org.apache.mahout.math.function.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); log.info(v); Functions F = Functions.functions; DoubleDoubleFunction f = F.chain(F.plus,F.sin,F.chain(F.square,F.cos)); log.info(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); } }; log.info(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 |
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 DoubleDoubleFunction |
GREATER
Function that returns a > b ? 1 : 0.
|
static DoubleFunction |
IDENTITY
Function that returns its argument.
|
static DoubleDoubleFunction |
IEEE_REMAINDER
Function that returns Math.IEEEremainder(a,b).
|
static DoubleFunction |
INV
Function that returns 1.0 / a.
|
static DoubleDoubleProcedure |
IS_EQUAL
Function that returns a == b.
|
static DoubleDoubleProcedure |
IS_GREATER
Function that returns
a > b . |
static DoubleDoubleProcedure |
IS_LESS
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 |
LOG2
Function that returns Math.log(a) / Math.log(2).
|
static DoubleFunction |
LOGARITHM
Function that returns Math.log(a).
|
static DoubleDoubleFunction |
MAX
Function that returns Math.max(a,b).
|
static DoubleDoubleFunction |
MAX_ABS |
static DoubleDoubleFunction |
MIN
Function that returns Math.min(a,b).
|
static DoubleDoubleFunction |
MINUS
Function that returns a - b.
|
static DoubleDoubleFunction |
MINUS_ABS |
static DoubleDoubleFunction |
MINUS_SQUARED |
static DoubleDoubleFunction |
MOD
Function that returns a % b.
|
static DoubleDoubleFunction |
MULT
Function that returns a * b.
|
static DoubleDoubleFunction |
MULT_RIGHT_PLUS1 |
static DoubleDoubleFunction |
MULT_SQUARE_LEFT |
static DoubleFunction |
NEGATE
Function that returns -a.
|
static DoubleDoubleFunction |
PLUS
Function that returns a + b.
|
static DoubleDoubleFunction |
PLUS_ABS
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 DoubleDoubleFunction |
SECOND |
static DoubleDoubleFunction |
SECOND_LEFT_ZERO
This function is specifically designed to be used when assigning a vector to one that is all zeros (created
by like()).
|
static DoubleFunction |
SIGMOID
Function that returns 1 / (1 + exp(-a)
|
static DoubleFunction |
SIGMOIDGRADIENT
Function that returns a * (1-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 IntIntFunction |
chain(DoubleFunction g,
IntIntFunction 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 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 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 |
mathIEEEremainder(double b)
Constructs a function that returns Math.IEEEremainder(a,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 |
minusAbsPow(double exponent) |
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 DoubleFunction |
notEqual(double b)
Constructs a function that returns a != b ? 1 : 0.
|
static DoubleFunction |
plus(double b)
Constructs a function that returns a + b.
|
static DoubleDoubleFunction |
plusMult(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 DoubleDoubleFunction |
reweigh(double wx,
double wy) |
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 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 LOGARITHM
public static final DoubleFunction LOG2
public static final DoubleFunction NEGATE
public static final DoubleFunction RINT
public static final DoubleFunction SIGN
a < 0 ? -1 : a > 0 ? 1 : 0
.public static final DoubleFunction SIN
public static final DoubleFunction SQRT
public static final DoubleFunction SQUARE
public static final DoubleFunction SIGMOID
public static final DoubleFunction SIGMOIDGRADIENT
public static final DoubleFunction TAN
public static final DoubleDoubleFunction ATAN2
public static final DoubleDoubleFunction COMPARE
public static final DoubleDoubleFunction DIV
public static final DoubleDoubleFunction EQUALS
public static final DoubleDoubleFunction GREATER
public static final DoubleDoubleFunction IEEE_REMAINDER
public static final DoubleDoubleProcedure IS_EQUAL
public static final DoubleDoubleProcedure IS_LESS
a < b
.public static final DoubleDoubleProcedure IS_GREATER
a > b
.public static final DoubleDoubleFunction LESS
public static final DoubleDoubleFunction LG
public static final DoubleDoubleFunction MAX
public static final DoubleDoubleFunction MAX_ABS
public static final DoubleDoubleFunction MIN
public static final DoubleDoubleFunction MINUS
public static final DoubleDoubleFunction MINUS_SQUARED
public static final DoubleDoubleFunction MOD
public static final DoubleDoubleFunction MULT
public static final DoubleDoubleFunction PLUS
public static final DoubleDoubleFunction PLUS_ABS
public static final DoubleDoubleFunction MINUS_ABS
public static final DoubleDoubleFunction POW
public static final DoubleDoubleFunction SECOND
public static final DoubleDoubleFunction SECOND_LEFT_ZERO
public static final DoubleDoubleFunction MULT_SQUARE_LEFT
public static final DoubleDoubleFunction MULT_RIGHT_PLUS1
public static DoubleDoubleFunction reweigh(double wx, double wy)
public static DoubleFunction between(double from, double to)
(from<=a && a<=to) ? 1 : 0
.
a is a variable, from and to are fixed.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 IntIntFunction chain(DoubleFunction g, IntIntFunction h)
g
- a unary function.h
- an IntIntFunction
function.public static DoubleFunction compare(double b)
a < b ? -1 : a > b ? 1 : 0
. a is a variable, b is
fixed.public static DoubleFunction constant(double c)
public static DoubleFunction div(double b)
public static DoubleFunction equals(double b)
public static DoubleFunction notEqual(double b)
public static DoubleFunction greater(double b)
public static DoubleFunction mathIEEEremainder(double b)
public static DoubleProcedure isBetween(double from, double to)
from<=a && a<=to
. a
is a variable, from and
to are fixed.
Note that DoubleProcedure is generated code and thus looks like an invalid reference unless you can see
the generated stuff.public static DoubleProcedure isEqual(double b)
public static DoubleProcedure isGreater(double b)
public static DoubleProcedure isLess(double b)
a < b
. a is a
variable, b is fixed.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 plusMult(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 MersenneTwister
and is
seeded with the current time. Note that any random engine derived from RandomEngine
and any random distribution derived from AbstractDistribution
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).public static DoubleDoubleFunction minusAbsPow(double exponent)
Copyright © 2008–2017 The Apache Software Foundation. All rights reserved.