Skip to content

Commit f8eacb1

Browse files
headlessNodekgryte
andauthored
feat: add blas/ext/join
PR-URL: #9009 Closes: stdlib-js/metr-issue-tracker#124 Ref: #2656 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 59376ad commit f8eacb1

File tree

15 files changed

+2997
-0
lines changed

15 files changed

+2997
-0
lines changed
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
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+
# join
22+
23+
> Return an [ndarray][@stdlib/ndarray/ctor] created by joining elements using a separator along one or more [ndarray][@stdlib/ndarray/ctor] dimensions.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var join = require( '@stdlib/blas/ext/join' );
31+
```
32+
33+
#### join( x\[, options] )
34+
35+
Returns an [ndarray][@stdlib/ndarray/ctor] created by joining elements using a separator 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>
43+
44+
// Perform operation:
45+
var out = join( 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+
- **sep**: separator. May be either a scalar value or an [ndarray][@stdlib/ndarray/ctor] having a "generic" [data type][@stdlib/ndarray/dtypes]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dims`. For example, given the input shape `[2, 3, 4]` and `options.dims=[0]`, an [ndarray][@stdlib/ndarray/ctor] separator value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor], an [ndarray][@stdlib/ndarray/ctor] separator value must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. Default: `,`.
57+
- **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].
58+
- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [ndarray][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`.
59+
60+
By default, the function joins [ndarray][@stdlib/ndarray/ctor] elements by using `,` as a separator. To perform the operation with a different separator, provide a `sep` option.
61+
62+
```javascript
63+
var array = require( '@stdlib/ndarray/array' );
64+
65+
var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
66+
67+
var out = join( x, {
68+
'sep': '|'
69+
});
70+
// returns <ndarray>[ '1|2|3|4|5|6' ]
71+
```
72+
73+
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.
74+
75+
```javascript
76+
var array = require( '@stdlib/ndarray/array' );
77+
78+
var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] );
79+
80+
var out = join( x, {
81+
'dims': [ 0 ]
82+
});
83+
// returns <ndarray>[ '1,3', '2,4' ]
84+
85+
out = join( x, {
86+
'dims': [ 1 ]
87+
});
88+
// returns <ndarray>[ '1,2', '3,4' ]
89+
90+
out = join( x, {
91+
'dims': [ 0, 1 ]
92+
});
93+
// returns <ndarray>[ '1,2,3,4' ]
94+
```
95+
96+
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`.
97+
98+
```javascript
99+
var array = require( '@stdlib/ndarray/array' );
100+
101+
var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] );
102+
103+
var opts = {
104+
'dims': [ 0 ],
105+
'keepdims': true
106+
};
107+
108+
var out = join( x, opts );
109+
// returns <ndarray>[ [ '1,3', '2,4' ] ]
110+
```
111+
112+
#### join.assign( x, out\[, options] )
113+
114+
Joins elements of an input [ndarray][@stdlib/ndarray/ctor] using a separator along one or more [ndarray][@stdlib/ndarray/ctor] dimensions and assigns results to a provided output [ndarray][@stdlib/ndarray/ctor].
115+
116+
```javascript
117+
var array = require( '@stdlib/ndarray/array' );
118+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
119+
120+
var x = array( [ 1.0, 2.0, 3.0, 4.0 ] );
121+
var y = scalar2ndarray( '', {
122+
'dtype': 'generic'
123+
});
124+
125+
var out = join.assign( x, y );
126+
// returns <ndarray>[ '1,2,3,4' ]
127+
128+
var bool = ( out === y );
129+
// returns true
130+
```
131+
132+
The method has the following parameters:
133+
134+
- **x**: input [ndarray][@stdlib/ndarray/ctor].
135+
- **out**: output [ndarray][@stdlib/ndarray/ctor].
136+
- **options**: function options (_optional_).
137+
138+
The method accepts the following options:
139+
140+
- **sep**: separator. May be either a scalar value or an [ndarray][@stdlib/ndarray/ctor] having a "generic" [data type][@stdlib/ndarray/dtypes]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dims`. For example, given the input shape `[2, 3, 4]` and `options.dims=[0]`, an [ndarray][@stdlib/ndarray/ctor] separator value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor], an [ndarray][@stdlib/ndarray/ctor] separator value must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. Default: `,`.
141+
- **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].
142+
143+
</section>
144+
145+
<!-- /.usage -->
146+
147+
<section class="notes">
148+
149+
## Notes
150+
151+
- 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].
152+
153+
</section>
154+
155+
<!-- /.notes -->
156+
157+
<section class="examples">
158+
159+
## Examples
160+
161+
<!-- eslint no-undef: "error" -->
162+
163+
```javascript
164+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
165+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
166+
var ndarray = require( '@stdlib/ndarray/ctor' );
167+
var join = require( '@stdlib/blas/ext/join' );
168+
169+
// Generate an array of random numbers:
170+
var xbuf = discreteUniform( 10, 0, 20, {
171+
'dtype': 'float64'
172+
});
173+
174+
// Wrap in an ndarray:
175+
var x = new ndarray( 'float64', xbuf, [ 5, 2 ], [ 2, 1 ], 0, 'row-major' );
176+
console.log( ndarray2array( x ) );
177+
178+
// Perform operation:
179+
var out = join( x, {
180+
'dims': [ -1 ]
181+
});
182+
183+
// Print the results:
184+
console.log( ndarray2array( out ) );
185+
```
186+
187+
</section>
188+
189+
<!-- /.examples -->
190+
191+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
192+
193+
<section class="related">
194+
195+
</section>
196+
197+
<!-- /.related -->
198+
199+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
200+
201+
<section class="links">
202+
203+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
204+
205+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
206+
207+
[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes
208+
209+
</section>
210+
211+
<!-- /.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) 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 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 join = 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 = join.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)