Skip to content

Commit

Permalink
Merge pull request #282 from Serrof/issue-280
Browse files Browse the repository at this point in the history
Fix #280: add square to FiedElement
  • Loading branch information
Serrof authored Nov 5, 2023
2 parents 40472e8 + 963d985 commit 43dcbbd
Show file tree
Hide file tree
Showing 49 changed files with 227 additions and 84 deletions.
5 changes: 5 additions & 0 deletions hipparchus-core/src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ If the output is not quite correct, check for invisible trailing spaces!
<title>Hipparchus Core Release Notes</title>
</properties>
<body>
<release version="3.1" date="TBD" description="TBD.">
<action dev="serrof" type="add" issue="issues/280">
Add square method to FieldElement.
</action>
</release>
<release version="3.0" date="2023-10-08" description="This is a major release.">
<action dev="serrof" type="fix" issue="issues/275">
UnivariateDerivative now implements Comparable.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 this<sup>p</sup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ public interface FieldElement<T extends FieldElement<T>> {
*/
T multiply(T a) throws NullArgumentException;

/** Compute this &times; this.
* @return a new element representing this &times; this
* @since 3.1
*/
T square() throws NullArgumentException;

/** Compute this &divide; a.
* @param a element to divide by
* @return a new element representing this &divide; a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2508,7 +2508,7 @@ public <T extends CalculusFieldElement<T>> 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]);
Expand Down Expand Up @@ -2625,7 +2625,7 @@ public <T extends CalculusFieldElement<T>> 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]);
Expand Down Expand Up @@ -2742,7 +2742,7 @@ public <T extends CalculusFieldElement<T>> 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]);
Expand Down Expand Up @@ -3261,7 +3261,7 @@ public <T extends CalculusFieldElement<T>> 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]);
Expand Down Expand Up @@ -3378,7 +3378,7 @@ public <T extends CalculusFieldElement<T>> 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]);
Expand Down Expand Up @@ -3495,7 +3495,7 @@ public <T extends CalculusFieldElement<T>> 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]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,12 @@ public DerivativeStructure multiply(final DerivativeStructure a)
return result;
}

/** {@inheritDoc} */
@Override
public DerivativeStructure square() {
return multiply(this);
}

/** {@inheritDoc}
*/
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,12 @@ public FieldDerivativeStructure<T> multiply(final FieldDerivativeStructure<T> a)
return result;
}

/** {@inheritDoc} */
@Override
public FieldDerivativeStructure<T> square() {
return multiply(this);
}

/** '&divide;' operator.
* @param a right hand side parameter of the operator
* @return this&divide;a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,16 @@ public FieldGradient<T> multiply(final FieldGradient<T> a) {
return result;
}

/** {@inheritDoc} */
@Override
public FieldGradient<T> square() {
final FieldGradient<T> result = newInstance(value.square());
for (int i = 0; i < grad.length; ++i) {
result.grad[i] = grad[i].multiply(value).multiply(2);
}
return result;
}

/** '&divide;' operator.
* @param a right hand side parameter of the operator
* @return this&divide;a
Expand Down Expand Up @@ -580,7 +590,7 @@ public FieldGradient<T> sqrt() {
@Override
public FieldGradient<T> 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} */
Expand Down Expand Up @@ -721,25 +731,25 @@ public FieldGradient<T> tan() {
/** {@inheritDoc} */
@Override
public FieldGradient<T> 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<T> 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<T> 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<T> atan2(final FieldGradient<T> 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<T> result = newInstance(FastMath.atan2(value, x.value));
final T xValueInv = x.value.multiply(inv);
final T mValueInv = value.negate().multiply(inv);
Expand Down Expand Up @@ -784,19 +794,19 @@ public FieldGradient<T> tanh() {
/** {@inheritDoc} */
@Override
public FieldGradient<T> 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<T> 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<T> 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} */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,12 @@ public FieldUnivariateDerivative1<T> multiply(final FieldUnivariateDerivative1<T
a.f0.linearCombination(f1, a.f0, f0, a.f1));
}

/** {@inheritDoc} */
@Override
public FieldUnivariateDerivative1<T> square() {
return multiply(this);
}

/** '&divide;' operator.
* @param a right hand side parameter of the operator
* @return this&divide;a
Expand Down Expand Up @@ -441,7 +447,7 @@ public FieldUnivariateDerivative1<T> sqrt() {
@Override
public FieldUnivariateDerivative1<T> 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} */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,12 @@ public FieldUnivariateDerivative2<T> multiply(final FieldUnivariateDerivative2<T
a.f0.linearCombination(f2, a.f0, f1.add(f1), a.f1, f0, a.f2));
}

/** {@inheritDoc} */
@Override
public FieldUnivariateDerivative2<T> square() {
return multiply(this);
}

/** '&divide;' operator.
* @param a right hand side parameter of the operator
* @return this&divide;a
Expand Down Expand Up @@ -484,7 +490,7 @@ public FieldUnivariateDerivative2<T> sqrt() {
@Override
public FieldUnivariateDerivative2<T> 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());
}

Expand All @@ -498,7 +504,7 @@ public FieldUnivariateDerivative2<T> 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));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,7 +53,7 @@
* @see FieldGradient
* @since 1.7
*/
public class Gradient implements Derivative<Gradient>, CalculusFieldElement<Gradient>, Serializable {
public class Gradient implements Derivative<Gradient>, Serializable {

/** Serializable UID. */
private static final long serialVersionUID = 20200520L;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected Pair<T[], T[]> computeRule(int numberOfPoints)
final Hermite<T> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public Pair<T[], T[]> computeRule(int numberOfPoints)
final Laguerre<T> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public Pair<T[], T[]> 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;
Expand Down
Loading

0 comments on commit 43dcbbd

Please sign in to comment.