|
Java example source code file (FunctionUtils.java)
The FunctionUtils.java Java example source code/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math3.analysis;
import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
import org.apache.commons.math3.analysis.differentiation.MultivariateDifferentiableFunction;
import org.apache.commons.math3.analysis.differentiation.MultivariateDifferentiableVectorFunction;
import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction;
import org.apache.commons.math3.analysis.function.Identity;
import org.apache.commons.math3.exception.DimensionMismatchException;
import org.apache.commons.math3.exception.NotStrictlyPositiveException;
import org.apache.commons.math3.exception.NumberIsTooLargeException;
import org.apache.commons.math3.exception.util.LocalizedFormats;
/**
* Utilities for manipulating function objects.
*
* @since 3.0
*/
public class FunctionUtils {
/**
* Class only contains static methods.
*/
private FunctionUtils() {}
/**
* Composes functions.
* <p>
* The functions in the argument list are composed sequentially, in the
* given order. For example, compose(f1,f2,f3) acts like f1(f2(f3(x))).</p>
*
* @param f List of functions.
* @return the composite function.
*/
public static UnivariateFunction compose(final UnivariateFunction ... f) {
return new UnivariateFunction() {
/** {@inheritDoc} */
public double value(double x) {
double r = x;
for (int i = f.length - 1; i >= 0; i--) {
r = f[i].value(r);
}
return r;
}
};
}
/**
* Composes functions.
* <p>
* The functions in the argument list are composed sequentially, in the
* given order. For example, compose(f1,f2,f3) acts like f1(f2(f3(x))).</p>
*
* @param f List of functions.
* @return the composite function.
* @since 3.1
*/
public static UnivariateDifferentiableFunction compose(final UnivariateDifferentiableFunction ... f) {
return new UnivariateDifferentiableFunction() {
/** {@inheritDoc} */
public double value(final double t) {
double r = t;
for (int i = f.length - 1; i >= 0; i--) {
r = f[i].value(r);
}
return r;
}
/** {@inheritDoc} */
public DerivativeStructure value(final DerivativeStructure t) {
DerivativeStructure r = t;
for (int i = f.length - 1; i >= 0; i--) {
r = f[i].value(r);
}
return r;
}
};
}
/**
* Composes functions.
* <p>
* The functions in the argument list are composed sequentially, in the
* given order. For example, compose(f1,f2,f3) acts like f1(f2(f3(x))).</p>
*
* @param f List of functions.
* @return the composite function.
* @deprecated as of 3.1 replaced by {@link #compose(UnivariateDifferentiableFunction...)}
*/
@Deprecated
public static DifferentiableUnivariateFunction compose(final DifferentiableUnivariateFunction ... f) {
return new DifferentiableUnivariateFunction() {
/** {@inheritDoc} */
public double value(double x) {
double r = x;
for (int i = f.length - 1; i >= 0; i--) {
r = f[i].value(r);
}
return r;
}
/** {@inheritDoc} */
public UnivariateFunction derivative() {
return new UnivariateFunction() {
/** {@inheritDoc} */
public double value(double x) {
double p = 1;
double r = x;
for (int i = f.length - 1; i >= 0; i--) {
p *= f[i].derivative().value(r);
r = f[i].value(r);
}
return p;
}
};
}
};
}
/**
* Adds functions.
*
* @param f List of functions.
* @return a function that computes the sum of the functions.
*/
public static UnivariateFunction add(final UnivariateFunction ... f) {
return new UnivariateFunction() {
/** {@inheritDoc} */
public double value(double x) {
double r = f[0].value(x);
for (int i = 1; i < f.length; i++) {
r += f[i].value(x);
}
return r;
}
};
}
/**
* Adds functions.
*
* @param f List of functions.
* @return a function that computes the sum of the functions.
* @since 3.1
*/
public static UnivariateDifferentiableFunction add(final UnivariateDifferentiableFunction ... f) {
return new UnivariateDifferentiableFunction() {
/** {@inheritDoc} */
public double value(final double t) {
double r = f[0].value(t);
for (int i = 1; i < f.length; i++) {
r += f[i].value(t);
}
return r;
}
/** {@inheritDoc}
* @throws DimensionMismatchException if functions are not consistent with each other
*/
public DerivativeStructure value(final DerivativeStructure t)
throws DimensionMismatchException {
DerivativeStructure r = f[0].value(t);
for (int i = 1; i < f.length; i++) {
r = r.add(f[i].value(t));
}
return r;
}
};
}
/**
* Adds functions.
*
* @param f List of functions.
* @return a function that computes the sum of the functions.
* @deprecated as of 3.1 replaced by {@link #add(UnivariateDifferentiableFunction...)}
*/
@Deprecated
public static DifferentiableUnivariateFunction add(final DifferentiableUnivariateFunction ... f) {
return new DifferentiableUnivariateFunction() {
/** {@inheritDoc} */
public double value(double x) {
double r = f[0].value(x);
for (int i = 1; i < f.length; i++) {
r += f[i].value(x);
}
return r;
}
/** {@inheritDoc} */
public UnivariateFunction derivative() {
return new UnivariateFunction() {
/** {@inheritDoc} */
public double value(double x) {
double r = f[0].derivative().value(x);
for (int i = 1; i < f.length; i++) {
r += f[i].derivative().value(x);
}
return r;
}
};
}
};
}
/**
* Multiplies functions.
*
* @param f List of functions.
* @return a function that computes the product of the functions.
*/
public static UnivariateFunction multiply(final UnivariateFunction ... f) {
return new UnivariateFunction() {
/** {@inheritDoc} */
public double value(double x) {
double r = f[0].value(x);
for (int i = 1; i < f.length; i++) {
r *= f[i].value(x);
}
return r;
}
};
}
/**
* Multiplies functions.
*
* @param f List of functions.
* @return a function that computes the product of the functions.
* @since 3.1
*/
public static UnivariateDifferentiableFunction multiply(final UnivariateDifferentiableFunction ... f) {
return new UnivariateDifferentiableFunction() {
/** {@inheritDoc} */
public double value(final double t) {
double r = f[0].value(t);
for (int i = 1; i < f.length; i++) {
r *= f[i].value(t);
}
return r;
}
/** {@inheritDoc} */
public DerivativeStructure value(final DerivativeStructure t) {
DerivativeStructure r = f[0].value(t);
for (int i = 1; i < f.length; i++) {
r = r.multiply(f[i].value(t));
}
return r;
}
};
}
/**
* Multiplies functions.
*
* @param f List of functions.
* @return a function that computes the product of the functions.
* @deprecated as of 3.1 replaced by {@link #multiply(UnivariateDifferentiableFunction...)}
*/
@Deprecated
public static DifferentiableUnivariateFunction multiply(final DifferentiableUnivariateFunction ... f) {
return new DifferentiableUnivariateFunction() {
/** {@inheritDoc} */
public double value(double x) {
double r = f[0].value(x);
for (int i = 1; i < f.length; i++) {
r *= f[i].value(x);
}
return r;
}
/** {@inheritDoc} */
public UnivariateFunction derivative() {
return new UnivariateFunction() {
/** {@inheritDoc} */
public double value(double x) {
double sum = 0;
for (int i = 0; i < f.length; i++) {
double prod = f[i].derivative().value(x);
for (int j = 0; j < f.length; j++) {
if (i != j) {
prod *= f[j].value(x);
}
}
sum += prod;
}
return sum;
}
};
}
};
}
/**
* Returns the univariate function
* {@code h(x) = combiner(f(x), g(x)).}
*
* @param combiner Combiner function.
* @param f Function.
* @param g Function.
* @return the composite function.
*/
public static UnivariateFunction combine(final BivariateFunction combiner,
final UnivariateFunction f,
final UnivariateFunction g) {
return new UnivariateFunction() {
/** {@inheritDoc} */
public double value(double x) {
return combiner.value(f.value(x), g.value(x));
}
};
}
/**
* Returns a MultivariateFunction h(x[]) defined by <pre>
*
* @param combiner Combiner function.
* @param f Function.
* @param initialValue Initial value.
* @return a collector function.
*/
public static MultivariateFunction collector(final BivariateFunction combiner,
final UnivariateFunction f,
final double initialValue) {
return new MultivariateFunction() {
/** {@inheritDoc} */
public double value(double[] point) {
double result = combiner.value(initialValue, f.value(point[0]));
for (int i = 1; i < point.length; i++) {
result = combiner.value(result, f.value(point[i]));
}
return result;
}
};
}
/**
* Returns a MultivariateFunction h(x[]) defined by <pre> |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.