diff options
Diffstat (limited to 'src/main/java/org/apache/commons/math3/analysis/function')
46 files changed, 3167 insertions, 0 deletions
diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Abs.java b/src/main/java/org/apache/commons/math3/analysis/function/Abs.java new file mode 100644 index 0000000..9db01fd --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Abs.java @@ -0,0 +1,33 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.UnivariateFunction; +import org.apache.commons.math3.util.FastMath; + +/** + * Absolute value function. + * + * @since 3.0 + */ +public class Abs implements UnivariateFunction { + /** {@inheritDoc} */ + public double value(double x) { + return FastMath.abs(x); + } +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Acos.java b/src/main/java/org/apache/commons/math3/analysis/function/Acos.java new file mode 100644 index 0000000..a9df4b7 --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Acos.java @@ -0,0 +1,53 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.FunctionUtils; +import org.apache.commons.math3.analysis.UnivariateFunction; +import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; +import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; +import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; +import org.apache.commons.math3.util.FastMath; + +/** + * Arc-cosine function. + * + * @since 3.0 + */ +public class Acos implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { + /** {@inheritDoc} */ + public double value(double x) { + return FastMath.acos(x); + } + + /** {@inheritDoc} + * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} + */ + @Deprecated + public UnivariateFunction derivative() { + return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); + } + + /** {@inheritDoc} + * @since 3.1 + */ + public DerivativeStructure value(final DerivativeStructure t) { + return t.acos(); + } + +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Acosh.java b/src/main/java/org/apache/commons/math3/analysis/function/Acosh.java new file mode 100644 index 0000000..58fb19f --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Acosh.java @@ -0,0 +1,53 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.FunctionUtils; +import org.apache.commons.math3.analysis.UnivariateFunction; +import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; +import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; +import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; +import org.apache.commons.math3.util.FastMath; + +/** + * Hyperbolic arc-cosine function. + * + * @since 3.0 + */ +public class Acosh implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { + /** {@inheritDoc} */ + public double value(double x) { + return FastMath.acosh(x); + } + + /** {@inheritDoc} + * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} + */ + @Deprecated + public UnivariateFunction derivative() { + return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); + } + + /** {@inheritDoc} + * @since 3.1 + */ + public DerivativeStructure value(final DerivativeStructure t) { + return t.acosh(); + } + +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Add.java b/src/main/java/org/apache/commons/math3/analysis/function/Add.java new file mode 100644 index 0000000..366a303 --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Add.java @@ -0,0 +1,32 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.BivariateFunction; + +/** + * Add the two operands. + * + * @since 3.0 + */ +public class Add implements BivariateFunction { + /** {@inheritDoc} */ + public double value(double x, double y) { + return x + y; + } +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Asin.java b/src/main/java/org/apache/commons/math3/analysis/function/Asin.java new file mode 100644 index 0000000..8fa9bdf --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Asin.java @@ -0,0 +1,53 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.FunctionUtils; +import org.apache.commons.math3.analysis.UnivariateFunction; +import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; +import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; +import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; +import org.apache.commons.math3.util.FastMath; + +/** + * Arc-sine function. + * + * @since 3.0 + */ +public class Asin implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { + /** {@inheritDoc} */ + public double value(double x) { + return FastMath.asin(x); + } + + /** {@inheritDoc} + * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} + */ + @Deprecated + public UnivariateFunction derivative() { + return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); + } + + /** {@inheritDoc} + * @since 3.1 + */ + public DerivativeStructure value(final DerivativeStructure t) { + return t.asin(); + } + +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Asinh.java b/src/main/java/org/apache/commons/math3/analysis/function/Asinh.java new file mode 100644 index 0000000..b5b9fd2 --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Asinh.java @@ -0,0 +1,53 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.FunctionUtils; +import org.apache.commons.math3.analysis.UnivariateFunction; +import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; +import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; +import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; +import org.apache.commons.math3.util.FastMath; + +/** + * Hyperbolic arc-sine function. + * + * @since 3.0 + */ +public class Asinh implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { + /** {@inheritDoc} */ + public double value(double x) { + return FastMath.asinh(x); + } + + /** {@inheritDoc} + * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} + */ + @Deprecated + public UnivariateFunction derivative() { + return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); + } + + /** {@inheritDoc} + * @since 3.1 + */ + public DerivativeStructure value(final DerivativeStructure t) { + return t.asinh(); + } + +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Atan.java b/src/main/java/org/apache/commons/math3/analysis/function/Atan.java new file mode 100644 index 0000000..36b1265 --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Atan.java @@ -0,0 +1,53 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.FunctionUtils; +import org.apache.commons.math3.analysis.UnivariateFunction; +import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; +import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; +import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; +import org.apache.commons.math3.util.FastMath; + +/** + * Arc-tangent function. + * + * @since 3.0 + */ +public class Atan implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { + /** {@inheritDoc} */ + public double value(double x) { + return FastMath.atan(x); + } + + /** {@inheritDoc} + * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} + */ + @Deprecated + public UnivariateFunction derivative() { + return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); + } + + /** {@inheritDoc} + * @since 3.1 + */ + public DerivativeStructure value(final DerivativeStructure t) { + return t.atan(); + } + +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Atan2.java b/src/main/java/org/apache/commons/math3/analysis/function/Atan2.java new file mode 100644 index 0000000..d5f385f --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Atan2.java @@ -0,0 +1,33 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.BivariateFunction; +import org.apache.commons.math3.util.FastMath; + +/** + * Arc-tangent function. + * + * @since 3.0 + */ +public class Atan2 implements BivariateFunction { + /** {@inheritDoc} */ + public double value(double x, double y) { + return FastMath.atan2(x, y); + } +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Atanh.java b/src/main/java/org/apache/commons/math3/analysis/function/Atanh.java new file mode 100644 index 0000000..5c04599 --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Atanh.java @@ -0,0 +1,53 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.FunctionUtils; +import org.apache.commons.math3.analysis.UnivariateFunction; +import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; +import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; +import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; +import org.apache.commons.math3.util.FastMath; + +/** + * Hyperbolic arc-tangent function. + * + * @since 3.0 + */ +public class Atanh implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { + /** {@inheritDoc} */ + public double value(double x) { + return FastMath.atanh(x); + } + + /** {@inheritDoc} + * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} + */ + @Deprecated + public UnivariateFunction derivative() { + return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); + } + + /** {@inheritDoc} + * @since 3.1 + */ + public DerivativeStructure value(final DerivativeStructure t) { + return t.atanh(); + } + +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Cbrt.java b/src/main/java/org/apache/commons/math3/analysis/function/Cbrt.java new file mode 100644 index 0000000..f26ef71 --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Cbrt.java @@ -0,0 +1,53 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.FunctionUtils; +import org.apache.commons.math3.analysis.UnivariateFunction; +import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; +import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; +import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; +import org.apache.commons.math3.util.FastMath; + +/** + * Cube root function. + * + * @since 3.0 + */ +public class Cbrt implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { + /** {@inheritDoc} */ + public double value(double x) { + return FastMath.cbrt(x); + } + + /** {@inheritDoc} + * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} + */ + @Deprecated + public UnivariateFunction derivative() { + return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); + } + + /** {@inheritDoc} + * @since 3.1 + */ + public DerivativeStructure value(final DerivativeStructure t) { + return t.cbrt(); + } + +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Ceil.java b/src/main/java/org/apache/commons/math3/analysis/function/Ceil.java new file mode 100644 index 0000000..2b9867e --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Ceil.java @@ -0,0 +1,33 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.UnivariateFunction; +import org.apache.commons.math3.util.FastMath; + +/** + * {@code ceil} function. + * + * @since 3.0 + */ +public class Ceil implements UnivariateFunction { + /** {@inheritDoc} */ + public double value(double x) { + return FastMath.ceil(x); + } +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Constant.java b/src/main/java/org/apache/commons/math3/analysis/function/Constant.java new file mode 100644 index 0000000..4027e59 --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Constant.java @@ -0,0 +1,60 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; +import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; +import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; + +/** + * Constant function. + * + * @since 3.0 + */ +public class Constant implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { + /** Constant. */ + private final double c; + + /** + * @param c Constant. + */ + public Constant(double c) { + this.c = c; + } + + /** {@inheritDoc} */ + public double value(double x) { + return c; + } + + /** {@inheritDoc} + * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} + */ + @Deprecated + public DifferentiableUnivariateFunction derivative() { + return new Constant(0); + } + + /** {@inheritDoc} + * @since 3.1 + */ + public DerivativeStructure value(final DerivativeStructure t) { + return new DerivativeStructure(t.getFreeParameters(), t.getOrder(), c); + } + +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Cos.java b/src/main/java/org/apache/commons/math3/analysis/function/Cos.java new file mode 100644 index 0000000..73a5e6e --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Cos.java @@ -0,0 +1,53 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.FunctionUtils; +import org.apache.commons.math3.analysis.UnivariateFunction; +import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; +import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; +import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; +import org.apache.commons.math3.util.FastMath; + +/** + * Cosine function. + * + * @since 3.0 + */ +public class Cos implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { + /** {@inheritDoc} */ + public double value(double x) { + return FastMath.cos(x); + } + + /** {@inheritDoc} + * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} + */ + @Deprecated + public UnivariateFunction derivative() { + return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); + } + + /** {@inheritDoc} + * @since 3.1 + */ + public DerivativeStructure value(final DerivativeStructure t) { + return t.cos(); + } + +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Cosh.java b/src/main/java/org/apache/commons/math3/analysis/function/Cosh.java new file mode 100644 index 0000000..185698b --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Cosh.java @@ -0,0 +1,51 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; +import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; +import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; +import org.apache.commons.math3.util.FastMath; + +/** + * Hyperbolic cosine function. + * + * @since 3.0 + */ +public class Cosh implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { + /** {@inheritDoc} */ + public double value(double x) { + return FastMath.cosh(x); + } + + /** {@inheritDoc} + * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} + */ + @Deprecated + public DifferentiableUnivariateFunction derivative() { + return new Sinh(); + } + + /** {@inheritDoc} + * @since 3.1 + */ + public DerivativeStructure value(final DerivativeStructure t) { + return t.cosh(); + } + +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Divide.java b/src/main/java/org/apache/commons/math3/analysis/function/Divide.java new file mode 100644 index 0000000..73413a2 --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Divide.java @@ -0,0 +1,32 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.BivariateFunction; + +/** + * Divide the first operand by the second. + * + * @since 3.0 + */ +public class Divide implements BivariateFunction { + /** {@inheritDoc} */ + public double value(double x, double y) { + return x / y; + } +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Exp.java b/src/main/java/org/apache/commons/math3/analysis/function/Exp.java new file mode 100644 index 0000000..f656712 --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Exp.java @@ -0,0 +1,53 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.FunctionUtils; +import org.apache.commons.math3.analysis.UnivariateFunction; +import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; +import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; +import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; +import org.apache.commons.math3.util.FastMath; + +/** + * Exponential function. + * + * @since 3.0 + */ +public class Exp implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { + /** {@inheritDoc} */ + public double value(double x) { + return FastMath.exp(x); + } + + /** {@inheritDoc} + * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} + */ + @Deprecated + public UnivariateFunction derivative() { + return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); + } + + /** {@inheritDoc} + * @since 3.1 + */ + public DerivativeStructure value(final DerivativeStructure t) { + return t.exp(); + } + +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Expm1.java b/src/main/java/org/apache/commons/math3/analysis/function/Expm1.java new file mode 100644 index 0000000..46b0b2f --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Expm1.java @@ -0,0 +1,53 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.FunctionUtils; +import org.apache.commons.math3.analysis.UnivariateFunction; +import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; +import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; +import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; +import org.apache.commons.math3.util.FastMath; + +/** + * <code>e<sup>x</sup>-1</code> function. + * + * @since 3.0 + */ +public class Expm1 implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { + /** {@inheritDoc} */ + public double value(double x) { + return FastMath.expm1(x); + } + + /** {@inheritDoc} + * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} + */ + @Deprecated + public UnivariateFunction derivative() { + return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); + } + + /** {@inheritDoc} + * @since 3.1 + */ + public DerivativeStructure value(final DerivativeStructure t) { + return t.expm1(); + } + +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Floor.java b/src/main/java/org/apache/commons/math3/analysis/function/Floor.java new file mode 100644 index 0000000..8d70627 --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Floor.java @@ -0,0 +1,33 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.UnivariateFunction; +import org.apache.commons.math3.util.FastMath; + +/** + * {@code floor} function. + * + * @since 3.0 + */ +public class Floor implements UnivariateFunction { + /** {@inheritDoc} */ + public double value(double x) { + return FastMath.floor(x); + } +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Gaussian.java b/src/main/java/org/apache/commons/math3/analysis/function/Gaussian.java new file mode 100644 index 0000000..8c64c8b --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Gaussian.java @@ -0,0 +1,259 @@ +/* + * 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.function; + +import java.util.Arrays; + +import org.apache.commons.math3.analysis.FunctionUtils; +import org.apache.commons.math3.analysis.UnivariateFunction; +import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; +import org.apache.commons.math3.analysis.ParametricUnivariateFunction; +import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; +import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; +import org.apache.commons.math3.exception.NotStrictlyPositiveException; +import org.apache.commons.math3.exception.NullArgumentException; +import org.apache.commons.math3.exception.DimensionMismatchException; +import org.apache.commons.math3.util.FastMath; +import org.apache.commons.math3.util.Precision; + +/** + * <a href="http://en.wikipedia.org/wiki/Gaussian_function"> + * Gaussian</a> function. + * + * @since 3.0 + */ +public class Gaussian implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { + /** Mean. */ + private final double mean; + /** Inverse of the standard deviation. */ + private final double is; + /** Inverse of twice the square of the standard deviation. */ + private final double i2s2; + /** Normalization factor. */ + private final double norm; + + /** + * Gaussian with given normalization factor, mean and standard deviation. + * + * @param norm Normalization factor. + * @param mean Mean. + * @param sigma Standard deviation. + * @throws NotStrictlyPositiveException if {@code sigma <= 0}. + */ + public Gaussian(double norm, + double mean, + double sigma) + throws NotStrictlyPositiveException { + if (sigma <= 0) { + throw new NotStrictlyPositiveException(sigma); + } + + this.norm = norm; + this.mean = mean; + this.is = 1 / sigma; + this.i2s2 = 0.5 * is * is; + } + + /** + * Normalized gaussian with given mean and standard deviation. + * + * @param mean Mean. + * @param sigma Standard deviation. + * @throws NotStrictlyPositiveException if {@code sigma <= 0}. + */ + public Gaussian(double mean, + double sigma) + throws NotStrictlyPositiveException { + this(1 / (sigma * FastMath.sqrt(2 * Math.PI)), mean, sigma); + } + + /** + * Normalized gaussian with zero mean and unit standard deviation. + */ + public Gaussian() { + this(0, 1); + } + + /** {@inheritDoc} */ + public double value(double x) { + return value(x - mean, norm, i2s2); + } + + /** {@inheritDoc} + * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} + */ + @Deprecated + public UnivariateFunction derivative() { + return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); + } + + /** + * Parametric function where the input array contains the parameters of + * the Gaussian, ordered as follows: + * <ul> + * <li>Norm</li> + * <li>Mean</li> + * <li>Standard deviation</li> + * </ul> + */ + public static class Parametric implements ParametricUnivariateFunction { + /** + * Computes the value of the Gaussian at {@code x}. + * + * @param x Value for which the function must be computed. + * @param param Values of norm, mean and standard deviation. + * @return the value of the function. + * @throws NullArgumentException if {@code param} is {@code null}. + * @throws DimensionMismatchException if the size of {@code param} is + * not 3. + * @throws NotStrictlyPositiveException if {@code param[2]} is negative. + */ + public double value(double x, double ... param) + throws NullArgumentException, + DimensionMismatchException, + NotStrictlyPositiveException { + validateParameters(param); + + final double diff = x - param[1]; + final double i2s2 = 1 / (2 * param[2] * param[2]); + return Gaussian.value(diff, param[0], i2s2); + } + + /** + * Computes the value of the gradient at {@code x}. + * The components of the gradient vector are the partial + * derivatives of the function with respect to each of the + * <em>parameters</em> (norm, mean and standard deviation). + * + * @param x Value at which the gradient must be computed. + * @param param Values of norm, mean and standard deviation. + * @return the gradient vector at {@code x}. + * @throws NullArgumentException if {@code param} is {@code null}. + * @throws DimensionMismatchException if the size of {@code param} is + * not 3. + * @throws NotStrictlyPositiveException if {@code param[2]} is negative. + */ + public double[] gradient(double x, double ... param) + throws NullArgumentException, + DimensionMismatchException, + NotStrictlyPositiveException { + validateParameters(param); + + final double norm = param[0]; + final double diff = x - param[1]; + final double sigma = param[2]; + final double i2s2 = 1 / (2 * sigma * sigma); + + final double n = Gaussian.value(diff, 1, i2s2); + final double m = norm * n * 2 * i2s2 * diff; + final double s = m * diff / sigma; + + return new double[] { n, m, s }; + } + + /** + * Validates parameters to ensure they are appropriate for the evaluation of + * the {@link #value(double,double[])} and {@link #gradient(double,double[])} + * methods. + * + * @param param Values of norm, mean and standard deviation. + * @throws NullArgumentException if {@code param} is {@code null}. + * @throws DimensionMismatchException if the size of {@code param} is + * not 3. + * @throws NotStrictlyPositiveException if {@code param[2]} is negative. + */ + private void validateParameters(double[] param) + throws NullArgumentException, + DimensionMismatchException, + NotStrictlyPositiveException { + if (param == null) { + throw new NullArgumentException(); + } + if (param.length != 3) { + throw new DimensionMismatchException(param.length, 3); + } + if (param[2] <= 0) { + throw new NotStrictlyPositiveException(param[2]); + } + } + } + + /** + * @param xMinusMean {@code x - mean}. + * @param norm Normalization factor. + * @param i2s2 Inverse of twice the square of the standard deviation. + * @return the value of the Gaussian at {@code x}. + */ + private static double value(double xMinusMean, + double norm, + double i2s2) { + return norm * FastMath.exp(-xMinusMean * xMinusMean * i2s2); + } + + /** {@inheritDoc} + * @since 3.1 + */ + public DerivativeStructure value(final DerivativeStructure t) + throws DimensionMismatchException { + + final double u = is * (t.getValue() - mean); + double[] f = new double[t.getOrder() + 1]; + + // the nth order derivative of the Gaussian has the form: + // dn(g(x)/dxn = (norm / s^n) P_n(u) exp(-u^2/2) with u=(x-m)/s + // where P_n(u) is a degree n polynomial with same parity as n + // P_0(u) = 1, P_1(u) = -u, P_2(u) = u^2 - 1, P_3(u) = -u^3 + 3 u... + // the general recurrence relation for P_n is: + // P_n(u) = P_(n-1)'(u) - u P_(n-1)(u) + // as per polynomial parity, we can store coefficients of both P_(n-1) and P_n in the same array + final double[] p = new double[f.length]; + p[0] = 1; + final double u2 = u * u; + double coeff = norm * FastMath.exp(-0.5 * u2); + if (coeff <= Precision.SAFE_MIN) { + Arrays.fill(f, 0.0); + } else { + f[0] = coeff; + for (int n = 1; n < f.length; ++n) { + + // update and evaluate polynomial P_n(x) + double v = 0; + p[n] = -p[n - 1]; + for (int k = n; k >= 0; k -= 2) { + v = v * u2 + p[k]; + if (k > 2) { + p[k - 2] = (k - 1) * p[k - 1] - p[k - 3]; + } else if (k == 2) { + p[0] = p[1]; + } + } + if ((n & 0x1) == 1) { + v *= u; + } + + coeff *= is; + f[n] = coeff * v; + + } + } + + return t.compose(f); + + } + +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/HarmonicOscillator.java b/src/main/java/org/apache/commons/math3/analysis/function/HarmonicOscillator.java new file mode 100644 index 0000000..0fbad9c --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/HarmonicOscillator.java @@ -0,0 +1,183 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; +import org.apache.commons.math3.analysis.FunctionUtils; +import org.apache.commons.math3.analysis.ParametricUnivariateFunction; +import org.apache.commons.math3.analysis.UnivariateFunction; +import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; +import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; +import org.apache.commons.math3.exception.DimensionMismatchException; +import org.apache.commons.math3.exception.NullArgumentException; +import org.apache.commons.math3.util.FastMath; + +/** + * <a href="http://en.wikipedia.org/wiki/Harmonic_oscillator"> + * simple harmonic oscillator</a> function. + * + * @since 3.0 + */ +public class HarmonicOscillator implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { + /** Amplitude. */ + private final double amplitude; + /** Angular frequency. */ + private final double omega; + /** Phase. */ + private final double phase; + + /** + * Harmonic oscillator function. + * + * @param amplitude Amplitude. + * @param omega Angular frequency. + * @param phase Phase. + */ + public HarmonicOscillator(double amplitude, + double omega, + double phase) { + this.amplitude = amplitude; + this.omega = omega; + this.phase = phase; + } + + /** {@inheritDoc} */ + public double value(double x) { + return value(omega * x + phase, amplitude); + } + + /** {@inheritDoc} + * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} + */ + @Deprecated + public UnivariateFunction derivative() { + return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); + } + + /** + * Parametric function where the input array contains the parameters of + * the harmonic oscillator function, ordered as follows: + * <ul> + * <li>Amplitude</li> + * <li>Angular frequency</li> + * <li>Phase</li> + * </ul> + */ + public static class Parametric implements ParametricUnivariateFunction { + /** + * Computes the value of the harmonic oscillator at {@code x}. + * + * @param x Value for which the function must be computed. + * @param param Values of norm, mean and standard deviation. + * @return the value of the function. + * @throws NullArgumentException if {@code param} is {@code null}. + * @throws DimensionMismatchException if the size of {@code param} is + * not 3. + */ + public double value(double x, double ... param) + throws NullArgumentException, + DimensionMismatchException { + validateParameters(param); + return HarmonicOscillator.value(x * param[1] + param[2], param[0]); + } + + /** + * Computes the value of the gradient at {@code x}. + * The components of the gradient vector are the partial + * derivatives of the function with respect to each of the + * <em>parameters</em> (amplitude, angular frequency and phase). + * + * @param x Value at which the gradient must be computed. + * @param param Values of amplitude, angular frequency and phase. + * @return the gradient vector at {@code x}. + * @throws NullArgumentException if {@code param} is {@code null}. + * @throws DimensionMismatchException if the size of {@code param} is + * not 3. + */ + public double[] gradient(double x, double ... param) + throws NullArgumentException, + DimensionMismatchException { + validateParameters(param); + + final double amplitude = param[0]; + final double omega = param[1]; + final double phase = param[2]; + + final double xTimesOmegaPlusPhase = omega * x + phase; + final double a = HarmonicOscillator.value(xTimesOmegaPlusPhase, 1); + final double p = -amplitude * FastMath.sin(xTimesOmegaPlusPhase); + final double w = p * x; + + return new double[] { a, w, p }; + } + + /** + * Validates parameters to ensure they are appropriate for the evaluation of + * the {@link #value(double,double[])} and {@link #gradient(double,double[])} + * methods. + * + * @param param Values of norm, mean and standard deviation. + * @throws NullArgumentException if {@code param} is {@code null}. + * @throws DimensionMismatchException if the size of {@code param} is + * not 3. + */ + private void validateParameters(double[] param) + throws NullArgumentException, + DimensionMismatchException { + if (param == null) { + throw new NullArgumentException(); + } + if (param.length != 3) { + throw new DimensionMismatchException(param.length, 3); + } + } + } + + /** + * @param xTimesOmegaPlusPhase {@code omega * x + phase}. + * @param amplitude Amplitude. + * @return the value of the harmonic oscillator function at {@code x}. + */ + private static double value(double xTimesOmegaPlusPhase, + double amplitude) { + return amplitude * FastMath.cos(xTimesOmegaPlusPhase); + } + + /** {@inheritDoc} + * @since 3.1 + */ + public DerivativeStructure value(final DerivativeStructure t) + throws DimensionMismatchException { + final double x = t.getValue(); + double[] f = new double[t.getOrder() + 1]; + + final double alpha = omega * x + phase; + f[0] = amplitude * FastMath.cos(alpha); + if (f.length > 1) { + f[1] = -amplitude * omega * FastMath.sin(alpha); + final double mo2 = - omega * omega; + for (int i = 2; i < f.length; ++i) { + f[i] = mo2 * f[i - 2]; + } + } + + return t.compose(f); + + } + +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Identity.java b/src/main/java/org/apache/commons/math3/analysis/function/Identity.java new file mode 100644 index 0000000..d21f7e0 --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Identity.java @@ -0,0 +1,50 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; +import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; +import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; + +/** + * Identity function. + * + * @since 3.0 + */ +public class Identity implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { + /** {@inheritDoc} */ + public double value(double x) { + return x; + } + + /** {@inheritDoc} + * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} + */ + @Deprecated + public DifferentiableUnivariateFunction derivative() { + return new Constant(1); + } + + /** {@inheritDoc} + * @since 3.1 + */ + public DerivativeStructure value(final DerivativeStructure t) { + return t; + } + +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Inverse.java b/src/main/java/org/apache/commons/math3/analysis/function/Inverse.java new file mode 100644 index 0000000..e38f689 --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Inverse.java @@ -0,0 +1,52 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.FunctionUtils; +import org.apache.commons.math3.analysis.UnivariateFunction; +import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; +import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; +import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; + +/** + * Inverse function. + * + * @since 3.0 + */ +public class Inverse implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { + /** {@inheritDoc} */ + public double value(double x) { + return 1 / x; + } + + /** {@inheritDoc} + * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} + */ + @Deprecated + public UnivariateFunction derivative() { + return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); + } + + /** {@inheritDoc} + * @since 3.1 + */ + public DerivativeStructure value(final DerivativeStructure t) { + return t.reciprocal(); + } + +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Log.java b/src/main/java/org/apache/commons/math3/analysis/function/Log.java new file mode 100644 index 0000000..a1e12dc --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Log.java @@ -0,0 +1,53 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.FunctionUtils; +import org.apache.commons.math3.analysis.UnivariateFunction; +import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; +import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; +import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; +import org.apache.commons.math3.util.FastMath; + +/** + * Natural logarithm function. + * + * @since 3.0 + */ +public class Log implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { + /** {@inheritDoc} */ + public double value(double x) { + return FastMath.log(x); + } + + /** {@inheritDoc} + * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} + */ + @Deprecated + public UnivariateFunction derivative() { + return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); + } + + /** {@inheritDoc} + * @since 3.1 + */ + public DerivativeStructure value(final DerivativeStructure t) { + return t.log(); + } + +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Log10.java b/src/main/java/org/apache/commons/math3/analysis/function/Log10.java new file mode 100644 index 0000000..66c03e1 --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Log10.java @@ -0,0 +1,54 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.FunctionUtils; +import org.apache.commons.math3.analysis.UnivariateFunction; +import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; +import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; +import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; +import org.apache.commons.math3.util.FastMath; + +/** + * Base 10 logarithm function. + * + * @since 3.0 + */ +public class Log10 implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { + + /** {@inheritDoc} */ + public double value(double x) { + return FastMath.log10(x); + } + + /** {@inheritDoc} + * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} + */ + @Deprecated + public UnivariateFunction derivative() { + return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); + } + + /** {@inheritDoc} + * @since 3.1 + */ + public DerivativeStructure value(final DerivativeStructure t) { + return t.log10(); + } + +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Log1p.java b/src/main/java/org/apache/commons/math3/analysis/function/Log1p.java new file mode 100644 index 0000000..4966318 --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Log1p.java @@ -0,0 +1,53 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.FunctionUtils; +import org.apache.commons.math3.analysis.UnivariateFunction; +import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; +import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; +import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; +import org.apache.commons.math3.util.FastMath; + +/** + * <code>log(1 + p)</code> function. + * + * @since 3.0 + */ +public class Log1p implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { + /** {@inheritDoc} */ + public double value(double x) { + return FastMath.log1p(x); + } + + /** {@inheritDoc} + * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} + */ + @Deprecated + public UnivariateFunction derivative() { + return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); + } + + /** {@inheritDoc} + * @since 3.1 + */ + public DerivativeStructure value(final DerivativeStructure t) { + return t.log1p(); + } + +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Logistic.java b/src/main/java/org/apache/commons/math3/analysis/function/Logistic.java new file mode 100644 index 0000000..c90203c --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Logistic.java @@ -0,0 +1,228 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.FunctionUtils; +import org.apache.commons.math3.analysis.UnivariateFunction; +import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; +import org.apache.commons.math3.analysis.ParametricUnivariateFunction; +import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; +import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; +import org.apache.commons.math3.exception.NotStrictlyPositiveException; +import org.apache.commons.math3.exception.NullArgumentException; +import org.apache.commons.math3.exception.DimensionMismatchException; +import org.apache.commons.math3.util.FastMath; + +/** + * <a href="http://en.wikipedia.org/wiki/Generalised_logistic_function"> + * Generalised logistic</a> function. + * + * @since 3.0 + */ +public class Logistic implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { + /** Lower asymptote. */ + private final double a; + /** Upper asymptote. */ + private final double k; + /** Growth rate. */ + private final double b; + /** Parameter that affects near which asymptote maximum growth occurs. */ + private final double oneOverN; + /** Parameter that affects the position of the curve along the ordinate axis. */ + private final double q; + /** Abscissa of maximum growth. */ + private final double m; + + /** + * @param k If {@code b > 0}, value of the function for x going towards +∞. + * If {@code b < 0}, value of the function for x going towards -∞. + * @param m Abscissa of maximum growth. + * @param b Growth rate. + * @param q Parameter that affects the position of the curve along the + * ordinate axis. + * @param a If {@code b > 0}, value of the function for x going towards -∞. + * If {@code b < 0}, value of the function for x going towards +∞. + * @param n Parameter that affects near which asymptote the maximum + * growth occurs. + * @throws NotStrictlyPositiveException if {@code n <= 0}. + */ + public Logistic(double k, + double m, + double b, + double q, + double a, + double n) + throws NotStrictlyPositiveException { + if (n <= 0) { + throw new NotStrictlyPositiveException(n); + } + + this.k = k; + this.m = m; + this.b = b; + this.q = q; + this.a = a; + oneOverN = 1 / n; + } + + /** {@inheritDoc} */ + public double value(double x) { + return value(m - x, k, b, q, a, oneOverN); + } + + /** {@inheritDoc} + * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} + */ + @Deprecated + public UnivariateFunction derivative() { + return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); + } + + /** + * Parametric function where the input array contains the parameters of + * the {@link Logistic#Logistic(double,double,double,double,double,double) + * logistic function}, ordered as follows: + * <ul> + * <li>k</li> + * <li>m</li> + * <li>b</li> + * <li>q</li> + * <li>a</li> + * <li>n</li> + * </ul> + */ + public static class Parametric implements ParametricUnivariateFunction { + /** + * Computes the value of the sigmoid at {@code x}. + * + * @param x Value for which the function must be computed. + * @param param Values for {@code k}, {@code m}, {@code b}, {@code q}, + * {@code a} and {@code n}. + * @return the value of the function. + * @throws NullArgumentException if {@code param} is {@code null}. + * @throws DimensionMismatchException if the size of {@code param} is + * not 6. + * @throws NotStrictlyPositiveException if {@code param[5] <= 0}. + */ + public double value(double x, double ... param) + throws NullArgumentException, + DimensionMismatchException, + NotStrictlyPositiveException { + validateParameters(param); + return Logistic.value(param[1] - x, param[0], + param[2], param[3], + param[4], 1 / param[5]); + } + + /** + * Computes the value of the gradient at {@code x}. + * The components of the gradient vector are the partial + * derivatives of the function with respect to each of the + * <em>parameters</em>. + * + * @param x Value at which the gradient must be computed. + * @param param Values for {@code k}, {@code m}, {@code b}, {@code q}, + * {@code a} and {@code n}. + * @return the gradient vector at {@code x}. + * @throws NullArgumentException if {@code param} is {@code null}. + * @throws DimensionMismatchException if the size of {@code param} is + * not 6. + * @throws NotStrictlyPositiveException if {@code param[5] <= 0}. + */ + public double[] gradient(double x, double ... param) + throws NullArgumentException, + DimensionMismatchException, + NotStrictlyPositiveException { + validateParameters(param); + + final double b = param[2]; + final double q = param[3]; + + final double mMinusX = param[1] - x; + final double oneOverN = 1 / param[5]; + final double exp = FastMath.exp(b * mMinusX); + final double qExp = q * exp; + final double qExp1 = qExp + 1; + final double factor1 = (param[0] - param[4]) * oneOverN / FastMath.pow(qExp1, oneOverN); + final double factor2 = -factor1 / qExp1; + + // Components of the gradient. + final double gk = Logistic.value(mMinusX, 1, b, q, 0, oneOverN); + final double gm = factor2 * b * qExp; + final double gb = factor2 * mMinusX * qExp; + final double gq = factor2 * exp; + final double ga = Logistic.value(mMinusX, 0, b, q, 1, oneOverN); + final double gn = factor1 * FastMath.log(qExp1) * oneOverN; + + return new double[] { gk, gm, gb, gq, ga, gn }; + } + + /** + * Validates parameters to ensure they are appropriate for the evaluation of + * the {@link #value(double,double[])} and {@link #gradient(double,double[])} + * methods. + * + * @param param Values for {@code k}, {@code m}, {@code b}, {@code q}, + * {@code a} and {@code n}. + * @throws NullArgumentException if {@code param} is {@code null}. + * @throws DimensionMismatchException if the size of {@code param} is + * not 6. + * @throws NotStrictlyPositiveException if {@code param[5] <= 0}. + */ + private void validateParameters(double[] param) + throws NullArgumentException, + DimensionMismatchException, + NotStrictlyPositiveException { + if (param == null) { + throw new NullArgumentException(); + } + if (param.length != 6) { + throw new DimensionMismatchException(param.length, 6); + } + if (param[5] <= 0) { + throw new NotStrictlyPositiveException(param[5]); + } + } + } + + /** + * @param mMinusX {@code m - x}. + * @param k {@code k}. + * @param b {@code b}. + * @param q {@code q}. + * @param a {@code a}. + * @param oneOverN {@code 1 / n}. + * @return the value of the function. + */ + private static double value(double mMinusX, + double k, + double b, + double q, + double a, + double oneOverN) { + return a + (k - a) / FastMath.pow(1 + q * FastMath.exp(b * mMinusX), oneOverN); + } + + /** {@inheritDoc} + * @since 3.1 + */ + public DerivativeStructure value(final DerivativeStructure t) { + return t.negate().add(m).multiply(b).exp().multiply(q).add(1).pow(oneOverN).reciprocal().multiply(k - a).add(a); + } + +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Logit.java b/src/main/java/org/apache/commons/math3/analysis/function/Logit.java new file mode 100644 index 0000000..39abe4d --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Logit.java @@ -0,0 +1,212 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; +import org.apache.commons.math3.analysis.FunctionUtils; +import org.apache.commons.math3.analysis.ParametricUnivariateFunction; +import org.apache.commons.math3.analysis.UnivariateFunction; +import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; +import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; +import org.apache.commons.math3.exception.DimensionMismatchException; +import org.apache.commons.math3.exception.NullArgumentException; +import org.apache.commons.math3.exception.OutOfRangeException; +import org.apache.commons.math3.util.FastMath; + +/** + * <a href="http://en.wikipedia.org/wiki/Logit"> + * Logit</a> function. + * It is the inverse of the {@link Sigmoid sigmoid} function. + * + * @since 3.0 + */ +public class Logit implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { + /** Lower bound. */ + private final double lo; + /** Higher bound. */ + private final double hi; + + /** + * Usual logit function, where the lower bound is 0 and the higher + * bound is 1. + */ + public Logit() { + this(0, 1); + } + + /** + * Logit function. + * + * @param lo Lower bound of the function domain. + * @param hi Higher bound of the function domain. + */ + public Logit(double lo, + double hi) { + this.lo = lo; + this.hi = hi; + } + + /** {@inheritDoc} */ + public double value(double x) + throws OutOfRangeException { + return value(x, lo, hi); + } + + /** {@inheritDoc} + * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} + */ + @Deprecated + public UnivariateFunction derivative() { + return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); + } + + /** + * Parametric function where the input array contains the parameters of + * the logit function, ordered as follows: + * <ul> + * <li>Lower bound</li> + * <li>Higher bound</li> + * </ul> + */ + public static class Parametric implements ParametricUnivariateFunction { + /** + * Computes the value of the logit at {@code x}. + * + * @param x Value for which the function must be computed. + * @param param Values of lower bound and higher bounds. + * @return the value of the function. + * @throws NullArgumentException if {@code param} is {@code null}. + * @throws DimensionMismatchException if the size of {@code param} is + * not 2. + */ + public double value(double x, double ... param) + throws NullArgumentException, + DimensionMismatchException { + validateParameters(param); + return Logit.value(x, param[0], param[1]); + } + + /** + * Computes the value of the gradient at {@code x}. + * The components of the gradient vector are the partial + * derivatives of the function with respect to each of the + * <em>parameters</em> (lower bound and higher bound). + * + * @param x Value at which the gradient must be computed. + * @param param Values for lower and higher bounds. + * @return the gradient vector at {@code x}. + * @throws NullArgumentException if {@code param} is {@code null}. + * @throws DimensionMismatchException if the size of {@code param} is + * not 2. + */ + public double[] gradient(double x, double ... param) + throws NullArgumentException, + DimensionMismatchException { + validateParameters(param); + + final double lo = param[0]; + final double hi = param[1]; + + return new double[] { 1 / (lo - x), 1 / (hi - x) }; + } + + /** + * Validates parameters to ensure they are appropriate for the evaluation of + * the {@link #value(double,double[])} and {@link #gradient(double,double[])} + * methods. + * + * @param param Values for lower and higher bounds. + * @throws NullArgumentException if {@code param} is {@code null}. + * @throws DimensionMismatchException if the size of {@code param} is + * not 2. + */ + private void validateParameters(double[] param) + throws NullArgumentException, + DimensionMismatchException { + if (param == null) { + throw new NullArgumentException(); + } + if (param.length != 2) { + throw new DimensionMismatchException(param.length, 2); + } + } + } + + /** + * @param x Value at which to compute the logit. + * @param lo Lower bound. + * @param hi Higher bound. + * @return the value of the logit function at {@code x}. + * @throws OutOfRangeException if {@code x < lo} or {@code x > hi}. + */ + private static double value(double x, + double lo, + double hi) + throws OutOfRangeException { + if (x < lo || x > hi) { + throw new OutOfRangeException(x, lo, hi); + } + return FastMath.log((x - lo) / (hi - x)); + } + + /** {@inheritDoc} + * @since 3.1 + * @exception OutOfRangeException if parameter is outside of function domain + */ + public DerivativeStructure value(final DerivativeStructure t) + throws OutOfRangeException { + final double x = t.getValue(); + if (x < lo || x > hi) { + throw new OutOfRangeException(x, lo, hi); + } + double[] f = new double[t.getOrder() + 1]; + + // function value + f[0] = FastMath.log((x - lo) / (hi - x)); + + if (Double.isInfinite(f[0])) { + + if (f.length > 1) { + f[1] = Double.POSITIVE_INFINITY; + } + // fill the array with infinities + // (for x close to lo the signs will flip between -inf and +inf, + // for x close to hi the signs will always be +inf) + // this is probably overkill, since the call to compose at the end + // of the method will transform most infinities into NaN ... + for (int i = 2; i < f.length; ++i) { + f[i] = f[i - 2]; + } + + } else { + + // function derivatives + final double invL = 1.0 / (x - lo); + double xL = invL; + final double invH = 1.0 / (hi - x); + double xH = invH; + for (int i = 1; i < f.length; ++i) { + f[i] = xL + xH; + xL *= -i * invL; + xH *= i * invH; + } + } + + return t.compose(f); + } +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Max.java b/src/main/java/org/apache/commons/math3/analysis/function/Max.java new file mode 100644 index 0000000..591ac55 --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Max.java @@ -0,0 +1,33 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.BivariateFunction; +import org.apache.commons.math3.util.FastMath; + +/** + * Maximum function. + * + * @since 3.0 + */ +public class Max implements BivariateFunction { + /** {@inheritDoc} */ + public double value(double x, double y) { + return FastMath.max(x, y); + } +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Min.java b/src/main/java/org/apache/commons/math3/analysis/function/Min.java new file mode 100644 index 0000000..a776b79 --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Min.java @@ -0,0 +1,33 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.BivariateFunction; +import org.apache.commons.math3.util.FastMath; + +/** + * Minimum function. + * + * @since 3.0 + */ +public class Min implements BivariateFunction { + /** {@inheritDoc} */ + public double value(double x, double y) { + return FastMath.min(x, y); + } +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Minus.java b/src/main/java/org/apache/commons/math3/analysis/function/Minus.java new file mode 100644 index 0000000..e532779 --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Minus.java @@ -0,0 +1,50 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; +import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; +import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; + +/** + * Minus function. + * + * @since 3.0 + */ +public class Minus implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { + /** {@inheritDoc} */ + public double value(double x) { + return -x; + } + + /** {@inheritDoc} + * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} + */ + @Deprecated + public DifferentiableUnivariateFunction derivative() { + return new Constant(-1); + } + + /** {@inheritDoc} + * @since 3.1 + */ + public DerivativeStructure value(final DerivativeStructure t) { + return t.negate(); + } + +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Multiply.java b/src/main/java/org/apache/commons/math3/analysis/function/Multiply.java new file mode 100644 index 0000000..b7e771b --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Multiply.java @@ -0,0 +1,32 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.BivariateFunction; + +/** + * Multiply the two operands. + * + * @since 3.0 + */ +public class Multiply implements BivariateFunction { + /** {@inheritDoc} */ + public double value(double x, double y) { + return x * y; + } +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Pow.java b/src/main/java/org/apache/commons/math3/analysis/function/Pow.java new file mode 100644 index 0000000..756dc42 --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Pow.java @@ -0,0 +1,33 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.BivariateFunction; +import org.apache.commons.math3.util.FastMath; + +/** + * Power function. + * + * @since 3.0 + */ +public class Pow implements BivariateFunction { + /** {@inheritDoc} */ + public double value(double x, double y) { + return FastMath.pow(x, y); + } +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Power.java b/src/main/java/org/apache/commons/math3/analysis/function/Power.java new file mode 100644 index 0000000..953bcab --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Power.java @@ -0,0 +1,63 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.FunctionUtils; +import org.apache.commons.math3.analysis.UnivariateFunction; +import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; +import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; +import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; +import org.apache.commons.math3.util.FastMath; + +/** + * Power function. + * + * @since 3.0 + */ +public class Power implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { + /** Power. */ + private final double p; + + /** + * @param p Power. + */ + public Power(double p) { + this.p = p; + } + + /** {@inheritDoc} */ + public double value(double x) { + return FastMath.pow(x, p); + } + + /** {@inheritDoc} + * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} + */ + @Deprecated + public UnivariateFunction derivative() { + return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); + } + + /** {@inheritDoc} + * @since 3.1 + */ + public DerivativeStructure value(final DerivativeStructure t) { + return t.pow(p); + } + +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Rint.java b/src/main/java/org/apache/commons/math3/analysis/function/Rint.java new file mode 100644 index 0000000..4edde58 --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Rint.java @@ -0,0 +1,33 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.UnivariateFunction; +import org.apache.commons.math3.util.FastMath; + +/** + * {@code rint} function. + * + * @since 3.0 + */ +public class Rint implements UnivariateFunction { + /** {@inheritDoc} */ + public double value(double x) { + return FastMath.rint(x); + } +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Sigmoid.java b/src/main/java/org/apache/commons/math3/analysis/function/Sigmoid.java new file mode 100644 index 0000000..54639f9 --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Sigmoid.java @@ -0,0 +1,218 @@ +/* + * 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.function; + +import java.util.Arrays; + +import org.apache.commons.math3.analysis.FunctionUtils; +import org.apache.commons.math3.analysis.UnivariateFunction; +import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; +import org.apache.commons.math3.analysis.ParametricUnivariateFunction; +import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; +import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; +import org.apache.commons.math3.exception.NullArgumentException; +import org.apache.commons.math3.exception.DimensionMismatchException; +import org.apache.commons.math3.util.FastMath; + +/** + * <a href="http://en.wikipedia.org/wiki/Sigmoid_function"> + * Sigmoid</a> function. + * It is the inverse of the {@link Logit logit} function. + * A more flexible version, the generalised logistic, is implemented + * by the {@link Logistic} class. + * + * @since 3.0 + */ +public class Sigmoid implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { + /** Lower asymptote. */ + private final double lo; + /** Higher asymptote. */ + private final double hi; + + /** + * Usual sigmoid function, where the lower asymptote is 0 and the higher + * asymptote is 1. + */ + public Sigmoid() { + this(0, 1); + } + + /** + * Sigmoid function. + * + * @param lo Lower asymptote. + * @param hi Higher asymptote. + */ + public Sigmoid(double lo, + double hi) { + this.lo = lo; + this.hi = hi; + } + + /** {@inheritDoc} + * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} + */ + @Deprecated + public UnivariateFunction derivative() { + return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); + } + + /** {@inheritDoc} */ + public double value(double x) { + return value(x, lo, hi); + } + + /** + * Parametric function where the input array contains the parameters of + * the {@link Sigmoid#Sigmoid(double,double) sigmoid function}, ordered + * as follows: + * <ul> + * <li>Lower asymptote</li> + * <li>Higher asymptote</li> + * </ul> + */ + public static class Parametric implements ParametricUnivariateFunction { + /** + * Computes the value of the sigmoid at {@code x}. + * + * @param x Value for which the function must be computed. + * @param param Values of lower asymptote and higher asymptote. + * @return the value of the function. + * @throws NullArgumentException if {@code param} is {@code null}. + * @throws DimensionMismatchException if the size of {@code param} is + * not 2. + */ + public double value(double x, double ... param) + throws NullArgumentException, + DimensionMismatchException { + validateParameters(param); + return Sigmoid.value(x, param[0], param[1]); + } + + /** + * Computes the value of the gradient at {@code x}. + * The components of the gradient vector are the partial + * derivatives of the function with respect to each of the + * <em>parameters</em> (lower asymptote and higher asymptote). + * + * @param x Value at which the gradient must be computed. + * @param param Values for lower asymptote and higher asymptote. + * @return the gradient vector at {@code x}. + * @throws NullArgumentException if {@code param} is {@code null}. + * @throws DimensionMismatchException if the size of {@code param} is + * not 2. + */ + public double[] gradient(double x, double ... param) + throws NullArgumentException, + DimensionMismatchException { + validateParameters(param); + + final double invExp1 = 1 / (1 + FastMath.exp(-x)); + + return new double[] { 1 - invExp1, invExp1 }; + } + + /** + * Validates parameters to ensure they are appropriate for the evaluation of + * the {@link #value(double,double[])} and {@link #gradient(double,double[])} + * methods. + * + * @param param Values for lower and higher asymptotes. + * @throws NullArgumentException if {@code param} is {@code null}. + * @throws DimensionMismatchException if the size of {@code param} is + * not 2. + */ + private void validateParameters(double[] param) + throws NullArgumentException, + DimensionMismatchException { + if (param == null) { + throw new NullArgumentException(); + } + if (param.length != 2) { + throw new DimensionMismatchException(param.length, 2); + } + } + } + + /** + * @param x Value at which to compute the sigmoid. + * @param lo Lower asymptote. + * @param hi Higher asymptote. + * @return the value of the sigmoid function at {@code x}. + */ + private static double value(double x, + double lo, + double hi) { + return lo + (hi - lo) / (1 + FastMath.exp(-x)); + } + + /** {@inheritDoc} + * @since 3.1 + */ + public DerivativeStructure value(final DerivativeStructure t) + throws DimensionMismatchException { + + double[] f = new double[t.getOrder() + 1]; + final double exp = FastMath.exp(-t.getValue()); + if (Double.isInfinite(exp)) { + + // special handling near lower boundary, to avoid NaN + f[0] = lo; + Arrays.fill(f, 1, f.length, 0.0); + + } else { + + // the nth order derivative of sigmoid has the form: + // dn(sigmoid(x)/dxn = P_n(exp(-x)) / (1+exp(-x))^(n+1) + // where P_n(t) is a degree n polynomial with normalized higher term + // P_0(t) = 1, P_1(t) = t, P_2(t) = t^2 - t, P_3(t) = t^3 - 4 t^2 + t... + // the general recurrence relation for P_n is: + // P_n(x) = n t P_(n-1)(t) - t (1 + t) P_(n-1)'(t) + final double[] p = new double[f.length]; + + final double inv = 1 / (1 + exp); + double coeff = hi - lo; + for (int n = 0; n < f.length; ++n) { + + // update and evaluate polynomial P_n(t) + double v = 0; + p[n] = 1; + for (int k = n; k >= 0; --k) { + v = v * exp + p[k]; + if (k > 1) { + p[k - 1] = (n - k + 2) * p[k - 2] - (k - 1) * p[k - 1]; + } else { + p[0] = 0; + } + } + + coeff *= inv; + f[n] = coeff * v; + + } + + // fix function value + f[0] += lo; + + } + + return t.compose(f); + + } + +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Signum.java b/src/main/java/org/apache/commons/math3/analysis/function/Signum.java new file mode 100644 index 0000000..ddde66e --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Signum.java @@ -0,0 +1,33 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.UnivariateFunction; +import org.apache.commons.math3.util.FastMath; + +/** + * {@code signum} function. + * + * @since 3.0 + */ +public class Signum implements UnivariateFunction { + /** {@inheritDoc} */ + public double value(double x) { + return FastMath.signum(x); + } +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Sin.java b/src/main/java/org/apache/commons/math3/analysis/function/Sin.java new file mode 100644 index 0000000..71c91e7 --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Sin.java @@ -0,0 +1,51 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; +import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; +import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; +import org.apache.commons.math3.util.FastMath; + +/** + * Sine function. + * + * @since 3.0 + */ +public class Sin implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { + /** {@inheritDoc} */ + public double value(double x) { + return FastMath.sin(x); + } + + /** {@inheritDoc} + * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} + */ + @Deprecated + public DifferentiableUnivariateFunction derivative() { + return new Cos(); + } + + /** {@inheritDoc} + * @since 3.1 + */ + public DerivativeStructure value(final DerivativeStructure t) { + return t.sin(); + } + +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Sinc.java b/src/main/java/org/apache/commons/math3/analysis/function/Sinc.java new file mode 100644 index 0000000..553cfff --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Sinc.java @@ -0,0 +1,205 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; +import org.apache.commons.math3.analysis.FunctionUtils; +import org.apache.commons.math3.analysis.UnivariateFunction; +import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; +import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; +import org.apache.commons.math3.exception.DimensionMismatchException; +import org.apache.commons.math3.util.FastMath; + +/** + * <a href="http://en.wikipedia.org/wiki/Sinc_function">Sinc</a> function, + * defined by + * <pre><code> + * sinc(x) = 1 if x = 0, + * sin(x) / x otherwise. + * </code></pre> + * + * @since 3.0 + */ +public class Sinc implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { + /** + * Value below which the computations are done using Taylor series. + * <p> + * The Taylor series for sinc even order derivatives are: + * <pre> + * d^(2n)sinc/dx^(2n) = Sum_(k>=0) (-1)^(n+k) / ((2k)!(2n+2k+1)) x^(2k) + * = (-1)^n [ 1/(2n+1) - x^2/(4n+6) + x^4/(48n+120) - x^6/(1440n+5040) + O(x^8) ] + * </pre> + * </p> + * <p> + * The Taylor series for sinc odd order derivatives are: + * <pre> + * d^(2n+1)sinc/dx^(2n+1) = Sum_(k>=0) (-1)^(n+k+1) / ((2k+1)!(2n+2k+3)) x^(2k+1) + * = (-1)^(n+1) [ x/(2n+3) - x^3/(12n+30) + x^5/(240n+840) - x^7/(10080n+45360) + O(x^9) ] + * </pre> + * </p> + * <p> + * So the ratio of the fourth term with respect to the first term + * is always smaller than x^6/720, for all derivative orders. + * This implies that neglecting this term and using only the first three terms induces + * a relative error bounded by x^6/720. The SHORTCUT value is chosen such that this + * relative error is below double precision accuracy when |x| <= SHORTCUT. + * </p> + */ + private static final double SHORTCUT = 6.0e-3; + /** For normalized sinc function. */ + private final boolean normalized; + + /** + * The sinc function, {@code sin(x) / x}. + */ + public Sinc() { + this(false); + } + + /** + * Instantiates the sinc function. + * + * @param normalized If {@code true}, the function is + * <code> sin(πx) / πx</code>, otherwise {@code sin(x) / x}. + */ + public Sinc(boolean normalized) { + this.normalized = normalized; + } + + /** {@inheritDoc} */ + public double value(final double x) { + final double scaledX = normalized ? FastMath.PI * x : x; + if (FastMath.abs(scaledX) <= SHORTCUT) { + // use Taylor series + final double scaledX2 = scaledX * scaledX; + return ((scaledX2 - 20) * scaledX2 + 120) / 120; + } else { + // use definition expression + return FastMath.sin(scaledX) / scaledX; + } + } + + /** {@inheritDoc} + * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} + */ + @Deprecated + public UnivariateFunction derivative() { + return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); + } + + /** {@inheritDoc} + * @since 3.1 + */ + public DerivativeStructure value(final DerivativeStructure t) + throws DimensionMismatchException { + + final double scaledX = (normalized ? FastMath.PI : 1) * t.getValue(); + final double scaledX2 = scaledX * scaledX; + + double[] f = new double[t.getOrder() + 1]; + + if (FastMath.abs(scaledX) <= SHORTCUT) { + + for (int i = 0; i < f.length; ++i) { + final int k = i / 2; + if ((i & 0x1) == 0) { + // even derivation order + f[i] = (((k & 0x1) == 0) ? 1 : -1) * + (1.0 / (i + 1) - scaledX2 * (1.0 / (2 * i + 6) - scaledX2 / (24 * i + 120))); + } else { + // odd derivation order + f[i] = (((k & 0x1) == 0) ? -scaledX : scaledX) * + (1.0 / (i + 2) - scaledX2 * (1.0 / (6 * i + 24) - scaledX2 / (120 * i + 720))); + } + } + + } else { + + final double inv = 1 / scaledX; + final double cos = FastMath.cos(scaledX); + final double sin = FastMath.sin(scaledX); + + f[0] = inv * sin; + + // the nth order derivative of sinc has the form: + // dn(sinc(x)/dxn = [S_n(x) sin(x) + C_n(x) cos(x)] / x^(n+1) + // where S_n(x) is an even polynomial with degree n-1 or n (depending on parity) + // and C_n(x) is an odd polynomial with degree n-1 or n (depending on parity) + // S_0(x) = 1, S_1(x) = -1, S_2(x) = -x^2 + 2, S_3(x) = 3x^2 - 6... + // C_0(x) = 0, C_1(x) = x, C_2(x) = -2x, C_3(x) = -x^3 + 6x... + // the general recurrence relations for S_n and C_n are: + // S_n(x) = x S_(n-1)'(x) - n S_(n-1)(x) - x C_(n-1)(x) + // C_n(x) = x C_(n-1)'(x) - n C_(n-1)(x) + x S_(n-1)(x) + // as per polynomials parity, we can store both S_n and C_n in the same array + final double[] sc = new double[f.length]; + sc[0] = 1; + + double coeff = inv; + for (int n = 1; n < f.length; ++n) { + + double s = 0; + double c = 0; + + // update and evaluate polynomials S_n(x) and C_n(x) + final int kStart; + if ((n & 0x1) == 0) { + // even derivation order, S_n is degree n and C_n is degree n-1 + sc[n] = 0; + kStart = n; + } else { + // odd derivation order, S_n is degree n-1 and C_n is degree n + sc[n] = sc[n - 1]; + c = sc[n]; + kStart = n - 1; + } + + // in this loop, k is always even + for (int k = kStart; k > 1; k -= 2) { + + // sine part + sc[k] = (k - n) * sc[k] - sc[k - 1]; + s = s * scaledX2 + sc[k]; + + // cosine part + sc[k - 1] = (k - 1 - n) * sc[k - 1] + sc[k -2]; + c = c * scaledX2 + sc[k - 1]; + + } + sc[0] *= -n; + s = s * scaledX2 + sc[0]; + + coeff *= inv; + f[n] = coeff * (s * sin + c * scaledX * cos); + + } + + } + + if (normalized) { + double scale = FastMath.PI; + for (int i = 1; i < f.length; ++i) { + f[i] *= scale; + scale *= FastMath.PI; + } + } + + return t.compose(f); + + } + +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Sinh.java b/src/main/java/org/apache/commons/math3/analysis/function/Sinh.java new file mode 100644 index 0000000..1eac044 --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Sinh.java @@ -0,0 +1,51 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; +import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; +import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; +import org.apache.commons.math3.util.FastMath; + +/** + * Hyperbolic sine function. + * + * @since 3.0 + */ +public class Sinh implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { + /** {@inheritDoc} */ + public double value(double x) { + return FastMath.sinh(x); + } + + /** {@inheritDoc} + * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} + */ + @Deprecated + public DifferentiableUnivariateFunction derivative() { + return new Cosh(); + } + + /** {@inheritDoc} + * @since 3.1 + */ + public DerivativeStructure value(final DerivativeStructure t) { + return t.sinh(); + } + +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Sqrt.java b/src/main/java/org/apache/commons/math3/analysis/function/Sqrt.java new file mode 100644 index 0000000..720d44d --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Sqrt.java @@ -0,0 +1,53 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.FunctionUtils; +import org.apache.commons.math3.analysis.UnivariateFunction; +import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; +import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; +import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; +import org.apache.commons.math3.util.FastMath; + +/** + * Square-root function. + * + * @since 3.0 + */ +public class Sqrt implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { + /** {@inheritDoc} */ + public double value(double x) { + return FastMath.sqrt(x); + } + + /** {@inheritDoc} + * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} + */ + @Deprecated + public UnivariateFunction derivative() { + return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); + } + + /** {@inheritDoc} + * @since 3.1 + */ + public DerivativeStructure value(final DerivativeStructure t) { + return t.sqrt(); + } + +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/StepFunction.java b/src/main/java/org/apache/commons/math3/analysis/function/StepFunction.java new file mode 100644 index 0000000..e3d16be --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/StepFunction.java @@ -0,0 +1,101 @@ +/* + * 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.function; + +import java.util.Arrays; + +import org.apache.commons.math3.analysis.UnivariateFunction; +import org.apache.commons.math3.exception.DimensionMismatchException; +import org.apache.commons.math3.exception.NoDataException; +import org.apache.commons.math3.exception.NonMonotonicSequenceException; +import org.apache.commons.math3.exception.NullArgumentException; +import org.apache.commons.math3.util.MathArrays; + +/** + * <a href="http://en.wikipedia.org/wiki/Step_function"> + * Step function</a>. + * + * @since 3.0 + */ +public class StepFunction implements UnivariateFunction { + /** Abscissae. */ + private final double[] abscissa; + /** Ordinates. */ + private final double[] ordinate; + + /** + * Builds a step function from a list of arguments and the corresponding + * values. Specifically, returns the function h(x) defined by <pre><code> + * h(x) = y[0] for all x < x[1] + * y[1] for x[1] ≤ x < x[2] + * ... + * y[y.length - 1] for x ≥ x[x.length - 1] + * </code></pre> + * The value of {@code x[0]} is ignored, but it must be strictly less than + * {@code x[1]}. + * + * @param x Domain values where the function changes value. + * @param y Values of the function. + * @throws NonMonotonicSequenceException + * if the {@code x} array is not sorted in strictly increasing order. + * @throws NullArgumentException if {@code x} or {@code y} are {@code null}. + * @throws NoDataException if {@code x} or {@code y} are zero-length. + * @throws DimensionMismatchException if {@code x} and {@code y} do not + * have the same length. + */ + public StepFunction(double[] x, + double[] y) + throws NullArgumentException, NoDataException, + DimensionMismatchException, NonMonotonicSequenceException { + if (x == null || + y == null) { + throw new NullArgumentException(); + } + if (x.length == 0 || + y.length == 0) { + throw new NoDataException(); + } + if (y.length != x.length) { + throw new DimensionMismatchException(y.length, x.length); + } + MathArrays.checkOrder(x); + + abscissa = MathArrays.copyOf(x); + ordinate = MathArrays.copyOf(y); + } + + /** {@inheritDoc} */ + public double value(double x) { + int index = Arrays.binarySearch(abscissa, x); + double fx = 0; + + if (index < -1) { + // "x" is between "abscissa[-index-2]" and "abscissa[-index-1]". + fx = ordinate[-index-2]; + } else if (index >= 0) { + // "x" is exactly "abscissa[index]". + fx = ordinate[index]; + } else { + // Otherwise, "x" is smaller than the first value in "abscissa" + // (hence the returned value should be "ordinate[0]"). + fx = ordinate[0]; + } + + return fx; + } +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Subtract.java b/src/main/java/org/apache/commons/math3/analysis/function/Subtract.java new file mode 100644 index 0000000..7b87dd6 --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Subtract.java @@ -0,0 +1,32 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.BivariateFunction; + +/** + * Subtract the second operand from the first. + * + * @since 3.0 + */ +public class Subtract implements BivariateFunction { + /** {@inheritDoc} */ + public double value(double x, double y) { + return x - y; + } +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Tan.java b/src/main/java/org/apache/commons/math3/analysis/function/Tan.java new file mode 100644 index 0000000..03304b4 --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Tan.java @@ -0,0 +1,53 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.FunctionUtils; +import org.apache.commons.math3.analysis.UnivariateFunction; +import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; +import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; +import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; +import org.apache.commons.math3.util.FastMath; + +/** + * Tangent function. + * + * @since 3.0 + */ +public class Tan implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { + /** {@inheritDoc} */ + public double value(double x) { + return FastMath.tan(x); + } + + /** {@inheritDoc} + * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} + */ + @Deprecated + public UnivariateFunction derivative() { + return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); + } + + /** {@inheritDoc} + * @since 3.1 + */ + public DerivativeStructure value(final DerivativeStructure t) { + return t.tan(); + } + +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Tanh.java b/src/main/java/org/apache/commons/math3/analysis/function/Tanh.java new file mode 100644 index 0000000..6c7ef0d --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Tanh.java @@ -0,0 +1,53 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.FunctionUtils; +import org.apache.commons.math3.analysis.UnivariateFunction; +import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; +import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; +import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; +import org.apache.commons.math3.util.FastMath; + +/** + * Hyperbolic tangent function. + * + * @since 3.0 + */ +public class Tanh implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { + /** {@inheritDoc} */ + public double value(double x) { + return FastMath.tanh(x); + } + + /** {@inheritDoc} + * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} + */ + @Deprecated + public UnivariateFunction derivative() { + return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); + } + + /** {@inheritDoc} + * @since 3.1 + */ + public DerivativeStructure value(final DerivativeStructure t) { + return t.tanh(); + } + +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Ulp.java b/src/main/java/org/apache/commons/math3/analysis/function/Ulp.java new file mode 100644 index 0000000..d075a73 --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/Ulp.java @@ -0,0 +1,33 @@ +/* + * 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.function; + +import org.apache.commons.math3.analysis.UnivariateFunction; +import org.apache.commons.math3.util.FastMath; + +/** + * {@code ulp} function. + * + * @since 3.0 + */ +public class Ulp implements UnivariateFunction { + /** {@inheritDoc} */ + public double value(double x) { + return FastMath.ulp(x); + } +} diff --git a/src/main/java/org/apache/commons/math3/analysis/function/package-info.java b/src/main/java/org/apache/commons/math3/analysis/function/package-info.java new file mode 100644 index 0000000..cb24544 --- /dev/null +++ b/src/main/java/org/apache/commons/math3/analysis/function/package-info.java @@ -0,0 +1,26 @@ +/* + * 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. + */ +/** + * + * <p> + * The {@code function} package contains function objects that wrap the + * methods contained in {@link java.lang.Math}, as well as common + * mathematical functions such as the gaussian and sinc functions. + * </p> + * + */ +package org.apache.commons.math3.analysis.function; |