Skip to content

Commit b05fe40

Browse files
MeKaustubh07kgryte
andauthored
feat: add C implementation for blas/ext/base/ndarray/zsum
PR-URL: #10699 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent d939241 commit b05fe40

File tree

20 files changed

+1904
-154
lines changed

20 files changed

+1904
-154
lines changed

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

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,164 @@ 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 "stdlib/complex/float64/real.h"
139+
#include "stdlib/complex/float64/imag.h"
140+
#include <stdint.h>
141+
142+
// Create an ndarray:
143+
const double data[] = { 1.0, 2.0, 3.0, 4.0 };
144+
int64_t shape[] = { 2 };
145+
int64_t strides[] = { STDLIB_NDARRAY_COMPLEX128_BYTES_PER_ELEMENT };
146+
int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR };
147+
148+
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 );
149+
150+
// Compute the sum:
151+
const struct ndarray *arrays[] = { x };
152+
stdlib_complex128_t v = stdlib_blas_ext_zsum( arrays );
153+
154+
double re = stdlib_complex128_real( v );
155+
// returns 4.0
156+
157+
double im = stdlib_complex128_imag( v );
158+
// returns 6.0
159+
160+
// Free allocated memory:
161+
stdlib_ndarray_free( x );
162+
```
163+
164+
The function accepts the following arguments:
165+
166+
- **arrays**: `[in] struct ndarray**` list containing a one-dimensional input ndarray.
167+
168+
```c
169+
stdlib_complex128_t stdlib_blas_ext_zsum( const struct ndarray *arrays[] );
170+
```
171+
172+
</section>
173+
174+
<!-- /.usage -->
175+
176+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
177+
178+
<section class="notes">
179+
180+
</section>
181+
182+
<!-- /.notes -->
183+
184+
<!-- C API usage examples. -->
185+
186+
<section class="examples">
187+
188+
### Examples
189+
190+
```c
191+
#include "stdlib/blas/ext/base/ndarray/zsum.h"
192+
#include "stdlib/complex/float64/ctor.h"
193+
#include "stdlib/complex/float64/real.h"
194+
#include "stdlib/complex/float64/imag.h"
195+
#include "stdlib/ndarray/ctor.h"
196+
#include "stdlib/ndarray/dtypes.h"
197+
#include "stdlib/ndarray/index_modes.h"
198+
#include "stdlib/ndarray/orders.h"
199+
#include "stdlib/ndarray/base/bytes_per_element.h"
200+
#include <stdint.h>
201+
#include <stdlib.h>
202+
#include <stdio.h>
203+
204+
int main( void ) {
205+
// Create a data buffer:
206+
const double data[] = { 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0, -8.0 };
207+
208+
// Specify the number of array dimensions:
209+
const int64_t ndims = 1;
210+
211+
// Specify the array shape:
212+
int64_t shape[] = { 4 };
213+
214+
// Specify the array strides:
215+
int64_t strides[] = { STDLIB_NDARRAY_COMPLEX128_BYTES_PER_ELEMENT };
216+
217+
// Specify the byte offset:
218+
const int64_t offset = 0;
219+
220+
// Specify the array order:
221+
const enum STDLIB_NDARRAY_ORDER order = STDLIB_NDARRAY_ROW_MAJOR;
222+
223+
// Specify the index mode:
224+
const enum STDLIB_NDARRAY_INDEX_MODE imode = STDLIB_NDARRAY_INDEX_ERROR;
225+
226+
// Specify the subscript index modes:
227+
int8_t submodes[] = { STDLIB_NDARRAY_INDEX_ERROR };
228+
const int64_t nsubmodes = 1;
229+
230+
// Create an ndarray:
231+
struct ndarray *x = stdlib_ndarray_allocate( STDLIB_NDARRAY_COMPLEX128, (uint8_t *)data, ndims, shape, strides, offset, order, imode, nsubmodes, submodes );
232+
if ( x == NULL ) {
233+
fprintf( stderr, "Error allocating memory.\n" );
234+
exit( 1 );
235+
}
236+
237+
// Define a list of ndarrays:
238+
const struct ndarray *arrays[] = { x };
239+
240+
// Compute the sum:
241+
stdlib_complex128_t v = stdlib_blas_ext_zsum( arrays );
242+
243+
// Print the result:
244+
printf( "sum: %lf + %lfi\n", stdlib_complex128_real( v ), stdlib_complex128_imag( v ) );
245+
246+
// Free allocated memory:
247+
stdlib_ndarray_free( x );
248+
}
249+
```
250+
251+
</section>
252+
253+
<!-- /.examples -->
254+
255+
</section>
256+
257+
<!-- /.c -->
258+
101259
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
102260
103261
<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)