Skip to content

feat: add ndarray/base/diagonal#11851

Open
headlessNode wants to merge 4 commits intostdlib-js:developfrom
headlessNode:nd/base-diag
Open

feat: add ndarray/base/diagonal#11851
headlessNode wants to merge 4 commits intostdlib-js:developfrom
headlessNode:nd/base-diag

Conversation

@headlessNode
Copy link
Copy Markdown
Member


type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes. report:

  • task: lint_filenames status: passed
  • task: lint_editorconfig status: passed
  • task: lint_markdown status: passed
  • task: lint_package_json status: passed
  • task: lint_repl_help status: passed
  • task: lint_javascript_src status: passed
  • task: lint_javascript_cli status: na
  • task: lint_javascript_examples status: passed
  • task: lint_javascript_tests status: passed
  • task: lint_javascript_benchmarks status: passed
  • task: lint_python status: na
  • task: lint_r status: na
  • task: lint_c_src status: na
  • task: lint_c_examples status: na
  • task: lint_c_benchmarks status: na
  • task: lint_c_tests_fixtures status: na
  • task: lint_shell status: na
  • task: lint_typescript_declarations status: passed
  • task: lint_typescript_tests status: passed
  • task: lint_license_headers status: passed ---

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

Description

What is the purpose of this pull request?

This pull request:

  • add ndarray/base/diagonal

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

---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
  - task: lint_filenames
    status: passed
  - task: lint_editorconfig
    status: passed
  - task: lint_markdown
    status: passed
  - task: lint_package_json
    status: passed
  - task: lint_repl_help
    status: passed
  - task: lint_javascript_src
    status: passed
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: passed
  - task: lint_javascript_tests
    status: passed
  - task: lint_javascript_benchmarks
    status: passed
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: na
  - task: lint_c_examples
    status: na
  - task: lint_c_benchmarks
    status: na
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: passed
  - task: lint_typescript_tests
    status: passed
  - task: lint_license_headers
    status: passed
---
@headlessNode headlessNode added Feature Issue or pull request for adding a new feature. METR Pull request associated with the METR project. labels Apr 29, 2026
@stdlib-bot
Copy link
Copy Markdown
Contributor

stdlib-bot commented Apr 29, 2026

Coverage Report

Package Statements Branches Functions Lines
ndarray/base/diagonal $\color{green}165/165$
$\color{green}+0.00%$
$\color{green}11/11$
$\color{green}+0.00%$
$\color{green}1/1$
$\color{green}+0.00%$
$\color{green}165/165$
$\color{green}+0.00%$

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

@headlessNode headlessNode marked this pull request as ready for review May 1, 2026 17:24
@headlessNode headlessNode requested a review from a team May 1, 2026 17:24
@stdlib-bot stdlib-bot added the Needs Review A pull request which needs code review. label May 1, 2026
Comment thread lib/node_modules/@stdlib/ndarray/base/diagonal/examples/index.js Outdated
Comment thread lib/node_modules/@stdlib/ndarray/base/diagonal/examples/index.js Outdated
Comment thread lib/node_modules/@stdlib/ndarray/base/diagonal/examples/index.js Outdated
Comment thread lib/node_modules/@stdlib/ndarray/base/diagonal/examples/index.js Outdated
Comment thread lib/node_modules/@stdlib/ndarray/base/diagonal/README.md Outdated
Comment thread lib/node_modules/@stdlib/ndarray/base/diagonal/README.md Outdated
Comment thread lib/node_modules/@stdlib/ndarray/base/diagonal/README.md Outdated
Comment thread lib/node_modules/@stdlib/ndarray/base/diagonal/README.md Outdated
Co-authored-by: Athan <kgryte@gmail.com>
Signed-off-by: Athan <kgryte@gmail.com>
Comment thread lib/node_modules/@stdlib/ndarray/base/diagonal/examples/index.js Outdated
Comment thread lib/node_modules/@stdlib/ndarray/base/diagonal/README.md Outdated
Co-authored-by: Athan <kgryte@gmail.com>
Signed-off-by: Athan <kgryte@gmail.com>
sh = getShape( x );
st = getStrides( x );
N = sh.length;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As stated in stdlib-js/metr-issue-tracker#480 and as done in ndarray/base/rot90, it is not clear why we are not validating dims. Furthermore, it is not clear why we are not using to-unique-normalized-indices.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also similar to rot90, we should be validating that the input array has at least two dimensions given that dims must contain exactly two elements.

sr = st[ d[0] ];
sc = st[ d[1] ];

// Resolve the positive and negative parts of `k`:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It isn't clear what is meant by "positive and negative parts". What are you meaning by this?

It is better to speak about "row" and "column" offset/index. That is, after all, what we say in the docs.

Comment on lines +90 to +93
L = sh[ d[0] ] - kn;
if ( sh[ d[1] ] - kp < L ) {
L = sh[ d[1] ] - kp;
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is a bit odd. First you set it. Then, you compute the other. And then you overwrite. I believe what you are wanting is the min. I suggest being clearer in your code.

// Adjust the offset so that we point to the first element along the specified diagonal:
offset = getOffset( x ) + ( kp*sc ) + ( kn*sr );

// Drop the specified dimensions and append the diagonal dimension:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This isn't the most intuitive in the sense that the dimension is appended. This matches NumPy's logic: https://github.com/numpy/numpy/blob/main/numpy/_core/src/multiarray/item_selection.c#L2319.

And I suppose it makes sense if you think about in terms of, e.g., a physical 3D cube. If you want the stack of diagonals for the first two dimensions, you take a 2D slice and then rotate it, and have it be such that the diagonal elements are always in the last dimension.

Nothing to be be done for this comment; just trying to visualize the result.

Copy link
Copy Markdown
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.

// MODULES //

var toNormalizedIndices = require( '@stdlib/ndarray/base/to-normalized-indices' );
var getDType = require( '@stdlib/ndarray/dtype' );
Copy link
Copy Markdown
Member

@kgryte kgryte May 2, 2026

Choose a reason for hiding this comment

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

This is a "base" package. Why are you using top-level attribute packages?

* - The `writable` parameter **only** applies to ndarray constructors supporting **read-only** instances.
*
* @param {ndarray} x - input array
* @param {IntegerArray} dims - dimension indices defining the plane in which to extract the diagonal
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
* @param {IntegerArray} dims - dimension indices defining the plane in which to extract the diagonal
* @param {IntegerArray} dims - dimension indices defining the plane from which to extract the diagonal

Applies here and elsewhere in this PR.

@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 May 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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 ndarray/base/diagonal

3 participants