Skip to content

Commit 0848cd9

Browse files
Jaysukh-409kgryte
andauthored
feat: add reduce method to array/complex128
PR-URL: #1367 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Athan Reines <kgryte@gmail.com>
1 parent f3def6d commit 0848cd9

File tree

6 files changed

+570
-0
lines changed

6 files changed

+570
-0
lines changed

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

+54
Original file line numberDiff line numberDiff line change
@@ -1620,6 +1620,60 @@ var count = context.count;
16201620
// returns 3
16211621
```
16221622

1623+
<a name="method-reduce"></a>
1624+
1625+
#### Complex128Array.prototype.reduce( reducerFn\[, initialValue] )
1626+
1627+
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.
1628+
1629+
```javascript
1630+
var real = require( '@stdlib/complex/real' );
1631+
var imag = require( '@stdlib/complex/imag' );
1632+
var cadd = require( '@stdlib/math/base/ops/cadd' );
1633+
1634+
var arr = new Complex128Array( 3 );
1635+
1636+
arr.set( [ 1.0, 1.0 ], 0 );
1637+
arr.set( [ 2.0, 2.0 ], 1 );
1638+
arr.set( [ 3.0, 3.0 ], 2 );
1639+
1640+
var z = arr.reduce( cadd );
1641+
// returns <Complex128>
1642+
1643+
var re = real( z );
1644+
// returns 6.0
1645+
1646+
var im = imag( z );
1647+
// returns 6.0
1648+
```
1649+
1650+
The reducer function is provided four arguments:
1651+
1652+
- **acc**: accumulated result.
1653+
- **value**: current array element.
1654+
- **index**: current array element index.
1655+
- **arr**: the array on which this method was called.
1656+
1657+
By default, the function initializes the accumulated result to the first element in the array and passes the second array element as `value` during the first invocation of the provided callback. To begin accumulation from a different starting value and pass in the first array element as `value` during the first invocation of the provided callback, provide an `initialValue` argument.
1658+
1659+
```javascript
1660+
var real = require( '@stdlib/complex/real' );
1661+
1662+
function reducer( acc, v ) {
1663+
acc += real( v );
1664+
return acc;
1665+
}
1666+
1667+
var arr = new Complex128Array( 3 );
1668+
1669+
arr.set( [ 1.0, 1.0 ], 0 );
1670+
arr.set( [ 2.0, 2.0 ], 1 );
1671+
arr.set( [ 3.0, 3.0 ], 2 );
1672+
1673+
var z = arr.reduce( reducer, 0.0 );
1674+
// returns 6.0
1675+
```
1676+
16231677
<a name="method-reverse"></a>
16241678

16251679
#### Complex128Array.prototype.reverse()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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+':reduce', 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.reduce( 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+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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.reduce( 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+':reduce:len='+len, f );
101+
}
102+
}
103+
104+
main();

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

+83
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,60 @@ type TernaryMapFcn<U> = ( this: U, value: Complex128, index: number, arr: Comple
195195
*/
196196
type MapFcn<U> = NullaryMapFcn<U> | UnaryMapFcn<U> | BinaryMapFcn<U> | TernaryMapFcn<U>;
197197

198+
/**
199+
* Reducer function invoked for each element in an array.
200+
*
201+
* @returns accumulated result
202+
*/
203+
type NullaryReducer<U> = () => U;
204+
205+
/**
206+
* Reducer function invoked for each element in an array.
207+
*
208+
* @param acc - accumulated result
209+
* @returns accumulated result
210+
*/
211+
type UnaryReducer<U> = ( acc: U ) => U;
212+
213+
/**
214+
* Reducer function invoked for each element in an array.
215+
*
216+
* @param acc - accumulated result
217+
* @param value - current array element
218+
* @returns accumulated result
219+
*/
220+
type BinaryReducer<U> = ( acc: U, value: Complex128 ) => U;
221+
222+
/**
223+
* Reducer function invoked for each element in an array.
224+
*
225+
* @param acc - accumulated result
226+
* @param value - current array element
227+
* @param index - current array element index
228+
* @returns accumulated result
229+
*/
230+
type TernaryReducer<U> = ( acc: U, value: Complex128, index: number ) => U;
231+
232+
/**
233+
* @param acc - accumulated result
234+
* @param value - current array element
235+
* @param index - current array element index
236+
* @param arr - array on which the method was called
237+
* @returns accumulated result
238+
*/
239+
type QuaternaryReducer<U> = ( acc: U, value: Complex128, index: number, arr: Complex128Array ) => U;
240+
241+
/**
242+
* Reducer function invoked for each element in an array.
243+
*
244+
* @param acc - accumulated result
245+
* @param value - current array element
246+
* @param index - current array element index
247+
* @param arr - array on which the method was called
248+
* @returns accumulated result
249+
*/
250+
type Reducer<U> = NullaryReducer<U> | UnaryReducer<U> | BinaryReducer<U> | TernaryReducer<U> | QuaternaryReducer<U>;
251+
198252
/**
199253
* Class for creating a 128-bit complex number array.
200254
*/
@@ -811,6 +865,35 @@ declare class Complex128Array implements Complex128ArrayInterface {
811865
*/
812866
map<U = unknown>( fcn: MapFcn<U>, thisArg?: ThisParameterType<MapFcn<U>> ): Complex128Array;
813867

868+
/**
869+
* 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.
870+
*
871+
* @param reducer - callback function
872+
* @param initialValue - initial value
873+
* @returns accumulated result
874+
*
875+
* @example
876+
* var real = require( '@stdlib/complex/real' );
877+
* var imag = require( '@stdlib/complex/imag' );
878+
* var cadd = require( '@stdlib/math/base/ops/cadd' );
879+
*
880+
* var arr = new Complex128Array( 3 );
881+
*
882+
* arr.set( [ 1.0, 1.0 ], 0 );
883+
* arr.set( [ 2.0, 2.0 ], 1 );
884+
* arr.set( [ 3.0, 3.0 ], 2 );
885+
*
886+
* var z = arr.reduce( cadd );
887+
* // returns <Complex128>
888+
*
889+
* var re = real( z );
890+
* // returns 6.0
891+
*
892+
* var im = imag( z );
893+
* // returns 6.0
894+
*/
895+
reduce<U = unknown>( reducer: Reducer<U>, initialValue?: U ): U;
896+
814897
/**
815898
* Reverses an array in-place.
816899
*

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

+65
Original file line numberDiff line numberDiff line change
@@ -1696,6 +1696,71 @@ setReadOnly( Complex128Array.prototype, 'map', function map( fcn, thisArg ) {
16961696
return out;
16971697
});
16981698

1699+
/**
1700+
* 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.
1701+
*
1702+
* @name reduce
1703+
* @memberof Complex128Array.prototype
1704+
* @type {Function}
1705+
* @param {Function} reducer - callback function
1706+
* @param {*} [initialValue] - initial value
1707+
* @throws {TypeError} `this` must be a complex number array
1708+
* @throws {TypeError} first argument must be a function
1709+
* @throws {Error} if not provided an initial value, the array must have at least one element
1710+
* @returns {*} accumulated result
1711+
*
1712+
* @example
1713+
* var real = require( '@stdlib/complex/real' );
1714+
* var imag = require( '@stdlib/complex/imag' );
1715+
* var cadd = require( '@stdlib/math/base/ops/cadd' );
1716+
*
1717+
* var arr = new Complex128Array( 3 );
1718+
*
1719+
* arr.set( [ 1.0, 1.0 ], 0 );
1720+
* arr.set( [ 2.0, 2.0 ], 1 );
1721+
* arr.set( [ 3.0, 3.0 ], 2 );
1722+
*
1723+
* var z = arr.reduce( cadd );
1724+
* // returns <Complex128>
1725+
*
1726+
* var re = real( z );
1727+
* // returns 6.0
1728+
*
1729+
* var im = imag( z );
1730+
* // returns 6.0
1731+
*/
1732+
setReadOnly( Complex128Array.prototype, 'reduce', function reduce( reducer, initialValue ) {
1733+
var buf;
1734+
var acc;
1735+
var len;
1736+
var v;
1737+
var i;
1738+
1739+
if ( !isComplexArray( this ) ) {
1740+
throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
1741+
}
1742+
if ( !isFunction( reducer ) ) {
1743+
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', reducer ) );
1744+
}
1745+
buf = this._buffer;
1746+
len = this._length;
1747+
if ( arguments.length > 1 ) {
1748+
acc = initialValue;
1749+
i = 0;
1750+
} else {
1751+
if ( len === 0 ) {
1752+
throw new Error( 'invalid operation. If not provided an initial value, an array must contain at least one element.' );
1753+
}
1754+
acc = getComplex128( buf, 0 );
1755+
i = 1;
1756+
}
1757+
for ( ; i < len; i++ ) {
1758+
v = getComplex128( buf, i );
1759+
acc = reducer( acc, v, i, this );
1760+
}
1761+
return acc;
1762+
});
1763+
16991764
/**
17001765
* Reverses an array in-place.
17011766
*

0 commit comments

Comments
 (0)