Skip to content

feat: add reduceRight method to array/complex128 #1406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions lib/node_modules/@stdlib/array/complex128/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1674,6 +1674,60 @@ var z = arr.reduce( reducer, 0.0 );
// returns 6.0
```

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

#### Complex128Array.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 real = require( '@stdlib/complex/real' );
var imag = require( '@stdlib/complex/imag' );
var cadd = require( '@stdlib/math/base/ops/cadd' );

var arr = new Complex128Array( 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( cadd );
// returns <Complex128>

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

var im = imag( 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 real = require( '@stdlib/complex/real' );

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

var arr = new Complex128Array( 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>

#### Complex128Array.prototype.reverse()
Expand Down
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' );
var cadd = require( '@stdlib/math/base/ops/cadd' );
var isComplexLike = require('@stdlib/assert/is-complex-like' );
var pkg = require( './../package.json' ).name;
var Complex128Array = require( './../lib' );


// MAIN //

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

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

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = arr.reduceRight( cadd );
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) 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' );
var pow = require( '@stdlib/math/base/special/pow' );
var cadd = require( '@stdlib/math/base/ops/cadd' );
var isComplexLike = require('@stdlib/assert/is-complex-like' );
var Complex128 = require( '@stdlib/complex/float64' );
var pkg = require( './../package.json' ).name;
var Complex128Array = 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 Complex128( i, i ) );
}
arr = new Complex128Array( 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( cadd );
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();
29 changes: 29 additions & 0 deletions lib/node_modules/@stdlib/array/complex128/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,35 @@ declare class Complex128Array implements Complex128ArrayInterface {
*/
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 real = require( '@stdlib/complex/real' );
* var imag = require( '@stdlib/complex/imag' );
* var cadd = require( '@stdlib/math/base/ops/cadd' );
*
* var arr = new Complex128Array( 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( cadd );
* // returns <Complex128>
*
* var re = real( z );
* // returns 6.0
*
* var im = imag( z );
* // returns 6.0
*/
reduceRight<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/complex128/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1761,6 +1761,71 @@ setReadOnly( Complex128Array.prototype, 'reduce', function reduce( reducer, init
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 following element and returning the accumulated result upon completion.
*
* @name reduceRight
* @memberof Complex128Array.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 real = require( '@stdlib/complex/real' );
* var imag = require( '@stdlib/complex/imag' );
* var cadd = require( '@stdlib/math/base/ops/cadd' );
*
* var arr = new Complex128Array( 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( cadd );
* // returns <Complex128>
*
* var re = real( z );
* // returns 6.0
*
* var im = imag( z );
* // returns 6.0
*/
setReadOnly( Complex128Array.prototype, 'reduceRight', function reduceRight( 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 = getComplex128( buf, len-1 );
i = len-2;
}
for ( ; i >= 0; i-- ) {
v = getComplex128( buf, i );
acc = reducer( acc, v, i, this );
}
return acc;
});

/**
* Reverses an array in-place.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var cadd = require( '@stdlib/math/base/ops/cadd' );
var instanceOf = require( '@stdlib/assert/instance-of' );
var real = require( '@stdlib/complex/real' );
var imag = require( '@stdlib/complex/imag' );
var Complex128 = require('@stdlib/complex/float64');
var Complex128 = require( '@stdlib/complex/float64' );
var Complex128Array = require( './../lib' );


Expand Down
Loading