Skip to content

Commit f3def6d

Browse files
Jaysukh-409kgryte
andauthored
feat: add reduce method to array/complex64
PR-URL: #1271 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 aed166e commit f3def6d

File tree

6 files changed

+570
-0
lines changed

6 files changed

+570
-0
lines changed

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,6 +1622,60 @@ var count = context.count;
16221622
// returns 3
16231623
```
16241624

1625+
<a name="method-reduce"></a>
1626+
1627+
#### Complex64Array.prototype.reduce( reducerFn\[, initialValue] )
1628+
1629+
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.
1630+
1631+
```javascript
1632+
var realf = require( '@stdlib/complex/realf' );
1633+
var imagf = require( '@stdlib/complex/imagf' );
1634+
var caddf = require( '@stdlib/math/base/ops/caddf' );
1635+
1636+
var arr = new Complex64Array( 3 );
1637+
1638+
arr.set( [ 1.0, 1.0 ], 0 );
1639+
arr.set( [ 2.0, 2.0 ], 1 );
1640+
arr.set( [ 3.0, 3.0 ], 2 );
1641+
1642+
var z = arr.reduce( caddf );
1643+
// returns <Complex64>
1644+
1645+
var re = realf( z );
1646+
// returns 6.0
1647+
1648+
var im = imagf( z );
1649+
// returns 6.0
1650+
```
1651+
1652+
The reducer function is provided four arguments:
1653+
1654+
- **acc**: accumulated result.
1655+
- **value**: current array element.
1656+
- **index**: current array element index.
1657+
- **arr**: the array on which this method was called.
1658+
1659+
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.
1660+
1661+
```javascript
1662+
var realf = require( '@stdlib/complex/realf' );
1663+
1664+
function reducer( acc, v ) {
1665+
acc += realf( v );
1666+
return acc;
1667+
}
1668+
1669+
var arr = new Complex64Array( 3 );
1670+
1671+
arr.set( [ 1.0, 1.0 ], 0 );
1672+
arr.set( [ 2.0, 2.0 ], 1 );
1673+
arr.set( [ 3.0, 3.0 ], 2 );
1674+
1675+
var z = arr.reduce( reducer, 0.0 );
1676+
// returns 6.0
1677+
```
1678+
16251679
<a name="method-reverse"></a>
16261680

16271681
#### 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) 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 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+':reduce', 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.reduce( 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) 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 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.reduce( 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+':reduce:len='+len, f );
101+
}
102+
}
103+
104+
main();

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

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,60 @@ type TernaryMapFcn<U> = ( this: U, value: Complex64, index: number, arr: Complex
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: Complex64 ) => 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: Complex64, 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: Complex64, index: number, arr: Complex64Array ) => 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 64-bit complex number array.
200254
*/
@@ -813,6 +867,35 @@ declare class Complex64Array implements Complex64ArrayInterface {
813867
*/
814868
map<U = unknown>( fcn: MapFcn<U>, thisArg?: ThisParameterType<MapFcn<U>> ): Complex64Array;
815869

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

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,6 +1699,71 @@ setReadOnly( Complex64Array.prototype, 'map', function map( fcn, thisArg ) {
16991699
return out;
17001700
});
17011701

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

0 commit comments

Comments
 (0)