Class FloatFunctions
- java.lang.Object
-
- cern.jet.math.tfloat.FloatFunctions
-
public class FloatFunctions extends java.lang.ObjectFunction objects to be passed to generic methods. Contains the functions ofMathas 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 typeFloatFloatFunction. All can be retrieved via public static final variables named after the function. Unary predicates are of typeFloatProcedure, binary predicates of typeFloatFloatProcedure. 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:
- Functions.pow gives the function ab.
- Functions.pow.apply(2,3)==8.
- Functions.pow(3) gives the function a3.
- Functions.pow(3).apply(2)==8.
bindArg1(FloatFloatFunction,float)andbindArg2(FloatFloatFunction,float). The order of arguments can be swapped so that the first argument becomes the second and vice-versa. See methodswapArgs(FloatFloatFunction). Example:- Functions.pow gives the function ab.
- Functions.bindArg2(Functions.pow,3) gives the function x3.
- Functions.bindArg1(Functions.pow,3) gives the function 3x.
- Functions.swapArgs(Functions.pow) gives the function ba.
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):- Functions.chain(g,h);
chain(FloatFunction,FloatFloatFunction):- Functions.chain(g,f);
chain(FloatFloatFunction,FloatFunction,FloatFunction):- Functions.chain(f,g,h);
- chain(plus,sin,chain(square,cos));
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));Performance
Surprise. Using modern non-adaptive JITs such as SunJDK 1.2.2 (java -classic) there seems to be no or only moderate performance penalty in using function objects in a loop over traditional code in a loop. For complex nested function objects (e.g. F.chain(F.abs,F.chain(F.plus,F.sin,F.chain(F.square,F.cos)))) the penalty is zero, for trivial functions (e.g. F.plus) the penalty is often acceptable.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
-
-
Field Summary
Fields Modifier and Type Field and Description static FloatFunctionabsFunction that returns Math.abs(a).static FloatFunctionacosFunction that returns Math.acos(a).static FloatFunctionasinFunction that returns Math.asin(a).static FloatFunctionatanFunction that returns Math.atan(a).static FloatFloatFunctionatan2Function that returns Math.atan2(a,b).static FloatFunctionceilFunction that returns Math.ceil(a).static FloatFloatFunctioncompareFunction that returns a < b ? -1 : a > b ? 1 : 0.static FloatFunctioncosFunction that returns Math.cos(a).static FloatFloatFunctiondivFunction that returns a / b.static FloatFloatFunctiondivNegFunction that returns -(a / b).static FloatFloatFunctionequalsFunction that returns a == b ? 1 : 0.static FloatFunctionexpFunction that returns Math.exp(a).static FloatFunctionfloorFunction that returns Math.floor(a).static FloatFunctionsfunctionsLittle trick to allow for "aliasing", that is, renaming this class.static FloatFloatFunctiongreaterFunction that returns a > b ? 1 : 0.static FloatFunctionidentityFunction that returns its argument.static FloatFloatFunctionIEEEremainderFunction that returns Math.IEEEremainder(a,b).static FloatFunctioninvFunction that returns 1.0 / a.static FloatFloatProcedureisEqualFunction that returns a == b.static FloatFloatProcedureisGreaterFunction that returns a > b.static FloatFloatProcedureisLessFunction that returns a < b.static FloatFloatFunctionlessFunction that returns a < b ? 1 : 0.static FloatFloatFunctionlgFunction that returns Math.log(a) / Math.log(b).static FloatFunctionlogFunction that returns Math.log(a).static FloatFunctionlog2Function that returns Math.log(a) / Math.log(2).static FloatFloatFunctionmaxFunction that returns Math.max(a,b).static FloatFloatFunctionminFunction that returns Math.min(a,b).static FloatFloatFunctionminusFunction that returns a - b.static FloatFloatFunctionmodFunction that returns a % b.static FloatFloatFunctionmultFunction that returns a * b.static FloatFloatFunctionmultNegFunction that returns -(a * b).static FloatFloatFunctionmultSquareFunction that returns a * b^2.static FloatFunctionnegFunction that returns -a.static FloatFloatFunctionplusFunction that returns a + b.static FloatFloatFunctionplusAbsFunction that returns Math.abs(a) + Math.abs(b).static FloatFloatFunctionpowFunction that returns Math.pow(a,b).static FloatFunctionrintFunction that returns Math.rint(a).static FloatFunctionsignFunction that returns a < 0 ? -1 : a > 0 ? 1 : 0.static FloatFunctionsinFunction that returns Math.sin(a).static FloatFunctionsqrtFunction that returns Math.sqrt(a).static FloatFunctionsquareFunction that returns a * a.static FloatFunctiontanFunction that returns Math.tan(a).
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method and Description static FloatFunctionbetween(float from, float to)Constructs a function that returns (from<=a && a<=to) ? 1 : 0.static FloatFunctionbindArg1(FloatFloatFunction function, float c)Constructs a unary function from a binary function with the first operand (argument) fixed to the given constant c.static FloatFunctionbindArg2(FloatFloatFunction function, float c)Constructs a unary function from a binary function with the second operand (argument) fixed to the given constant c.static FloatFloatFunctionchain(FloatFloatFunction f, FloatFunction g, FloatFunction h)Constructs the function f( g(a), h(b) ).static FloatFloatFunctionchain(FloatFunction g, FloatFloatFunction h)Constructs the function g( h(a,b) ).static FloatFunctionchain(FloatFunction g, FloatFunction h)Constructs the function g( h(a) ).static FloatFunctioncompare(float b)Constructs a function that returns a < b ? -1 : a > b ? 1 : 0.static FloatFunctionconstant(float c)Constructs a function that returns the constant c.static voiddemo1()Demonstrates usage of this class.static voiddemo2(int size)Benchmarks and demonstrates usage of trivial and complex functions.static FloatFunctiondiv(float b)Constructs a function that returns a / b.static FloatFunctionequals(float b)Constructs a function that returns a == b ? 1 : 0.static FloatFunctiongreater(float b)Constructs a function that returns a > b ? 1 : 0.static FloatFunctionIEEEremainder(float b)Constructs a function that returns Math.IEEEremainder(a,b).static FloatProcedureisBetween(float from, float to)Constructs a function that returns from<=a && a<=to.static FloatProcedureisEqual(float b)Constructs a function that returns a == b.static FloatProcedureisGreater(float b)Constructs a function that returns a > b.static FloatProcedureisLess(float b)Constructs a function that returns a < b.static FloatFunctionless(float b)Constructs a function that returns a < b ? 1 : 0.static FloatFunctionlg(float b)Constructs a function that returns Math.log(a) / Math.log(b) .static FloatFunctionmax(float b)Constructs a function that returns Math.max(a,b).static FloatFunctionmin(float b)Constructs a function that returns Math.min(a,b).static FloatFunctionminus(float b)Constructs a function that returns a - b.static FloatFloatFunctionminusMult(float constant)Constructs a function that returns a - b*constant.static FloatFunctionmod(float b)Constructs a function that returns a % b.static FloatFunctionmult(float b)Constructs a function that returns a * b.static FloatFloatFunctionmultSecond(float constant)static FloatFunctionplus(float b)Constructs a function that returns a + b.static FloatFloatFunctionplusMultFirst(float constant)Constructs a function that returns a * constant + b.static FloatFloatFunctionplusMultSecond(float constant)Constructs a function that returns a + b*constant.static FloatFunctionpow(float b)Constructs a function that returns Math.pow(a,b).static FloatFunctionrandom()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 FloatFunctionround(float precision)Constructs a function that returns the number rounded to the given precision; Math.rint(a/precision)*precision.static FloatFloatFunctionswapArgs(FloatFloatFunction function)Constructs a function that returns function.apply(b,a), i.e.
-
-
-
Field Detail
-
functions
public static final FloatFunctions functions
Little trick to allow for "aliasing", that is, renaming this class. Writing code likeFunctions.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));
-
abs
public static final FloatFunction abs
Function that returns Math.abs(a).
-
acos
public static final FloatFunction acos
Function that returns Math.acos(a).
-
asin
public static final FloatFunction asin
Function that returns Math.asin(a).
-
atan
public static final FloatFunction atan
Function that returns Math.atan(a).
-
ceil
public static final FloatFunction ceil
Function that returns Math.ceil(a).
-
cos
public static final FloatFunction cos
Function that returns Math.cos(a).
-
exp
public static final FloatFunction exp
Function that returns Math.exp(a).
-
floor
public static final FloatFunction floor
Function that returns Math.floor(a).
-
identity
public static final FloatFunction identity
Function that returns its argument.
-
inv
public static final FloatFunction inv
Function that returns 1.0 / a.
-
log
public static final FloatFunction log
Function that returns Math.log(a).
-
log2
public static final FloatFunction log2
Function that returns Math.log(a) / Math.log(2).
-
neg
public static final FloatFunction neg
Function that returns -a.
-
rint
public static final FloatFunction rint
Function that returns Math.rint(a).
-
sign
public static final FloatFunction sign
Function that returns a < 0 ? -1 : a > 0 ? 1 : 0.
-
sin
public static final FloatFunction sin
Function that returns Math.sin(a).
-
sqrt
public static final FloatFunction sqrt
Function that returns Math.sqrt(a).
-
square
public static final FloatFunction square
Function that returns a * a.
-
tan
public static final FloatFunction tan
Function that returns Math.tan(a).
-
atan2
public static final FloatFloatFunction atan2
Function that returns Math.atan2(a,b).
-
compare
public static final FloatFloatFunction compare
Function that returns a < b ? -1 : a > b ? 1 : 0.
-
div
public static final FloatFloatFunction div
Function that returns a / b.
-
divNeg
public static final FloatFloatFunction divNeg
Function that returns -(a / b).
-
equals
public static final FloatFloatFunction equals
Function that returns a == b ? 1 : 0.
-
greater
public static final FloatFloatFunction greater
Function that returns a > b ? 1 : 0.
-
IEEEremainder
public static final FloatFloatFunction IEEEremainder
Function that returns Math.IEEEremainder(a,b).
-
isEqual
public static final FloatFloatProcedure isEqual
Function that returns a == b.
-
isLess
public static final FloatFloatProcedure isLess
Function that returns a < b.
-
isGreater
public static final FloatFloatProcedure isGreater
Function that returns a > b.
-
less
public static final FloatFloatFunction less
Function that returns a < b ? 1 : 0.
-
lg
public static final FloatFloatFunction lg
Function that returns Math.log(a) / Math.log(b).
-
max
public static final FloatFloatFunction max
Function that returns Math.max(a,b).
-
min
public static final FloatFloatFunction min
Function that returns Math.min(a,b).
-
minus
public static final FloatFloatFunction minus
Function that returns a - b.
-
mod
public static final FloatFloatFunction mod
Function that returns a % b.
-
mult
public static final FloatFloatFunction mult
Function that returns a * b.
-
multNeg
public static final FloatFloatFunction multNeg
Function that returns -(a * b).
-
multSquare
public static final FloatFloatFunction multSquare
Function that returns a * b^2.
-
plus
public static final FloatFloatFunction plus
Function that returns a + b.
-
plusAbs
public static final FloatFloatFunction plusAbs
Function that returns Math.abs(a) + Math.abs(b).
-
pow
public static final FloatFloatFunction pow
Function that returns Math.pow(a,b).
-
-
Method Detail
-
between
public static FloatFunction between(float from, float to)
Constructs a function that returns (from<=a && a<=to) ? 1 : 0. a is a variable, from and to are fixed.
-
bindArg1
public 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. The second operand is variable (free).- Parameters:
function- a binary function taking operands in the form function.apply(c,var).- Returns:
- the unary function function(c,var).
-
bindArg2
public 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. The first operand is variable (free).- Parameters:
function- a binary function taking operands in the form function.apply(var,c).- Returns:
- the unary function function(var,c).
-
chain
public static FloatFloatFunction chain(FloatFloatFunction f, FloatFunction g, FloatFunction h)
Constructs the function f( g(a), h(b) ).- Parameters:
f- a binary function.g- a unary function.h- a unary function.- Returns:
- the binary function f( g(a), h(b) ).
-
chain
public static FloatFloatFunction chain(FloatFunction g, FloatFloatFunction h)
Constructs the function g( h(a,b) ).- Parameters:
g- a unary function.h- a binary function.- Returns:
- the unary function g( h(a,b) ).
-
chain
public static FloatFunction chain(FloatFunction g, FloatFunction h)
Constructs the function g( h(a) ).- Parameters:
g- a unary function.h- a unary function.- Returns:
- the unary function g( h(a) ).
-
compare
public static FloatFunction compare(float b)
Constructs a function that returns a < b ? -1 : a > b ? 1 : 0. a is a variable, b is fixed.
-
constant
public static FloatFunction constant(float c)
Constructs a function that returns the constant c.
-
demo1
public static void demo1()
Demonstrates usage of this class.
-
demo2
public static void demo2(int size)
Benchmarks and demonstrates usage of trivial and complex functions.
-
div
public static FloatFunction div(float b)
Constructs a function that returns a / b. a is a variable, b is fixed.
-
equals
public static FloatFunction equals(float b)
Constructs a function that returns a == b ? 1 : 0. a is a variable, b is fixed.
-
greater
public static FloatFunction greater(float b)
Constructs a function that returns a > b ? 1 : 0. a is a variable, b is fixed.
-
IEEEremainder
public static FloatFunction IEEEremainder(float b)
Constructs a function that returns Math.IEEEremainder(a,b). a is a variable, b is fixed.
-
isBetween
public static FloatProcedure isBetween(float from, float to)
Constructs a function that returns from<=a && a<=to. a is a variable, from and to are fixed.
-
isEqual
public static FloatProcedure isEqual(float b)
Constructs a function that returns a == b. a is a variable, b is fixed.
-
isGreater
public static FloatProcedure isGreater(float b)
Constructs a function that returns a > b. a is a variable, b is fixed.
-
isLess
public static FloatProcedure isLess(float b)
Constructs a function that returns a < b. a is a variable, b is fixed.
-
less
public static FloatFunction less(float b)
Constructs a function that returns a < b ? 1 : 0. a is a variable, b is fixed.
-
lg
public static FloatFunction lg(float b)
Constructs a function that returns Math.log(a) / Math.log(b) . a is a variable, b is fixed.
-
max
public static FloatFunction max(float b)
Constructs a function that returns Math.max(a,b). a is a variable, b is fixed.
-
min
public static FloatFunction min(float b)
Constructs a function that returns Math.min(a,b). a is a variable, b is fixed.
-
minus
public static FloatFunction minus(float b)
Constructs a function that returns a - b. a is a variable, b is fixed.
-
minusMult
public static FloatFloatFunction minusMult(float constant)
Constructs a function that returns a - b*constant. a and b are variables, constant is fixed.
-
mod
public static FloatFunction mod(float b)
Constructs a function that returns a % b. a is a variable, b is fixed.
-
mult
public static FloatFunction mult(float b)
Constructs a function that returns a * b. a is a variable, b is fixed.
-
plus
public static FloatFunction plus(float b)
Constructs a function that returns a + b. a is a variable, b is fixed.
-
multSecond
public static FloatFloatFunction multSecond(float constant)
-
plusMultSecond
public static FloatFloatFunction plusMultSecond(float constant)
Constructs a function that returns a + b*constant. a and b are variables, constant is fixed.
-
plusMultFirst
public static FloatFloatFunction plusMultFirst(float constant)
Constructs a function that returns a * constant + b. a and b are variables, constant is fixed.
-
pow
public static FloatFunction pow(float b)
Constructs a function that returns Math.pow(a,b). a is a variable, b is fixed.
-
random
public 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). Currently the engine isFloatMersenneTwisterand is seeded with the current time.Note that any random engine derived from
FloatRandomEngineand any random distribution derived fromAbstractFloatDistributionare 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.
-
round
public static FloatFunction round(float precision)
Constructs a function that returns the number rounded to the given precision; Math.rint(a/precision)*precision. Examples:precision = 0.01 rounds 0.012 --> 0.01, 0.018 --> 0.02 precision = 10 rounds 123 --> 120 , 127 --> 130
-
swapArgs
public static FloatFloatFunction swapArgs(FloatFloatFunction function)
Constructs a function that returns function.apply(b,a), i.e. applies the function with the first operand as second operand and the second operand as first operand.- Parameters:
function- a function taking operands in the form function.apply(a,b).- Returns:
- the binary function function(b,a).
-
-
DMelt 3.0 © DataMelt by jWork.ORG