-
-
Notifications
You must be signed in to change notification settings - Fork 904
feat: add ndarray/flatten
#8021
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
a71f1bd
dcd128d
c0ab24d
07281ae
3055793
d0c2a3b
eee3f89
47852b7
9fb5075
aee31ef
a9f2471
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 ) | ||
|
||
|
||
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. | ||
headlessNode marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
</section> | ||
|
||
<!-- /.usage --> | ||
|
||
<section class="notes"> | ||
|
||
## Notes | ||
|
||
- The provided input [`ndarray`][@stdlib/ndarray/ctor] **must** have more than one-dimension. | ||
headlessNode marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
</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 --> |
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(); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
|
||
{{alias}}( x, depth ) | ||
headlessNode marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Returns a flattened copy of an input ndarray. | ||
|
||
The `depth` must be a valid non-negative integer. | ||
|
||
headlessNode marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Parameters | ||
---------- | ||
x: ndarray | ||
Input ndarray. | ||
|
||
depth: integer | ||
A non-negative integer specifying the number of input ndarray dimensions | ||
to flatten. | ||
headlessNode marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
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 | ||
-------- | ||
|
Uh oh!
There was an error while loading. Please reload this page.