Skip to content

Commit 25f3c66

Browse files
committed
feat: add blas/ext/join
--- 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 e4bb414 commit 25f3c66

15 files changed

Lines changed: 2659 additions & 0 deletions

File tree

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
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 specified separator along an [ndarray][@stdlib/ndarray/ctor] dimension.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var join = require( '@stdlib/blas/ext/join' );
31+
```
32+
33+
#### join( x, separator\[, options] )
34+
35+
Returns an [ndarray][@stdlib/ndarray/ctor] created by joining elements using a specified separator along an [ndarray][@stdlib/ndarray/ctor] dimension.
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>
47+
48+
var v = out.get();
49+
// returns '1,2,3,4,5,6'
50+
```
51+
52+
The function has the following parameters:
53+
54+
- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have at least one dimension.
55+
- **separator**: separator. May be either a scalar value or an [ndarray][@stdlib/ndarray/ctor] with 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 non-reduced dimensions of the input [ndarray][@stdlib/ndarray/ctor]. For example, given the input shape `[2, 3, 4]` and `options.dim=0`, the separator [ndarray][@stdlib/ndarray/ctor] must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`.
56+
- **options**: function options (_optional_).
57+
58+
The function accepts the following options:
59+
60+
- **dim**: dimension over which to perform operation. If provided a negative integer, the dimension along which to perform the operation is determined by counting backward from the last dimension (where `-1` refers to the last dimension). Default: `-1`.
61+
- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [ndarray][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`.
62+
63+
By default, the function performs the operation over elements in the last dimension. To perform the operation over a different dimension, provide a `dim` option.
64+
65+
```javascript
66+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
67+
var array = require( '@stdlib/ndarray/array' );
68+
69+
var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] );
70+
71+
var out = join( x, ',', {
72+
'dim': 0
73+
});
74+
// returns <ndarray>
75+
76+
var v = ndarray2array( out );
77+
// returns [ '1,3', '2,4' ]
78+
```
79+
80+
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`.
81+
82+
```javascript
83+
var array = require( '@stdlib/ndarray/array' );
84+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
85+
86+
var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] );
87+
88+
var opts = {
89+
'dim': 0,
90+
'keepdims': true
91+
};
92+
93+
var out = join( x, ',', opts );
94+
// returns <ndarray>
95+
96+
var v = ndarray2array( out );
97+
// returns [ [ '1,3', '2,4' ] ]
98+
```
99+
100+
#### join.assign( x, separator, out\[, options] )
101+
102+
Joins elements of an input [ndarray][@stdlib/ndarray/ctor] using a specified separator along an [ndarray][@stdlib/ndarray/ctor] dimension and assigns results to a provided output [ndarray][@stdlib/ndarray/ctor].
103+
104+
```javascript
105+
var array = require( '@stdlib/ndarray/array' );
106+
var empty = require( '@stdlib/ndarray/empty' );
107+
108+
var x = array( [ 1.0, 2.0, 3.0, 4.0 ], {
109+
'dtype': 'generic'
110+
});
111+
var y = empty( [], {
112+
'dtype': 'generic'
113+
});
114+
115+
var out = join.assign( x, ',', y );
116+
// returns <ndarray>
117+
118+
var v = out.get();
119+
// returns '1,2,3,4'
120+
121+
var bool = ( out === y );
122+
// returns true
123+
```
124+
125+
The method has the following parameters:
126+
127+
- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have at least one dimension.
128+
- **separator**: separator. May be either a scalar value or an [ndarray][@stdlib/ndarray/ctor] with 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 non-reduced dimensions of the input [ndarray][@stdlib/ndarray/ctor]. For example, given the input shape `[2, 3, 4]` and `options.dim=0`, the separator [ndarray][@stdlib/ndarray/ctor] must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`.
129+
- **out**: output [ndarray][@stdlib/ndarray/ctor].
130+
- **options**: function options (_optional_).
131+
132+
The method accepts the following options:
133+
134+
- **dim**: dimension over which to perform operation. If provided a negative integer, the dimension along which to perform the operation is determined by counting backward from the last dimension (where `-1` refers to the last dimension). Default: `-1`.
135+
136+
</section>
137+
138+
<!-- /.usage -->
139+
140+
<section class="notes">
141+
142+
## Notes
143+
144+
- 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].
145+
146+
</section>
147+
148+
<!-- /.notes -->
149+
150+
<section class="examples">
151+
152+
## Examples
153+
154+
<!-- eslint no-undef: "error" -->
155+
156+
```javascript
157+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
158+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
159+
var ndarray = require( '@stdlib/ndarray/ctor' );
160+
var join = require( '@stdlib/blas/ext/join' );
161+
162+
// Generate an array of random numbers:
163+
var xbuf = discreteUniform( 10, 0, 20, {
164+
'dtype': 'float64'
165+
});
166+
167+
// Wrap in an ndarray:
168+
var x = new ndarray( 'float64', xbuf, [ 5, 2 ], [ 2, 1 ], 0, 'row-major' );
169+
console.log( ndarray2array( x ) );
170+
171+
// Perform operation:
172+
var out = join( x, ',', {
173+
'dim': 0
174+
});
175+
176+
// Print the results:
177+
console.log( ndarray2array( out ) );
178+
```
179+
180+
</section>
181+
182+
<!-- /.examples -->
183+
184+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
185+
186+
<section class="related">
187+
188+
</section>
189+
190+
<!-- /.related -->
191+
192+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
193+
194+
<section class="links">
195+
196+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
197+
198+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
199+
200+
[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes
201+
202+
</section>
203+
204+
<!-- /.links -->
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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 pkg = require( './../package.json' ).name;
30+
var join = require( './../lib' );
31+
32+
33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'float64'
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;
51+
var x;
52+
53+
x = uniform( len, -50.0, 50.0, options );
54+
x = new ndarray( options.dtype, x, [ len ], [ 1 ], 0, 'row-major' );
55+
56+
out = empty( [], {
57+
'dtype': 'generic'
58+
});
59+
60+
return benchmark;
61+
62+
/**
63+
* Benchmark function.
64+
*
65+
* @private
66+
* @param {Benchmark} b - benchmark instance
67+
*/
68+
function benchmark( b ) {
69+
var o;
70+
var i;
71+
72+
b.tic();
73+
for ( i = 0; i < b.iterations; i++ ) {
74+
o = join.assign( x, ',', out );
75+
if ( typeof o !== 'object' ) {
76+
b.fail( 'should return an ndarray' );
77+
}
78+
}
79+
b.toc();
80+
if ( isnan( o.get() ) ) {
81+
b.fail( 'should not return NaN' );
82+
}
83+
b.pass( 'benchmark finished' );
84+
b.end();
85+
}
86+
}
87+
88+
89+
// MAIN //
90+
91+
/**
92+
* Main execution sequence.
93+
*
94+
* @private
95+
*/
96+
function main() {
97+
var len;
98+
var min;
99+
var max;
100+
var f;
101+
var i;
102+
103+
min = 1; // 10^min
104+
max = 6; // 10^max
105+
106+
for ( i = min; i <= max; i++ ) {
107+
len = pow( 10, i );
108+
f = createBenchmark( len );
109+
bench( pkg+':assign:dtype='+options.dtype+',len='+len, f );
110+
}
111+
}
112+
113+
main();

0 commit comments

Comments
 (0)