numpy / numpy/numpy

BUG: arctan inaccurate for small complex numbers

Open
#6,083 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
32.8k
Forks
12.8k
Avg merge
1d 7h
Merged PRs (30d)
197

Description

Consider the following examples:
The first few terms of taylor-series for arctan is given by

>>> import numpy as np
>>> def arctan_3(x):
...     return x-x**3/3+x**5/5

Then we compare the taylor-series with numpy.arctan for small complex number:

>>> x =  0.01+1je-14
>>> v1 = np.arctan(x); v1
(0.0099996666866652394+9.9920072216263095e-15j)

>>> v2 = arctan_3(x); v2
(0.009999666686666667+9.9990001e-15j)


>>> (v2-v1).imag/v2.imag
0.0006993577661521059

Lets compute the complex step derivative of arctan

>>> y = 0.01
>>> h = 1e-14
>>> np.arctan(y+1j*h).imag/h
0.99920072216263089

The exact value is

>>>1./(1+y**2) 
0.9999000099990001:

>>> arctan_3(1j*h).imag/h
0.9999000100000001

In both the examples the relative error of the complex part of numpy.arcsin function is around 1e-4.

It should be relatively easy to fix this as the complex part of arctan function is proportional to x when x is small.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the reported cases with Python and numpy.arctan, comparing the complex component against the Taylor-series results and the exact derivative. Trace the arctan implementation and its numerical tests; done means small complex inputs produce accurate imaginary parts without regressing existing arctan behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.