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
*/

0 commit comments

Comments
 (0)