Skip to content

Commit beef502

Browse files
committed
feat: add blas/ext/base/ndarray/dfill
1 parent aca91ba commit beef502

File tree

10 files changed

+741
-0
lines changed

10 files changed

+741
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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+
# dfill
22+
23+
> Fill a one-dimensional double-precision floating-point ndarray with a specified scalar constant.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var dfill = require( '@stdlib/blas/ext/base/ndarray/dfill' );
37+
```
38+
39+
#### dfill( alpha, arrays )
40+
41+
Fills a one-dimensional double-precision floating-point ndarray with a specified scalar constant.
42+
43+
```javascript
44+
var Float64Array = require( '@stdlib/array/float64' );
45+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
46+
47+
var xbuf = new Float64Array( [ 1.0, 3.0, 4.0, 2.0 ] );
48+
var x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
49+
50+
dfill( 5.0, [ x ] );
51+
// x => <ndarray>[ 5.0, 5.0, 5.0, 5.0 ]
52+
```
53+
54+
The function has the following parameters:
55+
56+
- **alpha**: scalar constant.
57+
- **arrays**: array-like object containing a one-dimensional input ndarray.
58+
59+
</section>
60+
61+
<!-- /.usage -->
62+
63+
<section class="notes">
64+
65+
## Notes
66+
67+
- If provided an empty one-dimensional ndarray, the function returns the input ndarray unchanged.
68+
69+
</section>
70+
71+
<!-- /.notes -->
72+
73+
<section class="examples">
74+
75+
## Examples
76+
77+
<!-- eslint no-undef: "error" -->
78+
79+
```javascript
80+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
81+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
82+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
83+
var dfill = require( '@stdlib/blas/ext/base/ndarray/dfill' );
84+
85+
var xbuf = discreteUniform( 10, -50, 50, {
86+
'dtype': 'float64'
87+
});
88+
var x = new ndarray( 'float64', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
89+
console.log( ndarray2array( x ) );
90+
91+
dfill( 5.0, [ x ] );
92+
console.log( ndarray2array( x ) );
93+
```
94+
95+
</section>
96+
97+
<!-- /.examples -->
98+
99+
<section class="references">
100+
101+
</section>
102+
103+
<!-- /.references -->
104+
105+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
106+
107+
<section class="related">
108+
109+
</section>
110+
111+
<!-- /.related -->
112+
113+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
114+
115+
<section class="links">
116+
117+
</section>
118+
119+
<!-- /.links -->
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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 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 getData = require('@stdlib/ndarray/base/data-buffer');
29+
var pkg = require('./../package.json').name;
30+
var dfill = require('./../lib');
31+
32+
33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'float64'
37+
};
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Creates a benchmark function.
44+
*
45+
* @private
46+
* @param {PositiveInteger} len - array length
47+
* @returns {Function} benchmark function
48+
*/
49+
function createBenchmark(len) {
50+
var xbuf;
51+
var x;
52+
53+
xbuf = uniform(len, -10.0, 10.0, options);
54+
x = new ndarray(options.dtype, xbuf, [len], [1], 0, 'row-major');
55+
56+
return benchmark;
57+
58+
function benchmark(b) {
59+
var d;
60+
var i;
61+
62+
b.tic();
63+
for (i = 0; i < b.iterations; i++) {
64+
dfill(i, [x]);
65+
d = getData(x);
66+
if (isnan(d[i % len])) {
67+
b.fail('should not return NaN');
68+
}
69+
}
70+
b.toc();
71+
if (isnan(d[i % len])) {
72+
b.fail('should not return NaN');
73+
}
74+
b.pass('benchmark finished');
75+
b.end();
76+
}
77+
}
78+
79+
80+
// MAIN //
81+
82+
/**
83+
* Main execution sequence.
84+
*
85+
* @private
86+
*/
87+
function main() {
88+
var len;
89+
var min;
90+
var max;
91+
var f;
92+
var i;
93+
94+
min = 1; // 10^min
95+
max = 6; // 10^max
96+
97+
for (i = min; i <= max; i++) {
98+
len = pow(10, i);
99+
f = createBenchmark(len);
100+
bench(pkg + ':len=' + len, f);
101+
}
102+
}
103+
104+
main();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
{{alias}}( alpha, arrays )
3+
Fills a one-dimensional double-precision floating-point ndarray with a
4+
specified scalar constant.
5+
6+
Parameters
7+
----------
8+
alpha: number
9+
Scalar constant.
10+
11+
arrays: ArrayLikeObject<ndarray>
12+
Array-like object containing a one-dimensional input ndarray.
13+
14+
Returns
15+
-------
16+
x: ndarray
17+
Input ndarray.
18+
19+
Examples
20+
--------
21+
> var xbuf = new {{alias:@stdlib/array/float64}}( [ 1.0, 2.0, 3.0 ] );
22+
> var dt = 'float64';
23+
> var sh = [ xbuf.length ];
24+
> var sx = [ 1 ];
25+
> var ox = 0;
26+
> var ord = 'row-major';
27+
> var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, sx, ox, ord );
28+
> {{alias}}( 5.0, [ x ] );
29+
> xbuf
30+
<Float64Array>[ 5.0, 5.0, 5.0 ]
31+
32+
See Also
33+
--------
34+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 { float64ndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Fills a one-dimensional double-precision floating-point ndarray with a specified scalar constant.
27+
*
28+
* @param alpha - scalar constant
29+
* @param arrays - array-like object containing an input ndarray
30+
* @returns input ndarray
31+
*
32+
* @example
33+
* var Float64Array = require( '@stdlib/array/float64' );
34+
* var ndarray = require( '@stdlib/ndarray/base/ctor' );
35+
*
36+
* var xbuf = new Float64Array( [ 1.0, 3.0, 4.0, 2.0 ] );
37+
* var x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
38+
*
39+
* dfill( 5.0, [ x ] );
40+
* // x => <ndarray>[ 5.0, 5.0, 5.0, 5.0 ]
41+
*/
42+
declare function dfill(alpha: number, arrays: [float64ndarray]): float64ndarray;
43+
44+
45+
// EXPORTS //
46+
47+
export = dfill;
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
/* eslint-disable space-in-parens */
20+
21+
import zeros = require('@stdlib/ndarray/zeros');
22+
import dfill = require('./index');
23+
24+
25+
// TESTS //
26+
27+
// The function returns an ndarray...
28+
{
29+
const x = zeros([10], {
30+
'dtype': 'float64'
31+
});
32+
33+
dfill(5.0, [x]); // $ExpectType float64ndarray
34+
}
35+
36+
// The compiler throws an error if the function is provided a first argument which is not a number...
37+
{
38+
const x = zeros([10], {
39+
'dtype': 'float64'
40+
});
41+
42+
dfill('10', [x]); // $ExpectError
43+
dfill(true, [x]); // $ExpectError
44+
dfill(false, [x]); // $ExpectError
45+
dfill(null, [x]); // $ExpectError
46+
dfill(undefined, [x]); // $ExpectError
47+
dfill([], [x]); // $ExpectError
48+
dfill({}, [x]); // $ExpectError
49+
dfill((x: number): number => x, [x]); // $ExpectError
50+
}
51+
52+
// The compiler throws an error if the function is provided a second argument which is not an array of ndarrays...
53+
{
54+
dfill(5.0, '10'); // $ExpectError
55+
dfill(5.0, 10); // $ExpectError
56+
dfill(5.0, true); // $ExpectError
57+
dfill(5.0, false); // $ExpectError
58+
dfill(5.0, null); // $ExpectError
59+
dfill(5.0, undefined); // $ExpectError
60+
dfill(5.0, []); // $ExpectError
61+
dfill(5.0, {}); // $ExpectError
62+
dfill(5.0, (x: number): number => x); // $ExpectError
63+
}
64+
65+
// The compiler throws an error if the function is provided an unsupported number of arguments...
66+
{
67+
const x = zeros([10], {
68+
'dtype': 'float64'
69+
});
70+
71+
dfill(); // $ExpectError
72+
dfill(5.0); // $ExpectError
73+
dfill(5.0, [x], {}); // $ExpectError
74+
}

0 commit comments

Comments
 (0)