Skip to content

Commit

Permalink
fixes Hipparchus-Math#281: introducing default implementations in Cal…
Browse files Browse the repository at this point in the history
…culusFieldElement
  • Loading branch information
Serrof committed Nov 11, 2023
1 parent 43dcbbd commit 1ce7f84
Show file tree
Hide file tree
Showing 8 changed files with 350 additions and 71 deletions.
Expand Up @@ -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;
Expand All @@ -42,7 +43,9 @@ public interface CalculusFieldElement<T extends FieldElement<T>> 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
Expand All @@ -54,25 +57,39 @@ public interface CalculusFieldElement<T extends FieldElement<T>> 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));
}

/** '&times;' operator.
* @param a right hand side parameter of the operator
* @return this&times;a
*/
T multiply(double a);
default T multiply(double a) {
return multiply(newInstance(a));
}

/** '&divide;' operator.
* @param a right hand side parameter of the operator
* @return this&divide;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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -153,7 +172,9 @@ default T square() {
* @param n power to apply
* @return this<sup>n</sup>
*/
T pow(int n);
default T pow(int n) {
return pow((double) n);
}

/** Power operation.
* @param e exponent
Expand Down Expand Up @@ -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)]
Expand All @@ -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)
Expand Down Expand Up @@ -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)]
Expand All @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
Expand Up @@ -25,6 +25,12 @@
*/
public interface Derivative<T extends CalculusFieldElement<T>> extends CalculusFieldElement<T> {

/** {@inheritDoc} */
@Override
default double getReal() {
return getValue();
}

/** Get the number of free parameters.
* @return number of free parameters
*/
Expand Down
Expand Up @@ -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...)
Expand Down
Expand Up @@ -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
*/
Expand Down
Expand Up @@ -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() {
Expand Down
Expand Up @@ -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() {
Expand Down
28 changes: 0 additions & 28 deletions hipparchus-core/src/main/java/org/hipparchus/dfp/Dfp.java
Expand Up @@ -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
Expand Down

0 comments on commit 1ce7f84

Please sign in to comment.