Skip to content

Commit

Permalink
Auto-generated commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stdlib-bot committed Feb 27, 2024
1 parent 4e83641 commit 68b4a9e
Show file tree
Hide file tree
Showing 11 changed files with 538 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ branches.md
.postinstall.json
Makefile

# Ignore `binding.gyp` file to avoid compilation of native addon when installing package:
# Ignore files to avoid compilation of native addon when installing package:
binding.gyp
include.gypi

# Directories #
###############
Expand Down
4 changes: 4 additions & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Dominik Moritz <[email protected]>
Dorrin Sotoudeh <[email protected]>
Frank Kovacs <[email protected]>
GUNJ JOSHI <[email protected]>
Golden <[email protected]>
Harshita Kalani <[email protected]>
James Gelok <[email protected]>
Jaysukh Makvana <[email protected]>
Expand All @@ -24,6 +25,7 @@ Jordan Gallivan <[email protected]>
Joris Labie <[email protected]>
Justin Dennison <[email protected]>
Karthik Prakash <[email protected]>
Khaldon <[email protected]>
Marcus Fantham <[email protected]>
Matt Cochrane <[email protected]>
Milan Raj <[email protected]>
Expand All @@ -34,9 +36,11 @@ Ognjen Jevremović <[email protected]>
Philipp Burckhardt <[email protected]>
Prajwal Kulkarni <[email protected]>
Pranav Goswami <[email protected]>
Pratik <[email protected]>
Ricky Reusser <[email protected]>
Robert Gislason <[email protected]>
Roman Stetsyk <[email protected]>
Rutam <[email protected]>
Ryan Seal <[email protected]>
Seyyed Parsa Neshaei <[email protected]>
Shraddheya Shendre <[email protected]>
Expand Down
58 changes: 56 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1709,6 +1709,60 @@ var z = arr.reduce( reducer, 0.0 );
// returns 6.0
```

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

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

Applies a provided callback function to each element of the array, in reverse order, passing in the return value from the calculation on the following 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.reduceRight( 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 last element in the array and passes the second-last array element as `value` during the first invocation of the provided callback. To begin accumulation from a different starting value and pass in the last 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.reduceRight( reducer, 0.0 );
// returns 6.0
```

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

#### Complex64Array.prototype.reverse()
Expand Down Expand Up @@ -2366,8 +2420,8 @@ Copyright &copy; 2016-2024. The Stdlib [Authors][stdlib-authors].
[npm-image]: http://img.shields.io/npm/v/@stdlib/array-complex64.svg
[npm-url]: https://npmjs.org/package/@stdlib/array-complex64

[test-image]: https://github.com/stdlib-js/array-complex64/actions/workflows/test.yml/badge.svg?branch=v0.2.1
[test-url]: https://github.com/stdlib-js/array-complex64/actions/workflows/test.yml?query=branch:v0.2.1
[test-image]: https://github.com/stdlib-js/array-complex64/actions/workflows/test.yml/badge.svg?branch=main
[test-url]: https://github.com/stdlib-js/array-complex64/actions/workflows/test.yml?query=branch:main

[coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/array-complex64/main.svg
[coverage-url]: https://codecov.io/github/stdlib-js/array-complex64?branch=main
Expand Down
52 changes: 52 additions & 0 deletions benchmark/benchmark.reduce_right.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 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-harness' );
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+':reduceRight', 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.reduceRight( 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();
});
104 changes: 104 additions & 0 deletions benchmark/benchmark.reduce_right.length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 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-harness' );
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.reduceRight( 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+':reduceRight:len='+len, f );
}
}

main();
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/index.js.map

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,35 @@ declare class Complex64Array implements Complex64ArrayInterface {
*/
reduce<U = unknown>( reducer: Reducer<U>, initialValue?: U ): U;

/**
* Applies a provided callback function to each element of the array, in reverse order, passing in the return value from the calculation on the following 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.reduceRight( caddf );
* // returns <Complex64>
*
* var re = realf( z );
* // returns 6.0
*
* var im = imagf( z );
* // returns 6.0
*/
reduceRight<U = unknown>( reducer: Reducer<U>, initialValue?: U ): U;

/**
* Reverses an array in-place.
*
Expand Down
67 changes: 66 additions & 1 deletion lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1700,7 +1700,7 @@ setReadOnly( Complex64Array.prototype, 'map', function map( fcn, thisArg ) {
});

/**
* 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.
* Applies a provided callback function to each element of the array, in order, passing in the return value from the calculation on the following element and returning the accumulated result upon completion.
*
* @name reduce
* @memberof Complex64Array.prototype
Expand Down Expand Up @@ -1764,6 +1764,71 @@ setReadOnly( Complex64Array.prototype, 'reduce', function reduce( reducer, initi
return acc;
});

/**
* Applies a provided callback function to each element of the array, in reverse order, passing in the return value from the calculation on the succeding element and returning the accumulated result upon completion.
*
* @name reduceRight
* @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.reduceRight( caddf );
* // returns <Complex64>
*
* var re = realf( z );
* // returns 6.0
*
* var im = imagf( z );
* // returns 6.0
*/
setReadOnly( Complex64Array.prototype, 'reduceRight', 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 = len-1;
} 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, len-1 );
i = len-2;
}
for ( ; i >= 0; i-- ) {
v = getComplex64( buf, i );
acc = reducer( acc, v, i, this );
}
return acc;
});

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

0 comments on commit 68b4a9e

Please sign in to comment.