Skip to content

Commit 4e84566

Browse files
Jaysukh-409stdlib-botPlaneshifter
authored
feat: add reduceRight method to array/complex128
PR-URL: #1406 Signed-off-by: Philipp Burckhardt <pburckhardt@outlook.com> Co-authored-by: stdlib-bot <82920195+stdlib-bot@users.noreply.github.com> Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent 9d730e8 commit 4e84566

File tree

8 files changed

+523
-2
lines changed

8 files changed

+523
-2
lines changed

lib/node_modules/@stdlib/array/complex128/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,6 +1674,60 @@ var z = arr.reduce( reducer, 0.0 );
16741674
// returns 6.0
16751675
```
16761676

1677+
<a name="method-reduce-right"></a>
1678+
1679+
#### Complex128Array.prototype.reduceRight( reducerFn\[, initialValue] )
1680+
1681+
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.
1682+
1683+
```javascript
1684+
var real = require( '@stdlib/complex/real' );
1685+
var imag = require( '@stdlib/complex/imag' );
1686+
var cadd = require( '@stdlib/math/base/ops/cadd' );
1687+
1688+
var arr = new Complex128Array( 3 );
1689+
1690+
arr.set( [ 1.0, 1.0 ], 0 );
1691+
arr.set( [ 2.0, 2.0 ], 1 );
1692+
arr.set( [ 3.0, 3.0 ], 2 );
1693+
1694+
var z = arr.reduceRight( cadd );
1695+
// returns <Complex128>
1696+
1697+
var re = real( z );
1698+
// returns 6.0
1699+
1700+
var im = imag( z );
1701+
// returns 6.0
1702+
```
1703+
1704+
The reducer function is provided four arguments:
1705+
1706+
- **acc**: accumulated result.
1707+
- **value**: current array element.
1708+
- **index**: current array element index.
1709+
- **arr**: the array on which this method was called.
1710+
1711+
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.
1712+
1713+
```javascript
1714+
var real = require( '@stdlib/complex/real' );
1715+
1716+
function reducer( acc, v ) {
1717+
acc += real( v );
1718+
return acc;
1719+
}
1720+
1721+
var arr = new Complex128Array( 3 );
1722+
1723+
arr.set( [ 1.0, 1.0 ], 0 );
1724+
arr.set( [ 2.0, 2.0 ], 1 );
1725+
arr.set( [ 3.0, 3.0 ], 2 );
1726+
1727+
var z = arr.reduceRight( reducer, 0.0 );
1728+
// returns 6.0
1729+
```
1730+
16771731
<a name="method-reverse"></a>
16781732

16791733
#### Complex128Array.prototype.reverse()
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var cadd = require( '@stdlib/math/base/ops/cadd' );
25+
var isComplexLike = require('@stdlib/assert/is-complex-like' );
26+
var pkg = require( './../package.json' ).name;
27+
var Complex128Array = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+':reduceRight', function benchmark( b ) {
33+
var out;
34+
var arr;
35+
var i;
36+
37+
arr = new Complex128Array( [ 1, 2, 3, 4, 5, 6 ] );
38+
39+
b.tic();
40+
for ( i = 0; i < b.iterations; i++ ) {
41+
out = arr.reduceRight( cadd );
42+
if ( typeof out !== 'object' ) {
43+
b.fail( 'should return an object' );
44+
}
45+
}
46+
b.toc();
47+
if ( !isComplexLike( out ) ) {
48+
b.fail( 'should return a complex number' );
49+
}
50+
b.pass( 'benchmark finished' );
51+
b.end();
52+
});
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var pow = require( '@stdlib/math/base/special/pow' );
25+
var cadd = require( '@stdlib/math/base/ops/cadd' );
26+
var isComplexLike = require('@stdlib/assert/is-complex-like' );
27+
var Complex128 = require( '@stdlib/complex/float64' );
28+
var pkg = require( './../package.json' ).name;
29+
var Complex128Array = require( './../lib' );
30+
31+
32+
// FUNCTIONS //
33+
34+
/**
35+
* Creates a benchmark function.
36+
*
37+
* @private
38+
* @param {PositiveInteger} len - array length
39+
* @returns {Function} benchmark function
40+
*/
41+
function createBenchmark( len ) {
42+
var arr;
43+
var i;
44+
45+
arr = [];
46+
for ( i = 0; i < len; i++ ) {
47+
arr.push( new Complex128( i, i ) );
48+
}
49+
arr = new Complex128Array( arr );
50+
51+
return benchmark;
52+
53+
/**
54+
* Benchmark function.
55+
*
56+
* @private
57+
* @param {Benchmark} b - benchmark instance
58+
*/
59+
function benchmark( b ) {
60+
var out;
61+
var i;
62+
63+
b.tic();
64+
for ( i = 0; i < b.iterations; i++ ) {
65+
out = arr.reduceRight( cadd );
66+
if ( typeof out !== 'object' ) {
67+
b.fail( 'should return an object' );
68+
}
69+
}
70+
b.toc();
71+
if ( !isComplexLike( out ) ) {
72+
b.fail( 'should return a complex number' );
73+
}
74+
b.pass( 'benchmark finished' );
75+
b.end();
76+
}
77+
}
78+
79+
80+
// MAIN //
81+
82+
/**
83+
* Main execution sequence.
84+
*
85+
* @private
86+
*/
87+
function main() {
88+
var len;
89+
var min;
90+
var max;
91+
var f;
92+
var i;
93+
94+
min = 1; // 10^min
95+
max = 6; // 10^max
96+
97+
for ( i = min; i <= max; i++ ) {
98+
len = pow( 10, i );
99+
f = createBenchmark( len );
100+
bench( pkg+':reduceRight:len='+len, f );
101+
}
102+
}
103+
104+
main();

lib/node_modules/@stdlib/array/complex128/docs/types/index.d.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,35 @@ declare class Complex128Array implements Complex128ArrayInterface {
894894
*/
895895
reduce<U = unknown>( reducer: Reducer<U>, initialValue?: U ): U;
896896

897+
/**
898+
* 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.
899+
*
900+
* @param reducer - callback function
901+
* @param initialValue - initial value
902+
* @returns accumulated result
903+
*
904+
* @example
905+
* var real = require( '@stdlib/complex/real' );
906+
* var imag = require( '@stdlib/complex/imag' );
907+
* var cadd = require( '@stdlib/math/base/ops/cadd' );
908+
*
909+
* var arr = new Complex128Array( 3 );
910+
*
911+
* arr.set( [ 1.0, 1.0 ], 0 );
912+
* arr.set( [ 2.0, 2.0 ], 1 );
913+
* arr.set( [ 3.0, 3.0 ], 2 );
914+
*
915+
* var z = arr.reduceRight( cadd );
916+
* // returns <Complex128>
917+
*
918+
* var re = real( z );
919+
* // returns 6.0
920+
*
921+
* var im = imag( z );
922+
* // returns 6.0
923+
*/
924+
reduceRight<U = unknown>( reducer: Reducer<U>, initialValue?: U ): U;
925+
897926
/**
898927
* Reverses an array in-place.
899928
*

lib/node_modules/@stdlib/array/complex128/lib/main.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,6 +1761,71 @@ setReadOnly( Complex128Array.prototype, 'reduce', function reduce( reducer, init
17611761
return acc;
17621762
});
17631763

1764+
/**
1765+
* 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.
1766+
*
1767+
* @name reduceRight
1768+
* @memberof Complex128Array.prototype
1769+
* @type {Function}
1770+
* @param {Function} reducer - callback function
1771+
* @param {*} [initialValue] - initial value
1772+
* @throws {TypeError} `this` must be a complex number array
1773+
* @throws {TypeError} first argument must be a function
1774+
* @throws {Error} if not provided an initial value, the array must have at least one element
1775+
* @returns {*} accumulated result
1776+
*
1777+
* @example
1778+
* var real = require( '@stdlib/complex/real' );
1779+
* var imag = require( '@stdlib/complex/imag' );
1780+
* var cadd = require( '@stdlib/math/base/ops/cadd' );
1781+
*
1782+
* var arr = new Complex128Array( 3 );
1783+
*
1784+
* arr.set( [ 1.0, 1.0 ], 0 );
1785+
* arr.set( [ 2.0, 2.0 ], 1 );
1786+
* arr.set( [ 3.0, 3.0 ], 2 );
1787+
*
1788+
* var z = arr.reduceRight( cadd );
1789+
* // returns <Complex128>
1790+
*
1791+
* var re = real( z );
1792+
* // returns 6.0
1793+
*
1794+
* var im = imag( z );
1795+
* // returns 6.0
1796+
*/
1797+
setReadOnly( Complex128Array.prototype, 'reduceRight', function reduceRight( reducer, initialValue ) {
1798+
var buf;
1799+
var acc;
1800+
var len;
1801+
var v;
1802+
var i;
1803+
1804+
if ( !isComplexArray( this ) ) {
1805+
throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
1806+
}
1807+
if ( !isFunction( reducer ) ) {
1808+
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', reducer ) );
1809+
}
1810+
buf = this._buffer;
1811+
len = this._length;
1812+
if ( arguments.length > 1 ) {
1813+
acc = initialValue;
1814+
i = len-1;
1815+
} else {
1816+
if ( len === 0 ) {
1817+
throw new Error( 'invalid operation. If not provided an initial value, an array must contain at least one element.' );
1818+
}
1819+
acc = getComplex128( buf, len-1 );
1820+
i = len-2;
1821+
}
1822+
for ( ; i >= 0; i-- ) {
1823+
v = getComplex128( buf, i );
1824+
acc = reducer( acc, v, i, this );
1825+
}
1826+
return acc;
1827+
});
1828+
17641829
/**
17651830
* Reverses an array in-place.
17661831
*

lib/node_modules/@stdlib/array/complex128/test/test.reduce.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var cadd = require( '@stdlib/math/base/ops/cadd' );
2727
var instanceOf = require( '@stdlib/assert/instance-of' );
2828
var real = require( '@stdlib/complex/real' );
2929
var imag = require( '@stdlib/complex/imag' );
30-
var Complex128 = require('@stdlib/complex/float64');
30+
var Complex128 = require( '@stdlib/complex/float64' );
3131
var Complex128Array = require( './../lib' );
3232

3333

0 commit comments

Comments
 (0)