Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
127 changes: 127 additions & 0 deletions lib/node_modules/@stdlib/ndarray/flatten/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<!--

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

-->

# flatten

> Return a flattened copy of an input [`ndarray`][@stdlib/ndarray/ctor].

<section class="intro">

</section>

<!-- /.intro -->

<section class="usage">

## Usage

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

#### flatten( x, depth )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, similar to NumPy (ref: https://numpy.org/doc/stable/reference/generated/numpy.ravel.html#numpy-ravel), we should support specifying a "read" order.

flatten( x[, opts] )

with options

  • depth: default infinity.
  • order: read order. If row-major, always flatten an input array in lexicographic order. If column-major, always flatten in colexicographic order. If same, flatten in the order of the input array (i.e., equivalent to order = getOrder( x )). If any, flatten in colexicographic order if input array data is column-major contiguous; otherwise, flatten in lexicographic order (i.e., flatten according to how the data is arranged in memory, which may not correspond to the stated array order).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


Returns a flattened copy of an input [`ndarray`][@stdlib/ndarray/ctor].

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

// Create an input ndarray:
var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] );
// returns <ndarray>

var y = flatten( x, 2 );
// returns <ndarray>

var arr = ndarray2array( y );
// returns [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]
```

The function accepts the following arguments:

- **x**: input [`ndarray`][@stdlib/ndarray/ctor].
- **depth**: a non-negative integer specifying the number of input [`ndarray`][@stdlib/ndarray/ctor] dimensions to flatten.

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- The provided input [`ndarray`][@stdlib/ndarray/ctor] **must** have more than one-dimension.

</section>

<!-- /.notes -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

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

var xbuf = discreteUniform( 12, -100, 100, {
'dtype': 'generic'
});

// Create an input ndarray:
var x = array( xbuf, {
'shape': [ 2, 2, 3 ],
'dtype': 'generic'
});
console.log( ndarray2array( x ) );

// Flatten the input ndarray:
var y = flatten( x, 2 );
console.log( ndarray2array( y ) );
```

</section>

<!-- /.examples -->

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

<section class="related">

</section>

<!-- /.related -->

<section class="links">

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

<!-- <related-links> -->

<!-- </related-links> -->

</section>

<!-- /.links -->
150 changes: 150 additions & 0 deletions lib/node_modules/@stdlib/ndarray/flatten/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/**
* @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/base/zeros' );
var pkg = require( './../package.json' ).name;
var flatten = require( './../lib' );


// MAIN //

bench( pkg+'::2d', function benchmark( b ) {
var values;
var y;
var i;
var j;

values = [
zeros( 'float64', [ 2, 2 ], 'row-major' ),
zeros( 'float32', [ 2, 2 ], 'row-major' ),
zeros( 'int32', [ 2, 2 ], 'row-major' ),
zeros( 'complex128', [ 2, 2 ], 'row-major' ),
zeros( 'generic', [ 2, 2 ], 'row-major' )
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
j = i % values.length;
y = flatten( values[ j ], 1 );
if ( typeof y !== 'object' ) {
b.fail( 'should return an ndarray' );
}
}
b.toc();
if ( !isndarrayLike( y ) ) {
b.fail( 'should return an ndarray' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::3d', function benchmark( b ) {
var values;
var y;
var i;
var j;

values = [
zeros( 'float64', [ 2, 2, 2 ], 'row-major' ),
zeros( 'float32', [ 2, 2, 2 ], 'row-major' ),
zeros( 'int32', [ 2, 2, 2 ], 'row-major' ),
zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ),
zeros( 'generic', [ 2, 2, 2 ], 'row-major' )
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
j = i % values.length;
y = flatten( values[ j ], 2 );
if ( typeof y !== 'object' ) {
b.fail( 'should return an ndarray' );
}
}
b.toc();
if ( !isndarrayLike( y ) ) {
b.fail( 'should return an ndarray' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::4d', function benchmark( b ) {
var values;
var y;
var i;
var j;

values = [
zeros( 'float64', [ 2, 2, 2, 2 ], 'row-major' ),
zeros( 'float32', [ 2, 2, 2, 2 ], 'row-major' ),
zeros( 'int32', [ 2, 2, 2, 2 ], 'row-major' ),
zeros( 'complex128', [ 2, 2, 2, 2 ], 'row-major' ),
zeros( 'generic', [ 2, 2, 2, 2 ], 'row-major' )
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
j = i % values.length;
y = flatten( values[ j ], 3 );
if ( typeof y !== 'object' ) {
b.fail( 'should return an ndarray' );
}
}
b.toc();
if ( !isndarrayLike( y ) ) {
b.fail( 'should return an ndarray' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::5d', function benchmark( b ) {
var values;
var y;
var i;
var j;

values = [
zeros( 'float64', [ 2, 2, 2, 2, 2 ], 'row-major' ),
zeros( 'float32', [ 2, 2, 2, 2, 2 ], 'row-major' ),
zeros( 'int32', [ 2, 2, 2, 2, 2 ], 'row-major' ),
zeros( 'complex128', [ 2, 2, 2, 2, 2 ], 'row-major' ),
zeros( 'generic', [ 2, 2, 2, 2, 2 ], 'row-major' )
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
j = i % values.length;
y = flatten( values[ j ], 4 );
if ( typeof y !== 'object' ) {
b.fail( 'should return an ndarray' );
}
}
b.toc();
if ( !isndarrayLike( y ) ) {
b.fail( 'should return an ndarray' );
}
b.pass( 'benchmark finished' );
b.end();
});
30 changes: 30 additions & 0 deletions lib/node_modules/@stdlib/ndarray/flatten/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

{{alias}}( x, depth )
Returns a flattened copy of an input ndarray.

The `depth` must be a valid non-negative integer.

Parameters
----------
x: ndarray
Input ndarray.

depth: integer
A non-negative integer specifying the number of input ndarray dimensions
to flatten.

Returns
-------
out: ndarray
Output ndarray.

Examples
--------
> var x = {{alias:@stdlib/ndarray/array}}( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] );
> var y = {{alias}}( x, 1 );
> var arr = {{alias:@stdlib/ndarray/to-array}}( y )
[ 1.0, 2.0, 3.0, 4.0 ]

See Also
--------

Loading