Skip to content

Commit ee1c957

Browse files
feat: add reduceRight method to array/complex64
PR-URL: #1395 Signed-off-by: Philipp Burckhardt <pburckhardt@outlook.com> Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent f36bef4 commit ee1c957

File tree

6 files changed

+522
-1
lines changed

6 files changed

+522
-1
lines changed

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

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

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

16811735
#### Complex64Array.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 caddf = require( '@stdlib/math/base/ops/caddf' );
25+
var isComplexLike = require( '@stdlib/assert/is-complex-like' );
26+
var pkg = require( './../package.json' ).name;
27+
var Complex64Array = 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 Complex64Array( [ 1, 2, 3, 4, 5, 6 ] );
38+
39+
b.tic();
40+
for ( i = 0; i < b.iterations; i++ ) {
41+
out = arr.reduceRight( caddf );
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 caddf = require( '@stdlib/math/base/ops/caddf' );
26+
var isComplexLike = require( '@stdlib/assert/is-complex-like' );
27+
var Complex64 = require( '@stdlib/complex/float32' );
28+
var pkg = require( './../package.json' ).name;
29+
var Complex64Array = 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 Complex64( i, i ) );
48+
}
49+
arr = new Complex64Array( 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( caddf );
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/complex64/docs/types/index.d.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,35 @@ declare class Complex64Array implements Complex64ArrayInterface {
896896
*/
897897
reduce<U = unknown>( reducer: Reducer<U>, initialValue?: U ): U;
898898

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

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

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1700,7 +1700,7 @@ setReadOnly( Complex64Array.prototype, 'map', function map( fcn, thisArg ) {
17001700
});
17011701

17021702
/**
1703-
* 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.
1703+
* 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.
17041704
*
17051705
* @name reduce
17061706
* @memberof Complex64Array.prototype
@@ -1764,6 +1764,71 @@ setReadOnly( Complex64Array.prototype, 'reduce', function reduce( reducer, initi
17641764
return acc;
17651765
});
17661766

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

0 commit comments

Comments
 (0)