Skip to content

Commit a24cbaf

Browse files
committed
feat: add C implementation for blas/ext/base/ndarray/zsum
1 parent 675c404 commit a24cbaf

20 files changed

Lines changed: 1897 additions & 154 deletions

File tree

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

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,157 @@ console.log( v );
9898

9999
<!-- /.examples -->
100100

101+
<!-- C interface documentation. -->
102+
103+
* * *
104+
105+
<section class="c">
106+
107+
## C APIs
108+
109+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
110+
111+
<section class="intro">
112+
113+
</section>
114+
115+
<!-- /.intro -->
116+
117+
<!-- C usage documentation. -->
118+
119+
<section class="usage">
120+
121+
### Usage
122+
123+
```c
124+
#include "stdlib/blas/ext/base/ndarray/zsum.h"
125+
```
126+
127+
#### stdlib_blas_ext_zsum( arrays )
128+
129+
Computes the sum of all elements in a one-dimensional double-precision complex floating-point ndarray.
130+
131+
```c
132+
#include "stdlib/complex/float64/ctor.h"
133+
#include "stdlib/ndarray/ctor.h"
134+
#include "stdlib/ndarray/dtypes.h"
135+
#include "stdlib/ndarray/index_modes.h"
136+
#include "stdlib/ndarray/orders.h"
137+
#include "stdlib/ndarray/base/bytes_per_element.h"
138+
#include <stdint.h>
139+
140+
// Create an ndarray:
141+
const double data[] = { 1.0, 2.0, 3.0, 4.0 };
142+
int64_t shape[] = { 2 };
143+
int64_t strides[] = { STDLIB_NDARRAY_COMPLEX128_BYTES_PER_ELEMENT };
144+
int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR };
145+
146+
struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_COMPLEX128, (uint8_t *)data, 1, shape, strides, 0, STDLIB_NDARRAY_ROW_MAJOR, STDLIB_NDARRAY_INDEX_ERROR, 1, submodes );
147+
148+
// Compute the sum:
149+
const struct ndarray *arrays[] = { x };
150+
stdlib_complex128_t v = stdlib_blas_ext_zsum( arrays );
151+
// returns <stdlib_complex128_t>[ 4.0, 6.0 ]
152+
153+
// Free allocated memory:
154+
stdlib_ndarray_free( x );
155+
```
156+
157+
The function accepts the following arguments:
158+
159+
- **arrays**: `[in] struct ndarray**` list containing a one-dimensional input ndarray.
160+
161+
```c
162+
stdlib_complex128_t stdlib_blas_ext_zsum( const struct ndarray *arrays[] );
163+
```
164+
165+
</section>
166+
167+
<!-- /.usage -->
168+
169+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
170+
171+
<section class="notes">
172+
173+
</section>
174+
175+
<!-- /.notes -->
176+
177+
<!-- C API usage examples. -->
178+
179+
<section class="examples">
180+
181+
### Examples
182+
183+
```c
184+
#include "stdlib/blas/ext/base/ndarray/zsum.h"
185+
#include "stdlib/complex/float64/ctor.h"
186+
#include "stdlib/complex/float64/real.h"
187+
#include "stdlib/complex/float64/imag.h"
188+
#include "stdlib/ndarray/ctor.h"
189+
#include "stdlib/ndarray/dtypes.h"
190+
#include "stdlib/ndarray/index_modes.h"
191+
#include "stdlib/ndarray/orders.h"
192+
#include "stdlib/ndarray/base/bytes_per_element.h"
193+
#include <stdint.h>
194+
#include <stdlib.h>
195+
#include <stdio.h>
196+
197+
int main( void ) {
198+
// Create a data buffer:
199+
const double data[] = { 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0, -8.0 };
200+
201+
// Specify the number of array dimensions:
202+
const int64_t ndims = 1;
203+
204+
// Specify the array shape:
205+
int64_t shape[] = { 4 };
206+
207+
// Specify the array strides:
208+
int64_t strides[] = { STDLIB_NDARRAY_COMPLEX128_BYTES_PER_ELEMENT };
209+
210+
// Specify the byte offset:
211+
const int64_t offset = 0;
212+
213+
// Specify the array order:
214+
const enum STDLIB_NDARRAY_ORDER order = STDLIB_NDARRAY_ROW_MAJOR;
215+
216+
// Specify the index mode:
217+
const enum STDLIB_NDARRAY_INDEX_MODE imode = STDLIB_NDARRAY_INDEX_ERROR;
218+
219+
// Specify the subscript index modes:
220+
int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR };
221+
const int64_t nsubmodes = 1;
222+
223+
// Create an ndarray:
224+
struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_COMPLEX128, (uint8_t *)data, ndims, shape, strides, offset, order, imode, nsubmodes, submodes );
225+
if ( x == NULL ) {
226+
fprintf( stderr, "Error allocating memory.\n" );
227+
exit( 1 );
228+
}
229+
230+
// Define a list of ndarrays:
231+
const struct ndarray *arrays[] = { x };
232+
233+
// Compute the sum:
234+
stdlib_complex128_t v = stdlib_blas_ext_zsum( arrays );
235+
236+
// Print the result:
237+
printf( "sum: %lf + %lfi\n", stdlib_complex128_real( v ), stdlib_complex128_imag( v ) );
238+
239+
// Free allocated memory:
240+
stdlib_ndarray_free( x );
241+
}
242+
```
243+
244+
</section>
245+
246+
<!-- /.examples -->
247+
248+
</section>
249+
250+
<!-- /.c -->
251+
101252
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
102253
103254
<section class="related">

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ var imag = require( '@stdlib/complex/float64/imag' );
2828
var pow = require( '@stdlib/math/base/special/pow' );
2929
var ndarray = require( '@stdlib/ndarray/base/ctor' );
3030
var Complex128Array = require( '@stdlib/array/complex128' );
31+
var format = require( '@stdlib/string/format' );
3132
var pkg = require( './../package.json' ).name;
32-
var zsum = require( './../lib' );
33+
var zsum = require( './../lib/main.js' );
3334

3435

3536
// VARIABLES //
@@ -106,7 +107,7 @@ function main() {
106107
for ( i = min; i <= max; i++ ) {
107108
len = pow( 10, i );
108109
f = createBenchmark( len );
109-
bench( pkg+':len='+len, f );
110+
bench( format( '%s:len=%d', pkg, len ), f );
110111
}
111112
}
112113

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

0 commit comments

Comments
 (0)