Skip to content

Commit 1a6e701

Browse files
headlessNodekgrytestdlib-bot
authored
feat: add ndarray/concat
PR-URL: #7969 Ref: #2656 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Co-authored-by: stdlib-bot <noreply@stdlib.io>
1 parent 7c5f460 commit 1a6e701

File tree

20 files changed

+3228
-0
lines changed

20 files changed

+3228
-0
lines changed
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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][@stdlib/ndarray/ctor] along a specified [ndarray][@stdlib/ndarray/ctor] 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][@stdlib/ndarray/ctor] along a specified [ndarray][@stdlib/ndarray/ctor] dimension.
46+
47+
```javascript
48+
var array = require( '@stdlib/ndarray/array' );
49+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
50+
51+
var x = array( [ [ -1.0, 2.0 ], [ -3.0, 4.0 ] ] );
52+
var y = array( [ [ -5.0, 6.0, -7.0 ], [ 8.0, -9.0, 10.0 ] ] );
53+
54+
var out = concat( [ x, y ], -1 );
55+
// returns <ndarray>
56+
57+
var arr = ndarray2array( out );
58+
// returns [ [ -1.0, 2.0, -5.0, 6.0, -7.0 ], [ -3.0, 4.0, 8.0, -9.0, 10.0 ] ]
59+
```
60+
61+
The function accepts the following arguments:
62+
63+
- **arrays**: a list of input [ndarrays][@stdlib/ndarray/ctor]. Must be [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes] except for the dimension along which to concatenate. 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].
64+
- **dim**: dimension along which to concatenate input [ndarrays][@stdlib/ndarray/ctor] (_optional_). Must be a negative integer. The index of the dimension along which to concatenate is resolved relative to the last dimension, with the last dimension corresponding to the value `-1`. Default: `-1`.
65+
66+
#### concat.assign( arrays, out\[, dim] )
67+
68+
Concatenates a list of ndarrays along a specified ndarray dimension and assigns results to an output ndarray.
69+
70+
```javascript
71+
var array = require( '@stdlib/ndarray/array' );
72+
var zeros = require( '@stdlib/ndarray/zeros' );
73+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
74+
75+
var x = array( [ [ -1.0, 2.0 ], [ -3.0, 4.0 ] ] );
76+
var y = array( [ [ -5.0, 6.0, -7.0 ], [ 8.0, -9.0, 10.0 ] ] );
77+
78+
var z = zeros( [ 2, 5 ] );
79+
80+
var out = concat.assign( [ x, y ], z, -1 );
81+
// returns <ndarray>
82+
83+
var bool = ( out === z );
84+
// returns true
85+
86+
var arr = ndarray2array( z );
87+
// returns [ [ -1.0, 2.0, -5.0, 6.0, -7.0 ], [ -3.0, 4.0, 8.0, -9.0, 10.0 ] ]
88+
```
89+
90+
The function accepts the following arguments:
91+
92+
- **arrays**: a list of input [ndarrays][@stdlib/ndarray/ctor]. Must be [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes] except for the dimension along which to concatenate. 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].
93+
- **out**: output [ndarray][@stdlib/ndarray/ctor].
94+
- **dim**: dimension along which to concatenate input [ndarrays][@stdlib/ndarray/ctor] (_optional_). Must be a negative integer. The index of the dimension along which to concatenate is resolved relative to the last dimension, with the last dimension corresponding to the value `-1`. Default: `-1`.
95+
96+
</section>
97+
98+
<!-- /.usage -->
99+
100+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
101+
102+
<section class="notes">
103+
104+
</section>
105+
106+
<!-- /.notes -->
107+
108+
<!-- Package usage examples. -->
109+
110+
<section class="examples">
111+
112+
## Examples
113+
114+
```javascript
115+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
116+
var ndarray = require( '@stdlib/ndarray/ctor' );
117+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
118+
var concat = require( '@stdlib/ndarray/concat' );
119+
120+
var xbuf = discreteUniform( 6, 0, 10, {
121+
'dtype': 'generic'
122+
});
123+
var x = new ndarray( 'generic', xbuf, [ 2, 3 ], [ 3, 1 ], 0, 'row-major' );
124+
console.log( ndarray2array( x ) );
125+
126+
var ybuf = discreteUniform( 8, 0, 10, {
127+
'dtype': 'generic'
128+
});
129+
var y = new ndarray( 'generic', ybuf, [ 2, 4 ], [ 4, 1 ], 0, 'row-major' );
130+
console.log( ndarray2array( y ) );
131+
132+
var out = concat( [ x, y ], -1 );
133+
console.log( ndarray2array( out ) );
134+
```
135+
136+
</section>
137+
138+
<!-- /.examples -->
139+
140+
<!-- 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. -->
141+
142+
<section class="references">
143+
144+
</section>
145+
146+
<!-- /.references -->
147+
148+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
149+
150+
<section class="related">
151+
152+
</section>
153+
154+
<!-- /.related -->
155+
156+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
157+
158+
<section class="links">
159+
160+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
161+
162+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
163+
164+
[@stdlib/ndarray/orders]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/orders
165+
166+
[@stdlib/ndarray/defaults]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/defaults
167+
168+
[@stdlib/ndarray/promotion-rules]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/promotion-rules
169+
170+
[@stdlib/ndarray/mostly-safe-casts]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/mostly-safe-casts
171+
172+
[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes
173+
174+
</section>
175+
176+
<!-- /.links -->

0 commit comments

Comments
 (0)