Skip to content

Commit 945f942

Browse files
committed
feat: add blas/ext/base/ndarray/sapx
1 parent 53ce62b commit 945f942

10 files changed

Lines changed: 745 additions & 0 deletions

File tree

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# sapx
22+
23+
> Add a scalar constant to each element in a single-precision floating-point ndarray.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var sapx = require( '@stdlib/blas/ext/base/ndarray/sapx' );
31+
```
32+
33+
#### sapx( x, alpha )
34+
35+
Adds a scalar constant to each element in a single-precision floating-point ndarray.
36+
37+
```javascript
38+
var Float32Array = require( '@stdlib/array/float32' );
39+
var ndarray = require( '@stdlib/ndarray/ctor' );
40+
41+
var xbuf = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] );
42+
var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
43+
44+
sapx( x, 5.0 );
45+
46+
var y = x.data;
47+
// returns <Float32Array>[ 6.0, 7.0, 8.0, 9.0 ]
48+
```
49+
50+
The function has the following parameters:
51+
52+
- **x**: input ndarray.
53+
- **alpha**: scalar constant.
54+
55+
Note that indexing is relative to the first index. To introduce an offset, use [`ndarray`][@stdlib/ndarray/ctor] view creation.
56+
57+
```javascript
58+
var Float32Array = require( '@stdlib/array/float32' );
59+
var ndarray = require( '@stdlib/ndarray/ctor' );
60+
61+
// Initial array:
62+
var xbuf = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
63+
64+
// Create an ndarray view:
65+
var x = new ndarray( 'float32', xbuf, [ 3 ], [ 1 ], 2, 'row-major' );
66+
67+
sapx( x, 5.0 );
68+
69+
var y = x.data;
70+
// returns <Float32Array>[ 1.0, 2.0, 8.0, 9.0, 10.0 ]
71+
```
72+
73+
</section>
74+
75+
<!-- /.usage -->
76+
77+
<section class="notes">
78+
79+
## Notes
80+
81+
- The function **mutates** the input ndarray.
82+
83+
</section>
84+
85+
<!-- /.notes -->
86+
87+
<section class="examples">
88+
89+
## Examples
90+
91+
<!-- eslint no-undef: "error" -->
92+
93+
```javascript
94+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
95+
var ndarray = require( '@stdlib/ndarray/ctor' );
96+
var sapx = require( '@stdlib/blas/ext/base/ndarray/sapx' );
97+
98+
var xbuf = discreteUniform( 10, 0, 100, {
99+
'dtype': 'float32'
100+
});
101+
var x = new ndarray( 'float32', xbuf, [ 10 ], [ 1 ], 0, 'row-major' );
102+
103+
console.log( x.data );
104+
105+
sapx( x, 5.0 );
106+
107+
console.log( x.data );
108+
```
109+
110+
</section>
111+
112+
<!-- /.examples -->
113+
114+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
115+
116+
<section class="related">
117+
118+
</section>
119+
120+
<!-- /.related -->
121+
122+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
123+
124+
<section class="links">
125+
126+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/ndarray-ctor
127+
128+
</section>
129+
130+
<!-- /.links -->
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var ndarray = require( '@stdlib/ndarray/ctor' );
28+
var pkg = require( './../package.json' ).name;
29+
var sapx = require( './../lib' );
30+
31+
32+
// FUNCTIONS //
33+
34+
/**
35+
* Creates a benchmark function.
36+
*
37+
* @private
38+
* @param {PositiveInteger} len - array length
39+
* @returns {Function} benchmark function
40+
*/
41+
function createBenchmark( len ) {
42+
var x;
43+
44+
x = new ndarray( 'float32', discreteUniform( len, -100, 100, {
45+
'dtype': 'float32'
46+
}), [ len ], [ 1 ], 0, 'row-major' );
47+
48+
return benchmark;
49+
50+
/**
51+
* Benchmark function.
52+
*
53+
* @private
54+
* @param {Benchmark} b - benchmark instance
55+
*/
56+
function benchmark( b ) {
57+
var z;
58+
var i;
59+
60+
b.tic();
61+
for ( i = 0; i < b.iterations; i++ ) {
62+
z = sapx( x, 5.0 );
63+
if ( isnanf( z.data[ i % len ] ) ) {
64+
b.fail( 'should not return NaN' );
65+
}
66+
}
67+
b.toc();
68+
if ( isnanf( z.data[ i % len ] ) ) {
69+
b.fail( 'should not return NaN' );
70+
}
71+
b.pass( 'benchmark finished' );
72+
b.end();
73+
}
74+
}
75+
76+
77+
// MAIN //
78+
79+
/**
80+
* Main execution sequence.
81+
*
82+
* @private
83+
*/
84+
function main() {
85+
var len;
86+
var min;
87+
var max;
88+
var f;
89+
var i;
90+
91+
min = 1; // 10^min
92+
max = 6; // 10^max
93+
94+
for ( i = min; i <= max; i++ ) {
95+
len = pow( 10, i );
96+
f = createBenchmark( len );
97+
bench( pkg+':len='+len, f );
98+
}
99+
}
100+
101+
main();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{{alias}}( x, alpha )
2+
Adds a scalar constant to each element in a single-precision floating-
3+
point ndarray.
4+
5+
Parameters
6+
----------
7+
x: float32ndarray
8+
Input ndarray.
9+
10+
alpha: number
11+
Scalar constant.
12+
13+
Returns
14+
-------
15+
out: float32ndarray
16+
Input ndarray.
17+
18+
Examples
19+
--------
20+
> var xbuf = new {{alias:@stdlib/array/float32}}( [ 1.0, 2.0, 3.0, 4.0 ] );
21+
> var x = {{alias:@stdlib/ndarray/ctor}}( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
22+
> {{alias}}( x, 5.0 )
23+
<ndarray>
24+
> x.data
25+
<Float32Array>[ 6.0, 7.0, 8.0, 9.0 ]
26+
27+
See Also
28+
--------
29+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 { float32ndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Adds a scalar constant to each element in a single-precision floating-point ndarray.
27+
*
28+
* @param x - input ndarray
29+
* @param alpha - scalar constant
30+
* @returns input ndarray
31+
*
32+
* @example
33+
* var Float32Array = require( '@stdlib/array/float32' );
34+
* var ndarray = require( '@stdlib/ndarray/ctor' );
35+
*
36+
* var xbuf = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] );
37+
* var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
38+
*
39+
* sapx( x, 5.0 );
40+
*
41+
* var y = x.data;
42+
* // returns <Float32Array>[ 6.0, 7.0, 8.0, 9.0 ]
43+
*/
44+
declare function sapx( x: float32ndarray, alpha: number ): float32ndarray;
45+
46+
47+
// EXPORTS //
48+
49+
export = sapx;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
import sapx = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an ndarray...
25+
{
26+
const x: any = null;
27+
sapx( x, 5.0 ); // $ExpectType float32ndarray
28+
}
29+
30+
// The compiler throws an error if the function is provided a first argument which is not an ndarray...
31+
{
32+
sapx( 123, 5.0 ); // $ExpectError
33+
sapx( true, 5.0 ); // $ExpectError
34+
sapx( false, 5.0 ); // $ExpectError
35+
sapx( null, 5.0 ); // $ExpectError
36+
sapx( undefined, 5.0 ); // $ExpectError
37+
sapx( '5', 5.0 ); // $ExpectError
38+
sapx( [ '1', '2' ], 5.0 ); // $ExpectError
39+
sapx( {}, 5.0 ); // $ExpectError
40+
sapx( ( x: number ): number => x, 5.0 ); // $ExpectError
41+
}
42+
43+
// The compiler throws an error if the function is provided a second argument which is not a number...
44+
{
45+
const x: any = null;
46+
sapx( x, '5' ); // $ExpectError
47+
sapx( x, true ); // $ExpectError
48+
sapx( x, false ); // $ExpectError
49+
sapx( x, null ); // $ExpectError
50+
sapx( x, undefined ); // $ExpectError
51+
sapx( x, [ '1' ] ); // $ExpectError
52+
sapx( x, {} ); // $ExpectError
53+
sapx( x, ( x: number ): number => x ); // $ExpectError
54+
}
55+
56+
// The compiler throws an error if the function is provided an unsupported number of arguments...
57+
{
58+
const x: any = null;
59+
sapx(); // $ExpectError
60+
sapx( x ); // $ExpectError
61+
sapx( x, 5.0, 10 ); // $ExpectError
62+
}

0 commit comments

Comments
 (0)