diff --git a/hipparchus-core/src/main/java/org/hipparchus/CalculusFieldElement.java b/hipparchus-core/src/main/java/org/hipparchus/CalculusFieldElement.java index a7f57eb88..368fbbfa4 100644 --- a/hipparchus-core/src/main/java/org/hipparchus/CalculusFieldElement.java +++ b/hipparchus-core/src/main/java/org/hipparchus/CalculusFieldElement.java @@ -22,6 +22,7 @@ package org.hipparchus; import org.hipparchus.exception.MathIllegalArgumentException; +import org.hipparchus.exception.NullArgumentException; import org.hipparchus.util.FastMath; import org.hipparchus.util.FieldSinCos; import org.hipparchus.util.FieldSinhCosh; @@ -42,7 +43,9 @@ public interface CalculusFieldElement> extends FieldEl * @return Archimedes constant π * @since 2.0 */ - T getPi(); + default T getPi() { + return newInstance(FastMath.PI); + } /** Create an instance corresponding to a constant real value. * @param value constant real value @@ -54,25 +57,39 @@ public interface CalculusFieldElement> extends FieldEl * @param a right hand side parameter of the operator * @return this+a */ - T add(double a); + default T add(double a) { + return add(newInstance(a)); + } /** '-' operator. * @param a right hand side parameter of the operator * @return this-a */ - T subtract(double a); + default T subtract(double a) { + return subtract(newInstance(a)); + } /** '×' operator. * @param a right hand side parameter of the operator * @return this×a */ - T multiply(double a); + default T multiply(double a) { + return multiply(newInstance(a)); + } /** '÷' operator. * @param a right hand side parameter of the operator * @return this÷a */ - T divide(double a); + default T divide(double a) { + return divide(newInstance(a)); + } + + /** {@inheritDoc} */ + @Override + default T subtract(T a) { + return add(a.negate()); + } /** * Return the exponent of the instance, removing the bias. @@ -119,7 +136,9 @@ T hypot(T y) /** {@inheritDoc} */ @Override - T reciprocal(); + default T divide(T a) { + return multiply(a.reciprocal()); + } /** Square root. * @return square root of the instance @@ -153,7 +172,9 @@ default T square() { * @param n power to apply * @return thisn */ - T pow(int n); + default T pow(int n) { + return pow((double) n); + } /** Power operation. * @param e exponent @@ -191,12 +212,16 @@ T pow(T e) /** Cosine operation. * @return cos(this) */ - T cos(); + default T cos() { + return sinCos().cos(); + } /** Sine operation. * @return sin(this) */ - T sin(); + default T sin() { + return sinCos().sin(); + } /** Combined Sine and Cosine operation. * @return [sin(this), cos(this)] @@ -207,7 +232,9 @@ T pow(T e) /** Tangent operation. * @return tan(this) */ - T tan(); + default T tan() { + return sin().divide(cos()); + } /** Arc cosine operation. * @return acos(this) @@ -248,12 +275,16 @@ T atan2(T x) /** Hyperbolic cosine operation. * @return cosh(this) */ - T cosh(); + default T cosh() { + return sinhCosh().cosh(); + } /** Hyperbolic sine operation. * @return sinh(this) */ - T sinh(); + default T sinh() { + return sinhCosh().sinh(); + } /** Combined hyperbolic sine and sosine operation. * @return [sinh(this), cosh(this)] @@ -264,7 +295,9 @@ T atan2(T x) /** Hyperbolic tangent operation. * @return tanh(this) */ - T tanh(); + default T tanh() { + return sinh().divide(cosh()); + } /** Inverse hyperbolic cosine operation. * @return acosh(this) @@ -284,12 +317,16 @@ T atan2(T x) /** Convert radians to degrees, with error of less than 0.5 ULP * @return instance converted into degrees */ - T toDegrees(); + default T toDegrees() { + return multiply(FastMath.toDegrees(1.)); + } /** Convert degrees to radians, with error of less than 0.5 ULP * @return instance converted into radians */ - T toRadians(); + default T toRadians() { + return multiply(FastMath.toRadians(1.)); + } /** * Compute a linear combination. @@ -335,7 +372,9 @@ T linearCombination(double[] a, T[] b) * @see #linearCombination(double, FieldElement, double, FieldElement, double, FieldElement) * @see #linearCombination(double, FieldElement, double, FieldElement, double, FieldElement, double, FieldElement) */ - T linearCombination(double a1, T b1, double a2, T b2); + default T linearCombination(double a1, T b1, double a2, T b2) { + return linearCombination(newInstance(a1), b1, newInstance(a2), b2); + } /** * Compute a linear combination. @@ -365,7 +404,9 @@ T linearCombination(double[] a, T[] b) * @see #linearCombination(double, FieldElement, double, FieldElement) * @see #linearCombination(double, FieldElement, double, FieldElement, double, FieldElement, double, FieldElement) */ - T linearCombination(double a1, T b1, double a2, T b2, double a3, T b3); + default T linearCombination(double a1, T b1, double a2, T b2, double a3, T b3) { + return linearCombination(newInstance(a1), b1, newInstance(a2), b2, newInstance(a3), b3); + } /** * Compute a linear combination. @@ -401,7 +442,10 @@ T linearCombination(double[] a, T[] b) * @see #linearCombination(double, FieldElement, double, FieldElement) * @see #linearCombination(double, FieldElement, double, FieldElement, double, FieldElement) */ - T linearCombination(double a1, T b1, double a2, T b2, double a3, T b3, double a4, T b4); + default T linearCombination(double a1, T b1, double a2, T b2, double a3, T b3, double a4, T b4) { + return linearCombination(newInstance(a1), b1, newInstance(a2), b2, newInstance(a3), b3, + newInstance(a4), b4); + } /** Get the smallest whole number larger than instance. * @return ceil(this) diff --git a/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/Derivative.java b/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/Derivative.java index 0ea90bf13..d8bf56930 100644 --- a/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/Derivative.java +++ b/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/Derivative.java @@ -25,6 +25,12 @@ */ public interface Derivative> extends CalculusFieldElement { + /** {@inheritDoc} */ + @Override + default double getReal() { + return getValue(); + } + /** Get the number of free parameters. * @return number of free parameters */ diff --git a/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/DerivativeStructure.java b/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/DerivativeStructure.java index 0d8d5ce29..90db4b944 100644 --- a/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/DerivativeStructure.java +++ b/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/DerivativeStructure.java @@ -146,13 +146,6 @@ void setDerivativeComponent(final int index, final double value) { return data[index]; } - /** {@inheritDoc} - */ - @Override - public double getReal() { - return data[0]; - } - /** Get the value part of the derivative structure. * @return value part of the derivative structure * @see #getPartialDerivative(int...) diff --git a/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/Gradient.java b/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/Gradient.java index c8558ae50..76ef96a0b 100644 --- a/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/Gradient.java +++ b/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/Gradient.java @@ -125,12 +125,6 @@ public Gradient newInstance(final double c) { return new Gradient(c, new double[grad.length]); } - /** {@inheritDoc} */ - @Override - public double getReal() { - return getValue(); - } - /** Get the value part of the function. * @return value part of the value of the function */ diff --git a/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/UnivariateDerivative1.java b/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/UnivariateDerivative1.java index 6815841ba..c6ed2d087 100644 --- a/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/UnivariateDerivative1.java +++ b/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/UnivariateDerivative1.java @@ -94,12 +94,6 @@ public UnivariateDerivative1 newInstance(final double value) { return new UnivariateDerivative1(value, 0.0); } - /** {@inheritDoc} */ - @Override - public double getReal() { - return getValue(); - } - /** {@inheritDoc} */ @Override public double getValue() { diff --git a/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/UnivariateDerivative2.java b/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/UnivariateDerivative2.java index c916b22e0..450a60486 100644 --- a/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/UnivariateDerivative2.java +++ b/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/UnivariateDerivative2.java @@ -100,12 +100,6 @@ public UnivariateDerivative2 newInstance(final double value) { return new UnivariateDerivative2(value, 0.0, 0.0); } - /** {@inheritDoc} */ - @Override - public double getReal() { - return getValue(); - } - /** {@inheritDoc} */ @Override public double getValue() { diff --git a/hipparchus-core/src/main/java/org/hipparchus/dfp/Dfp.java b/hipparchus-core/src/main/java/org/hipparchus/dfp/Dfp.java index 312f737bf..08f5ae1f4 100644 --- a/hipparchus-core/src/main/java/org/hipparchus/dfp/Dfp.java +++ b/hipparchus-core/src/main/java/org/hipparchus/dfp/Dfp.java @@ -2686,34 +2686,6 @@ public double getReal() { return toDouble(); } - /** {@inheritDoc} - */ - @Override - public Dfp add(final double a) { - return add(newInstance(a)); - } - - /** {@inheritDoc} - */ - @Override - public Dfp subtract(final double a) { - return subtract(newInstance(a)); - } - - /** {@inheritDoc} - */ - @Override - public Dfp multiply(final double a) { - return multiply(newInstance(a)); - } - - /** {@inheritDoc} - */ - @Override - public Dfp divide(final double a) { - return divide(newInstance(a)); - } - /** {@inheritDoc} */ @Override diff --git a/hipparchus-core/src/test/java/org/hipparchus/CalculusFieldElementTest.java b/hipparchus-core/src/test/java/org/hipparchus/CalculusFieldElementTest.java new file mode 100644 index 000000000..f3705b7cb --- /dev/null +++ b/hipparchus-core/src/test/java/org/hipparchus/CalculusFieldElementTest.java @@ -0,0 +1,282 @@ +/* + * 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 + * + * https://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.hipparchus; + +import org.hipparchus.exception.MathIllegalArgumentException; +import org.hipparchus.exception.MathRuntimeException; +import org.hipparchus.exception.NullArgumentException; +import org.hipparchus.util.FastMath; +import org.hipparchus.util.FieldSinCos; +import org.hipparchus.util.FieldSinhCosh; +import org.junit.Assert; +import org.junit.Test; + +public class CalculusFieldElementTest { + + @Test + public void testAddDouble() { + // GIVEN + final double value1 = 1.; + final double value2 = 2.; + final TestCalculusFieldElement testElement1 = new TestCalculusFieldElement(value1); + // WHEN + final TestCalculusFieldElement actualAddition = testElement1.add(value2); + // THEN + final TestCalculusFieldElement expectedAddition = new TestCalculusFieldElement(value1 + value2); + Assert.assertEquals(expectedAddition, actualAddition); + } + + private static class TestCalculusFieldElement implements CalculusFieldElement { + + private final double value; + + TestCalculusFieldElement (double value) { + this.value = value; + } + + @Override + public TestCalculusFieldElement newInstance(double value) { + return new TestCalculusFieldElement(value); + } + + @Override + public TestCalculusFieldElement scalb(int n) { + return null; + } + + @Override + public TestCalculusFieldElement ulp() { + return null; + } + + @Override + public TestCalculusFieldElement hypot(TestCalculusFieldElement y) throws MathIllegalArgumentException { + return null; + } + + @Override + public TestCalculusFieldElement sqrt() { + return null; + } + + @Override + public TestCalculusFieldElement cbrt() { + return null; + } + + @Override + public TestCalculusFieldElement rootN(int n) { + return null; + } + + @Override + public TestCalculusFieldElement pow(double p) { + return new TestCalculusFieldElement(FastMath.pow(value, p)); + } + + @Override + public TestCalculusFieldElement pow(TestCalculusFieldElement e) throws MathIllegalArgumentException { + return null; + } + + @Override + public TestCalculusFieldElement exp() { + return null; + } + + @Override + public TestCalculusFieldElement expm1() { + return null; + } + + @Override + public TestCalculusFieldElement log() { + return null; + } + + @Override + public TestCalculusFieldElement log1p() { + return null; + } + + @Override + public TestCalculusFieldElement log10() { + return null; + } + + @Override + public FieldSinCos sinCos() { + return null; + } + + @Override + public TestCalculusFieldElement acos() { + return null; + } + + @Override + public TestCalculusFieldElement asin() { + return null; + } + + @Override + public TestCalculusFieldElement atan() { + return null; + } + + @Override + public TestCalculusFieldElement atan2(TestCalculusFieldElement x) throws MathIllegalArgumentException { + return null; + } + + @Override + public FieldSinhCosh sinhCosh() { + return null; + } + + @Override + public TestCalculusFieldElement acosh() { + return null; + } + + @Override + public TestCalculusFieldElement asinh() { + return null; + } + + @Override + public TestCalculusFieldElement atanh() { + return null; + } + + @Override + public TestCalculusFieldElement linearCombination(TestCalculusFieldElement[] a, TestCalculusFieldElement[] b) throws MathIllegalArgumentException { + return null; + } + + @Override + public TestCalculusFieldElement linearCombination(double[] a, TestCalculusFieldElement[] b) throws MathIllegalArgumentException { + return null; + } + + @Override + public TestCalculusFieldElement linearCombination(TestCalculusFieldElement a1, TestCalculusFieldElement b1, TestCalculusFieldElement a2, TestCalculusFieldElement b2) { + return null; + } + + @Override + public TestCalculusFieldElement linearCombination(TestCalculusFieldElement a1, TestCalculusFieldElement b1, TestCalculusFieldElement a2, TestCalculusFieldElement b2, TestCalculusFieldElement a3, TestCalculusFieldElement b3) { + return null; + } + + @Override + public TestCalculusFieldElement linearCombination(TestCalculusFieldElement a1, TestCalculusFieldElement b1, TestCalculusFieldElement a2, TestCalculusFieldElement b2, TestCalculusFieldElement a3, TestCalculusFieldElement b3, TestCalculusFieldElement a4, TestCalculusFieldElement b4) { + return null; + } + + @Override + public TestCalculusFieldElement ceil() { + return null; + } + + @Override + public TestCalculusFieldElement floor() { + return null; + } + + @Override + public TestCalculusFieldElement rint() { + return null; + } + + @Override + public TestCalculusFieldElement remainder(double a) { + return null; + } + + @Override + public TestCalculusFieldElement remainder(TestCalculusFieldElement a) { + return null; + } + + @Override + public TestCalculusFieldElement sign() { + return null; + } + + @Override + public TestCalculusFieldElement copySign(TestCalculusFieldElement sign) { + return null; + } + + @Override + public TestCalculusFieldElement copySign(double sign) { + return null; + } + + @Override + public TestCalculusFieldElement abs() { + return null; + } + + @Override + public double getReal() { + return value; + } + + @Override + public TestCalculusFieldElement add(TestCalculusFieldElement a) throws NullArgumentException { + return new TestCalculusFieldElement(value + a.value); + } + + @Override + public TestCalculusFieldElement negate() { + return new TestCalculusFieldElement(-value); + } + + @Override + public TestCalculusFieldElement multiply(int n) { + return new TestCalculusFieldElement(value * n); + } + + @Override + public TestCalculusFieldElement multiply(TestCalculusFieldElement a) throws NullArgumentException { + return new TestCalculusFieldElement(value * a.value); + } + + @Override + public TestCalculusFieldElement reciprocal() throws MathRuntimeException { + return new TestCalculusFieldElement(1. / value); + } + + @Override + public Field getField() { + return null; + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof TestCalculusFieldElement) { + return Double.compare(value, ((TestCalculusFieldElement) obj).value) == 0; + } else { + return false; + } + } + } + +} \ No newline at end of file