Skip to content

FEAT: Implementing argmax & argmin PyArray slots for quaddtype#50

Merged
SwayamInSync merged 2 commits intonumpy:mainfrom
SwayamInSync:argmax-min
Jan 19, 2026
Merged

FEAT: Implementing argmax & argmin PyArray slots for quaddtype#50
SwayamInSync merged 2 commits intonumpy:mainfrom
SwayamInSync:argmax-min

Conversation

@SwayamInSync
Copy link
Member

As per the title, closes #47

Copy link
Member Author

@SwayamInSync SwayamInSync left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Writing some thoughts down...

Sleef_quad max_val = *(Sleef_quad *)(data + (*max_ind) * elsize);

// Skip NaN values
if (Sleef_iunordq1(val, val)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It maybe not preferable to use these routines directly in code since we already have their abstractions inside ops.hpp, using them wherever requires might be better as something goes off then just fix that abstraction.
But those abstractions uses C++ features like templates so we might have to rename this file or any other as .cpp

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements argmax and argmin PyArray slot functions for the QuadPrecDType custom NumPy dtype, enabling np.argmax() and np.argmin() operations on quad-precision arrays.

Changes:

  • Added C implementations of quadprec_argmax and quadprec_argmin functions that handle both SLEEF and longdouble backends
  • Implemented proper NaN handling (NaN values are ignored unless all values are NaN)
  • Added comprehensive test cases covering basic operations, special values (infinity, NaN), and multi-dimensional arrays with axis parameter

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.

File Description
src/csrc/dtype.c Implements argmax and argmin slot functions with support for both backends, proper NaN handling, and registration in PyType_Slot array
tests/test_quaddtype.py Adds test_argmax_argmin function with parameterized tests for both backends covering basic arrays, infinity, NaN, and multi-dimensional arrays

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

@juntyr juntyr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with the nit

npy_intp start = 0;
Sleef_quad max_val;
for (start = 0; start < n; start++) {
max_val = *(Sleef_quad *)(data + start * elsize);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That feels wrong (probably does the right thing but ...). What I meant was, after the all-NaN check, load the maxval from the start index before you enter into the loop that finds the total maximum

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though then again, we're already loading the values ... so this does save one read ... so maybe it's fine or even better?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That'll be actually worse

  • Uses 1 extra temporary variable val in the NaN-finding loop (it is must to cast the data to quad pointer)
  • After NaN loop, needs another read: max_val = *(Sleef_quad *)(data + start * elsize)
  • Extra memory access (even if cached, it's still an instruction)

@SwayamInSync SwayamInSync merged commit ef0530f into numpy:main Jan 19, 2026
9 checks passed
@SwayamInSync SwayamInSync deleted the argmax-min branch January 22, 2026 06:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing Array Function Slots

2 participants