Skip to content

Commit 7475c9d

Browse files
ShabiShett07kgryte
andauthored
feat: add complex/float32/base/scale
PR-URL: #7156 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 5c8faed commit 7475c9d

35 files changed

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

0 commit comments

Comments
 (0)