Skip to content

Commit ed84f77

Browse files
committed
feat: add dtype option support in ndarray/flatten-by
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 0ed631d commit ed84f77

File tree

6 files changed

+109
-4
lines changed

6 files changed

+109
-4
lines changed

lib/node_modules/@stdlib/ndarray/flatten-by/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ The function accepts the following options:
7878

7979
- **depth**: maximum number of input [ndarray][@stdlib/ndarray/ctor] dimensions to flatten.
8080

81+
- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. By default, the function returns an [ndarray][@stdlib/ndarray/ctor] having the same [data type][@stdlib/ndarray/dtypes] as a provided input [ndarray][@stdlib/ndarray/ctor].
82+
8183
By default, the function flattens all dimensions of the input [ndarray][@stdlib/ndarray/ctor]. To flatten to a desired depth, specify the `depth` option.
8284

8385
```javascript
@@ -126,6 +128,33 @@ var arr = ndarray2array( y );
126128
// returns [ 2.0, 6.0, 10.0, 4.0, 8.0, 12.0 ]
127129
```
128130

131+
By default, the output ndarray [data type][@stdlib/ndarray/dtypes] is inferred from the input [ndarray][@stdlib/ndarray/ctor]. To return an ndarray with a different [data type][@stdlib/ndarray/dtypes], specify the `dtype` option.
132+
133+
```javascript
134+
var array = require( '@stdlib/ndarray/array' );
135+
var dtype = require( '@stdlib/ndarray/dtype' );
136+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
137+
138+
function scale( value ) {
139+
return value * 2.0;
140+
}
141+
142+
var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] );
143+
// returns <ndarray>
144+
145+
var opts = {
146+
'dtype': 'float32'
147+
};
148+
var y = flattenBy( x, opts, scale );
149+
// returns <ndarray>
150+
151+
var dt = dtype( y );
152+
// returns 'float32'
153+
154+
var arr = ndarray2array( y );
155+
// returns [ 2.0, 4.0, 6.0, 8.0, 10.0, 12.0 ]
156+
```
157+
129158
To set the callback function execution context, provide a `thisArg`.
130159

131160
<!-- eslint-disable no-invalid-this, max-len -->
@@ -224,6 +253,8 @@ console.log( ndarray2array( y ) );
224253

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

256+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
257+
227258
[@stdlib/ndarray/orders]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/orders
228259

229260
</section>

lib/node_modules/@stdlib/ndarray/flatten-by/docs/repl.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626

2727
Default: 'row-major'.
2828

29+
options.dtype: string (optional)
30+
Output ndarray data type. By default, the function returns an ndarray
31+
having the same data type as the provided input ndarray.
32+
2933
fcn: Function
3034
Callback function.
3135

lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/index.d.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
/// <reference types="@stdlib/types"/>
2222

23-
import { typedndarray, genericndarray, Order } from '@stdlib/types/ndarray';
23+
import { typedndarray, genericndarray, Order, DataType } from '@stdlib/types/ndarray';
2424
import { ComplexLike } from '@stdlib/types/complex';
2525

2626
/**
@@ -95,6 +95,15 @@ interface Options {
9595
* - Default: 'row-major'.
9696
*/
9797
order?: Order | 'same' | 'any';
98+
99+
/**
100+
* Output ndarray data type.
101+
*
102+
* ## Notes
103+
*
104+
* - By default, the function returns an ndarray having the same data type as a provided input ndarray.
105+
*/
106+
dtype?: DataType;
98107
}
99108

100109
/**
@@ -232,6 +241,7 @@ declare function flattenBy<T = unknown, U extends genericndarray<T> = genericnda
232241
* @param options - function options
233242
* @param options.depth - maximum number of dimensions to flatten
234243
* @param options.order - order in which input ndarray elements should be flattened
244+
* @param options.dtype - output ndarray data type
235245
* @param fcn - callback function
236246
* @param thisArg - callback execution context
237247
* @returns output ndarray
@@ -272,6 +282,7 @@ declare function flattenBy<T extends typedndarray<number> = typedndarray<number>
272282
* @param options - function options
273283
* @param options.depth - maximum number of dimensions to flatten
274284
* @param options.order - order in which input ndarray elements should be flattened
285+
* @param options.dtype - output ndarray data type
275286
* @param fcn - callback function
276287
* @param thisArg - callback execution context
277288
* @returns output ndarray
@@ -309,6 +320,7 @@ declare function flattenBy<T extends ComplexLike = ComplexLike, U extends typedn
309320
* @param options - function options
310321
* @param options.depth - maximum number of dimensions to flatten
311322
* @param options.order - order in which input ndarray elements should be flattened
323+
* @param options.dtype - output ndarray data type
312324
* @param fcn - callback function
313325
* @param thisArg - callback execution context
314326
* @returns output ndarray
@@ -349,6 +361,7 @@ declare function flattenBy<T extends typedndarray<boolean> = typedndarray<boolea
349361
* @param options - function options
350362
* @param options.depth - maximum number of dimensions to flatten
351363
* @param options.order - order in which input ndarray elements should be flattened
364+
* @param options.dtype - output ndarray data type
352365
* @param fcn - callback function
353366
* @param thisArg - callback execution context
354367
* @returns output ndarray

lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,23 @@ function identity( x: any ): any {
178178
flattenBy( x, { 'order': [ 1 ] }, identity, {} ); // $ExpectError
179179
}
180180

181+
// The compiler throws an error if the function is provided a second argument with invalid `dtype` option...
182+
{
183+
const x = zeros( 'generic', [ 2, 2, 2 ], 'row-major' );
184+
185+
flattenBy( x, { 'dtype': '5' }, identity ); // $ExpectError
186+
flattenBy( x, { 'dtype': true }, identity ); // $ExpectError
187+
flattenBy( x, { 'dtype': false }, identity ); // $ExpectError
188+
flattenBy( x, { 'dtype': null }, identity ); // $ExpectError
189+
flattenBy( x, { 'dtype': [ 1 ] }, identity ); // $ExpectError
190+
191+
flattenBy( x, { 'dtype': '5' }, identity, {} ); // $ExpectError
192+
flattenBy( x, { 'dtype': true }, identity, {} ); // $ExpectError
193+
flattenBy( x, { 'dtype': false }, identity, {} ); // $ExpectError
194+
flattenBy( x, { 'dtype': null }, identity, {} ); // $ExpectError
195+
flattenBy( x, { 'dtype': [ 1 ] }, identity, {} ); // $ExpectError
196+
}
197+
181198
// The compiler throws an error if the function is provided a callback which is not a function...
182199
{
183200
const x = zeros( 'generic', [ 2, 2 ], 'row-major' );

lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ var COL_MAJOR = 'column-major';
5555
* @param {Options} [options] - function options
5656
* @param {NonNegativeInteger} [options.depth] - maximum number of dimensions to flatten
5757
* @param {string} [options.order='row-major'] - order in which input ndarray elements should be flattened
58+
* @param {*} [options.dtype] - output ndarray data type
5859
* @param {Function} fcn - callback function
5960
* @param {*} [thisArg] - callback execution context
6061
* @throws {TypeError} first argument must be an ndarray-like object
@@ -102,7 +103,8 @@ function flattenBy( x, options, fcn, thisArg ) {
102103
// Define default options:
103104
opts = {
104105
'depth': xsh.length, // by default, flatten to a one-dimensional ndarray
105-
'order': ROW_MAJOR // by default, flatten in lexicographic order (i.e., trailing dimensions first; e.g., if `x` is a matrix, flatten row-by-row)
106+
'order': ROW_MAJOR, // by default, flatten in lexicographic order (i.e., trailing dimensions first; e.g., if `x` is a matrix, flatten row-by-row)
107+
'dtype': getDType( x )
106108
};
107109

108110
// Case: flattenBy( x, fcn )
@@ -165,16 +167,21 @@ function flattenBy( x, options, fcn, thisArg ) {
165167
throw new TypeError( format( 'invalid option. `%s` option must be a recognized order. Option: `%s`.', 'order', options.order ) );
166168
}
167169
}
170+
if ( hasOwnProp( options, 'dtype' ) ) {
171+
// Delegate `dtype` validation to `emptyLike` during output array creation:
172+
opts.dtype = options.dtype;
173+
}
168174
}
169175
// Create an output ndarray having contiguous memory:
170176
y = emptyLike( x, {
171177
'shape': flattenShape( xsh, opts.depth ),
172-
'order': opts.order
178+
'order': opts.order,
179+
'dtype': opts.dtype
173180
});
174181

175182
// Create a view on top of output ndarray having the same shape as the input ndarray:
176183
st = ( xsh.length > 0 ) ? shape2strides( xsh, opts.order ) : [ 0 ];
177-
view = ndarray( getDType( y ), getData( y ), xsh, st, 0, opts.order );
184+
view = ndarray( opts.dtype, getData( y ), xsh, st, 0, opts.order );
178185

179186
// Transform and assign elements to the output ndarray:
180187
map( [ x, view ], cb, ctx );

lib/node_modules/@stdlib/ndarray/flatten-by/test/test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,39 @@ tape( 'the function throws an error if provided an invalid `order` option', func
272272
}
273273
});
274274

275+
tape( 'the function throws an error if provided an invalid `dtype` option', function test( t ) {
276+
var values;
277+
var i;
278+
279+
values = [
280+
'foo',
281+
'bar',
282+
1,
283+
NaN,
284+
true,
285+
false,
286+
void 0,
287+
null,
288+
[],
289+
{},
290+
function noop() {}
291+
];
292+
293+
for ( i = 0; i < values.length; i++ ) {
294+
t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+ values[i] );
295+
}
296+
t.end();
297+
298+
function badValue( value ) {
299+
return function badValue() {
300+
var opts = {
301+
'dtype': value
302+
};
303+
flattenBy( zeros( [ 2 ] ), opts, identity );
304+
};
305+
}
306+
});
307+
275308
tape( 'the function throws an error if provided a callback argument which is not a function', function test( t ) {
276309
var values;
277310
var i;

0 commit comments

Comments
 (0)