Skip to content

Commit b0cffa6

Browse files
headlessNodekgryte
andauthored
feat: add ndarray/rowcat
PR-URL: #11535 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#260
1 parent 5e89f8b commit b0cffa6

File tree

14 files changed

+2270
-0
lines changed

14 files changed

+2270
-0
lines changed
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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+
# rowcat
22+
23+
> Concatenate a list of one-dimensional or two-dimensional [ndarrays][@stdlib/ndarray/ctor] as rows.
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 rowcat = require( '@stdlib/ndarray/rowcat' );
41+
```
42+
43+
#### rowcat( arrays )
44+
45+
Concatenates a list of one-dimensional or two-dimensional [ndarrays][@stdlib/ndarray/ctor] as rows.
46+
47+
```javascript
48+
var array = require( '@stdlib/ndarray/array' );
49+
50+
var x = array( [ 1.0, 2.0, 3.0 ] );
51+
// returns <ndarray>[ 1.0, 2.0, 3.0 ]
52+
53+
var y = array( [ [ 4.0, 5.0, 6.0 ], [ 7.0, 8.0, 9.0 ] ] );
54+
// returns <ndarray>[ [ 4.0, 5.0, 6.0 ], [ 7.0, 8.0, 9.0 ] ]
55+
56+
var out = rowcat( [ x, y ] );
57+
// returns <ndarray>[ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ], [ 7.0, 8.0, 9.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+
#### rowcat.assign( arrays, out )
65+
66+
Concatenates a list of one-dimensional or two-dimensional [ndarrays][@stdlib/ndarray/ctor] as rows 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 ] );
73+
// returns <ndarray>[ 1.0, 2.0, 3.0 ]
74+
75+
var y = array( [ [ 4.0, 5.0, 6.0 ], [ 7.0, 8.0, 9.0 ] ] );
76+
// returns <ndarray>[ [ 4.0, 5.0, 6.0 ], [ 7.0, 8.0, 9.0 ] ]
77+
78+
var z = zeros( [ 3, 3 ] );
79+
// returns <ndarray>[ [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ] ]
80+
81+
var out = rowcat.assign( [ x, y ], z );
82+
// returns <ndarray>[ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ], [ 7.0, 8.0, 9.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]. Must be a two-dimensional [ndarray][@stdlib/ndarray/ctor].
92+
93+
</section>
94+
95+
<!-- /.usage -->
96+
97+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
98+
99+
<section class="notes">
100+
101+
- One-dimensional input [ndarrays][@stdlib/ndarray/ctor] having length `N` are promoted to two-dimensional [ndarrays][@stdlib/ndarray/ctor] having shape `[1, N]`.
102+
- Input [ndarrays][@stdlib/ndarray/ctor] must have the same number of columns.
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/discrete-uniform' );
116+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
117+
var rowcat = require( '@stdlib/ndarray/rowcat' );
118+
119+
var x = discreteUniform( [ 3 ], 0, 10, {
120+
'dtype': 'generic'
121+
});
122+
console.log( ndarray2array( x ) );
123+
124+
var y = discreteUniform( [ 2, 3 ], 0, 10, {
125+
'dtype': 'generic'
126+
});
127+
console.log( ndarray2array( y ) );
128+
129+
var out = rowcat( [ x, y ] );
130+
console.log( ndarray2array( out ) );
131+
```
132+
133+
</section>
134+
135+
<!-- /.examples -->
136+
137+
<!-- 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. -->
138+
139+
<section class="references">
140+
141+
</section>
142+
143+
<!-- /.references -->
144+
145+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
146+
147+
<section class="related">
148+
149+
</section>
150+
151+
<!-- /.related -->
152+
153+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
154+
155+
<section class="links">
156+
157+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
158+
159+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
160+
161+
[@stdlib/ndarray/orders]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/orders
162+
163+
[@stdlib/ndarray/defaults]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/defaults
164+
165+
[@stdlib/ndarray/promotion-rules]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/promotion-rules
166+
167+
[@stdlib/ndarray/mostly-safe-casts]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/mostly-safe-casts
168+
169+
</section>
170+
171+
<!-- /.links -->
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
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 rowcat = 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( [ 4, 32 ], opts );
52+
53+
b.tic();
54+
for ( i = 0; i < b.iterations; i++ ) {
55+
v = rowcat( 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::1d,casting', pkg ), function benchmark( b ) {
69+
var values;
70+
var out;
71+
var v;
72+
var i;
73+
74+
/* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */
75+
76+
values = [
77+
discreteUniform( [ 32 ], -100, 100, { 'dtype': 'float64' } ),
78+
discreteUniform( [ 32 ], -100, 100, { 'dtype': 'float32' } ),
79+
discreteUniform( [ 32 ], -100, 100, { 'dtype': 'int32' } ),
80+
discreteUniform( [ 32 ], -100, 100, { 'dtype': 'generic' } )
81+
];
82+
out = zeros( [ 4, 32 ], { 'dtype': 'generic' } );
83+
84+
/* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */
85+
86+
b.tic();
87+
for ( i = 0; i < b.iterations; i++ ) {
88+
v = rowcat( 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::2d', pkg ), function benchmark( b ) {
102+
var values;
103+
var opts;
104+
var out;
105+
var v;
106+
var i;
107+
108+
opts = {
109+
'dtype': 'float64'
110+
};
111+
112+
values = [
113+
discreteUniform( [ 16, 32 ], -100, 100, opts ),
114+
discreteUniform( [ 16, 32 ], -100, 100, opts ),
115+
discreteUniform( [ 16, 32 ], -100, 100, opts ),
116+
discreteUniform( [ 16, 32 ], -100, 100, opts )
117+
];
118+
out = zeros( [ 64, 32 ], opts );
119+
120+
b.tic();
121+
for ( i = 0; i < b.iterations; i++ ) {
122+
v = rowcat( 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::2d,casting', pkg ), function benchmark( b ) {
136+
var values;
137+
var out;
138+
var v;
139+
var i;
140+
141+
/* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */
142+
143+
values = [
144+
discreteUniform( [ 16, 32 ], -100, 100, { 'dtype': 'float64' } ),
145+
discreteUniform( [ 16, 32 ], -100, 100, { 'dtype': 'float32' } ),
146+
discreteUniform( [ 16, 32 ], -100, 100, { 'dtype': 'int32' } ),
147+
discreteUniform( [ 16, 32 ], -100, 100, { 'dtype': 'generic' } )
148+
];
149+
out = zeros( [ 64, 32 ], { 'dtype': 'generic' } );
150+
151+
/* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */
152+
153+
b.tic();
154+
for ( i = 0; i < b.iterations; i++ ) {
155+
v = rowcat( values, out );
156+
if ( typeof v !== 'object' ) {
157+
b.fail( 'should return an ndarray' );
158+
}
159+
}
160+
b.toc();
161+
if ( !isndarrayLike( v ) ) {
162+
b.fail( 'should return an ndarray' );
163+
}
164+
b.pass( 'benchmark finished' );
165+
b.end();
166+
});
167+
168+
bench( format( '%s::mixed_1d_2d', pkg ), function benchmark( b ) {
169+
var values;
170+
var opts;
171+
var out;
172+
var v;
173+
var i;
174+
175+
opts = {
176+
'dtype': 'float64'
177+
};
178+
179+
values = [
180+
discreteUniform( [ 32 ], -100, 100, opts ),
181+
discreteUniform( [ 16, 32 ], -100, 100, opts ),
182+
discreteUniform( [ 32 ], -100, 100, opts ),
183+
discreteUniform( [ 16, 32 ], -100, 100, opts )
184+
];
185+
out = zeros( [ 34, 32 ], opts );
186+
187+
b.tic();
188+
for ( i = 0; i < b.iterations; i++ ) {
189+
v = rowcat( values, out );
190+
if ( typeof v !== 'object' ) {
191+
b.fail( 'should return an ndarray' );
192+
}
193+
}
194+
b.toc();
195+
if ( !isndarrayLike( v ) ) {
196+
b.fail( 'should return an ndarray' );
197+
}
198+
b.pass( 'benchmark finished' );
199+
b.end();
200+
});

0 commit comments

Comments
 (0)