Skip to content

Commit 0de464a

Browse files
committed
feat: add C implementation for blas/ext/base/ndarray/sindex-of-not-equal
1 parent 7166567 commit 0de464a

25 files changed

Lines changed: 2381 additions & 338 deletions

File tree

‎lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-not-equal/README.md‎

Lines changed: 193 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ var searchElement = scalar2ndarray( 1.0, {
5151
});
5252

5353
var fromIndex = scalar2ndarray( 0, {
54-
'dtype': 'generic'
54+
'dtype': 'int32'
5555
});
5656

5757
var idx = sindexOfNotEqual( [ x, searchElement, fromIndex ] );
@@ -79,7 +79,7 @@ var searchElement = scalar2ndarray( 1.0, {
7979
});
8080

8181
var fromIndex = scalar2ndarray( 0, {
82-
'dtype': 'generic'
82+
'dtype': 'int32'
8383
});
8484

8585
var idx = sindexOfNotEqual( [ x, searchElement, fromIndex ] );
@@ -125,7 +125,7 @@ var searchElement = scalar2ndarray( 0, opts );
125125
console.log( 'Search Element:', ndarraylike2scalar( searchElement ) );
126126

127127
var fromIndex = scalar2ndarray( 0, {
128-
'dtype': 'generic'
128+
'dtype': 'int32'
129129
});
130130
console.log( 'From Index:', ndarraylike2scalar( fromIndex ) );
131131

@@ -137,6 +137,196 @@ console.log( idx );
137137

138138
<!-- /.examples -->
139139

140+
<!-- C interface documentation. -->
141+
142+
* * *
143+
144+
<section class="c">
145+
146+
## C APIs
147+
148+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
149+
150+
<section class="intro">
151+
152+
</section>
153+
154+
<!-- /.intro -->
155+
156+
<!-- C usage documentation. -->
157+
158+
<section class="usage">
159+
160+
### Usage
161+
162+
```c
163+
#include "stdlib/blas/ext/base/ndarray/sindex_of_not_equal.h"
164+
```
165+
166+
#### stdlib_blas_ext_sindex_of_not_equal( arrays )
167+
168+
Returns the first index of an element in a one-dimensional single-precision floating-point ndarray which is not equal to a specified search element.
169+
170+
```c
171+
#include "stdlib/ndarray/ctor.h"
172+
#include "stdlib/ndarray/dtypes.h"
173+
#include "stdlib/ndarray/index_modes.h"
174+
#include "stdlib/ndarray/orders.h"
175+
#include "stdlib/ndarray/base/bytes_per_element.h"
176+
#include "stdlib/blas/base/shared.h"
177+
#include <stdint.h>
178+
179+
// Create an input ndarray:
180+
const float data[] = { 1.0f, 1.0f, 3.0f };
181+
int64_t shape[] = { 3 };
182+
int64_t strides[] = { STDLIB_NDARRAY_FLOAT32_BYTES_PER_ELEMENT };
183+
int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR };
184+
185+
struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)data, 1, shape, strides, 0, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 1, submodes );
186+
187+
// Create zero-dimensional ndarrays containing the search element and the index from which to begin searching:
188+
const float sdata[] = { 1.0f };
189+
const int32_t fdata[] = { 0 };
190+
int64_t sstrides[] = { 0 };
191+
192+
struct ndarray *searchElement = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)sdata, 0, NULL, sstrides, 0, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 1, submodes );
193+
struct ndarray *fromIndex = stdlib_ndarray_allocate( STDLIB_NDARRAY_INT32, (uint8_t *)fdata, 0, NULL, sstrides, 0, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 1, submodes );
194+
195+
// Resolve the index:
196+
const struct ndarray *arrays[] = { x, searchElement, fromIndex };
197+
CBLAS_INT idx = stdlib_blas_ext_sindex_of_not_equal( arrays );
198+
// returns 2
199+
200+
// Free allocated memory:
201+
stdlib_ndarray_free( x );
202+
stdlib_ndarray_free( searchElement );
203+
stdlib_ndarray_free( fromIndex );
204+
```
205+
206+
The function accepts the following arguments:
207+
208+
- **arrays**: `[in] struct ndarray**` list containing the following ndarrays:
209+
210+
- a one-dimensional input ndarray.
211+
- a zero-dimensional ndarray containing the search element.
212+
- a zero-dimensional ndarray containing the index from which to begin searching.
213+
214+
```c
215+
CBLAS_INT stdlib_blas_ext_sindex_of_not_equal( const struct ndarray *arrays[] );
216+
```
217+
218+
</section>
219+
220+
<!-- /.usage -->
221+
222+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
223+
224+
<section class="notes">
225+
226+
- The zero-dimensional ndarray containing the index from which to begin searching must have an `int32` data type.
227+
228+
</section>
229+
230+
<!-- /.notes -->
231+
232+
<!-- C API usage examples. -->
233+
234+
<section class="examples">
235+
236+
### Examples
237+
238+
```c
239+
#include "stdlib/blas/ext/base/ndarray/sindex_of_not_equal.h"
240+
#include "stdlib/ndarray/ctor.h"
241+
#include "stdlib/ndarray/dtypes.h"
242+
#include "stdlib/ndarray/index_modes.h"
243+
#include "stdlib/ndarray/orders.h"
244+
#include "stdlib/ndarray/base/bytes_per_element.h"
245+
#include "stdlib/blas/base/shared.h"
246+
#include <stdint.h>
247+
#include <stdlib.h>
248+
#include <stdio.h>
249+
250+
int main( void ) {
251+
// Create a data buffer:
252+
const float data[] = { 1.0f, -2.0f, 3.0f, -4.0f, 5.0f, -6.0f, 7.0f, -8.0f };
253+
254+
// Specify the number of array dimensions:
255+
const int64_t ndims = 1;
256+
257+
// Specify the array shape:
258+
int64_t shape[] = { 4 };
259+
260+
// Specify the array strides:
261+
int64_t strides[] = { 2*STDLIB_NDARRAY_FLOAT32_BYTES_PER_ELEMENT };
262+
263+
// Specify the byte offset:
264+
const int64_t offset = 0;
265+
266+
// Specify the array order:
267+
const enum STDLIB_NDARRAY_ORDER order = STDLIB_NDARRAY_ROW_MAJOR;
268+
269+
// Specify the index mode:
270+
const enum STDLIB_NDARRAY_INDEX_MODE imode = STDLIB_NDARRAY_INDEX_ERROR;
271+
272+
// Specify the subscript index modes:
273+
int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR };
274+
const int64_t nsubmodes = 1;
275+
276+
// Create an input ndarray:
277+
struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)data, ndims, shape, strides, offset, order, imode, nsubmodes, submodes );
278+
if ( x == NULL ) {
279+
fprintf( stderr, "Error allocating memory.\n" );
280+
exit( 1 );
281+
}
282+
283+
// Specify the array strides for zero-dimensional ndarrays:
284+
int64_t sstrides[] = { 0 };
285+
286+
// Create a data buffer for an ndarray containing the search element:
287+
const float sdata[] = { 1.0f };
288+
289+
// Create a zero-dimensional ndarray containing the search element:
290+
struct ndarray *searchElement = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT32, (uint8_t *)sdata, 0, NULL, sstrides, 0, order, imode, nsubmodes, submodes );
291+
if ( searchElement == NULL ) {
292+
fprintf( stderr, "Error allocating memory.\n" );
293+
exit( 1 );
294+
}
295+
296+
// Create a data buffer for an ndarray containing the index from which to begin searching:
297+
const int32_t fdata[] = { 0 };
298+
299+
// Create a zero-dimensional ndarray containing the index from which to begin searching:
300+
struct ndarray *fromIndex = stdlib_ndarray_allocate( STDLIB_NDARRAY_INT32, (uint8_t *)fdata, 0, NULL, sstrides, 0, order, imode, nsubmodes, submodes );
301+
if ( fromIndex == NULL ) {
302+
fprintf( stderr, "Error allocating memory.\n" );
303+
exit( 1 );
304+
}
305+
306+
// Define a list of ndarrays:
307+
const struct ndarray *arrays[] = { x, searchElement, fromIndex };
308+
309+
// Resolve the index:
310+
CBLAS_INT idx = stdlib_blas_ext_sindex_of_not_equal( arrays );
311+
312+
// Print the result:
313+
printf( "index: %i\n", (int)idx );
314+
315+
// Free allocated memory:
316+
stdlib_ndarray_free( x );
317+
stdlib_ndarray_free( searchElement );
318+
stdlib_ndarray_free( fromIndex );
319+
}
320+
```
321+
322+
</section>
323+
324+
<!-- /.examples -->
325+
326+
</section>
327+
328+
<!-- /.c -->
329+
140330
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
141331
142332
<section class="related">

‎lib/node_modules/@stdlib/blas/ext/base/ndarray/sindex-of-not-equal/benchmark/benchmark.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var pow = require( '@stdlib/math/base/special/pow' );
2727
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
2828
var format = require( '@stdlib/string/format' );
2929
var pkg = require( './../package.json' ).name;
30-
var sindexOfNotEqual = require( './../lib' );
30+
var sindexOfNotEqual = require( './../lib/main.js' );
3131

3232

3333
// VARIABLES //
@@ -54,7 +54,7 @@ function createBenchmark( len ) {
5454
x = zeros( [ len ], options );
5555
searchElement = scalar2ndarray( 0.0, options );
5656
fromIndex = scalar2ndarray( 0, {
57-
'dtype': 'generic'
57+
'dtype': 'int32'
5858
});
5959
return benchmark;
6060

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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 resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var zeros = require( '@stdlib/ndarray/zeros' );
26+
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
27+
var pow = require( '@stdlib/math/base/special/pow' );
28+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
29+
var format = require( '@stdlib/string/format' );
30+
var tryRequire = require( '@stdlib/utils/try-require' );
31+
var pkg = require( './../package.json' ).name;
32+
33+
34+
// VARIABLES //
35+
36+
var sindexOfNotEqual = tryRequire( resolve( __dirname, './../lib/native.js' ) );
37+
var opts = {
38+
'skip': ( sindexOfNotEqual instanceof Error )
39+
};
40+
var options = {
41+
'dtype': 'float32'
42+
};
43+
44+
45+
// FUNCTIONS //
46+
47+
/**
48+
* Creates a benchmark function.
49+
*
50+
* @private
51+
* @param {PositiveInteger} len - array length
52+
* @returns {Function} benchmark function
53+
*/
54+
function createBenchmark( len ) {
55+
var searchElement;
56+
var fromIndex;
57+
var x;
58+
59+
x = zeros( [ len ], options );
60+
searchElement = scalar2ndarray( 0.0, options );
61+
fromIndex = scalar2ndarray( 0, {
62+
'dtype': 'int32'
63+
});
64+
return benchmark;
65+
66+
/**
67+
* Benchmark function.
68+
*
69+
* @private
70+
* @param {Benchmark} b - benchmark instance
71+
*/
72+
function benchmark( b ) {
73+
var out;
74+
var i;
75+
76+
b.tic();
77+
for ( i = 0; i < b.iterations; i++ ) {
78+
out = sindexOfNotEqual( [ x, searchElement, fromIndex ] );
79+
if ( out !== out ) {
80+
b.fail( 'should return an integer' );
81+
}
82+
}
83+
b.toc();
84+
if ( !isInteger( out ) ) {
85+
b.fail( 'should return an integer' );
86+
}
87+
b.pass( 'benchmark finished' );
88+
b.end();
89+
}
90+
}
91+
92+
93+
// MAIN //
94+
95+
/**
96+
* Main execution sequence.
97+
*
98+
* @private
99+
*/
100+
function main() {
101+
var len;
102+
var min;
103+
var max;
104+
var f;
105+
var i;
106+
107+
min = 1; // 10^min
108+
max = 6; // 10^max
109+
110+
for ( i = min; i <= max; i++ ) {
111+
len = pow( 10, i );
112+
f = createBenchmark( len );
113+
bench( format( '%s::native:len=%d', pkg, len ), opts, f );
114+
}
115+
}
116+
117+
main();

0 commit comments

Comments
 (0)