Skip to content

Commit 1153c3e

Browse files
committed
feat: add /complex/float32/base/mul-add
1 parent 8785e54 commit 1153c3e

File tree

35 files changed

+3903
-0
lines changed

35 files changed

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