cern.jet.math
Class Functions

java.lang.Object
  |
  +--cern.jet.math.Functions

public class Functions
extends java.lang.Object

Function objects to be passed to generic methods. Contains the functions of Math and Sfun 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:

More general, any binary function can be made an unary functions by fixing either the first or the second argument. See methods 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):

Assume further we have a binary function f. The binary function g(f(a,b)) can be generated via chain(DoubleFunction,DoubleDoubleFunction): The binary function f(g(a),h(b)) can be generated via chain(DoubleDoubleFunction,DoubleFunction,DoubleFunction): Arbitrarily complex functions can be composed from these building blocks. For example sin(a) + cos2(b) can be specified as follows: or, of course, as
 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));
 

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
static cern.colt.function.DoubleFunction abs
          Function that returns Math.abs(a).
static cern.colt.function.DoubleFunction acos
          Function that returns Math.acos(a).
static cern.colt.function.DoubleFunction acosh
          Function that returns com.imsl.math.Sfun.acosh(a).
static cern.colt.function.DoubleFunction asin
          Function that returns Math.asin(a).
static cern.colt.function.DoubleFunction asinh
          Function that returns com.imsl.math.Sfun.asinh(a).
static cern.colt.function.DoubleFunction atan
          Function that returns Math.atan(a).
static cern.colt.function.DoubleDoubleFunction atan2
          Function that returns Math.atan2(a,b).
static cern.colt.function.DoubleFunction atanh
          Function that returns com.imsl.math.Sfun.atanh(a).
static cern.colt.function.DoubleFunction ceil
          Function that returns Math.ceil(a).
static cern.colt.function.DoubleDoubleFunction compare
          Function that returns a < b ? -1 : a > b ? 1 : 0.
static cern.colt.function.DoubleFunction cos
          Function that returns Math.cos(a).
static cern.colt.function.DoubleFunction cosh
          Function that returns com.imsl.math.Sfun.cosh(a).
static cern.colt.function.DoubleFunction cot
          Function that returns com.imsl.math.Sfun.cot(a).
static cern.colt.function.DoubleDoubleFunction div
          Function that returns a / b.
static cern.colt.function.DoubleDoubleFunction equals
          Function that returns a == b ? 1 : 0.
static cern.colt.function.DoubleFunction erf
          Function that returns com.imsl.math.Sfun.erf(a).
static cern.colt.function.DoubleFunction erfc
          Function that returns com.imsl.math.Sfun.erfc(a).
static cern.colt.function.DoubleFunction exp
          Function that returns Math.exp(a).
static cern.colt.function.DoubleFunction floor
          Function that returns Math.floor(a).
static Functions functions
          Little trick to allow for "aliasing", that is, renaming this class.
static cern.colt.function.DoubleFunction gamma
          Function that returns com.imsl.math.Sfun.gamma(a).
static cern.colt.function.DoubleDoubleFunction greater
          Function that returns a > b ? 1 : 0.
static cern.colt.function.DoubleFunction identity
          Function that returns its argument.
static cern.colt.function.DoubleDoubleFunction IEEEremainder
          Function that returns Math.IEEEremainder(a,b).
static cern.colt.function.DoubleFunction inv
          Function that returns 1.0 / a.
static cern.colt.function.DoubleDoubleProcedure isEqual
          Function that returns a == b.
static cern.colt.function.DoubleDoubleProcedure isGreater
          Function that returns a > b.
static cern.colt.function.DoubleDoubleProcedure isLess
          Function that returns a < b.
static cern.colt.function.DoubleDoubleFunction less
          Function that returns a < b ? 1 : 0.
static cern.colt.function.DoubleDoubleFunction lg
          Function that returns Math.log(a) / Math.log(b).
static cern.colt.function.DoubleFunction log
          Function that returns Math.log(a).
static cern.colt.function.DoubleFunction log10
          Function that returns com.imsl.math.Sfun.log10(a).
static cern.colt.function.DoubleFunction log2
          Function that returns Math.log(a) / Math.log(2).
static cern.colt.function.DoubleDoubleFunction logBeta
          Function that returns com.imsl.math.Sfun.logBeta(a,b).
static cern.colt.function.DoubleFunction logGamma
          Function that returns com.imsl.math.Sfun.logGamma(a).
static cern.colt.function.DoubleDoubleFunction max
          Function that returns Math.max(a,b).
static cern.colt.function.DoubleDoubleFunction min
          Function that returns Math.min(a,b).
static cern.colt.function.DoubleDoubleFunction minus
          Function that returns a - b.
static cern.colt.function.DoubleDoubleFunction mod
          Function that returns a % b.
static cern.colt.function.DoubleDoubleFunction mult
          Function that returns a * b.
static cern.colt.function.DoubleFunction neg
          Function that returns -a.
static cern.colt.function.DoubleDoubleFunction plus
          Function that returns a + b.
static cern.colt.function.DoubleDoubleFunction plusAbs
          Function that returns Math.abs(a) + Math.abs(b).
static cern.colt.function.DoubleDoubleFunction pow
          Function that returns Math.pow(a,b).
static cern.colt.function.DoubleFunction rint
          Function that returns Math.rint(a).
static cern.colt.function.DoubleFunction sign
          Function that returns a < 0 ? -1 : a > 0 ? 1 : 0.
static cern.colt.function.DoubleFunction sin
          Function that returns Math.sin(a).
static cern.colt.function.DoubleFunction sinh
          Function that returns com.imsl.math.Sfun.sinh(a).
static cern.colt.function.DoubleFunction sqrt
          Function that returns Math.sqrt(a).
static cern.colt.function.DoubleFunction square
          Function that returns a * a.
static cern.colt.function.DoubleFunction tan
          Function that returns Math.tan(a).
static cern.colt.function.DoubleFunction tanh
          Function that returns com.imsl.math.Sfun.tanh(a).
 
Constructor Summary
protected Functions()
          Makes this class non instantiable, but still let's others inherit from it.
 
Method Summary
static cern.colt.function.DoubleFunction between(double from, double to)
          Constructs a function that returns (from<=a && a<=to) ? 1 : 0.
static cern.colt.function.DoubleFunction bindArg1(cern.colt.function.DoubleDoubleFunction function, double c)
          Constructs a unary function from a binary function with the first operand (argument) fixed to the given constant c.
static cern.colt.function.DoubleFunction bindArg2(cern.colt.function.DoubleDoubleFunction function, double c)
          Constructs a unary function from a binary function with the second operand (argument) fixed to the given constant c.
static cern.colt.function.DoubleDoubleFunction chain(cern.colt.function.DoubleDoubleFunction f, cern.colt.function.DoubleFunction g, cern.colt.function.DoubleFunction h)
          Constructs the function f( g(a), h(b) ).
static cern.colt.function.DoubleDoubleFunction chain(cern.colt.function.DoubleFunction g, cern.colt.function.DoubleDoubleFunction h)
          Constructs the function g( h(a,b) ).
static cern.colt.function.DoubleFunction chain(cern.colt.function.DoubleFunction g, cern.colt.function.DoubleFunction h)
          Constructs the function g( h(a) ).
static cern.colt.function.DoubleFunction compare(double b)
          Constructs a function that returns a < b ? -1 : a > b ? 1 : 0.
static cern.colt.function.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 cern.colt.function.DoubleFunction div(double b)
          Constructs a function that returns a / b.
static cern.colt.function.DoubleFunction equals(double b)
          Constructs a function that returns a == b ? 1 : 0.
static cern.colt.function.DoubleFunction greater(double b)
          Constructs a function that returns a > b ? 1 : 0.
static cern.colt.function.DoubleFunction IEEEremainder(double b)
          Constructs a function that returns Math.IEEEremainder(a,b).
static cern.colt.function.DoubleProcedure isBetween(double from, double to)
          Constructs a function that returns from<=a && a<=to.
static cern.colt.function.DoubleProcedure isEqual(double b)
          Constructs a function that returns a == b.
static cern.colt.function.DoubleProcedure isGreater(double b)
          Constructs a function that returns a > b.
static cern.colt.function.DoubleProcedure isLess(double b)
          Constructs a function that returns a < b.
static cern.colt.function.DoubleFunction less(double b)
          Constructs a function that returns a < b ? 1 : 0.
static cern.colt.function.DoubleFunction lg(double b)
          Constructs a function that returns Math.log(a) / Math.log(b).
protected static void main(java.lang.String[] args)
          Tests various methods of this class.
static cern.colt.function.DoubleFunction max(double b)
          Constructs a function that returns Math.max(a,b).
static cern.colt.function.DoubleFunction min(double b)
          Constructs a function that returns Math.min(a,b).
static cern.colt.function.DoubleFunction minus(double b)
          Constructs a function that returns a - b.
static cern.colt.function.DoubleDoubleFunction minusMult(double constant)
          Constructs a function that returns a - b*constant.
static cern.colt.function.DoubleFunction mod(double b)
          Constructs a function that returns a % b.
static cern.colt.function.DoubleFunction mult(double b)
          Constructs a function that returns a * b.
static cern.colt.function.DoubleFunction plus(double b)
          Constructs a function that returns a + b.
static cern.colt.function.DoubleDoubleFunction plusMult(double constant)
          Constructs a function that returns a + b*constant.
static cern.colt.function.DoubleFunction pow(double b)
          Constructs a function that returns Math.pow(a,b).
static cern.colt.function.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 cern.colt.function.DoubleFunction round(double precision)
          Constructs a function that returns the number rounded to the given precision; Math.rint(a/precision)*precision.
static cern.colt.function.DoubleDoubleFunction swapArgs(cern.colt.function.DoubleDoubleFunction function)
          Constructs a function that returns function.apply(b,a), i.e.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

functions

public static final Functions functions
Little trick to allow for "aliasing", that is, renaming this class. Writing code like

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));


abs

public static final cern.colt.function.DoubleFunction abs
Function that returns Math.abs(a).


acos

public static final cern.colt.function.DoubleFunction acos
Function that returns Math.acos(a).


acosh

public static final cern.colt.function.DoubleFunction acosh
Function that returns com.imsl.math.Sfun.acosh(a).


asin

public static final cern.colt.function.DoubleFunction asin
Function that returns Math.asin(a).


asinh

public static final cern.colt.function.DoubleFunction asinh
Function that returns com.imsl.math.Sfun.asinh(a).


atan

public static final cern.colt.function.DoubleFunction atan
Function that returns Math.atan(a).


atanh

public static final cern.colt.function.DoubleFunction atanh
Function that returns com.imsl.math.Sfun.atanh(a).


ceil

public static final cern.colt.function.DoubleFunction ceil
Function that returns Math.ceil(a).


cos

public static final cern.colt.function.DoubleFunction cos
Function that returns Math.cos(a).


cosh

public static final cern.colt.function.DoubleFunction cosh
Function that returns com.imsl.math.Sfun.cosh(a).


cot

public static final cern.colt.function.DoubleFunction cot
Function that returns com.imsl.math.Sfun.cot(a).


erf

public static final cern.colt.function.DoubleFunction erf
Function that returns com.imsl.math.Sfun.erf(a).


erfc

public static final cern.colt.function.DoubleFunction erfc
Function that returns com.imsl.math.Sfun.erfc(a).


exp

public static final cern.colt.function.DoubleFunction exp
Function that returns Math.exp(a).


floor

public static final cern.colt.function.DoubleFunction floor
Function that returns Math.floor(a).


gamma

public static final cern.colt.function.DoubleFunction gamma
Function that returns com.imsl.math.Sfun.gamma(a).


identity

public static final cern.colt.function.DoubleFunction identity
Function that returns its argument.


inv

public static final cern.colt.function.DoubleFunction inv
Function that returns 1.0 / a.


log

public static final cern.colt.function.DoubleFunction log
Function that returns Math.log(a).


log10

public static final cern.colt.function.DoubleFunction log10
Function that returns com.imsl.math.Sfun.log10(a).


log2

public static final cern.colt.function.DoubleFunction log2
Function that returns Math.log(a) / Math.log(2).


logGamma

public static final cern.colt.function.DoubleFunction logGamma
Function that returns com.imsl.math.Sfun.logGamma(a).


neg

public static final cern.colt.function.DoubleFunction neg
Function that returns -a.


rint

public static final cern.colt.function.DoubleFunction rint
Function that returns Math.rint(a).


sign

public static final cern.colt.function.DoubleFunction sign
Function that returns a < 0 ? -1 : a > 0 ? 1 : 0.


sin

public static final cern.colt.function.DoubleFunction sin
Function that returns Math.sin(a).


sinh

public static final cern.colt.function.DoubleFunction sinh
Function that returns com.imsl.math.Sfun.sinh(a).


sqrt

public static final cern.colt.function.DoubleFunction sqrt
Function that returns Math.sqrt(a).


square

public static final cern.colt.function.DoubleFunction square
Function that returns a * a.


tan

public static final cern.colt.function.DoubleFunction tan
Function that returns Math.tan(a).


tanh

public static final cern.colt.function.DoubleFunction tanh
Function that returns com.imsl.math.Sfun.tanh(a).


atan2

public static final cern.colt.function.DoubleDoubleFunction atan2
Function that returns Math.atan2(a,b).


logBeta

public static final cern.colt.function.DoubleDoubleFunction logBeta
Function that returns com.imsl.math.Sfun.logBeta(a,b).


compare

public static final cern.colt.function.DoubleDoubleFunction compare
Function that returns a < b ? -1 : a > b ? 1 : 0.


div

public static final cern.colt.function.DoubleDoubleFunction div
Function that returns a / b.


equals

public static final cern.colt.function.DoubleDoubleFunction equals
Function that returns a == b ? 1 : 0.


greater

public static final cern.colt.function.DoubleDoubleFunction greater
Function that returns a > b ? 1 : 0.


IEEEremainder

public static final cern.colt.function.DoubleDoubleFunction IEEEremainder
Function that returns Math.IEEEremainder(a,b).


isEqual

public static final cern.colt.function.DoubleDoubleProcedure isEqual
Function that returns a == b.


isLess

public static final cern.colt.function.DoubleDoubleProcedure isLess
Function that returns a < b.


isGreater

public static final cern.colt.function.DoubleDoubleProcedure isGreater
Function that returns a > b.


less

public static final cern.colt.function.DoubleDoubleFunction less
Function that returns a < b ? 1 : 0.


lg

public static final cern.colt.function.DoubleDoubleFunction lg
Function that returns Math.log(a) / Math.log(b).


max

public static final cern.colt.function.DoubleDoubleFunction max
Function that returns Math.max(a,b).


min

public static final cern.colt.function.DoubleDoubleFunction min
Function that returns Math.min(a,b).


minus

public static final cern.colt.function.DoubleDoubleFunction minus
Function that returns a - b.


mod

public static final cern.colt.function.DoubleDoubleFunction mod
Function that returns a % b.


mult

public static final cern.colt.function.DoubleDoubleFunction mult
Function that returns a * b.


plus

public static final cern.colt.function.DoubleDoubleFunction plus
Function that returns a + b.


plusAbs

public static final cern.colt.function.DoubleDoubleFunction plusAbs
Function that returns Math.abs(a) + Math.abs(b).


pow

public static final cern.colt.function.DoubleDoubleFunction pow
Function that returns Math.pow(a,b).

Constructor Detail

Functions

protected Functions()
Makes this class non instantiable, but still let's others inherit from it.

Method Detail

between

public static cern.colt.function.DoubleFunction between(double from,
                                                        double to)
Constructs a function that returns (from<=a && a<=to) ? 1 : 0. a is a variable, from and to are fixed.


bindArg1

public static cern.colt.function.DoubleFunction bindArg1(cern.colt.function.DoubleDoubleFunction function,
                                                         double 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 cern.colt.function.DoubleFunction bindArg2(cern.colt.function.DoubleDoubleFunction function,
                                                         double 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 cern.colt.function.DoubleDoubleFunction chain(cern.colt.function.DoubleDoubleFunction f,
                                                            cern.colt.function.DoubleFunction g,
                                                            cern.colt.function.DoubleFunction 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 cern.colt.function.DoubleDoubleFunction chain(cern.colt.function.DoubleFunction g,
                                                            cern.colt.function.DoubleDoubleFunction 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 cern.colt.function.DoubleFunction chain(cern.colt.function.DoubleFunction g,
                                                      cern.colt.function.DoubleFunction 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 cern.colt.function.DoubleFunction compare(double b)
Constructs a function that returns a < b ? -1 : a > b ? 1 : 0. a is a variable, b is fixed.


constant

public static cern.colt.function.DoubleFunction constant(double 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 cern.colt.function.DoubleFunction div(double b)
Constructs a function that returns a / b. a is a variable, b is fixed.


equals

public static cern.colt.function.DoubleFunction equals(double b)
Constructs a function that returns a == b ? 1 : 0. a is a variable, b is fixed.


greater

public static cern.colt.function.DoubleFunction greater(double b)
Constructs a function that returns a > b ? 1 : 0. a is a variable, b is fixed.


IEEEremainder

public static cern.colt.function.DoubleFunction IEEEremainder(double b)
Constructs a function that returns Math.IEEEremainder(a,b). a is a variable, b is fixed.


isBetween

public static cern.colt.function.DoubleProcedure isBetween(double from,
                                                           double to)
Constructs a function that returns from<=a && a<=to. a is a variable, from and to are fixed.


isEqual

public static cern.colt.function.DoubleProcedure isEqual(double b)
Constructs a function that returns a == b. a is a variable, b is fixed.


isGreater

public static cern.colt.function.DoubleProcedure isGreater(double b)
Constructs a function that returns a > b. a is a variable, b is fixed.


isLess

public static cern.colt.function.DoubleProcedure isLess(double b)
Constructs a function that returns a < b. a is a variable, b is fixed.


less

public static cern.colt.function.DoubleFunction less(double b)
Constructs a function that returns a < b ? 1 : 0. a is a variable, b is fixed.


lg

public static cern.colt.function.DoubleFunction lg(double b)
Constructs a function that returns Math.log(a) / Math.log(b). a is a variable, b is fixed.


main

protected static void main(java.lang.String[] args)
Tests various methods of this class.


max

public static cern.colt.function.DoubleFunction max(double b)
Constructs a function that returns Math.max(a,b). a is a variable, b is fixed.


min

public static cern.colt.function.DoubleFunction min(double b)
Constructs a function that returns Math.min(a,b). a is a variable, b is fixed.


minus

public static cern.colt.function.DoubleFunction minus(double b)
Constructs a function that returns a - b. a is a variable, b is fixed.


minusMult

public static cern.colt.function.DoubleDoubleFunction minusMult(double constant)
Constructs a function that returns a - b*constant. a and b are variables, constant is fixed.


mod

public static cern.colt.function.DoubleFunction mod(double b)
Constructs a function that returns a % b. a is a variable, b is fixed.


mult

public static cern.colt.function.DoubleFunction mult(double b)
Constructs a function that returns a * b. a is a variable, b is fixed.


plus

public static cern.colt.function.DoubleFunction plus(double b)
Constructs a function that returns a + b. a is a variable, b is fixed.


plusMult

public static cern.colt.function.DoubleDoubleFunction plusMult(double constant)
Constructs a function that returns a + b*constant. a and b are variables, constant is fixed.


pow

public static cern.colt.function.DoubleFunction pow(double b)
Constructs a function that returns Math.pow(a,b). a is a variable, b is fixed.


random

public static cern.colt.function.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). 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.


round

public static cern.colt.function.DoubleFunction round(double 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 cern.colt.function.DoubleDoubleFunction swapArgs(cern.colt.function.DoubleDoubleFunction 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).