Skip to content

Commit 1124fc6

Browse files
committed
feat: add ndarray/numel-dimension
1 parent 46c964b commit 1124fc6

File tree

10 files changed

+791
-0
lines changed

10 files changed

+791
-0
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2023 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+
# numelDimension
22+
23+
> Return the size (i.e., number of elements) of a specified dimension for a provided [ndarray][@stdlib/ndarray/ctor].
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 numelDimension = require( '@stdlib/ndarray/numel-dimension' );
41+
```
42+
43+
#### numelDimension( x, dim )
44+
45+
Returns the size (i.e., number of elements) of a specified dimension for a provided [ndarray][@stdlib/ndarray/ctor].
46+
47+
```javascript
48+
var zeros = require( '@stdlib/ndarray/zeros' );
49+
50+
var x = zeros( [ 4, 2, 3 ] );
51+
// returns <ndarray>
52+
53+
var d = numelDimension( x, 0 );
54+
// returns 4
55+
```
56+
57+
The function accepts the following arguments:
58+
59+
- **x**: input ndarray.
60+
- **dim**: dimension index. If provided an integer less than zero, the dimension index is resolved relative to the last dimension, with the last dimension corresponding to the value `-1`.
61+
62+
</section>
63+
64+
<!-- /.usage -->
65+
66+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
67+
68+
<section class="notes">
69+
70+
## Notes
71+
72+
- This function is intended as a slight performance optimization over [`@stdlib/ndarray/shape`][@stdlib/ndarray/shape] when **only** a single dimension size is needed. For retrieving multiple dimension sizes, use [`@stdlib/ndarray/shape`][@stdlib/ndarray/shape] directly.
73+
74+
</section>
75+
76+
<!-- /.notes -->
77+
78+
<!-- Package usage examples. -->
79+
80+
<section class="examples">
81+
82+
## Examples
83+
84+
<!-- eslint no-undef: "error" -->
85+
86+
<!-- eslint-disable new-cap -->
87+
88+
```javascript
89+
var zeros = require( '@stdlib/ndarray/zeros' );
90+
var slice = require( '@stdlib/ndarray/slice' );
91+
var E = require( '@stdlib/slice/multi' );
92+
var S = require( '@stdlib/slice/ctor' );
93+
var numelDimension = require( '@stdlib/ndarray/numel-dimension' );
94+
95+
// Create an array:
96+
var x = zeros( [ 10, 10, 10, 10 ] );
97+
// returns <ndarray>
98+
99+
// Define some slices:
100+
var slices = [
101+
// :,:,:,:
102+
E( null, null, null, null ),
103+
104+
// 5:10,4,2:4,::-1
105+
E( S( 5, 10 ), 4, S( 2, 4 ), S( null, null, -1 ) ),
106+
107+
// :,:,2,:
108+
E( null, null, 2, null ),
109+
110+
// 1,2,3,:
111+
E( 1, 2, 3, null ),
112+
113+
// 1,3,::2,4::2
114+
E( 1, 3, S( null, null, 2 ), S( 4, null, 2 ) )
115+
];
116+
117+
// Resolve the size of the first dimension for each slice...
118+
var s;
119+
var i;
120+
for ( i = 0; i < slices.length; i++ ) {
121+
s = slice( x, slices[ i ] );
122+
console.log( '(%s) => %d', s.shape.join( ',' ), numelDimension( s, 0 ) );
123+
}
124+
```
125+
126+
</section>
127+
128+
<!-- /.examples -->
129+
130+
<!-- 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. -->
131+
132+
<section class="references">
133+
134+
</section>
135+
136+
<!-- /.references -->
137+
138+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
139+
140+
<section class="related">
141+
142+
</section>
143+
144+
<!-- /.related -->
145+
146+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
147+
148+
<section class="links">
149+
150+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
151+
152+
[@stdlib/ndarray/shape]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/shape
153+
154+
</section>
155+
156+
<!-- /.links -->
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 zeros = require( '@stdlib/ndarray/zeros' );
25+
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
26+
var pkg = require( './../package.json' ).name;
27+
var numelDimension = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var values;
34+
var out;
35+
var i;
36+
37+
values = [
38+
zeros( [ 10, 10, 10, 1 ] ),
39+
zeros( [ 5, 5, 5, 1, 1 ] ),
40+
zeros( [ 3, 4, 5 ] )
41+
];
42+
43+
b.tic();
44+
for ( i = 0; i < b.iterations; i++ ) {
45+
out = numelDimension( values[ i%values.length ], i%2 );
46+
if ( out !== out ) {
47+
b.fail( 'should return an integer' );
48+
}
49+
}
50+
b.toc();
51+
if ( !isInteger( out ) ) {
52+
b.fail( 'should return an integer' );
53+
}
54+
b.pass( 'benchmark finished' );
55+
b.end();
56+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
{{alias}}( x, dim )
3+
Returns the size (i.e., number of elements) of a specified dimension for a
4+
provided ndarray.
5+
6+
Parameters
7+
----------
8+
x: ndarray
9+
Input ndarray.
10+
11+
dim: integer
12+
Dimension index. If less than zero, the index is resolved relative to
13+
the last dimension, with the last dimension corresponding to the value
14+
`-1`.
15+
16+
Returns
17+
-------
18+
out: integer
19+
Dimension size.
20+
21+
Examples
22+
--------
23+
> var out = {{alias}}( {{alias:@stdlib/ndarray/zeros}}( [ 4, 2, 3 ] ), 0 )
24+
4
25+
26+
See Also
27+
--------
28+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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+
* Returns the size (i.e., number of elements) of a specified dimension for a provided ndarray.
27+
*
28+
* @param x - input ndarray
29+
* @param dim - dimension index
30+
* @returns dimension size
31+
*
32+
* @example
33+
* var zeros = require( `@stdlib/ndarray/zeros` );
34+
*
35+
* var d = numelDimension( zeros( [ 4, 2, 3 ] ), 0 );
36+
* // returns 4
37+
*/
38+
declare function numelDimension( x: ndarray, dim: number ): number;
39+
40+
41+
// EXPORTS //
42+
43+
export = numelDimension;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 zeros = require( '@stdlib/ndarray/zeros' );
20+
import numelDimension = require( './index' );
21+
22+
23+
// TESTS //
24+
25+
// The function returns a number...
26+
{
27+
numelDimension( zeros( [ 3, 2, 1 ] ), 0 ); // $ExpectType number
28+
}
29+
30+
// The compiler throws an error if the function is provided a first argument which is not an ndarray...
31+
{
32+
numelDimension( '5', 0 ); // $ExpectError
33+
numelDimension( 5, 0 ); // $ExpectError
34+
numelDimension( true, 0 ); // $ExpectError
35+
numelDimension( false, 0 ); // $ExpectError
36+
numelDimension( null, 0 ); // $ExpectError
37+
numelDimension( undefined, 0 ); // $ExpectError
38+
numelDimension( [ '1', '2' ], 0 ); // $ExpectError
39+
numelDimension( {}, 0 ); // $ExpectError
40+
numelDimension( ( x: number ): number => x, 0 ); // $ExpectError
41+
}
42+
43+
// The compiler throws an error if the function is provided a second argument which is not a number...
44+
{
45+
numelDimension( zeros( [ 3, 2, 1 ] ), '5' ); // $ExpectError
46+
numelDimension( zeros( [ 3, 2, 1 ] ), true ); // $ExpectError
47+
numelDimension( zeros( [ 3, 2, 1 ] ), false ); // $ExpectError
48+
numelDimension( zeros( [ 3, 2, 1 ] ), null ); // $ExpectError
49+
numelDimension( zeros( [ 3, 2, 1 ] ), undefined ); // $ExpectError
50+
numelDimension( zeros( [ 3, 2, 1 ] ), [ '1', '2' ] ); // $ExpectError
51+
numelDimension( zeros( [ 3, 2, 1 ] ), {} ); // $ExpectError
52+
numelDimension( zeros( [ 3, 2, 1 ] ), ( x: number ): number => x ); // $ExpectError
53+
}
54+
55+
// The compiler throws an error if the function is provided an unsupported number of arguments...
56+
{
57+
numelDimension(); // $ExpectError
58+
numelDimension( zeros( [ 2, 2 ] ) ); // $ExpectError
59+
numelDimension( zeros( [ 2, 2 ] ), 0, {} ); // $ExpectError
60+
}

0 commit comments

Comments
 (0)