Skip to content
Merged
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
132 changes: 132 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,132 @@
<!--

@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 array = require( '@stdlib/ndarray/array' );

var x = array( [ 1, 2, 3, 4 ], {
'shape': [ 2, 2 ]
});
// returns <ndarray>

var str = ndarray2string( x );
// returns "ndarray( 'float64', new Float64Array( [ 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 array = require( '@stdlib/ndarray/array' );
var ndarray2string = require( '@stdlib/ndarray/to-string' );

// Create a 2x3 ndarray:
var x = array( [ 1, 2, 3, 4, 5, 6 ], {
'shape': [ 2, 3 ],
'dtype': 'generic'
});

// Serialize the ndarray as a string:
var str = ndarray2string( x );
console.log( str );
// => 'ndarray( \'generic\', [ 1, 2, 3, 4, 5, 6 ], [ 2, 3 ], [ 3, 1 ], 0, \'row-major\' )'
```

</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 { ndarray } 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( x: ndarray ): 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
}
33 changes: 33 additions & 0 deletions lib/node_modules/@stdlib/ndarray/to-string/examples/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @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';

var array = require( '@stdlib/ndarray/array' );
var ndarray2string = require( './../lib' );

// Create a 2x3 ndarray:
var x = array( [ 1, 2, 3, 4, 5, 6 ], {
'shape': [ 2, 3 ],
'dtype': 'generic'
});

// Serialize the ndarray as a string:
var str = ndarray2string( x );
console.log( str );
// => 'ndarray( \'generic\', [ 1, 2, 3, 4, 5, 6 ], [ 2, 3 ], [ 3, 1 ], 0, \'row-major\' )'
Loading