Skip to content

Commit 0d309eb

Browse files
committed
Added getAddendum() to CalculusFieldElement interface.
Fixes #349
1 parent a44d874 commit 0d309eb

File tree

47 files changed

+290
-46
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+290
-46
lines changed

hipparchus-clustering/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<parent>
2424
<groupId>org.hipparchus</groupId>
2525
<artifactId>hipparchus</artifactId>
26-
<version>3.2-SNAPSHOT</version>
26+
<version>4.0-SNAPSHOT</version>
2727
<relativePath>../hipparchus-parent</relativePath>
2828
</parent>
2929

hipparchus-clustering/src/changes/changes.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ If the output is not quite correct, check for invisible trailing spaces!
4949
<title>Hipparchus Clustering Release Notes</title>
5050
</properties>
5151
<body>
52-
<release version="3.2" date="TBD" description="TBD">
52+
<release version="4.0" date="TBD" description="TBD">
5353
<action dev="vincent" type="update" issue="issues/285">
5454
Migrated tests from JUnit 4 to JUnit 5
5555
</action>

hipparchus-core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<parent>
2424
<groupId>org.hipparchus</groupId>
2525
<artifactId>hipparchus</artifactId>
26-
<version>3.2-SNAPSHOT</version>
26+
<version>4.0-SNAPSHOT</version>
2727
<relativePath>../hipparchus-parent</relativePath>
2828
</parent>
2929

hipparchus-core/src/changes/changes.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ If the output is not quite correct, check for invisible trailing spaces!
4949
<title>Hipparchus Core Release Notes</title>
5050
</properties>
5151
<body>
52-
<release version="3.2" date="TBD" description="TBD">
52+
<release version="4.0" date="TBD" description="TBD">
53+
<action dev="luc" type="update" issue="issues/349">
54+
Added getAddendum() to CalculusFieldElement interface.
55+
</action>
5356
<action dev="luc" type="update" issue="issues/346">
5457
Added getLocalizedString(baseName, key, locale) with default implementation
5558
to Localizable interface

hipparchus-core/src/main/java/org/hipparchus/CalculusFieldElement.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,26 @@
3636
*/
3737
public interface CalculusFieldElement<T extends FieldElement<T>> extends FieldElement<T> {
3838

39+
/** Get the addendum to the real value of the number.
40+
* <p>
41+
* The addendum is considered to be the part that when added back to
42+
* the {@link #getReal() real part} recovers the instance. This means
43+
* that when {@code e.getReal()} is finite (i.e. neither infinite
44+
* nor NaN), then {@code e.getAddendum().add(e.getReal())} is {@code e}
45+
* and {@code e.subtract(e.getReal())} is {@code e.getAddendum()}.
46+
* Beware that for non-finite numbers, these two equalities may not hold.
47+
* The first equality (with the addition), always holds even for infinity
48+
* and NaNs if the real part is independent of the addendum (this is the
49+
* case for all derivatives types, as well as for complex and Dfp, but it
50+
* is not the case for Tuple and FieldTuple). The second equality (with
51+
* the subtraction), generally doesn't hold for non-finite numbers, because
52+
* the subtraction generates NaNs.
53+
* </p>
54+
* @return real value
55+
* @since 4.0
56+
*/
57+
T getAddendum();
58+
3959
/** Get the Archimedes constant π.
4060
* <p>
4161
* Archimedes constant is the ratio of a circle's circumference to its diameter.

hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/DerivativeStructure.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,14 @@ void setDerivativeComponent(final int index, final double value) {
155155
return data[index];
156156
}
157157

158+
/** {@inheritDoc} */
159+
@Override
160+
public DerivativeStructure getAddendum() {
161+
final double[] addendum = data.clone();
162+
addendum[0] = 0;
163+
return new DerivativeStructure(factory, addendum);
164+
}
165+
158166
/** Get the value part of the derivative structure.
159167
* @return value part of the derivative structure
160168
* @see #getPartialDerivative(int...)

hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/FieldDerivativeStructure.java

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import org.hipparchus.util.MathArrays;
2626
import org.hipparchus.util.MathUtils;
2727

28+
import java.util.Arrays;
29+
2830
/** Class representing both the value and the differentials of a function.
2931
* <p>This class is similar to {@link DerivativeStructure} except function
3032
* parameters and value can be any {@link CalculusFieldElement}.</p>
@@ -89,14 +91,14 @@ public FDSFactory<T> getFactory() {
8991
return factory;
9092
}
9193

92-
@Override
9394
/** {@inheritDoc} */
95+
@Override
9496
public int getFreeParameters() {
9597
return getFactory().getCompiler().getFreeParameters();
9698
}
9799

98-
@Override
99100
/** {@inheritDoc} */
101+
@Override
100102
public int getOrder() {
101103
return getFactory().getCompiler().getOrder();
102104
}
@@ -131,6 +133,14 @@ T getDerivativeComponent(final int index) {
131133
return data[index];
132134
}
133135

136+
/** {@inheritDoc} */
137+
@Override
138+
public FieldDerivativeStructure<T> getAddendum() {
139+
final T[] addendum = data.clone();
140+
addendum[0] = addendum[0].getField().getZero();
141+
return new FieldDerivativeStructure<>(factory, addendum);
142+
}
143+
134144
/** Get the value part of the derivative structure.
135145
* @return value part of the derivative structure
136146
* @see #getPartialDerivative(int...)
@@ -1301,4 +1311,41 @@ public FieldDerivativeStructure<T> getPi() {
13011311
return factory.getDerivativeField().getPi();
13021312
}
13031313

1314+
/**
1315+
* Test for the equality of two derivative structures.
1316+
* <p>
1317+
* Derivative structures are considered equal if they have the same number
1318+
* of free parameters, the same derivation order, and the same derivatives.
1319+
* </p>
1320+
* @param other Object to test for equality to this
1321+
* @return true if two derivative structures are equal
1322+
*/
1323+
@Override
1324+
public boolean equals(Object other) {
1325+
1326+
if (this == other) {
1327+
return true;
1328+
}
1329+
1330+
if (other instanceof FieldDerivativeStructure &&
1331+
((FieldDerivativeStructure<?>) other).getField().equals(getField())) {
1332+
final FieldDerivativeStructure<T> rhs = (FieldDerivativeStructure<T>) other;
1333+
return (getFreeParameters() == rhs.getFreeParameters()) &&
1334+
(getOrder() == rhs.getOrder()) &&
1335+
MathArrays.equals(data, rhs.data);
1336+
}
1337+
1338+
return false;
1339+
1340+
}
1341+
1342+
/**
1343+
* Get a hashCode for the derivative structure.
1344+
* @return a hash code value for this object
1345+
*/
1346+
@Override
1347+
public int hashCode() {
1348+
return 227 + 229 * getFreeParameters() + 233 * getOrder() + 239 * Arrays.hashCode(data);
1349+
}
1350+
13041351
}

hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/FieldGradient.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@ public FieldGradient<T> withValue(final T v) {
149149
return new FieldGradient<>(v, grad);
150150
}
151151

152+
/** {@inheritDoc} */
153+
@Override
154+
public FieldGradient<T> getAddendum() {
155+
return new FieldGradient<>(value.getField().getZero(), grad);
156+
}
157+
152158
/** Get the value part of the function.
153159
* @return value part of the value of the function
154160
*/

hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/FieldUnivariateDerivative1.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ public FieldUnivariateDerivative1<T> withValue(final T value) {
101101
return new FieldUnivariateDerivative1<>(value, f1);
102102
}
103103

104+
/** {@inheritDoc} */
105+
@Override
106+
public FieldUnivariateDerivative1<T> getAddendum() {
107+
return new FieldUnivariateDerivative1<>(f0.getField().getZero(), f1);
108+
}
109+
104110
/** Get the value part of the univariate derivative.
105111
* @return value part of the univariate derivative
106112
*/

hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/FieldUnivariateDerivative2.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ public FieldUnivariateDerivative2<T> withValue(final T value) {
108108
return new FieldUnivariateDerivative2<>(value, f1, f2);
109109
}
110110

111+
/** {@inheritDoc} */
112+
@Override
113+
public FieldUnivariateDerivative2<T> getAddendum() {
114+
return new FieldUnivariateDerivative2<>(f0.getField().getZero(), f1, f2);
115+
}
116+
111117
/** Get the value part of the univariate derivative.
112118
* @return value part of the univariate derivative
113119
*/

hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/Gradient.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ public Gradient withValue(final double v) {
141141
return new Gradient(v, grad);
142142
}
143143

144+
/** {@inheritDoc} */
145+
@Override
146+
public Gradient getAddendum() {
147+
return new Gradient(0, grad);
148+
}
149+
144150
/** Get the value part of the function.
145151
* @return value part of the value of the function
146152
*/

hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/SparseGradient.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ public double getDerivative(final int index) {
148148
return (out == null) ? 0.0 : out;
149149
}
150150

151+
/** {@inheritDoc} */
152+
@Override
153+
public SparseGradient getAddendum() {
154+
return new SparseGradient(0, derivatives);
155+
}
156+
151157
/**
152158
* Get the value of the function.
153159
* @return value of the function.

hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/UnivariateDerivative1.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ public UnivariateDerivative1 withValue(final double value) {
9797
return new UnivariateDerivative1(value, f1);
9898
}
9999

100+
/** {@inheritDoc} */
101+
@Override
102+
public UnivariateDerivative1 getAddendum() {
103+
return new UnivariateDerivative1(0, f1);
104+
}
105+
100106
/** {@inheritDoc} */
101107
@Override
102108
public double getValue() {

hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation/UnivariateDerivative2.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ public UnivariateDerivative2 withValue(final double value) {
105105
return new UnivariateDerivative2(value, f1, f2);
106106
}
107107

108+
/** {@inheritDoc} */
109+
@Override
110+
public UnivariateDerivative2 getAddendum() {
111+
return new UnivariateDerivative2(0, f1, f2);
112+
}
113+
108114
/** {@inheritDoc} */
109115
@Override
110116
public double getValue() {

hipparchus-core/src/main/java/org/hipparchus/complex/Complex.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,12 @@ public double getReal() {
506506
return real;
507507
}
508508

509+
/** {@inheritDoc} */
510+
@Override
511+
public Complex getAddendum() {
512+
return new Complex(0, imaginary);
513+
}
514+
509515
/**
510516
* Access the real part.
511517
*
@@ -825,7 +831,7 @@ public Complex atan() {
825831
* <pre>
826832
* Examples:
827833
* <code>
828-
* cos(1 &plusmn; INFINITY i) = 1 \u2213 INFINITY i
834+
* cos(1 &plusmn; INFINITY i) = 1 INFINITY i
829835
* cos(&plusmn;INFINITY + i) = NaN + NaN i
830836
* cos(&plusmn;INFINITY &plusmn; INFINITY i) = NaN + NaN i
831837
* </code>

hipparchus-core/src/main/java/org/hipparchus/complex/FieldComplex.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,12 @@ public double getReal() {
580580
return real.getReal();
581581
}
582582

583+
/** {@inheritDoc} */
584+
@Override
585+
public FieldComplex<T> getAddendum() {
586+
return new FieldComplex<>(real.getAddendum(), imaginary);
587+
}
588+
583589
/**
584590
* Access the real part.
585591
*
@@ -933,7 +939,7 @@ public FieldComplex<T> atan() {
933939
* <pre>
934940
* Examples:
935941
* <code>
936-
* cos(1 &plusmn; INFINITY i) = 1 \u2213 INFINITY i
942+
* cos(1 &plusmn; INFINITY i) = 1 INFINITY i
937943
* cos(&plusmn;INFINITY + i) = NaN + NaN i
938944
* cos(&plusmn;INFINITY &plusmn; INFINITY i) = NaN + NaN i
939945
* </code>

hipparchus-core/src/main/java/org/hipparchus/dfp/Dfp.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ protected Dfp(final DfpField field, double x) {
312312
}
313313

314314
Dfp xdfp = new Dfp(field, mantissa);
315-
xdfp = xdfp.divide(new Dfp(field, 4503599627370496l)).add(field.getOne()); // Divide by 2^52, then add one
315+
xdfp = xdfp.divide(new Dfp(field, 4503599627370496L)).add(field.getOne()); // Divide by 2^52, then add one
316316
xdfp = xdfp.multiply(DfpMath.pow(field.getTwo(), exponent));
317317

318318
if ((bits & 0x8000000000000000L) != 0) {
@@ -1285,7 +1285,7 @@ protected Dfp trunc(final DfpField.RoundingMode rmode) {
12851285
result = result.add(a);
12861286
}
12871287

1288-
/** If exactly equal to 1/2 and odd then increment */
1288+
// If exactly equal to 1/2 and odd then increment
12891289
if (a.equals(half) && result.exp > 0 && (result.mant[mant.length-result.exp]&1) != 0) {
12901290
a = newInstance(getOne());
12911291
a.sign = sign;
@@ -2632,7 +2632,7 @@ public double toDouble() {
26322632
}
26332633

26342634

2635-
y = y.multiply(newInstance(4503599627370496l)).rint();
2635+
y = y.multiply(newInstance(4503599627370496L)).rint();
26362636
String str = y.toString();
26372637
str = str.substring(0, str.length()-1);
26382638
long mantissa = Long.parseLong(str);
@@ -2685,6 +2685,12 @@ public double getReal() {
26852685
return toDouble();
26862686
}
26872687

2688+
/** {@inheritDoc} */
2689+
@Override
2690+
public Dfp getAddendum() {
2691+
return isFinite() ? subtract(getReal()) : getZero();
2692+
}
2693+
26882694
/** {@inheritDoc}
26892695
*/
26902696
@Override

hipparchus-core/src/main/java/org/hipparchus/util/Binary64.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,12 @@ public double getReal() {
347347
return value;
348348
}
349349

350+
/** {@inheritDoc} */
351+
@Override
352+
public Binary64 getAddendum() {
353+
return Binary64.ZERO;
354+
}
355+
350356
/** {@inheritDoc} */
351357
@Override
352358
public Binary64 add(final double a) {

hipparchus-core/src/main/java/org/hipparchus/util/FieldTuple.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,17 @@ public double getReal() {
185185
return values[0].getReal();
186186
}
187187

188+
/** {@inheritDoc} */
189+
@Override
190+
public FieldTuple<T> getAddendum() {
191+
final T[] addendum = values.clone();
192+
addendum[0] = addendum[0].getField().getZero();
193+
for (int i = 1; i < addendum.length; ++i) {
194+
addendum[i] = addendum[i].subtract(values[0]);
195+
}
196+
return new FieldTuple<>(field, addendum);
197+
}
198+
188199
/** {@inheritDoc} */
189200
@Override
190201
public FieldTuple<T> add(final double a) {

0 commit comments

Comments
 (0)