Return-Path: Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: (qmail 2100 invoked from network); 22 Nov 2010 21:30:53 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 22 Nov 2010 21:30:53 -0000 Received: (qmail 86194 invoked by uid 500); 22 Nov 2010 21:31:25 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 86109 invoked by uid 500); 22 Nov 2010 21:31:24 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 86102 invoked by uid 99); 22 Nov 2010 21:31:24 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 22 Nov 2010 21:31:24 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 22 Nov 2010 21:31:23 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id B0F332388A39; Mon, 22 Nov 2010 21:29:51 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1037896 [2/2] - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/analysis/ main/java/org/apache/commons/math/analysis/function/ test/java/org/apache/commons/math/analysis/ Date: Mon, 22 Nov 2010 21:29:51 -0000 To: commits@commons.apache.org From: erans@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20101122212951.B0F332388A39@eris.apache.org> Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Sqrt.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Sqrt.java?rev=1037896&view=auto ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Sqrt.java (added) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Sqrt.java Mon Nov 22 21:29:49 2010 @@ -0,0 +1,34 @@ +/* + * 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.math.analysis.function; + +import org.apache.commons.math.analysis.UnivariateRealFunction; +import org.apache.commons.math.util.FastMath; + +/** + * Square-root function. + * + * @version $Revision$ $Date$ + * @since 3.0 + */ +public class Sqrt implements UnivariateRealFunction { + /** {@inheritDoc} */ + public double value(double x) { + return FastMath.sqrt(x); + } +} Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Sqrt.java ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Subtract.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Subtract.java?rev=1037896&view=auto ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Subtract.java (added) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Subtract.java Mon Nov 22 21:29:49 2010 @@ -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.math.analysis.function; + +import org.apache.commons.math.analysis.BivariateRealFunction; + +/** + * Subtract the second operand from the first. + * + * @version $Revision$ $Date$ + * @since 3.0 + */ +public class Subtract implements BivariateRealFunction { + /** {@inheritDoc} */ + public double value(double x, double y) { + return x - y; + } +} Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Subtract.java ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Tan.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Tan.java?rev=1037896&view=auto ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Tan.java (added) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Tan.java Mon Nov 22 21:29:49 2010 @@ -0,0 +1,34 @@ +/* + * 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.math.analysis.function; + +import org.apache.commons.math.analysis.UnivariateRealFunction; +import org.apache.commons.math.util.FastMath; + +/** + * Tangent function. + * + * @version $Revision$ $Date$ + * @since 3.0 + */ +public class Tan implements UnivariateRealFunction { + /** {@inheritDoc} */ + public double value(double x) { + return FastMath.tan(x); + } +} Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Tan.java ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Tanh.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Tanh.java?rev=1037896&view=auto ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Tanh.java (added) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Tanh.java Mon Nov 22 21:29:49 2010 @@ -0,0 +1,34 @@ +/* + * 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.math.analysis.function; + +import org.apache.commons.math.analysis.UnivariateRealFunction; +import org.apache.commons.math.util.FastMath; + +/** + * Hyperbolic tangent function. + * + * @version $Revision$ $Date$ + * @since 3.0 + */ +public class Tanh implements UnivariateRealFunction { + /** {@inheritDoc} */ + public double value(double x) { + return FastMath.tanh(x); + } +} Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/function/Tanh.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/ComposableFunctionTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/ComposableFunctionTest.java?rev=1037896&r1=1037895&r2=1037896&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/ComposableFunctionTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/ComposableFunctionTest.java Mon Nov 22 21:29:49 2010 @@ -21,6 +21,10 @@ import org.apache.commons.math.util.Fast import org.junit.Assert; import org.junit.Test; +/** + * @deprecated To be removed when the class {@link ComposableFunction} is removed. + */ +@Deprecated public class ComposableFunctionTest { @Test Added: commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/FunctionUtilsTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/FunctionUtilsTest.java?rev=1037896&view=auto ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/FunctionUtilsTest.java (added) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/FunctionUtilsTest.java Mon Nov 22 21:29:49 2010 @@ -0,0 +1,148 @@ +/* + * 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.math.analysis; + +import org.apache.commons.math.analysis.UnivariateRealFunction; +import org.apache.commons.math.analysis.function.Identity; +import org.apache.commons.math.analysis.function.Constant; +import org.apache.commons.math.analysis.function.Minus; +import org.apache.commons.math.analysis.function.Inverse; +import org.apache.commons.math.analysis.function.Power; +import org.apache.commons.math.analysis.function.Sin; +import org.apache.commons.math.analysis.function.Sinc; +import org.apache.commons.math.analysis.BivariateRealFunction; +import org.apache.commons.math.analysis.function.Add; +import org.apache.commons.math.analysis.function.Multiply; +import org.apache.commons.math.analysis.function.Divide; +import org.apache.commons.math.analysis.function.Min; +import org.apache.commons.math.analysis.function.Max; +import org.apache.commons.math.analysis.function.Pow; +import org.apache.commons.math.analysis.MultivariateRealFunction; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Test for {@link FunctionUtils}. + */ +public class FunctionUtilsTest { + private final double EPS = Math.ulp(1d); + + @Test + public void testCompose() { + UnivariateRealFunction id = new Identity(); + Assert.assertEquals(3, FunctionUtils.compose(id, id, id).value(3), EPS); + + UnivariateRealFunction c = new Constant(4); + Assert.assertEquals(4, FunctionUtils.compose(id, c).value(3), EPS); + Assert.assertEquals(4, FunctionUtils.compose(c, id).value(3), EPS); + + UnivariateRealFunction m = new Minus(); + Assert.assertEquals(-3, FunctionUtils.compose(m).value(3), EPS); + Assert.assertEquals(3, FunctionUtils.compose(m, m).value(3), EPS); + + UnivariateRealFunction inv = new Inverse(); + Assert.assertEquals(-0.25, FunctionUtils.compose(inv, m, c, id).value(3), EPS); + + UnivariateRealFunction pow = new Power(2); + Assert.assertEquals(81, FunctionUtils.compose(pow, pow).value(3), EPS); + } + + @Test + public void testAdd() { + UnivariateRealFunction id = new Identity(); + UnivariateRealFunction c = new Constant(4); + UnivariateRealFunction m = new Minus(); + UnivariateRealFunction inv = new Inverse(); + + Assert.assertEquals(4.5, FunctionUtils.add(inv, m, c, id).value(2), EPS); + Assert.assertEquals(4 + 2, FunctionUtils.add(c, id).value(2), EPS); + Assert.assertEquals(4 - 2, FunctionUtils.add(c, FunctionUtils.compose(m, id)).value(2), EPS); + } + + @Test + public void testMultiply() { + UnivariateRealFunction c = new Constant(4); + Assert.assertEquals(16, FunctionUtils.multiply(c, c).value(12345), EPS); + + UnivariateRealFunction inv = new Inverse(); + UnivariateRealFunction pow = new Power(2); + Assert.assertEquals(1, FunctionUtils.multiply(FunctionUtils.compose(inv, pow), pow).value(3.5), EPS); + } + + @Test + public void testCombine() { + BivariateRealFunction bi = new Add(); + UnivariateRealFunction id = new Identity(); + UnivariateRealFunction m = new Minus(); + UnivariateRealFunction c = FunctionUtils.combine(bi, id, m); + Assert.assertEquals(0, c.value(2.3456), EPS); + + bi = new Multiply(); + UnivariateRealFunction inv = new Inverse(); + c = FunctionUtils.combine(bi, id, inv); + Assert.assertEquals(1, c.value(2.3456), EPS); + } + + @Test + public void testCollector() { + BivariateRealFunction bi = new Add(); + MultivariateRealFunction coll = FunctionUtils.collector(bi, 0); + Assert.assertEquals(10, coll.value(new double[] {1, 2, 3, 4}), EPS); + + bi = new Multiply(); + coll = FunctionUtils.collector(bi, 1); + Assert.assertEquals(24, coll.value(new double[] {1, 2, 3, 4}), EPS); + + bi = new Max(); + coll = FunctionUtils.collector(bi, Double.NEGATIVE_INFINITY); + Assert.assertEquals(10, coll.value(new double[] {1, -2, 7.5, 10, -24, 9.99}), 0); + + bi = new Min(); + coll = FunctionUtils.collector(bi, Double.POSITIVE_INFINITY); + Assert.assertEquals(-24, coll.value(new double[] {1, -2, 7.5, 10, -24, 9.99}), 0); + } + + @Test + public void testSinc() { + BivariateRealFunction div = new Divide(); + UnivariateRealFunction sin = new Sin(); + UnivariateRealFunction id = new Identity(); + UnivariateRealFunction sinc1 = FunctionUtils.combine(div, sin, id); + UnivariateRealFunction sinc2 = new Sinc(); + + for (int i = 0; i < 10; i++) { + double x = Math.random(); + Assert.assertEquals(sinc1.value(x), sinc2.value(x), EPS); + } + } + + @Test + public void testFixingArguments() { + UnivariateRealFunction scaler = FunctionUtils.fix1stArgument(new Multiply(), 10); + Assert.assertEquals(1.23456, scaler.value(0.123456), EPS); + + UnivariateRealFunction pow1 = new Power(2); + UnivariateRealFunction pow2 = FunctionUtils.fix2ndArgument(new Pow(), 2); + + for (int i = 0; i < 10; i++) { + double x = Math.random() * 10; + Assert.assertEquals(pow1.value(x), pow2.value(x), 0); + } + } +} Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/FunctionUtilsTest.java ------------------------------------------------------------------------------ svn:eol-style = native