@stdlib/complex-float32 0.0.3 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/NOTICE +1 -1
- package/README.md +328 -8
- package/docs/types/index.d.ts +2 -2
- package/docs/types/test.ts +2 -2
- package/include/stdlib/complex/float32.h +148 -0
- package/lib/index.js +2 -2
- package/lib/{complex64.js → main.js} +7 -31
- package/manifest.json +38 -0
- package/package.json +4 -3
- package/src/main.c +151 -0
- package/CHANGELOG.md +0 -5
package/NOTICE
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Copyright (c) 2016-
|
|
1
|
+
Copyright (c) 2016-2022 The Stdlib Authors.
|
package/README.md
CHANGED
|
@@ -20,7 +20,7 @@ limitations under the License.
|
|
|
20
20
|
|
|
21
21
|
# Complex64
|
|
22
22
|
|
|
23
|
-
[![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url] [![dependencies][dependencies-image]][dependencies-url]
|
|
23
|
+
[![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url] <!-- [![dependencies][dependencies-image]][dependencies-url] -->
|
|
24
24
|
|
|
25
25
|
> 64-bit complex number.
|
|
26
26
|
|
|
@@ -197,25 +197,311 @@ var Complex64 = require( '@stdlib/complex-float32' );
|
|
|
197
197
|
var z = new Complex64( 3.0, -2.0 );
|
|
198
198
|
|
|
199
199
|
console.log( 'type: %s', typeof z );
|
|
200
|
-
// => type: object
|
|
200
|
+
// => 'type: object'
|
|
201
201
|
|
|
202
202
|
console.log( 'str: %s', z );
|
|
203
|
-
// => str: 3 - 2i
|
|
203
|
+
// => 'str: 3 - 2i'
|
|
204
204
|
|
|
205
205
|
console.log( 'real: %d', z.re );
|
|
206
|
-
// => real: 3
|
|
206
|
+
// => 'real: 3'
|
|
207
207
|
|
|
208
208
|
console.log( 'imag: %d', z.im );
|
|
209
|
-
// => imag: -2
|
|
209
|
+
// => 'imag: -2'
|
|
210
210
|
|
|
211
211
|
console.log( 'JSON: %s', JSON.stringify( z ) );
|
|
212
|
-
// => JSON: {"type":"Complex64","re":3,"im":-2}
|
|
212
|
+
// => 'JSON: {"type":"Complex64","re":3,"im":-2}'
|
|
213
213
|
```
|
|
214
214
|
|
|
215
215
|
</section>
|
|
216
216
|
|
|
217
217
|
<!-- /.examples -->
|
|
218
218
|
|
|
219
|
+
<!-- C interface documentation. -->
|
|
220
|
+
|
|
221
|
+
* * *
|
|
222
|
+
|
|
223
|
+
<section class="c">
|
|
224
|
+
|
|
225
|
+
## C APIs
|
|
226
|
+
|
|
227
|
+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
|
|
228
|
+
|
|
229
|
+
<section class="intro">
|
|
230
|
+
|
|
231
|
+
</section>
|
|
232
|
+
|
|
233
|
+
<!-- /.intro -->
|
|
234
|
+
|
|
235
|
+
<!-- C usage documentation. -->
|
|
236
|
+
|
|
237
|
+
<section class="usage">
|
|
238
|
+
|
|
239
|
+
### Usage
|
|
240
|
+
|
|
241
|
+
```c
|
|
242
|
+
#include "stdlib/complex/float32.h"
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
#### stdlib_complex64_t
|
|
246
|
+
|
|
247
|
+
An opaque type definition for a single-precision complex floating-point number.
|
|
248
|
+
|
|
249
|
+
```c
|
|
250
|
+
stdlib_complex64_t z = stdlib_complex64( 5.0f, 2.0f );
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
#### stdlib_complex64_parts_t
|
|
254
|
+
|
|
255
|
+
An opaque type definition for a union for accessing the real and imaginary parts of a single-precision complex floating-point number.
|
|
256
|
+
|
|
257
|
+
```c
|
|
258
|
+
float realf( const stdlib_complex64_t z ) {
|
|
259
|
+
stdlib_complex64_parts_t v;
|
|
260
|
+
|
|
261
|
+
// Assign a single-precision complex floating-point number:
|
|
262
|
+
v.value = z;
|
|
263
|
+
|
|
264
|
+
// Extract the real component:
|
|
265
|
+
float re = v.parts[ 0 ];
|
|
266
|
+
|
|
267
|
+
return re;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// ...
|
|
271
|
+
|
|
272
|
+
// Create a complex number:
|
|
273
|
+
stdlib_complex64_t z = stdlib_complex64( 5.0f, 2.0f );
|
|
274
|
+
|
|
275
|
+
// ...
|
|
276
|
+
|
|
277
|
+
// Access the real component:
|
|
278
|
+
float re = realf( z );
|
|
279
|
+
// returns 5.0f
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
The union has the following members:
|
|
283
|
+
|
|
284
|
+
- **value**: `stdlib_complex64_t` single-precision complex floating-point number.
|
|
285
|
+
|
|
286
|
+
- **parts**: `float[]` array having the following elements:
|
|
287
|
+
|
|
288
|
+
- **0**: `float` real component.
|
|
289
|
+
- **1**: `float` imaginary component.
|
|
290
|
+
|
|
291
|
+
#### stdlib_complex64( real, imag )
|
|
292
|
+
|
|
293
|
+
Returns a single-precision complex floating-point number.
|
|
294
|
+
|
|
295
|
+
```c
|
|
296
|
+
stdlib_complex64_t z = stdlib_complex64( 5.0f, 2.0f );
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
The function accepts the following arguments:
|
|
300
|
+
|
|
301
|
+
- **real**: `[in] float` real component.
|
|
302
|
+
- **imag**: `[in] float` imaginary component.
|
|
303
|
+
|
|
304
|
+
```c
|
|
305
|
+
stdlib_complex64_t stdlib_complex64( const float real, const float imag );
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
#### stdlib_complex64_from_float32( real )
|
|
309
|
+
|
|
310
|
+
Converts a single-precision floating-point number to a single-precision complex floating-point number.
|
|
311
|
+
|
|
312
|
+
```c
|
|
313
|
+
stdlib_complex64_t z = stdlib_complex64_from_float32( 5.0f );
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
The function accepts the following arguments:
|
|
317
|
+
|
|
318
|
+
- **real**: `[in] float` real component.
|
|
319
|
+
|
|
320
|
+
```c
|
|
321
|
+
stdlib_complex64_t stdlib_complex64_from_float32( const float real );
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
#### stdlib_complex64_from_float64( real )
|
|
325
|
+
|
|
326
|
+
Converts a double-precision floating-point number to a single-precision complex floating-point number.
|
|
327
|
+
|
|
328
|
+
```c
|
|
329
|
+
stdlib_complex64_t z = stdlib_complex64_from_float64( 5.0 );
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
The function accepts the following arguments:
|
|
333
|
+
|
|
334
|
+
- **real**: `[in] double` real component.
|
|
335
|
+
|
|
336
|
+
```c
|
|
337
|
+
stdlib_complex64_t stdlib_complex64_from_float64( const double real );
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
#### stdlib_complex64_from_complex64( z )
|
|
341
|
+
|
|
342
|
+
Converts (copies) a single-precision complex floating-point number to a single-precision complex floating-point number.
|
|
343
|
+
|
|
344
|
+
```c
|
|
345
|
+
stdlib_complex64_t z1 = stdlib_complex64( 5.0f, 3.0f );
|
|
346
|
+
stdlib_complex64_t z2 = stdlib_complex64_from_complex64( z1 );
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
The function accepts the following arguments:
|
|
350
|
+
|
|
351
|
+
- **z**: `[in] stdlib_complex64_t` single-precision complex floating-point number.
|
|
352
|
+
|
|
353
|
+
```c
|
|
354
|
+
stdlib_complex64_t stdlib_complex64_from_complex64( const stdlib_complex64_t z );
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
#### stdlib_complex64_from_int8( real )
|
|
358
|
+
|
|
359
|
+
Converts a signed 8-bit integer to a single-precision complex floating-point number.
|
|
360
|
+
|
|
361
|
+
```c
|
|
362
|
+
stdlib_complex64_t z = stdlib_complex64_from_int8( 5 );
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
The function accepts the following arguments:
|
|
366
|
+
|
|
367
|
+
- **real**: `[in] int8_t` real component.
|
|
368
|
+
|
|
369
|
+
```c
|
|
370
|
+
stdlib_complex64_t stdlib_complex64_from_int8( const int8_t real );
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
#### stdlib_complex64_from_uint8( real )
|
|
374
|
+
|
|
375
|
+
Converts an unsigned 8-bit integer to a single-precision complex floating-point number.
|
|
376
|
+
|
|
377
|
+
```c
|
|
378
|
+
stdlib_complex64_t z = stdlib_complex64_from_uint8( 5 );
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
The function accepts the following arguments:
|
|
382
|
+
|
|
383
|
+
- **real**: `[in] uint8_t` real component.
|
|
384
|
+
|
|
385
|
+
```c
|
|
386
|
+
stdlib_complex64_t stdlib_complex64_from_uint8( const uint8_t real );
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
#### stdlib_complex64_from_int16( real )
|
|
390
|
+
|
|
391
|
+
Converts a signed 16-bit integer to a single-precision complex floating-point number.
|
|
392
|
+
|
|
393
|
+
```c
|
|
394
|
+
stdlib_complex64_t z = stdlib_complex64_from_int16( 5 );
|
|
395
|
+
```
|
|
396
|
+
|
|
397
|
+
The function accepts the following arguments:
|
|
398
|
+
|
|
399
|
+
- **real**: `[in] int16_t` real component.
|
|
400
|
+
|
|
401
|
+
```c
|
|
402
|
+
stdlib_complex64_t stdlib_complex64_from_int16( const int16_t real );
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
#### stdlib_complex64_from_uint16( real )
|
|
406
|
+
|
|
407
|
+
Converts an unsigned 16-bit integer to a single-precision complex floating-point number.
|
|
408
|
+
|
|
409
|
+
```c
|
|
410
|
+
stdlib_complex64_t z = stdlib_complex64_from_uint16( 5 );
|
|
411
|
+
```
|
|
412
|
+
|
|
413
|
+
The function accepts the following arguments:
|
|
414
|
+
|
|
415
|
+
- **real**: `[in] uint16_t` real component.
|
|
416
|
+
|
|
417
|
+
```c
|
|
418
|
+
stdlib_complex64_t stdlib_complex64_from_uint16( const uint16_t real );
|
|
419
|
+
```
|
|
420
|
+
|
|
421
|
+
</section>
|
|
422
|
+
|
|
423
|
+
<!-- /.usage -->
|
|
424
|
+
|
|
425
|
+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
426
|
+
|
|
427
|
+
<section class="notes">
|
|
428
|
+
|
|
429
|
+
</section>
|
|
430
|
+
|
|
431
|
+
<!-- /.notes -->
|
|
432
|
+
|
|
433
|
+
<!-- C API usage examples. -->
|
|
434
|
+
|
|
435
|
+
<section class="examples">
|
|
436
|
+
|
|
437
|
+
### Examples
|
|
438
|
+
|
|
439
|
+
```c
|
|
440
|
+
#include "stdlib/complex/float32.h"
|
|
441
|
+
#include <stdint.h>
|
|
442
|
+
#include <stdio.h>
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* Return the real component of a single-precision complex floating-point number.
|
|
446
|
+
*
|
|
447
|
+
* @param z complex number
|
|
448
|
+
* @return real component
|
|
449
|
+
*/
|
|
450
|
+
static float real( const stdlib_complex64_t z ) {
|
|
451
|
+
stdlib_complex64_parts_t v;
|
|
452
|
+
|
|
453
|
+
// Assign a single-precision complex floating-point number:
|
|
454
|
+
v.value = z;
|
|
455
|
+
|
|
456
|
+
// Extract the real component:
|
|
457
|
+
float re = v.parts[ 0 ];
|
|
458
|
+
|
|
459
|
+
return re;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/**
|
|
463
|
+
* Return the imaginary component of a single-precision complex floating-point number.
|
|
464
|
+
*
|
|
465
|
+
* @param z complex number
|
|
466
|
+
* @return imaginary component
|
|
467
|
+
*/
|
|
468
|
+
static float imag( const stdlib_complex64_t z ) {
|
|
469
|
+
stdlib_complex64_parts_t v;
|
|
470
|
+
|
|
471
|
+
// Assign a single-precision complex floating-point number:
|
|
472
|
+
v.value = z;
|
|
473
|
+
|
|
474
|
+
// Extract the imaginary component:
|
|
475
|
+
float im = v.parts[ 1 ];
|
|
476
|
+
|
|
477
|
+
return im;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
int main() {
|
|
481
|
+
stdlib_complex64_t x[] = {
|
|
482
|
+
stdlib_complex64( 5.0f, 2.0f ),
|
|
483
|
+
stdlib_complex64( -2.0f, 1.0f ),
|
|
484
|
+
stdlib_complex64( 0.0f, -0.0f ),
|
|
485
|
+
stdlib_complex64( 0.0f/0.0f, 0.0f/0.0f )
|
|
486
|
+
};
|
|
487
|
+
|
|
488
|
+
stdlib_complex64_t v;
|
|
489
|
+
int i;
|
|
490
|
+
for ( i = 0; i < 4; i++ ) {
|
|
491
|
+
v = x[ i ];
|
|
492
|
+
printf( "%f + %fi\n", real( v ), imag( v ) );
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
```
|
|
496
|
+
|
|
497
|
+
</section>
|
|
498
|
+
|
|
499
|
+
<!-- /.examples -->
|
|
500
|
+
|
|
501
|
+
</section>
|
|
502
|
+
|
|
503
|
+
<!-- /.c -->
|
|
504
|
+
|
|
219
505
|
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
220
506
|
|
|
221
507
|
<section class="references">
|
|
@@ -224,6 +510,21 @@ console.log( 'JSON: %s', JSON.stringify( z ) );
|
|
|
224
510
|
|
|
225
511
|
<!-- /.references -->
|
|
226
512
|
|
|
513
|
+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
|
|
514
|
+
|
|
515
|
+
<section class="related">
|
|
516
|
+
|
|
517
|
+
* * *
|
|
518
|
+
|
|
519
|
+
## See Also
|
|
520
|
+
|
|
521
|
+
- <span class="package-name">[`@stdlib/complex/cmplx`][@stdlib/complex/cmplx]</span><span class="delimiter">: </span><span class="description">create a complex number.</span>
|
|
522
|
+
- <span class="package-name">[`@stdlib/complex/float64`][@stdlib/complex/float64]</span><span class="delimiter">: </span><span class="description">128-bit complex number.</span>
|
|
523
|
+
|
|
524
|
+
</section>
|
|
525
|
+
|
|
526
|
+
<!-- /.related -->
|
|
527
|
+
|
|
227
528
|
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
228
529
|
|
|
229
530
|
|
|
@@ -250,7 +551,7 @@ See [LICENSE][stdlib-license].
|
|
|
250
551
|
|
|
251
552
|
## Copyright
|
|
252
553
|
|
|
253
|
-
Copyright © 2016-
|
|
554
|
+
Copyright © 2016-2022. The Stdlib [Authors][stdlib-authors].
|
|
254
555
|
|
|
255
556
|
</section>
|
|
256
557
|
|
|
@@ -269,9 +570,20 @@ Copyright © 2016-2021. The Stdlib [Authors][stdlib-authors].
|
|
|
269
570
|
[coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/complex-float32/main.svg
|
|
270
571
|
[coverage-url]: https://codecov.io/github/stdlib-js/complex-float32?branch=main
|
|
271
572
|
|
|
573
|
+
<!--
|
|
574
|
+
|
|
272
575
|
[dependencies-image]: https://img.shields.io/david/stdlib-js/complex-float32.svg
|
|
273
576
|
[dependencies-url]: https://david-dm.org/stdlib-js/complex-float32/main
|
|
274
577
|
|
|
578
|
+
-->
|
|
579
|
+
|
|
580
|
+
[umd]: https://github.com/umdjs/umd
|
|
581
|
+
[es-module]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
|
|
582
|
+
|
|
583
|
+
[deno-url]: https://github.com/stdlib-js/complex-float32/tree/deno
|
|
584
|
+
[umd-url]: https://github.com/stdlib-js/complex-float32/tree/umd
|
|
585
|
+
[esm-url]: https://github.com/stdlib-js/complex-float32/tree/esm
|
|
586
|
+
|
|
275
587
|
[chat-image]: https://img.shields.io/gitter/room/stdlib-js/stdlib.svg
|
|
276
588
|
[chat-url]: https://gitter.im/stdlib-js/stdlib/
|
|
277
589
|
|
|
@@ -287,7 +599,15 @@ Copyright © 2016-2021. The Stdlib [Authors][stdlib-authors].
|
|
|
287
599
|
|
|
288
600
|
[mdn-json-parse]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
|
|
289
601
|
|
|
290
|
-
[@stdlib/complex/reviver-float32]: https://
|
|
602
|
+
[@stdlib/complex/reviver-float32]: https://www.npmjs.com/package/@stdlib/complex-reviver-float32
|
|
603
|
+
|
|
604
|
+
<!-- <related-links> -->
|
|
605
|
+
|
|
606
|
+
[@stdlib/complex/cmplx]: https://www.npmjs.com/package/@stdlib/complex-cmplx
|
|
607
|
+
|
|
608
|
+
[@stdlib/complex/float64]: https://www.npmjs.com/package/@stdlib/complex-float64
|
|
609
|
+
|
|
610
|
+
<!-- </related-links> -->
|
|
291
611
|
|
|
292
612
|
</section>
|
|
293
613
|
|
package/docs/types/index.d.ts
CHANGED
|
@@ -58,7 +58,7 @@ declare class Complex64 {
|
|
|
58
58
|
* var nbytes = Complex64.BYTES_PER_ELEMENT;
|
|
59
59
|
* // returns 4
|
|
60
60
|
*/
|
|
61
|
-
readonly BYTES_PER_ELEMENT:
|
|
61
|
+
readonly BYTES_PER_ELEMENT: 4;
|
|
62
62
|
|
|
63
63
|
/**
|
|
64
64
|
* Length (in bytes) of a complex number.
|
|
@@ -71,7 +71,7 @@ declare class Complex64 {
|
|
|
71
71
|
* var nbytes = z.byteLength;
|
|
72
72
|
* // returns 8
|
|
73
73
|
*/
|
|
74
|
-
readonly byteLength:
|
|
74
|
+
readonly byteLength: 8;
|
|
75
75
|
|
|
76
76
|
/**
|
|
77
77
|
* Serializes a complex number as a string.
|
package/docs/types/test.ts
CHANGED
|
@@ -32,10 +32,10 @@ import Complex64 = require( './index' );
|
|
|
32
32
|
x.re; // $ExpectType number
|
|
33
33
|
|
|
34
34
|
// tslint:disable-next-line:no-unused-expression
|
|
35
|
-
x.BYTES_PER_ELEMENT; // $ExpectType
|
|
35
|
+
x.BYTES_PER_ELEMENT; // $ExpectType 4
|
|
36
36
|
|
|
37
37
|
// tslint:disable-next-line:no-unused-expression
|
|
38
|
-
x.byteLength; // $ExpectType
|
|
38
|
+
x.byteLength; // $ExpectType 8
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
// 64-bit complex number comes with a `toString` method to serialize a complex number as a string...
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2021 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
|
+
#ifndef STDLIB_COMPLEX_FLOAT32_H
|
|
20
|
+
#define STDLIB_COMPLEX_FLOAT32_H
|
|
21
|
+
|
|
22
|
+
#include <complex.h>
|
|
23
|
+
#include <stdint.h>
|
|
24
|
+
|
|
25
|
+
/*
|
|
26
|
+
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
|
|
27
|
+
*/
|
|
28
|
+
#ifdef __cplusplus
|
|
29
|
+
extern "C" {
|
|
30
|
+
#endif
|
|
31
|
+
|
|
32
|
+
// Check for C11 support where we can precisely define a complex number and thus avoid issues concerning infinities and NaNs as real and/or imaginary components... (TODO: revisit the following check; similar to NumPy, we may want to check for a compile time variable (e.g., STDLIB_USE_C99_COMPLEX), rather than checking for C11 support, especially if we are not using C11 functionality)
|
|
33
|
+
#if defined(_Imaginary_I) && defined(CMPLXF)
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* An opaque type definition for a single-precision complex floating-point number.
|
|
37
|
+
*/
|
|
38
|
+
typedef float complex stdlib_complex64_t;
|
|
39
|
+
|
|
40
|
+
// If we aren't going to use the native complex number type, we need to define a complex number as an "opaque" struct (here, "opaque" meaning type consumers should **not** be accessing the components directly, but only through dedicated functions) for storing the real and imaginary components...
|
|
41
|
+
#else
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* An opaque type definition for a single-precision complex floating-point number.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* stdlib_complex64_t z;
|
|
48
|
+
*
|
|
49
|
+
* // Set the real component:
|
|
50
|
+
* z.re = 5.0f;
|
|
51
|
+
*
|
|
52
|
+
* // Set the imaginary component:
|
|
53
|
+
* z.im = 2.0f;
|
|
54
|
+
*/
|
|
55
|
+
typedef struct {
|
|
56
|
+
/**
|
|
57
|
+
* Real component.
|
|
58
|
+
*/
|
|
59
|
+
float re;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Imaginary component.
|
|
63
|
+
*/
|
|
64
|
+
float im;
|
|
65
|
+
} stdlib_complex64_t;
|
|
66
|
+
|
|
67
|
+
#endif
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* An opaque type definition for a union for accessing the real and imaginary parts of a single-precision complex floating-point number.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* float realf( const stdlib_complex64_t z ) {
|
|
74
|
+
* stdlib_complex64_parts_t v;
|
|
75
|
+
*
|
|
76
|
+
* // Assign a single-precision complex floating-point number:
|
|
77
|
+
* v.value = z;
|
|
78
|
+
*
|
|
79
|
+
* // Extract the real component:
|
|
80
|
+
* float re = v.parts[ 0 ];
|
|
81
|
+
*
|
|
82
|
+
* return re;
|
|
83
|
+
* }
|
|
84
|
+
*
|
|
85
|
+
* // ...
|
|
86
|
+
*
|
|
87
|
+
* // Create a complex number:
|
|
88
|
+
* stdlib_complex64_t z = stdlib_complex64( 5.0f, 2.0f );
|
|
89
|
+
*
|
|
90
|
+
* // ...
|
|
91
|
+
*
|
|
92
|
+
* // Access the real component:
|
|
93
|
+
* float re = realf( z );
|
|
94
|
+
* // returns 5.0f
|
|
95
|
+
*/
|
|
96
|
+
typedef union {
|
|
97
|
+
// An opaque type for the output value (e.g., could be a `struct` or a C99 complex number):
|
|
98
|
+
stdlib_complex64_t value;
|
|
99
|
+
|
|
100
|
+
// Leverage the fact that C99 specifies that complex numbers have the same representation and alignment as a two-element array (see <https://en.cppreference.com/w/c/language/arithmetic_types#Complex_floating_types>), where the first element is the real component and the second element is the imaginary component, thus allowing us to create a complex number irrespective of its native data type (e.g., `struct` vs `float complex`):
|
|
101
|
+
float parts[ 2 ];
|
|
102
|
+
} stdlib_complex64_parts_t;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Returns a single-precision complex floating-point number.
|
|
106
|
+
*/
|
|
107
|
+
stdlib_complex64_t stdlib_complex64( const float real, const float imag );
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Converts a single-precision floating-point number to a single-precision complex floating-point number.
|
|
111
|
+
*/
|
|
112
|
+
stdlib_complex64_t stdlib_complex64_from_float32( const float real );
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Converts a double-precision floating-point number to a single-precision complex floating-point number.
|
|
116
|
+
*/
|
|
117
|
+
stdlib_complex64_t stdlib_complex64_from_float64( const double real );
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Converts (copies) a single-precision complex floating-point number to a single-precision complex floating-point number.
|
|
121
|
+
*/
|
|
122
|
+
stdlib_complex64_t stdlib_complex64_from_complex64( const stdlib_complex64_t z );
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Converts a signed 8-bit integer to a single-precision complex floating-point number.
|
|
126
|
+
*/
|
|
127
|
+
stdlib_complex64_t stdlib_complex64_from_int8( const int8_t real );
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Converts an unsigned 8-bit integer to a single-precision complex floating-point number.
|
|
131
|
+
*/
|
|
132
|
+
stdlib_complex64_t stdlib_complex64_from_uint8( const uint8_t real );
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Converts a signed 16-bit integer to a single-precision complex floating-point number.
|
|
136
|
+
*/
|
|
137
|
+
stdlib_complex64_t stdlib_complex64_from_int16( const int16_t real );
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Converts an unsigned 16-bit integer to a single-precision complex floating-point number.
|
|
141
|
+
*/
|
|
142
|
+
stdlib_complex64_t stdlib_complex64_from_uint16( const uint16_t real );
|
|
143
|
+
|
|
144
|
+
#ifdef __cplusplus
|
|
145
|
+
}
|
|
146
|
+
#endif
|
|
147
|
+
|
|
148
|
+
#endif // !STDLIB_COMPLEX_FLOAT32_H
|
package/lib/index.js
CHANGED
|
@@ -23,8 +23,8 @@
|
|
|
23
23
|
var isNumber = require( '@stdlib/assert-is-number' ).isPrimitive;
|
|
24
24
|
var defineProperty = require( '@stdlib/utils-define-property' );
|
|
25
25
|
var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );
|
|
26
|
-
var
|
|
27
|
-
var
|
|
26
|
+
var float64ToFloat32 = require( '@stdlib/number-float64-base-to-float32' );
|
|
27
|
+
var toStr = require( './tostring.js' );
|
|
28
28
|
var toJSON = require( './tojson.js' );
|
|
29
29
|
|
|
30
30
|
|
|
@@ -46,7 +46,6 @@ var toJSON = require( './tojson.js' );
|
|
|
46
46
|
* // returns <Complex64>
|
|
47
47
|
*/
|
|
48
48
|
function Complex64( real, imag ) {
|
|
49
|
-
var view;
|
|
50
49
|
if ( !( this instanceof Complex64 ) ) {
|
|
51
50
|
throw new TypeError( 'invalid invocation. Constructor must be called with the `new` keyword.' );
|
|
52
51
|
}
|
|
@@ -59,39 +58,16 @@ function Complex64( real, imag ) {
|
|
|
59
58
|
defineProperty( this, 're', {
|
|
60
59
|
'configurable': false,
|
|
61
60
|
'enumerable': true,
|
|
62
|
-
'
|
|
61
|
+
'writable': false,
|
|
62
|
+
'value': float64ToFloat32( real )
|
|
63
63
|
});
|
|
64
|
-
|
|
65
64
|
defineProperty( this, 'im', {
|
|
66
65
|
'configurable': false,
|
|
67
66
|
'enumerable': true,
|
|
68
|
-
'
|
|
67
|
+
'writable': false,
|
|
68
|
+
'value': float64ToFloat32( imag )
|
|
69
69
|
});
|
|
70
|
-
view = new Float32Array( 2 );
|
|
71
|
-
view[ 0 ] = real;
|
|
72
|
-
view[ 1 ] = imag;
|
|
73
|
-
|
|
74
70
|
return this;
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Returns a real component.
|
|
78
|
-
*
|
|
79
|
-
* @private
|
|
80
|
-
* @returns {number} real component
|
|
81
|
-
*/
|
|
82
|
-
function getReal() {
|
|
83
|
-
return view[ 0 ];
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Returns an imaginary component.
|
|
88
|
-
*
|
|
89
|
-
* @private
|
|
90
|
-
* @returns {number} imaginary component
|
|
91
|
-
*/
|
|
92
|
-
function getImag() {
|
|
93
|
-
return view[ 1 ];
|
|
94
|
-
}
|
|
95
71
|
}
|
|
96
72
|
|
|
97
73
|
/**
|
|
@@ -154,7 +130,7 @@ setReadOnly( Complex64.prototype, 'byteLength', 8 );
|
|
|
154
130
|
* var str = z.toString();
|
|
155
131
|
* // returns '5 + 3i'
|
|
156
132
|
*/
|
|
157
|
-
setReadOnly( Complex64.prototype, 'toString',
|
|
133
|
+
setReadOnly( Complex64.prototype, 'toString', toStr );
|
|
158
134
|
|
|
159
135
|
/**
|
|
160
136
|
* Serializes a complex number as a JSON object.
|
package/manifest.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"options": {},
|
|
3
|
+
"fields": [
|
|
4
|
+
{
|
|
5
|
+
"field": "src",
|
|
6
|
+
"resolve": true,
|
|
7
|
+
"relative": true
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
"field": "include",
|
|
11
|
+
"resolve": true,
|
|
12
|
+
"relative": true
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
"field": "libraries",
|
|
16
|
+
"resolve": false,
|
|
17
|
+
"relative": false
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
"field": "libpath",
|
|
21
|
+
"resolve": true,
|
|
22
|
+
"relative": false
|
|
23
|
+
}
|
|
24
|
+
],
|
|
25
|
+
"confs": [
|
|
26
|
+
{
|
|
27
|
+
"src": [
|
|
28
|
+
"./src/main.c"
|
|
29
|
+
],
|
|
30
|
+
"include": [
|
|
31
|
+
"./include"
|
|
32
|
+
],
|
|
33
|
+
"libraries": [],
|
|
34
|
+
"libpath": [],
|
|
35
|
+
"dependencies": []
|
|
36
|
+
}
|
|
37
|
+
]
|
|
38
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stdlib/complex-float32",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7",
|
|
4
4
|
"description": "64-bit complex number.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": {
|
|
@@ -37,10 +37,11 @@
|
|
|
37
37
|
"url": "https://github.com/stdlib-js/stdlib/issues"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@stdlib/array-float32": "^0.0.x",
|
|
41
40
|
"@stdlib/assert-is-number": "^0.0.x",
|
|
41
|
+
"@stdlib/number-float64-base-to-float32": "^0.0.x",
|
|
42
42
|
"@stdlib/utils-define-nonenumerable-read-only-property": "^0.0.x",
|
|
43
|
-
"@stdlib/utils-define-property": "^0.0.x"
|
|
43
|
+
"@stdlib/utils-define-property": "^0.0.x",
|
|
44
|
+
"@stdlib/utils-library-manifest": "^0.0.x"
|
|
44
45
|
},
|
|
45
46
|
"devDependencies": {
|
|
46
47
|
"@stdlib/assert-has-own-property": "^0.0.x",
|
package/src/main.c
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2021 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
|
+
#include "stdlib/complex/float32.h"
|
|
20
|
+
#include <stdint.h>
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Returns a single-precision complex floating-point number.
|
|
24
|
+
*
|
|
25
|
+
* @param real real component
|
|
26
|
+
* @param imag imaginary component
|
|
27
|
+
* @return single-precision complex floating-point number
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* stdlib_complex64_t z = stdlib_complex64( 5.0f, 2.0f );
|
|
31
|
+
*/
|
|
32
|
+
stdlib_complex64_t stdlib_complex64( const float real, const float imag ) {
|
|
33
|
+
stdlib_complex64_parts_t z;
|
|
34
|
+
z.parts[ 0 ] = real;
|
|
35
|
+
z.parts[ 1 ] = imag; // cppcheck-suppress unreadVariable
|
|
36
|
+
return z.value;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Converts a single-precision floating-point number to a single-precision complex floating-point number.
|
|
41
|
+
*
|
|
42
|
+
* @param real real component
|
|
43
|
+
* @return single-precision complex floating-point number
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* stdlib_complex64_t z = stdlib_complex64_from_float32( 5.0f );
|
|
47
|
+
*/
|
|
48
|
+
stdlib_complex64_t stdlib_complex64_from_float32( const float real ) {
|
|
49
|
+
stdlib_complex64_parts_t z;
|
|
50
|
+
z.parts[ 0 ] = real;
|
|
51
|
+
z.parts[ 1 ] = 0.0f; // cppcheck-suppress unreadVariable
|
|
52
|
+
return z.value;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Converts a double-precision floating-point number to a single-precision complex floating-point number.
|
|
57
|
+
*
|
|
58
|
+
* @param real real component
|
|
59
|
+
* @return single-precision complex floating-point number
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* stdlib_complex64_t z = stdlib_complex64_from_float64( 5.0 );
|
|
63
|
+
*/
|
|
64
|
+
stdlib_complex64_t stdlib_complex64_from_float64( const double real ) {
|
|
65
|
+
stdlib_complex64_parts_t z;
|
|
66
|
+
z.parts[ 0 ] = (float)real;
|
|
67
|
+
z.parts[ 1 ] = 0.0f; // cppcheck-suppress unreadVariable
|
|
68
|
+
return z.value;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Converts (copies) a single-precision complex floating-point number to a single-precision complex floating-point number.
|
|
73
|
+
*
|
|
74
|
+
* @param z single-precision complex floating-point number
|
|
75
|
+
* @return single-precision complex floating-point number
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* stdlib_complex64_t z1 = stdlib_complex64( 5.0f, 3.0f );
|
|
79
|
+
* stdlib_complex64_t z2 = stdlib_complex64_from_complex64( z1 );
|
|
80
|
+
*/
|
|
81
|
+
stdlib_complex64_t stdlib_complex64_from_complex64( const stdlib_complex64_t z ) {
|
|
82
|
+
stdlib_complex64_parts_t v1 = { z };
|
|
83
|
+
stdlib_complex64_parts_t v2;
|
|
84
|
+
v2.parts[ 0 ] = v1.parts[ 0 ];
|
|
85
|
+
v2.parts[ 1 ] = v1.parts[ 1 ]; // cppcheck-suppress unreadVariable
|
|
86
|
+
return v2.value;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Converts a signed 8-bit integer to a single-precision complex floating-point number.
|
|
91
|
+
*
|
|
92
|
+
* @param real real component
|
|
93
|
+
* @return single-precision complex floating-point number
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* stdlib_complex64_t z = stdlib_complex64_from_int8( 5 );
|
|
97
|
+
*/
|
|
98
|
+
stdlib_complex64_t stdlib_complex64_from_int8( const int8_t real ) {
|
|
99
|
+
stdlib_complex64_parts_t z;
|
|
100
|
+
z.parts[ 0 ] = (float)real;
|
|
101
|
+
z.parts[ 1 ] = 0.0f; // cppcheck-suppress unreadVariable
|
|
102
|
+
return z.value;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Converts an unsigned 8-bit integer to a single-precision complex floating-point number.
|
|
107
|
+
*
|
|
108
|
+
* @param real real component
|
|
109
|
+
* @return single-precision complex floating-point number
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* stdlib_complex64_t z = stdlib_complex64_from_uint8( 5 );
|
|
113
|
+
*/
|
|
114
|
+
stdlib_complex64_t stdlib_complex64_from_uint8( const uint8_t real ) {
|
|
115
|
+
stdlib_complex64_parts_t z;
|
|
116
|
+
z.parts[ 0 ] = (float)real;
|
|
117
|
+
z.parts[ 1 ] = 0.0f; // cppcheck-suppress unreadVariable
|
|
118
|
+
return z.value;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Converts a signed 16-bit integer to a single-precision complex floating-point number.
|
|
123
|
+
*
|
|
124
|
+
* @param real real component
|
|
125
|
+
* @return single-precision complex floating-point number
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* stdlib_complex64_t z = stdlib_complex64_from_int16( 5 );
|
|
129
|
+
*/
|
|
130
|
+
stdlib_complex64_t stdlib_complex64_from_int16( const int16_t real ) {
|
|
131
|
+
stdlib_complex64_parts_t z;
|
|
132
|
+
z.parts[ 0 ] = (float)real;
|
|
133
|
+
z.parts[ 1 ] = 0.0f; // cppcheck-suppress unreadVariable
|
|
134
|
+
return z.value;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Converts an unsigned 16-bit integer to a single-precision complex floating-point number.
|
|
139
|
+
*
|
|
140
|
+
* @param real real component
|
|
141
|
+
* @return single-precision complex floating-point number
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* stdlib_complex64_t z = stdlib_complex64_from_uint16( 5 );
|
|
145
|
+
*/
|
|
146
|
+
stdlib_complex64_t stdlib_complex64_from_uint16( const uint16_t real ) {
|
|
147
|
+
stdlib_complex64_parts_t z;
|
|
148
|
+
z.parts[ 0 ] = (float)real;
|
|
149
|
+
z.parts[ 1 ] = 0.0f; // cppcheck-suppress unreadVariable
|
|
150
|
+
return z.value;
|
|
151
|
+
}
|