Skip to content

Commit

Permalink
Fixed {Field}Complex.atan for real values.
Browse files Browse the repository at this point in the history
Fixes #351
  • Loading branch information
maisonobe committed Aug 22, 2024
1 parent e6a137b commit 1a9b2e5
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 0 deletions.
3 changes: 3 additions & 0 deletions hipparchus-core/src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ If the output is not quite correct, check for invisible trailing spaces!
</properties>
<body>
<release version="4.0" date="TBD" description="TBD">
<action dev="luc" type="fix" issue="issues/351">
Fixed {Field}Complex.atan for real values.
</action>
<action dev="luc" type="update" issue="issues/349">
Added getAddendum() to CalculusFieldElement interface.
</action>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,9 @@ public Complex atan() {
final Complex tmp = createComplex((1 + imaginary) / (1 - imaginary), 0.0).log().multiplyPlusI().multiply(0.5);
return createComplex(FastMath.copySign(tmp.real, real), tmp.imaginary);

} else if (imaginary == 0.0) {
// taking care to preserve the sign of the zero imaginary part
return createComplex(FastMath.atan(real), imaginary);
} else {
// regular formula
final Complex n = createComplex(1 + imaginary, -real);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,9 @@ public FieldComplex<T> atan() {
log().multiplyPlusI().multiply(0.5);
return createComplex(FastMath.copySign(tmp.real, real), tmp.imaginary);

} else if (imaginary.isZero()) {
// taking care to preserve the sign of the zero imaginary part
return createComplex(FastMath.atan(real), imaginary);
} else {
// regular formula
final FieldComplex<T> n = createComplex(one.add(imaginary), real.negate());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.hipparchus.util.FastMath;
import org.hipparchus.util.MathUtils;
import org.hipparchus.util.Precision;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
Expand Down Expand Up @@ -860,6 +861,18 @@ void testAtanBranchCuts() {
assertTrue(FastMath.copySign(1.0, FastMath.atan(new Complex(-0.0, -0.25)).getReal()) < 0.0);
}

@Test
public void testAtanReal() {
final Complex zP = new Complex(0.8734729023516287, 0.0);
final Complex aP = new Complex(0.717964439926383, 0.0);
Assertions.assertEquals(aP, zP.atan());
Assertions.assertEquals(1.0, FastMath.copySign(1.0, zP.atan().getImaginary()), 1.0e-15);
final Complex zM = new Complex(0.8734729023516287, -0.0);
final Complex aM = new Complex(0.717964439926383, -0.0);
Assertions.assertEquals(aM, zM.atan());
Assertions.assertEquals(-1.0, FastMath.copySign(1.0, zM.atan().getImaginary()), 1.0e-15);
}

@Test
@Override
public void testAtan2() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.hipparchus.util.MathArrays;
import org.hipparchus.util.MathUtils;
import org.hipparchus.util.Precision;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
Expand Down Expand Up @@ -915,6 +916,18 @@ void testAtanBranchCuts() {
assertTrue(FastMath.copySign(1.0, FastMath.atan(build(-0.0, -0.25)).getReal()) < 0.0);
}

@Test
public void testAtanReal() {
final FieldComplex<Binary64> zP = build(0.8734729023516287, 0.0);
final FieldComplex<Binary64> aP = build(0.717964439926383, 0.0);
Assertions.assertEquals(aP, zP.atan());
Assertions.assertEquals(1.0, FastMath.copySign(new Binary64(1.0), zP.atan().getImaginary()).getReal(), 1.0e-15);
final FieldComplex<Binary64> zM = build(0.8734729023516287, -0.0);
final FieldComplex<Binary64> aM = build(0.717964439926383, -0.0);
Assertions.assertEquals(aM, zM.atan());
Assertions.assertEquals(-1.0, FastMath.copySign(new Binary64(1.0), zM.atan().getImaginary()).getReal(), 1.0e-15);
}

@Test
@Override
public void testAtan2() {
Expand Down

0 comments on commit 1a9b2e5

Please sign in to comment.