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
26 changes: 26 additions & 0 deletions lib/node_modules/@stdlib/ndarray/flatten/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ The function accepts the following options:

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

- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. If not specified, the output ndarray [data type][@stdlib/ndarray/dtypes] is inferred from the input [ndarray][@stdlib/ndarray/ctor].

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
Expand Down Expand Up @@ -108,6 +110,28 @@ var arr = ndarray2array( y );
// returns [ 1.0, 3.0, 5.0, 2.0, 4.0, 6.0 ]
```

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.

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

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

var y = flatten( x, {
'dtype': 'float32'
});
// returns <ndarray>

var dt = dtype( y );
// returns 'float32'

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

</section>

<!-- /.usage -->
Expand Down Expand Up @@ -164,6 +188,8 @@ console.log( ndarray2array( y ) );

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

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

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

<!-- <related-links> -->
Expand Down
4 changes: 4 additions & 0 deletions lib/node_modules/@stdlib/ndarray/flatten/docs/repl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@

Default: 'row-major'.

options.dtype: string (optional)
Output ndarray data type. By default, the function returns an ndarray
having the same data type as the provided input ndarray.

Returns
-------
out: ndarray
Expand Down
11 changes: 10 additions & 1 deletion lib/node_modules/@stdlib/ndarray/flatten/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

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

import { ndarray, Order } from '@stdlib/types/ndarray';
import { ndarray, Order, DataType } from '@stdlib/types/ndarray';

/**
* Interface defining function options.
Expand Down Expand Up @@ -50,6 +50,15 @@ interface Options {
* - Default: 'row-major'.
*/
order?: Order | 'same' | 'any';

/**
* Output ndarray data type.
*
* ## Notes
*
* - This option overrides using the input ndarray's inferred data type.
*/
dtype?: DataType;
}

/**
Expand Down
24 changes: 24 additions & 0 deletions lib/node_modules/@stdlib/ndarray/flatten/docs/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,30 @@ import flatten = require( './index' );
flatten( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), { 'order': ( x: number ): number => x } ); // $ExpectError
}

// The compiler throws an error if the function is provided a second argument with invalid `dtype` option...
{
flatten( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'dtype': '5' } ); // $ExpectError
flatten( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'dtype': true } ); // $ExpectError
flatten( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'dtype': false } ); // $ExpectError
flatten( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'dtype': null } ); // $ExpectError
flatten( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'dtype': [ 1 ] } ); // $ExpectError
flatten( zeros( 'float64', [ 2, 2, 2 ], 'row-major' ), { 'dtype': ( x: number ): number => x } ); // $ExpectError

flatten( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), { 'dtype': '5' } ); // $ExpectError
flatten( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), { 'dtype': true } ); // $ExpectError
flatten( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), { 'dtype': false } ); // $ExpectError
flatten( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), { 'dtype': null } ); // $ExpectError
flatten( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), { 'dtype': [ 1 ] } ); // $ExpectError
flatten( zeros( 'complex128', [ 2, 2, 2 ], 'row-major' ), { 'dtype': ( x: number ): number => x } ); // $ExpectError

flatten( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), { 'dtype': '5' } ); // $ExpectError
flatten( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), { 'dtype': true } ); // $ExpectError
flatten( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), { 'dtype': false } ); // $ExpectError
flatten( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), { 'dtype': null } ); // $ExpectError
flatten( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), { 'dtype': [ 1 ] } ); // $ExpectError
flatten( zeros( 'generic', [ 2, 2, 2 ], 'row-major' ), { 'dtype': ( x: number ): number => x } ); // $ExpectError
}

// The compiler throws an error if the function is provided an unsupported number of arguments...
{
const x = zeros( 'float64', [ 2, 2, 2 ], 'row-major' );
Expand Down
10 changes: 8 additions & 2 deletions lib/node_modules/@stdlib/ndarray/flatten/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,8 @@ function flatten( x, options ) {
// Define default options:
opts = {
'depth': xsh.length, // by default, flatten to a one-dimensional ndarray
'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)
'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)
'dtype': getDType( x )
};

// Resolve function options...
Expand Down Expand Up @@ -335,11 +336,16 @@ function flatten( x, options ) {
throw new TypeError( format( 'invalid option. `%s` option must be a recognized order. Option: `%s`.', 'order', options.order ) );
}
}
if ( hasOwnProp( options, 'dtype' ) ) {
// Delegate `dtype` validation to `emptyLike` during output array creation:
opts.dtype = options.dtype;
}
}
// Create an output ndarray having contiguous memory:
y = emptyLike( x, {
'shape': flattenShape( xsh, opts.depth ),
'order': opts.order
'order': opts.order,
'dtype': opts.dtype
});

// Create a view on top of output ndarray having the same shape as the input ndarray:
Expand Down
37 changes: 37 additions & 0 deletions lib/node_modules/@stdlib/ndarray/flatten/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,43 @@
}
});

tape( 'the function throws an error if provided an invalid `dtype` option', function test( t ) {
var values;
var i;

values = [
'foo',
'bar',
1,
NaN,
true,
false,
void 0,
null,
[],
{},
function noop() {}
];

for ( i = 0; i < values.length; i++ ) {
t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+ values[i] );
}
t.end();

function badValue( value ) {
return function badValue() {
var opts = {
'dtype': value
};
flatten( zeros( [ 2 ] ), opts, scale );
};
}

function scale( z ) {
return z * 10.0;
}
});

tape( 'by default, the function flattens all dimensions of a provided input ndarray in lexicographic order (row-major, contiguous)', function test( t ) {
var expected;
var xbuf;
Expand Down Expand Up @@ -1333,7 +1370,7 @@
t.strictEqual( isSameFloat64Array( getData( y ), expected ), true, 'returns expected value' );
t.deepEqual( getShape( y ), [ 8 ], 'returns expected value' );
t.strictEqual( getDType( y ), dt, 'returns expected value' );
t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' );

Check warning on line 1373 in lib/node_modules/@stdlib/ndarray/flatten/test/test.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

File has too many lines (1020). Maximum allowed is 1000

dt = 'float64';
ord = 'column-major';
Expand Down