Skip to content

Commit 9b655da

Browse files
authored
feat: add blas/ext/base/snone
PR-URL: #13011 Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#813
1 parent 8f249bb commit 9b655da

33 files changed

Lines changed: 3390 additions & 0 deletions
Lines changed: 289 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,289 @@
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+
# snone
22+
23+
> Test whether every element in a single-precision floating-point strided array is falsy.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var snone = require( '@stdlib/blas/ext/base/snone' );
37+
```
38+
39+
#### snone( N, x, strideX )
40+
41+
Tests whether every element in a single-precision floating-point strided array is falsy.
42+
43+
```javascript
44+
var Float32Array = require( '@stdlib/array/float32' );
45+
46+
var x = new Float32Array( [ 0.0, 0.0, 1.0, 1.0 ] );
47+
48+
var v = snone( x.length, x, 1 );
49+
// returns false
50+
```
51+
52+
The function has the following parameters:
53+
54+
- **N**: number of indexed elements.
55+
- **x**: input [`Float32Array`][@stdlib/array/float32].
56+
- **strideX**: stride length.
57+
58+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to test every other element:
59+
60+
```javascript
61+
var Float32Array = require( '@stdlib/array/float32' );
62+
63+
var x = new Float32Array( [ 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 ] );
64+
65+
var v = snone( 3, x, 2 );
66+
// returns false
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 Float32Array = require( '@stdlib/array/float32' );
75+
76+
var x0 = new Float32Array( [ 0.0, 0.0, 1.0, 0.0, 0.0, 1.0 ] );
77+
var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
78+
79+
var v = snone( 3, x1, 2 );
80+
// returns false
81+
```
82+
83+
#### snone.ndarray( N, x, strideX, offsetX )
84+
85+
Tests whether every element in a single-precision floating-point strided array is falsy using alternative indexing semantics.
86+
87+
```javascript
88+
var Float32Array = require( '@stdlib/array/float32' );
89+
90+
var x = new Float32Array( [ 0.0, 0.0, 1.0, 1.0 ] );
91+
92+
var v = snone.ndarray( x.length, x, 1, 0 );
93+
// returns false
94+
```
95+
96+
The function has the following additional parameters:
97+
98+
- **offsetX**: starting index.
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 test every other element starting from the second element:
101+
102+
```javascript
103+
var Float32Array = require( '@stdlib/array/float32' );
104+
105+
var x = new Float32Array( [ 0.0, 0.0, 1.0, 0.0, 0.0, 1.0 ] );
106+
107+
var v = snone.ndarray( 3, x, 2, 1 );
108+
// returns false
109+
```
110+
111+
</section>
112+
113+
<!-- /.usage -->
114+
115+
<section class="notes">
116+
117+
## Notes
118+
119+
- If `N <= 0`, both functions return `true`.
120+
- Both functions explicitly treat `NaN` values as falsy.
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 bernoulli = require( '@stdlib/random/array/bernoulli' );
134+
var snone = require( '@stdlib/blas/ext/base/snone' );
135+
136+
var x = bernoulli( 10, 0.5, {
137+
'dtype': 'float32'
138+
});
139+
console.log( x );
140+
141+
var out = snone( x.length, x, 1 );
142+
console.log( out );
143+
```
144+
145+
</section>
146+
147+
<!-- /.examples -->
148+
149+
<!-- C interface documentation. -->
150+
151+
* * *
152+
153+
<section class="c">
154+
155+
## C APIs
156+
157+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
158+
159+
<section class="intro">
160+
161+
</section>
162+
163+
<!-- /.intro -->
164+
165+
<!-- C usage documentation. -->
166+
167+
<section class="usage">
168+
169+
### Usage
170+
171+
```c
172+
#include "stdlib/blas/ext/base/snone.h"
173+
```
174+
175+
#### stdlib_strided_snone( N, \*X, strideX )
176+
177+
Tests whether every element in a single-precision floating-point strided array is falsy.
178+
179+
```c
180+
const float x[] = { 0.0f, 0.0f, 1.0f, 1.0f };
181+
182+
bool result = stdlib_strided_snone( 4, x, 1 );
183+
```
184+
185+
The function accepts the following arguments:
186+
187+
- **N**: `[in] CBLAS_INT` number of indexed elements.
188+
- **X**: `[in] float*` input array.
189+
- **strideX**: `[in] CBLAS_INT` stride length.
190+
191+
```c
192+
bool stdlib_strided_snone( const CBLAS_INT N, const float *X, const CBLAS_INT strideX );
193+
```
194+
195+
#### stdlib_strided_snone_ndarray( N, \*X, strideX, offsetX )
196+
197+
Tests whether every element in a single-precision floating-point strided array is falsy using alternative indexing semantics.
198+
199+
```c
200+
const float x[] = { 0.0f, 0.0f, 1.0f, 1.0f };
201+
202+
bool result = stdlib_strided_snone_ndarray( 4, x, 1, 0 );
203+
```
204+
205+
The function accepts the following arguments:
206+
207+
- **N**: `[in] CBLAS_INT` number of indexed elements.
208+
- **X**: `[in] float*` input array.
209+
- **strideX**: `[in] CBLAS_INT` stride length.
210+
- **offsetX**: `[in] CBLAS_INT` starting index.
211+
212+
```c
213+
bool stdlib_strided_snone_ndarray( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
214+
```
215+
216+
</section>
217+
218+
<!-- /.usage -->
219+
220+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
221+
222+
<section class="notes">
223+
224+
- Both functions explicitly treat `NaN` values as falsy.
225+
226+
</section>
227+
228+
<!-- /.notes -->
229+
230+
<!-- C API usage examples. -->
231+
232+
<section class="examples">
233+
234+
### Examples
235+
236+
```c
237+
#include "stdlib/blas/ext/base/snone.h"
238+
#include <stdbool.h>
239+
#include <stdio.h>
240+
241+
int main( void ) {
242+
// Create a strided array:
243+
const float x[] = { 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f };
244+
245+
// Specify the number of indexed elements:
246+
const int N = 8;
247+
248+
// Specify a stride:
249+
const int strideX = 1;
250+
251+
// Test whether every element is falsy:
252+
bool result = stdlib_strided_snone( N, x, strideX );
253+
254+
// Print the result:
255+
printf( "Result: %s\n", result ? "true" : "false" );
256+
}
257+
```
258+
259+
</section>
260+
261+
<!-- /.examples -->
262+
263+
</section>
264+
265+
<!-- /.c -->
266+
267+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
268+
269+
<section class="related">
270+
271+
</section>
272+
273+
<!-- /.related -->
274+
275+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
276+
277+
<section class="links">
278+
279+
[@stdlib/array/float32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float32
280+
281+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
282+
283+
<!-- <related-links> -->
284+
285+
<!-- </related-links> -->
286+
287+
</section>
288+
289+
<!-- /.links -->
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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 zeros = require( '@stdlib/array/zeros' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var format = require( '@stdlib/string/format' );
27+
var pkg = require( './../package.json' ).name;
28+
var snone = require( './../lib/snone.js' );
29+
30+
31+
// VARIABLES //
32+
33+
var options = {
34+
'dtype': 'float32'
35+
};
36+
37+
38+
// FUNCTIONS //
39+
40+
/**
41+
* Create a benchmark function.
42+
*
43+
* @private
44+
* @param {PositiveInteger} len - array length
45+
* @returns {Function} benchmark function
46+
*/
47+
function createBenchmark( len ) {
48+
var x = zeros( len, options.dtype );
49+
return benchmark;
50+
51+
/**
52+
* Benchmark function.
53+
*
54+
* @private
55+
* @param {Benchmark} b - benchmark instance
56+
*/
57+
function benchmark( b ) {
58+
var out;
59+
var i;
60+
61+
b.tic();
62+
for ( i = 0; i < b.iterations; i++ ) {
63+
out = snone( len, x, 1 );
64+
if ( typeof out !== 'boolean' ) {
65+
b.fail( 'should return a boolean' );
66+
}
67+
}
68+
b.toc();
69+
if ( typeof out !== 'boolean' ) {
70+
b.fail( 'should return a boolean' );
71+
}
72+
b.pass( 'benchmark finished' );
73+
b.end();
74+
}
75+
}
76+
77+
78+
// MAIN //
79+
80+
/**
81+
* Main execution sequence.
82+
*
83+
* @private
84+
*/
85+
function main() {
86+
var len;
87+
var min;
88+
var max;
89+
var f;
90+
var i;
91+
92+
min = 1; // 10^min
93+
max = 6; // 10^max
94+
95+
for ( i = min; i <= max; i++ ) {
96+
len = pow( 10, i );
97+
f = createBenchmark( len );
98+
bench( format( '%s:len=%d', pkg, len ), f );
99+
}
100+
}
101+
102+
main();

0 commit comments

Comments
 (0)