Skip to content

Commit 74b8842

Browse files
committed
feat: add slice/base/nonreduced-dimensions
1 parent 2bcc7a5 commit 74b8842

File tree

10 files changed

+889
-0
lines changed

10 files changed

+889
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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+
# nonreducedDimensions
22+
23+
> Return a list of non-reduced dimensions in an un-normalized [`MultiSlice`][@stdlib/slice/multi] object.
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+
In a multi-axis indexing expression (e.g., '1,:,2,:,0:10:1'), a non-reduced dimension corresponds to a dimension which is retained in the slice result. Only integer indexing expressions (e.g., only the first and third dimensions in '1,:,2,:,0:10:1') result in dimension reduction; slices always retain respective dimensions, even if a slice is empty or only selects a single element.
30+
31+
</section>
32+
33+
<!-- /.intro -->
34+
35+
<!-- Package usage documentation. -->
36+
37+
<section class="usage">
38+
39+
## Usage
40+
41+
```javascript
42+
var nonreducedDimensions = require( '@stdlib/slice/base/nonreduced-dimensions' );
43+
```
44+
45+
<a name="main"></a>
46+
47+
#### nonreducedDimensions( slice )
48+
49+
Return a list of non-reduced dimensions in an un-normalized [`MultiSlice`][@stdlib/slice/multi] object.
50+
51+
```javascript
52+
var MultiSlice = require( '@stdlib/slice/multi' );
53+
var Slice = require( '@stdlib/slice/ctor' );
54+
55+
var s = new MultiSlice( 1, null, 2, void 0, new Slice( 0, 10, 1 ) );
56+
// returns <MultiSlice>
57+
58+
var indices = nonreducedDimensions( s );
59+
// returns [ 1, 3, 4 ]
60+
```
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+
</section>
71+
72+
<!-- /.notes -->
73+
74+
<!-- Package usage examples. -->
75+
76+
<section class="examples">
77+
78+
## Examples
79+
80+
<!-- eslint no-undef: "error" -->
81+
82+
<!-- eslint-disable new-cap -->
83+
84+
```javascript
85+
var S = require( '@stdlib/slice/ctor' );
86+
var MultiSlice = require( '@stdlib/slice/multi' );
87+
var nonreducedDimensions = require( '@stdlib/slice/base/nonreduced-dimensions' );
88+
89+
var s = new MultiSlice( null, S(), -1 );
90+
var out = nonreducedDimensions( s );
91+
console.log( '%s => [%s]', s.toString(), out.join( ',' ) );
92+
93+
s = new MultiSlice( null );
94+
out = nonreducedDimensions( s );
95+
console.log( '%s => [%s]', s.toString(), out.join( ',' ) );
96+
97+
s = new MultiSlice( S( -1, null, -1 ), 3 );
98+
out = nonreducedDimensions( s );
99+
console.log( '%s => [%s]', s.toString(), out.join( ',' ) );
100+
101+
s = new MultiSlice(out, S( -10, -2, 2 ), 3, null );
102+
out = nonreducedDimensions( s );
103+
console.log( '%s => [%s]', s.toString(), out.join( ',' ) );
104+
105+
s = new MultiSlice( S( 1, 20, 2 ), S( null, null, -1 ) );
106+
out = nonreducedDimensions( s );
107+
console.log( '%s => [%s]', s.toString(), out.join( ',' ) );
108+
```
109+
110+
</section>
111+
112+
<!-- /.examples -->
113+
114+
<!-- 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. -->
115+
116+
<section class="references">
117+
118+
</section>
119+
120+
<!-- /.references -->
121+
122+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
123+
124+
<section class="related">
125+
126+
</section>
127+
128+
<!-- /.related -->
129+
130+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
131+
132+
<section class="links">
133+
134+
[@stdlib/slice/multi]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/slice/multi
135+
136+
</section>
137+
138+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
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+
/* eslint-disable max-len */
20+
21+
'use strict';
22+
23+
// MODULES //
24+
25+
var bench = require( '@stdlib/bench' );
26+
var isNonNegativeIntegerArray = require( '@stdlib/assert/is-nonnegative-integer-array' ).primitives;
27+
var S = require( '@stdlib/slice/ctor' );
28+
var MultiSlice = require( '@stdlib/slice/multi' );
29+
var pkg = require( './../package.json' ).name;
30+
var nonreducedDimensions = require( './../lib' );
31+
32+
33+
// MAIN //
34+
35+
bench( pkg+'::ndims=1', function benchmark( b ) {
36+
var values;
37+
var out;
38+
var i;
39+
40+
/* eslint-disable new-cap */
41+
42+
values = [
43+
new MultiSlice( S( null, null, null ) ),
44+
new MultiSlice( S( 0, 10, 2 ) ),
45+
new MultiSlice( S( null, 10, 2 ) ),
46+
new MultiSlice( S( 0, null, 2 ) ),
47+
new MultiSlice( S( 0, 10, null ) ),
48+
new MultiSlice( S( -20, -5, -2 ) ),
49+
new MultiSlice( S( 5, 20, 2 ) ),
50+
new MultiSlice( S( null, null, -1 ) ),
51+
new MultiSlice( S( 5, -20, -2 ) ),
52+
new MultiSlice( S( 20, null, -1 ) ),
53+
new MultiSlice( S( 0, -2, 2 ) ),
54+
new MultiSlice( S( -5, null, 2 ) ),
55+
new MultiSlice( 1 )
56+
];
57+
58+
/* eslint-enable new-cap */
59+
60+
b.tic();
61+
for ( i = 0; i < b.iterations; i++ ) {
62+
out = nonreducedDimensions( values[ i%values.length ] );
63+
if ( typeof out !== 'object' ) {
64+
b.fail( 'should return an array' );
65+
}
66+
}
67+
b.toc();
68+
if ( !isNonNegativeIntegerArray( out ) ) {
69+
b.fail( 'should return an array' );
70+
}
71+
b.pass( 'benchmark finished' );
72+
b.end();
73+
});
74+
75+
bench( pkg+'::ndims=2', function benchmark( b ) {
76+
var values;
77+
var out;
78+
var i;
79+
80+
/* eslint-disable new-cap */
81+
82+
values = [
83+
new MultiSlice( S( 2, 10, 2 ), S( null, null, null ) ),
84+
new MultiSlice( S( 2, 10, 2 ), S( 0, 10, 2 ) ),
85+
new MultiSlice( S( 2, 10, 2 ), S( null, 10, 2 ) ),
86+
new MultiSlice( S( 2, -10, 2 ), S( 0, null, 2 ) ),
87+
new MultiSlice( S( -2, 10, 2 ), S( 0, 10, null ) ),
88+
new MultiSlice( S( 2, 10, 2 ), S( -20, -5, -2 ) ),
89+
new MultiSlice( S( 2, null, -2 ), S( 5, 20, 2 ) ),
90+
new MultiSlice( S( 2, 10, 2 ), S( null, null, -1 ) ),
91+
new MultiSlice( S( 20, -10, -2 ), S( 5, -20, -2 ) ),
92+
new MultiSlice( S( 2, 10, 2 ), S( 20, null, -1 ) ),
93+
new MultiSlice( S( 2, 10, 2 ), S( 0, -2, 2 ) ),
94+
new MultiSlice( S( 2, 10, 2 ), S( -5, null, 2 ) ),
95+
new MultiSlice( 1, 2 )
96+
];
97+
98+
/* eslint-enable new-cap */
99+
100+
b.tic();
101+
for ( i = 0; i < b.iterations; i++ ) {
102+
out = nonreducedDimensions( values[ i%values.length ] );
103+
if ( typeof out !== 'object' ) {
104+
b.fail( 'should return an array' );
105+
}
106+
}
107+
b.toc();
108+
if ( !isNonNegativeIntegerArray( out ) ) {
109+
b.fail( 'should return an array' );
110+
}
111+
b.pass( 'benchmark finished' );
112+
b.end();
113+
});
114+
115+
bench( pkg+'::ndims=3', function benchmark( b ) {
116+
var values;
117+
var out;
118+
var i;
119+
120+
/* eslint-disable new-cap */
121+
122+
values = [
123+
new MultiSlice( S(), S( 2, 10, 2 ), S( null, null, null ) ),
124+
new MultiSlice( S(), S( 2, 10, 2 ), S( 0, 10, 2 ) ),
125+
new MultiSlice( S(), S( 2, 10, 2 ), S( null, 10, 2 ) ),
126+
new MultiSlice( S(), S( 2, -10, 2 ), S( 0, null, 2 ) ),
127+
new MultiSlice( S(), S( -2, 10, 2 ), S( 0, 10, null ) ),
128+
new MultiSlice( S(), S( 2, 10, 2 ), S( -20, -5, -2 ) ),
129+
new MultiSlice( S(), S( 2, null, -2 ), S( 5, 20, 2 ) ),
130+
new MultiSlice( S(), S( 2, 10, 2 ), S( null, null, -1 ) ),
131+
new MultiSlice( S(), S( 20, -10, -2 ), S( 5, -20, -2 ) ),
132+
new MultiSlice( S(), S( 2, 10, 2 ), S( 20, null, -1 ) ),
133+
new MultiSlice( S(), S( 2, 10, 2 ), S( 0, -2, 2 ) ),
134+
new MultiSlice( S(), S( 2, 10, 2 ), S( -5, null, 2 ) ),
135+
new MultiSlice( 1, 2, 3 )
136+
];
137+
138+
/* eslint-enable new-cap */
139+
140+
b.tic();
141+
for ( i = 0; i < b.iterations; i++ ) {
142+
out = nonreducedDimensions( values[ i%values.length ] );
143+
if ( typeof out !== 'object' ) {
144+
b.fail( 'should return an array' );
145+
}
146+
}
147+
b.toc();
148+
if ( !isNonNegativeIntegerArray( out ) ) {
149+
b.fail( 'should return an array' );
150+
}
151+
b.pass( 'benchmark finished' );
152+
b.end();
153+
});
154+
155+
bench( pkg+'::ndims=4', function benchmark( b ) {
156+
var values;
157+
var out;
158+
var i;
159+
160+
/* eslint-disable new-cap */
161+
162+
values = [
163+
new MultiSlice( S( 2, null, -1 ), S(), S( 2, 10, 2 ), S( null, null, null ) ),
164+
new MultiSlice( S( 2, null, -1 ), S(), S( 2, 10, 2 ), S( 0, 10, 2 ) ),
165+
new MultiSlice( S( 2, null, -1 ), S(), S( 2, 10, 2 ), S( null, 10, 2 ) ),
166+
new MultiSlice( S( 2, null, -1 ), S(), S( 2, -10, 2 ), S( 0, null, 2 ) ),
167+
new MultiSlice( S( 2, null, -1 ), S(), S( -2, 10, 2 ), S( 0, 10, null ) ),
168+
new MultiSlice( S( 2, null, -1 ), S(), S( 2, 10, 2 ), S( -20, -5, -2 ) ),
169+
new MultiSlice( S( 2, 5, -1 ), S(), S( 2, null, -2 ), S( 5, 20, 2 ) ),
170+
new MultiSlice( S( 2, null, -1 ), S(), S( 2, 10, 2 ), S( null, null, -1 ) ),
171+
new MultiSlice( S( 2, null, -1 ), S(), S( 20, -10, -2 ), S( 5, -20, -2 ) ),
172+
new MultiSlice( S( 2, -5, -1 ), S(), S( 2, 10, 2 ), S( 20, null, -1 ) ),
173+
new MultiSlice( S( 2, null, -1 ), S(), S( 2, 10, 2 ), S( 0, -2, 2 ) ),
174+
new MultiSlice( S( 2, null, -1 ), S(), S( 2, 10, 2 ), S( -5, null, 2 ) ),
175+
new MultiSlice( 1, 2, 3, 4 )
176+
];
177+
178+
/* eslint-enable new-cap */
179+
180+
b.tic();
181+
for ( i = 0; i < b.iterations; i++ ) {
182+
out = nonreducedDimensions( values[ i%values.length ] );
183+
if ( typeof out !== 'object' ) {
184+
b.fail( 'should return an array' );
185+
}
186+
}
187+
b.toc();
188+
if ( !isNonNegativeIntegerArray( out ) ) {
189+
b.fail( 'should return an array' );
190+
}
191+
b.pass( 'benchmark finished' );
192+
b.end();
193+
});
194+
195+
bench( pkg+'::ndims=5', function benchmark( b ) {
196+
var values;
197+
var out;
198+
var i;
199+
200+
/* eslint-disable new-cap */
201+
202+
values = [
203+
new MultiSlice( S(), S( 2, null, -1 ), S(), S( 2, 10, 2 ), S( null, null, null ) ),
204+
new MultiSlice( S(), S( 2, null, -1 ), S(), S( 2, 10, 2 ), S( 0, 10, 2 ) ),
205+
new MultiSlice( S(), S( 2, null, -1 ), S(), S( 2, 10, 2 ), S( null, 10, 2 ) ),
206+
new MultiSlice( S(), S( 2, null, -1 ), S(), S( 2, -10, 2 ), S( 0, null, 2 ) ),
207+
new MultiSlice( S(), S( 2, null, -1 ), S(), S( -2, 10, 2 ), S( 0, 10, null ) ),
208+
new MultiSlice( S(), S( 2, null, -1 ), S(), S( 2, 10, 2 ), S( -20, -5, -2 ) ),
209+
new MultiSlice( S(), S( 2, 5, -1 ), S(), S( 2, null, -2 ), S( 5, 20, 2 ) ),
210+
new MultiSlice( S(), S( 2, null, -1 ), S(), S( 2, 10, 2 ), S( null, null, -1 ) ),
211+
new MultiSlice( S(), S( 2, null, -1 ), S(), S( 20, -10, -2 ), S( 5, -20, -2 ) ),
212+
new MultiSlice( S(), S( 2, -5, -1 ), S(), S( 2, 10, 2 ), S( 20, null, -1 ) ),
213+
new MultiSlice( S(), S( 2, null, -1 ), S(), S( 2, 10, 2 ), S( 0, -2, 2 ) ),
214+
new MultiSlice( S(), S( 2, null, -1 ), S(), S( 2, 10, 2 ), S( -5, null, 2 ) ),
215+
new MultiSlice( 1, 2, 3, 4, 5 )
216+
];
217+
218+
/* eslint-enable new-cap */
219+
220+
b.tic();
221+
for ( i = 0; i < b.iterations; i++ ) {
222+
out = nonreducedDimensions( values[ i%values.length ] );
223+
if ( typeof out !== 'object' ) {
224+
b.fail( 'should return an array' );
225+
}
226+
}
227+
b.toc();
228+
if ( !isNonNegativeIntegerArray( out ) ) {
229+
b.fail( 'should return an array' );
230+
}
231+
b.pass( 'benchmark finished' );
232+
b.end();
233+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
{{alias}}( slice )
3+
Returns a list of non-reduced dimensions in an un-normalized multi-slice.
4+
5+
Parameters
6+
----------
7+
slice: MultiSlice
8+
Input slice object.
9+
10+
Returns
11+
-------
12+
out: Array<integer>
13+
Dimension indices.
14+
15+
Examples
16+
--------
17+
> var s = new {{alias:@stdlib/slice/multi}}( 1, 3, null );
18+
> var out = {{alias}}( s )
19+
[ 2 ]
20+
21+
See Also
22+
--------
23+

0 commit comments

Comments
 (0)