Serialize an ndarray as a locale-aware string.
var ndarray2localeString = require( '@stdlib/ndarray/to-locale-string' );Serializes an ndarray as a locale-aware string.
var array = require( '@stdlib/ndarray/array' );
var x = array( [ 1, 2, 3, 4 ], {
'shape': [ 2, 2 ]
});
// returns <ndarray>
var str = ndarray2localeString( x );
// returns <string>The function supports the following parameters:
- x: input ndarray.
- locales: a BCP 47 language tag or an array of such strings (optional).
- options: configuration options (optional).
- The function does not serialize data outside of the buffer defined by the ndarray view.
- For ndarrays with more than
100elements, the function abbreviates the data, showing only the first and last three values.
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
var strides2offset = require( '@stdlib/ndarray/base/strides2offset' );
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
var zeroTo = require( '@stdlib/array/base/zero-to' );
var ndarray = require( '@stdlib/ndarray/ctor' );
var ndarray2localeString = require( '@stdlib/ndarray/to-locale-string' );
// Create a data buffer:
var buffer = zeroTo( 27 );
// Specify array meta data:
var shape = [ 3, 3, 3 ];
var order = 'column-major';
var ndims = shape.length;
// Compute array meta data:
var strides = shape2strides( shape, order );
var offset = strides2offset( shape, strides );
// Print array information:
console.log( '' );
console.log( 'Dims: %s', shape.join( 'x' ) );
// Randomly flip strides and convert an ndarray to a locale-aware string...
var arr;
var i;
for ( i = 0; i < 20; i++ ) {
strides[ discreteUniform( 0, ndims-1 ) ] *= -1;
offset = strides2offset( shape, strides );
console.log( '' );
console.log( 'Strides: %s', strides.join( ',' ) );
console.log( 'Offset: %d', offset );
arr = ndarray( 'generic', buffer, shape, strides, offset, order );
console.log( ndarray2localeString( arr ) );
}