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 332f138 commit 932bf9d
Show file tree
Hide file tree
Showing 7 changed files with 524 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -36,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
54 changes: 54 additions & 0 deletions complex64/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1676,6 +1676,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
52 changes: 52 additions & 0 deletions complex64/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' );
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 complex64/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' );
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();
29 changes: 29 additions & 0 deletions complex64/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 complex64/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 932bf9d

Please sign in to comment.