Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
160 changes: 160 additions & 0 deletions lib/node_modules/@stdlib/ndarray/to-string/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<!--

@license Apache-2.0

Copyright (c) 2026 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.

-->

# ndarray2string

> Serialize an [ndarray][@stdlib/ndarray/ctor] as a string.

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage

```javascript
var ndarray2string = require( '@stdlib/ndarray/to-string' );
```

#### ndarray2string( x )

Serializes an [ndarray][@stdlib/ndarray/ctor] as a string.

```javascript
var ndarray = require( '@stdlib/ndarray/ctor' );

var buffer = [ 1, 2, 3, 4 ];
var shape = [ 2, 2 ];
var order = 'row-major';
var strides = [ 2, 1 ];
var offset = 0;

var x = ndarray( 'generic', buffer, shape, strides, offset, order );
// returns <ndarray>

var str = ndarray2string( x );
// returns "ndarray( 'generic', [ 1, 2, 3, 4 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' )"
```

</section>

<!-- /.usage -->

<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

## Notes

- The function does **not** serialize data outside of the buffer defined by the [ndarray][@stdlib/ndarray/ctor] view.
- For ndarrays with more than `100` elements, the function abbreviates the data, showing only the first and last three values.

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
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 ndarray2string = require( '@stdlib/ndarray/to-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 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( ndarray2string( arr ) );
}
```

</section>

<!-- /.examples -->

<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="references">

</section>

<!-- /.references -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

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

<!-- <related-links> -->

<!-- </related-links> -->

</section>

<!-- /.links -->
99 changes: 99 additions & 0 deletions lib/node_modules/@stdlib/ndarray/to-string/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 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.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
var strides2offset = require( '@stdlib/ndarray/base/strides2offset' );
var numel = require( '@stdlib/ndarray/base/numel' );
var zeroTo = require( '@stdlib/array/base/zero-to' );
var format = require( '@stdlib/string/format' );
var ndarray = require( '@stdlib/ndarray/ctor' );
var pkg = require( './../package.json' ).name;
var ndarray2string = require( './../lib' );


// MAIN //

bench( format( '%s:order=row-major', pkg ), function benchmark( b ) {
var strides;
var buffer;
var offset;
var order;
var shape;
var arr;
var out;
var i;

shape = [ 10, 10, 10 ];
order = 'row-major';
buffer = zeroTo( numel( shape ) );
strides = shape2strides( shape, order );
offset = strides2offset( shape, strides );
arr = ndarray( 'generic', buffer, shape, strides, offset, order );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = ndarray2string( arr );
if ( typeof out !== 'string' ) {
b.fail( 'should return a string' );
}
}
b.toc();
if ( !isString( out ) ) {
b.fail( 'should return a string' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( format( '%s:order=column-major', pkg ), function benchmark( b ) {
var strides;
var buffer;
var offset;
var order;
var shape;
var arr;
var out;
var i;

shape = [ 10, 10, 10 ];
order = 'column-major';
buffer = zeroTo( numel( shape ) );
strides = shape2strides( shape, order );
offset = strides2offset( shape, strides );
arr = ndarray( 'generic', buffer, shape, strides, offset, order );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = ndarray2string( arr );
if ( typeof out !== 'string' ) {
b.fail( 'should return a string' );
}
}
b.toc();
if ( !isString( out ) ) {
b.fail( 'should return a string' );
}
b.pass( 'benchmark finished' );
b.end();
});
26 changes: 26 additions & 0 deletions lib/node_modules/@stdlib/ndarray/to-string/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

{{alias}}( x )
Serializes an ndarray as a string.

This function does *not* serialize data outside of the buffer region
defined by the ndarray view.

Parameters
----------
x: ndarray
Input ndarray.

Returns
-------
out: string
String representation.

Examples
--------
> var arr = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] );
> var out = {{alias}}( arr )
<string>

See Also
--------

49 changes: 49 additions & 0 deletions lib/node_modules/@stdlib/ndarray/to-string/docs/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2026 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.
*/

// TypeScript Version: 4.1

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

import { typedndarray } from '@stdlib/types/ndarray';

/**
* Serializes an ndarray as a string.
*
* ## Notes
*
* - The function does **not** serialize data outside of the buffer region defined by the ndarray view.
*
* @param x - input ndarray
* @returns string representation
*
* @example
* var array = require( `@stdlib/ndarray/array` );
*
* var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] );
* // returns <ndarray>
*
* var str = ndarray2string( x );
* // returns <string>
*/
declare function ndarray2string<T = unknown>( x: typedndarray<T> ): string;


// EXPORTS //

export = ndarray2string;
49 changes: 49 additions & 0 deletions lib/node_modules/@stdlib/ndarray/to-string/docs/types/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2026 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.
*/

import array = require( '@stdlib/ndarray/array' );
import ndarray2string = require( './index' );


// TESTS //

// The function returns a string...
{
ndarray2string( array( [ [ 1, 2 ], [ 3, 4 ] ] ) ); // $ExpectType string
}

// The compiler throws an error if the function is provided a first argument which is not an ndarray...
{
ndarray2string( 10 ); // $ExpectError
ndarray2string( '10' ); // $ExpectError
ndarray2string( true ); // $ExpectError
ndarray2string( false ); // $ExpectError
ndarray2string( null ); // $ExpectError
ndarray2string( undefined ); // $ExpectError
ndarray2string( [ 1, 2 ] ); // $ExpectError
ndarray2string( {} ); // $ExpectError
ndarray2string( ( x: number ): number => x ); // $ExpectError
}

// The compiler throws an error if the function is provided an unsupported number of arguments...
{
const arr = array( [ [ 1, 2 ], [ 3, 4 ] ] );

ndarray2string(); // $ExpectError
ndarray2string( arr, {} ); // $ExpectError
}
Loading
Loading