# commons-issues mailing list archives

##### Site index · List index
Message view
Top
From "Jake Mannix (JIRA)" <j...@apache.org>
Subject [jira] Commented: (MATH-313) Functions could be more object-oriented without losing any power.
Date Sat, 31 Oct 2009 17:30:59 GMT
```
[ https://issues.apache.org/jira/browse/MATH-313?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12772262#action_12772262
]

Jake Mannix commented on MATH-313:
----------------------------------

Excellent, I can whip up a patch for that sometime this weekend - I think ComposableFunction
is closer to real english, that can work.

Some other things pure function objects can be used for, which I thought might be nice to
provide in the abstract implementation is the following: if you have a univariate function,
you can produce a MultiVariateRealFunction by applying the input function on each entry of
the multivariate case, and then combining the values together, either by default by addition,
or by providing a BinaryRealFunction to combine them:

{code}
public interface BinaryRealFunction {
// useful special case of MultivariateRealFunction for two variables
double value(double d1, double d2);
// a binary real function, when you fix one of the two arguments to be some value, becomes
a UnivariateRealFunction
ComposableFunction fix2ndArgument(double d2);
ComposableFunction fix1stArgument(double d1);
}
{code}

{code}
public interface ComposableFunction extends UnivariateRealFunction {

ComposableFunction preCompose(final UnivariateRealFunction f);

ComposableFunction postCompose(final UnivariateRealFunction f);

/*
* returns a function implemented as:  combiner.value(this.value(input), f.value(input));
*/
ComposableFunction combine(final UnivariateRealFunction f, final BinaryRealFunction combiner);

/****
* common algebraically sensible use cases for the above combine method:
****/

ComposableFunction plus(UnivariateRealFunction f);

ComposableFunction minus(UnivariateRealFunction f);

ComposableFunction times(UnivariateRealFunction f);

ComposableFunction scale(final double scaleFactor);

/*
* generates a function on double[] inputs as:
* initialize result = initialValue;
* then as you iterate: result = combiner.value(result, this.value(nextMultivariateEntry)
);
* return result;
*/
MultivariateRealFunction asCollector(final BinaryRealFunction combiner, final double initialValue);

/*
* use initialValue as 0 as a simple default:
*/
MultivariateRealFunction asCollector(final BinaryRealFunction combiner);

/*
* use Addition as combiner as a simple default:
*/
MultivariateRealFunction asCollector(final double initialValue);

/*
* initialValue = 0; combiner = Addition as the most default default
*/
MultivariateRealFunction asCollector();
}
{code}

Maybe I can just put them all in the same patch with abstract base class definitions which
have the full implementation of what I'm talking about, since it's now "non-invasive" as far
as the analysis package is concerned, and doesn't affect any other classes, and it could be
useful in the implementation of AbstractRealVector, among other places.

> Functions could be more object-oriented without losing any power.
> -----------------------------------------------------------------
>
>                 Key: MATH-313
>                 URL: https://issues.apache.org/jira/browse/MATH-313
>             Project: Commons Math
>          Issue Type: New Feature
>    Affects Versions: 2.0
>         Environment: all
>            Reporter: Jake Mannix
>             Fix For: 2.1
>
>
> UnivariateRealFunction, for example, is a map from R to R.  The set of such functions
has tons and tons of structure: in addition to being an algebra, equipped with +,-,*, and
scaling by constants, it maps the same space into itself, so it is composable, both pre and
post.
> {code}
>   UnivariateRealFunction plus(UnivariateRealFunction other);
>   UnivariateRealFunction minus(UnivariateRealFunction other);
>   UnivariateRealFunction times(UnivariateRealFunction other);
>   UnivariateRealFunction times(double scale);
>   UnivariateRealFunction preCompose(UnivariateRealFunction other);
>   UnivariateRealFunction postCompose(UnivariateRealFunction other);
> {code}
> to the interface, and then implement them in an AbstractUnivariateRealFunction base class.
No implementer would need to notice, other than switching to extend this class rather than
implement UnivariateRealFunction.
> Many people don't need or use this, but... it makes for some powerfully easy code:
> {code}UnivariateRealFunction gaussian = Exp.preCompose(Negate.preCompose(Pow2));{code}
> which is even nicer when done anonymously passing into a map/collect method (a la MATH-312).

--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

```
Mime
View raw message