Skip to content

Commit f29b107

Browse files
committed
feat: add C implementation for blas/ext/base/ndarray/dwhere
1 parent 7166567 commit f29b107

20 files changed

Lines changed: 2526 additions & 366 deletions

File tree

‎lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/README.md‎

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,182 @@ console.log( ndarray2array( out ) );
111111

112112
<!-- /.examples -->
113113

114+
<!-- C interface documentation. -->
115+
116+
* * *
117+
118+
<section class="c">
119+
120+
## C APIs
121+
122+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
123+
124+
<section class="intro">
125+
126+
</section>
127+
128+
<!-- /.intro -->
129+
130+
<!-- C usage documentation. -->
131+
132+
<section class="usage">
133+
134+
### Usage
135+
136+
```c
137+
#include "stdlib/blas/ext/base/ndarray/dwhere.h"
138+
```
139+
140+
#### stdlib_blas_ext_dwhere( arrays )
141+
142+
Takes elements from one of two one-dimensional double-precision floating-point ndarrays depending on a condition.
143+
144+
```c
145+
#include "stdlib/ndarray/ctor.h"
146+
#include "stdlib/ndarray/dtypes.h"
147+
#include "stdlib/ndarray/index_modes.h"
148+
#include "stdlib/ndarray/orders.h"
149+
#include "stdlib/ndarray/base/bytes_per_element.h"
150+
#include <stdint.h>
151+
#include <stdbool.h>
152+
153+
// Create data buffers:
154+
const uint8_t dataC[] = { 1, 0, 1, 0 };
155+
const double dataX[] = { 1.0, 2.0, 3.0, 4.0 };
156+
const double dataY[] = { 5.0, 6.0, 7.0, 8.0 };
157+
double dataOut[] = { 0.0, 0.0, 0.0, 0.0 };
158+
159+
int64_t shape[] = { 4 };
160+
int64_t stridesC[] = { STDLIB_NDARRAY_BOOL_BYTES_PER_ELEMENT };
161+
int64_t strides[] = { STDLIB_NDARRAY_FLOAT64_BYTES_PER_ELEMENT };
162+
int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR };
163+
164+
struct ndarray *c = stdlib_ndarray_allocate( STDLIB_NDARRAY_BOOL, (uint8_t *)dataC, 1, shape, stridesC, 0, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 1, submodes );
165+
struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT64, (uint8_t *)dataX, 1, shape, strides, 0, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 1, submodes );
166+
struct ndarray *y = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT64, (uint8_t *)dataY, 1, shape, strides, 0, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 1, submodes );
167+
struct ndarray *out = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT64, (uint8_t *)dataOut, 1, shape, strides, 0, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 1, submodes );
168+
169+
// Perform computation:
170+
const struct ndarray *arrays[] = { c, x, y, out };
171+
stdlib_blas_ext_dwhere( arrays );
172+
173+
// Free allocated memory:
174+
stdlib_ndarray_free( c );
175+
stdlib_ndarray_free( x );
176+
stdlib_ndarray_free( y );
177+
stdlib_ndarray_free( out );
178+
```
179+
180+
The function accepts the following arguments:
181+
182+
- **arrays**: `[in] struct ndarray**` list containing the following ndarrays:
183+
184+
- `[in] struct ndarray*` a one-dimensional condition ndarray.
185+
- `[in] struct ndarray*` first one-dimensional input ndarray.
186+
- `[in] struct ndarray*` second one-dimensional input ndarray.
187+
- `[out] struct ndarray*` a one-dimensional output ndarray.
188+
189+
```c
190+
void stdlib_blas_ext_dwhere( const struct ndarray *arrays[] );
191+
```
192+
193+
</section>
194+
195+
<!-- /.usage -->
196+
197+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
198+
199+
<section class="notes">
200+
201+
</section>
202+
203+
<!-- /.notes -->
204+
205+
<!-- C API usage examples. -->
206+
207+
<section class="examples">
208+
209+
### Examples
210+
211+
```c
212+
#include "stdlib/blas/ext/base/ndarray/dwhere.h"
213+
#include "stdlib/ndarray/ctor.h"
214+
#include "stdlib/ndarray/dtypes.h"
215+
#include "stdlib/ndarray/index_modes.h"
216+
#include "stdlib/ndarray/orders.h"
217+
#include "stdlib/ndarray/base/bytes_per_element.h"
218+
#include <stdint.h>
219+
#include <stdbool.h>
220+
#include <stdlib.h>
221+
#include <stdio.h>
222+
223+
int main( void ) {
224+
// Create data buffers:
225+
const uint8_t dataC[] = { 1, 0, 1, 0 };
226+
const double dataX[] = { 1.0, 2.0, 3.0, 4.0 };
227+
const double dataY[] = { 5.0, 6.0, 7.0, 8.0 };
228+
double dataOut[] = { 0.0, 0.0, 0.0, 0.0 };
229+
230+
// Specify the number of array dimensions:
231+
const int64_t ndims = 1;
232+
233+
// Specify the array shape:
234+
int64_t shape[] = { 4 };
235+
236+
// Specify the array strides:
237+
int64_t stridesC[] = { STDLIB_NDARRAY_BOOL_BYTES_PER_ELEMENT };
238+
int64_t strides[] = { STDLIB_NDARRAY_FLOAT64_BYTES_PER_ELEMENT };
239+
240+
// Specify the byte offset:
241+
const int64_t offset = 0;
242+
243+
// Specify the array order:
244+
const enum STDLIB_NDARRAY_ORDER order = STDLIB_NDARRAY_ROW_MAJOR;
245+
246+
// Specify the index mode:
247+
const enum STDLIB_NDARRAY_INDEX_MODE imode = STDLIB_NDARRAY_INDEX_ERROR;
248+
249+
// Specify the subscript index modes:
250+
int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR };
251+
const int64_t nsubmodes = 1;
252+
253+
// Create ndarrays:
254+
struct ndarray *c = stdlib_ndarray_allocate( STDLIB_NDARRAY_BOOL, (uint8_t *)dataC, ndims, shape, stridesC, offset, order, imode, nsubmodes, submodes );
255+
struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT64, (uint8_t *)dataX, ndims, shape, strides, offset, order, imode, nsubmodes, submodes );
256+
struct ndarray *y = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT64, (uint8_t *)dataY, ndims, shape, strides, offset, order, imode, nsubmodes, submodes );
257+
struct ndarray *out = stdlib_ndarray_allocate( STDLIB_NDARRAY_FLOAT64, (uint8_t *)dataOut, ndims, shape, strides, offset, order, imode, nsubmodes, submodes );
258+
if ( c == NULL || x == NULL || y == NULL || out == NULL ) {
259+
fprintf( stderr, "Error allocating memory.\n" );
260+
exit( 1 );
261+
}
262+
263+
// Define a list of ndarrays:
264+
const struct ndarray *arrays[] = { c, x, y, out };
265+
266+
// Perform computation:
267+
stdlib_blas_ext_dwhere( arrays );
268+
269+
// Print the result:
270+
for ( int i = 0; i < 4; i++ ) {
271+
printf( "out[ %i ] = %lf\n", i, dataOut[ i ] );
272+
}
273+
274+
// Free allocated memory:
275+
stdlib_ndarray_free( c );
276+
stdlib_ndarray_free( x );
277+
stdlib_ndarray_free( y );
278+
stdlib_ndarray_free( out );
279+
}
280+
```
281+
282+
</section>
283+
284+
<!-- /.examples -->
285+
286+
</section>
287+
288+
<!-- /.c -->
289+
114290
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
115291
116292
<section class="related">

‎lib/node_modules/@stdlib/blas/ext/base/ndarray/dwhere/benchmark/benchmark.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var BooleanVector = require( '@stdlib/ndarray/vector/bool' );
2727
var pow = require( '@stdlib/math/base/special/pow' );
2828
var format = require( '@stdlib/string/format' );
2929
var pkg = require( './../package.json' ).name;
30-
var dwhere = require( './../lib' );
30+
var dwhere = require( './../lib/main.js' );
3131

3232

3333
// VARIABLES //
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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 bernoulli = require( '@stdlib/random/array/bernoulli' );
26+
var uniform = require( '@stdlib/random/uniform' );
27+
var BooleanVector = require( '@stdlib/ndarray/vector/bool' );
28+
var pow = require( '@stdlib/math/base/special/pow' );
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 dwhere = tryRequire( resolve( __dirname, './../lib/native.js' ) );
37+
var opts = {
38+
'skip': ( dwhere instanceof Error )
39+
};
40+
var options = {
41+
'dtype': 'float64'
42+
};
43+
44+
45+
// FUNCTIONS //
46+
47+
/**
48+
* Creates a benchmark function.
49+
*
50+
* @private
51+
* @param {PositiveInteger} len - ndarray length
52+
* @returns {Function} benchmark function
53+
*/
54+
function createBenchmark( len ) {
55+
var condition;
56+
var cbuf;
57+
var out;
58+
var x;
59+
var y;
60+
61+
cbuf = bernoulli( len, 0.5, {
62+
'dtype': 'uint8'
63+
});
64+
condition = new BooleanVector( cbuf.buffer );
65+
out = uniform( [ len ], -100.0, 100.0, options );
66+
x = uniform( [ len ], -100.0, 100.0, options );
67+
y = uniform( [ len ], -100.0, 100.0, options );
68+
return benchmark;
69+
70+
/**
71+
* Benchmark function.
72+
*
73+
* @private
74+
* @param {Benchmark} b - benchmark instance
75+
*/
76+
function benchmark( b ) {
77+
var v;
78+
var i;
79+
80+
b.tic();
81+
for ( i = 0; i < b.iterations; i++ ) {
82+
v = dwhere( [ condition, x, y, out ] );
83+
if ( typeof v !== 'object' ) {
84+
b.fail( 'should return an ndarray' );
85+
}
86+
}
87+
b.toc();
88+
if ( typeof v !== 'object' ) {
89+
b.fail( 'should return an ndarray' );
90+
}
91+
b.pass( 'benchmark finished' );
92+
b.end();
93+
}
94+
}
95+
96+
97+
// MAIN //
98+
99+
/**
100+
* Main execution sequence.
101+
*
102+
* @private
103+
*/
104+
function main() {
105+
var len;
106+
var min;
107+
var max;
108+
var f;
109+
var i;
110+
111+
min = 1; // 10^min
112+
max = 6; // 10^max
113+
114+
for ( i = min; i <= max; i++ ) {
115+
len = pow( 10, i );
116+
f = createBenchmark( len );
117+
bench( format( '%s::native:len=%d', pkg, len ), opts, f );
118+
}
119+
}
120+
121+
main();

0 commit comments

Comments
 (0)