Skip to content

Commit 6a6bc1d

Browse files
headlessNodekgryte
andauthored
feat: add ndarray/base/fill
PR-URL: #2817 Ref: #2656 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Muhammad Haris <[email protected]>
1 parent 583e957 commit 6a6bc1d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+6177
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# fill
22+
23+
> Fill an input 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 fill = require( '@stdlib/ndarray/base/fill' );
37+
```
38+
39+
#### fill( x, value )
40+
41+
Fills an input ndarray with a specified value.
42+
43+
<!-- eslint-disable max-len -->
44+
45+
```javascript
46+
var Float64Array = require( '@stdlib/array/float64' );
47+
48+
// Create a data buffer:
49+
var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
50+
51+
// Define the shape of the input array:
52+
var shape = [ 3, 1, 2 ];
53+
54+
// Define the array strides:
55+
var sx = [ 2, 2, 1 ];
56+
57+
// Define the index offset:
58+
var ox = 0;
59+
60+
// Create the input ndarray-like object:
61+
var x = {
62+
'dtype': 'float64',
63+
'data': xbuf,
64+
'shape': shape,
65+
'strides': sx,
66+
'offset': ox,
67+
'order': 'row-major'
68+
};
69+
70+
fill( x, 10.0 );
71+
72+
console.log( x.data );
73+
// => <Float64Array>[ 10.0, 10.0, 10.0, 10.0, 10.0, 10.0 ]
74+
```
75+
76+
The function accepts the following arguments:
77+
78+
- **x**: array-like object containing an input ndarray.
79+
- **value**: scalar value.
80+
81+
A provided ndarray should be an object with the following properties:
82+
83+
- **dtype**: data type.
84+
- **data**: data buffer.
85+
- **shape**: dimensions.
86+
- **strides**: stride lengths.
87+
- **offset**: index offset.
88+
- **order**: specifies whether an ndarray is row-major (C-style) or column major (Fortran-style).
89+
90+
</section>
91+
92+
<!-- /.usage -->
93+
94+
<section class="notes">
95+
96+
## Notes
97+
98+
- If `value` is a number and `x` has a complex [data type][@stdlib/ndarray/dtypes], the function fills an input ndarray with a complex number whose real component equals the provided scalar `value` and whose imaginary component is zero.
99+
- The function **mutates** the input ndarray.
100+
101+
</section>
102+
103+
<!-- /.notes -->
104+
105+
<section class="examples">
106+
107+
## Examples
108+
109+
<!-- eslint no-undef: "error" -->
110+
111+
```javascript
112+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
113+
var filledarrayBy = require( '@stdlib/array/filled-by' );
114+
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
115+
var fill = require( '@stdlib/ndarray/base/fill' );
116+
117+
var x = {
118+
'dtype': 'generic',
119+
'data': filledarrayBy( 10, 'generic', discreteUniform( -100, 100 ) ),
120+
'shape': [ 5, 2 ],
121+
'strides': [ 2, 1 ],
122+
'offset': 0,
123+
'order': 'row-major'
124+
};
125+
126+
fill( x, 10.0 );
127+
console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
128+
```
129+
130+
</section>
131+
132+
<!-- /.examples -->
133+
134+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
135+
136+
<section class="related">
137+
138+
</section>
139+
140+
<!-- /.related -->
141+
142+
<section class="links">
143+
144+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
145+
146+
<!-- <related-links> -->
147+
148+
<!-- </related-links> -->
149+
150+
</section>
151+
152+
<!-- /.links -->
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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/base/discrete-uniform' ).factory;
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var floor = require( '@stdlib/math/base/special/floor' );
28+
var filledarrayBy = require( '@stdlib/array/filled-by' );
29+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
30+
var pkg = require( './../package.json' ).name;
31+
var fill = require( './../lib' );
32+
33+
34+
// VARIABLES //
35+
36+
var types = [ 'float64' ];
37+
var order = [ 'column-major' ];
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Creates a benchmark function.
44+
*
45+
* @private
46+
* @param {PositiveInteger} len - ndarray length
47+
* @param {NonNegativeIntegerArray} shape - ndarray shape
48+
* @param {string} xtype - input ndarray data type
49+
* @returns {Function} benchmark function
50+
*/
51+
function createBenchmark( len, shape, xtype ) {
52+
var x;
53+
54+
x = filledarrayBy( len, xtype, discreteUniform( -100, 100 ) );
55+
x = {
56+
'dtype': xtype,
57+
'data': x,
58+
'shape': shape,
59+
'strides': shape2strides( shape, order ),
60+
'offset': 0,
61+
'order': order
62+
};
63+
return benchmark;
64+
65+
/**
66+
* Benchmark function.
67+
*
68+
* @private
69+
* @param {Benchmark} b - benchmark instance
70+
*/
71+
function benchmark( b ) {
72+
var i;
73+
74+
b.tic();
75+
for ( i = 0; i < b.iterations; i++ ) {
76+
fill( x, i );
77+
if ( isnan( x.data[ i%len ]) ) {
78+
b.fail( 'should not return NaN' );
79+
}
80+
}
81+
b.toc();
82+
if ( isnan( x.data[ i%len ]) ) {
83+
b.fail( 'should not return NaN' );
84+
}
85+
b.pass( 'benchmark finished' );
86+
b.end();
87+
}
88+
}
89+
90+
91+
// MAIN //
92+
93+
/**
94+
* Main execution sequence.
95+
*
96+
* @private
97+
*/
98+
function main() {
99+
var len;
100+
var min;
101+
var max;
102+
var sh;
103+
var t1;
104+
var f;
105+
var i;
106+
var j;
107+
108+
min = 1; // 10^min
109+
max = 6; // 10^max
110+
111+
for ( j = 0; j < types.length; j++ ) {
112+
t1 = types[ j ];
113+
for ( i = min; i <= max; i++ ) {
114+
len = pow( 10, i );
115+
116+
sh = [ len/2, 2, 1, 1, 1, 1, 1, 1, 1, 1 ];
117+
f = createBenchmark( len, sh, t1 );
118+
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+order+',xtype='+t1, f );
119+
120+
sh = [ 1, 1, 1, 1, 1, 1, 1, 1, 2, len/2 ];
121+
f = createBenchmark( len, sh, t1 );
122+
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+order+',xtype='+t1, f );
123+
124+
len = floor( pow( len, 1.0/10.0 ) );
125+
sh = [ len, len, len, len, len, len, len, len, len, len ];
126+
len *= pow( len, 9 );
127+
f = createBenchmark( len, sh, t1 );
128+
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+order+',xtype='+t1, f );
129+
}
130+
}
131+
}
132+
133+
main();

0 commit comments

Comments
 (0)