Skip to content

Commit 96d593c

Browse files
committed
feat: add blas/ext/join-between
--- 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 2840211 commit 96d593c

File tree

15 files changed

+2264
-0
lines changed

15 files changed

+2264
-0
lines changed
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
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+
# joinBetween
22+
23+
> Return an [ndarray][@stdlib/ndarray/ctor] created by joining elements using specified separators for each pair of consecutive elements along one or more [ndarray][@stdlib/ndarray/ctor] dimensions.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var joinBetween = require( '@stdlib/blas/ext/join-between' );
31+
```
32+
33+
#### joinBetween( x\[, options] )
34+
35+
Returns an [ndarray][@stdlib/ndarray/ctor] created by joining elements using specified separators for each pair of consecutive elements along one or more [ndarray][@stdlib/ndarray/ctor] dimensions.
36+
37+
```javascript
38+
var array = require( '@stdlib/ndarray/array' );
39+
40+
// Create an input ndarray:
41+
var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
42+
// returns <ndarray>[ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]
43+
44+
// Perform operation:
45+
var out = joinBetween( x );
46+
// returns <ndarray>[ '1,2,3,4,5,6' ]
47+
```
48+
49+
The function has the following parameters:
50+
51+
- **x**: input [ndarray][@stdlib/ndarray/ctor].
52+
- **options**: function options (_optional_).
53+
54+
The function accepts the following options:
55+
56+
- **prefix**: prefix to prepend to each joined string. Default: `''`.
57+
- **suffix**: suffix to append to each joined string. Default: `''`.
58+
- **separators**: separators. Must be an array-like object. When provided an array containing a single element, the same separator is used between all consecutive pairs of elements. When containing multiple elements, each element specifies the separator to use between consecutive pairs, and the number of elements must equal one less than the number of elements along the reduced dimensions. Default: `[ ',' ]`.
59+
- **dims**: list of dimensions over which to perform operation. If not provided, the function performs the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor].
60+
- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [ndarray][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`.
61+
62+
By default, the function joins [ndarray][@stdlib/ndarray/ctor] elements by using `,` as a separator between all consecutive pairs of elements. To perform the operation with a different separator, provide a `separators` option.
63+
64+
```javascript
65+
var array = require( '@stdlib/ndarray/array' );
66+
67+
var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
68+
69+
var out = joinBetween( x, {
70+
'separators': [ '|' ]
71+
});
72+
// returns <ndarray>[ '1|2|3|4|5|6' ]
73+
```
74+
75+
To specify a prefix and suffix to prepend and append to each joined string, provide `prefix` and `suffix` options.
76+
77+
```javascript
78+
var array = require( '@stdlib/ndarray/array' );
79+
80+
var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
81+
// returns <ndarray>[ 1, 2, 3, 4, 5, 6 ]
82+
83+
var out = joinBetween( x, {
84+
'prefix': '[ ',
85+
'suffix': ' ]',
86+
'separators': [ ', ' ]
87+
});
88+
// returns <ndarray>[ '[ 1, 2, 3, 4, 5, 6 ]' ]
89+
```
90+
91+
By default, the function performs the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. To perform the operation over specific dimensions, provide a `dims` option.
92+
93+
```javascript
94+
var array = require( '@stdlib/ndarray/array' );
95+
96+
var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] );
97+
// returns <ndarray>[ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]
98+
99+
var out = joinBetween( x, {
100+
'dims': [ 0 ]
101+
});
102+
// returns <ndarray>[ '1,3', '2,4' ]
103+
```
104+
105+
By default, the function excludes reduced dimensions from the output [ndarray][@stdlib/ndarray/ctor]. To include the reduced dimensions as singleton dimensions, set the `keepdims` option to `true`.
106+
107+
```javascript
108+
var array = require( '@stdlib/ndarray/array' );
109+
110+
var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] );
111+
// returns <ndarray>[ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]
112+
113+
var out = joinBetween( x, {
114+
'dims': [ 0 ],
115+
'keepdims': true
116+
});
117+
// returns <ndarray>[ [ '1,3', '2,4' ] ]
118+
```
119+
120+
#### joinBetween.assign( x, out\[, options] )
121+
122+
Joins elements of an input [ndarray][@stdlib/ndarray/ctor] using specified separators for each pair of consecutive elements along one or more [ndarray][@stdlib/ndarray/ctor] dimensions and assigns results to a provided output [ndarray][@stdlib/ndarray/ctor].
123+
124+
```javascript
125+
var array = require( '@stdlib/ndarray/array' );
126+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
127+
128+
var x = array( [ 1.0, 2.0, 3.0, 4.0 ] );
129+
var y = scalar2ndarray( '', {
130+
'dtype': 'generic'
131+
});
132+
133+
var out = joinBetween.assign( x, y );
134+
// returns <ndarray>[ '1,2,3,4' ]
135+
136+
var bool = ( out === y );
137+
// returns true
138+
```
139+
140+
The method has the following parameters:
141+
142+
- **x**: input [ndarray][@stdlib/ndarray/ctor].
143+
- **out**: output [ndarray][@stdlib/ndarray/ctor].
144+
- **options**: function options (_optional_).
145+
146+
The method accepts the following options:
147+
148+
- **prefix**: prefix to prepend to each joined string. Default: `''`.
149+
- **suffix**: suffix to append to each joined string. Default: `''`.
150+
- **separators**: separators. Must be an array-like object. When provided an array containing a single element, the same separator is used between all consecutive pairs of elements. When containing multiple elements, each element specifies the separator to use between consecutive pairs, and the number of elements must equal one less than the number of elements along the reduced dimensions. Default: `[ ',' ]`.
151+
- **dims**: list of dimensions over which to perform operation. If not provided, the function performs the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor].
152+
153+
</section>
154+
155+
<!-- /.usage -->
156+
157+
<section class="notes">
158+
159+
## Notes
160+
161+
- Setting the `keepdims` option to `true` can be useful when wanting to ensure that the output [ndarray][@stdlib/ndarray/ctor] is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with ndarrays having the same shape as the input [ndarray][@stdlib/ndarray/ctor].
162+
- When providing an array containing multiple separators, each element specifies the separator to use between consecutive pairs. The same separators are used for every complement position when reducing specific dimensions.
163+
164+
</section>
165+
166+
<!-- /.notes -->
167+
168+
<section class="examples">
169+
170+
## Examples
171+
172+
<!-- eslint no-undef: "error" -->
173+
174+
```javascript
175+
var uniform = require( '@stdlib/random/uniform' );
176+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
177+
var joinBetween = require( '@stdlib/blas/ext/join-between' );
178+
179+
var x = uniform( [ 5, 2 ], 0.0, 20.0 );
180+
console.log( ndarray2array( x ) );
181+
182+
var out = joinBetween( x, {
183+
'dims': [ -1 ],
184+
'prefix': '[ ',
185+
'suffix': ' ]',
186+
'separators': [ ', ' ]
187+
});
188+
console.log( ndarray2array( out ) );
189+
```
190+
191+
</section>
192+
193+
<!-- /.examples -->
194+
195+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
196+
197+
<section class="related">
198+
199+
</section>
200+
201+
<!-- /.related -->
202+
203+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
204+
205+
<section class="links">
206+
207+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
208+
209+
[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes
210+
211+
</section>
212+
213+
<!-- /.links -->
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var uniform = require( '@stdlib/random/array/uniform' );
27+
var empty = require( '@stdlib/ndarray/empty' );
28+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
29+
var format = require( '@stdlib/string/format' );
30+
var pkg = require( './../package.json' ).name;
31+
var joinBetween = require( './../lib' );
32+
33+
34+
// VARIABLES //
35+
36+
var options = {
37+
'dtype': 'float64'
38+
};
39+
40+
41+
// FUNCTIONS //
42+
43+
/**
44+
* Creates a benchmark function.
45+
*
46+
* @private
47+
* @param {PositiveInteger} len - array length
48+
* @returns {Function} benchmark function
49+
*/
50+
function createBenchmark( len ) {
51+
var out;
52+
var x;
53+
54+
x = uniform( len, -50.0, 50.0, options );
55+
x = new ndarray( options.dtype, x, [ len ], [ 1 ], 0, 'row-major' );
56+
57+
out = empty( [], {
58+
'dtype': 'generic'
59+
});
60+
61+
return benchmark;
62+
63+
/**
64+
* Benchmark function.
65+
*
66+
* @private
67+
* @param {Benchmark} b - benchmark instance
68+
*/
69+
function benchmark( b ) {
70+
var o;
71+
var i;
72+
73+
b.tic();
74+
for ( i = 0; i < b.iterations; i++ ) {
75+
o = joinBetween.assign( x, out );
76+
if ( typeof o !== 'object' ) {
77+
b.fail( 'should return an ndarray' );
78+
}
79+
}
80+
b.toc();
81+
if ( isnan( o.get() ) ) {
82+
b.fail( 'should not return NaN' );
83+
}
84+
b.pass( 'benchmark finished' );
85+
b.end();
86+
}
87+
}
88+
89+
90+
// MAIN //
91+
92+
/**
93+
* Main execution sequence.
94+
*
95+
* @private
96+
*/
97+
function main() {
98+
var len;
99+
var min;
100+
var max;
101+
var f;
102+
var i;
103+
104+
min = 1; // 10^min
105+
max = 6; // 10^max
106+
107+
for ( i = min; i <= max; i++ ) {
108+
len = pow( 10, i );
109+
f = createBenchmark( len );
110+
bench( format( '%s:assign:dtype=%s,len=%d', pkg, options.dtype, len ), f );
111+
}
112+
}
113+
114+
main();

0 commit comments

Comments
 (0)