From 963d985665a41c4232fb406242f85098a8b6ca8d Mon Sep 17 00:00:00 2001 From: Serrof Date: Fri, 3 Nov 2023 22:10:24 +0100 Subject: [PATCH] Fix #280: add square to FieldElement --- hipparchus-core/src/changes/changes.xml | 5 ++++ .../org/hipparchus/CalculusFieldElement.java | 6 +++++ .../java/org/hipparchus/FieldElement.java | 6 +++++ .../analysis/differentiation/DSCompiler.java | 12 ++++----- .../differentiation/DerivativeStructure.java | 6 +++++ .../FieldDerivativeStructure.java | 6 +++++ .../differentiation/FieldGradient.java | 26 +++++++++++++------ .../FieldUnivariateDerivative1.java | 8 +++++- .../FieldUnivariateDerivative2.java | 10 +++++-- .../analysis/differentiation/Gradient.java | 13 ++++++++-- .../differentiation/SparseGradient.java | 8 +++++- .../UnivariateDerivative1.java | 6 +++++ .../UnivariateDerivative2.java | 6 +++++ .../gauss/FieldHermiteRuleFactory.java | 2 +- .../gauss/FieldLaguerreRuleFactory.java | 2 +- .../gauss/FieldLegendreRuleFactory.java | 2 +- .../java/org/hipparchus/complex/Complex.java | 14 +++++++--- .../org/hipparchus/complex/FieldComplex.java | 13 +++++++--- .../src/main/java/org/hipparchus/dfp/Dfp.java | 12 ++++++--- .../main/java/org/hipparchus/dfp/DfpMath.java | 8 +++--- .../org/hipparchus/fraction/BigFraction.java | 5 ++++ .../org/hipparchus/fraction/Fraction.java | 7 +++++ .../linear/FieldQRDecomposition.java | 2 +- .../main/java/org/hipparchus/special/Erf.java | 4 +-- .../java/org/hipparchus/special/Gamma.java | 8 +++--- .../elliptic/jacobi/FieldJacobiElliptic.java | 8 +++--- .../java/org/hipparchus/util/BigReal.java | 7 +++++ .../java/org/hipparchus/util/Binary64.java | 5 ++++ .../java/org/hipparchus/util/FieldTuple.java | 6 +++++ .../main/java/org/hipparchus/util/Tuple.java | 6 +++++ .../CalculusFieldElementAbstractTest.java | 7 +++++ .../analysis/FieldFunctionsTest.java | 8 +++--- .../analysis/FunctionUtilsTest.java | 6 ++--- .../FieldDerivativeStructureComplexTest.java | 2 +- ...ieldUnivariateDerivative1AbstractTest.java | 2 +- ...ieldUnivariateDerivative2AbstractTest.java | 2 +- .../differentiation/GradientFunctionTest.java | 2 +- .../UnivariateDerivativeAbstractTest.java | 2 +- .../AkimaSplineInterpolatorTest.java | 6 ++--- .../FieldHermiteInterpolatorTest.java | 2 +- ...icubicSplineInterpolatingFunctionTest.java | 4 +-- .../org/hipparchus/complex/ComplexTest.java | 6 ++--- .../hipparchus/complex/FieldComplexTest.java | 6 ++--- .../java/org/hipparchus/special/ErfTest.java | 4 +-- ...onEllipticIntegralAbstractComplexTest.java | 2 +- .../CarlsonEllipticIntegralFieldTest.java | 2 +- .../euclidean/threed/FieldVector3D.java | 10 +++---- .../euclidean/twod/FieldVector2D.java | 4 +-- src/changes/changes.xml | 5 ++++ 49 files changed, 227 insertions(+), 84 deletions(-) diff --git a/hipparchus-core/src/changes/changes.xml b/hipparchus-core/src/changes/changes.xml index 559e5767f..d2f40ba4a 100644 --- a/hipparchus-core/src/changes/changes.xml +++ b/hipparchus-core/src/changes/changes.xml @@ -49,6 +49,11 @@ If the output is not quite correct, check for invisible trailing spaces! Hipparchus Core Release Notes + + + Add square method to FieldElement. + + UnivariateDerivative now implements Comparable. diff --git a/hipparchus-core/src/main/java/org/hipparchus/CalculusFieldElement.java b/hipparchus-core/src/main/java/org/hipparchus/CalculusFieldElement.java index 57f9a888f..a7f57eb88 100644 --- a/hipparchus-core/src/main/java/org/hipparchus/CalculusFieldElement.java +++ b/hipparchus-core/src/main/java/org/hipparchus/CalculusFieldElement.java @@ -137,6 +137,12 @@ T hypot(T y) */ T rootN(int n); + /** {@inheritDoc} */ + @Override + default T square() { + return pow(2); + } + /** Power operation. * @param p power to apply * @return thisp diff --git a/hipparchus-core/src/main/java/org/hipparchus/FieldElement.java b/hipparchus-core/src/main/java/org/hipparchus/FieldElement.java index 2577fad73..92a9f79ce 100644 --- a/hipparchus-core/src/main/java/org/hipparchus/FieldElement.java +++ b/hipparchus-core/src/main/java/org/hipparchus/FieldElement.java @@ -74,6 +74,12 @@ public interface FieldElement> { */ T multiply(T a) throws NullArgumentException; + /** Compute this × this. + * @return a new element representing this × this + * @since 3.1 + */ + T square() throws NullArgumentException; + /** Compute this ÷ a. * @param a element to divide by * @return a new element representing this ÷ a diff --git a/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/DSCompiler.java b/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/DSCompiler.java index 166b677fe..6f088d745 100644 --- a/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/DSCompiler.java +++ b/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/DSCompiler.java @@ -2508,7 +2508,7 @@ public > void acos(final T[] operand, final in // as per polynomial parity, we can store coefficients of both P_(n-1) and P_n in the same array final T[] p = MathArrays.buildArray(field, order); p[0] = field.getOne().negate(); - final T x2 = x.multiply(x); + final T x2 = x.square(); final T f = x2.subtract(1).negate().reciprocal(); T coeff = f.sqrt(); function[1] = coeff.multiply(p[0]); @@ -2625,7 +2625,7 @@ public > void asin(final T[] operand, final in // as per polynomial parity, we can store coefficients of both P_(n-1) and P_n in the same array final T[] p = MathArrays.buildArray(field, order); p[0] = field.getOne(); - final T x2 = x.multiply(x); + final T x2 = x.square(); final T f = x2.subtract(1).negate().reciprocal(); T coeff = f.sqrt(); function[1] = coeff.multiply(p[0]); @@ -2742,7 +2742,7 @@ public > void atan(final T[] operand, final in // as per polynomial parity, we can store coefficients of both Q_(n-1) and Q_n in the same array final T[] q = MathArrays.buildArray(field, order); q[0] = field.getOne(); - final T x2 = x.multiply(x); + final T x2 = x.square(); final T f = x2.add(1).reciprocal(); T coeff = f; function[1] = coeff.multiply(q[0]); @@ -3261,7 +3261,7 @@ public > void acosh(final T[] operand, final i // as per polynomial parity, we can store coefficients of both P_(n-1) and P_n in the same array final T[] p = MathArrays.buildArray(field, order); p[0] = field.getOne(); - final T x2 = x.multiply(x); + final T x2 = x.square(); final T f = x2.subtract(1).reciprocal(); T coeff = f.sqrt(); function[1] = coeff.multiply(p[0]); @@ -3378,7 +3378,7 @@ public > void asinh(final T[] operand, final i // as per polynomial parity, we can store coefficients of both P_(n-1) and P_n in the same array final T[] p = MathArrays.buildArray(field, order); p[0] = field.getOne(); - final T x2 = x.multiply(x); + final T x2 = x.square(); final T f = x2.add(1).reciprocal(); T coeff = f.sqrt(); function[1] = coeff.multiply(p[0]); @@ -3495,7 +3495,7 @@ public > void atanh(final T[] operand, final i // as per polynomial parity, we can store coefficients of both Q_(n-1) and Q_n in the same array final T[] q = MathArrays.buildArray(field, order); q[0] = field.getOne(); - final T x2 = x.multiply(x); + final T x2 = x.square(); final T f =x2.subtract(1).negate().reciprocal(); T coeff = f; function[1] = coeff.multiply(q[0]); 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 8fffa2e67..0d8d5ce29 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 @@ -250,6 +250,12 @@ public DerivativeStructure multiply(final DerivativeStructure a) return result; } + /** {@inheritDoc} */ + @Override + public DerivativeStructure square() { + return multiply(this); + } + /** {@inheritDoc} */ @Override diff --git a/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/FieldDerivativeStructure.java b/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/FieldDerivativeStructure.java index ed009b0b1..07e2afccb 100644 --- a/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/FieldDerivativeStructure.java +++ b/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/FieldDerivativeStructure.java @@ -258,6 +258,12 @@ public FieldDerivativeStructure multiply(final FieldDerivativeStructure a) return result; } + /** {@inheritDoc} */ + @Override + public FieldDerivativeStructure square() { + return multiply(this); + } + /** '÷' operator. * @param a right hand side parameter of the operator * @return this÷a diff --git a/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/FieldGradient.java b/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/FieldGradient.java index 6b125c67c..bbd31323e 100644 --- a/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/FieldGradient.java +++ b/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/FieldGradient.java @@ -330,6 +330,16 @@ public FieldGradient multiply(final FieldGradient a) { return result; } + /** {@inheritDoc} */ + @Override + public FieldGradient square() { + final FieldGradient result = newInstance(value.square()); + for (int i = 0; i < grad.length; ++i) { + result.grad[i] = grad[i].multiply(value).multiply(2); + } + return result; + } + /** '÷' operator. * @param a right hand side parameter of the operator * @return this÷a @@ -580,7 +590,7 @@ public FieldGradient sqrt() { @Override public FieldGradient cbrt() { final T c = FastMath.cbrt(value); - return compose(c, c.multiply(c).multiply(3).reciprocal()); + return compose(c, c.square().multiply(3).reciprocal()); } /** {@inheritDoc} */ @@ -721,25 +731,25 @@ public FieldGradient tan() { /** {@inheritDoc} */ @Override public FieldGradient acos() { - return compose(FastMath.acos(value), value.multiply(value).negate().add(1).sqrt().reciprocal().negate()); + return compose(FastMath.acos(value), value.square().negate().add(1).sqrt().reciprocal().negate()); } /** {@inheritDoc} */ @Override public FieldGradient asin() { - return compose(FastMath.asin(value), value.multiply(value).negate().add(1).sqrt().reciprocal()); + return compose(FastMath.asin(value), value.square().negate().add(1).sqrt().reciprocal()); } /** {@inheritDoc} */ @Override public FieldGradient atan() { - return compose(FastMath.atan(value), value.multiply(value).add(1).reciprocal()); + return compose(FastMath.atan(value), value.square().add(1).reciprocal()); } /** {@inheritDoc} */ @Override public FieldGradient atan2(final FieldGradient x) { - final T inv = value.multiply(value).add(x.value.multiply(x.value)).reciprocal(); + final T inv = value.square().add(x.value.multiply(x.value)).reciprocal(); final FieldGradient result = newInstance(FastMath.atan2(value, x.value)); final T xValueInv = x.value.multiply(inv); final T mValueInv = value.negate().multiply(inv); @@ -784,19 +794,19 @@ public FieldGradient tanh() { /** {@inheritDoc} */ @Override public FieldGradient acosh() { - return compose(FastMath.acosh(value), value.multiply(value).subtract(1).sqrt().reciprocal()); + return compose(FastMath.acosh(value), value.square().subtract(1).sqrt().reciprocal()); } /** {@inheritDoc} */ @Override public FieldGradient asinh() { - return compose(FastMath.asinh(value), value.multiply(value).add(1).sqrt().reciprocal()); + return compose(FastMath.asinh(value), value.square().add(1).sqrt().reciprocal()); } /** {@inheritDoc} */ @Override public FieldGradient atanh() { - return compose(FastMath.atanh(value), value.multiply(value).negate().add(1).reciprocal()); + return compose(FastMath.atanh(value), value.square().negate().add(1).reciprocal()); } /** {@inheritDoc} */ diff --git a/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/FieldUnivariateDerivative1.java b/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/FieldUnivariateDerivative1.java index 866483c0a..34a9cfb04 100644 --- a/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/FieldUnivariateDerivative1.java +++ b/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/FieldUnivariateDerivative1.java @@ -218,6 +218,12 @@ public FieldUnivariateDerivative1 multiply(final FieldUnivariateDerivative1 square() { + return multiply(this); + } + /** '÷' operator. * @param a right hand side parameter of the operator * @return this÷a @@ -441,7 +447,7 @@ public FieldUnivariateDerivative1 sqrt() { @Override public FieldUnivariateDerivative1 cbrt() { final T c = FastMath.cbrt(f0); - return compose(c, c.multiply(c).multiply(3).reciprocal()); + return compose(c, c.square().multiply(3).reciprocal()); } /** {@inheritDoc} */ diff --git a/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/FieldUnivariateDerivative2.java b/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/FieldUnivariateDerivative2.java index 8bfcaf76b..499625243 100644 --- a/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/FieldUnivariateDerivative2.java +++ b/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/FieldUnivariateDerivative2.java @@ -236,6 +236,12 @@ public FieldUnivariateDerivative2 multiply(final FieldUnivariateDerivative2 square() { + return multiply(this); + } + /** '÷' operator. * @param a right hand side parameter of the operator * @return this÷a @@ -484,7 +490,7 @@ public FieldUnivariateDerivative2 sqrt() { @Override public FieldUnivariateDerivative2 cbrt() { final T c = FastMath.cbrt(f0); - final T c2 = c.multiply(c); + final T c2 = c.square(); return compose(c, c2.multiply(3).reciprocal(), c2.multiply(-4.5).multiply(f0).reciprocal()); } @@ -498,7 +504,7 @@ public FieldUnivariateDerivative2 rootN(final int n) { } else { final T r = FastMath.pow(f0, 1.0 / n); final T z = FastMath.pow(r, n - 1).multiply(n); - return compose(r, z.reciprocal(), z.multiply(z).multiply(r).reciprocal().multiply(1 -n)); + return compose(r, z.reciprocal(), z.square().multiply(r).reciprocal().multiply(1 -n)); } } 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 07faffcba..c8558ae50 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 @@ -19,7 +19,6 @@ import java.io.Serializable; import java.util.Arrays; -import org.hipparchus.CalculusFieldElement; import org.hipparchus.exception.LocalizedCoreFormats; import org.hipparchus.exception.MathIllegalArgumentException; import org.hipparchus.util.FastMath; @@ -54,7 +53,7 @@ * @see FieldGradient * @since 1.7 */ -public class Gradient implements Derivative, CalculusFieldElement, Serializable { +public class Gradient implements Derivative, Serializable { /** Serializable UID. */ private static final long serialVersionUID = 20200520L; @@ -274,6 +273,16 @@ public Gradient multiply(final Gradient a) { return result; } + /** {@inheritDoc} */ + @Override + public Gradient square() { + final Gradient result = newInstance(value * value); + for (int i = 0; i < grad.length; ++i) { + result.grad[i] = 2 * grad[i] * value; + } + return result; + } + /** {@inheritDoc} */ @Override public Gradient divide(final double a) { diff --git a/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/SparseGradient.java b/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/SparseGradient.java index a1923135b..d75b78c14 100644 --- a/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/SparseGradient.java +++ b/hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/SparseGradient.java @@ -281,6 +281,12 @@ public SparseGradient multiply(final int n) { return new SparseGradient(value * n, n, derivatives); } + /** {@inheritDoc} */ + @Override + public SparseGradient square() { + return multiply(this); + } + /** {@inheritDoc} */ @Override public SparseGradient divide(final SparseGradient a) { @@ -698,7 +704,7 @@ public SparseGradient atan() { public SparseGradient atan2(final SparseGradient x) { // compute r = sqrt(x^2+y^2) - final SparseGradient r = multiply(this).add(x.multiply(x)).sqrt(); + final SparseGradient r = square().add(x.square()).sqrt(); final SparseGradient a; if (x.value >= 0) { 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 2d0aed915..6815841ba 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 @@ -182,6 +182,12 @@ public UnivariateDerivative1 multiply(final UnivariateDerivative1 a) { MathArrays.linearCombination(f1, a.f0, f0, a.f1)); } + /** {@inheritDoc} */ + @Override + public UnivariateDerivative1 square() { + return new UnivariateDerivative1(f0 * f0, 2 * f0 * f1); + } + /** {@inheritDoc} */ @Override public UnivariateDerivative1 divide(final double a) { 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 2cde3c981..c916b22e0 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 @@ -201,6 +201,12 @@ public UnivariateDerivative2 multiply(final UnivariateDerivative2 a) { MathArrays.linearCombination(f2, a.f0, 2 * f1, a.f1, f0, a.f2)); } + /** {@inheritDoc} */ + @Override + public UnivariateDerivative2 square() { + return new UnivariateDerivative2(f0 * f0, 2 * f0 * f1, 2 * (f0 * f2 + f1 * f1)); + } + /** {@inheritDoc} */ @Override public UnivariateDerivative2 divide(final double a) { diff --git a/hipparchus-core/src/main/java/org/hipparchus/analysis/integration/gauss/FieldHermiteRuleFactory.java b/hipparchus-core/src/main/java/org/hipparchus/analysis/integration/gauss/FieldHermiteRuleFactory.java index 896f426af..61c7529a1 100644 --- a/hipparchus-core/src/main/java/org/hipparchus/analysis/integration/gauss/FieldHermiteRuleFactory.java +++ b/hipparchus-core/src/main/java/org/hipparchus/analysis/integration/gauss/FieldHermiteRuleFactory.java @@ -78,7 +78,7 @@ protected Pair computeRule(int numberOfPoints) final Hermite hm1 = new Hermite<>(field, numberOfPoints - 1); for (int i = 0; i < numberOfPoints; i++) { final T y = hm1.hNhNm1(points[i])[0]; - weights[i] = sqrtPi.divide(y.multiply(y).multiply(numberOfPoints)); + weights[i] = sqrtPi.divide(y.square().multiply(numberOfPoints)); } return new Pair<>(points, weights); diff --git a/hipparchus-core/src/main/java/org/hipparchus/analysis/integration/gauss/FieldLaguerreRuleFactory.java b/hipparchus-core/src/main/java/org/hipparchus/analysis/integration/gauss/FieldLaguerreRuleFactory.java index c5df96bba..629af81f6 100644 --- a/hipparchus-core/src/main/java/org/hipparchus/analysis/integration/gauss/FieldLaguerreRuleFactory.java +++ b/hipparchus-core/src/main/java/org/hipparchus/analysis/integration/gauss/FieldLaguerreRuleFactory.java @@ -57,7 +57,7 @@ public Pair computeRule(int numberOfPoints) final Laguerre laguerreN1 = new Laguerre<>(n1); for (int i = 0; i < numberOfPoints; i++) { final T y = laguerreN1.value(points[i]); - weights[i] = points[i].divide(y.multiply(y).multiply(n1Squared)); + weights[i] = points[i].divide(y.square().multiply(n1Squared)); } return new Pair<>(points, weights); diff --git a/hipparchus-core/src/main/java/org/hipparchus/analysis/integration/gauss/FieldLegendreRuleFactory.java b/hipparchus-core/src/main/java/org/hipparchus/analysis/integration/gauss/FieldLegendreRuleFactory.java index 951074add..05f1f64a1 100644 --- a/hipparchus-core/src/main/java/org/hipparchus/analysis/integration/gauss/FieldLegendreRuleFactory.java +++ b/hipparchus-core/src/main/java/org/hipparchus/analysis/integration/gauss/FieldLegendreRuleFactory.java @@ -70,7 +70,7 @@ public Pair computeRule(int numberOfPoints) final T c = points[i]; final T[] pKpKm1 = p.pNpNm1(c); final T d = pKpKm1[1].subtract(c.multiply(pKpKm1[0])).multiply(numberOfPoints); - weights[i] = c.multiply(c).subtract(1).multiply(-2).divide(d.multiply(d)); + weights[i] = c.square().subtract(1).multiply(-2).divide(d.multiply(d)); // symmetrical point final int idx = numberOfPoints - i - 1; diff --git a/hipparchus-core/src/main/java/org/hipparchus/complex/Complex.java b/hipparchus-core/src/main/java/org/hipparchus/complex/Complex.java index a842c3a73..28a5a0b2e 100644 --- a/hipparchus-core/src/main/java/org/hipparchus/complex/Complex.java +++ b/hipparchus-core/src/main/java/org/hipparchus/complex/Complex.java @@ -656,6 +656,12 @@ public Complex multiplyMinusI() { return createComplex(imaginary, -real); } + /** {@inheritDoc} */ + @Override + public Complex square() { + return multiply(this); + } + /** * Returns a {@code Complex} whose value is {@code (-this)}. * Returns {@code NaN} if either real or imaginary @@ -1164,7 +1170,7 @@ public FieldSinCos sinCos() { public Complex atan2(Complex x) { // compute r = sqrt(x^2+y^2) - final Complex r = x.multiply(x).add(multiply(this)).sqrt(); + final Complex r = x.square().add(multiply(this)).sqrt(); if (FastMath.copySign(1.0, x.real) >= 0) { // compute atan2(y, x) = 2 atan(y / (r + x)) @@ -1328,7 +1334,7 @@ public Complex sqrt() { * square root of 1 - this2 for this complex * number. * Computes the result directly as - * {@code sqrt(ONE.subtract(z.multiply(z)))}. + * {@code sqrt(ONE.subtract(z.square()))}. *

* Returns {@link Complex#NaN} if either real or imaginary part of the * input argument is {@code NaN}. @@ -1339,7 +1345,7 @@ public Complex sqrt() { * @return the square root of 1 - this2. */ public Complex sqrt1z() { - final Complex t2 = this.multiply(this); + final Complex t2 = this.square(); return createComplex(1 - t2.real, -t2.imaginary).sqrt(); } @@ -1647,7 +1653,7 @@ public Complex hypot(Complex y) { } else if (isNaN() || y.isNaN()) { return NaN; } else { - return multiply(this).add(y.multiply(y)).sqrt(); + return square().add(y.square()).sqrt(); } } diff --git a/hipparchus-core/src/main/java/org/hipparchus/complex/FieldComplex.java b/hipparchus-core/src/main/java/org/hipparchus/complex/FieldComplex.java index 1b15a37e6..c2b39b342 100644 --- a/hipparchus-core/src/main/java/org/hipparchus/complex/FieldComplex.java +++ b/hipparchus-core/src/main/java/org/hipparchus/complex/FieldComplex.java @@ -747,6 +747,11 @@ public FieldComplex multiplyMinusI() { return createComplex(imaginary, real.negate()); } + @Override + public FieldComplex square() { + return multiply(this); + } + /** * Returns a {@code Complex} whose value is {@code (-this)}. * Returns {@code NaN} if either real or imaginary @@ -1296,7 +1301,7 @@ public FieldSinCos> sinCos() { public FieldComplex atan2(FieldComplex x) { // compute r = sqrt(x^2+y^2) - final FieldComplex r = x.multiply(x).add(multiply(this)).sqrt(); + final FieldComplex r = x.square().add(multiply(this)).sqrt(); if (x.real.getReal() >= 0) { // compute atan2(y, x) = 2 atan(y / (r + x)) @@ -1457,7 +1462,7 @@ public FieldComplex sqrt() { * square root of 1 - this2 for this complex * number. * Computes the result directly as - * {@code sqrt(ONE.subtract(z.multiply(z)))}. + * {@code sqrt(ONE.subtract(z.square()))}. *

* Returns {@link #getNaN(Field)} if either real or imaginary part of the * input argument is {@code NaN}. @@ -1468,7 +1473,7 @@ public FieldComplex sqrt() { * @return the square root of 1 - this2. */ public FieldComplex sqrt1z() { - final FieldComplex t2 = this.multiply(this); + final FieldComplex t2 = this.square(); return createComplex(getPartsField().getOne().subtract(t2.real), t2.imaginary.negate()).sqrt(); } @@ -1766,7 +1771,7 @@ public FieldComplex hypot(FieldComplex y) { } else if (isNaN() || y.isNaN()) { return getNaN(getPartsField()); } else { - return multiply(this).add(y.multiply(y)).sqrt(); + return square().add(y.square()).sqrt(); } } 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 4db2f2d3f..312f737bf 100644 --- a/hipparchus-core/src/main/java/org/hipparchus/dfp/Dfp.java +++ b/hipparchus-core/src/main/java/org/hipparchus/dfp/Dfp.java @@ -1852,6 +1852,12 @@ private Dfp multiplyFast(final int x) { return result; } + /** {@inheritDoc} */ + @Override + public Dfp square() { + return multiply(this); + } + /** Divide this by divisor. * @param divisor divisor * @return quotient of this by divisor @@ -2950,7 +2956,7 @@ public Dfp atan2(final Dfp x) throws MathIllegalArgumentException { // compute r = sqrt(x^2+y^2) - final Dfp r = x.multiply(x).add(multiply(this)).sqrt(); + final Dfp r = x.square().add(multiply(this)).sqrt(); if (r.isZero()) { // special cases handling if (x.sign >= 0) { @@ -3012,14 +3018,14 @@ public Dfp tanh() { */ @Override public Dfp acosh() { - return multiply(this).subtract(getOne()).sqrt().add(this).log(); + return square().subtract(getOne()).sqrt().add(this).log(); } /** {@inheritDoc} */ @Override public Dfp asinh() { - return multiply(this).add(getOne()).sqrt().add(this).log(); + return square().add(getOne()).sqrt().add(this).log(); } /** {@inheritDoc} diff --git a/hipparchus-core/src/main/java/org/hipparchus/dfp/DfpMath.java b/hipparchus-core/src/main/java/org/hipparchus/dfp/DfpMath.java index 5a33a44a9..3a49a9eba 100644 --- a/hipparchus-core/src/main/java/org/hipparchus/dfp/DfpMath.java +++ b/hipparchus-core/src/main/java/org/hipparchus/dfp/DfpMath.java @@ -677,7 +677,7 @@ protected static Dfp sinInternal(Dfp a[]) { Dfp c = a[0].add(a[1]); Dfp y = c; - c = c.multiply(c); + c = c.square(); Dfp x = y; Dfp fact = a[0].getOne(); Dfp py = new Dfp(y); @@ -710,7 +710,7 @@ protected static Dfp cosInternal(Dfp a[]) { Dfp x = one; Dfp y = one; Dfp c = a[0].add(a[1]); - c = c.multiply(c); + c = c.square(); Dfp fact = one; Dfp py = new Dfp(y); @@ -940,7 +940,7 @@ public static Dfp atan(final Dfp a) { * @return asin(a) */ public static Dfp asin(final Dfp a) { - return atan(a.divide(a.getOne().subtract(a.multiply(a)).sqrt())); + return atan(a.divide(a.getOne().subtract(a.square()).sqrt())); } /** computes the arc-cosine of the argument. @@ -957,7 +957,7 @@ public static Dfp acos(Dfp a) { a = Dfp.copysign(a, a.getOne()); // absolute value - result = atan(a.getOne().subtract(a.multiply(a)).sqrt().divide(a)); + result = atan(a.getOne().subtract(a.square()).sqrt().divide(a)); if (negative) { result = a.getField().getPi().subtract(result); diff --git a/hipparchus-core/src/main/java/org/hipparchus/fraction/BigFraction.java b/hipparchus-core/src/main/java/org/hipparchus/fraction/BigFraction.java index 60c435c1b..0b2fe7191 100644 --- a/hipparchus-core/src/main/java/org/hipparchus/fraction/BigFraction.java +++ b/hipparchus-core/src/main/java/org/hipparchus/fraction/BigFraction.java @@ -966,6 +966,11 @@ public BigFraction multiply(final BigFraction fraction) { denominator.multiply(fraction.denominator)); } + @Override + public BigFraction square() throws NullArgumentException { + return multiply(this); + } + /** *

* Return the additive inverse of this fraction, returning the result in diff --git a/hipparchus-core/src/main/java/org/hipparchus/fraction/Fraction.java b/hipparchus-core/src/main/java/org/hipparchus/fraction/Fraction.java index f7458a6a6..3126aebf6 100644 --- a/hipparchus-core/src/main/java/org/hipparchus/fraction/Fraction.java +++ b/hipparchus-core/src/main/java/org/hipparchus/fraction/Fraction.java @@ -31,6 +31,7 @@ import org.hipparchus.exception.LocalizedCoreFormats; import org.hipparchus.exception.MathIllegalStateException; import org.hipparchus.exception.MathRuntimeException; +import org.hipparchus.exception.NullArgumentException; import org.hipparchus.fraction.ConvergentsIterator.ConvergenceStep; import org.hipparchus.util.ArithmeticUtils; import org.hipparchus.util.FastMath; @@ -614,6 +615,12 @@ public Fraction multiply(final int i) { return multiply(new Fraction(i)); } + /** {@inheritDoc} */ + @Override + public Fraction square() throws NullArgumentException { + return multiply(this); + } + /** * Divide the value of this fraction by another. * diff --git a/hipparchus-core/src/main/java/org/hipparchus/linear/FieldQRDecomposition.java b/hipparchus-core/src/main/java/org/hipparchus/linear/FieldQRDecomposition.java index 616a6e20b..1400cef4c 100644 --- a/hipparchus-core/src/main/java/org/hipparchus/linear/FieldQRDecomposition.java +++ b/hipparchus-core/src/main/java/org/hipparchus/linear/FieldQRDecomposition.java @@ -144,7 +144,7 @@ protected void performHouseholderReflection(int minor, T[][] matrix) { T xNormSqr = zero; for (int row = minor; row < qrtMinor.length; row++) { final T c = qrtMinor[row]; - xNormSqr = xNormSqr.add(c.multiply(c)); + xNormSqr = xNormSqr.add(c.square()); } final T a = (qrtMinor[minor].getReal() > 0) ? xNormSqr.sqrt().negate() : xNormSqr.sqrt(); rDiag[minor] = a; diff --git a/hipparchus-core/src/main/java/org/hipparchus/special/Erf.java b/hipparchus-core/src/main/java/org/hipparchus/special/Erf.java index f9efd6c03..2575aa8d9 100644 --- a/hipparchus-core/src/main/java/org/hipparchus/special/Erf.java +++ b/hipparchus-core/src/main/java/org/hipparchus/special/Erf.java @@ -109,7 +109,7 @@ public static > T erf(T x) { if (FastMath.abs(x.getReal()) > 40) { return x.getReal() > 0 ? one : one.negate(); } - final T ret = Gamma.regularizedGammaP(one.multiply(0.5), x.multiply(x), 1.0e-15, 10000); + final T ret = Gamma.regularizedGammaP(one.multiply(0.5), x.square(), 1.0e-15, 10000); return x.getReal() < 0 ? ret.negate() : ret; } @@ -176,7 +176,7 @@ public static > T erfc(T x) { if (FastMath.abs(x.getReal()) > 40) { return x.getReal() > 0 ? zero : one.multiply(2.); } - final T ret = Gamma.regularizedGammaQ(one.multiply(0.5), x.multiply(x), 1.0e-15, 10000); + final T ret = Gamma.regularizedGammaQ(one.multiply(0.5), x.square(), 1.0e-15, 10000); return x.getReal() < 0 ? ret.negate().add(2.) : ret; } diff --git a/hipparchus-core/src/main/java/org/hipparchus/special/Gamma.java b/hipparchus-core/src/main/java/org/hipparchus/special/Gamma.java index 0a49f2f9e..ad2a3a70b 100644 --- a/hipparchus-core/src/main/java/org/hipparchus/special/Gamma.java +++ b/hipparchus-core/src/main/java/org/hipparchus/special/Gamma.java @@ -736,7 +736,7 @@ public static > T digamma(T x) { if (x.getReal() >= C_LIMIT) { // use method 8 (accurate to O(1/x^8)) - T inv = x.multiply(x).reciprocal(); + T inv = x.square().reciprocal(); // 1 1 1 1 1 5 691 1 // log(x) - --- - ------ + ------- - ------- + ------- - ------- + ---------- - ------- // 2 x 12 x^2 120 x^4 252 x^6 240 x^8 660 x^10 32760 x^12 12 x^14 @@ -809,12 +809,12 @@ public static > T trigamma(T x) { if (x.getReal() > 0 && x.getReal() <= S_LIMIT) { // use method 5 from Bernardo AS103 // accurate to O(x) - return x.multiply(x).reciprocal(); + return x.square().reciprocal(); } if (x.getReal() >= C_LIMIT) { // use method 4 (accurate to O(1/x^8) - T inv = x.multiply(x).reciprocal(); + T inv = x.square().reciprocal(); T invCub = inv.multiply(x.reciprocal()); // 1 1 1 1 1 1 5 691 7 // - + ---- + ---- - ----- + ----- + ----- + ------- - -------- + ------ @@ -836,7 +836,7 @@ public static > T trigamma(T x) { .add(1.0 / 6))); } - return trigamma(x.add(1.)).add(x.multiply(x).reciprocal()); + return trigamma(x.add(1.)).add(x.square().reciprocal()); } /** diff --git a/hipparchus-core/src/main/java/org/hipparchus/special/elliptic/jacobi/FieldJacobiElliptic.java b/hipparchus-core/src/main/java/org/hipparchus/special/elliptic/jacobi/FieldJacobiElliptic.java index 866840557..bb71857f8 100644 --- a/hipparchus-core/src/main/java/org/hipparchus/special/elliptic/jacobi/FieldJacobiElliptic.java +++ b/hipparchus-core/src/main/java/org/hipparchus/special/elliptic/jacobi/FieldJacobiElliptic.java @@ -356,7 +356,7 @@ public T arccd(final double x) { private T arcps(final T x, final T deltaQP, final T deltaRP) { // see equation 19.25.32 in Digital Library of Mathematical Functions // https://dlmf.nist.gov/19.25.E32 - final T x2 = x.multiply(x); + final T x2 = x.square(); final T rf = CarlsonEllipticIntegral.rF(x2, x2.add(deltaQP), x2.add(deltaRP)); return FastMath.copySign(1.0, rf.getReal()) * FastMath.copySign(1.0, x.getReal()) < 0 ? rf.negate() : rf; @@ -375,7 +375,7 @@ private T arcps(final T x, final T deltaQP, final T deltaRP) { private T arcsp(final T x, final T deltaQP, final T deltaRP) { // see equation 19.25.33 in Digital Library of Mathematical Functions // https://dlmf.nist.gov/19.25.E33 - final T x2 = x.multiply(x); + final T x2 = x.square(); return x.multiply(CarlsonEllipticIntegral.rF(x.getField().getOne(), deltaQP.multiply(x2).add(1), deltaRP.multiply(x2).add(1))); @@ -394,7 +394,7 @@ private T arcsp(final T x, final T deltaQP, final T deltaRP) { private T arcpq(final T x, final T deltaQP, final T deltaRQ) { // see equation 19.25.34 in Digital Library of Mathematical Functions // https://dlmf.nist.gov/19.25.E34 - final T x2 = x.multiply(x); + final T x2 = x.square(); final T w = x2.subtract(1).negate().divide(deltaQP); final T rf = CarlsonEllipticIntegral.rF(x2, x.getField().getOne(), deltaRQ.multiply(w).add(1)); final T positive = w.sqrt().multiply(rf); @@ -420,7 +420,7 @@ private T arcpq(final T x, final T deltaQP, final T deltaRQ) { private T arcpqNoDivision(final T x, final T deltaQP, final T deltaRQ) { // see equation 19.25.34 in Digital Library of Mathematical Functions // https://dlmf.nist.gov/19.25.E34 - final T x2 = x.multiply(x); + final T x2 = x.square(); final T wDeltaQP = x2.subtract(1).negate(); final T rf = CarlsonEllipticIntegral.rF(x2.multiply(deltaQP), deltaQP, deltaRQ.multiply(wDeltaQP).add(deltaQP)); final T positive = wDeltaQP.sqrt().multiply(rf); diff --git a/hipparchus-core/src/main/java/org/hipparchus/util/BigReal.java b/hipparchus-core/src/main/java/org/hipparchus/util/BigReal.java index 078d52447..fbac5f550 100644 --- a/hipparchus-core/src/main/java/org/hipparchus/util/BigReal.java +++ b/hipparchus-core/src/main/java/org/hipparchus/util/BigReal.java @@ -32,6 +32,7 @@ import org.hipparchus.FieldElement; import org.hipparchus.exception.LocalizedCoreFormats; import org.hipparchus.exception.MathRuntimeException; +import org.hipparchus.exception.NullArgumentException; /** * Arbitrary precision decimal number. @@ -292,6 +293,12 @@ public BigReal multiply(final int n) { return new BigReal(d.multiply(new BigDecimal(n))); } + /** {@inheritDoc} */ + @Override + public BigReal square() throws NullArgumentException { + return multiply(this); + } + /** {@inheritDoc} */ @Override public int compareTo(BigReal a) { diff --git a/hipparchus-core/src/main/java/org/hipparchus/util/Binary64.java b/hipparchus-core/src/main/java/org/hipparchus/util/Binary64.java index 5124e29aa..ead05ce69 100644 --- a/hipparchus-core/src/main/java/org/hipparchus/util/Binary64.java +++ b/hipparchus-core/src/main/java/org/hipparchus/util/Binary64.java @@ -131,6 +131,11 @@ public Binary64 negate() { return new Binary64(-this.value); } + @Override + public Binary64 square() { + return multiply(this); + } + /** * {@inheritDoc} * diff --git a/hipparchus-core/src/main/java/org/hipparchus/util/FieldTuple.java b/hipparchus-core/src/main/java/org/hipparchus/util/FieldTuple.java index 53f5ef612..3a38c902e 100644 --- a/hipparchus-core/src/main/java/org/hipparchus/util/FieldTuple.java +++ b/hipparchus-core/src/main/java/org/hipparchus/util/FieldTuple.java @@ -218,6 +218,12 @@ public FieldTuple multiply(final double a) { return result; } + /** {@inheritDoc} */ + @Override + public FieldTuple square() { + return multiply(this); + } + /** {@inheritDoc} */ @Override public FieldTuple divide(final double a) { diff --git a/hipparchus-core/src/main/java/org/hipparchus/util/Tuple.java b/hipparchus-core/src/main/java/org/hipparchus/util/Tuple.java index edb149cef..9cee6c538 100644 --- a/hipparchus-core/src/main/java/org/hipparchus/util/Tuple.java +++ b/hipparchus-core/src/main/java/org/hipparchus/util/Tuple.java @@ -136,6 +136,12 @@ public Tuple multiply(final int n) { return result; } + /** {@inheritDoc} */ + @Override + public Tuple square() { + return multiply(this); + } + /** {@inheritDoc} */ @Override public Tuple divide(final Tuple a) { diff --git a/hipparchus-core/src/test/java/org/hipparchus/CalculusFieldElementAbstractTest.java b/hipparchus-core/src/test/java/org/hipparchus/CalculusFieldElementAbstractTest.java index 546929cc9..738d09d68 100644 --- a/hipparchus-core/src/test/java/org/hipparchus/CalculusFieldElementAbstractTest.java +++ b/hipparchus-core/src/test/java/org/hipparchus/CalculusFieldElementAbstractTest.java @@ -101,6 +101,13 @@ public void testMultiplyInt() { } } + @Test + public void testSquare() { + for (double x = -3; x < 3; x += 0.2) { + checkRelative(x * x, build(x).square()); + } + } + @Test public void testDivideField() { for (double x = -3; x < 3; x += 0.2) { diff --git a/hipparchus-core/src/test/java/org/hipparchus/analysis/FieldFunctionsTest.java b/hipparchus-core/src/test/java/org/hipparchus/analysis/FieldFunctionsTest.java index ed0f94486..78ac44fb1 100644 --- a/hipparchus-core/src/test/java/org/hipparchus/analysis/FieldFunctionsTest.java +++ b/hipparchus-core/src/test/java/org/hipparchus/analysis/FieldFunctionsTest.java @@ -68,13 +68,13 @@ public > T[] value(T x) { T[] y = MathArrays.buildArray(x.getField(), 3); y[0] = x.add(1); y[1] = x.multiply(2); - y[2] = x.multiply(x); + y[2] = x.square(); return y; } }; CalculusFieldUnivariateVectorFunction f1Converted = f1.toCalculusFieldUnivariateVectorFunction(Binary64Field.getInstance()); CalculusFieldUnivariateVectorFunction f2 = x -> new Binary64[] { - x.add(1), x.multiply(2), x.multiply(x) + x.add(1), x.multiply(2), x.square() }; for (double x = 0; x < 1; x += 0.01) { @@ -120,7 +120,7 @@ public > T[][] value(T x) { T[][] y = MathArrays.buildArray(x.getField(), 2, 2); y[0][0] = x.add(1); y[0][1] = x.multiply(2); - y[1][0] = x.multiply(x); + y[1][0] = x.square(); y[1][1] = x.sin(); return y; } @@ -128,7 +128,7 @@ public > T[][] value(T x) { CalculusFieldUnivariateMatrixFunction f1Converted = f1.toCalculusFieldUnivariateMatrixFunction(Binary64Field.getInstance()); CalculusFieldUnivariateMatrixFunction f2 = x -> new Binary64[][] { { x.add(1), x.multiply(2) }, - { x.multiply(x), x.sin() } + { x.square(), x.sin() } }; for (double x = 0; x < 1; x += 0.01) { diff --git a/hipparchus-core/src/test/java/org/hipparchus/analysis/FunctionUtilsTest.java b/hipparchus-core/src/test/java/org/hipparchus/analysis/FunctionUtilsTest.java index 395c22d2f..732c32233 100644 --- a/hipparchus-core/src/test/java/org/hipparchus/analysis/FunctionUtilsTest.java +++ b/hipparchus-core/src/test/java/org/hipparchus/analysis/FunctionUtilsTest.java @@ -386,7 +386,7 @@ public double value(double x) { @Override public > T value(T x) { - return x.multiply(x); + return x.square(); } }; @@ -420,8 +420,8 @@ public double value(double[] point) { public DerivativeStructure value(DerivativeStructure[] point) { DerivativeStructure x = point[0]; DerivativeStructure y = point[1]; - DerivativeStructure x2 = x.multiply(x); - DerivativeStructure y2 = y.multiply(y); + DerivativeStructure x2 = x.square(); + DerivativeStructure y2 = y.square(); DerivativeStructure xy = x.multiply(y); return x2.multiply(a).add(y2.multiply(b)).add(xy.multiply(c)); } diff --git a/hipparchus-core/src/test/java/org/hipparchus/analysis/differentiation/FieldDerivativeStructureComplexTest.java b/hipparchus-core/src/test/java/org/hipparchus/analysis/differentiation/FieldDerivativeStructureComplexTest.java index 03c2efa30..1aa5107c6 100644 --- a/hipparchus-core/src/test/java/org/hipparchus/analysis/differentiation/FieldDerivativeStructureComplexTest.java +++ b/hipparchus-core/src/test/java/org/hipparchus/analysis/differentiation/FieldDerivativeStructureComplexTest.java @@ -282,7 +282,7 @@ public void testCbrt() { @Test public void testCbrtComplex() { Complex z = new Complex(15, 2); - UnitTestUtils.assertEquals(z, z.multiply(z).multiply(z).cbrt(), 1.0e-14); + UnitTestUtils.assertEquals(z, z.square().multiply(z).cbrt(), 1.0e-14); Complex branchCutPlus = new Complex(-8.0, +0.0); Complex cbrtPlus = branchCutPlus.cbrt(); UnitTestUtils.assertEquals(branchCutPlus, cbrtPlus.multiply(cbrtPlus).multiply(cbrtPlus), 1.0e-14); diff --git a/hipparchus-core/src/test/java/org/hipparchus/analysis/differentiation/FieldUnivariateDerivative1AbstractTest.java b/hipparchus-core/src/test/java/org/hipparchus/analysis/differentiation/FieldUnivariateDerivative1AbstractTest.java index 3b8c3141a..25ed809b5 100644 --- a/hipparchus-core/src/test/java/org/hipparchus/analysis/differentiation/FieldUnivariateDerivative1AbstractTest.java +++ b/hipparchus-core/src/test/java/org/hipparchus/analysis/differentiation/FieldUnivariateDerivative1AbstractTest.java @@ -86,7 +86,7 @@ public void testGetPartialDerivative() { @Test public void testGetDerivative() { FieldUnivariateDerivative1 x = build(3.0); - FieldUnivariateDerivative1 ud = x.multiply(x); + FieldUnivariateDerivative1 ud = x.square(); try { ud.getDerivative(-1); Assert.fail("an exception should have been thrown"); diff --git a/hipparchus-core/src/test/java/org/hipparchus/analysis/differentiation/FieldUnivariateDerivative2AbstractTest.java b/hipparchus-core/src/test/java/org/hipparchus/analysis/differentiation/FieldUnivariateDerivative2AbstractTest.java index cdb55045d..f70e5c4bf 100644 --- a/hipparchus-core/src/test/java/org/hipparchus/analysis/differentiation/FieldUnivariateDerivative2AbstractTest.java +++ b/hipparchus-core/src/test/java/org/hipparchus/analysis/differentiation/FieldUnivariateDerivative2AbstractTest.java @@ -108,7 +108,7 @@ public void testGetPartialDerivative() { @Test public void testGetDerivative() { FieldUnivariateDerivative2 x = build(3.0); - FieldUnivariateDerivative2 ud = x.multiply(x); + FieldUnivariateDerivative2 ud = x.square(); try { ud.getDerivative(-1); Assert.fail("an exception should have been thrown"); diff --git a/hipparchus-core/src/test/java/org/hipparchus/analysis/differentiation/GradientFunctionTest.java b/hipparchus-core/src/test/java/org/hipparchus/analysis/differentiation/GradientFunctionTest.java index db65edaae..5280d4629 100644 --- a/hipparchus-core/src/test/java/org/hipparchus/analysis/differentiation/GradientFunctionTest.java +++ b/hipparchus-core/src/test/java/org/hipparchus/analysis/differentiation/GradientFunctionTest.java @@ -75,7 +75,7 @@ public DerivativeStructure value(DerivativeStructure[] point) throws MathIllegalArgumentException { DerivativeStructure d2 = point[0].getField().getZero(); for (DerivativeStructure x : point) { - d2 = d2.add(x.multiply(x)); + d2 = d2.add(x.square()); } return d2.sqrt(); } diff --git a/hipparchus-core/src/test/java/org/hipparchus/analysis/differentiation/UnivariateDerivativeAbstractTest.java b/hipparchus-core/src/test/java/org/hipparchus/analysis/differentiation/UnivariateDerivativeAbstractTest.java index 7a41f5c29..d2abfde97 100644 --- a/hipparchus-core/src/test/java/org/hipparchus/analysis/differentiation/UnivariateDerivativeAbstractTest.java +++ b/hipparchus-core/src/test/java/org/hipparchus/analysis/differentiation/UnivariateDerivativeAbstractTest.java @@ -69,7 +69,7 @@ public void testGetPartialDerivative() { @Test public void testGetDerivative() { T x = build(3.0); - T ud = x.multiply(x); + T ud = x.square(); try { ud.getDerivative(-1); Assert.fail("an exception should have been thrown"); diff --git a/hipparchus-core/src/test/java/org/hipparchus/analysis/interpolation/AkimaSplineInterpolatorTest.java b/hipparchus-core/src/test/java/org/hipparchus/analysis/interpolation/AkimaSplineInterpolatorTest.java index 8610bd28b..f69b9def9 100644 --- a/hipparchus-core/src/test/java/org/hipparchus/analysis/interpolation/AkimaSplineInterpolatorTest.java +++ b/hipparchus-core/src/test/java/org/hipparchus/analysis/interpolation/AkimaSplineInterpolatorTest.java @@ -288,7 +288,7 @@ public void testInterpolateParabolaD64() final double interpolationTolerance = 7e-15; final double maxTolerance = 6e-14; - CalculusFieldUnivariateFunction f = x -> x.multiply(x).multiply(3). + CalculusFieldUnivariateFunction f = x -> x.square().multiply(3). subtract(x.multiply(5)). add(7); @@ -332,8 +332,8 @@ public void testInterpolateCubic64() final double interpolationTolerance = 0.37; final double maxTolerance = 3.8; - CalculusFieldUnivariateFunction f = x -> x.multiply(x).multiply(x).multiply(3). - subtract(x.multiply(x).multiply(0.5)). + CalculusFieldUnivariateFunction f = x -> x.square().multiply(x).multiply(3). + subtract(x.square().multiply(0.5)). add(x). subtract(1); diff --git a/hipparchus-core/src/test/java/org/hipparchus/analysis/interpolation/FieldHermiteInterpolatorTest.java b/hipparchus-core/src/test/java/org/hipparchus/analysis/interpolation/FieldHermiteInterpolatorTest.java index 80ae5245c..2b9224d7b 100644 --- a/hipparchus-core/src/test/java/org/hipparchus/analysis/interpolation/FieldHermiteInterpolatorTest.java +++ b/hipparchus-core/src/test/java/org/hipparchus/analysis/interpolation/FieldHermiteInterpolatorTest.java @@ -231,7 +231,7 @@ public void testWikipedia() { new BigFraction[] { new BigFraction(56) }); for (BigFraction x = new BigFraction(-1); x.doubleValue() <= 1.0; x = x.add(new BigFraction(1, 8))) { BigFraction y = interpolator.value(x)[0]; - BigFraction x2 = x.multiply(x); + BigFraction x2 = x.square(); BigFraction x4 = x2.multiply(x2); BigFraction x8 = x4.multiply(x4); Assert.assertEquals(x8.add(new BigFraction(1)), y); diff --git a/hipparchus-core/src/test/java/org/hipparchus/analysis/interpolation/PiecewiseBicubicSplineInterpolatingFunctionTest.java b/hipparchus-core/src/test/java/org/hipparchus/analysis/interpolation/PiecewiseBicubicSplineInterpolatingFunctionTest.java index 733b071a2..913d15783 100644 --- a/hipparchus-core/src/test/java/org/hipparchus/analysis/interpolation/PiecewiseBicubicSplineInterpolatingFunctionTest.java +++ b/hipparchus-core/src/test/java/org/hipparchus/analysis/interpolation/PiecewiseBicubicSplineInterpolatingFunctionTest.java @@ -179,8 +179,8 @@ public void testParabaloid() { // Function values BivariateFunction f = (x, y) -> 2 * x * x - 3 * y * y + 4 * x * y - 5; - CalculusFieldBivariateFunction fT = (x, y) -> x.multiply(x).multiply(2). - subtract(y.multiply(y).multiply(3)). + CalculusFieldBivariateFunction fT = (x, y) -> x.square().multiply(2). + subtract(y.square().multiply(3)). add(x.multiply(y).multiply(4)). subtract(5); diff --git a/hipparchus-core/src/test/java/org/hipparchus/complex/ComplexTest.java b/hipparchus-core/src/test/java/org/hipparchus/complex/ComplexTest.java index aef357012..e561e0e80 100644 --- a/hipparchus-core/src/test/java/org/hipparchus/complex/ComplexTest.java +++ b/hipparchus-core/src/test/java/org/hipparchus/complex/ComplexTest.java @@ -404,7 +404,7 @@ public void testMultiplyNaNInf() { Assert.assertTrue(Double.isNaN(w.getImaginary())); z = new Complex(1, neginf); - Assert.assertSame(Complex.INF, z.multiply(z)); + Assert.assertSame(Complex.INF, z.square()); } @Test @@ -1467,7 +1467,7 @@ public void testCbrt() { @Test public void testCbrtComplex() { Complex z = new Complex(15, 2); - UnitTestUtils.assertEquals(z, z.multiply(z).multiply(z).cbrt(), 1.0e-14); + UnitTestUtils.assertEquals(z, z.square().multiply(z).cbrt(), 1.0e-14); Complex branchCutPlus = new Complex(-8.0, +0.0); Complex cbrtPlus = branchCutPlus.cbrt(); UnitTestUtils.assertEquals(branchCutPlus, cbrtPlus.multiply(cbrtPlus).multiply(cbrtPlus), 1.0e-14); @@ -1504,7 +1504,7 @@ public void testRootN() { @Test public void testRootNComplex() { Complex z = new Complex(15, 2); - UnitTestUtils.assertEquals(z, z.multiply(z).multiply(z).rootN(3), 1.0e-14); + UnitTestUtils.assertEquals(z, z.square().multiply(z).rootN(3), 1.0e-14); Complex branchCutPlus = new Complex(-8.0, +0.0); Complex cbrtPlus = branchCutPlus.rootN(3); UnitTestUtils.assertEquals(branchCutPlus, cbrtPlus.multiply(cbrtPlus).multiply(cbrtPlus), 1.0e-14); diff --git a/hipparchus-core/src/test/java/org/hipparchus/complex/FieldComplexTest.java b/hipparchus-core/src/test/java/org/hipparchus/complex/FieldComplexTest.java index 12d89564c..17059f1a3 100644 --- a/hipparchus-core/src/test/java/org/hipparchus/complex/FieldComplexTest.java +++ b/hipparchus-core/src/test/java/org/hipparchus/complex/FieldComplexTest.java @@ -429,7 +429,7 @@ public void testMultiplyNaNInf() { Assert.assertTrue(w.getImaginaryPart().isNaN()); z = build(1, Double.NEGATIVE_INFINITY); - UnitTestUtils.assertSame(FieldComplex.getInf(Binary64Field.getInstance()), z.multiply(z)); + UnitTestUtils.assertSame(FieldComplex.getInf(Binary64Field.getInstance()), z.square()); } @Test @@ -1554,7 +1554,7 @@ public void testCbrt() { @Test public void testCbrtComplex() { FieldComplex z = build(15, 2); - UnitTestUtils.assertEquals(z, z.multiply(z).multiply(z).cbrt(), 1.0e-14); + UnitTestUtils.assertEquals(z, z.square().multiply(z).cbrt(), 1.0e-14); FieldComplex branchCutPlus = build(-8.0, +0.0); FieldComplex cbrtPlus = branchCutPlus.cbrt(); UnitTestUtils.assertEquals(branchCutPlus, cbrtPlus.multiply(cbrtPlus).multiply(cbrtPlus), 1.0e-14); @@ -1591,7 +1591,7 @@ public void testRootN() { @Test public void testRootNComplex() { FieldComplex z = build(15, 2); - UnitTestUtils.assertEquals(z, z.multiply(z).multiply(z).rootN(3), 1.0e-14); + UnitTestUtils.assertEquals(z, z.square().multiply(z).rootN(3), 1.0e-14); FieldComplex branchCutPlus = build(-8.0, +0.0); FieldComplex cbrtPlus = branchCutPlus.rootN(3); UnitTestUtils.assertEquals(branchCutPlus, cbrtPlus.multiply(cbrtPlus).multiply(cbrtPlus), 1.0e-14); diff --git a/hipparchus-core/src/test/java/org/hipparchus/special/ErfTest.java b/hipparchus-core/src/test/java/org/hipparchus/special/ErfTest.java index c087e179c..d9d56df13 100644 --- a/hipparchus-core/src/test/java/org/hipparchus/special/ErfTest.java +++ b/hipparchus-core/src/test/java/org/hipparchus/special/ErfTest.java @@ -448,7 +448,7 @@ public void testErfInv() { public void testErfInvField() { for (Binary64 x = new Binary64(-5.9); x.getReal() < 5.9; x = x.add(0.01)) { final Binary64 y = Erf.erf(x); - final Binary64 dydx = x.multiply(x).negate().exp().multiply(2/FastMath.sqrt(FastMath.PI)); + final Binary64 dydx = x.square().negate().exp().multiply(2/FastMath.sqrt(FastMath.PI)); Assert.assertEquals(x.getReal(), Erf.erfInv(y).getReal(), 1.0e-15 / dydx.getReal()); } } @@ -494,7 +494,7 @@ public void testErfcInv() { public void testErfcInvField() { for (Binary64 x = new Binary64(-5.85); x.getReal() < 5.9; x = x.add(0.01)) { final Binary64 y = Erf.erfc(x); - final Binary64 dydxAbs = x.multiply(x).negate().exp().multiply(2/FastMath.sqrt(FastMath.PI)); + final Binary64 dydxAbs = x.square().negate().exp().multiply(2/FastMath.sqrt(FastMath.PI)); Assert.assertEquals(x.getReal(), Erf.erfcInv(y).getReal(), 1.0e-15 / dydxAbs.getReal()); } } diff --git a/hipparchus-core/src/test/java/org/hipparchus/special/elliptic/carlson/CarlsonEllipticIntegralAbstractComplexTest.java b/hipparchus-core/src/test/java/org/hipparchus/special/elliptic/carlson/CarlsonEllipticIntegralAbstractComplexTest.java index 79941f4d2..7ecdcafa7 100644 --- a/hipparchus-core/src/test/java/org/hipparchus/special/elliptic/carlson/CarlsonEllipticIntegralAbstractComplexTest.java +++ b/hipparchus-core/src/test/java/org/hipparchus/special/elliptic/carlson/CarlsonEllipticIntegralAbstractComplexTest.java @@ -133,7 +133,7 @@ public void testCarlson1995ConsistencyRc() { for (int i = 0; i < 10000; ++i) { T x = buildComplex(random.nextDouble() * 3); T lambda = buildComplex(random.nextDouble() * 6 - 3, random.nextDouble() * 3); - T mu = x.multiply(x).divide(lambda); + T mu = x.square().divide(lambda); T rcL = rC(lambda, x.add(lambda)); T rcM = rC(mu, x.add(mu)); T rc0 = rC(buildComplex(0), x); diff --git a/hipparchus-core/src/test/java/org/hipparchus/special/elliptic/carlson/CarlsonEllipticIntegralFieldTest.java b/hipparchus-core/src/test/java/org/hipparchus/special/elliptic/carlson/CarlsonEllipticIntegralFieldTest.java index 78bd5719d..6e004dc4b 100644 --- a/hipparchus-core/src/test/java/org/hipparchus/special/elliptic/carlson/CarlsonEllipticIntegralFieldTest.java +++ b/hipparchus-core/src/test/java/org/hipparchus/special/elliptic/carlson/CarlsonEllipticIntegralFieldTest.java @@ -95,7 +95,7 @@ public void testCarlson1995ConsistencyRc() { for (int i = 0; i < 10000; ++i) { Binary64 x = build(random.nextDouble() * 3); Binary64 lambda = build(random.nextDouble() * 3); - Binary64 mu = x.multiply(x).divide(lambda); + Binary64 mu = x.square().divide(lambda); Binary64 rcL = CarlsonEllipticIntegral.rC(lambda, x.add(lambda)); Binary64 rcM = CarlsonEllipticIntegral.rC(mu, x.add(mu)); Binary64 rc0 = CarlsonEllipticIntegral.rC(Binary64.ZERO, x); diff --git a/hipparchus-geometry/src/main/java/org/hipparchus/geometry/euclidean/threed/FieldVector3D.java b/hipparchus-geometry/src/main/java/org/hipparchus/geometry/euclidean/threed/FieldVector3D.java index e24ac2bb7..d2b0b586b 100644 --- a/hipparchus-geometry/src/main/java/org/hipparchus/geometry/euclidean/threed/FieldVector3D.java +++ b/hipparchus-geometry/src/main/java/org/hipparchus/geometry/euclidean/threed/FieldVector3D.java @@ -465,7 +465,7 @@ public T getNorm1() { */ public T getNorm() { // there are no cancellation problems here, so we use the straightforward formula - return x.multiply(x).add(y.multiply(y)).add(z.multiply(z)).sqrt(); + return x.square().add(y.square()).add(z.square()).sqrt(); } /** Get the square of the norm for the vector. @@ -473,7 +473,7 @@ public T getNorm() { */ public T getNormSq() { // there are no cancellation problems here, so we use the straightforward formula - return x.multiply(x).add(y.multiply(y)).add(z.multiply(z)); + return x.square().add(y.square()).add(z.square()); } /** Get the L norm for the vector. @@ -647,13 +647,13 @@ public FieldVector3D orthogonal() throws MathRuntimeException { } if (FastMath.abs(x.getReal()) <= threshold) { - final T inverse = y.multiply(y).add(z.multiply(z)).sqrt().reciprocal(); + final T inverse = y.square().add(z.square()).sqrt().reciprocal(); return new FieldVector3D(inverse.getField().getZero(), inverse.multiply(z), inverse.multiply(y).negate()); } else if (FastMath.abs(y.getReal()) <= threshold) { - final T inverse = x.multiply(x).add(z.multiply(z)).sqrt().reciprocal(); + final T inverse = x.square().add(z.square()).sqrt().reciprocal(); return new FieldVector3D(inverse.multiply(z).negate(), inverse.getField().getZero(), inverse.multiply(x)); } else { - final T inverse = x.multiply(x).add(y.multiply(y)).sqrt().reciprocal(); + final T inverse = x.square().add(y.square()).sqrt().reciprocal(); return new FieldVector3D(inverse.multiply(y), inverse.multiply(x).negate(), inverse.getField().getZero()); } diff --git a/hipparchus-geometry/src/main/java/org/hipparchus/geometry/euclidean/twod/FieldVector2D.java b/hipparchus-geometry/src/main/java/org/hipparchus/geometry/euclidean/twod/FieldVector2D.java index d589010ca..e79b01755 100644 --- a/hipparchus-geometry/src/main/java/org/hipparchus/geometry/euclidean/twod/FieldVector2D.java +++ b/hipparchus-geometry/src/main/java/org/hipparchus/geometry/euclidean/twod/FieldVector2D.java @@ -389,7 +389,7 @@ public T getNorm1() { */ public T getNorm() { // there are no cancellation problems here, so we use the straightforward formula - return x.multiply(x).add(y.multiply(y)).sqrt(); + return x.square().add(y.square()).sqrt(); } /** Get the square of the norm for the vector. @@ -397,7 +397,7 @@ public T getNorm() { */ public T getNormSq() { // there are no cancellation problems here, so we use the straightforward formula - return x.multiply(x).add(y.multiply(y)); + return x.square().add(y.square()); } /** Get the L norm for the vector. diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 933763860..c68b88885 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -49,6 +49,11 @@ If the output is not quite correct, check for invisible trailing spaces! Hipparchus Release Notes + + + Add square method to FieldElement. + + Fixed internal error in RandomPercentile aggregation.