Skip to content

Commit 7101d28

Browse files
authored
Add convergence checker provider for Fielded scalar (#447)
1 parent 5726033 commit 7101d28

9 files changed

Lines changed: 105 additions & 31 deletions

File tree

hipparchus-optim/src/changes/changes.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ If the output is not quite correct, check for invisible trailing spaces!
4949
<title>Hipparchus Optim Release Notes</title>
5050
</properties>
5151
<body>
52+
<release version="4.1" date="TBD" description="TBD.">
53+
<action dev="serror" type="add" issue="issues/446">
54+
Add convergence checker provider for Fielded scalar.
55+
</action>
56+
</release>
5257
<release version="4.0.2" date="2025-09-08" description="This is a patch release.">
5358
<action dev="bryan" type="update">
5459
No changes directly in this module. However, lower level Hipparchus modules did change,

hipparchus-optim/src/main/java/org/hipparchus/optim/AbstractConvergenceChecker.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,4 @@ public double getRelativeThreshold() {
6363
public double getAbsoluteThreshold() {
6464
return absoluteThreshold;
6565
}
66-
67-
/**
68-
* {@inheritDoc}
69-
*/
70-
@Override
71-
public abstract boolean converged(int iteration,
72-
P previous,
73-
P current);
7466
}

hipparchus-optim/src/main/java/org/hipparchus/optim/ConvergenceChecker.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@
4646
* @see org.hipparchus.optim.SimpleVectorValueChecker
4747
*
4848
*/
49+
@FunctionalInterface
4950
public interface ConvergenceChecker<P> {
51+
5052
/**
5153
* Check if the optimization algorithm has converged.
5254
*

hipparchus-optim/src/main/java/org/hipparchus/optim/ConvergenceCheckerAndMultiplexer.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,7 @@ public ConvergenceCheckerAndMultiplexer(final List<ConvergenceChecker<P>> checke
4343
/** {@inheritDoc} */
4444
@Override
4545
public boolean converged(final int iteration, final P previous, final P current) {
46-
for (final ConvergenceChecker<P> checker : checkers) {
47-
if (!checker.converged(iteration, previous, current)) {
48-
return false;
49-
}
50-
}
51-
return true;
46+
return checkers.stream().allMatch(checker -> checker.converged(iteration, previous, current));
5247
}
5348

5449
}

hipparchus-optim/src/main/java/org/hipparchus/optim/ConvergenceCheckerOrMultiplexer.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,7 @@ public ConvergenceCheckerOrMultiplexer(final List<ConvergenceChecker<P>> checker
4343
/** {@inheritDoc} */
4444
@Override
4545
public boolean converged(final int iteration, final P previous, final P current) {
46-
for (final ConvergenceChecker<P> checker : checkers) {
47-
if (checker.converged(iteration, previous, current)) {
48-
return true;
49-
}
50-
}
51-
return false;
46+
return checkers.stream().anyMatch(checker -> checker.converged(iteration, previous, current));
5247
}
5348

5449
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/*
19+
* This is not the original file distributed by the Apache Software Foundation
20+
* It has been modified by the Hipparchus project
21+
*/
22+
23+
package org.hipparchus.optim;
24+
25+
import org.hipparchus.CalculusFieldElement;
26+
import org.hipparchus.Field;
27+
28+
/**
29+
* Interface providing convergence checkers for scalar field.
30+
*
31+
* @see ConvergenceChecker
32+
* @since 4.1
33+
*/
34+
public interface FieldScalarConvergenceCheckerProvider {
35+
36+
/**
37+
* Method returning a checker for given field.
38+
*
39+
* @param field calculus field
40+
* @return convergence checker for input field
41+
*/
42+
<T extends CalculusFieldElement<T>> ConvergenceChecker<T> getChecker(Field<T> field);
43+
}

hipparchus-optim/src/main/java/org/hipparchus/optim/nonlinear/vector/leastsquares/LeastSquaresFactory.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,7 @@ public Evaluation evaluate(final RealVector point) {
249249
* @return a convergence checker that delegates to {@code checker}.
250250
*/
251251
public static ConvergenceChecker<Evaluation> evaluationChecker(final ConvergenceChecker<PointVectorValuePair> checker) {
252-
return new ConvergenceChecker<Evaluation>() {
253-
/** {@inheritDoc} */
254-
@Override
255-
public boolean converged(final int iteration,
256-
final Evaluation previous,
257-
final Evaluation current) {
258-
return checker.converged(
252+
return (iteration, previous, current) -> checker.converged(
259253
iteration,
260254
new PointVectorValuePair(
261255
previous.getPoint().toArray(),
@@ -264,10 +258,7 @@ public boolean converged(final int iteration,
264258
new PointVectorValuePair(
265259
current.getPoint().toArray(),
266260
current.getResiduals().toArray(),
267-
false)
268-
);
269-
}
270-
};
261+
false));
271262
}
272263

273264
/**
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/*
19+
* This is not the original file distributed by the Apache Software Foundation
20+
* It has been modified by the Hipparchus project
21+
*/
22+
package org.hipparchus.optim;
23+
24+
import org.hipparchus.CalculusFieldElement;
25+
import org.hipparchus.Field;
26+
import org.hipparchus.util.Binary64Field;
27+
import org.junit.jupiter.params.ParameterizedTest;
28+
import org.junit.jupiter.params.provider.ValueSource;
29+
import static org.junit.jupiter.api.Assertions.assertEquals;
30+
31+
class FieldScalarConvergenceCheckerProviderTest {
32+
33+
@ParameterizedTest
34+
@ValueSource(booleans = {true, false})
35+
void testAlwaysSame(final boolean value) {
36+
// GIVEN
37+
final Binary64Field field = Binary64Field.getInstance();
38+
// WHEN
39+
final FieldScalarConvergenceCheckerProvider checkerProvider = new FieldScalarConvergenceCheckerProvider() {
40+
@Override
41+
public <T extends CalculusFieldElement<T>> ConvergenceChecker<T> getChecker(Field<T> field) {
42+
return (iteration, previous, current) -> value;
43+
}
44+
};
45+
// THEN
46+
assertEquals(value, checkerProvider.getChecker(field).converged(0, field.getZero(), field.getOne()));
47+
}
48+
}

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ If the output is not quite correct, check for invisible trailing spaces!
5050
</properties>
5151
<body>
5252
<release version="4.1" date="TBD" description="TBD">
53+
<action dev="serror" type="add" issue="issues/446">
54+
Add convergence checker provider for Fielded scalar.
55+
</action>
5356
<action dev="axkr" type="add" issue="issues/442">
5457
Added Complex Schur transformer
5558
</action>

0 commit comments

Comments
 (0)