Java tutorial
/* * Copyright 2015 by Yields. * * Licensed 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 io.yields.math.concepts.operator; import org.apache.commons.math3.analysis.function.Asinh; import org.fest.assertions.Assertions; import org.fest.assertions.Delta; import org.junit.Test; import org.junit.runner.RunWith; import java.util.function.Function; import io.yields.math.framework.FunctionalContext; import io.yields.math.framework.Variable; import io.yields.math.framework.functional.DoubleIdentity; import io.yields.math.framework.junit.Spec; import io.yields.math.framework.junit.SpecRunner; import static io.yields.math.concepts.operator.Operators.length; import static io.yields.math.framework.assertions.Assertions.assertThat; import static io.yields.math.framework.property.Properties.identity; @RunWith(SpecRunner.class) public class CurveLengthTest { public static class LengthOfParabola extends DoubleIdentity { @Variable("]0,100[") public double endOfIntegration; private Function<Double, Double> function = x -> .5 * x * x; @Override protected Double originalComputation() { return length().between(0, endOfIntegration).apply(function); } @Override protected Double alternativeComputation() { return .5 * (Math.sqrt(1 + Math.pow(endOfIntegration, 2)) * endOfIntegration + new Asinh().value(endOfIntegration)); } } @Spec(name = "Check curve length along parabola", functional = LengthOfParabola.class) public void lengthOfParabola(FunctionalContext<Double> function) { assertThat(function).is(identity(.001)); } @Test public void testFailoverCalculation() { Function<Double, Double> curve = x -> Math.sin(x); Assertions.assertThat(length().between(0, 2 * Math.PI).apply(curve)) .isEqualTo(length().between(0, 2 * Math.PI).simpsonIntegration(curve), Delta.delta(1.e-8)); } }