Skip to content

Commit 535ec3b

Browse files
authored
feat: add blas/ext/base/znancount
PR-URL: #11514 Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 342466e commit 535ec3b

33 files changed

Lines changed: 3587 additions & 0 deletions
Lines changed: 323 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,323 @@
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+
# znancount
22+
23+
> Calculate the number of non-`NaN` elements in a double-precision complex floating-point 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 znancount = require( '@stdlib/blas/ext/base/znancount' );
37+
```
38+
39+
#### znancount( N, x, strideX )
40+
41+
Computes the number of non-`NaN` elements in a double-precision complex floating-point strided array.
42+
43+
```javascript
44+
var Complex128Array = require( '@stdlib/array/complex128' );
45+
46+
var x = new Complex128Array( [ 1.0, -2.0, NaN, 2.0 ] );
47+
48+
var v = znancount( x.length, x, 1 );
49+
// returns 1
50+
```
51+
52+
The function has the following parameters:
53+
54+
- **N**: number of indexed elements.
55+
- **x**: input [`Complex128Array`][@stdlib/array/complex128].
56+
- **strideX**: stride length for `x`.
57+
58+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to count every other element in `x`,
59+
60+
```javascript
61+
var Complex128Array = require( '@stdlib/array/complex128' );
62+
63+
var x = new Complex128Array( [ 1.0, 2.0, NaN, -2.0, 4.0, 3.0, NaN, NaN ] );
64+
65+
var v = znancount( 2, x, 2 );
66+
// returns 2
67+
```
68+
69+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
70+
71+
<!-- eslint-disable stdlib/capitalized-comments -->
72+
73+
```javascript
74+
var Complex128Array = require( '@stdlib/array/complex128' );
75+
76+
var x0 = new Complex128Array( [ 2.0, 1.0, NaN, -2.0, 3.0, 4.0, NaN, NaN ] );
77+
var x1 = new Complex128Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
78+
79+
var v = znancount( 2, x1, 2 );
80+
// returns 0
81+
```
82+
83+
#### znancount.ndarray( N, x, strideX, offsetX )
84+
85+
Computes the number of non-`NaN` elements in a double-precision complex floating-point strided array using alternative indexing semantics.
86+
87+
```javascript
88+
var Complex128Array = require( '@stdlib/array/complex128' );
89+
90+
var x = new Complex128Array( [ 2.0, 1.0, -2.0, -2.0, 3.0, 4.0, NaN, NaN ] );
91+
92+
var v = znancount.ndarray( 4, x, 1, 0 );
93+
// returns 3
94+
```
95+
96+
The function has the following additional parameter:
97+
98+
- **offsetX**: starting index for `x`.
99+
100+
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 count every other element in `x` starting from the second element,
101+
102+
```javascript
103+
var Complex128Array = require( '@stdlib/array/complex128' );
104+
105+
var x = new Complex128Array( [ 2.0, 1.0, NaN, -2.0, 3.0, 4.0, NaN, NaN ] );
106+
107+
var v = znancount.ndarray( 2, x, 2, 1 );
108+
// returns 0
109+
```
110+
111+
</section>
112+
113+
<!-- /.usage -->
114+
115+
<section class="notes">
116+
117+
## Notes
118+
119+
- If `N <= 0`, both functions return `0`.
120+
- An element is considered `NaN` if either its real or imaginary component is `NaN`.
121+
122+
</section>
123+
124+
<!-- /.notes -->
125+
126+
<section class="examples">
127+
128+
## Examples
129+
130+
<!-- eslint no-undef: "error" -->
131+
132+
```javascript
133+
var uniform = require( '@stdlib/random/base/uniform' );
134+
var filledarrayBy = require( '@stdlib/array/filled-by' );
135+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
136+
var Complex128Array = require( '@stdlib/array/complex128' );
137+
var znancount = require( '@stdlib/blas/ext/base/znancount' );
138+
139+
function rand() {
140+
if ( bernoulli( 0.8 ) < 1 ) {
141+
return NaN;
142+
}
143+
return uniform( -50.0, 50.0 );
144+
}
145+
146+
var xbuf = filledarrayBy( 10, 'float64', rand );
147+
console.log( xbuf );
148+
149+
var x = new Complex128Array( xbuf );
150+
var v = znancount( x.length, x, 1 );
151+
console.log( v );
152+
```
153+
154+
</section>
155+
156+
<!-- /.examples -->
157+
158+
<!-- C interface documentation. -->
159+
160+
* * *
161+
162+
<section class="c">
163+
164+
## C APIs
165+
166+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
167+
168+
<section class="intro">
169+
170+
</section>
171+
172+
<!-- /.intro -->
173+
174+
<!-- C usage documentation. -->
175+
176+
<section class="usage">
177+
178+
### Usage
179+
180+
```c
181+
#include "stdlib/blas/ext/base/znancount.h"
182+
```
183+
184+
#### stdlib_strided_znancount( N, \*X, strideX )
185+
186+
Computes the number of non-`NaN` elements in a double-precision complex floating-point strided array.
187+
188+
```c
189+
#include "stdlib/complex/float64/ctor.h"
190+
191+
const stdlib_complex128_t x[] = {
192+
stdlib_complex128( 2.0, 1.0 ),
193+
stdlib_complex128( NaN, -2.0 ),
194+
stdlib_complex128( 3.0, 4.0 ),
195+
stdlib_complex128( NaN, NaN )
196+
};
197+
198+
int v = stdlib_strided_znancount( 4, x, 1 );
199+
// returns 2
200+
```
201+
202+
The function accepts the following arguments:
203+
204+
- **N**: `[in] CBLAS_INT` number of indexed elements.
205+
- **X**: `[in] stdlib_complex128_t*` input array.
206+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
207+
208+
```c
209+
CBLAS_INT stdlib_strided_znancount( const CBLAS_INT N, const stdlib_complex128_t *X, const CBLAS_INT strideX );
210+
```
211+
212+
#### stdlib_strided_znancount_ndarray( N, \*X, strideX, offsetX )
213+
214+
Computes the number of non-`NaN` elements in a double-precision complex floating-point strided array using alternative indexing semantics.
215+
216+
```c
217+
#include "stdlib/complex/float64/ctor.h"
218+
219+
const stdlib_complex128_t x[] = {
220+
stdlib_complex128( 2.0, 1.0 ),
221+
stdlib_complex128( NaN, -2.0 ),
222+
stdlib_complex128( 3.0, 4.0 ),
223+
stdlib_complex128( NaN, NaN )
224+
};
225+
226+
int v = stdlib_strided_znancount_ndarray( 4, x, 1, 0 );
227+
// returns 2
228+
```
229+
230+
The function accepts the following arguments:
231+
232+
- **N**: `[in] CBLAS_INT` number of indexed elements.
233+
- **X**: `[in] stdlib_complex128_t*` input array.
234+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
235+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
236+
237+
```c
238+
CBLAS_INT stdlib_strided_znancount_ndarray( const CBLAS_INT N, const stdlib_complex128_t *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
239+
```
240+
241+
</section>
242+
243+
<!-- /.usage -->
244+
245+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
246+
247+
<section class="notes">
248+
249+
### Notes
250+
251+
- If `N <= 0`, both functions return `0`.
252+
- An element is considered `NaN` if either its real or imaginary component is `NaN`.
253+
254+
</section>
255+
256+
<!-- /.notes -->
257+
258+
<!-- C API usage examples. -->
259+
260+
<section class="examples">
261+
262+
### Examples
263+
264+
```c
265+
#include "stdlib/blas/ext/base/znancount.h"
266+
#include "stdlib/complex/float64/ctor.h"
267+
#include <stdio.h>
268+
269+
int main( void ) {
270+
// Create a strided array:
271+
const stdlib_complex128_t x[] = {
272+
stdlib_complex128( 1.0, 2.0 ),
273+
stdlib_complex128( 3.0, 4.0 ),
274+
stdlib_complex128( 0.0/0.0, 5.0 ), // NaN
275+
stdlib_complex128( 6.0, 0.0/0.0 ), // NaN
276+
stdlib_complex128( 7.0, 8.0 )
277+
};
278+
279+
// Specify the number of elements:
280+
const int N = 5;
281+
282+
// Specify the stride length:
283+
const int strideX = 1;
284+
285+
// Compute the number of non-NaN elements:
286+
int v = stdlib_strided_znancount( N, x, strideX );
287+
288+
// Print the result:
289+
printf( "count: %d\n", v );
290+
}
291+
```
292+
293+
</section>
294+
295+
<!-- /.examples -->
296+
297+
</section>
298+
299+
<!-- /.c -->
300+
301+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
302+
303+
<section class="related">
304+
305+
</section>
306+
307+
<!-- /.related -->
308+
309+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
310+
311+
<section class="links">
312+
313+
[@stdlib/array/complex128]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex128
314+
315+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
316+
317+
<!-- <related-links> -->
318+
319+
<!-- </related-links> -->
320+
321+
</section>
322+
323+
<!-- /.links -->

0 commit comments

Comments
 (0)