Skip to content

Conversation

@Amansingh0807
Copy link
Contributor

Resolves None.

Description

What is the purpose of this pull request?

This pull request:

  • add blas/ext/base/ndarray/dapx

Related Issues

Does this pull request have any related issues?

This pull request has the following related issues:

  • Resolves None

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


@stdlib-js/reviewers

@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 Dec 18, 2025
@stdlib-bot
Copy link
Contributor

stdlib-bot commented Dec 18, 2025

Coverage Report

Package Statements Branches Functions Lines
blas/ext/base/ndarray/dapx $\color{green}122/122$
$\color{green}+0.00%$
$\color{green}3/3$
$\color{green}+0.00%$
$\color{green}1/1$
$\color{green}+0.00%$
$\color{green}122/122$
$\color{green}+0.00%$

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

@kgryte kgryte added Feature Issue or pull request for adding a new feature. and removed Needs Review A pull request which needs code review. labels Dec 20, 2025
var dapx = require( '@stdlib/blas/ext/base/ndarray/dapx' );
```

#### dapx( x, alpha )
Copy link
Member

Choose a reason for hiding this comment

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

This isn't correct. You need to study https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/ndarray/dcusum. alpha should be a zero-dimensional ndarray and there should be only a single argument arrays containing two arrays: the input ndarray and the zero-dimensional ndarray corresponding to alpha.

This comment applies to your other PRs for *apx.

@@ -0,0 +1,27 @@
{{alias}}( x, alpha )
Copy link
Member

Choose a reason for hiding this comment

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

Missing empty line. Study other packages.


// The function returns an ndarray...
{
const x: any = null;
Copy link
Member

Choose a reason for hiding this comment

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

This is very clearly not what we want to do.


// The compiler throws an error if the function is provided a second argument which is not a number...
{
const x: any = null;
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.

/**
* Adds a scalar constant to each element in a double-precision floating-point ndarray.
*
* @param {float64ndarray} x - input ndarray
Copy link
Member

Choose a reason for hiding this comment

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

This isn't a valid type for JSDoc. Study other packages.

Comment on lines 52 to 66
var buf;
var sx;
var ox;
var N;

N = numelDimension( x, 0 );
if ( N <= 0 ) {
return x;
}
buf = getData( x );
sx = getStride( x, 0 );
ox = getOffset( x );

strided( N, alpha, buf, sx, ox );
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.

Study other packages.

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. This PR (and related PRs) need significant clean-up before they can move forward.

@kgryte kgryte added the Needs Changes Pull request which needs changes before being merged. label Dec 20, 2025
@Amansingh0807
Copy link
Contributor Author

Amansingh0807 commented Dec 20, 2025

Thanks for the detailed guidance @kgryte. It really clarified the architectural pattern for me.

I have refactored the implementation to align with dcusum and other packages. The function now accepts a single arrays argument, and I am using ndarraylike2scalar for correct value extraction.

I have also updated the JSDoc types and examples (using the new notation) and will apply these changes across all my related *apx PRs. This one is Ready for review.


# dapx

> Add a scalar constant to each element in a double-precision floating-point ndarray.
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
> Add a scalar constant to each element in a double-precision floating-point ndarray.
> Add a scalar constant to each element in a one-dimensional double-precision floating-point ndarray.

Applies here and throughout this PR.

Comment on lines 60 to 79
Note that indexing is relative to the first index. To introduce an offset, use [`ndarray`][@stdlib/ndarray/ctor] view creation.

```javascript
var Float64Array = require( '@stdlib/array/float64' );
var ndarray = require( '@stdlib/ndarray/ctor' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );

// Initial array:
var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );

// Create an ndarray view:
var x = new ndarray( 'float64', xbuf, [ 3 ], [ 1 ], 2, 'row-major' );

var alpha = scalar2ndarray( 5.0, {
'dtype': 'float64'
});

var out = dapx( [ x, alpha ] );
// returns <ndarray>
```
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
Note that indexing is relative to the first index. To introduce an offset, use [`ndarray`][@stdlib/ndarray/ctor] view creation.
```javascript
var Float64Array = require( '@stdlib/array/float64' );
var ndarray = require( '@stdlib/ndarray/ctor' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
// Initial array:
var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
// Create an ndarray view:
var x = new ndarray( 'float64', xbuf, [ 3 ], [ 1 ], 2, 'row-major' );
var alpha = scalar2ndarray( 5.0, {
'dtype': 'float64'
});
var out = dapx( [ x, alpha ] );
// returns <ndarray>
```

Comment on lines 87 to 90
## Notes

- The function **mutates** the input ndarray.

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
## Notes
- The function **mutates** the input ndarray.

});
var x = new ndarray( 'float64', xbuf, [ 10 ], [ 1 ], 0, 'row-major' );

console.log( x.data );
Copy link
Member

Choose a reason for hiding this comment

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

Study other packages. Use ndarray2array.


The function has the following parameters:

- **arrays**: array-like object containing an input ndarray and a zero-dimensional ndarray containing the scalar constant.
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
- **arrays**: array-like object containing an input ndarray and a zero-dimensional ndarray containing the scalar constant.
- **arrays**: array-like object containing a one-dimensional input ndarray and a zero-dimensional ndarray containing a scalar constant.

});

var out = dapx( [ x, alpha ] );
// returns <ndarray>
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
// returns <ndarray>
// returns <ndarray>[ 6.0, 7.0, 8.0, 9.0 ]

Copy link
Member

Choose a reason for hiding this comment

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

Show what the expected values are.

var dapx = require( './../lib' );


// FUNCTIONS //
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
// FUNCTIONS //
// VARIABLES //
var options = {
'dtype': 'float64'
};
// FUNCTIONS //

Prefer parameterization to limit copy-paste mistakes in other packages. This is one reason to first do one package (e.g., dapx) before moving on to other packages (e.g., sapx, gapx), as it reduces the number of changes you need to make. Currently, you are having to replicate changes.

Comment on lines 47 to 53
xbuf = discreteUniform( len, -100, 100, {
'dtype': 'float64'
});
x = new ndarray( 'float64', xbuf, [ len ], [ 1 ], 0, 'row-major' );
alpha = scalar2ndarray( 5.0, {
'dtype': 'float64'
});
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
xbuf = discreteUniform( len, -100, 100, {
'dtype': 'float64'
});
x = new ndarray( 'float64', xbuf, [ len ], [ 1 ], 0, 'row-major' );
alpha = scalar2ndarray( 5.0, {
'dtype': 'float64'
});
xbuf = discreteUniform( len, -100, 100, options );
x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' );
alpha = scalar2ndarray( 5.0, options );


var bench = require( '@stdlib/bench' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
Copy link
Member

Choose a reason for hiding this comment

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

It is not clear to me why you are using a single-precision assertion package here.

@@ -0,0 +1,27 @@
{{alias}}( arrays )
Copy link
Member

Choose a reason for hiding this comment

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

See other packages. This was not the correct change.

> var x = {{alias:@stdlib/ndarray/ctor}}( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
> var alpha = {{alias:@stdlib/ndarray/from-scalar}}( 5.0, { 'dtype': 'float64' } );
> {{alias}}( [ x, alpha ] )
<ndarray>
Copy link
Member

Choose a reason for hiding this comment

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

Show expected values.

* var out = dapx( [ x, alpha ] );
* // returns <ndarray>
*/
declare function dapx( arrays: ArrayLike<float64ndarray> ): float64ndarray;
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
declare function dapx( arrays: ArrayLike<float64ndarray> ): float64ndarray;
declare function dapx( arrays: [ float64ndarray, float64ndarray ] ): float64ndarray;

By using ArrayLike, you end up losing type specificity. Here, we can specify that one must provide exactly two ndarrays.

* });
*
* var out = dapx( [ x, alpha ] );
* // returns <ndarray>
Copy link
Member

Choose a reason for hiding this comment

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

Show expected values.

* });
*
* var out = dapx( [ x, alpha ] );
* // returns <ndarray>
Copy link
Member

Choose a reason for hiding this comment

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

Show expected values.

/**
* Adds a scalar constant to each element in a double-precision floating-point ndarray.
*
* @param {ArrayLikeObject<Object>} arrays - array-like object containing an input ndarray and a zero-dimensional ndarray containing the scalar constant
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
* @param {ArrayLikeObject<Object>} arrays - array-like object containing an input ndarray and a zero-dimensional ndarray containing the scalar constant
* @param {ArrayLikeObject<Object>} arrays - array-like object containing a one-dimensional input ndarray and a zero-dimensional ndarray containing the scalar constant

// MAIN //

/**
* Adds a scalar constant to each element in a double-precision floating-point ndarray.
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
* Adds a scalar constant to each element in a double-precision floating-point ndarray.
* Adds a scalar constant to each element in a one-dimensional double-precision floating-point ndarray.

* });
*
* var out = dapx( [ x, alpha ] );
* // returns <ndarray>
Copy link
Member

Choose a reason for hiding this comment

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

Show expected values.

Comment on lines 56 to 59

if ( numelDimension( x, 0 ) <= 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 ( numelDimension( x, 0 ) <= 0 ) {
return x;
}

{
"name": "@stdlib/blas/ext/base/ndarray/dapx",
"version": "0.0.0",
"description": "Add a scalar constant to each element in a double-precision floating-point ndarray.",
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
"description": "Add a scalar constant to each element in a double-precision floating-point ndarray.",
"description": "Add a scalar constant to each element in a one-dimensional double-precision floating-point ndarray.",

"constant",
"plus",
"increment",
"augment",
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
"augment",

"array",
"multidimensional",
"add",
"constant",
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
"constant",

t.end();
});

tape( 'the function adds a scalar constant to each element in a double-precision floating-point ndarray', function test( t ) {
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
tape( 'the function adds a scalar constant to each element in a double-precision floating-point ndarray', function test( t ) {
tape( 'the function adds a scalar constant to each element in an ndarray', function test( t ) {

Copy link
Member

Choose a reason for hiding this comment

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

Minimize future copy-paste errors.


dapx( [ x, alpha ] );

t.deepEqual( x.data, expected, 'returns expected value' );
Copy link
Member

Choose a reason for hiding this comment

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

Use ndarray2array or getData (see @stdlib/ndarray/data-buffer). Meaning, prefer functional accessors in order to minimize future migration burden should we ever rename properties, etc.

t.end();
});

tape( 'the function supports ndarrays with an offset', function test( t ) {
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
tape( 'the function supports ndarrays with an offset', function test( t ) {
tape( 'the function supports ndarrays with non-zero offsets', function test( t ) {

Comment on lines 141 to 161
tape( 'the function supports adding zero', function test( t ) {
var expected;
var alpha;
var xbuf;
var x;

xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] );
x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );

expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] );

alpha = scalar2ndarray( 0.0, {
'dtype': 'float64'
});

dapx( [ x, alpha ] );

t.deepEqual( x.data, expected, 'returns expected value' );
t.end();
});

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
tape( 'the function supports adding zero', function test( t ) {
var expected;
var alpha;
var xbuf;
var x;
xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] );
x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
expected = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] );
alpha = scalar2ndarray( 0.0, {
'dtype': 'float64'
});
dapx( [ x, alpha ] );
t.deepEqual( x.data, expected, 'returns expected value' );
t.end();
});

This test is not particularly informative.

Comment on lines 162 to 181
tape( 'the function supports negative constants', function test( t ) {
var expected;
var alpha;
var xbuf;
var x;

xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] );
x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );

expected = new Float64Array( [ -4.0, -3.0, -2.0, -1.0 ] );

alpha = scalar2ndarray( -5.0, {
'dtype': 'float64'
});

dapx( [ x, alpha ] );

t.deepEqual( x.data, expected, 'returns expected value' );
t.end();
});
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
tape( 'the function supports negative constants', function test( t ) {
var expected;
var alpha;
var xbuf;
var x;
xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] );
x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
expected = new Float64Array( [ -4.0, -3.0, -2.0, -1.0 ] );
alpha = scalar2ndarray( -5.0, {
'dtype': 'float64'
});
dapx( [ x, alpha ] );
t.deepEqual( x.data, expected, 'returns expected value' );
t.end();
});

Neither is this one.

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 another round of comments.

@kgryte
Copy link
Member

kgryte commented Dec 20, 2025

Actually, one package you should study is blas/ext/base/ndarray/dsorthp. It, too, takes both a one-dimensional ndarray and a zero-dimensional scalar. If you compare line-by-line, you'll likely see things that you can change/improve in this PR.

- Add 'one-dimensional' to all descriptions
- Use ndarray2array to show expected values
- Remove dimension check from main function
- Update TypeScript types to tuple syntax
- Use getData instead of .data in tests
- Remove uninformative test cases
- Add VARIABLES section in benchmark
- Use options parameterization
- Change isnanf to isnan for double precision
- Remove Notes section from README
- Update all documentation with expected outputs
@Amansingh0807
Copy link
Contributor Author

Thanks for the extensive review @kgryte.

I have refactored this package (dapx) to align strictly with the dsorthp structure as suggested. Key changes include:

  • Documentation: Updated descriptions to specify "one-dimensional", restored the Mutation Note, and updated examples with expected output values.
  • Benchmarks: Parameterized the options object to prevent future errors.
  • Tests: Removed direct .data access (switched to ndarray2array/accessors) and removed uninformative test cases.
  • Types: Tightened the TypeScript definition to use a Tuple [ float64ndarray, float64ndarray ].
  • Package: Cleaned up keywords and descriptions.

I have applied these changes ONLY to this PR first. Once you confirm this implementation meets the standard, I will immediately propagate the exact same pattern to the other pending PRs (sapx, gapx, etc.) to ensure consistency.

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. Needs Changes Pull request which needs changes before being merged.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants