Skip to content

Commit c034ac5

Browse files
headlessNodekgryte
andauthored
feat: add ndarray/concat1d
PR-URL: #8584 Closes: stdlib-js/metr-issue-tracker#116 Ref: #2656 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 29e738e commit c034ac5

File tree

16 files changed

+2179
-0
lines changed

16 files changed

+2179
-0
lines changed
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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 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 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. Each argument must either be a one-dimensional [ndarray][@stdlib/ndarray/ctor], a zero-dimensional[ndarray][@stdlib/ndarray/ctor], or a scalar value.
64+
65+
The data type of the output [ndarray][@stdlib/ndarray/ctor] is determined by applying [type promotion rules][@stdlib/ndarray/promotion-rules]. If provided [ndarrays][@stdlib/ndarray/ctor] having different [memory layouts][@stdlib/ndarray/orders] or only scalar inputs, the output [ndarray][@stdlib/ndarray/ctor] has the [default memory layout][@stdlib/ndarray/defaults].
66+
67+
#### concat1d.assign( ...arrays, out )
68+
69+
Concatenates provided input arguments and assigns the result to a provided one-dimensional output [ndarray][@stdlib/ndarray/ctor].
70+
71+
```javascript
72+
var array = require( '@stdlib/ndarray/array' );
73+
var zeros = require( '@stdlib/ndarray/zeros' );
74+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
75+
76+
var x = array( [ -1.0, 2.0, 3.0, 4.0 ] );
77+
var y = array( [ -5.0, 6.0, -7.0, -8.0 ] );
78+
var z = zeros( [ 8 ] );
79+
80+
var out = concat1d.assign( x, y, z );
81+
// returns <ndarray>
82+
83+
var bool = ( out === z );
84+
// returns true
85+
86+
var arr = ndarray2array( z );
87+
// returns [ -1.0, 2.0, 3.0, 4.0, -5.0, 6.0, -7.0, -8.0 ]
88+
```
89+
90+
The function accepts the following arguments:
91+
92+
- **...arrays**: inputs to concatenate. May be passed as separate arguments or an array of arguments. Each argument must either be a one-dimensional [ndarray][@stdlib/ndarray/ctor], a zero-dimensional[ndarray][@stdlib/ndarray/ctor], or a scalar value.
93+
- **out**: output [ndarray][@stdlib/ndarray/ctor].
94+
95+
</section>
96+
97+
<!-- /.usage -->
98+
99+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
100+
101+
<section class="notes">
102+
103+
</section>
104+
105+
<!-- /.notes -->
106+
107+
<!-- Package usage examples. -->
108+
109+
<section class="examples">
110+
111+
## Examples
112+
113+
```javascript
114+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
115+
var array = require( '@stdlib/ndarray/array' );
116+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
117+
var concat1d = require( '@stdlib/ndarray/concat1d' );
118+
119+
var opts = {
120+
'dtype': 'generic'
121+
};
122+
var x = array( discreteUniform( 6, 0, 10, opts ), opts );
123+
console.log( ndarray2array( x ) );
124+
125+
var y = array( discreteUniform( 8, 0, 10, opts ), opts );
126+
console.log( ndarray2array( y ) );
127+
128+
var out = concat1d( x, y );
129+
console.log( ndarray2array( out ) );
130+
```
131+
132+
</section>
133+
134+
<!-- /.examples -->
135+
136+
<!-- 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. -->
137+
138+
<section class="references">
139+
140+
</section>
141+
142+
<!-- /.references -->
143+
144+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
145+
146+
<section class="related">
147+
148+
</section>
149+
150+
<!-- /.related -->
151+
152+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
153+
154+
<section class="links">
155+
156+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
157+
158+
[@stdlib/ndarray/orders]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/orders
159+
160+
[@stdlib/ndarray/defaults]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/defaults
161+
162+
[@stdlib/ndarray/promotion-rules]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/promotion-rules
163+
164+
</section>
165+
166+
<!-- /.links -->
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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 zeroTo = require( '@stdlib/array/zero-to' );
27+
var format = require( '@stdlib/string/format' );
28+
var pkg = require( './../package.json' ).name;
29+
var concat1d = require( './../lib/assign.js' );
30+
31+
32+
// MAIN //
33+
34+
bench( format( '%s::ndarrays', 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+
zeros( [ 32 ], opts ),
47+
zeros( [ 32 ], opts ),
48+
zeros( [ 32 ], opts ),
49+
zeros( [ 32 ], opts )
50+
];
51+
out = zeros( [ 128 ], opts );
52+
53+
b.tic();
54+
for ( i = 0; i < b.iterations; i++ ) {
55+
v = concat1d( 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::ndarrays,casting', pkg ), function benchmark( b ) {
69+
var values;
70+
var out;
71+
var v;
72+
var i;
73+
74+
/* eslint-disable object-curly-newline */
75+
76+
values = [
77+
zeros( [ 32 ], { 'dtype': 'float64' }),
78+
zeros( [ 32 ], { 'dtype': 'float32' }),
79+
zeros( [ 32 ], { 'dtype': 'int32' }),
80+
zeros( [ 32 ], { 'dtype': 'generic' })
81+
];
82+
out = zeros( [ 128 ], { 'dtype': 'generic' });
83+
84+
/* eslint-enable object-curly-newline */
85+
86+
b.tic();
87+
for ( i = 0; i < b.iterations; i++ ) {
88+
v = concat1d( values, out );
89+
if ( typeof v !== 'object' ) {
90+
b.fail( 'should return an ndarray' );
91+
}
92+
}
93+
b.toc();
94+
if ( !isndarrayLike( v ) ) {
95+
b.fail( 'should return an ndarray' );
96+
}
97+
b.pass( 'benchmark finished' );
98+
b.end();
99+
});
100+
101+
bench( format( '%s::scalars', pkg ), function benchmark( b ) {
102+
var values;
103+
var out;
104+
var v;
105+
var i;
106+
107+
values = zeroTo( 128 );
108+
out = zeros( [ 128 ] );
109+
110+
b.tic();
111+
for ( i = 0; i < b.iterations; i++ ) {
112+
v = concat1d( values, out );
113+
if ( typeof v !== 'object' ) {
114+
b.fail( 'should return an ndarray' );
115+
}
116+
}
117+
b.toc();
118+
if ( !isndarrayLike( v ) ) {
119+
b.fail( 'should return an ndarray' );
120+
}
121+
b.pass( 'benchmark finished' );
122+
b.end();
123+
});
124+
125+
bench( format( '%s::ndarrays+scalars', pkg ), function benchmark( b ) {
126+
var values;
127+
var out;
128+
var v;
129+
var i;
130+
131+
values = zeroTo( 124, 'generic' );
132+
values[ 0 ] = zeros( [ 4 ] );
133+
values[ 3 ] = zeros( [ 2 ] );
134+
out = zeros( [ 128 ] );
135+
136+
b.tic();
137+
for ( i = 0; i < b.iterations; i++ ) {
138+
v = concat1d( values, out );
139+
if ( typeof v !== 'object' ) {
140+
b.fail( 'should return an ndarray' );
141+
}
142+
}
143+
b.toc();
144+
if ( !isndarrayLike( v ) ) {
145+
b.fail( 'should return an ndarray' );
146+
}
147+
b.pass( 'benchmark finished' );
148+
b.end();
149+
});

0 commit comments

Comments
 (0)