Skip to content

Commit 3dda09b

Browse files
aman-095kgryte
andauthored
feat: add blas/base/scasum
PR-URL: #2573 Ref: #2039 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent a41081b commit 3dda09b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+4427
-0
lines changed
Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# scasum
22+
23+
> Compute the sum of the absolute values of the real and imaginary components of a single-precision complex floating-point vector.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var scasum = require( '@stdlib/blas/base/scasum' );
31+
```
32+
33+
#### scasum( N, cx, strideX )
34+
35+
Computes the sum of the absolute values of the real and imaginary components of a single-precision complex floating-point vector.
36+
37+
```javascript
38+
var Complex64Array = require( '@stdlib/array/complex64' );
39+
40+
var cx = new Complex64Array( [ 0.3, 0.1, 0.5, 0.0, 0.0, 0.5, 0.0, 0.2 ] );
41+
42+
var out = scasum( 4, cx, 1 );
43+
// returns ~1.6
44+
```
45+
46+
The function has the following parameters:
47+
48+
- **N**: number of indexed elements.
49+
- **cx**: input [`Complex64Array`][@stdlib/array/complex64].
50+
- **strideX**: index increment for `cx`.
51+
52+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to traverse every other value,
53+
54+
```javascript
55+
var Complex64Array = require( '@stdlib/array/complex64' );
56+
57+
var cx = new Complex64Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] );
58+
59+
var out = scasum( 2, cx, 2 );
60+
// returns 7.0
61+
```
62+
63+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
64+
65+
```javascript
66+
var Complex64Array = require( '@stdlib/array/complex64' );
67+
68+
// Initial array:
69+
var cx0 = new Complex64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );
70+
71+
// Create an offset view:
72+
var cx1 = new Complex64Array( cx0.buffer, cx0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
73+
74+
// Compute the L2-out:
75+
var out = scasum( 2, cx1, 1 );
76+
// returns 18.0
77+
```
78+
79+
#### scasum.ndarray( N, cx, strideX, offset )
80+
81+
Computes the sum of the absolute values of the real and imaginary components of a single-precision complex floating-point vector using alternative indexing semantics.
82+
83+
```javascript
84+
var Complex64Array = require( '@stdlib/array/complex64' );
85+
86+
var cx = new Complex64Array( [ 0.3, 0.1, 0.5, 0.0, 0.0, 0.5, 0.0, 0.2 ] );
87+
88+
var out = scasum.ndarray( 4, cx, 1, 0 );
89+
// returns ~1.6
90+
```
91+
92+
The function has the following additional parameters:
93+
94+
- **offsetX**: starting index.
95+
96+
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 start from the second index,
97+
98+
```javascript
99+
var Complex64Array = require( '@stdlib/array/complex64' );
100+
101+
var cx = new Complex64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );
102+
103+
var out = scasum.ndarray( 2, cx, 1, 1 );
104+
// returns 18.0
105+
```
106+
107+
</section>
108+
109+
<!-- /.usage -->
110+
111+
<section class="notes">
112+
113+
## Notes
114+
115+
- If `N <= 0`, both functions return `0.0`.
116+
- `scasum()` corresponds to the [BLAS][blas] level 1 function [`scasum`][scasum].
117+
118+
</section>
119+
120+
<!-- /.notes -->
121+
122+
<section class="examples">
123+
124+
## Examples
125+
126+
<!-- eslint no-undef: "error" -->
127+
128+
```javascript
129+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
130+
var filledarrayBy = require( '@stdlib/array/filled-by' );
131+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
132+
var scasum = require( '@stdlib/blas/base/scasum' );
133+
134+
function rand() {
135+
return new Complex64( discreteUniform( 0, 10 ), discreteUniform( -5, 5 ) );
136+
}
137+
138+
var cx = filledarrayBy( 10, 'complex64', rand );
139+
console.log( cx.toString() );
140+
141+
// Compute the sum of the absolute values of real and imaginary components:
142+
var out = scasum( cx.length, cx, 1 );
143+
console.log( out );
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/base/scasum.h"
174+
```
175+
176+
#### c_scasum( N, \*CX, strideX )
177+
178+
Computes the sum of the absolute values of the real and imaginary components of a single-precision complex floating-point vector.
179+
180+
```c
181+
const float cx[] = { 0.3f, 0.1f, 0.5f, 0.0f, 0.0f, 0.5f, 0.0f, 0.2f };
182+
183+
float out = c_scasum( 4, (void *)cx, 1 );
184+
// returns 1.6f
185+
```
186+
187+
The function accepts the following arguments:
188+
189+
- **N**: `[in] CBLAS_INT` number of indexed elements.
190+
- **CX**: `[in] void*` input array.
191+
- **strideX**: `[in] CBLAS_INT` index increment for `CX`.
192+
193+
```c
194+
float c_scasum( const CBLAS_INT N, const void *CX, const CBLAS_INT strideX );
195+
```
196+
197+
</section>
198+
199+
<!-- /.usage -->
200+
201+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
202+
203+
<section class="notes">
204+
205+
</section>
206+
207+
<!-- /.notes -->
208+
209+
<!-- C API usage examples. -->
210+
211+
<section class="examples">
212+
213+
### Examples
214+
215+
```c
216+
#include "stdlib/blas/base/scasum.h"
217+
#include <stdio.h>
218+
219+
int main( void ) {
220+
// Create a strided array of interleaved real and imaginary components:
221+
const float cx[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
222+
223+
// Specify the number of elements:
224+
const int N = 4;
225+
226+
// Specify stride length:
227+
const int strideX = 1;
228+
229+
// Compute the sum of the absolute values of real and imaginary components:
230+
float out = c_scasum( N, (void *)cx, strideX );
231+
232+
// Print the result:
233+
printf( "out: %f\n", out );
234+
}
235+
```
236+
237+
</section>
238+
239+
<!-- /.examples -->
240+
241+
</section>
242+
243+
<!-- /.c -->
244+
245+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
246+
247+
<section class="related">
248+
249+
</section>
250+
251+
<!-- /.related -->
252+
253+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
254+
255+
<section class="links">
256+
257+
[blas]: http://www.netlib.org/blas
258+
259+
[scasum]: https://www.netlib.org/lapack/explore-html/d5/d72/group__asum_ga89c76eef329f84ba9ed106b34fedab16.html#ga89c76eef329f84ba9ed106b34fedab16
260+
261+
[@stdlib/array/complex64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex64
262+
263+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
264+
265+
</section>
266+
267+
<!-- /.links -->
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var Complex64Array = require( '@stdlib/array/complex64' );
28+
var pkg = require( './../package.json' ).name;
29+
var scasum = require( './../lib/scasum.js' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'float32'
36+
};
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Create a benchmark function.
43+
*
44+
* @private
45+
* @param {PositiveInteger} len - array length
46+
* @returns {Function} benchmark function
47+
*/
48+
function createBenchmark( len ) {
49+
var cx = new Complex64Array( uniform( len*2, -10.0, 10.0, options ) );
50+
return benchmark;
51+
52+
function benchmark( b ) {
53+
var out;
54+
var i;
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
out = scasum( cx.length, cx, 1 );
59+
if ( isnanf( out ) ) {
60+
b.fail( 'should not return NaN' );
61+
}
62+
}
63+
b.toc();
64+
if ( isnanf( out ) ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
}
70+
}
71+
72+
73+
// MAIN //
74+
75+
function main() {
76+
var len;
77+
var min;
78+
var max;
79+
var f;
80+
var i;
81+
82+
min = 1; // 10^min
83+
max = 6; // 10^max
84+
85+
for ( i = min; i <= max; i++ ) {
86+
len = pow( 10, i );
87+
f = createBenchmark( len );
88+
bench( pkg+':len='+len, f );
89+
}
90+
}
91+
92+
main();

0 commit comments

Comments
 (0)