JScience v4.3

## org.jscience.mathematics.function Class Function<X,Y>

```java.lang.Object
org.jscience.mathematics.function.Function<X,Y>
```
All Implemented Interfaces:
java.io.Serializable, javolution.lang.Realtime
Direct Known Subclasses:
DiscreteFunction, Polynomial, RationalFunction

`public abstract class Function<X,Y>extends java.lang.Objectimplements java.io.Serializable, javolution.lang.Realtime`

This abstract class represents a mapping between two sets such that there is a unique element in the second set assigned to each element in the first set.

Functions can be discrete or continuous and multivariate functions (functions with multiple variables) are also supported as illustrated below:

``````         // Defines local variables.
Variable.Local<Rational> varX = new Variable.Local<Rational>("x");
Variable.Local<Rational> varY = new Variable.Local<Rational>("y");

// f(x, y) =  x² + x·y + 1;
Polynomial<Rational> x = Polynomial.valueOf(Rational.ONE, varX);
Polynomial<Rational> y = Polynomial.valueOf(Rational.ONE, varY);
Polynomial<Rational> fx_y = x.pow(2).plus(x.times(y)).plus(Rational.ONE);
System.out.println("f(x,y) = " + fx_y);

// Evaluates f(1,0)
System.out.println("f(1,0) = " + fx_y.evaluate(Rational.ONE, Rational.ZERO));

// Calculates df(x,y)/dx
System.out.println("df(x,y)/dx = " + fx_y.differentiate(varX));

> f(x,y) = [1/1]x^2 + [1/1]xy + [1/1]
> f(1,0) = 2/1
> df(x,y)/dx = [2/1]x + [1/1]y
``````

Functions are often given by formula (e.g. ```f(x) = x²-x+1, f(x,y)= x·y```) but the general function instance might tabulate the values, solve an equation, etc.

Version:
3.1, April 1, 2006
Author:
Jean-Marie Dautelle
Wikipedia: Functions (mathematics), Serialized Form

Constructor Summary
`protected ` `Function()`
Default constructor.

Method Summary
``` <Z> Function<Z,Y> ``` `compose(Function<Z,X> that)`
Returns the composition of this function with the one specified.
` Function<X,Y>` `differentiate(Variable<X> v)`
Returns the first derivative of this function with respect to the specified variable.
` Function<X,Y>` `divide(Function<X,Y> that)`
Returns the quotient of this function with the one specified.
` boolean` `equals(java.lang.Object obj)`
Indicates if this function is equals to the specified object.
`abstract  Y` `evaluate()`
Evaluates this function using its `variables` current values.
` Y` `evaluate(X... args)`
Evaluates this function for the specified arguments values (convenience method).
` Y` `evaluate(X arg)`
Evaluates this function for the specified argument value (convenience method).
` Variable<X>` `getVariable(java.lang.String symbol)`
Retrieves the variable from this function having the specified symbol (convenience method).
`abstract  java.util.List<Variable<X>>` `getVariables()`
Returns a lexically ordered list of the variables (or arguments) for this function (empty list for constant functions).
` int` `hashCode()`
Returns the hash code for this function (consistent with `equals(Object)`.
` Function<X,Y>` `integrate(Variable<X> v)`
Returns an integral of this function with respect to the specified variable.
` Function<X,Y>` `minus(Function<X,Y> that)`
Returns the difference of this function with the one specified.
` Function<X,Y>` `plus(Function<X,Y> that)`
Returns the sum of this function with the one specified.
` Function<X,Y>` `pow(int n)`
Returns this function raised at the specified exponent.
` Function<X,Y>` `times(Function<X,Y> that)`
Returns the product of this function with the one specified.
` java.lang.String` `toString()`
Returns the text representation of this function as a `java.lang.String`.
`abstract  javolution.text.Text` `toText()`
Returns the textual representation of this real-time object (equivalent to `toString` except that the returned value can be allocated from the local context space).

Methods inherited from class java.lang.Object
`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

Constructor Detail

### Function

`protected Function()`
Default constructor.

Method Detail

### getVariables

`public abstract java.util.List<Variable<X>> getVariables()`
Returns a lexically ordered list of the variables (or arguments) for this function (empty list for constant functions).

Returns:
this function current unset variables (sorted).

### evaluate

`public abstract Y evaluate()`
Evaluates this function using its `variables` current values.

Returns:
the evaluation of this function.
Throws:
`FunctionException` - if any of this function's variable is not set.

### equals

`public boolean equals(java.lang.Object obj)`
Indicates if this function is equals to the specified object.

Overrides:
`equals` in class `java.lang.Object`
Parameters:
`obj` - the object to be compared with.
Returns:
`true` if this function and the specified argument represent the same function; `false` otherwise.

### hashCode

`public int hashCode()`
Returns the hash code for this function (consistent with `equals(Object)`.

Overrides:
`hashCode` in class `java.lang.Object`
Returns:
this function hash code.

### getVariable

`public final Variable<X> getVariable(java.lang.String symbol)`
Retrieves the variable from this function having the specified symbol (convenience method).

Returns:
the variable having the specified symbol or `null` if none.

### evaluate

`public final Y evaluate(X arg)`
Evaluates this function for the specified argument value (convenience method). The evaluation is performed in a `LocalContext` and can safely be called upon functions with ```global variables```.

Parameters:
`arg` - the single variable value used for the evaluation.
Returns:
the evaluation of this function.
Throws:
`FunctionException` - if `getVariables().size() != 1`

### evaluate

`public final Y evaluate(X... args)`
Evaluates this function for the specified arguments values (convenience method). The evaluation is performed in a `LocalContext` and can safely be called upon functions with ```global variables```.

Parameters:
`args` - the variables values used for the evaluation.
Returns:
the evaluation of this function.
Throws:
`java.lang.IllegalArgumentException` - if `args.length != getVariables().size())`

### compose

`public <Z> Function<Z,Y> compose(Function<Z,X> that)`
Returns the composition of this function with the one specified.

Parameters:
`that` - the function for which the return value is passed as argument to this function.
Returns:
the function `(this o that)`
Throws:
`FunctionException` - if this function is not monovariate.

### differentiate

`public Function<X,Y> differentiate(Variable<X> v)`
Returns the first derivative of this function with respect to the specified variable.

Parameters:
`v` - the variable for which the derivative is calculated.
Returns:
`d[this]/dv`
Throws:
`FunctionException` - if the derivative is undefined.
Derivative -- from MathWorld

### integrate

`public Function<X,Y> integrate(Variable<X> v)`
Returns an integral of this function with respect to the specified variable.

Parameters:
`v` - the variable for which the integral is calculated.
Returns:
`S[this·dv]`
Integral -- from MathWorld

### plus

`public Function<X,Y> plus(Function<X,Y> that)`
Returns the sum of this function with the one specified.

Parameters:
`that` - the function to be added.
Returns:
`this + that`.

### minus

`public Function<X,Y> minus(Function<X,Y> that)`
Returns the difference of this function with the one specified.

Parameters:
`that` - the function to be subtracted.
Returns:
`this - that`.

### times

`public Function<X,Y> times(Function<X,Y> that)`
Returns the product of this function with the one specified.

Parameters:
`that` - the function multiplier.
Returns:
`this · that`.

### divide

`public Function<X,Y> divide(Function<X,Y> that)`
Returns the quotient of this function with the one specified. Evaluation of this function may raise an exception if the function result is not a {

Parameters:
`that` - the function divisor.
Returns:
`this / that`.

### pow

`public Function<X,Y> pow(int n)`
Returns this function raised at the specified exponent.

Parameters:
`n` - the exponent.
Returns:
`thisn`
Throws:
`java.lang.IllegalArgumentException` - if `n <= 0`

### toText

`public abstract javolution.text.Text toText()`
Returns the textual representation of this real-time object (equivalent to `toString` except that the returned value can be allocated from the local context space).

Specified by:
`toText` in interface `javolution.lang.Realtime`
Returns:
this object's textual representation.

### toString

`public final java.lang.String toString()`
Returns the text representation of this function as a `java.lang.String`.

Overrides:
`toString` in class `java.lang.Object`
Returns:
`toText().toString()`

JScience v4.3