Skip to content

Commit c84f0ca

Browse files
committed
feat: add blas/ext/base/gnancount
1 parent c578a85 commit c84f0ca

15 files changed

Lines changed: 1667 additions & 0 deletions

File tree

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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+
# gnancount
22+
23+
> Calculate the number of non-`NaN` elements in a strided array.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var gnancount = require( '@stdlib/blas/ext/base/gnancount' );
37+
```
38+
39+
#### gnancount( N, x, strideX )
40+
41+
Calculates the number of non-`NaN` elements in a strided array.
42+
43+
```javascript
44+
var x = [ 1.0, -2.0, NaN, 2.0 ];
45+
46+
var v = gnancount( x.length, x, 1 );
47+
// returns 3
48+
```
49+
50+
The function has the following parameters:
51+
52+
- **N**: number of indexed elements.
53+
- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
54+
- **strideX**: stride length for `x`.
55+
56+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to calculate the count for every other element in `x`,
57+
58+
```javascript
59+
var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0, NaN, NaN ];
60+
61+
var v = gnancount( 5, x, 2 );
62+
// returns 4
63+
```
64+
65+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
66+
67+
<!-- eslint-disable stdlib/capitalized-comments -->
68+
69+
```javascript
70+
var Float64Array = require( '@stdlib/array/float64' );
71+
72+
var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, NaN, NaN, 4.0 ] );
73+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
74+
75+
var v = gnancount( 4, x1, 2 );
76+
// returns 3
77+
```
78+
79+
#### gnancount.ndarray( N, x, strideX, offsetX )
80+
81+
Calculates the number of non-`NaN` elements in a strided array using alternative indexing semantics.
82+
83+
```javascript
84+
var x = [ 1.0, -2.0, NaN, 2.0 ];
85+
86+
var v = gnancount.ndarray( x.length, x, 1, 0 );
87+
// returns 3
88+
```
89+
90+
The function has the following additional parameters:
91+
92+
- **offsetX**: starting index for `x`.
93+
94+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to calculate the count for every other element in `x` starting from the second element
95+
96+
```javascript
97+
var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, NaN, NaN, 2.0, 3.0, 4.0 ];
98+
99+
var v = gnancount.ndarray( 5, x, 2, 1 );
100+
// returns 4
101+
```
102+
103+
</section>
104+
105+
<!-- /.usage -->
106+
107+
<section class="notes">
108+
109+
## Notes
110+
111+
- If `N <= 0`, both functions return `0`.
112+
- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).
113+
114+
</section>
115+
116+
<!-- /.notes -->
117+
118+
<section class="examples">
119+
120+
## Examples
121+
122+
<!-- eslint no-undef: "error" -->
123+
124+
```javascript
125+
var uniform = require( '@stdlib/random/base/uniform' );
126+
var filledarrayBy = require( '@stdlib/array/filled-by' );
127+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
128+
var gnancount = require( '@stdlib/blas/ext/base/gnancount' );
129+
130+
function rand() {
131+
if ( bernoulli( 0.8 ) < 1 ) {
132+
return NaN;
133+
}
134+
return uniform( -50.0, 50.0 );
135+
}
136+
137+
var x = filledarrayBy( 10, 'float64', rand );
138+
console.log( x );
139+
140+
var v = gnancount( x.length, x, 1 );
141+
console.log( v );
142+
```
143+
144+
</section>
145+
146+
<!-- /.examples -->
147+
148+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
149+
150+
<section class="related">
151+
152+
</section>
153+
154+
<!-- /.related -->
155+
156+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
157+
158+
<section class="links">
159+
160+
[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
161+
162+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
163+
164+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
165+
166+
<!-- <related-links> -->
167+
168+
169+
<!-- </related-links> -->
170+
171+
</section>
172+
173+
<!-- /.links -->
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var uniform = require( '@stdlib/random/base/uniform' );
26+
var format = require( '@stdlib/string/format' );
27+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
28+
var filledarrayBy = require( '@stdlib/array/filled-by' );
29+
var pow = require( '@stdlib/math/base/special/pow' );
30+
var pkg = require( './../package.json' ).name;
31+
var gnancount = require( './../lib/main.js' );
32+
33+
34+
// FUNCTIONS //
35+
36+
/**
37+
* Returns a random number.
38+
*
39+
* @private
40+
* @returns {number} random number
41+
*/
42+
function rand() {
43+
if ( bernoulli( 0.8 ) < 1 ) {
44+
return NaN;
45+
}
46+
return uniform( -10.0, 10.0 );
47+
}
48+
49+
/**
50+
* Creates a benchmark function.
51+
*
52+
* @private
53+
* @param {PositiveInteger} len - array length
54+
* @returns {Function} benchmark function
55+
*/
56+
function createBenchmark( len ) {
57+
var x = filledarrayBy( len, 'generic', rand );
58+
return benchmark;
59+
60+
/**
61+
* Benchmark function.
62+
*
63+
* @private
64+
* @param {Benchmark} b - benchmark instance
65+
*/
66+
function benchmark( b ) {
67+
var v;
68+
var i;
69+
70+
b.tic();
71+
for ( i = 0; i < b.iterations; i++ ) {
72+
v = gnancount( x.length, x, 1 );
73+
if ( isnan( v ) ) {
74+
b.fail( 'should not return NaN' );
75+
}
76+
}
77+
b.toc();
78+
if ( isnan( v ) ) {
79+
b.fail( 'should not return NaN' );
80+
}
81+
b.pass( 'benchmark finished' );
82+
b.end();
83+
}
84+
}
85+
86+
87+
// MAIN //
88+
89+
/**
90+
* Main execution sequence.
91+
*
92+
* @private
93+
*/
94+
function main() {
95+
var len;
96+
var min;
97+
var max;
98+
var f;
99+
var i;
100+
101+
min = 1; // 10^min
102+
max = 6; // 10^max
103+
104+
for ( i = min; i <= max; i++ ) {
105+
len = pow( 10, i );
106+
f = createBenchmark( len );
107+
bench( format( '%s:len=%d', pkg, len ), f );
108+
}
109+
}
110+
111+
main();
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var uniform = require( '@stdlib/random/base/uniform' );
26+
var format = require( '@stdlib/string/format' );
27+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
28+
var filledarrayBy = require( '@stdlib/array/filled-by' );
29+
var pow = require( '@stdlib/math/base/special/pow' );
30+
var pkg = require( './../package.json' ).name;
31+
var gnancount = require( './../lib/ndarray.js' );
32+
33+
34+
// FUNCTIONS //
35+
36+
/**
37+
* Returns a random number.
38+
*
39+
* @private
40+
* @returns {number} random number
41+
*/
42+
function rand() {
43+
if ( bernoulli( 0.8 ) < 1 ) {
44+
return NaN;
45+
}
46+
return uniform( -10.0, 10.0 );
47+
}
48+
49+
/**
50+
* Creates a benchmark function.
51+
*
52+
* @private
53+
* @param {PositiveInteger} len - array length
54+
* @returns {Function} benchmark function
55+
*/
56+
function createBenchmark( len ) {
57+
var x = filledarrayBy( len, 'generic', rand );
58+
return benchmark;
59+
60+
/**
61+
* Benchmark function.
62+
*
63+
* @private
64+
* @param {Benchmark} b - benchmark instance
65+
*/
66+
function benchmark( b ) {
67+
var v;
68+
var i;
69+
70+
b.tic();
71+
for ( i = 0; i < b.iterations; i++ ) {
72+
v = gnancount( x.length, x, 1, 0 );
73+
if ( isnan( v ) ) {
74+
b.fail( 'should not return NaN' );
75+
}
76+
}
77+
b.toc();
78+
if ( isnan( v ) ) {
79+
b.fail( 'should not return NaN' );
80+
}
81+
b.pass( 'benchmark finished' );
82+
b.end();
83+
}
84+
}
85+
86+
87+
// MAIN //
88+
89+
/**
90+
* Main execution sequence.
91+
*
92+
* @private
93+
*/
94+
function main() {
95+
var len;
96+
var min;
97+
var max;
98+
var f;
99+
var i;
100+
101+
min = 1; // 10^min
102+
max = 6; // 10^max
103+
104+
for ( i = min; i <= max; i++ ) {
105+
len = pow( 10, i );
106+
f = createBenchmark( len );
107+
bench( format( '%s:ndarray:len=%d', pkg, len ), f );
108+
}
109+
}
110+
111+
main();

0 commit comments

Comments
 (0)