-
-
Notifications
You must be signed in to change notification settings - Fork 903
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a71f1bd
feat: add ndarra/flatten
headlessNode dcd128d
fix: import in benchmark file
headlessNode c0ab24d
refactor: most of the stuff
headlessNode 07281ae
docs: update copy
kgryte 3055793
docs: update copy
kgryte d0c2a3b
style: remove linebreak
kgryte eee3f89
docs: fix description
kgryte 47852b7
docs: update description based on conventions
kgryte 9fb5075
Merge branch 'develop' of https://github.com/stdlib-js/stdlib into pr…
kgryte aee31ef
chore: clean-up
kgryte a9f2471
test: add and update tests
kgryte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
<!-- | ||
|
||
@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\[, options] ) | ||
|
||
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 ); | ||
// 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]. | ||
- **options**: function options (_optional_). | ||
|
||
The function accepts the following options: | ||
|
||
- **depth**: a nonnegative integer specifying the number of input [`ndarray`][@stdlib/ndarray/ctor] dimensions to flatten. | ||
|
||
- **order**: order of the output [`ndarray`][@stdlib/ndarray/ctor]. The option may be one of the following values: | ||
|
||
- `'row-major'`: the input [`ndarray`][@stdlib/ndarray/ctor] is flattened in lexicographic [order][@stdlib/ndarray/orders]. | ||
- `'column-major'`: the input [`ndarray`][@stdlib/ndarray/ctor] is flattened in colexicographic [order][@stdlib/ndarray/orders]. | ||
- `'any'`: the input [`ndarray`][@stdlib/ndarray/ctor] is flattened in colexicographic [order][@stdlib/ndarray/orders] if the input [`ndarray`][@stdlib/ndarray/ctor] is column-major; otherwise, it is flattened in lexicographic [order][@stdlib/ndarray/orders]. | ||
- `'same'`: the input [`ndarray`][@stdlib/ndarray/ctor] is flattened in the [order][@stdlib/ndarray/orders] of the input [`ndarray`][@stdlib/ndarray/ctor]. | ||
|
||
By default, all the dimensinos of the input [ndarray][@stdlib/ndarray/ctor] are flattened. To flatten upto to a specific `depth`, specify the `depth` option. | ||
kgryte marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
```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, { | ||
'depth': 1 | ||
}); | ||
// returns <ndarray> | ||
|
||
var arr = ndarray2array( y ); | ||
// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] | ||
``` | ||
|
||
By default, the input [ndarray][@stdlib/ndarray/ctor] is flattened in lexicographic order. To flatten in a different [order][@stdlib/ndarray/orders], specify the `order` option. | ||
|
||
```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, { | ||
'order': 'column-major' | ||
}); | ||
// returns <ndarray> | ||
|
||
var arr = ndarray2array( y ); | ||
// returns [ 1.0, 3.0, 5.0, 2.0, 4.0, 6.0 ] | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.usage --> | ||
|
||
<section class="notes"> | ||
|
||
</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 ); | ||
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 | ||
|
||
[@stdlib/ndarray/orders]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/orders | ||
|
||
<!-- <related-links> --> | ||
|
||
<!-- </related-links> --> | ||
|
||
</section> | ||
|
||
<!-- /.links --> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.