Skip to content

Commit 1d20b6d

Browse files
headlessNodekgryte
andauthored
feat: add blas/ext/base/greplicate
PR-URL: #11303 Closes: stdlib-js/metr-issue-tracker#249 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 0f3aec0 commit 1d20b6d

File tree

15 files changed

+2300
-0
lines changed

15 files changed

+2300
-0
lines changed
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# greplicate
22+
23+
> Replicate each strided array element a specified number of times.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var greplicate = require( '@stdlib/blas/ext/base/greplicate' );
37+
```
38+
39+
#### greplicate( N, k, x, strideX, out, strideOut )
40+
41+
Replicates each strided array element a specified number of times.
42+
43+
```javascript
44+
var x = [ 1.0, 2.0, 3.0 ];
45+
var out = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];
46+
47+
greplicate( x.length, 2, x, 1, out, 1 );
48+
// out => [ 1.0, 1.0, 2.0, 2.0, 3.0, 3.0 ]
49+
```
50+
51+
The function has the following parameters:
52+
53+
- **N**: number of indexed elements.
54+
- **k**: number of times to replicate each element.
55+
- **x**: input array.
56+
- **strideX**: stride length for `x`.
57+
- **out**: output array.
58+
- **strideOut**: stride length for `out`.
59+
60+
The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to replicate every other element:
61+
62+
```javascript
63+
var x = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];
64+
var out = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];
65+
66+
greplicate( 3, 2, x, 2, out, 1 );
67+
// out => [ 1.0, 1.0, 3.0, 3.0, 5.0, 5.0 ]
68+
```
69+
70+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
71+
72+
<!-- eslint-disable stdlib/capitalized-comments -->
73+
74+
```javascript
75+
var Float64Array = require( '@stdlib/array/float64' );
76+
77+
// Initial arrays...
78+
var x0 = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
79+
var out0 = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
80+
81+
// Create offset views...
82+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
83+
var out1 = new Float64Array( out0.buffer, out0.BYTES_PER_ELEMENT*2 ); // start at 3rd element
84+
85+
greplicate( 3, 2, x1, 2, out1, 1 );
86+
// out0 => <Float64Array>[ 0.0, 0.0, 2.0, 2.0, 4.0, 4.0, 6.0, 6.0 ]
87+
```
88+
89+
#### greplicate.ndarray( N, k, x, strideX, offsetX, out, strideOut, offsetOut )
90+
91+
Replicates each strided array element a specified number of times using alternative indexing semantics.
92+
93+
```javascript
94+
var x = [ 1.0, 2.0, 3.0 ];
95+
var out = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];
96+
97+
greplicate.ndarray( x.length, 2, x, 1, 0, out, 1, 0 );
98+
// out => [ 1.0, 1.0, 2.0, 2.0, 3.0, 3.0 ]
99+
```
100+
101+
The function has the following additional parameters:
102+
103+
- **offsetX**: starting index for `x`.
104+
- **offsetOut**: starting index for `out`.
105+
106+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, offset parameters support indexing semantics based on starting indices. For example, to replicate every other element in the strided input array starting from the second element and to store in the last `N*k` elements of the strided output array starting from the last element:
107+
108+
```javascript
109+
var x = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];
110+
var out = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];
111+
112+
greplicate.ndarray( 3, 2, x, 2, 1, out, -1, out.length-1 );
113+
// out => [ 0.0, 0.0, 6.0, 6.0, 4.0, 4.0, 2.0, 2.0 ]
114+
```
115+
116+
</section>
117+
118+
<!-- /.usage -->
119+
120+
<section class="notes">
121+
122+
## Notes
123+
124+
- If `N <= 0` or `k <= 0`, both functions return `out` unchanged.
125+
- Both functions assume that the output array supports `N*k` indexed elements.
126+
- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).
127+
128+
</section>
129+
130+
<!-- /.notes -->
131+
132+
<section class="examples">
133+
134+
## Examples
135+
136+
<!-- eslint no-undef: "error" -->
137+
138+
```javascript
139+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
140+
var Float64Array = require( '@stdlib/array/float64' );
141+
var greplicate = require( '@stdlib/blas/ext/base/greplicate' );
142+
143+
var x = discreteUniform( 10, -100, 100, {
144+
'dtype': 'float64'
145+
});
146+
console.log( x );
147+
148+
var out = new Float64Array( x.length * 3 );
149+
console.log( out );
150+
151+
greplicate( x.length, 3, x, 1, out, 1 );
152+
console.log( out );
153+
```
154+
155+
</section>
156+
157+
<!-- /.examples -->
158+
159+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
160+
161+
<section class="related">
162+
163+
</section>
164+
165+
<!-- /.related -->
166+
167+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
168+
169+
<section class="links">
170+
171+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
172+
173+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
174+
175+
</section>
176+
177+
<!-- /.links -->
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 zeros = require( '@stdlib/array/zeros' );
28+
var format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
var greplicate = require( './../lib/main.js' );
31+
32+
33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'generic'
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 out = zeros( len * 2, options.dtype );
51+
var x = uniform( len, -100, 100, options );
52+
return benchmark;
53+
54+
/**
55+
* Benchmark function.
56+
*
57+
* @private
58+
* @param {Benchmark} b - benchmark instance
59+
*/
60+
function benchmark( b ) {
61+
var v;
62+
var i;
63+
64+
b.tic();
65+
for ( i = 0; i < b.iterations; i++ ) {
66+
x[ 0 ] += 1.0;
67+
v = greplicate( x.length, 2, x, 1, out, 1 );
68+
if ( isnan( v[ i % ( len * 2 ) ] ) ) {
69+
b.fail( 'should not return NaN' );
70+
}
71+
}
72+
b.toc();
73+
if ( isnan( v[ i % ( len * 2 ) ] ) ) {
74+
b.fail( 'should not return NaN' );
75+
}
76+
b.pass( 'benchmark finished' );
77+
b.end();
78+
}
79+
}
80+
81+
82+
// MAIN //
83+
84+
/**
85+
* Main execution sequence.
86+
*
87+
* @private
88+
*/
89+
function main() {
90+
var len;
91+
var min;
92+
var max;
93+
var f;
94+
var i;
95+
96+
min = 1; // 10^min
97+
max = 6; // 10^max
98+
99+
for ( i = min; i <= max; i++ ) {
100+
len = pow( 10, i );
101+
f = createBenchmark( len );
102+
bench( format( '%s:len=%d', pkg, len ), f );
103+
}
104+
}
105+
106+
main();
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 zeros = require( '@stdlib/array/zeros' );
28+
var format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
var greplicate = require( './../lib/ndarray.js' );
31+
32+
33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'generic'
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 out = zeros( len * 2, options.dtype );
51+
var x = uniform( len, -100, 100, options );
52+
return benchmark;
53+
54+
/**
55+
* Benchmark function.
56+
*
57+
* @private
58+
* @param {Benchmark} b - benchmark instance
59+
*/
60+
function benchmark( b ) {
61+
var v;
62+
var i;
63+
64+
b.tic();
65+
for ( i = 0; i < b.iterations; i++ ) {
66+
x[ 0 ] += 1.0;
67+
v = greplicate( x.length, 2, x, 1, 0, out, 1, 0 );
68+
if ( isnan( v[ i % ( len * 2 ) ] ) ) {
69+
b.fail( 'should not return NaN' );
70+
}
71+
}
72+
b.toc();
73+
if ( isnan( v[ i % ( len * 2 ) ] ) ) {
74+
b.fail( 'should not return NaN' );
75+
}
76+
b.pass( 'benchmark finished' );
77+
b.end();
78+
}
79+
}
80+
81+
82+
// MAIN //
83+
84+
/**
85+
* Main execution sequence.
86+
*
87+
* @private
88+
*/
89+
function main() {
90+
var len;
91+
var min;
92+
var max;
93+
var f;
94+
var i;
95+
96+
min = 1; // 10^min
97+
max = 6; // 10^max
98+
99+
for ( i = min; i <= max; i++ ) {
100+
len = pow( 10, i );
101+
f = createBenchmark( len );
102+
bench( format( '%s:ndarray:len=%d', pkg, len ), f );
103+
}
104+
}
105+
106+
main();

0 commit comments

Comments
 (0)