Skip to content

Commit 35051b4

Browse files
headlessNodekgryte
andauthored
feat: add ndarray/ndarraylike2scalar
PR-URL: #11780 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#468
1 parent cfc58ab commit 35051b4

10 files changed

Lines changed: 650 additions & 0 deletions

File tree

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
# ndarraylike2scalar
22+
23+
> Convert an ndarray-like object to a scalar value.
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 ndarraylike2scalar = require( '@stdlib/ndarray/ndarraylike2scalar' );
41+
```
42+
43+
#### ndarraylike2scalar( x )
44+
45+
Converts an ndarray-like object to a scalar value.
46+
47+
```javascript
48+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
49+
50+
var x = scalar2ndarray( 1.0 );
51+
// returns <ndarray>[ 1.0 ]
52+
53+
var out = ndarraylike2scalar( x );
54+
// returns 1.0
55+
```
56+
57+
</section>
58+
59+
<!-- /.usage -->
60+
61+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
62+
63+
<section class="notes">
64+
65+
</section>
66+
67+
<!-- /.notes -->
68+
69+
<!-- Package usage examples. -->
70+
71+
<section class="examples">
72+
73+
## Examples
74+
75+
<!-- eslint no-undef: "error" -->
76+
77+
```javascript
78+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
79+
var compose = require( '@stdlib/utils/compose' );
80+
var naryFunction = require( '@stdlib/utils/nary-function' );
81+
var oneTo = require( '@stdlib/array/one-to' );
82+
var logEachMap = require( '@stdlib/console/log-each-map' );
83+
var ndarraylike2scalar = require( '@stdlib/ndarray/ndarraylike2scalar' );
84+
85+
// Create a list of scalars:
86+
var scalars = oneTo( 10 );
87+
88+
// Create a composite function which round-trips a scalar to an ndarray and back:
89+
var f = compose( ndarraylike2scalar, naryFunction( scalar2ndarray, 1 ) );
90+
91+
// Apply the function to the list of scalars:
92+
logEachMap( '%d => %d', scalars, f );
93+
```
94+
95+
</section>
96+
97+
<!-- /.examples -->
98+
99+
<!-- 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. -->
100+
101+
<section class="references">
102+
103+
</section>
104+
105+
<!-- /.references -->
106+
107+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
108+
109+
<section class="related">
110+
111+
</section>
112+
113+
<!-- /.related -->
114+
115+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
116+
117+
<section class="links">
118+
119+
</section>
120+
121+
<!-- /.links -->
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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 scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
25+
var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
26+
var format = require( '@stdlib/string/format' );
27+
var pkg = require( './../package.json' ).name;
28+
var ndarraylike2scalar = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( format( '%s:dtype=float64', pkg ), function benchmark( b ) {
34+
var values;
35+
var out;
36+
var i;
37+
38+
values = [
39+
scalar2ndarray( 1.0 ),
40+
scalar2ndarray( 2.0 ),
41+
scalar2ndarray( 3.0 ),
42+
scalar2ndarray( 4.0 ),
43+
scalar2ndarray( 5.0 )
44+
];
45+
46+
b.tic();
47+
for ( i = 0; i < b.iterations; i++ ) {
48+
out = ndarraylike2scalar( values[ i%values.length ] );
49+
if ( typeof out !== 'number' ) {
50+
b.fail( 'should return a number' );
51+
}
52+
}
53+
b.toc();
54+
if ( !isNumber( out ) ) {
55+
b.fail( 'should return a number' );
56+
}
57+
b.pass( 'benchmark finished' );
58+
b.end();
59+
});
60+
61+
bench( format( '%s:dtype=float32', pkg ), function benchmark( b ) {
62+
var values;
63+
var opts;
64+
var out;
65+
var i;
66+
67+
opts = {
68+
'dtype': 'float32'
69+
};
70+
values = [
71+
scalar2ndarray( 1.0, opts ),
72+
scalar2ndarray( 2.0, opts ),
73+
scalar2ndarray( 3.0, opts ),
74+
scalar2ndarray( 4.0, opts ),
75+
scalar2ndarray( 5.0, opts )
76+
];
77+
78+
b.tic();
79+
for ( i = 0; i < b.iterations; i++ ) {
80+
out = ndarraylike2scalar( values[ i%values.length ] );
81+
if ( typeof out !== 'number' ) {
82+
b.fail( 'should return a number' );
83+
}
84+
}
85+
b.toc();
86+
if ( !isNumber( out ) ) {
87+
b.fail( 'should return a number' );
88+
}
89+
b.pass( 'benchmark finished' );
90+
b.end();
91+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
{{alias}}( x )
3+
Converts an ndarray-like object to a scalar value.
4+
5+
Parameters
6+
----------
7+
x: ndarrayLike
8+
Input ndarray-like object.
9+
10+
Returns
11+
-------
12+
out: any
13+
Scalar value.
14+
15+
Examples
16+
--------
17+
> var x = {{alias:@stdlib/ndarray/from-scalar}}( 1.0 );
18+
> var out = {{alias}}( x )
19+
1.0
20+
21+
See Also
22+
--------
23+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 { typedndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Converts an ndarray-like object to a scalar value.
27+
*
28+
* @param x - input ndarray
29+
* @returns scalar value
30+
*
31+
* @example
32+
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
33+
*
34+
* var x = scalar2ndarray( 1.0 );
35+
* // returns <ndarray>[ 1.0 ]
36+
*
37+
* var out = ndarraylike2scalar( x );
38+
* // returns 1.0
39+
*/
40+
declare function ndarraylike2scalar<T = unknown>( x: typedndarray<T> ): T;
41+
42+
43+
// EXPORTS //
44+
45+
export = ndarraylike2scalar;
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+
/// <reference types="@stdlib/types"/>
20+
21+
import scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
22+
import ndarraylike2scalar = require( './index' );
23+
24+
25+
// TESTS //
26+
27+
// The function returns a scalar value...
28+
{
29+
ndarraylike2scalar( scalar2ndarray( 1.0 ) ); // $ExpectType number
30+
}
31+
32+
// The compiler throws an error if the function is not provided an ndarray...
33+
{
34+
ndarraylike2scalar( '5' ); // $ExpectError
35+
ndarraylike2scalar( 123 ); // $ExpectError
36+
ndarraylike2scalar( true ); // $ExpectError
37+
ndarraylike2scalar( false ); // $ExpectError
38+
ndarraylike2scalar( null ); // $ExpectError
39+
ndarraylike2scalar( undefined ); // $ExpectError
40+
ndarraylike2scalar( [] ); // $ExpectError
41+
ndarraylike2scalar( {} ); // $ExpectError
42+
ndarraylike2scalar( ( x: number ): number => x ); // $ExpectError
43+
}
44+
45+
// The compiler throws an error if the function is provided an unsupported number of arguments...
46+
{
47+
ndarraylike2scalar(); // $ExpectError
48+
ndarraylike2scalar( scalar2ndarray( 1.0 ), {} ); // $ExpectError
49+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
22+
var compose = require( '@stdlib/utils/compose' );
23+
var naryFunction = require( '@stdlib/utils/nary-function' );
24+
var oneTo = require( '@stdlib/array/one-to' );
25+
var logEachMap = require( '@stdlib/console/log-each-map' );
26+
var ndarraylike2scalar = require( './../lib' );
27+
28+
// Create a list of scalars:
29+
var scalars = oneTo( 10 );
30+
31+
// Create a composite function which round-trips a scalar to an ndarray and back:
32+
var f = compose( ndarraylike2scalar, naryFunction( scalar2ndarray, 1 ) );
33+
34+
// Apply the function to the list of scalars:
35+
logEachMap( '%d => %d', scalars, f );

0 commit comments

Comments
 (0)