Skip to content

Commit

Permalink
feat: add reduce method to array/complex64
Browse files Browse the repository at this point in the history
PR-URL: 	#1271
Co-authored-by: Athan Reines <kgryte@gmail.com>
Reviewed-by: Athan Reines <kgryte@gmail.com> 
Signed-off-by: Athan Reines <kgryte@gmail.com>
  • Loading branch information
Jaysukh-409 and kgryte authored Feb 24, 2024
1 parent aed166e commit f3def6d
Show file tree
Hide file tree
Showing 6 changed files with 570 additions and 0 deletions.
54 changes: 54 additions & 0 deletions lib/node_modules/@stdlib/array/complex64/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1622,6 +1622,60 @@ var count = context.count;
// returns 3
```

<a name="method-reduce"></a>

#### Complex64Array.prototype.reduce( reducerFn\[, initialValue] )

Applies a provided callback function to each element of the array, in order, passing in the return value from the calculation on the preceding element and returning the accumulated result upon completion.

```javascript
var realf = require( '@stdlib/complex/realf' );
var imagf = require( '@stdlib/complex/imagf' );
var caddf = require( '@stdlib/math/base/ops/caddf' );

var arr = new Complex64Array( 3 );

arr.set( [ 1.0, 1.0 ], 0 );
arr.set( [ 2.0, 2.0 ], 1 );
arr.set( [ 3.0, 3.0 ], 2 );

var z = arr.reduce( caddf );
// returns <Complex64>

var re = realf( z );
// returns 6.0

var im = imagf( z );
// returns 6.0
```

The reducer function is provided four arguments:

- **acc**: accumulated result.
- **value**: current array element.
- **index**: current array element index.
- **arr**: the array on which this method was called.

By default, the function initializes the accumulated result to the first element in the array and passes the second array element as `value` during the first invocation of the provided callback. To begin accumulation from a different starting value and pass in the first array element as `value` during the first invocation of the provided callback, provide an `initialValue` argument.

```javascript
var realf = require( '@stdlib/complex/realf' );

function reducer( acc, v ) {
acc += realf( v );
return acc;
}

var arr = new Complex64Array( 3 );

arr.set( [ 1.0, 1.0 ], 0 );
arr.set( [ 2.0, 2.0 ], 1 );
arr.set( [ 3.0, 3.0 ], 2 );

var z = arr.reduce( reducer, 0.0 );
// returns 6.0
```

<a name="method-reverse"></a>

#### Complex64Array.prototype.reverse()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @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 bench = require( '@stdlib/bench' );
var caddf = require( '@stdlib/math/base/ops/caddf' );
var isComplexLike = require('@stdlib/assert/is-complex-like');
var pkg = require( './../package.json' ).name;
var Complex64Array = require( './../lib' );


// MAIN //

bench( pkg+':reduce', function benchmark( b ) {
var out;
var arr;
var i;

arr = new Complex64Array( [ 1, 2, 3, 4, 5, 6 ] );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = arr.reduce( caddf );
if ( typeof out !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( !isComplexLike( out ) ) {
b.fail( 'should return a complex number' );
}
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
* @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 bench = require( '@stdlib/bench' );
var pow = require( '@stdlib/math/base/special/pow' );
var caddf = require( '@stdlib/math/base/ops/caddf' );
var isComplexLike = require('@stdlib/assert/is-complex-like');
var Complex64 = require( '@stdlib/complex/float32' );
var pkg = require( './../package.json' ).name;
var Complex64Array = require( './../lib' );


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var arr;
var i;

arr = [];
for ( i = 0; i < len; i++ ) {
arr.push( new Complex64( i, i ) );
}
arr = new Complex64Array( arr );

return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var out;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = arr.reduce( caddf );
if ( typeof out !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( !isComplexLike( out ) ) {
b.fail( 'should return a complex number' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var len;
var min;
var max;
var f;
var i;

min = 1; // 10^min
max = 6; // 10^max

for ( i = min; i <= max; i++ ) {
len = pow( 10, i );
f = createBenchmark( len );
bench( pkg+':reduce:len='+len, f );
}
}

main();
83 changes: 83 additions & 0 deletions lib/node_modules/@stdlib/array/complex64/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,60 @@ type TernaryMapFcn<U> = ( this: U, value: Complex64, index: number, arr: Complex
*/
type MapFcn<U> = NullaryMapFcn<U> | UnaryMapFcn<U> | BinaryMapFcn<U> | TernaryMapFcn<U>;

/**
* Reducer function invoked for each element in an array.
*
* @returns accumulated result
*/
type NullaryReducer<U> = () => U;

/**
* Reducer function invoked for each element in an array.
*
* @param acc - accumulated result
* @returns accumulated result
*/
type UnaryReducer<U> = ( acc: U ) => U;

/**
* Reducer function invoked for each element in an array.
*
* @param acc - accumulated result
* @param value - current array element
* @returns accumulated result
*/
type BinaryReducer<U> = ( acc: U, value: Complex64 ) => U;

/**
* Reducer function invoked for each element in an array.
*
* @param acc - accumulated result
* @param value - current array element
* @param index - current array element index
* @returns accumulated result
*/
type TernaryReducer<U> = ( acc: U, value: Complex64, index: number ) => U;

/**
* @param acc - accumulated result
* @param value - current array element
* @param index - current array element index
* @param arr - array on which the method was called
* @returns accumulated result
*/
type QuaternaryReducer<U> = ( acc: U, value: Complex64, index: number, arr: Complex64Array ) => U;

/**
* Reducer function invoked for each element in an array.
*
* @param acc - accumulated result
* @param value - current array element
* @param index - current array element index
* @param arr - array on which the method was called
* @returns accumulated result
*/
type Reducer<U> = NullaryReducer<U> | UnaryReducer<U> | BinaryReducer<U> | TernaryReducer<U> | QuaternaryReducer<U>;

/**
* Class for creating a 64-bit complex number array.
*/
Expand Down Expand Up @@ -813,6 +867,35 @@ declare class Complex64Array implements Complex64ArrayInterface {
*/
map<U = unknown>( fcn: MapFcn<U>, thisArg?: ThisParameterType<MapFcn<U>> ): Complex64Array;

/**
* Applies a provided callback function to each element of the array, in order, passing in the return value from the calculation on the preceding element and returning the accumulated result upon completion.
*
* @param reducer - callback function
* @param initialValue - initial value
* @returns accumulated result
*
* @example
* var realf = require( '@stdlib/complex/realf' );
* var imagf = require( '@stdlib/complex/imagf' );
* var caddf = require( '@stdlib/math/base/ops/caddf' );
*
* var arr = new Complex64Array( 3 );
*
* arr.set( [ 1.0, 1.0 ], 0 );
* arr.set( [ 2.0, 2.0 ], 1 );
* arr.set( [ 3.0, 3.0 ], 2 );
*
* var z = arr.reduce( caddf );
* // returns <Complex64>
*
* var re = realf( z );
* // returns 6.0
*
* var im = imagf( z );
* // returns 6.0
*/
reduce<U = unknown>( reducer: Reducer<U>, initialValue?: U ): U;

/**
* Reverses an array in-place.
*
Expand Down
65 changes: 65 additions & 0 deletions lib/node_modules/@stdlib/array/complex64/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1699,6 +1699,71 @@ setReadOnly( Complex64Array.prototype, 'map', function map( fcn, thisArg ) {
return out;
});

/**
* Applies a provided callback function to each element of the array, in order, passing in the return value from the calculation on the preceding element and returning the accumulated result upon completion.
*
* @name reduce
* @memberof Complex64Array.prototype
* @type {Function}
* @param {Function} reducer - callback function
* @param {*} [initialValue] - initial value
* @throws {TypeError} `this` must be a complex number array
* @throws {TypeError} first argument must be a function
* @throws {Error} if not provided an initial value, the array must have at least one element
* @returns {*} accumulated result
*
* @example
* var realf = require( '@stdlib/complex/realf' );
* var imagf = require( '@stdlib/complex/imagf' );
* var caddf = require( '@stdlib/math/base/ops/caddf' );
*
* var arr = new Complex64Array( 3 );
*
* arr.set( [ 1.0, 1.0 ], 0 );
* arr.set( [ 2.0, 2.0 ], 1 );
* arr.set( [ 3.0, 3.0 ], 2 );
*
* var z = arr.reduce( caddf );
* // returns <Complex64>
*
* var re = realf( z );
* // returns 6.0
*
* var im = imagf( z );
* // returns 6.0
*/
setReadOnly( Complex64Array.prototype, 'reduce', function reduce( reducer, initialValue ) {
var buf;
var acc;
var len;
var v;
var i;

if ( !isComplexArray( this ) ) {
throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
}
if ( !isFunction( reducer ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', reducer ) );
}
buf = this._buffer;
len = this._length;
if ( arguments.length > 1 ) {
acc = initialValue;
i = 0;
} else {
if ( len === 0 ) {
throw new Error( 'invalid operation. If not provided an initial value, an array must contain at least one element.' );
}
acc = getComplex64( buf, 0 );
i = 1;
}
for ( ; i < len; i++ ) {
v = getComplex64( buf, i );
acc = reducer( acc, v, i, this );
}
return acc;
});

/**
* Reverses an array in-place.
*
Expand Down
Loading

1 comment on commit f3def6d

@stdlib-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage Report

Package Statements Branches Functions Lines
array/complex64 $\color{red}2787/2794$
$\color{green}+99.75\%$
$\color{red}466/472$
$\color{green}+98.73\%$
$\color{green}42/42$
$\color{green}+100.00\%$
$\color{red}2787/2794$
$\color{green}+99.75\%$

The above coverage report was generated for the changes in this push.

Please sign in to comment.