Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
184 changes: 184 additions & 0 deletions lib/node_modules/@stdlib/blas/ext/axpb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
<!--

@license Apache-2.0

Copyright (c) 2026 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# axpb

> Multiply each element in an input [ndarray][@stdlib/ndarray/ctor] by a scalar constant and add a scalar constant to each result along one or more [ndarray][@stdlib/ndarray/ctor] dimensions.

<section class="intro">

This BLAS extension implements the operation

<!-- <equation class="equation" label="eq:axpb" align="center" raw="\mathbf{x} = \alpha \mathbf{x} + \beta" alt="Equation for axpb operation."> -->

```math
\mathbf{x} = \alpha \mathbf{x} + \beta
```

<!-- </equation> -->

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var axpb = require( '@stdlib/blas/ext/axpb' );
```

#### axpb( x, alpha, beta\[, options] )

Multiplies each element in an input [ndarray][@stdlib/ndarray/ctor] by a scalar constant and adds a scalar constant to each result along one or more [ndarray][@stdlib/ndarray/ctor] dimensions.

```javascript
var array = require( '@stdlib/ndarray/array' );

var x = array( [ -2.0, 1.0, 3.0, -5.0 ] );

var y = axpb( x, 5.0, 3.0 );
// returns <ndarray>[ -7.0, 8.0, 18.0, -22.0 ]

var bool = ( x === y );
// returns true
```

The function has the following parameters:

- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a floating-point or "generic" [data type][@stdlib/ndarray/dtypes].

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
- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a floating-point or "generic" [data type][@stdlib/ndarray/dtypes].
- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a numeric or "generic" [data type][@stdlib/ndarray/dtypes].

- **alpha**: scalar constant to multiply. May be a numeric scalar (number or complex number) or an [ndarray][@stdlib/ndarray/ctor] having a floating-point or "generic" [data type][@stdlib/ndarray/dtypes]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dims`. For example, given the input shape `[2, 3, 4]` and `options.dims=[0]`, an [ndarray][@stdlib/ndarray/ctor] alpha must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor], an [ndarray][@stdlib/ndarray/ctor] alpha must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor].

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
- **alpha**: scalar constant to multiply. May be a numeric scalar (number or complex number) or an [ndarray][@stdlib/ndarray/ctor] having a floating-point or "generic" [data type][@stdlib/ndarray/dtypes]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dims`. For example, given the input shape `[2, 3, 4]` and `options.dims=[0]`, an [ndarray][@stdlib/ndarray/ctor] alpha must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor], an [ndarray][@stdlib/ndarray/ctor] alpha must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor].
- **alpha**: scalar constant to multiply. May be a numeric scalar (number or complex number) or an [ndarray][@stdlib/ndarray/ctor] having a numeric or "generic" [data type][@stdlib/ndarray/dtypes]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dims`. For example, given the input shape `[2, 3, 4]` and `options.dims=[0]`, an [ndarray][@stdlib/ndarray/ctor] `alpha` must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor], an [ndarray][@stdlib/ndarray/ctor] `alpha` must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor].

- **beta**: scalar constant to add. May be a numeric scalar (number or complex number) or an [ndarray][@stdlib/ndarray/ctor] having a floating-point or "generic" [data type][@stdlib/ndarray/dtypes]. Broadcasting semantics are the same as for alpha.

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
- **beta**: scalar constant to add. May be a numeric scalar (number or complex number) or an [ndarray][@stdlib/ndarray/ctor] having a floating-point or "generic" [data type][@stdlib/ndarray/dtypes]. Broadcasting semantics are the same as for alpha.
- **beta**: scalar constant to add. May be a numeric scalar (number or complex number) or an [ndarray][@stdlib/ndarray/ctor] having a numeric or "generic" [data type][@stdlib/ndarray/dtypes]. Broadcasting semantics are the same as for alpha.

I would also be more explicit regarding the broadcasting behavior, rather than just referring to alpha. If you want to avoid duplication, consider moving the broadcast discussion to the ## Notes section below and then saying "Must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by options.dims. For more discussion, see the notes below." for both alpha and beta.

- **options**: function options (_optional_).

The function accepts the following options:

- **dims**: list of dimensions over which to perform operation. If not provided, the function performs the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor].

Both `alpha` and `beta` can be complex numbers:

```javascript
var Complex128 = require( '@stdlib/complex/float64/ctor' );
var Complex128Vector = require( '@stdlib/ndarray/vector/complex128' );

var x = new Complex128Vector( [ 1.0, 2.0, 3.0, 4.0 ] );

var alpha = new Complex128( 2.0, 1.0 );
var beta = new Complex128( 1.0, 0.0 );

var y = axpb( x, alpha, beta );
// returns <ndarray>[ <Complex128>[ 1.0, 5.0 ], <Complex128>[ 3.0, 11.0 ] ]
```

The function also supports complex-valued floating-point ndarrays:

```javascript
var Complex128Vector = require( '@stdlib/ndarray/vector/complex128' );

var x = new Complex128Vector( [ 1.0, 2.0, 3.0, 4.0 ] );

var y = axpb( x, 2.0, 1.0 );
// returns <ndarray>[ <Complex128>[ 3.0, 4.0 ], <Complex128>[ 7.0, 8.0 ] ]
```
Comment on lines +76 to +100

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.

These examples are a bit weak. Instead, I suggest consolidating into a single example which demonstrates the behavior when alpha and/or beta are/is real-valued and x is complex-valued. This highlights both that complex arrays are accepted and that real-values are casted to the dtype of x.


By default, the function performs the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. To perform the operation over specific dimensions, provide a `dims` option.

```javascript
var array = require( '@stdlib/ndarray/array' );

var x = array( [ -2.0, 1.0, 3.0, -5.0 ], {
'shape': [ 2, 2 ],
'order': 'row-major'
});
// returns <ndarray>[ [ -2.0, 1.0 ], [ 3.0, -5.0 ] ]

var y = axpb( x, 5.0, 3.0, {
'dims': [ 0 ]
});
// returns <ndarray>[ [ -7.0, 8.0 ], [ 18.0, -22.0 ] ]
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- The input [ndarray][@stdlib/ndarray/ctor] is modified **in-place** (i.e., the input [ndarray][@stdlib/ndarray/ctor] is **mutated**).
- The function iterates over [ndarray][@stdlib/ndarray/ctor] elements according to the memory layout of the input [ndarray][@stdlib/ndarray/ctor]. Accordingly, performance degradation is possible when operating over multiple dimensions of a large non-contiguous multi-dimensional input [ndarray][@stdlib/ndarray/ctor]. In such scenarios, one may want to copy an input [ndarray][@stdlib/ndarray/ctor] to contiguous memory before performing the operation.

</section>

<!-- /.notes -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var discreteUniform = require( '@stdlib/random/discrete-uniform' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var axpb = require( '@stdlib/blas/ext/axpb' );

// Create an input ndarray:
var x = discreteUniform( [ 5, 2 ], -100, 100, {
'dtype': 'generic'
});

// Print the input ndarray:
Comment on lines +148 to +150

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
});
// Print the input ndarray:
});

console.log( ndarray2array( x ) );

// Perform operation:
axpb( x, 2.0, 5.0 );

// Print the result:
console.log( ndarray2array( x ) );
```

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor

[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes

[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes

</section>

<!-- /.links -->
103 changes: 103 additions & 0 deletions lib/node_modules/@stdlib/blas/ext/axpb/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pow = require( '@stdlib/math/base/special/pow' );
var uniform = require( '@stdlib/random/uniform' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var axpb = require( './../lib' );


// VARIABLES //

var options = {
'dtype': 'float64'
};


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x = uniform( [ len ], -50.0, 50.0, options );
return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var o;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
o = axpb( x, 5.0, 3.0 );
if ( typeof o !== 'object' ) {
b.fail( 'should return an ndarray' );
}
}
b.toc();
if ( isnan( o.get( i%len ) ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var len;
var min;
var max;
var f;
var i;

min = 1; // 10^min
max = 6; // 10^max

for ( i = min; i <= max; i++ ) {
len = pow( 10, i );
f = createBenchmark( len );
bench( format( '%s:dtype=%s,len=%d', pkg, options.dtype, len ), f );
}
}

main();
49 changes: 49 additions & 0 deletions lib/node_modules/@stdlib/blas/ext/axpb/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

{{alias}}( x, alpha, beta[, options] )
Multiplies each element in an input ndarray by a scalar constant and adds a
scalar constant to each result along one or more ndarray dimensions.

The function performs the operation in-place and thus mutates the input
ndarray.

Parameters
----------
x: ndarray
Input array. Must have a numeric or "generic" data type.

alpha: ndarray|number|ComplexLike
Scalar constant to multiply. May be either a scalar value, or an ndarray
having a floating-point or "generic" data type. If provided an ndarray,

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
having a floating-point or "generic" data type. If provided an ndarray,
having a numeric or "generic" data type. If provided an ndarray,

Given this change, you'll need to rewrap this description.

the value must have a shape which is broadcast compatible with the
complement of the shape defined by `options.dims`. For example, given
the input shape `[2, 3, 4]` and `options.dims=[0]`, an ndarray alpha
must have a shape which is broadcast compatible with the shape `[3, 4]`.
Similarly, when performing the operation over all elements in a provided
input ndarray, an ndarray alpha must be a zero-dimensional ndarray.

beta: ndarray|number|ComplexLike
Scalar constant to add. May be either a scalar value, or an ndarray
having a floating-point or "generic" data type. Broadcasting semantics

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
having a floating-point or "generic" data type. Broadcasting semantics
having a numeric or "generic" data type. Broadcasting semantics

Rewrap.

are the same as for alpha.

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.

I would just copy, paste, and modify the broadcasting semantics description from alpha. This makes very explicit that broadcasting is purely related to x and options.dims, and that there are no broadcasting semantics between alpha and beta.


options: Object (optional)
Function options.

options.dims: Array<integer> (optional)
List of dimensions over which to perform operation. If not provided, the

@kgryte kgryte Jun 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.

This means "flattening" during the operation, which I think is the right behavior, by default, for this API.

function performs the operation over all elements in a provided input
ndarray.

Returns
-------
out: ndarray
Input array.

Examples
--------
> var x = {{alias:@stdlib/ndarray/array}}( [ -2.0, 1.0, 3.0, -5.0 ] );
> var y = {{alias}}( x, 5.0, 3.0 )
<ndarray>[ -7.0, 8.0, 18.0, -22.0 ]

See Also
--------
Loading