From 462a99b320560d80db5d1e772b6c5a11ed76c5a1 Mon Sep 17 00:00:00 2001 From: Stephannie Jimenez Date: Fri, 21 Jul 2023 12:37:55 -0500 Subject: [PATCH 01/15] feat: remove out keyword, add C implementation and addon to cpolar API --- .../math/base/special/cpolar/README.md | 136 +++++++++++--- .../special/cpolar/benchmark/benchmark.js | 26 --- .../cpolar/benchmark/benchmark.native.js | 65 +++++++ .../cpolar/benchmark/c/native/Makefile | 146 +++++++++++++++ .../cpolar/benchmark/c/native/benchmark.c | 144 +++++++++++++++ .../math/base/special/cpolar/binding.gyp | 170 ++++++++++++++++++ .../math/base/special/cpolar/docs/repl.txt | 26 +-- .../base/special/cpolar/docs/types/index.d.ts | 31 +--- .../base/special/cpolar/docs/types/test.ts | 45 ++--- .../base/special/cpolar/examples/c/Makefile | 146 +++++++++++++++ .../base/special/cpolar/examples/c/example.c | 43 +++++ .../base/special/cpolar/examples/index.js | 4 +- .../math/base/special/cpolar/include.gypi | 53 ++++++ .../include/stdlib/math/base/special/cpolar.h | 40 +++++ .../math/base/special/cpolar/lib/cpolar.js | 60 ------- .../math/base/special/cpolar/lib/index.js | 16 +- .../math/base/special/cpolar/lib/main.js | 29 +-- .../math/base/special/cpolar/lib/native.js | 52 ++++++ .../math/base/special/cpolar/manifest.json | 81 +++++++++ .../math/base/special/cpolar/package.json | 3 +- .../math/base/special/cpolar/src/Makefile | 70 ++++++++ .../math/base/special/cpolar/src/addon.c | 140 +++++++++++++++ .../math/base/special/cpolar/src/main.c | 47 +++++ .../math/base/special/cpolar/test/test.js | 62 +------ .../base/special/cpolar/test/test.native.js | 76 ++++++++ 25 files changed, 1428 insertions(+), 283 deletions(-) create mode 100644 lib/node_modules/@stdlib/math/base/special/cpolar/benchmark/benchmark.native.js create mode 100644 lib/node_modules/@stdlib/math/base/special/cpolar/benchmark/c/native/Makefile create mode 100644 lib/node_modules/@stdlib/math/base/special/cpolar/benchmark/c/native/benchmark.c create mode 100644 lib/node_modules/@stdlib/math/base/special/cpolar/binding.gyp create mode 100644 lib/node_modules/@stdlib/math/base/special/cpolar/examples/c/Makefile create mode 100644 lib/node_modules/@stdlib/math/base/special/cpolar/examples/c/example.c create mode 100644 lib/node_modules/@stdlib/math/base/special/cpolar/include.gypi create mode 100644 lib/node_modules/@stdlib/math/base/special/cpolar/include/stdlib/math/base/special/cpolar.h delete mode 100644 lib/node_modules/@stdlib/math/base/special/cpolar/lib/cpolar.js create mode 100644 lib/node_modules/@stdlib/math/base/special/cpolar/lib/native.js create mode 100644 lib/node_modules/@stdlib/math/base/special/cpolar/manifest.json create mode 100644 lib/node_modules/@stdlib/math/base/special/cpolar/src/Makefile create mode 100644 lib/node_modules/@stdlib/math/base/special/cpolar/src/addon.c create mode 100644 lib/node_modules/@stdlib/math/base/special/cpolar/src/main.c create mode 100644 lib/node_modules/@stdlib/math/base/special/cpolar/test/test.native.js diff --git a/lib/node_modules/@stdlib/math/base/special/cpolar/README.md b/lib/node_modules/@stdlib/math/base/special/cpolar/README.md index 6f16127ee00..661f84b1c2d 100644 --- a/lib/node_modules/@stdlib/math/base/special/cpolar/README.md +++ b/lib/node_modules/@stdlib/math/base/special/cpolar/README.md @@ -18,9 +18,9 @@ limitations under the License. --> -# polar +# cpolar -> Compute the [absolute value][@stdlib/math/base/special/cabs] and [phase][@stdlib/math/base/special/cphase] of a complex number. +> Compute the [absolute value][@stdlib/math/base/special/cabs] and [phase][@stdlib/math/base/special/cphase] of a double-precision complex floating-point number.
@@ -36,27 +36,15 @@ limitations under the License. var cpolar = require( '@stdlib/math/base/special/cpolar' ); ``` -#### cpolar( \[out,] re, im ) +#### cpolar( z ) -Computes the [absolute value][@stdlib/math/base/special/cabs] and [phase][@stdlib/math/base/special/cphase] of a complex number comprised of a **real** component `re` and an **imaginary** component `im`. +Computes the [absolute value][@stdlib/math/base/special/cabs] and [phase][@stdlib/math/base/special/cphase] of a double-precision complex floating-point number. ```javascript -var o = cpolar( 5.0, 3.0 ); -// returns [ ~5.83, ~0.5404 ] -``` - -By default, the function returns real and imaginary components as a two-element `array`. To avoid unnecessary memory allocation, the function supports providing an output (destination) object. - -```javascript -var Float64Array = require( '@stdlib/array/float64' ); - -var out = new Float64Array( 2 ); - -var o = cpolar( out, 5.0, 3.0 ); -// returns [ ~5.83, ~0.5404 ] +var Complex128 = require( '@stdlib/complex/float64' ); -var bool = ( o === out ); -// returns true +var o = cpolar( new Complex128( 5.0, 3.0 ) ); +// returns [ ~5.83, ~0.5404 ] ```
@@ -87,7 +75,7 @@ for ( i = 0; i < 100; i++ ) { re = round( randu()*100.0 ) - 50.0; im = round( randu()*100.0 ) - 50.0; z = new Complex128( re, im ); - o = cpolar( real(z), imag(z) ); + o = cpolar( z ); z = z.toString(); console.log( 'abs(%s) = %d. arg(%s) = %d', z, o[0], z, o[1] ); } @@ -97,6 +85,110 @@ for ( i = 0; i < 100; i++ ) { + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/math/base/special/cpolar.h" +``` + +#### stdlib_base_cpolar( z ) + +Computes the [absolute value][@stdlib/math/base/special/cabs] and [phase][@stdlib/math/base/special/cphase] of a double-precision complex floating-point number. + +```c +#include "stdlib/complex/float64.h" +#include "stdlib/complex/real.h" +#include "stdlib/complex/imag.h" + +stdlib_complex128_t z = stdlib_complex128( 5.0, 3.0 ); +double abs; +double cphase; +stdlib_base_cpolar( z, &abs, &cphase ); +``` + +The function accepts the following arguments: + +- **z**: `[in] stdlib_complex128_t` input value. +- **abs**: `[out] double*` destination for the absolute value. +- **cphase**: `[out] double*` destination for the phase value in radians. + +```c +double stdlib_base_cpolar( const stdlib_complex128_t z, double *abs, double *cphase ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/math/base/special/cpolar.h" +#include "stdlib/complex/float64.h" +#include "stdlib/complex/reim.h" +#include + +int main( void ) { + const stdlib_complex128_t x[] = { + stdlib_complex128( 3.14, 1.0 ), + stdlib_complex128( -3.14, -1.0 ), + stdlib_complex128( 0.0, 0.0 ), + stdlib_complex128( 0.0/0.0, 0.0/0.0 ) + }; + + int i; + double re; + double im; + double abs; + double cphase; + for ( i = 0; i < 12; i++ ) { + stdlib_base_cpolar( x[i], &abs, &cphase ); + stdlib_reim( x[i], &re, &im ); + printf( "cpolar(%lf + %lfi) => abs: %lf, cphase: %lf\n", re, im, abs, cphase ); + } +} +``` + +
+ + + +
+ + +