Skip to content

Commit 47e4985

Browse files
headlessNodekgrytestdlib-bot
authored
feat: add ndarray/to-string
PR-URL: #10899 Closes: stdlib-js/metr-issue-tracker#188 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Co-authored-by: stdlib-bot <noreply@stdlib.io>
1 parent 236975e commit 47e4985

File tree

10 files changed

+1283
-0
lines changed

10 files changed

+1283
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# ndarray2string
22+
23+
> Serialize an [ndarray][@stdlib/ndarray/ctor] as a string.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var ndarray2string = require( '@stdlib/ndarray/to-string' );
41+
```
42+
43+
#### ndarray2string( x )
44+
45+
Serializes an [ndarray][@stdlib/ndarray/ctor] as a string.
46+
47+
```javascript
48+
var array = require( '@stdlib/ndarray/array' );
49+
50+
var x = array( [ 1, 2, 3, 4 ], {
51+
'shape': [ 2, 2 ]
52+
});
53+
// returns <ndarray>
54+
55+
var str = ndarray2string( x );
56+
// returns "ndarray( 'float64', new Float64Array( [ 1, 2, 3, 4 ] ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' )"
57+
```
58+
59+
</section>
60+
61+
<!-- /.usage -->
62+
63+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
64+
65+
<section class="notes">
66+
67+
## Notes
68+
69+
- The function does **not** serialize data outside of the buffer defined by the [ndarray][@stdlib/ndarray/ctor] view.
70+
- For ndarrays with more than `100` elements, the function abbreviates the data, showing only the first and last three values.
71+
72+
</section>
73+
74+
<!-- /.notes -->
75+
76+
<!-- Package usage examples. -->
77+
78+
<section class="examples">
79+
80+
## Examples
81+
82+
<!-- eslint no-undef: "error" -->
83+
84+
```javascript
85+
var array = require( '@stdlib/ndarray/array' );
86+
var ndarray2string = require( '@stdlib/ndarray/to-string' );
87+
88+
// Create a 2x3 ndarray:
89+
var x = array( [ 1, 2, 3, 4, 5, 6 ], {
90+
'shape': [ 2, 3 ],
91+
'dtype': 'generic'
92+
});
93+
94+
// Serialize the ndarray as a string:
95+
var str = ndarray2string( x );
96+
console.log( str );
97+
// => 'ndarray( \'generic\', [ 1, 2, 3, 4, 5, 6 ], [ 2, 3 ], [ 3, 1 ], 0, \'row-major\' )'
98+
```
99+
100+
</section>
101+
102+
<!-- /.examples -->
103+
104+
<!-- 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. -->
105+
106+
<section class="references">
107+
108+
</section>
109+
110+
<!-- /.references -->
111+
112+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
113+
114+
<section class="related">
115+
116+
</section>
117+
118+
<!-- /.related -->
119+
120+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
121+
122+
<section class="links">
123+
124+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
125+
126+
<!-- <related-links> -->
127+
128+
<!-- </related-links> -->
129+
130+
</section>
131+
132+
<!-- /.links -->
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
25+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
26+
var strides2offset = require( '@stdlib/ndarray/base/strides2offset' );
27+
var numel = require( '@stdlib/ndarray/base/numel' );
28+
var zeroTo = require( '@stdlib/array/base/zero-to' );
29+
var format = require( '@stdlib/string/format' );
30+
var ndarray = require( '@stdlib/ndarray/ctor' );
31+
var pkg = require( './../package.json' ).name;
32+
var ndarray2string = require( './../lib' );
33+
34+
35+
// MAIN //
36+
37+
bench( format( '%s:order=row-major', pkg ), function benchmark( b ) {
38+
var strides;
39+
var buffer;
40+
var offset;
41+
var order;
42+
var shape;
43+
var arr;
44+
var out;
45+
var i;
46+
47+
shape = [ 10, 10, 10 ];
48+
order = 'row-major';
49+
buffer = zeroTo( numel( shape ) );
50+
strides = shape2strides( shape, order );
51+
offset = strides2offset( shape, strides );
52+
arr = ndarray( 'generic', buffer, shape, strides, offset, order );
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
out = ndarray2string( arr );
57+
if ( typeof out !== 'string' ) {
58+
b.fail( 'should return a string' );
59+
}
60+
}
61+
b.toc();
62+
if ( !isString( out ) ) {
63+
b.fail( 'should return a string' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
});
68+
69+
bench( format( '%s:order=column-major', pkg ), function benchmark( b ) {
70+
var strides;
71+
var buffer;
72+
var offset;
73+
var order;
74+
var shape;
75+
var arr;
76+
var out;
77+
var i;
78+
79+
shape = [ 10, 10, 10 ];
80+
order = 'column-major';
81+
buffer = zeroTo( numel( shape ) );
82+
strides = shape2strides( shape, order );
83+
offset = strides2offset( shape, strides );
84+
arr = ndarray( 'generic', buffer, shape, strides, offset, order );
85+
86+
b.tic();
87+
for ( i = 0; i < b.iterations; i++ ) {
88+
out = ndarray2string( arr );
89+
if ( typeof out !== 'string' ) {
90+
b.fail( 'should return a string' );
91+
}
92+
}
93+
b.toc();
94+
if ( !isString( out ) ) {
95+
b.fail( 'should return a string' );
96+
}
97+
b.pass( 'benchmark finished' );
98+
b.end();
99+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
{{alias}}( x )
3+
Serializes an ndarray as a string.
4+
5+
This function does *not* serialize data outside of the buffer region
6+
defined by the ndarray view.
7+
8+
Parameters
9+
----------
10+
x: ndarray
11+
Input ndarray.
12+
13+
Returns
14+
-------
15+
out: string
16+
String representation.
17+
18+
Examples
19+
--------
20+
> var arr = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] );
21+
> var out = {{alias}}( arr )
22+
<string>
23+
24+
See Also
25+
--------
26+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { ndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Serializes an ndarray as a string.
27+
*
28+
* ## Notes
29+
*
30+
* - The function does **not** serialize data outside of the buffer region defined by the ndarray view.
31+
*
32+
* @param x - input ndarray
33+
* @returns string representation
34+
*
35+
* @example
36+
* var array = require( `@stdlib/ndarray/array` );
37+
*
38+
* var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] );
39+
* // returns <ndarray>
40+
*
41+
* var str = ndarray2string( x );
42+
* // returns <string>
43+
*/
44+
declare function ndarray2string( x: ndarray ): string;
45+
46+
47+
// EXPORTS //
48+
49+
export = ndarray2string;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import array = require( '@stdlib/ndarray/array' );
20+
import ndarray2string = require( './index' );
21+
22+
23+
// TESTS //
24+
25+
// The function returns a string...
26+
{
27+
ndarray2string( array( [ [ 1, 2 ], [ 3, 4 ] ] ) ); // $ExpectType string
28+
}
29+
30+
// The compiler throws an error if the function is provided a first argument which is not an ndarray...
31+
{
32+
ndarray2string( 10 ); // $ExpectError
33+
ndarray2string( '10' ); // $ExpectError
34+
ndarray2string( true ); // $ExpectError
35+
ndarray2string( false ); // $ExpectError
36+
ndarray2string( null ); // $ExpectError
37+
ndarray2string( undefined ); // $ExpectError
38+
ndarray2string( [ 1, 2 ] ); // $ExpectError
39+
ndarray2string( {} ); // $ExpectError
40+
ndarray2string( ( x: number ): number => x ); // $ExpectError
41+
}
42+
43+
// The compiler throws an error if the function is provided an unsupported number of arguments...
44+
{
45+
const arr = array( [ [ 1, 2 ], [ 3, 4 ] ] );
46+
47+
ndarray2string(); // $ExpectError
48+
ndarray2string( arr, {} ); // $ExpectError
49+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
var array = require( '@stdlib/ndarray/array' );
22+
var ndarray2string = require( './../lib' );
23+
24+
// Create a 2x3 ndarray:
25+
var x = array( [ 1, 2, 3, 4, 5, 6 ], {
26+
'shape': [ 2, 3 ],
27+
'dtype': 'generic'
28+
});
29+
30+
// Serialize the ndarray as a string:
31+
var str = ndarray2string( x );
32+
console.log( str );
33+
// => 'ndarray( \'generic\', [ 1, 2, 3, 4, 5, 6 ], [ 2, 3 ], [ 3, 1 ], 0, \'row-major\' )'

0 commit comments

Comments
 (0)