Skip to content

Commit 3a25a68

Browse files
authored
Merge pull request #371 from Serrof/issue-370
add native way to stack an independent variable in (Field)Gradient
2 parents 70fc710 + ce45b5b commit 3a25a68

File tree

6 files changed

+84
-1
lines changed

6 files changed

+84
-1
lines changed

hipparchus-core/src/changes/changes.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ If the output is not quite correct, check for invisible trailing spaces!
5050
</properties>
5151
<body>
5252
<release version="4.0" date="TBD" description="TBD">
53-
<action dev="luc" type="fix" issue="issuers/360">
53+
<action dev="serrof" type="add" issue="issues/370">
54+
Added native way to stack an independent variable in (Field)Gradient.
55+
</action>
56+
<action dev="luc" type="fix" issue="issues/360">
5457
Increased dimension of directions numbers in Sobol sequence generation.
5558
</action>
5659
<action dev="luc" type="fix" issue="issues/354">

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,17 @@ public FieldGradient<T> linearCombination(final double a1, final FieldGradient<T
877877
return result;
878878
}
879879

880+
/**
881+
* Add an independent variable to the Taylor expansion.
882+
* @return object with one more variable
883+
* @since 13.0
884+
*/
885+
public FieldGradient<T> stackVariable() {
886+
final T[] gradient = MathArrays.buildArray(getValueField(), this.getFreeParameters() + 1);
887+
System.arraycopy(this.grad, 0, gradient, 0, this.getFreeParameters());
888+
return new FieldGradient<>(this.value, gradient);
889+
}
890+
880891
/** {@inheritDoc} */
881892
@Override
882893
public FieldGradient<T> getPi() {

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,17 @@ public Gradient linearCombination(final double a1, final Gradient b1,
712712
return result;
713713
}
714714

715+
/**
716+
* Add an independent variable to the Taylor expansion.
717+
* @return object with one more variable
718+
* @since 13.0
719+
*/
720+
public Gradient stackVariable() {
721+
final double[] gradient = new double[this.getFreeParameters() + 1];
722+
System.arraycopy(this.grad, 0, gradient, 0, this.getFreeParameters());
723+
return new Gradient(gradient, this.getValue());
724+
}
725+
715726
/** Test for the equality of two univariate derivatives.
716727
* <p>
717728
* univariate derivatives are considered equal if they have the same derivatives.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Licensed to the Hipparchus project 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 Hipparchus project 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+
package org.hipparchus.analysis.differentiation;
18+
19+
import org.hipparchus.complex.Complex;
20+
import org.junit.jupiter.api.Assertions;
21+
import org.junit.jupiter.api.Test;
22+
23+
import java.util.Arrays;
24+
25+
class FieldGradientTest {
26+
27+
@Test
28+
void testStackVariable() {
29+
// GIVEN
30+
final FieldGradient<Complex> gradient = new FieldGradient<>(Complex.ONE, new Complex(2));
31+
// WHEN
32+
final FieldGradient<Complex> gradientWithMoreVariable = gradient.stackVariable();
33+
// THEN
34+
Assertions.assertEquals(gradient.getValue(), gradientWithMoreVariable.getValue());
35+
Assertions.assertEquals(gradient.getFreeParameters() + 1, gradientWithMoreVariable.getFreeParameters());
36+
Assertions.assertArrayEquals(gradient.getGradient(), Arrays.copyOfRange(gradientWithMoreVariable.getGradient(),
37+
0, gradient.getFreeParameters()));
38+
}
39+
}

hipparchus-core/src/test/java/org/hipparchus/analysis/differentiation/GradientTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@
2626
import org.hipparchus.util.FastMath;
2727
import org.hipparchus.util.FieldSinCos;
2828
import org.hipparchus.util.MathArrays;
29+
import org.junit.jupiter.api.Assertions;
2930
import org.junit.jupiter.api.Test;
3031

32+
import java.util.Arrays;
33+
3134
import static org.junit.jupiter.api.Assertions.assertEquals;
3235
import static org.junit.jupiter.api.Assertions.assertNotEquals;
3336
import static org.junit.jupiter.api.Assertions.assertNotSame;
@@ -91,6 +94,19 @@ void testVariable() {
9194
}
9295
}
9396

97+
@Test
98+
void testStackVariable() {
99+
// GIVEN
100+
final Gradient gradient = new Gradient(1, 2, 3);
101+
// WHEN
102+
final Gradient gradientWithMoreVariable = gradient.stackVariable();
103+
// THEN
104+
Assertions.assertEquals(gradient.getValue(), gradientWithMoreVariable.getValue());
105+
Assertions.assertEquals(gradient.getFreeParameters() + 1, gradientWithMoreVariable.getFreeParameters());
106+
Assertions.assertArrayEquals(gradient.getGradient(), Arrays.copyOfRange(gradientWithMoreVariable.getGradient(),
107+
0, gradient.getFreeParameters()));
108+
}
109+
94110
@Test
95111
void testDoublePow() {
96112
assertSame(build(3).getField().getZero(), Gradient.pow(0.0, build(1.5)));

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.0" date="TBD" description="TBD">
53+
<action dev="serrof" type="add" issue="issues/370">
54+
Added native way to stack an independent variable in (Field)Gradient.
55+
</action>
5356
<action dev="luc" type="fix" issue="issues/368">
5457
Added point, hyperplane and sub-hyperplane types to geometry classes parameters
5558
</action>

0 commit comments

Comments
 (0)