Skip to content

Commit a65bc43

Browse files
committed
feat: add blas/ext/base/ndarray/gapx
1 parent 53ce62b commit a65bc43

10 files changed

Lines changed: 754 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)