Skip to content

Commit c60545c

Browse files
ShabiShett07kgrytestdlib-bot
authored
feat: add complex/float32/base/mul-add
PR-URL: #5214 Closes: #5213 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent fea0e25 commit c60545c

35 files changed

+3820
-0
lines changed
Lines changed: 286 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,286 @@
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+
# muladd
22+
23+
> Perform a multiply-add operation involving three single-precision complex floating-point numbers.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var muladd = require( '@stdlib/complex/float32/base/mul-add' );
37+
```
38+
39+
#### muladd( alpha, x, y )
40+
41+
Performs a multiply-add operation involving three single-precision complex floating-point numbers.
42+
43+
```javascript
44+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
45+
46+
var z1 = new Complex64( 5.0, 3.0 );
47+
var z2 = new Complex64( -2.0, 1.0 );
48+
var z3 = new Complex64( 7.0, -8.0 );
49+
50+
// Compute `alpha*x + y`:
51+
var v = muladd( z1, z2, z3 );
52+
// returns <Complex64>[ -6.0, -9.0 ]
53+
```
54+
55+
The function supports the following parameters:
56+
57+
- **alpha**: first [complex number][@stdlib/complex/float32/ctor]
58+
- **x**: second [complex number][@stdlib/complex/float32/ctor].
59+
- **y**: third [complex number][@stdlib/complex/float32/ctor].
60+
61+
#### muladd.assign( ar, ai, xr, xi, yr, yi, out, strideOut, offsetOut )
62+
63+
Performs a multiply-add operation involving three single-precision complex floating-point numbers and assigns the results to an output strided array.
64+
65+
```javascript
66+
var Float32Array = require( '@stdlib/array/float32' );
67+
68+
var out = new Float32Array( 2 );
69+
var v = muladd.assign( 5.0, 3.0, -2.0, 1.0, 7.0, -8.0, out, 1, 0 );
70+
// returns <Float32Array>[ -6.0, -9.0 ]
71+
72+
var bool = ( out === v );
73+
// returns true
74+
```
75+
76+
The function supports the following parameters:
77+
78+
- **ar**: real component of the first complex number.
79+
- **ai**: imaginary component of the first complex number.
80+
- **xr**: real component of the second complex number.
81+
- **xi**: imaginary component of the second complex number.
82+
- **yr**: real component of the third complex number.
83+
- **yi**: imaginary component of the third complex number.
84+
- **out**: output array.
85+
- **strideOut**: stride length for `out`.
86+
- **offsetOut**: starting index for `out`.
87+
88+
#### muladd.strided( alpha, sa, oa, x, sx, ox, y, sy, oy, out, so, oo )
89+
90+
Performs a multiply-add operation involving three single-precision complex floating-point numbers stored in real-valued strided array views and assigns results to a provided strided output array.
91+
92+
```javascript
93+
var Float32Array = require( '@stdlib/array/float32' );
94+
95+
var z1 = new Float32Array( [ 5.0, 3.0 ] );
96+
var z2 = new Float32Array( [ -2.0, 1.0 ] );
97+
var z3 = new Float32Array( [ 7.0, -8.0 ] );
98+
var out = new Float32Array( 2 );
99+
100+
var v = muladd.strided( z1, 1, 0, z2, 1, 0, z3, 1, 0, out, 1, 0 );
101+
// returns <Float32Array>[ -6.0, -9.0 ]
102+
103+
var bool = ( out === v );
104+
// returns true
105+
```
106+
107+
The function supports the following parameters:
108+
109+
- **alpha**: first complex number strided array view.
110+
- **sa**: stride length for `alpha`.
111+
- **oa**: starting index for `alpha`.
112+
- **x**: second complex number strided array view.
113+
- **sx**: stride length for `x`.
114+
- **ox**: starting index for `x`.
115+
- **y**: third complex number strided array view.
116+
- **sy**: stride length for `y`.
117+
- **oy**: starting index for `y`.
118+
- **out**: output array.
119+
- **so**: stride length for `out`.
120+
- **oo**: starting index for `out`.
121+
122+
</section>
123+
124+
<!-- /.usage -->
125+
126+
<section class="examples">
127+
128+
## Examples
129+
130+
<!-- eslint no-undef: "error" -->
131+
132+
```javascript
133+
var Complex64Array = require( '@stdlib/array/complex64' );
134+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
135+
var logEachMap = require( '@stdlib/console/log-each-map' );
136+
var muladd = require( '@stdlib/complex/float32/base/mul-add' );
137+
138+
// Generate arrays of random values:
139+
var z1 = new Complex64Array( discreteUniform( 200, -50, 50 ) );
140+
var z2 = new Complex64Array( discreteUniform( 200, -50, 50 ) );
141+
var z3 = new Complex64Array( discreteUniform( 200, -50, 50 ) );
142+
143+
// Perform element-wise computation:
144+
logEachMap( '( (%s) * (%s) ) + (%s) = %s', z1, z2, z3, muladd );
145+
```
146+
147+
</section>
148+
149+
<!-- /.examples -->
150+
151+
<!-- C interface documentation. -->
152+
153+
* * *
154+
155+
<section class="c">
156+
157+
## C APIs
158+
159+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
160+
161+
<section class="intro">
162+
163+
</section>
164+
165+
<!-- /.intro -->
166+
167+
<!-- C usage documentation. -->
168+
169+
<section class="usage">
170+
171+
### Usage
172+
173+
```c
174+
#include "stdlib/complex/float32/base/mul_add.h"
175+
```
176+
177+
#### stdlib_base_complex64_muladd( alpha, x, y )
178+
179+
Performs a multiply-add operation involving three single-precision complex floating-point numbers.
180+
181+
```c
182+
#include "stdlib/complex/float32/ctor.h"
183+
#include "stdlib/complex/float32/real.h"
184+
#include "stdlib/complex/float32/imag.h"
185+
186+
stdlib_complex64_t z1 = sstdlib_complex64( 5.0f, 3.0f );
187+
stdlib_complex64_t z2 = sstdlib_complex64( -2.0f, 1.0f );
188+
stdlib_complex64_t z3 = sstdlib_complex64( 7.0f, -8.0f );
189+
190+
stdlib_complex64_t out = stdlib_base_complex64_muladd( z1, z2, z3 );
191+
192+
float re = stdlib_complex64_real( out );
193+
// returns -6.0f
194+
195+
float im = stdlib_complex64_imag( out );
196+
// returns -9.0f
197+
```
198+
199+
The function accepts the following arguments:
200+
201+
- **alpha**: `[in] stdlib_complex64_t` input value.
202+
- **z1**: `[in] stdlib_complex64_t` input value.
203+
- **z2**: `[in] stdlib_complex64_t` input value.
204+
205+
```c
206+
stdlib_complex64_t stdlib_base_complex64_muladd( const stdlib_complex64_t alpha, const stdlib_complex64_t x, const stdlib_complex64_t y );
207+
```
208+
209+
</section>
210+
211+
<!-- /.usage -->
212+
213+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
214+
215+
<section class="notes">
216+
217+
</section>
218+
219+
<!-- /.notes -->
220+
221+
<!-- C API usage examples. -->
222+
223+
<section class="examples">
224+
225+
### Examples
226+
227+
```c
228+
#include "stdlib/complex/float32/base/mul_add.h"
229+
#include "stdlib/complex/float32/ctor.h"
230+
#include "stdlib/complex/float32/reim.h"
231+
#include <stdio.h>
232+
233+
int main( void ) {
234+
const stdlib_complex64_t x[] = {
235+
stdlib_complex64( 3.14f, 1.5f ),
236+
stdlib_complex64( -3.14f, 1.5f ),
237+
stdlib_complex64( 0.0f, -0.0f ),
238+
stdlib_complex64( 0.0f/0.0f, 0.0f/0.0f )
239+
};
240+
241+
stdlib_complex64_t v;
242+
stdlib_complex64_t y;
243+
float re;
244+
float im;
245+
int i;
246+
for ( i = 0; i < 4; i++ ) {
247+
v = x[ i ];
248+
stdlib_complex64_reim( v, &re, &im );
249+
printf( "z = %f + %fi\n", re, im );
250+
251+
y = stdlib_base_complex64_muladd( v, v, v );
252+
stdlib_complex64_reim( y, &re, &im );
253+
printf( "z*z + z = %f + %fi\n", re, im );
254+
}
255+
}
256+
```
257+
258+
</section>
259+
260+
<!-- /.examples -->
261+
262+
</section>
263+
264+
<!-- /.c -->
265+
266+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
267+
268+
<section class="related">
269+
270+
</section>
271+
272+
<!-- /.related -->
273+
274+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
275+
276+
<section class="links">
277+
278+
[@stdlib/complex/float32/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/complex/float32/ctor
279+
280+
<!-- <related-links> -->
281+
282+
<!-- </related-links> -->
283+
284+
</section>
285+
286+
<!-- /.links -->
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 Float32Array = require( '@stdlib/array/float32' );
27+
var pkg = require( './../package.json' ).name;
28+
var muladd = require( './../lib' );
29+
30+
31+
// VARIABLES //
32+
33+
var options = {
34+
'dtype': 'float32'
35+
};
36+
37+
38+
// MAIN //
39+
40+
bench( pkg+':assign', function benchmark( b ) {
41+
var out;
42+
var re;
43+
var im;
44+
var N;
45+
var i;
46+
var j;
47+
var k;
48+
49+
N = 100;
50+
re = uniform( N, -500.0, 500.0, options );
51+
im = uniform( N, -500.0, 500.0, options );
52+
53+
out = new Float32Array( 2 );
54+
55+
b.tic();
56+
for ( i = 0; i < b.iterations; i++ ) {
57+
j = i % N;
58+
k = ( i+1 ) % N;
59+
out = muladd.assign( re[ j ], im[ j ], re[ k ], im[ k ], re[ k ], im[ k ], out, 1, 0 ); // eslint-disable-line max-len
60+
if ( typeof out !== 'object' ) {
61+
b.fail( 'should return an object' );
62+
}
63+
}
64+
b.toc();
65+
if ( isnanf( out[ 0 ] ) || isnanf( out[ 1 ] ) ) {
66+
b.fail( 'should not return NaN' );
67+
}
68+
b.pass( 'benchmark finished' );
69+
b.end();
70+
});

0 commit comments

Comments
 (0)