Skip to content

feat: add C implementation for blas/ext/base/ndarray/ztril - #16063

Open
yb175 wants to merge 10 commits into
stdlib-js:developfrom
yb175:feat/ndarray-ztril-c
Open

yb175 wants to merge 10 commits into
stdlib-js:developfrom
yb175:feat/ndarray-ztril-c

Conversation

@yb175

@yb175 yb175 commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Related to #14819.

Description

What is the purpose of this pull request?

This pull request:

  • Adds a native C ndarray implementation for blas/ext/base/ndarray/ztril, using the existing strided C routine.
  • Adds the Node.js addon and uses the native implementation when available, with the JavaScript implementation as a fallback.
  • Adds native tests, benchmarks, a C example, and C API documentation.

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?

The native JS wrapper extracts k from the generic scalar ndarray and passes a float64 scalar ndarray to C, so the existing JS example works. For direct C callers, should stdlib_blas_ext_ztril require a 0-D float64 ndarray for k, or support other numeric dtypes? Since the C function returns void, how would you prefer it to handle an unsupported dtype or unreadable scalar?

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.".

I used codex for tests generation and documentation writing , all the changes made by ai are reviewed carefully


@stdlib-js/reviewers

@yb175
yb175 requested a review from a team October 11, 2026 13:20
@stdlib-bot

stdlib-bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Coverage Report

Package Statements Branches Functions Lines
blas/ext/base/ndarray/ztril $\\color{green}227/227$
$\\color{green}+0.00\\%$
$\\color{green}7/7$
$\\color{green}+0.00\\%$
$\\color{green}2/2$
$\\color{green}+0.00\\%$
$\\color{green}227/227$
$\\color{green}+0.00\\%$

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

@stdlib-bot stdlib-bot added BLAS Issue or pull request related to Basic Linear Algebra Subprograms (BLAS). Needs Review A pull request which needs code review. labels Oct 11, 2026
Comment on lines +201 to +205
<section class="notes">

- Elements outside of the copied region are left unchanged.

</section>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
<section class="notes">
- Elements outside of the copied region are left unchanged.
</section>
<section class="notes">
### Notes
- Elements outside of the copied region are left unchanged.
</section>

Whenever <section class="notes"> in the C APIs section contains text, it must include a ### Notes heading right after opening the tag (see e.g. znancount/README.md and zany/README.md).

Comment on lines +171 to +180
stdlib_blas_ext_ztril( arrays );
}
elapsed = tic() - t;
for ( j = 0; j < len; j++ ) {
parts.value = dataB[ j ];
if ( isnan( parts.parts[ 0 ] ) || isnan( parts.parts[ 1 ] ) ) {
fprintf( stderr, "should not return NaN\n" );
break;
}
}

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 not what is desired. You a) need to retain an assertion check within the hot loop and b) should be using functional accessors, such as complex/float64/real and complex/float64/imag.

@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 Oct 13, 2026
AryanSharma48

This comment was marked as outdated.

@yb175

yb175 commented Oct 14, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback. I added the ### Notes heading and moved the NaN check into the benchmark’s hot loop, using the complex/float64/real and complex/float64/imag accessors. Please let me know if anything else is needed!

@yb175

yb175 commented Oct 14, 2026

Copy link
Copy Markdown
Contributor Author

while reviewing the implementation I made few changes following merged PRs convention

  • switched k to an int32 ndarray throughout the package. following the discussion in feat: add C implementation blas/ext/base/ndarray/dtril #14471
  • updated C examples to use stlib_complex128_t buffers
  • added browser fallback in package.json
  • Made the C benchmark report a TAP failure if NaN is detected
  • updated the native Js benchmark check both real and imaginary components

Comment thread lib/node_modules/@stdlib/blas/ext/base/ndarray/ztril/benchmark/benchmark.js Outdated
Comment thread lib/node_modules/@stdlib/blas/ext/base/ndarray/ztril/docs/types/index.d.ts Outdated
Comment thread lib/node_modules/@stdlib/blas/ext/base/ndarray/ztril/docs/repl.txt Outdated
Comment thread lib/node_modules/@stdlib/blas/ext/base/ndarray/ztril/examples/c/example.c Outdated
Comment thread lib/node_modules/@stdlib/blas/ext/base/ndarray/ztril/lib/main.js Outdated
Comment thread lib/node_modules/@stdlib/blas/ext/base/ndarray/ztril/lib/native.js Outdated
Comment thread lib/node_modules/@stdlib/blas/ext/base/ndarray/ztril/src/main.c Outdated
Comment thread lib/node_modules/@stdlib/blas/ext/base/ndarray/ztril/README.md Outdated
Comment thread lib/node_modules/@stdlib/blas/ext/base/ndarray/ztril/README.md Outdated
Comment thread lib/node_modules/@stdlib/blas/ext/base/ndarray/ztril/README.md Outdated
Comment thread lib/node_modules/@stdlib/blas/ext/base/ndarray/ztril/README.md Outdated
Comment thread lib/node_modules/@stdlib/blas/ext/base/ndarray/ztril/src/main.c Outdated
@ujjwalv01

Copy link
Copy Markdown
Member

@yb175 , Left a few suggestions, PTAL;

@kgryte

kgryte commented Oct 14, 2026

Copy link
Copy Markdown
Member

@yb175 In general, you need to pay much closer attention to how other packages are structured. Only the minimal essential changes should be present among separate similar packages.

@yb175

yb175 commented Oct 14, 2026

Copy link
Copy Markdown
Contributor Author

@yb175 In general, you need to pay much closer attention to how other packages are structured. Only the minimal essential changes should be present among separate similar packages.

Thanks @kgryte for the feedback , from next time I would carefully observe the merged PRs from the similar packages before opening future PRs 😊

* // returns true
*/
declare function ztril( arrays: [ complex128ndarray, complex128ndarray, typedndarray<number> ] ): complex128ndarray;
declare function ztril( arrays: [ complex128ndarray, complex128ndarray, int32ndarray ] ): complex128ndarray;

@ujjwalv01 ujjwalv01 Oct 14, 2026 •

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.

@kgryte Is it okay for k to be int32ndarray here as well ?

@yb175

yb175 commented Oct 14, 2026

Copy link
Copy Markdown
Contributor Author

addressed all other review suggestions . leaving the int32ndarray type question open until I get @kgryte guidance , Then I will follow up with that change .

Comment on lines +24 to +32
var bench = require( '@stdlib/bench' );
var pow = require( '@stdlib/math/base/special/pow' );
var format = require( '@stdlib/string/format' );
var Complex128Matrix = require( '@stdlib/ndarray/matrix/complex128' );
var uniform = require( '@stdlib/random/array/uniform' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var real = require( '@stdlib/complex/float64/real' );
var tryRequire = require( '@stdlib/utils/try-require' );

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.

Keep the same order as benchmark.js.

Suggested change
var bench = require( '@stdlib/bench' );
var pow = require( '@stdlib/math/base/special/pow' );
var format = require( '@stdlib/string/format' );
var Complex128Matrix = require( '@stdlib/ndarray/matrix/complex128' );
var uniform = require( '@stdlib/random/array/uniform' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var real = require( '@stdlib/complex/float64/real' );
var tryRequire = require( '@stdlib/utils/try-require' );
var bench = require( '@stdlib/bench' );
var uniform = require( '@stdlib/random/array/uniform' );
var Complex128Matrix = require( '@stdlib/ndarray/matrix/complex128' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var real = require( '@stdlib/complex/float64/real' );
var pow = require( '@stdlib/math/base/special/pow' );
var format = require( '@stdlib/string/format' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var tryRequire = require( '@stdlib/utils/try-require' );

Comment on lines +38 to +41
var ztril = tryRequire( resolve( __dirname, './../lib/native.js' ) );
var opts = {
'skip': ( ztril instanceof Error )
};

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
var ztril = tryRequire( resolve( __dirname, './../lib/native.js' ) );
var opts = {
'skip': ( ztril instanceof Error )
};
var ztril = tryRequire( resolve( __dirname, './../lib/native.js' ) );
var opts = {
'skip': ( ztril instanceof Error )
};
var options = {
'dtype': 'float64'
};

* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} N - number of rows and columns

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 {PositiveInteger} N - number of rows and columns
* @param {PositiveInteger} N - number of elements along each dimension

Comment on lines +60 to +67
abuf = uniform( N*N*2, -100.0, 100.0, {
'dtype': 'float64'
});
bbuf = uniform( N*N*2, -100.0, 100.0, {
'dtype': 'float64'
});
A = new Complex128Matrix( abuf.buffer, 0, N, N );
B = new Complex128Matrix( bbuf.buffer, 0, N, N );

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
abuf = uniform( N*N*2, -100.0, 100.0, {
'dtype': 'float64'
});
bbuf = uniform( N*N*2, -100.0, 100.0, {
'dtype': 'float64'
});
A = new Complex128Matrix( abuf.buffer, 0, N, N );
B = new Complex128Matrix( bbuf.buffer, 0, N, N );
abuf = uniform( N*N*2, -100.0, 100.0, options );
bbuf = uniform( N*N*2, -100.0, 100.0, options );
A = new Complex128Matrix( abuf.buffer, 0, N, N );
B = new Complex128Matrix( bbuf.buffer, 0, N, N );

@yb175
yb175 requested a review from ujjwalv01 October 15, 2026 05:40
@stdlib-bot stdlib-bot added the Needs Review A pull request which needs code review. label Oct 15, 2026

@ujjwalv01 ujjwalv01 left a comment

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.

Looks good to me;

cc : @kgryte

@ujjwalv01 ujjwalv01 removed the Needs Changes Pull request which needs changes before being merged. label Oct 15, 2026
@ujjwalv01 ujjwalv01 added Feature Issue or pull request for adding a new feature. C Issue involves or relates to C. labels Oct 27, 2026

This branch has not been deployed

No deployments
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). C Issue involves or relates to C. Feature Issue or pull request for adding a new feature. Needs Review A pull request which needs code review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants