numpy / numpy/numpy-simd-routines
Maybe small improvement for accuracy for cos for regular angles
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 10
- Forks
- 2
- PR merge metrics
- No merged PRs in 30d
Description
@seiko2plus - Came here out of curiosity from https://github.com/numpy/numpy/pull/29699#issuecomment-3630501457, to see how things get done. For cos, the following transformation is done before any real calculation:
https://github.com/numpy/numpy-simd-routines/blob/1acd14571b9f58072d1f7f17886945000d78b56a/npsr/trig/high-inl.h#L35-L43
This would seem to loose some precision for the common case where angle input will be between -pi and pi, by moving those angles to pi/2 to 3*pi/2. Instead, one can move those angles to near zero and thus get optimal precision near both crossings by using that cos(x) = sin(pi/2 - |x|) , i.e. do something like,
// Transform cosine to sine using identity cos(x) = sin(π/2 - |x|)
// where |x| ensures optimal precision near both -pi/2 and pi/2.
const V half_pi = Set(d, data::kHalfPi<T>);
V x_trans = x;
if constexpr (OP == Operation::kCos) {
x_trans = And(abs_mask, x);
x_trans = Sub(half_pi, x_trans);
}
const V x_abs = And(abs_mask, x_trans);
const V x_sign = AndNot(x_abs, x_trans);
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in npsr/trig/high-inl.h at lines 35–43 and trace the cosine preprocessing path in the SIMD routine. Compare the existing transformation with the proposed cos(x) = sin(π/2 - |x|) approach, focusing on inputs between -pi and pi. Done means the cosine path uses the accuracy-preserving transformation without regressing the surrounding sine/cosine handling.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- performance
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100