Skip to content

Commit 50e53a1

Browse files
authored
feat: add ndarray/hconcat
PR-URL: #11583 Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#262
1 parent 6f1f614 commit 50e53a1

File tree

14 files changed

+1847
-0
lines changed

14 files changed

+1847
-0
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
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+
# hconcat
22+
23+
> Concatenate a list of [ndarrays][@stdlib/ndarray/ctor] along the last dimension.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var hconcat = require( '@stdlib/ndarray/hconcat' );
41+
```
42+
43+
#### hconcat( arrays )
44+
45+
Concatenates a list of [ndarrays][@stdlib/ndarray/ctor] along the last dimension.
46+
47+
```javascript
48+
var array = require( '@stdlib/ndarray/array' );
49+
50+
var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] );
51+
// returns <ndarray>[ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]
52+
53+
var y = array( [ [ 5.0, 6.0, 7.0 ], [ 8.0, 9.0, 10.0 ] ] );
54+
// returns <ndarray>[ [ 5.0, 6.0, 7.0 ], [ 8.0, 9.0, 10.0 ] ]
55+
56+
var out = hconcat( [ x, y ] );
57+
// returns <ndarray>[ [ 1.0, 2.0, 5.0, 6.0, 7.0 ], [ 3.0, 4.0, 8.0, 9.0, 10.0 ] ]
58+
```
59+
60+
The function accepts the following arguments:
61+
62+
- **arrays**: a list of input [ndarrays][@stdlib/ndarray/ctor]. The data type of the output [ndarray][@stdlib/ndarray/ctor] is determined by applying [type promotion rules][@stdlib/ndarray/promotion-rules] to the list of input [ndarrays][@stdlib/ndarray/ctor]. If provided [ndarrays][@stdlib/ndarray/ctor] having different [memory layouts][@stdlib/ndarray/orders], the output [ndarray][@stdlib/ndarray/ctor] has the [default order][@stdlib/ndarray/defaults].
63+
64+
#### hconcat.assign( arrays, out )
65+
66+
Concatenates a list of [ndarrays][@stdlib/ndarray/ctor] along the last dimension and assigns results to a provided output [ndarray][@stdlib/ndarray/ctor].
67+
68+
```javascript
69+
var array = require( '@stdlib/ndarray/array' );
70+
var zeros = require( '@stdlib/ndarray/zeros' );
71+
72+
var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] );
73+
// returns <ndarray>[ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]
74+
75+
var y = array( [ [ 5.0, 6.0, 7.0 ], [ 8.0, 9.0, 10.0 ] ] );
76+
// returns <ndarray>[ [ 5.0, 6.0, 7.0 ], [ 8.0, 9.0, 10.0 ] ]
77+
78+
var z = zeros( [ 2, 5 ] );
79+
// returns <ndarray>[ [ 0.0, 0.0, 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0, 0.0, 0.0 ] ]
80+
81+
var out = hconcat.assign( [ x, y ], z );
82+
// returns <ndarray>[ [ 1.0, 2.0, 5.0, 6.0, 7.0 ], [ 3.0, 4.0, 8.0, 9.0, 10.0 ] ]
83+
84+
var bool = ( out === z );
85+
// returns true
86+
```
87+
88+
The function accepts the following arguments:
89+
90+
- **arrays**: a list of input [ndarrays][@stdlib/ndarray/ctor]. Must [promote][@stdlib/ndarray/promotion-rules] to a [data type][@stdlib/ndarray/dtypes] which can be (mostly) [safely cast][@stdlib/ndarray/mostly-safe-casts] to the [data type][@stdlib/ndarray/dtypes] of the output [ndarray][@stdlib/ndarray/ctor].
91+
- **out**: output [ndarray][@stdlib/ndarray/ctor].
92+
93+
</section>
94+
95+
<!-- /.usage -->
96+
97+
<!-- Package usage examples. -->
98+
99+
<section class="examples">
100+
101+
## Examples
102+
103+
<!-- eslint no-undef: "error" -->
104+
105+
```javascript
106+
var discreteUniform = require( '@stdlib/random/discrete-uniform' );
107+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
108+
var hconcat = require( '@stdlib/ndarray/hconcat' );
109+
110+
var x = discreteUniform( [ 2, 2 ], 0, 10, {
111+
'dtype': 'generic'
112+
});
113+
console.log( ndarray2array( x ) );
114+
115+
var y = discreteUniform( [ 2, 3 ], 0, 10, {
116+
'dtype': 'generic'
117+
});
118+
console.log( ndarray2array( y ) );
119+
120+
var out = hconcat( [ x, y ] );
121+
console.log( ndarray2array( out ) );
122+
```
123+
124+
</section>
125+
126+
<!-- /.examples -->
127+
128+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
129+
130+
<section class="references">
131+
132+
</section>
133+
134+
<!-- /.references -->
135+
136+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
137+
138+
<section class="related">
139+
140+
</section>
141+
142+
<!-- /.related -->
143+
144+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
145+
146+
<section class="links">
147+
148+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
149+
150+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
151+
152+
[@stdlib/ndarray/orders]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/orders
153+
154+
[@stdlib/ndarray/defaults]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/defaults
155+
156+
[@stdlib/ndarray/promotion-rules]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/promotion-rules
157+
158+
[@stdlib/ndarray/mostly-safe-casts]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/mostly-safe-casts
159+
160+
</section>
161+
162+
<!-- /.links -->
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
25+
var discreteUniform = require( '@stdlib/random/discrete-uniform' );
26+
var zeros = require( '@stdlib/ndarray/zeros' );
27+
var format = require( '@stdlib/string/format' );
28+
var pkg = require( './../package.json' ).name;
29+
var hconcat = require( './../lib/assign.js' );
30+
31+
32+
// MAIN //
33+
34+
bench( format( '%s::1d', pkg ), function benchmark( b ) {
35+
var values;
36+
var opts;
37+
var out;
38+
var v;
39+
var i;
40+
41+
opts = {
42+
'dtype': 'float64'
43+
};
44+
45+
values = [
46+
discreteUniform( [ 32 ], -100, 100, opts ),
47+
discreteUniform( [ 32 ], -100, 100, opts ),
48+
discreteUniform( [ 32 ], -100, 100, opts ),
49+
discreteUniform( [ 32 ], -100, 100, opts )
50+
];
51+
out = zeros( [ 128 ], opts );
52+
53+
b.tic();
54+
for ( i = 0; i < b.iterations; i++ ) {
55+
v = hconcat( values, out );
56+
if ( typeof v !== 'object' ) {
57+
b.fail( 'should return an ndarray' );
58+
}
59+
}
60+
b.toc();
61+
if ( !isndarrayLike( v ) ) {
62+
b.fail( 'should return an ndarray' );
63+
}
64+
b.pass( 'benchmark finished' );
65+
b.end();
66+
});
67+
68+
bench( format( '%s::2d', pkg ), function benchmark( b ) {
69+
var values;
70+
var opts;
71+
var out;
72+
var v;
73+
var i;
74+
75+
opts = {
76+
'dtype': 'float64'
77+
};
78+
79+
values = [
80+
discreteUniform( [ 32, 16 ], -100, 100, opts ),
81+
discreteUniform( [ 32, 16 ], -100, 100, opts ),
82+
discreteUniform( [ 32, 16 ], -100, 100, opts ),
83+
discreteUniform( [ 32, 16 ], -100, 100, opts )
84+
];
85+
out = zeros( [ 32, 64 ], opts );
86+
87+
b.tic();
88+
for ( i = 0; i < b.iterations; i++ ) {
89+
v = hconcat( values, out );
90+
if ( typeof v !== 'object' ) {
91+
b.fail( 'should return an ndarray' );
92+
}
93+
}
94+
b.toc();
95+
if ( !isndarrayLike( v ) ) {
96+
b.fail( 'should return an ndarray' );
97+
}
98+
b.pass( 'benchmark finished' );
99+
b.end();
100+
});
101+
102+
bench( format( '%s::2d,casting', pkg ), function benchmark( b ) {
103+
var values;
104+
var out;
105+
var v;
106+
var i;
107+
108+
/* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */
109+
110+
values = [
111+
discreteUniform( [ 32, 16 ], -100, 100, { 'dtype': 'float64' } ),
112+
discreteUniform( [ 32, 16 ], -100, 100, { 'dtype': 'float32' } ),
113+
discreteUniform( [ 32, 16 ], -100, 100, { 'dtype': 'int32' } ),
114+
discreteUniform( [ 32, 16 ], -100, 100, { 'dtype': 'generic' } )
115+
];
116+
out = zeros( [ 32, 64 ], { 'dtype': 'generic' } );
117+
118+
/* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */
119+
120+
b.tic();
121+
for ( i = 0; i < b.iterations; i++ ) {
122+
v = hconcat( values, out );
123+
if ( typeof v !== 'object' ) {
124+
b.fail( 'should return an ndarray' );
125+
}
126+
}
127+
b.toc();
128+
if ( !isndarrayLike( v ) ) {
129+
b.fail( 'should return an ndarray' );
130+
}
131+
b.pass( 'benchmark finished' );
132+
b.end();
133+
});
134+
135+
bench( format( '%s::3d', pkg ), function benchmark( b ) {
136+
var values;
137+
var opts;
138+
var out;
139+
var v;
140+
var i;
141+
142+
opts = {
143+
'dtype': 'float64'
144+
};
145+
146+
values = [
147+
discreteUniform( [ 4, 16, 16 ], -100, 100, opts ),
148+
discreteUniform( [ 4, 16, 16 ], -100, 100, opts ),
149+
discreteUniform( [ 4, 16, 16 ], -100, 100, opts ),
150+
discreteUniform( [ 4, 16, 16 ], -100, 100, opts )
151+
];
152+
out = zeros( [ 4, 16, 64 ], opts );
153+
154+
b.tic();
155+
for ( i = 0; i < b.iterations; i++ ) {
156+
v = hconcat( values, out );
157+
if ( typeof v !== 'object' ) {
158+
b.fail( 'should return an ndarray' );
159+
}
160+
}
161+
b.toc();
162+
if ( !isndarrayLike( v ) ) {
163+
b.fail( 'should return an ndarray' );
164+
}
165+
b.pass( 'benchmark finished' );
166+
b.end();
167+
});

0 commit comments

Comments
 (0)