Skip to content

Commit 10ccb4a

Browse files
committed
feat: add blas/ext/base/csum
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: passed - task: lint_c_benchmarks status: passed - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 84af975 commit 10ccb4a

33 files changed

+3458
-0
lines changed
Lines changed: 307 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,307 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# csum
22+
23+
> Calculate the sum of single-precision complex floating-point strided array elements.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var csum = require( '@stdlib/blas/ext/base/csum' );
37+
```
38+
39+
#### csum( N, x, strideX )
40+
41+
Computes the sum of single-precision complex floating-point strided array elements.
42+
43+
```javascript
44+
var Complex64Array = require( '@stdlib/array/complex64' );
45+
46+
var x = new Complex64Array( [ 1.0, -2.0, 2.0, 3.0 ] );
47+
48+
var v = csum( x.length, x, 1 );
49+
// returns <Complex64>[ 3.0, 1.0 ]
50+
```
51+
52+
The function has the following parameters:
53+
54+
- **N**: number of indexed elements.
55+
- **x**: input [`Complex64Array`][@stdlib/array/complex64].
56+
- **strideX**: stride length for `x`.
57+
58+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the sum of every other element:
59+
60+
```javascript
61+
var Complex64Array = require( '@stdlib/array/complex64' );
62+
63+
var x = new Complex64Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ] );
64+
65+
var v = csum( 2, x, 2 );
66+
// returns <Complex64>[ -1.0, 5.0 ]
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 Complex64Array = require( '@stdlib/array/complex64' );
75+
76+
var x0 = new Complex64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
77+
var x1 = new Complex64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
78+
79+
var v = csum( 2, x1, 2 );
80+
// returns <Complex64>[ 5.0, 2.0 ]
81+
```
82+
83+
#### csum.ndarray( N, x, strideX, offsetX )
84+
85+
Computes the sum of single-precision complex floating-point strided array elements using alternative indexing semantics.
86+
87+
```javascript
88+
var Complex64Array = require( '@stdlib/array/complex64' );
89+
90+
var x = new Complex64Array( [ 1.0, -2.0, 2.0, 3.0 ] );
91+
92+
var v = csum.ndarray( 2, x, 1, 0 );
93+
// returns <Complex64>[ 3.0, 1.0 ]
94+
```
95+
96+
The function has the following additional parameters:
97+
98+
- **offsetX**: starting index for `x`.
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 calculate the sum of every other element starting from the second element:
101+
102+
```javascript
103+
var Complex64Array = require( '@stdlib/array/complex64' );
104+
105+
var x = new Complex64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
106+
107+
var v = csum.ndarray( 2, x, 2, 1 );
108+
// returns <Complex64>[ 5.0, 2.0 ]
109+
```
110+
111+
</section>
112+
113+
<!-- /.usage -->
114+
115+
<section class="notes">
116+
117+
## Notes
118+
119+
- If `N <= 0`, both functions return `0.0 + 0.0i`.
120+
121+
</section>
122+
123+
<!-- /.notes -->
124+
125+
<section class="examples">
126+
127+
## Examples
128+
129+
<!-- eslint no-undef: "error" -->
130+
131+
```javascript
132+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
133+
var Complex64Array = require( '@stdlib/array/complex64' );
134+
var csum = require( '@stdlib/blas/ext/base/csum' );
135+
136+
var xbuf = discreteUniform( 10, -100, 100, {
137+
'dtype': 'float32'
138+
});
139+
console.log( xbuf );
140+
141+
var x = new Complex64Array( xbuf );
142+
var v = csum( x.length, x, 1 );
143+
console.log( v );
144+
```
145+
146+
</section>
147+
148+
<!-- /.examples -->
149+
150+
<!-- C interface documentation. -->
151+
152+
* * *
153+
154+
<section class="c">
155+
156+
## C APIs
157+
158+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
159+
160+
<section class="intro">
161+
162+
</section>
163+
164+
<!-- /.intro -->
165+
166+
<!-- C usage documentation. -->
167+
168+
<section class="usage">
169+
170+
### Usage
171+
172+
```c
173+
#include "stdlib/blas/ext/base/csum.h"
174+
```
175+
176+
#### stdlib_strided_csum( N, \*X, strideX )
177+
178+
Computes the sum of single-precision complex floating-point strided array elements.
179+
180+
```c
181+
#include "stdlib/complex/float32/ctor.h"
182+
183+
const stdlib_complex64_t x[] = {
184+
stdlib_complex64( 1.0f, 2.0f ),
185+
stdlib_complex64( 3.0f, 4.0f )
186+
};
187+
188+
stdlib_complex64_t v = stdlib_strided_csum( 2, x, 1 );
189+
```
190+
191+
The function accepts the following arguments:
192+
193+
- **N**: `[in] CBLAS_INT` number of indexed elements.
194+
- **X**: `[in] stdlib_complex64_t*` input array.
195+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
196+
197+
```c
198+
stdlib_complex64_t stdlib_strided_csum( const CBLAS_INT N, const stdlib_complex64_t *X, const CBLAS_INT strideX );
199+
```
200+
201+
#### stdlib_strided_csum_ndarray( N, \*X, strideX, offsetX )
202+
203+
Computes the sum of single-precision complex floating-point strided array elements using alternative indexing semantics.
204+
205+
```c
206+
#include "stdlib/complex/float32/ctor.h"
207+
208+
const stdlib_complex64_t x[] = {
209+
stdlib_complex64( 1.0f, 2.0f ),
210+
stdlib_complex64( 3.0f, 4.0f )
211+
};
212+
213+
stdlib_complex64_t v = stdlib_strided_csum_ndarray( 2, x, 1, 0 );
214+
```
215+
216+
The function accepts the following arguments:
217+
218+
- **N**: `[in] CBLAS_INT` number of indexed elements.
219+
- **X**: `[in] stdlib_complex64_t*` input array.
220+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
221+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
222+
223+
```c
224+
stdlib_complex64_t stdlib_strided_csum_ndarray( const CBLAS_INT N, const stdlib_complex64_t *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
225+
```
226+
227+
</section>
228+
229+
<!-- /.usage -->
230+
231+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
232+
233+
<section class="notes">
234+
235+
</section>
236+
237+
<!-- /.notes -->
238+
239+
<!-- C API usage examples. -->
240+
241+
<section class="examples">
242+
243+
### Examples
244+
245+
```c
246+
#include "stdlib/blas/ext/base/csum.h"
247+
#include "stdlib/complex/float32/ctor.h"
248+
#include "stdlib/complex/float32/real.h"
249+
#include "stdlib/complex/float32/imag.h"
250+
#include <stdio.h>
251+
252+
int main( void ) {
253+
// Create a strided array:
254+
const stdlib_complex64_t x[] = {
255+
stdlib_complex64( 1.0f, 2.0f ),
256+
stdlib_complex64( 3.0f, 4.0f ),
257+
stdlib_complex64( 5.0f, 6.0f ),
258+
stdlib_complex64( 7.0f, 8.0f )
259+
};
260+
261+
// Specify the number of elements:
262+
const int N = 4;
263+
264+
// Specify the stride length:
265+
const int strideX = 1;
266+
267+
// Compute the sum:
268+
stdlib_complex64_t v = stdlib_strided_csum( N, x, strideX );
269+
270+
// Print the result:
271+
printf( "sum: %f + %fi\n", stdlib_complex64_real( v ), stdlib_complex64_imag( v ) );
272+
}
273+
```
274+
275+
</section>
276+
277+
<!-- /.examples -->
278+
279+
</section>
280+
281+
<!-- /.c -->
282+
283+
<section class="references">
284+
285+
</section>
286+
287+
<!-- /.references -->
288+
289+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
290+
291+
<section class="related">
292+
293+
</section>
294+
295+
<!-- /.related -->
296+
297+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
298+
299+
<section class="links">
300+
301+
[@stdlib/array/complex64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex64
302+
303+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
304+
305+
</section>
306+
307+
<!-- /.links -->

0 commit comments

Comments
 (0)