[ 
https://issues.apache.org/jira/browse/NUMBERS-66?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18074848#comment-18074848
 ] 

Alex Herbert commented on NUMBERS-66:
-------------------------------------

Complex using sin/cos on the same argument:
 * ofPolar
 * ofCis
 * exp
 * sinh
 * cosh
 * tanh
 * nthRoot

When evaluating either sin or cos the argument must be mapped to the domain [0, 
pi/2). This is then evaluated for sin or cos using the appropriate function 
(e.g. rational polynomial expansion). An optimisation is to compute the mapping 
once and reuse it for both functions.

There is a benchmark for comparing sin/cos implementations Apache Commons Math 
3.0 FastMath and java.lang.Math here:
{noformat}
org.apache.commons.numbers.examples.jmh.complex.SinCosPerformance{noformat}
It does not extract any of the FastMath code for the mapping of the argument to 
[0, pi/2). But an estimate of the cost can be made by running the benchmark 
using numbers in the correct domain [0, pi/2) or much larger.

Run on JDK Eclipse Adoptium 21.0.9 using a Mac M2 Pro for the sin method 
applied on uniform random numbers in [-range, range).
||Range||FastMath.sin||Math.sin||
|1.57079|5451.6|2901.8|
|3.14159|11713.4|3505.8|
|10|12011.5|3868.7|
|100|12567.8|4450.4|
|10000|12795.8|4411.2|
|100000000|29681.5|40920.3|
|1E+16|32835.1|43762.2|
|1E+32|34013.4|41638.4|

Note: Reduction is performed on |x| to the domain [0, pi/2). FastMath uses a 
threshold of 3294198.0 (pi * 2^20) to switch to the PayneHanek reduction. 
Otherwise it uses CodyWaite for arguments above pi.

The results here show that the JDK is faster by 2-fold for values in the 
correct domain and faster by 3-fold for values less than the PayneHanek 
reduction threshold. At larger values of |x| above the PayneHanek threshold the 
FastMath code is faster by around 25%.

Assuming the evaluation of a rational polynomial expansion for |x| in [0, pi/2) 
has a fixed runtime, the reduction of large arguments to the correct domain is 
a significant part of the computation. Any common usage of this reduction 
between sin/cos may increase performance by a factor approaching the limit of 2 
(where all computation is the reduction).

> Conversion from polar to Cartesian coordinates
> ----------------------------------------------
>
>                 Key: NUMBERS-66
>                 URL: https://issues.apache.org/jira/browse/NUMBERS-66
>             Project: Commons Numbers
>          Issue Type: Improvement
>          Components: complex
>            Reporter: Gilles Sadowski
>            Priority: Minor
>              Labels: gsoc2019, perfomance
>
> Factory methods
>  * ofPolar
>  * ofCis
> could perhaps benefit from optimizing the conversion to Cartesian "re" and 
> "im" fields: cf.
> [http://eprints.maths.manchester.ac.uk/2235/1/paper.pdf]



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to