-
-
Notifications
You must be signed in to change notification settings - Fork 896
feat: add ndarray/flatten-by
#8078
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 8 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
40421e2
feat: add ndarray/flatten-by
headlessNode 5a11ffb
refactor: apply suggestions from code review
headlessNode d8b8cdb
docs: apply review suggestion
headlessNode 5465edd
docs: apply suggestions from code review
headlessNode aea5ad2
refactor: apply suggestions from code review
headlessNode 2ea0120
docs: apply suggestions from code review
headlessNode 08134c4
docs: apply suggestions from code review
headlessNode 3af01ed
docs: apply suggestions from code review
headlessNode 49533b7
docs: apply suggestions from code review
headlessNode 959a92d
refactor: apply suggestions from code review
headlessNode cc413a8
docs: addd note
headlessNode 6c55278
refactor: apply suggestions from code review
headlessNode b62117f
test: add missing test cases
headlessNode a530c29
docs: remove spaces
headlessNode 0e6522c
fix: apply suggestions from code review
headlessNode 14302a6
fix: apply suggestions from code review
headlessNode 129673d
fix: lint error
headlessNode 355f267
test: add tests
headlessNode c047e28
test: add tests
headlessNode e6a090b
test: add tests
headlessNode 48f6e8b
test: add test case
headlessNode 86cc6a4
chore: clean-up
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,233 @@ | ||
<!-- | ||
|
||
@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. | ||
|
||
--> | ||
|
||
# flattenBy | ||
|
||
> Apply a callback to elements in an input [ndarray][@stdlib/ndarray/ctor] and return a flattened output [ndarray][@stdlib/ndarray/ctor]. | ||
|
||
<section class="intro"> | ||
|
||
</section> | ||
|
||
<!-- /.intro --> | ||
|
||
<section class="usage"> | ||
|
||
## Usage | ||
|
||
```javascript | ||
var flattenBy = require( '@stdlib/ndarray/flatten-by' ); | ||
``` | ||
|
||
#### flattenBy( x\[, options], fcn[, thisArg] ) | ||
headlessNode marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
Applies a callback to elements in an input [ndarray][@stdlib/ndarray/ctor] and returns a flattened output [ndarray][@stdlib/ndarray/ctor]. | ||
|
||
```javascript | ||
var array = require( '@stdlib/ndarray/array' ); | ||
var ndarray2array = require( '@stdlib/ndarray/to-array' ); | ||
|
||
function scale( value ) { | ||
return value * 2.0; | ||
} | ||
|
||
var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); | ||
// returns <ndarray> | ||
|
||
var y = flattenBy( x, scale ); | ||
// returns <ndarray> | ||
|
||
var arr = ndarray2array( y ); | ||
// returns [ 2.0, 4.0, 6.0, 8.0, 10.0, 12.0 ] | ||
``` | ||
|
||
The function accepts the following arguments: | ||
|
||
- **x**: input [ndarray][@stdlib/ndarray/ctor]. | ||
- **options**: function options (_optional_). | ||
- **fcn**: callback function. | ||
- **thisArg**: callback execution context. | ||
|
||
The function accepts the following options: | ||
|
||
- **order**: order in which input [ndarray][@stdlib/ndarray/ctor] elements should be flattened. Must be one of the following: | ||
|
||
- `'row-major'`: flatten elements in lexicographic order. For example, given a two-dimensional input [ndarray][@stdlib/ndarray/ctor] (i.e., a matrix), flattening in lexicographic order means flattening the input [ndarray][@stdlib/ndarray/ctor] row-by-row. | ||
- `'column-major'`: flatten elements in colexicographic order. For example, given a two-dimensional input [ndarray][@stdlib/ndarray/ctor] (i.e., a matrix), flattening in colexicographic order means flattening the input [ndarray][@stdlib/ndarray/ctor] column-by-column. | ||
- `'any'`: flatten according to the physical layout of the input [ndarray][@stdlib/ndarray/ctor] data in memory, regardless of the stated [order][@stdlib/ndarray/orders] of the input [ndarray][@stdlib/ndarray/ctor]. | ||
- `'same'`: flatten according to the stated [order][@stdlib/ndarray/orders] of the input [ndarray][@stdlib/ndarray/ctor]. | ||
|
||
Default: `'row-major'`. | ||
|
||
- **depth**: maximum number of input [ndarray][@stdlib/ndarray/ctor] dimensions to flatten. | ||
|
||
By default, the function flattens all dimensions of the input [ndarray][@stdlib/ndarray/ctor]. To flatten to a desired depth, specify the `depth` option. | ||
|
||
```javascript | ||
var array = require( '@stdlib/ndarray/array' ); | ||
var ndarray2array = require( '@stdlib/ndarray/to-array' ); | ||
|
||
function scale( value ) { | ||
return value * 2.0; | ||
} | ||
|
||
var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); | ||
// returns <ndarray> | ||
|
||
var opts = { | ||
'depth': 1 | ||
}; | ||
|
||
var y = flattenBy( x, opts, scale ); | ||
// returns <ndarray> | ||
|
||
var arr = ndarray2array( y ); | ||
// returns [ [ 2.0, 4.0 ], [ 6.0, 8.0 ], [ 10.0, 12.0 ] ] | ||
``` | ||
|
||
By default, the input [ndarray][@stdlib/ndarray/ctor] is flattened in lexicographic order. To flatten elements in a different order, specify the `order` option. | ||
|
||
```javascript | ||
var array = require( '@stdlib/ndarray/array' ); | ||
var ndarray2array = require( '@stdlib/ndarray/to-array' ); | ||
|
||
function scale( value ) { | ||
return value * 2.0; | ||
} | ||
|
||
var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); | ||
// returns <ndarray> | ||
|
||
var opts = { | ||
'order': 'column-major' | ||
}; | ||
|
||
var y = flattenBy( x, opts, scale ); | ||
// returns <ndarray> | ||
|
||
var arr = ndarray2array( y ); | ||
// returns [ 2.0, 6.0, 4.0, 10.0, 8.0, 12.0 ] | ||
headlessNode marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
``` | ||
|
||
To set the callback function execution context, provide a `thisArg`. | ||
|
||
<!-- eslint-disable no-invalid-this, max-len --> | ||
|
||
```javascript | ||
var array = require( '@stdlib/ndarray/array' ); | ||
var ndarray2array = require( '@stdlib/ndarray/to-array' ); | ||
|
||
function scale( value ) { | ||
this.count += 1; | ||
return value * 2.0; | ||
} | ||
|
||
var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] ); | ||
// returns <ndarray> | ||
|
||
var ctx = { | ||
'count': 0 | ||
}; | ||
|
||
var y = flattenBy( x, scale, ctx ); | ||
// returns <ndarray> | ||
|
||
var arr = ndarray2array( y ); | ||
// returns [ 2.0, 4.0, 6.0, 8.0, 10.0, 12.0 ] | ||
|
||
var count = ctx.count; | ||
// returns 6 | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.usage --> | ||
|
||
<section class="notes"> | ||
|
||
## Notes | ||
|
||
- The function **always** returns a copy of input [ndarray][@stdlib/ndarray/ctor] data, even when an input [ndarray][@stdlib/ndarray/ctor] already has the desired number of dimensions. | ||
|
||
- The callback function is provided the following arguments: | ||
|
||
- **value**: current array element. | ||
- **indices**: current array element indices. | ||
- **arr**: the input [ndarray][@stdlib/ndarray/ctor]. | ||
|
||
</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 flattenBy = require( '@stdlib/ndarray/flatten-by' ); | ||
|
||
function scale( value ) { | ||
return value * 2.0; | ||
} | ||
|
||
var xbuf = discreteUniform( 12, -100, 100, { | ||
'dtype': 'generic' | ||
}); | ||
|
||
var x = array( xbuf, { | ||
'shape': [ 2, 2, 3 ], | ||
'dtype': 'generic' | ||
}); | ||
console.log( ndarray2array( x ) ); | ||
|
||
var y = flattenBy( x, scale ); | ||
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.
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.