Skip to content

Commit c779e6f

Browse files
committed
feat: add array/base/scattered
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent af3a2de commit c779e6f

File tree

11 files changed

+909
-0
lines changed

11 files changed

+909
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
# scattered
22+
23+
> Scatter a list of provided values to specified indices in a new zero-filled "generic" array.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var scattered = require( '@stdlib/array/base/scattered' );
31+
```
32+
33+
#### scattered( len, indices, values, mode )
34+
35+
Scatters a list of provided values to specified indices in a new zero-filled "generic" array.
36+
37+
```javascript
38+
var out = scattered( 4, [ 1, 3 ], [ 20, 40 ], 'throw' );
39+
// returns [ 0, 20, 0, 40 ]
40+
```
41+
42+
The function supports the following parameters:
43+
44+
- **len**: output array length.
45+
- **indices**: list of indices.
46+
- **values**: values to scatter. When `indices` contains one or more elements, `values` must be broadcast [compatible][@stdlib/ndarray/base/broadcast-shapes] with `indices` (i.e., must have either one element or the same number of elements as `indices`).
47+
- **mode**: index [mode][@stdlib/ndarray/base/ind].
48+
49+
If `indices` is an empty array, the function returns a zero-filled array.
50+
51+
```javascript
52+
var out = scattered( 4, [], [ 20, 40 ], 'throw' );
53+
// returns [ 0, 0, 0, 0 ]
54+
```
55+
56+
The function supports broadcasting a `values` array containing a single element against an `indices` array containing one or more elements.
57+
58+
```javascript
59+
var out = scattered( 4, [ 1, 3 ], [ 20 ], 'throw' );
60+
// returns [ 0, 20, 0, 20 ]
61+
```
62+
63+
</section>
64+
65+
<!-- /.usage -->
66+
67+
<section class="notes">
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 filledBy = require( '@stdlib/array/base/filled-by' );
81+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
82+
var scattered = require( '@stdlib/array/base/scattered' );
83+
84+
// Generate an array of random indices:
85+
var N = discreteUniform( 5, 15 );
86+
var indices = filledBy( N, discreteUniform.factory( 0, 10 ) );
87+
console.log( indices );
88+
89+
// Generate an array of random values:
90+
var values = filledBy( N, discreteUniform.factory( 1000, 2025 ) );
91+
console.log( values );
92+
93+
// Scatter a random sample of elements to a new array:
94+
var out = scattered( 11, indices, values, 'throw' );
95+
console.log( out );
96+
```
97+
98+
</section>
99+
100+
<!-- /.examples -->
101+
102+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
103+
104+
<section class="related">
105+
106+
</section>
107+
108+
<!-- /.related -->
109+
110+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
111+
112+
<section class="links">
113+
114+
[@stdlib/ndarray/base/ind]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/ind
115+
116+
[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes
117+
118+
</section>
119+
120+
<!-- /.links -->
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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 isArray = require( '@stdlib/assert/is-array' );
25+
var zeroTo = require( '@stdlib/array/base/zero-to' );
26+
var pkg = require( './../package.json' ).name;
27+
var scattered = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+'::no_broadcasting:len=100', function benchmark( b ) {
33+
var idx;
34+
var x;
35+
var i;
36+
var v;
37+
38+
x = zeroTo( 100 );
39+
idx = zeroTo( 100 );
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
v = scattered( x.length, idx, x, 'throw' );
44+
if ( typeof v !== 'object' ) {
45+
b.fail( 'should return an array' );
46+
}
47+
}
48+
b.toc();
49+
if ( !isArray( v ) ) {
50+
b.fail( 'should return an array' );
51+
}
52+
b.pass( 'benchmark finished' );
53+
b.end();
54+
});
55+
56+
bench( pkg+'::broadcasting:len=100', function benchmark( b ) {
57+
var idx;
58+
var i;
59+
var v;
60+
61+
idx = zeroTo( 100 );
62+
63+
b.tic();
64+
for ( i = 0; i < b.iterations; i++ ) {
65+
v = scattered( 100, idx, [ i ], 'throw' );
66+
if ( typeof v !== 'object' ) {
67+
b.fail( 'should return an array' );
68+
}
69+
}
70+
b.toc();
71+
if ( !isArray( v ) ) {
72+
b.fail( 'should return an array' );
73+
}
74+
b.pass( 'benchmark finished' );
75+
b.end();
76+
});
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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 pow = require( '@stdlib/math/base/special/pow' );
25+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
26+
var zeroTo = require( '@stdlib/array/base/zero-to' );
27+
var isArray = require( '@stdlib/assert/is-array' );
28+
var pkg = require( './../package.json' ).name;
29+
var scattered = require( './../lib' );
30+
31+
32+
// VARIABLES //
33+
34+
var opts = {
35+
'dtype': 'generic'
36+
};
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Creates a benchmark function.
43+
*
44+
* @private
45+
* @param {PositiveInteger} len - array length
46+
* @returns {Function} benchmark function
47+
*/
48+
function createBenchmark( len ) {
49+
var values;
50+
var idx;
51+
52+
idx = zeroTo( len );
53+
values = [
54+
discreteUniform( len, -10, 0, opts ),
55+
discreteUniform( len, 0, 10, opts )
56+
];
57+
58+
return benchmark;
59+
60+
/**
61+
* Benchmark function.
62+
*
63+
* @private
64+
* @param {Benchmark} b - benchmark instance
65+
*/
66+
function benchmark( b ) {
67+
var v;
68+
var i;
69+
70+
b.tic();
71+
for ( i = 0; i < b.iterations; i++ ) {
72+
v = scattered( len, idx, values[ i%values.length ], 'throw' );
73+
if ( typeof v !== 'object' ) {
74+
b.fail( 'should return an array' );
75+
}
76+
}
77+
b.toc();
78+
if ( !isArray( v ) ) {
79+
b.fail( 'should return an array' );
80+
}
81+
b.pass( 'benchmark finished' );
82+
b.end();
83+
}
84+
}
85+
86+
87+
// MAIN //
88+
89+
/**
90+
* Main execution sequence.
91+
*
92+
* @private
93+
*/
94+
function main() {
95+
var len;
96+
var min;
97+
var max;
98+
var f;
99+
var i;
100+
101+
min = 1; // 10^min
102+
max = 6; // 10^max
103+
104+
for ( i = min; i <= max; i++ ) {
105+
len = pow( 10, i );
106+
f = createBenchmark( len );
107+
bench( pkg+':len='+len, f );
108+
}
109+
}
110+
111+
main();
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
{{alias}}( len, indices, values, mode )
3+
Scatters a list of provided values to specified indices in a new zero-filled
4+
"generic" array.
5+
6+
The function supports broadcasting a `values` array containing a single
7+
element against an `indices` array containing one or more elements.
8+
9+
If `indices` is an empty array, the function returns a zero-filed array.
10+
11+
Parameters
12+
----------
13+
len: integer
14+
Output array length.
15+
16+
indices: ArrayLikeObject<integer>
17+
List of element indices.
18+
19+
values: ArrayLikeObject
20+
Values to scatter. When `indices` contains one or more elements,
21+
`values` must be broadcast compatible with `indices` (i.e., must have
22+
either one element or the same number of elements as `indices`).
23+
24+
mode: string
25+
Specifies how to handle an index outside the interval [0, max], where
26+
`max` is the maximum possible array index. If equal to 'throw', the
27+
function throws an error. If equal to 'normalize', the function throws
28+
an error if provided an out-of-bounds normalized index. If equal to
29+
'wrap', the function wraps around an index using modulo arithmetic. If
30+
equal to 'clamp', the function sets an index to either 0 (minimum index)
31+
or the maximum index.
32+
33+
Returns
34+
-------
35+
out: Array
36+
Output array.
37+
38+
Examples
39+
--------
40+
> var out = {{alias}}( 4, [ 1, 3 ], [ 20, 40 ], 'throw' )
41+
[ 0, 20, 0, 40 ]
42+
43+
See Also
44+
--------
45+

0 commit comments

Comments
 (0)