Skip to content

Commit 1f36fca

Browse files
feat: add blas/ext/base/ndarray/daxpb
PR-URL: #12276 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#634
1 parent a5376c6 commit 1f36fca

10 files changed

Lines changed: 836 additions & 0 deletions

File tree

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# daxpb
22+
23+
> Multiply each element in a one-dimensional double-precision floating-point ndarray by a scalar constant and add a scalar constant to each result.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var daxpb = require( '@stdlib/blas/ext/base/ndarray/daxpb' );
37+
```
38+
39+
#### daxpb( arrays )
40+
41+
Multiplies each element in a one-dimensional double-precision floating-point ndarray by a scalar constant and adds a scalar constant to each result.
42+
43+
```javascript
44+
var Float64Vector = require( '@stdlib/ndarray/vector/float64' );
45+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
46+
47+
var x = new Float64Vector( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] );
48+
49+
var alpha = scalar2ndarray( 5.0, {
50+
'dtype': 'float64'
51+
});
52+
53+
var beta = scalar2ndarray( 3.0, {
54+
'dtype': 'float64'
55+
});
56+
57+
daxpb( [ x, alpha, beta ] );
58+
// x => <ndarray>[ -7.0, 8.0, 18.0, -22.0, 23.0, 3.0, -2.0, -12.0 ]
59+
```
60+
61+
The function has the following parameters:
62+
63+
- **arrays**: array-like object containing the following ndarrays:
64+
65+
- a one-dimensional input ndarray.
66+
- a zero-dimensional ndarray containing the scalar constant to multiply.
67+
- a zero-dimensional ndarray containing the scalar constant to add.
68+
69+
</section>
70+
71+
<!-- /.usage -->
72+
73+
<section class="notes">
74+
75+
## Notes
76+
77+
- The input ndarray is modified **in-place** (i.e., the input ndarray is **mutated**).
78+
79+
</section>
80+
81+
<!-- /.notes -->
82+
83+
<section class="examples">
84+
85+
## Examples
86+
87+
<!-- eslint no-undef: "error" -->
88+
89+
```javascript
90+
var discreteUniform = require( '@stdlib/random/discrete-uniform' );
91+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
92+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
93+
var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' );
94+
var daxpb = require( '@stdlib/blas/ext/base/ndarray/daxpb' );
95+
96+
var opts = {
97+
'dtype': 'float64'
98+
};
99+
100+
var x = discreteUniform( [ 10 ], -100, 100, opts );
101+
console.log( ndarray2array( x ) );
102+
103+
var alpha = scalar2ndarray( 5.0, opts );
104+
console.log( 'Alpha: %d', ndarraylike2scalar( alpha ) );
105+
106+
var beta = scalar2ndarray( 3.0, opts );
107+
console.log( 'Beta: %d', ndarraylike2scalar( beta ) );
108+
109+
daxpb( [ x, alpha, beta ] );
110+
console.log( ndarray2array( x ) );
111+
```
112+
113+
</section>
114+
115+
<!-- /.examples -->
116+
117+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
118+
119+
<section class="related">
120+
121+
</section>
122+
123+
<!-- /.related -->
124+
125+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
126+
127+
<section class="links">
128+
129+
</section>
130+
131+
<!-- /.links -->
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/uniform' );
25+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var format = require( '@stdlib/string/format' );
28+
var pkg = require( './../package.json' ).name;
29+
var daxpb = require( './../lib' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'float64'
36+
};
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Creates a benchmark function.
43+
*
44+
* @private
45+
* @param {PositiveInteger} len - ndarray length
46+
* @returns {Function} benchmark function
47+
*/
48+
function createBenchmark( len ) {
49+
var alpha;
50+
var beta;
51+
var x;
52+
53+
x = uniform( [ len ], -100.0, 100.0, options );
54+
alpha = scalar2ndarray( 5.0, options );
55+
beta = scalar2ndarray( 3.0, options );
56+
return benchmark;
57+
58+
/**
59+
* Benchmark function.
60+
*
61+
* @private
62+
* @param {Benchmark} b - benchmark instance
63+
*/
64+
function benchmark( b ) {
65+
var out;
66+
var i;
67+
68+
b.tic();
69+
for ( i = 0; i < b.iterations; i++ ) {
70+
out = daxpb( [ x, alpha, beta ] );
71+
if ( typeof out !== 'object' ) {
72+
b.fail( 'should return an ndarray' );
73+
}
74+
}
75+
b.toc();
76+
if ( typeof out !== 'object' ) {
77+
b.fail( 'should return an ndarray' );
78+
}
79+
b.pass( 'benchmark finished' );
80+
b.end();
81+
}
82+
}
83+
84+
85+
// MAIN //
86+
87+
/**
88+
* Main execution sequence.
89+
*
90+
* @private
91+
*/
92+
function main() {
93+
var len;
94+
var min;
95+
var max;
96+
var f;
97+
var i;
98+
99+
min = 1; // 10^min
100+
max = 6; // 10^max
101+
102+
for ( i = min; i <= max; i++ ) {
103+
len = pow( 10, i );
104+
f = createBenchmark( len );
105+
bench( format( '%s:len=%d', pkg, len ), f );
106+
}
107+
}
108+
109+
main();
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
{{alias}}( arrays )
3+
Multiplies each element in a one-dimensional double-precision floating-point
4+
ndarray by a scalar constant and adds a scalar constant to each result.
5+
6+
The input ndarray is modified *in-place* (i.e., the input ndarray is
7+
*mutated*).
8+
9+
Parameters
10+
----------
11+
arrays: ArrayLikeObject<ndarray>
12+
Array-like object containing the following ndarrays:
13+
14+
- a one-dimensional input ndarray.
15+
- a zero-dimensional ndarray containing the scalar constant to multiply.
16+
- a zero-dimensional ndarray containing the scalar constant to add.
17+
18+
Returns
19+
-------
20+
out: ndarray
21+
Input ndarray.
22+
23+
Examples
24+
--------
25+
> var buf = [ -2.0, 1.0, 3.0, -5.0 ];
26+
> var x = new {{alias:@stdlib/ndarray/vector/float64}}( buf );
27+
> var opts = { 'dtype': 'float64' };
28+
> var alpha = {{alias:@stdlib/ndarray/from-scalar}}( 5.0, opts );
29+
> var beta = {{alias:@stdlib/ndarray/from-scalar}}( 3.0, opts );
30+
> {{alias}}( [ x, alpha, beta ] )
31+
<ndarray>[ -7.0, 8.0, 18.0, -22.0 ]
32+
33+
See Also
34+
--------
35+
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { float64ndarray, typedndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Multiplies each element in a one-dimensional double-precision floating-point ndarray by a scalar constant and adds a scalar constant to each result.
27+
*
28+
* ## Notes
29+
*
30+
* - The function expects the following ndarrays:
31+
*
32+
* - a one-dimensional input ndarray.
33+
* - a zero-dimensional ndarray containing the scalar constant to multiply.
34+
* - a zero-dimensional ndarray containing the scalar constant to add.
35+
*
36+
* @param arrays - array-like object containing ndarrays
37+
* @returns input ndarray
38+
*
39+
* @example
40+
* var Float64Vector = require( '@stdlib/ndarray/vector/float64' );
41+
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
42+
*
43+
* var x = new Float64Vector( [ -2.0, 1.0, 3.0, -5.0 ] );
44+
*
45+
* var alpha = scalar2ndarray( 5.0, {
46+
* 'dtype': 'float64'
47+
* });
48+
*
49+
* var beta = scalar2ndarray( 3.0, {
50+
* 'dtype': 'float64'
51+
* });
52+
*
53+
* var out = daxpb( [ x, alpha, beta ] );
54+
* // returns <ndarray>[ -7.0, 8.0, 18.0, -22.0 ]
55+
*/
56+
declare function daxpb( arrays: [ float64ndarray, typedndarray<number>, typedndarray<number> ] ): float64ndarray;
57+
58+
59+
// EXPORTS //
60+
61+
export = daxpb;

0 commit comments

Comments
 (0)