Skip to content

Commit 00d86f3

Browse files
committed
Add ndarray utility to return the number of non-singleton dimensions
1 parent 3bb7336 commit 00d86f3

File tree

17 files changed

+1239
-0
lines changed

17 files changed

+1239
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2020 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+
# Non-Singleton Dimensions
22+
23+
> Return the number of non-singleton dimensions.
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 nonsingletonDimensions = require( '@stdlib/ndarray/base/nonsingleton-dimensions' );
41+
```
42+
43+
#### nonsingletonDimensions( shape )
44+
45+
Returns number of non-singleton dimensions.
46+
47+
```javascript
48+
var n = nonsingletonDimensions( [ 3, 1, 3 ] );
49+
// returns 2
50+
```
51+
52+
</section>
53+
54+
<!-- /.usage -->
55+
56+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
57+
58+
<section class="notes">
59+
60+
## Notes
61+
62+
- A singleton dimension is a dimension whose size is equal to `1`.
63+
64+
</section>
65+
66+
<!-- /.notes -->
67+
68+
<!-- Package usage examples. -->
69+
70+
<section class="examples">
71+
72+
## Examples
73+
74+
<!-- eslint no-undef: "error" -->
75+
76+
```javascript
77+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
78+
var nonsingletonDimensions = require( '@stdlib/ndarray/base/nonsingleton-dimensions' );
79+
80+
var shape;
81+
var n;
82+
var i;
83+
84+
shape = [ 0, 0, 0 ];
85+
for ( i = 0; i < 100; i++ ) {
86+
shape[ 0 ] = discreteUniform( 1, 5 );
87+
shape[ 1 ] = discreteUniform( 1, 5 );
88+
shape[ 2 ] = discreteUniform( 1, 5 );
89+
n = nonsingletonDimensions( shape );
90+
console.log( 'shape: %s. non-singleton dimensions: %d.', shape.join( 'x' ), n );
91+
}
92+
```
93+
94+
</section>
95+
96+
<!-- /.examples -->
97+
98+
<!-- C interface documentation. -->
99+
100+
* * *
101+
102+
<section class="c">
103+
104+
## C APIs
105+
106+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
107+
108+
<section class="intro">
109+
110+
</section>
111+
112+
<!-- /.intro -->
113+
114+
<!-- C usage documentation. -->
115+
116+
<section class="usage">
117+
118+
### Usage
119+
120+
```c
121+
#include "stdlib/ndarray/base/nonsingleton_dimensions.h"
122+
```
123+
124+
#### stdlib_ndarray_nonsingleton_dimensions( ndims, \*shape )
125+
126+
Returns the number of non-singleton dimensions.
127+
128+
```c
129+
int64_t ndims = 2;
130+
int64_t shape[] = { 10, 1 };
131+
132+
int64_t n = stdlib_ndarray_nonsingleton_dimensions( ndims, shape );
133+
// returns 1
134+
```
135+
136+
The function accepts the following arguments:
137+
138+
- **ndims**: `[in] int64_t` number of dimensions.
139+
- **shape**: `[in] int64_t*` array shape.
140+
141+
```c
142+
int64_t stdlib_ndarray_nonsingleton_dimensions( int64_t ndims, int64_t *shape );
143+
```
144+
145+
</section>
146+
147+
<!-- /.usage -->
148+
149+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
150+
151+
<section class="notes">
152+
153+
</section>
154+
155+
<!-- /.notes -->
156+
157+
<!-- C API usage examples. -->
158+
159+
<section class="examples">
160+
161+
### Examples
162+
163+
```c
164+
#include "stdlib/ndarray/base/nonsingleton_dimensions.h"
165+
#include <stdio.h>
166+
167+
int main() {
168+
int64_t shape[] = { 10, 3, 1, 1, 5 };
169+
170+
int64_t n = stdlib_ndarray_nonsingleton_dimensions( 5, shape );
171+
printf( "shape: %llux%llux%llux%llux%llu. non-singleton dimensions: %llu\n", shape[ 0 ], shape[ 1 ], shape[ 2 ], shape[ 3 ], shape[ 4 ], n );
172+
}
173+
```
174+
175+
</section>
176+
177+
<!-- /.examples -->
178+
179+
</section>
180+
181+
<!-- /.c -->
182+
183+
<!-- 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. -->
184+
185+
<section class="references">
186+
187+
</section>
188+
189+
<!-- /.references -->
190+
191+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
192+
193+
<section class="links">
194+
195+
</section>
196+
197+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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 discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
25+
var randu = require( '@stdlib/random/base/randu' );
26+
var floor = require( '@stdlib/math/base/special/floor' );
27+
var pkg = require( './../package.json' ).name;
28+
var nonsingletonDimensions = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var shape;
35+
var n;
36+
var i;
37+
38+
shape = [ 0, 0, 0 ];
39+
shape[ 0 ] = discreteUniform( 1, 5 );
40+
shape[ 1 ] = discreteUniform( 1, 5 );
41+
shape[ 2 ] = discreteUniform( 1, 5 );
42+
43+
b.tic();
44+
for ( i = 0; i < b.iterations; i++ ) {
45+
shape[ 0 ] = floor( randu()*5.0 );
46+
n = nonsingletonDimensions( shape );
47+
if ( n < 0 ) {
48+
b.fail( 'should be nonnegative' );
49+
}
50+
}
51+
b.toc();
52+
if ( n < 0 ) {
53+
b.fail( 'should be nonnegative' );
54+
}
55+
b.pass( 'benchmark finished' );
56+
b.end();
57+
});

0 commit comments

Comments
 (0)