Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
232 changes: 232 additions & 0 deletions lib/node_modules/@stdlib/ndarray/concat1d/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
<!--

@license Apache-2.0

Copyright (c) 2025 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# concat1d

> Return a one-dimensional [ndarray][@stdlib/ndarray/ctor] formed by concatenating the provided input arguments.

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage

```javascript
var concat1d = require( '@stdlib/ndarray/concat1d' );
```

#### concat1d( ...arrays )

Returns a one-dimensional [ndarray][@stdlib/ndarray/ctor] formed by concatenating the provided input arguments.

```javascript
var array = require( '@stdlib/ndarray/array' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );

var x = array( [ -1.0, 2.0, 3.0, 4.0 ] );
var y = array( [ -5.0, 6.0, -7.0, -8.0, 9.0, -10.0 ] );

var out = concat1d( x, y );
// returns <ndarray>

var arr = ndarray2array( out );
// returns [ -1.0, 2.0, 3.0, 4.0, -5.0, 6.0, -7.0, -8.0, 9.0, -10.0 ]
```

The function accepts the following arguments:

- **...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].

The following example demonstrates each invocation style returning equivalent results.

```javascript
var array = require( '@stdlib/ndarray/array' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );

var x = array( [ -1.0, 2.0 ] );
var y = scalar2ndarray( -3.0 );

// 1. Using separate arguments:
var out = concat1d( x, y, 4.0 );
// returns <ndarray>

var arr = ndarray2array( out );
// returns [ -1.0, 2.0, -3.0, 4.0 ]

x = array( [ -5.0, 6.0 ] );
y = scalar2ndarray( -7.0 );

// 2. Using an array of arguments:
out = concat1d( [ x, y, 8.0 ] );
// returns <ndarray>

arr = ndarray2array( out );
// returns [ -5.0, 6.0, -7.0, 8.0 ]
```

#### concat1d.assign( ...arrays, out )

Concatenates provided input arguments and assigns the result to a provided one-dimensional output [ndarray][@stdlib/ndarray/ctor].

```javascript
var array = require( '@stdlib/ndarray/array' );
var zeros = require( '@stdlib/ndarray/zeros' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );

var x = array( [ -1.0, 2.0, 3.0, 4.0 ] );
var y = array( [ -5.0, 6.0, -7.0, -8.0 ] );
var z = zeros( [ 8 ] );

var out = concat1d.assign( x, y, z );
// returns <ndarray>

var bool = ( out === z );
// returns true

var arr = ndarray2array( z );
// returns [ -1.0, 2.0, 3.0, 4.0, -5.0, 6.0, -7.0, -8.0 ]
```

The function accepts the following arguments:

- **...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].
- **out**: output [ndarray][@stdlib/ndarray/ctor].

The following example demonstrates each invocation style returning equivalent results.

```javascript
var array = require( '@stdlib/ndarray/array' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var zeros = require( '@stdlib/ndarray/zeros' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );

var x = array( [ -1.0, 2.0 ] );
var y = scalar2ndarray( -3.0 );
var z = zeros( [ 4 ] );

// 1. Using separate arguments:
var out = concat1d.assign( x, y, 4.0, z );
// returns <ndarray>

var bool = ( out === z );
// returns true

var arr = ndarray2array( z );
// returns [ -1.0, 2.0, -3.0, 4.0 ]

x = array( [ -5.0, 6.0 ] );
y = scalar2ndarray( -7.0 );
z = zeros( [ 4 ] );

// 2. Using an array of arguments:
out = concat1d.assign( [ x, y, 8.0 ], z );
// returns <ndarray>

bool = ( out === z );
// returns true

arr = ndarray2array( out );
// returns [ -5.0, 6.0, -7.0, 8.0 ]
```

</section>

<!-- /.usage -->

<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

```javascript
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var ndarray = require( '@stdlib/ndarray/ctor' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var concat1d = require( '@stdlib/ndarray/concat1d' );

var xbuf = discreteUniform( 6, 0, 10, {
'dtype': 'generic'
});
var x = new ndarray( 'generic', xbuf, [ 6 ], [ 1 ], 0, 'row-major' );
console.log( ndarray2array( x ) );

var ybuf = discreteUniform( 8, 0, 10, {
'dtype': 'generic'
});
var y = new ndarray( 'generic', ybuf, [ 8 ], [ 1 ], 0, 'row-major' );
console.log( ndarray2array( y ) );

var out = concat1d( x, y );
console.log( ndarray2array( out ) );
```

</section>

<!-- /.examples -->

<!-- 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. -->

<section class="references">

</section>

<!-- /.references -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor

[@stdlib/ndarray/orders]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/orders

[@stdlib/ndarray/defaults]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/defaults

[@stdlib/ndarray/promotion-rules]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/promotion-rules

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
var zeros = require( '@stdlib/ndarray/zeros' );
var pkg = require( './../package.json' ).name;
var concat1d = require( './../lib/assign.js' );


// MAIN //

bench( pkg+'::ndarrays', function benchmark( b ) {
var values;
var opts;
var out;
var v;
var i;

opts = {
'dtype': 'float64'
};

values = [
zeros( [ 32 ], opts ),
zeros( [ 32 ], opts ),
zeros( [ 32 ], opts ),
zeros( [ 32 ], opts )
];
out = zeros( [ 128 ], opts );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = concat1d( values, out );
if ( typeof v !== 'object' ) {
b.fail( 'should return an ndarray' );
}
}
b.toc();
if ( !isndarrayLike( v ) ) {
b.fail( 'should return an ndarray' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::ndarrays,casting', function benchmark( b ) {
var values;
var out;
var v;
var i;

/* eslint-disable object-curly-newline */

values = [
zeros( [ 32 ], { 'dtype': 'float64' }),
zeros( [ 32 ], { 'dtype': 'float32' }),
zeros( [ 32 ], { 'dtype': 'int32' }),
zeros( [ 32 ], { 'dtype': 'generic' })
];
out = zeros( [ 128 ], { 'dtype': 'generic' });

/* eslint-enable object-curly-newline */

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = concat1d( values, out );
if ( typeof v !== 'object' ) {
b.fail( 'should return an ndarray' );
}
}
b.toc();
if ( !isndarrayLike( v ) ) {
b.fail( 'should return an ndarray' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::scalars', function benchmark( b ) {
var values;
var out;
var v;
var i;

values = [
1.0,
2.0,
3.0,
4.0
];
out = zeros( [ 4 ] );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = concat1d( values, out );
if ( typeof v !== 'object' ) {
b.fail( 'should return an ndarray' );
}
}
b.toc();
if ( !isndarrayLike( v ) ) {
b.fail( 'should return an ndarray' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::ndarrays+scalars', function benchmark( b ) {
var values;
var out;
var v;
var i;

values = [
zeros( [ 4 ] ),
1.0,
2.0,
zeros( [ 2 ] ),
4.0
];
out = zeros( [ 9 ] );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = concat1d( values, out );
if ( typeof v !== 'object' ) {
b.fail( 'should return an ndarray' );
}
}
b.toc();
if ( !isndarrayLike( v ) ) {
b.fail( 'should return an ndarray' );
}
b.pass( 'benchmark finished' );
b.end();
});
Loading