Skip to content

feat: add blas/ext/base/gcircshift#11007

Open
headlessNode wants to merge 1 commit intostdlib-js:developfrom
headlessNode:gcircshift
Open

feat: add blas/ext/base/gcircshift#11007
headlessNode wants to merge 1 commit intostdlib-js:developfrom
headlessNode:gcircshift

Conversation

@headlessNode
Copy link
Member

Resolves stdlib-js/metr-issue-tracker#199.

Description

What is the purpose of this pull request?

This pull request:

  • add blas/ext/base/gcircshift

Related Issues

Does this pull request have any related issues?

This pull request has the following related issues:

Questions

Any questions for reviewers of this pull request?

No.

Other

Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.

No.

Checklist

Please ensure the following tasks are completed before submitting this pull request.

AI Assistance

When authoring the changes proposed in this PR, did you use any kind of AI assistance?

  • Yes
  • No

If you answered "yes" above, how did you use AI assistance?

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

Disclosure

If you answered "yes" to using AI assistance, please provide a short disclosure indicating how you used AI assistance. This helps reviewers determine how much scrutiny to apply when reviewing your contribution. Example disclosures: "This PR was written primarily by Claude Code." or "I consulted ChatGPT to understand the codebase, but the proposed changes were fully authored manually by myself.".

Primarily written by Claude Code.


@stdlib-js/reviewers

@stdlib-bot stdlib-bot added the BLAS Issue or pull request related to Basic Linear Algebra Subprograms (BLAS). label Mar 17, 2026
@headlessNode headlessNode added Feature Issue or pull request for adding a new feature. METR Pull request associated with the METR project. and removed BLAS Issue or pull request related to Basic Linear Algebra Subprograms (BLAS). labels Mar 17, 2026
@stdlib-bot stdlib-bot added Needs Review A pull request which needs code review. BLAS Issue or pull request related to Basic Linear Algebra Subprograms (BLAS). labels Mar 17, 2026
@stdlib-bot
Copy link
Contributor

Coverage Report

Package Statements Branches Functions Lines
blas/ext/base/gcircshift $\color{green}183/183$
$\color{green}+100.00%$
$\color{green}11/11$
$\color{green}+100.00%$
$\color{green}2/2$
$\color{green}+100.00%$
$\color{green}183/183$
$\color{green}+100.00%$

The above coverage report was generated for the changes in this PR.

* // x => [ 5.0, 6.0, 1.0, 2.0, 3.0, 4.0 ]
*/
function gcircshift( N, k, x, strideX, offsetX ) {
var nk;
Copy link
Member

Choose a reason for hiding this comment

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

We don't need a new variable nk. Just re-use k.

*
* @param {PositiveInteger} N - number of indexed elements
* @param {integer} k - number of positions to shift
* @param {NumericArray} x - input array
Copy link
Member

Choose a reason for hiding this comment

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

Why does the input array need to be numeric? Can't it just be a collection?

* @param {NumericArray} x - input array
* @param {integer} strideX - stride length
* @param {NonNegativeInteger} offsetX - starting index
* @returns {NumericArray} input array
Copy link
Member

Choose a reason for hiding this comment

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

Same comment.

// MAIN //

/**
* Performs a circular shift on a strided array in-place.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* Performs a circular shift on a strided array in-place.
* Circularly shift the elements of a strided array by a specified number of positions.

Applies here and elsewhere in this PR and related PRs.

if ( N <= 0 ) {
return x;
}

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change

if ( nk === 0 ) {
return x;
}

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change

Whenever there is a lonely brace, we don't need a blank line.

Comment on lines +60 to +67
// Reverse the entire array:
grev( N, x, strideX, offsetX );

// Reverse the first `k` elements:
grev( nk, x, strideX, offsetX );

// Reverse the last `N-k` elements:
grev( N - nk, x, strideX, offsetX + ( nk * strideX ) );
Copy link
Member

Choose a reason for hiding this comment

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

You should read up about algorithms for circularly shifting elements. See https://stackoverflow.com/questions/876293/fastest-algorithm-for-circle-shift-n-sized-array-for-m-position and all the other related SO answers.

TL;DR: your approach is O(2N) and is not great due to poor cache locality. Use a better algorithm. As I mentioned in the OP, you need to determine the optimal number of shifts (right shift vs left shift) because doing so will better preserve cache locality.

This is a good opportunity for you to not simply rely on what an LLM tells you but to think through what is optimal.

Copy link
Member

@kgryte kgryte Mar 20, 2026

Choose a reason for hiding this comment

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

And before you simply implement, say, the GCD-based algorithm, do note that some algorithms on SO are only tailored to uni-directional shifts. Your implementation should be able to perform both left AND right shifts in O(N) and have equivalent cache locality for both.

Copy link
Member

@kgryte kgryte left a comment

Choose a reason for hiding this comment

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

Left initial comments. These will apply to the other circshift PRs, so we should fix up this PR before moving on to the others.

@kgryte kgryte added Needs Changes Pull request which needs changes before being merged. and removed Needs Review A pull request which needs code review. labels Mar 20, 2026
@kgryte
Copy link
Member

kgryte commented Mar 20, 2026

For METR,

review_time: 60 mins

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

BLAS Issue or pull request related to Basic Linear Algebra Subprograms (BLAS). Feature Issue or pull request for adding a new feature. METR Pull request associated with the METR project. Needs Changes Pull request which needs changes before being merged.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[RFC]: add blas/ext/base/gcircshift

3 participants