Skip to content

Commit 2968353

Browse files
committed
feat: add @stdlib/blas/ext/base/ndarray/sfill
1 parent 94be096 commit 2968353

10 files changed

Lines changed: 727 additions & 0 deletions

File tree

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
# sfill
22+
23+
> Fill a single-precision floating-point ndarray with a specified value.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var sfill = require( '@stdlib/blas/ext/base/ndarray/sfill' );
37+
```
38+
39+
#### sfill( x, alpha )
40+
41+
Fills a single-precision floating-point ndarray `x` with a specified value `alpha`.
42+
43+
```javascript
44+
var Float32Array = require( '@stdlib/array/float32' );
45+
var array = require( '@stdlib/ndarray/array' );
46+
47+
var x = array( new Float32Array( [ 1.0, 2.0, 3.0 ] ) );
48+
49+
sfill( x, 5.0 );
50+
51+
var xbuf = x.data;
52+
// returns <Float32Array>[ 5.0, 5.0, 5.0 ]
53+
```
54+
55+
The function has the following parameters:
56+
57+
- **x**: input ndarray.
58+
- **alpha**: fill value.
59+
60+
</section>
61+
62+
<!-- /.usage -->
63+
64+
<section class="notes">
65+
66+
## Notes
67+
68+
- If provided an ndarray with a length of `0`, the function returns the ndarray unchanged.
69+
70+
</section>
71+
72+
<!-- /.notes -->
73+
74+
<section class="examples">
75+
76+
## Examples
77+
78+
<!-- eslint no-undef: "error" -->
79+
80+
```javascript
81+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
82+
var Float32Array = require( '@stdlib/array/float32' );
83+
var array = require( '@stdlib/ndarray/array' );
84+
var sfill = require( '@stdlib/blas/ext/base/ndarray/sfill' );
85+
86+
var xbuf = discreteUniform( 10, -100, 100, {
87+
'dtype': 'float32'
88+
});
89+
var x = array( xbuf );
90+
console.log( x.data );
91+
92+
sfill( x, 5.0 );
93+
console.log( x.data );
94+
```
95+
96+
</section>
97+
98+
<!-- /.examples -->
99+
100+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
101+
102+
<section class="related">
103+
104+
</section>
105+
106+
<!-- /.related -->
107+
108+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
109+
110+
<section class="links">
111+
112+
</section>
113+
114+
<!-- /.links -->
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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 Float32Array = require( '@stdlib/array/float32' );
25+
var array = require( '@stdlib/ndarray/array' );
26+
var isFloat32Array = require( '@stdlib/assert/is-float32array' );
27+
var pkg = require( './../package.json' ).name;
28+
var sfill = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var x;
35+
var i;
36+
37+
x = array( new Float32Array( 10 ) );
38+
39+
b.tic();
40+
for ( i = 0; i < b.iterations; i++ ) {
41+
sfill( x, 5.0 );
42+
if ( x.data[ 0 ] !== 5.0 ) {
43+
b.fail( 'should fill ndarray' );
44+
}
45+
}
46+
b.toc();
47+
if ( !isFloat32Array( x.data ) ) {
48+
b.fail( 'should return an ndarray' );
49+
}
50+
b.pass( 'benchmark finished' );
51+
b.end();
52+
});
53+
54+
bench( pkg+'::ndarray,len=10', function benchmark( b ) {
55+
var x;
56+
var i;
57+
58+
x = array( new Float32Array( 10 ) );
59+
60+
b.tic();
61+
for ( i = 0; i < b.iterations; i++ ) {
62+
sfill( x, 5.0 );
63+
if ( x.data[ 0 ] !== 5.0 ) {
64+
b.fail( 'should fill ndarray' );
65+
}
66+
}
67+
b.toc();
68+
if ( !isFloat32Array( x.data ) ) {
69+
b.fail( 'should return an ndarray' );
70+
}
71+
b.pass( 'benchmark finished' );
72+
b.end();
73+
});
74+
75+
bench( pkg+'::ndarray,len=100', function benchmark( b ) {
76+
var x;
77+
var i;
78+
79+
x = array( new Float32Array( 100 ) );
80+
81+
b.tic();
82+
for ( i = 0; i < b.iterations; i++ ) {
83+
sfill( x, 5.0 );
84+
if ( x.data[ 0 ] !== 5.0 ) {
85+
b.fail( 'should fill ndarray' );
86+
}
87+
}
88+
b.toc();
89+
if ( !isFloat32Array( x.data ) ) {
90+
b.fail( 'should return an ndarray' );
91+
}
92+
b.pass( 'benchmark finished' );
93+
b.end();
94+
});
95+
96+
bench( pkg+'::ndarray,len=1000', function benchmark( b ) {
97+
var x;
98+
var i;
99+
100+
x = array( new Float32Array( 1000 ) );
101+
102+
b.tic();
103+
for ( i = 0; i < b.iterations; i++ ) {
104+
sfill( x, 5.0 );
105+
if ( x.data[ 0 ] !== 5.0 ) {
106+
b.fail( 'should fill ndarray' );
107+
}
108+
}
109+
b.toc();
110+
if ( !isFloat32Array( x.data ) ) {
111+
b.fail( 'should return an ndarray' );
112+
}
113+
b.pass( 'benchmark finished' );
114+
b.end();
115+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
{{alias}}( x, alpha )
3+
Fills a single-precision floating-point ndarray with a specified value.
4+
5+
Parameters
6+
----------
7+
x: float32ndarray
8+
Input ndarray.
9+
10+
alpha: number
11+
Fill value.
12+
13+
Returns
14+
-------
15+
out: float32ndarray
16+
Input ndarray.
17+
18+
Examples
19+
--------
20+
> var x = {{alias:@stdlib/ndarray/array}}( new {{alias:@stdlib/array/float32}}( [ 1.0, 2.0, 3.0 ] ) );
21+
> {{alias}}( x, 5.0 )
22+
<ndarray>
23+
> x.data
24+
<Float32Array>[ 5.0, 5.0, 5.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 { float32ndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Fills a single-precision floating-point ndarray with a specified value.
27+
*
28+
* @param x - input ndarray
29+
* @param alpha - fill value
30+
* @returns input ndarray
31+
*
32+
* @example
33+
* var Float32Array = require( '@stdlib/array/float32' );
34+
* var array = require( '@stdlib/ndarray/array' );
35+
*
36+
* var x = array( new Float32Array( [ 1.0, 2.0, 3.0 ] ) );
37+
*
38+
* sfill( x, 5.0 );
39+
*
40+
* var xbuf = x.data;
41+
* // returns <Float32Array>[ 5.0, 5.0, 5.0 ]
42+
*/
43+
declare function sfill( x: float32ndarray, alpha: number ): float32ndarray;
44+
45+
46+
// EXPORTS //
47+
48+
export = sfill;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 zeros = require( '@stdlib/ndarray/zeros' );
20+
import sfill = require( './index' );
21+
22+
23+
// TESTS //
24+
25+
// The function returns an ndarray...
26+
{
27+
const x = zeros( [ 10 ], { 'dtype': 'float32' } );
28+
29+
sfill( x, 5.0 ); // $ExpectType float32ndarray
30+
}
31+
32+
// The compiler throws an error if the function is provided a first argument which is not an ndarray...
33+
{
34+
sfill( 5, 5.0 ); // $ExpectError
35+
sfill( true, 5.0 ); // $ExpectError
36+
sfill( false, 5.0 ); // $ExpectError
37+
sfill( null, 5.0 ); // $ExpectError
38+
sfill( undefined, 5.0 ); // $ExpectError
39+
sfill( [ 1, 2, 3 ], 5.0 ); // $ExpectError
40+
sfill( {}, 5.0 ); // $ExpectError
41+
sfill( ( x: number ): number => x, 5.0 ); // $ExpectError
42+
}
43+
44+
// The compiler throws an error if the function is provided a second argument which is not a number...
45+
{
46+
const x = zeros( [ 10 ], { 'dtype': 'float32' } );
47+
48+
sfill( x, '5' ); // $ExpectError
49+
sfill( x, true ); // $ExpectError
50+
sfill( x, false ); // $ExpectError
51+
sfill( x, null ); // $ExpectError
52+
sfill( x, [] ); // $ExpectError
53+
sfill( x, {} ); // $ExpectError
54+
sfill( x, ( x: number ): number => x ); // $ExpectError
55+
}
56+
57+
// The compiler throws an error if the function is provided an unsupported number of arguments...
58+
{
59+
const x = zeros( [ 10 ], { 'dtype': 'float32' } );
60+
61+
sfill(); // $ExpectError
62+
sfill( x ); // $ExpectError
63+
sfill( x, 5.0, 10 ); // $ExpectError
64+
}

0 commit comments

Comments
 (0)