Skip to content

Commit c02f734

Browse files
MeKaustubh07kgryte
andauthored
feat: add blas/base/ndarray/daxpy
PR-URL: #11318 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent e7b800e commit c02f734

File tree

10 files changed

+877
-0
lines changed

10 files changed

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

0 commit comments

Comments
 (0)