Skip to content

Commit da53943

Browse files
committed
feat: add ndarray/concat1d
1 parent 316ba7e commit da53943

File tree

17 files changed

+2157
-0
lines changed

17 files changed

+2157
-0
lines changed
Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
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+
# concat1d
22+
23+
> Return a one-dimensional [ndarray][@stdlib/ndarray/ctor] formed by concatenating the provided input arguments.
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 concat1d = require( '@stdlib/ndarray/concat1d' );
41+
```
42+
43+
#### concat1d( ...arrays )
44+
45+
Returns a one-dimensional [ndarray][@stdlib/ndarray/ctor] formed by concatenating the provided input arguments.
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 = concat1d( x, y );
55+
// returns <ndarray>
56+
57+
var arr = ndarray2array( out );
58+
// returns [ -1.0, 2.0, 3.0, 4.0, -5.0, 6.0, -7.0, -8.0, 9.0, -10.0 ]
59+
```
60+
61+
The function accepts the following arguments:
62+
63+
- **...arrays**: inputs to concatenate. May be passed as separate arguments or an array of arguments. Where each argument must either be a one-dimensional [ndarray][@stdlib/ndarray/ctor], a zero-dimensional[ndarray][@stdlib/ndarray/ctor] or a scalar value. The data type of the output [ndarray][@stdlib/ndarray/ctor] is determined by applying [type promotion rules][@stdlib/ndarray/promotion-rules]. If provided scalar inputs or [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+
65+
The following example demonstrates each invocation style returning equivalent results.
66+
67+
```javascript
68+
var array = require( '@stdlib/ndarray/array' );
69+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
70+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
71+
72+
var x = array( [ -1.0, 2.0 ] );
73+
var y = scalar2ndarray( -3.0 );
74+
75+
// 1. Using separate arguments:
76+
var out = concat1d( x, y, 4.0 );
77+
// returns <ndarray>
78+
79+
var arr = ndarray2array( out );
80+
// returns [ -1.0, 2.0, -3.0, 4.0 ]
81+
82+
x = array( [ -5.0, 6.0 ] );
83+
y = scalar2ndarray( -7.0 );
84+
85+
// 2. Using an array of arguments:
86+
out = concat1d( [ x, y, 8.0 ] );
87+
// returns <ndarray>
88+
89+
arr = ndarray2array( out );
90+
// returns [ -5.0, 6.0, -7.0, 8.0 ]
91+
```
92+
93+
#### concat1d.assign( ...arrays, out )
94+
95+
Concatenates provided input arguments and assigns the result to a provided one-dimensional output [ndarray][@stdlib/ndarray/ctor].
96+
97+
```javascript
98+
var array = require( '@stdlib/ndarray/array' );
99+
var zeros = require( '@stdlib/ndarray/zeros' );
100+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
101+
102+
var x = array( [ -1.0, 2.0, 3.0, 4.0 ] );
103+
var y = array( [ -5.0, 6.0, -7.0, -8.0 ] );
104+
var z = zeros( [ 8 ] );
105+
106+
var out = concat1d.assign( x, y, z );
107+
// returns <ndarray>
108+
109+
var bool = ( out === z );
110+
// returns true
111+
112+
var arr = ndarray2array( z );
113+
// returns [ -1.0, 2.0, 3.0, 4.0, -5.0, 6.0, -7.0, -8.0 ]
114+
```
115+
116+
The function accepts the following arguments:
117+
118+
- **...arrays**: inputs to concatenate. May be passed as separate arguments or an array of arguments. Where each argument must either be a one-dimensional [ndarray][@stdlib/ndarray/ctor], a zero-dimensional[ndarray][@stdlib/ndarray/ctor] or a scalar value. The data type of the output [ndarray][@stdlib/ndarray/ctor] is determined by applying [type promotion rules][@stdlib/ndarray/promotion-rules]. If provided scalar inputs or [ndarrays][@stdlib/ndarray/ctor] having different [memory layouts][@stdlib/ndarray/orders], the output [ndarray][@stdlib/ndarray/ctor] has the [default order][@stdlib/ndarray/defaults].
119+
- **out**: output [ndarray][@stdlib/ndarray/ctor].
120+
121+
The following example demonstrates each invocation style returning equivalent results.
122+
123+
```javascript
124+
var array = require( '@stdlib/ndarray/array' );
125+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
126+
var zeros = require( '@stdlib/ndarray/zeros' );
127+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
128+
129+
var x = array( [ -1.0, 2.0 ] );
130+
var y = scalar2ndarray( -3.0 );
131+
var z = zeros( [ 4 ] );
132+
133+
// 1. Using separate arguments:
134+
var out = concat1d.assign( x, y, 4.0, z );
135+
// returns <ndarray>
136+
137+
var bool = ( out === z );
138+
// returns true
139+
140+
var arr = ndarray2array( z );
141+
// returns [ -1.0, 2.0, -3.0, 4.0 ]
142+
143+
x = array( [ -5.0, 6.0 ] );
144+
y = scalar2ndarray( -7.0 );
145+
z = zeros( [ 4 ] );
146+
147+
// 2. Using an array of arguments:
148+
out = concat1d.assign( [ x, y, 8.0 ], z );
149+
// returns <ndarray>
150+
151+
bool = ( out === z );
152+
// returns true
153+
154+
arr = ndarray2array( out );
155+
// returns [ -5.0, 6.0, -7.0, 8.0 ]
156+
```
157+
158+
</section>
159+
160+
<!-- /.usage -->
161+
162+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
163+
164+
<section class="notes">
165+
166+
</section>
167+
168+
<!-- /.notes -->
169+
170+
<!-- Package usage examples. -->
171+
172+
<section class="examples">
173+
174+
## Examples
175+
176+
```javascript
177+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
178+
var ndarray = require( '@stdlib/ndarray/ctor' );
179+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
180+
var concat1d = require( '@stdlib/ndarray/concat1d' );
181+
182+
var xbuf = discreteUniform( 6, 0, 10, {
183+
'dtype': 'generic'
184+
});
185+
var x = new ndarray( 'generic', xbuf, [ 6 ], [ 1 ], 0, 'row-major' );
186+
console.log( ndarray2array( x ) );
187+
188+
var ybuf = discreteUniform( 8, 0, 10, {
189+
'dtype': 'generic'
190+
});
191+
var y = new ndarray( 'generic', ybuf, [ 8 ], [ 1 ], 0, 'row-major' );
192+
console.log( ndarray2array( y ) );
193+
194+
var out = concat1d( x, y );
195+
console.log( ndarray2array( out ) );
196+
```
197+
198+
</section>
199+
200+
<!-- /.examples -->
201+
202+
<!-- 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. -->
203+
204+
<section class="references">
205+
206+
</section>
207+
208+
<!-- /.references -->
209+
210+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
211+
212+
<section class="related">
213+
214+
</section>
215+
216+
<!-- /.related -->
217+
218+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
219+
220+
<section class="links">
221+
222+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
223+
224+
[@stdlib/ndarray/orders]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/orders
225+
226+
[@stdlib/ndarray/defaults]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/defaults
227+
228+
[@stdlib/ndarray/promotion-rules]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/promotion-rules
229+
230+
</section>
231+
232+
<!-- /.links -->
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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 concat1d = require( './../lib/assign.js' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+'::ndarrays', function benchmark( b ) {
33+
var values;
34+
var opts;
35+
var out;
36+
var v;
37+
var i;
38+
39+
opts = {
40+
'dtype': 'float64'
41+
};
42+
43+
values = [
44+
zeros( [ 32 ], opts ),
45+
zeros( [ 32 ], opts ),
46+
zeros( [ 32 ], opts ),
47+
zeros( [ 32 ], opts )
48+
];
49+
out = zeros( [ 128 ], opts );
50+
51+
b.tic();
52+
for ( i = 0; i < b.iterations; i++ ) {
53+
v = concat1d( values, out );
54+
if ( typeof v !== 'object' ) {
55+
b.fail( 'should return an ndarray' );
56+
}
57+
}
58+
b.toc();
59+
if ( !isndarrayLike( v ) ) {
60+
b.fail( 'should return an ndarray' );
61+
}
62+
b.pass( 'benchmark finished' );
63+
b.end();
64+
});
65+
66+
bench( pkg+'::ndarrays,casting', function benchmark( b ) {
67+
var values;
68+
var out;
69+
var v;
70+
var i;
71+
72+
/* eslint-disable object-curly-newline */
73+
74+
values = [
75+
zeros( [ 32 ], { 'dtype': 'float64' }),
76+
zeros( [ 32 ], { 'dtype': 'float32' }),
77+
zeros( [ 32 ], { 'dtype': 'int32' }),
78+
zeros( [ 32 ], { 'dtype': 'generic' })
79+
];
80+
out = zeros( [ 128 ], { 'dtype': 'generic' });
81+
82+
/* eslint-enable object-curly-newline */
83+
84+
b.tic();
85+
for ( i = 0; i < b.iterations; i++ ) {
86+
v = concat1d( values, out );
87+
if ( typeof v !== 'object' ) {
88+
b.fail( 'should return an ndarray' );
89+
}
90+
}
91+
b.toc();
92+
if ( !isndarrayLike( v ) ) {
93+
b.fail( 'should return an ndarray' );
94+
}
95+
b.pass( 'benchmark finished' );
96+
b.end();
97+
});
98+
99+
bench( pkg+'::scalars', function benchmark( b ) {
100+
var values;
101+
var out;
102+
var v;
103+
var i;
104+
105+
values = [
106+
1.0,
107+
2.0,
108+
3.0,
109+
4.0
110+
];
111+
out = zeros( [ 4 ] );
112+
113+
b.tic();
114+
for ( i = 0; i < b.iterations; i++ ) {
115+
v = concat1d( values, out );
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+'::ndarrays+scalars', function benchmark( b ) {
129+
var values;
130+
var out;
131+
var v;
132+
var i;
133+
134+
values = [
135+
zeros( [ 4 ] ),
136+
1.0,
137+
2.0,
138+
zeros( [ 2 ] ),
139+
4.0
140+
];
141+
out = zeros( [ 9 ] );
142+
143+
b.tic();
144+
for ( i = 0; i < b.iterations; i++ ) {
145+
v = concat1d( values, out );
146+
if ( typeof v !== 'object' ) {
147+
b.fail( 'should return an ndarray' );
148+
}
149+
}
150+
b.toc();
151+
if ( !isndarrayLike( v ) ) {
152+
b.fail( 'should return an ndarray' );
153+
}
154+
b.pass( 'benchmark finished' );
155+
b.end();
156+
});

0 commit comments

Comments
 (0)