Flatten a shape to a specified depth.
var flattenShape = require( '@stdlib/ndarray/base/flatten-shape' );Flattens a shape to a specified depth.
var sh = flattenShape( [ 3, 2 ], 1 );
// returns [ 6 ]The function accepts the following parameters:
- shape: array shape.
- depth: maximum depth to flatten.
Flattens a shape to a specified depth and assigns results to a provided output array.
var sh = [ 0 ];
var out = flattenShape.assign( [ 3, 2 ], 1, sh );
// returns [ 6 ]
var bool = ( sh === out );
// returns trueThe function accepts the following parameters:
- shape: array shape.
- depth: maximum depth to flatten.
- out: output array.
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var zip = require( '@stdlib/array/base/zip' );
var logEachMap = require( '@stdlib/console/log-each-map' );
var flattenShape = require( '@stdlib/ndarray/base/flatten-shape' );
var opts = {
'dtype': 'int32'
};
var d1 = discreteUniform( 100, 1, 10, opts );
var d2 = discreteUniform( d1.length, 1, 10, opts );
var d3 = discreteUniform( d1.length, 1, 10, opts );
var d4 = discreteUniform( d1.length, 1, 10, opts );
var depths = discreteUniform( d1.length, 0, 3, opts );
var shapes = zip( [ d1, d2, d3, d4 ] );
logEachMap( 'shape: (%s). depth: %d. flattened: (%s).', shapes, depths, flattenShape );#include "stdlib/ndarray/base/flatten_shape.h"Flattens a shape to a specified depth.
const int64_t ndims = 3;
const int64_t shape[] = { 2, 3, 10 };
int64_t out[ 1 ];
stdlib_ndarray_flatten_shape( ndims, shape, 2, out );The function accepts the following arguments:
- ndims:
[in] int64_tnumber of dimensions. - shape:
[in] int64_t*array shape (dimensions). - depth:
[in] int64_tmaximum depth to flatten. - out:
[out] int64_t*output array.
int8_t stdlib_ndarray_flatten_shape( const int64_t ndims, const int64_t *shape, const int64_t depth, int64_t *out );#include "stdlib/ndarray/base/flatten_shape.h"
#include <stdio.h>
#include <inttypes.h>
int main( void ) {
const int64_t shape[] = { 2, 3, 4, 10 };
const int64_t ndims = 4;
const int64_t depth = 2;
int64_t out[ 2 ];
stdlib_ndarray_flatten_shape( ndims, shape, depth, out );
int i;
printf( "shape = ( " );
for ( i = 0; i < ndims-depth; i++ ) {
printf( "%"PRId64"", out[ i ] );
if ( i < ndims-depth-1 ) {
printf( ", " );
}
}
printf( " )\n" );
}