Skip to content

Commit cbcb5a5

Browse files
MeKaustubh07kgryte
andauthored
feat: add blas/base/ndarray/gaxpy
PR-URL: #11442 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 886ad4c commit cbcb5a5

File tree

10 files changed

+870
-0
lines changed

10 files changed

+870
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
# gaxpy
22+
23+
> Multiply a one-dimensional ndarray `x` by a constant `alpha` and add the result to a one-dimensional ndarray `y`.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var gaxpy = require( '@stdlib/blas/base/ndarray/gaxpy' );
37+
```
38+
39+
#### gaxpy( arrays )
40+
41+
Multiplies a one-dimensional ndarray `x` by a constant `alpha` and adds the result to a one-dimensional ndarray `y`.
42+
43+
```javascript
44+
var scalar2ndarray = require( '@stdlib/ndarray/base/from-scalar' );
45+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
46+
47+
var xbuf = [ 1.0, 2.0, 3.0, 4.0, 5.0 ];
48+
var x = new ndarray( 'generic', xbuf, [ 5 ], [ 1 ], 0, 'row-major' );
49+
50+
var ybuf = [ 1.0, 1.0, 1.0, 1.0, 1.0 ];
51+
var y = new ndarray( 'generic', ybuf, [ 5 ], [ 1 ], 0, 'row-major' );
52+
53+
var alpha = scalar2ndarray( 5.0, 'generic', 'row-major' );
54+
var z = gaxpy( [ x, y, alpha ] );
55+
// returns <ndarray>[ 6.0, 11.0, 16.0, 21.0, 26.0 ]
56+
57+
var bool = ( y === z );
58+
// returns true
59+
```
60+
61+
The function has the following parameters:
62+
63+
- **arrays**: array-like object containing an input ndarray, an output ndarray, and a zero-dimensional ndarray containing a scalar constant.
64+
65+
</section>
66+
67+
<!-- /.usage -->
68+
69+
<section class="notes">
70+
71+
</section>
72+
73+
<!-- /.notes -->
74+
75+
<section class="examples">
76+
77+
## Examples
78+
79+
<!-- eslint no-undef: "error" -->
80+
81+
```javascript
82+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
83+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
84+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
85+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
86+
var gaxpy = require( '@stdlib/blas/base/ndarray/gaxpy' );
87+
88+
var opts = {
89+
'dtype': 'generic'
90+
};
91+
92+
var xbuf = discreteUniform( 10, 0, 100, opts );
93+
var x = new ndarray( opts.dtype, xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
94+
console.log( ndarray2array( x ) );
95+
96+
var ybuf = discreteUniform( xbuf.length, 0, 10, opts );
97+
var y = new ndarray( opts.dtype, ybuf, [ ybuf.length ], [ 1 ], 0, 'row-major' );
98+
console.log( ndarray2array( y ) );
99+
100+
var alpha = scalar2ndarray( 5.0, opts );
101+
var out = gaxpy( [ x, y, alpha ] );
102+
console.log( ndarray2array( out ) );
103+
```
104+
105+
</section>
106+
107+
<!-- /.examples -->
108+
109+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
110+
111+
<section class="related">
112+
113+
</section>
114+
115+
<!-- /.related -->
116+
117+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
118+
119+
<section class="links">
120+
121+
</section>
122+
123+
<!-- /.links -->
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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/array/uniform' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
28+
var scalar2ndarray = require( '@stdlib/ndarray/base/from-scalar' );
29+
var format = require( '@stdlib/string/format' );
30+
var pkg = require( './../package.json' ).name;
31+
var gaxpy = require( './../lib' );
32+
33+
34+
// VARIABLES //
35+
36+
var options = {
37+
'dtype': 'generic'
38+
};
39+
40+
41+
// FUNCTIONS //
42+
43+
/**
44+
* Creates a benchmark function.
45+
*
46+
* @private
47+
* @param {PositiveInteger} len - array length
48+
* @returns {Function} benchmark function
49+
*/
50+
function createBenchmark( len ) {
51+
var alpha;
52+
var xbuf;
53+
var ybuf;
54+
var x;
55+
var y;
56+
57+
xbuf = uniform( len, -100.0, 100.0, options );
58+
x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' );
59+
60+
ybuf = uniform( len, -100.0, 100.0, options );
61+
y = new ndarray( options.dtype, ybuf, [ len ], [ 1 ], 0, 'row-major' );
62+
63+
alpha = scalar2ndarray( 5.0, options.dtype, 'row-major' );
64+
65+
return benchmark;
66+
67+
/**
68+
* Benchmark function.
69+
*
70+
* @private
71+
* @param {Benchmark} b - benchmark instance
72+
*/
73+
function benchmark( b ) {
74+
var z;
75+
var i;
76+
77+
b.tic();
78+
for ( i = 0; i < b.iterations; i++ ) {
79+
z = gaxpy( [ x, y, alpha ] );
80+
if ( typeof z !== 'object' ) {
81+
b.fail( 'should not return NaN' );
82+
}
83+
}
84+
b.toc();
85+
if ( isnan( z.get( i%len ) ) ) {
86+
b.fail( 'should not return NaN' );
87+
}
88+
b.pass( 'benchmark finished' );
89+
b.end();
90+
}
91+
}
92+
93+
94+
// MAIN //
95+
96+
/**
97+
* Main execution sequence.
98+
*
99+
* @private
100+
*/
101+
function main() {
102+
var len;
103+
var min;
104+
var max;
105+
var f;
106+
var i;
107+
108+
min = 1; // 10^min
109+
max = 6; // 10^max
110+
111+
for ( i = min; i <= max; i++ ) {
112+
len = pow( 10, i );
113+
f = createBenchmark( len );
114+
bench( format( '%s:len=%d', pkg, len ), f );
115+
}
116+
}
117+
118+
main();
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
{{alias}}( arrays )
3+
Multiplies a one-dimensional ndarray `x` by a constant `alpha` and adds the
4+
result to a one-dimensional ndarray `y`.
5+
6+
If provided an empty input ndarray, the function returns the output ndarray
7+
unchanged.
8+
9+
Parameters
10+
----------
11+
arrays: ArrayLikeObject<ndarray>
12+
Array-like object containing an input ndarray, an output ndarray, and
13+
a zero-dimensional ndarray containing a scalar constant `alpha`.
14+
15+
Returns
16+
-------
17+
out: ndarray
18+
Output ndarray.
19+
20+
Examples
21+
--------
22+
> var xbuf = [ 4.0, 2.0, -3.0, 5.0, -1.0 ];
23+
> var ybuf = [ 2.0, 6.0, -1.0, -4.0, 8.0 ];
24+
> var dt = 'generic';
25+
> var sh = [ xbuf.length ];
26+
> var st = [ 1 ];
27+
> var oo = 0;
28+
> var ord = 'row-major';
29+
> var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, st, oo, ord );
30+
> var y = new {{alias:@stdlib/ndarray/ctor}}( dt, ybuf, sh, st, oo, ord );
31+
> var alpha = {{alias:@stdlib/ndarray/base/from-scalar}}( 5.0, dt, ord );
32+
33+
> {{alias}}( [ x, y, alpha ] );
34+
> y
35+
<ndarray>[ 22.0, 16.0, -16.0, 21.0, 3.0 ]
36+
37+
See Also
38+
--------
39+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 { typedndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Multiplies a one-dimensional ndarray `x` by a constant `alpha` and adds the result to a one-dimensional ndarray `y`.
27+
*
28+
* @param arrays - array-like object containing an input ndarray, an output ndarray, and a zero-dimensional ndarray containing a scalar constant
29+
* @returns output ndarray
30+
*
31+
* @example
32+
* var scalar2ndarray = require( '@stdlib/ndarray/base/from-scalar' );
33+
* var ndarray = require( '@stdlib/ndarray/base/ctor' );
34+
*
35+
* var xbuf = [ 1.0, 2.0, 3.0, 4.0, 5.0 ];
36+
* var x = new ndarray( 'generic', xbuf, [ 5 ], [ 1 ], 0, 'row-major' );
37+
*
38+
* var ybuf = [ 1.0, 1.0, 1.0, 1.0, 1.0 ];
39+
* var y = new ndarray( 'generic', ybuf, [ 5 ], [ 1 ], 0, 'row-major' );
40+
*
41+
* var alpha = scalar2ndarray( 5.0, 'generic', 'row-major' );
42+
*
43+
* var z = gaxpy( [ x, y, alpha ] );
44+
* // returns <ndarray>[ 6.0, 11.0, 16.0, 21.0, 26.0 ]
45+
*
46+
* var bool = ( z === y );
47+
* // returns true
48+
*/
49+
declare function gaxpy<T extends typedndarray<number> = typedndarray<number>, U extends typedndarray<number> = typedndarray<number>>( arrays: [ T, U, T ] ): U;
50+
51+
52+
// EXPORTS //
53+
54+
export = gaxpy;

0 commit comments

Comments
 (0)