Skip to content

Commit f375988

Browse files
committed
feat: add ndarray/concat
1 parent fc43c32 commit f375988

File tree

13 files changed

+1455
-0
lines changed

13 files changed

+1455
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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+
# concat
22+
23+
> Concatenate a list of ndarrays along a specified ndarray 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 concat = require( '@stdlib/ndarray/concat' );
41+
```
42+
43+
#### concat( arrays, dim )
44+
45+
Concatenates a list of ndarrays along a specified ndarray dimension.
46+
47+
```javascript
48+
var ndarray = require( '@stdlib/ndarray/ctor' );
49+
var Float64Array = require( '@stdlib/array/float64' );
50+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
51+
52+
var xbuf = new Float64Array( [ -1.0, 2.0, -3.0, 4.0 ] );
53+
var x = new ndarray( 'float64', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' );
54+
55+
var ybuf = new Float64Array( [ -5.0, 6.0, -7.0, 8.0, -9.0, 10.0 ] );
56+
var y = new ndarray( 'float64', ybuf, [ 2, 3 ], [ 3, 1 ], 0, 'row-major' );
57+
58+
var out = concat( [ x, y ], -1 );
59+
// returns <ndarray>
60+
61+
var arr = ndarray2array( out );
62+
// returns [ [ -1.0, 2.0, -5.0, 6.0, 7.0 ], [ -3.0, 4.0, 8.0, 9.0, 10.0 ] ]
63+
```
64+
65+
The function accepts the following arguments:
66+
67+
- **arrays**: a list of input ndarrays.
68+
- **dim**: dimension along which the arrays are concatenated. Default: `-1`.
69+
70+
#### concat.assign( arrays, out, dim )
71+
72+
<!-- TODO: Add documentation -->
73+
74+
</section>
75+
76+
<!-- /.usage -->
77+
78+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
79+
80+
<section class="notes">
81+
82+
## Notes
83+
84+
</section>
85+
86+
<!-- /.notes -->
87+
88+
<!-- Package usage examples. -->
89+
90+
<section class="examples">
91+
92+
## Examples
93+
94+
```javascript
95+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
96+
var ndarray = require( '@stdlib/ndarray/ctor' );
97+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
98+
var concat = require( '@stdlib/ndarray/concat' );
99+
100+
var xbuf = discreteUniform( 6, 0, 10, {
101+
'dtype': 'generic'
102+
});
103+
var x = new ndarray( 'generic', xbuf, [ 2, 3 ], [ 3, 1 ], 0, 'row-major' );
104+
console.log( ndarray2array( x ) );
105+
106+
var ybuf = discreteUniform( 8, 0, 10, {
107+
'dtype': 'generic'
108+
});
109+
var y = new ndarray( 'generic', ybuf, [ 2, 4 ], [ 4, 1 ], 0, 'row-major' );
110+
console.log( ndarray2array( y ) );
111+
112+
var out = concat( [ x, y ], -1 );
113+
console.log( ndarray2array( out ) );
114+
```
115+
116+
</section>
117+
118+
<!-- /.examples -->
119+
120+
<!-- 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. -->
121+
122+
<section class="references">
123+
124+
</section>
125+
126+
<!-- /.references -->
127+
128+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
129+
130+
<section class="related">
131+
132+
</section>
133+
134+
<!-- /.related -->
135+
136+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
137+
138+
<section class="links">
139+
140+
</section>
141+
142+
<!-- /.links -->
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
25+
var zeros = require( '@stdlib/ndarray/zeros' );
26+
var pkg = require( './../package.json' ).name;
27+
var concat = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+'::1d', function benchmark( b ) {
33+
var values;
34+
var out;
35+
var v;
36+
var i;
37+
38+
/* eslint-disable object-curly-newline */
39+
40+
values = [
41+
zeros( [ 2 ], { 'dtype': 'float64' }),
42+
zeros( [ 2 ], { 'dtype': 'float32' }),
43+
zeros( [ 2 ], { 'dtype': 'int32' }),
44+
zeros( [ 2 ], { 'dtype': 'complex128' }),
45+
zeros( [ 2 ], { 'dtype': 'generic' })
46+
];
47+
out = zeros( [ 10 ], { 'dtype': 'float64' });
48+
49+
b.tic();
50+
for ( i = 0; i < b.iterations; i++ ) {
51+
v = concat( values, out, -1 );
52+
if ( typeof v !== 'object' ) {
53+
b.fail( 'should return an ndarray' );
54+
}
55+
}
56+
b.toc();
57+
if ( !isndarrayLike( v ) ) {
58+
b.fail( 'should return an ndarray' );
59+
}
60+
b.pass( 'benchmark finished' );
61+
b.end();
62+
});
63+
64+
bench( pkg+'::2d', function benchmark( b ) {
65+
var values;
66+
var out;
67+
var v;
68+
var i;
69+
70+
/* eslint-disable object-curly-newline */
71+
72+
values = [
73+
zeros( [ 2, 2 ], { 'dtype': 'float64' }),
74+
zeros( [ 2, 2 ], { 'dtype': 'float32' }),
75+
zeros( [ 2, 2 ], { 'dtype': 'int32' }),
76+
zeros( [ 2, 2 ], { 'dtype': 'complex128' }),
77+
zeros( [ 2, 2 ], { 'dtype': 'generic' })
78+
];
79+
out = zeros( [ 2, 10 ], { 'dtype': 'float64' });
80+
81+
b.tic();
82+
for ( i = 0; i < b.iterations; i++ ) {
83+
v = concat( values, out, -1 );
84+
if ( typeof v !== 'object' ) {
85+
b.fail( 'should return an ndarray' );
86+
}
87+
}
88+
b.toc();
89+
if ( !isndarrayLike( v ) ) {
90+
b.fail( 'should return an ndarray' );
91+
}
92+
b.pass( 'benchmark finished' );
93+
b.end();
94+
});
95+
96+
bench( pkg+'::3d', function benchmark( b ) {
97+
var values;
98+
var out;
99+
var v;
100+
var i;
101+
102+
/* eslint-disable object-curly-newline */
103+
104+
values = [
105+
zeros( [ 2, 2, 2 ], { 'dtype': 'float64' }),
106+
zeros( [ 2, 2, 2 ], { 'dtype': 'float32' }),
107+
zeros( [ 2, 2, 2 ], { 'dtype': 'int32' }),
108+
zeros( [ 2, 2, 2 ], { 'dtype': 'complex128' }),
109+
zeros( [ 2, 2, 2 ], { 'dtype': 'generic' })
110+
];
111+
out = zeros( [ 2, 2, 10 ], { 'dtype': 'float64' });
112+
113+
b.tic();
114+
for ( i = 0; i < b.iterations; i++ ) {
115+
v = concat( values, out, -1 );
116+
if ( typeof v !== 'object' ) {
117+
b.fail( 'should return an ndarray' );
118+
}
119+
}
120+
b.toc();
121+
if ( !isndarrayLike( v ) ) {
122+
b.fail( 'should return an ndarray' );
123+
}
124+
b.pass( 'benchmark finished' );
125+
b.end();
126+
});
127+
128+
bench( pkg+'::4d', function benchmark( b ) {
129+
var values;
130+
var out;
131+
var v;
132+
var i;
133+
134+
/* eslint-disable object-curly-newline */
135+
136+
values = [
137+
zeros( [ 2, 2, 2, 2 ], { 'dtype': 'float64' }),
138+
zeros( [ 2, 2, 2, 2 ], { 'dtype': 'float32' }),
139+
zeros( [ 2, 2, 2, 2 ], { 'dtype': 'int32' }),
140+
zeros( [ 2, 2, 2, 2 ], { 'dtype': 'complex128' }),
141+
zeros( [ 2, 2, 2, 2 ], { 'dtype': 'generic' })
142+
];
143+
out = zeros( [ 2, 2, 2, 10 ], { 'dtype': 'float64' });
144+
145+
b.tic();
146+
for ( i = 0; i < b.iterations; i++ ) {
147+
v = concat( values, out, -1 );
148+
if ( typeof v !== 'object' ) {
149+
b.fail( 'should return an ndarray' );
150+
}
151+
}
152+
b.toc();
153+
if ( !isndarrayLike( v ) ) {
154+
b.fail( 'should return an ndarray' );
155+
}
156+
b.pass( 'benchmark finished' );
157+
b.end();
158+
});
159+
160+
bench( pkg+'::5d', function benchmark( b ) {
161+
var values;
162+
var out;
163+
var v;
164+
var i;
165+
166+
/* eslint-disable object-curly-newline */
167+
168+
values = [
169+
zeros( [ 2, 2, 2, 2, 2 ], { 'dtype': 'float64' }),
170+
zeros( [ 2, 2, 2, 2, 2 ], { 'dtype': 'float32' }),
171+
zeros( [ 2, 2, 2, 2, 2 ], { 'dtype': 'int32' }),
172+
zeros( [ 2, 2, 2, 2, 2 ], { 'dtype': 'complex128' }),
173+
zeros( [ 2, 2, 2, 2, 2 ], { 'dtype': 'generic' })
174+
];
175+
out = zeros( [ 2, 2, 2, 2, 10 ], { 'dtype': 'float64' });
176+
177+
b.tic();
178+
for ( i = 0; i < b.iterations; i++ ) {
179+
v = concat( values, out, -1 );
180+
if ( typeof v !== 'object' ) {
181+
b.fail( 'should return an ndarray' );
182+
}
183+
}
184+
b.toc();
185+
if ( !isndarrayLike( v ) ) {
186+
b.fail( 'should return an ndarray' );
187+
}
188+
b.pass( 'benchmark finished' );
189+
b.end();
190+
});

0 commit comments

Comments
 (0)