Skip to content

Commit

Permalink
feat: add assign API to cpolar
Browse files Browse the repository at this point in the history
  • Loading branch information
steff456 committed Jul 24, 2023
1 parent 462a99b commit 4e0b556
Show file tree
Hide file tree
Showing 11 changed files with 459 additions and 53 deletions.
31 changes: 24 additions & 7 deletions lib/node_modules/@stdlib/math/base/special/cpolar/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,23 @@ var o = cpolar( new Complex128( 5.0, 3.0 ) );
// returns [ ~5.83, ~0.5404 ]
```

#### cpolar.assign( z, out, stride, offset )

Computes the [absolute value][@stdlib/math/base/special/cabs] and [phase][@stdlib/math/base/special/cphase] of a double-precision complex floating-point number and assigns results to a provided output array.

```javascript
var Complex128 = require( '@stdlib/complex/float64' );
var Float64Array = require( '@stdlib/array/float64' );

var out = new Float64Array( 2 );

var v = cpolar.assign( new Complex128( 5.0, 3.0 ), out, 1, 0 );
// returns <Float64Array>[ ~5.83, ~0.5404 ]

var bool = ( v === out );
// returns true
```

</section>

<!-- /.usage -->
Expand Down Expand Up @@ -111,7 +128,7 @@ for ( i = 0; i < 100; i++ ) {
#include "stdlib/math/base/special/cpolar.h"
```

#### stdlib_base_cpolar( z )
#### stdlib_base_cpolar( z, abs, cphase )

Computes the [absolute value][@stdlib/math/base/special/cabs] and [phase][@stdlib/math/base/special/cphase] of a double-precision complex floating-point number.

Expand Down Expand Up @@ -168,11 +185,11 @@ int main( void ) {
stdlib_complex128( 0.0/0.0, 0.0/0.0 )
};

int i;
double cphase;
double re;
double im;
double abs;
double cphase;
int i;
for ( i = 0; i < 12; i++ ) {
stdlib_base_cpolar( x[i], &abs, &cphase );
stdlib_reim( x[i], &re, &im );
Expand Down Expand Up @@ -206,14 +223,14 @@ int main( void ) {
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
<section class="links">
<!-- <related-links> -->
[@stdlib/math/base/special/cabs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/cabs
[@stdlib/math/base/special/cphase]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/cphase
<section class="links">
<!-- <related-links> -->
<!-- </related-links> -->
</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,28 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var uniform = require( '@stdlib/random/base/uniform' );
var isArray = require( '@stdlib/assert/is-array' );
var Complex128 = require( '@stdlib/complex/float64' );
var pkg = require( './../package.json' ).name;
var cpolar = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var re;
var im;
var values;
var y;
var i;

values = [
new Complex128( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) ),
new Complex128( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) )
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
re = ( randu()*1000.0 ) - 500.0;
im = ( randu()*1000.0 ) - 500.0;
y = cpolar( re, im );
y = cpolar( values[ i%values.length ] );
if ( y.length === 0 ) {
b.fail( 'should not be empty' );
}
Expand All @@ -51,3 +54,30 @@ bench( pkg, function benchmark( b ) {
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+':assign', function benchmark( b ) {
var values;
var y;
var i;

values = [
new Complex128( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) ),
new Complex128( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) )
];

y = [ 0.0, 0.0 ];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
cpolar.assign( values[ i%values.length ], y, 1, 0 );
if ( y.length === 0 ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( !isArray( y ) ) {
b.fail( 'should return an array' );
}
b.pass( 'benchmark finished' );
b.end();
});
33 changes: 32 additions & 1 deletion lib/node_modules/@stdlib/math/base/special/cpolar/docs/repl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,37 @@
> var out = {{alias}}( new {{alias:@stdlib/complex/float64}}( 5.0, 3.0 ) )
[ ~5.83, ~0.5404 ]

See Also

{{alias}}.assign( z, out, stride, offset )
Returns the absolute value and phase of a double-precision complex
floating-point number and assigns results to a provided output array.

Parameters
----------
z: Complex128
Complex number.

out: Array<number>
Destination array.

stride: integer
Output array stride.

offset: integer
Output array index offset.

Returns
-------
out: Array<number>
Absolute value and phase, respectively.

Examples
--------
> var out = new {{alias:@stdlib/array/float64}}( 2 );
> var v = {{alias}}.assign( new {{alias:@stdlib/complex/float64}}( 5.0, 3.0 ), out, 1, 0 )
<Float64Array>[ ~5.83, ~0.5404 ]
> var bool = ( v === out )
true

See Also
--------
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,49 @@

/// <reference types="@stdlib/types"/>

import { ArrayLike } from '@stdlib/types/array';
import { Complex128 } from '@stdlib/types/object';
import { Complex128, Collection } from '@stdlib/types/object';

/**
* Interface describing `cpolar`.
*/
interface Cpolar {
/**
* Computes the absolute value and the phase of a double-precision complex floating-point number.
*
* @param z - complex number
* @returns absolute value and phase, respectively
*
* @example
* var Complex128 = require( `@stdlib/complex/float64` );
*
* var v = cpolar( new Complex128( 5.0, 3.0 ) );
* // returns [ ~5.83, ~0.5404 ]
*/
( z: Complex128 ): Array<number>;

/**
* Computes the absolute value and the phase of a double-precision complex floating-point number and assigns results to a provided output array.
*
* @param z - complex number
* @param out - output array
* @param stride - output array stride
* @param offset - output array index offset
* @returns output array
*
* @example
* var Complex128 = require( `@stdlib/complex/float64` );
* var Float64Array = require( `@stdlib/array/float64` );
*
* var out = new Float64Array( 2 );
*
* var v = cpolar.assign( new Complex128( 5.0, 3.0 ), out, 1, 0 );
* // returns <Float64Array>[ ~5.83, ~0.5404 ]
*
* var bool = ( v === out );
* // returns true
*/
assign( z: Complex128, out: Collection, stride: number, offset: number ): Collection; // tslint-disable-line max-line-length
}

/**
* Computes the absolute value and the phase of a double-precision complex floating-point number.
Expand All @@ -35,7 +76,7 @@ import { Complex128 } from '@stdlib/types/object';
* var v = cpolar( new Complex128( 5.0, 3.0 ) );
* // returns [ ~5.83, ~0.5404 ]
*/
declare function cpolar( z: Complex128 ): ArrayLike<number>;
declare var cpolar: Cpolar;


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import cpolar = require( './index' );

// The function returns an array of numbers...
{
cpolar( new Complex128( 5.0, 3.0 ) ); // $ExpectType ArrayLike<number>
cpolar( new Complex128( 5.0, 3.0 ) ); // $ExpectType number[]
}

// The compiler throws an error if the function is provided a complex number...
Expand All @@ -43,3 +43,69 @@ import cpolar = require( './index' );
{
cpolar(); // $ExpectError
}

// Attached to the main export is an `assign` method which returns an array-like object containing numbers...
{
const out = [ 0.0, 0.0 ];

cpolar.assign( new Complex128( 5.0, 3.0 ), out, 1, 0 ); // $ExpectType Collection
}

// The compiler throws an error if the `assign` method is provided a first argument which is not a number...
{
const out = [ 0.0, 0.0 ];

cpolar.assign( true, out, 1, 0 ); // $ExpectError
cpolar.assign( false, out, 1, 0 ); // $ExpectError
cpolar.assign( '5', out, 1, 0 ); // $ExpectError
cpolar.assign( null, out, 1, 0 ); // $ExpectError
cpolar.assign( [], out, 1, 0 ); // $ExpectError
cpolar.assign( {}, out, 1, 0 ); // $ExpectError
cpolar.assign( ( x: number ): number => x, out, 1, 0 ); // $ExpectError
}

// The compiler throws an error if the `assign` method is provided a second argument which is not an array-like object...
{
cpolar.assign( new Complex128( 5.0, 3.0 ), 1, 1, 0 ); // $ExpectError
cpolar.assign( new Complex128( 5.0, 3.0 ), true, 1, 0 ); // $ExpectError
cpolar.assign( new Complex128( 5.0, 3.0 ), false, 1, 0 ); // $ExpectError
cpolar.assign( new Complex128( 5.0, 3.0 ), null, 1, 0 ); // $ExpectError
cpolar.assign( new Complex128( 5.0, 3.0 ), {}, 1, 0 ); // $ExpectError
}

// The compiler throws an error if the `assign` method is provided a third argument which is not a number...
{
const out = [ 0.0, 0 ];

cpolar.assign( new Complex128( 5.0, 3.0 ), out, '5', 0 ); // $ExpectError
cpolar.assign( new Complex128( 5.0, 3.0 ), out, true, 0 ); // $ExpectError
cpolar.assign( new Complex128( 5.0, 3.0 ), out, false, 0 ); // $ExpectError
cpolar.assign( new Complex128( 5.0, 3.0 ), out, null, 0 ); // $ExpectError
cpolar.assign( new Complex128( 5.0, 3.0 ), out, [], 0 ); // $ExpectError
cpolar.assign( new Complex128( 5.0, 3.0 ), out, {}, 0 ); // $ExpectError
cpolar.assign( new Complex128( 5.0, 3.0 ), out, ( x: number ): number => x, 0 ); // $ExpectError
}

// The compiler throws an error if the `assign` method is provided a fourth argument which is not a number...
{
const out = [ 0.0, 0 ];

cpolar.assign( new Complex128( 5.0, 3.0 ), out, 1, '5' ); // $ExpectError
cpolar.assign( new Complex128( 5.0, 3.0 ), out, 1, true ); // $ExpectError
cpolar.assign( new Complex128( 5.0, 3.0 ), out, 1, false ); // $ExpectError
cpolar.assign( new Complex128( 5.0, 3.0 ), out, 1, null ); // $ExpectError
cpolar.assign( new Complex128( 5.0, 3.0 ), out, 1, [] ); // $ExpectError
cpolar.assign( new Complex128( 5.0, 3.0 ), out, 1, {} ); // $ExpectError
cpolar.assign( new Complex128( 5.0, 3.0 ), out, 1, ( x: number ): number => x ); // $ExpectError
}

// The compiler throws an error if the `assign` method is provided an unsupported number of arguments...
{
const out = [ 0.0, 0 ];

cpolar.assign(); // $ExpectError
cpolar.assign( new Complex128( 5.0, 3.0 ) ); // $ExpectError
cpolar.assign( new Complex128( 5.0, 3.0 ), out ); // $ExpectError
cpolar.assign( new Complex128( 5.0, 3.0 ), out, 1 ); // $ExpectError
cpolar.assign( new Complex128( 5.0, 3.0 ), out, 1, 0, 1 ); // $ExpectError
}
54 changes: 54 additions & 0 deletions lib/node_modules/@stdlib/math/base/special/cpolar/lib/assign.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2023 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var cabs = require( '@stdlib/math/base/special/cabs' );
var cphase = require( '@stdlib/math/base/special/cphase' );


// MAIN //

/**
* Computes the absolute value and the phase of a double-precision complex floating-point number.
*
* @private
* @param {Complex128} z - complex number
* @param {Collection} out - output array
* @param {integer} stride - output array stride
* @param {NonNegativeInteger} offset - output array index offset
* @returns {Collection} absolute value and phase (in radians)
*
* @example
* var Complex128 = require( '@stdlib/complex/float64' );
*
* var o = cpolar( new Complex128( 5.0, 3.0 ), [ 0.0, 0.0 ], 1, 0 );
* // returns [ ~5.83, ~0.5404 ]
*/
function cpolar( z, out, stride, offset ) {
out[ offset ] = cabs( z );
out[ offset+stride ] = cphase( z );
return out;
}


// EXPORTS //

module.exports = cpolar;
20 changes: 20 additions & 0 deletions lib/node_modules/@stdlib/math/base/special/cpolar/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,31 @@
*
* var o = cpolar( new Complex128( 5.0, 3.0 ) );
* // returns [ ~5.83, ~0.5404 ]
*
* @example
* var Complex128 = require( '@stdlib/complex/float64' );
* var cpolar = require( '@stdlib/math/base/special/cpolar' );
* var Float64Array = require( '@stdlib/array/float64' );
*
* var out = new Float64Array( 2 );
*
* var v = cpolar.assign( new Complex128( 5.0, 3.0 ), out, 1, 0 );
* // returns <Float64Array>[ ~5.83, ~0.5404 ]
*
* var bool = ( v === out );
* // returns true
*/

// MODULES //

var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
var main = require( './main.js' );
var assign = require( './assign.js' );


// MAIN //

setReadOnly( main, 'assign', assign );


// EXPORTS //
Expand Down
Loading

0 comments on commit 4e0b556

Please sign in to comment.