Skip to content

Commit 15e7395

Browse files
committedFeb 24, 2024
Auto-generated commit
1 parent ac21148 commit 15e7395

13 files changed

+620
-51
lines changed
 

‎.github/workflows/npm_downloads.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ jobs:
8686
8787
# Upload the download data:
8888
- name: 'Upload data'
89-
# Pin action to full length commit SHA corresponding to v3.1.3
90-
uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32
89+
# Pin action to full length commit SHA
90+
uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1
9191
with:
9292
# Define a name for the uploaded artifact (ensuring a unique name for each job):
9393
name: npm_downloads

‎.github/workflows/publish.yml

+4-10
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,10 @@ jobs:
124124
mv ./package.json.tmp ./package.json
125125
fi
126126
done
127-
jq -r '.devDependencies | keys[]' ./package.json | while read -r dep; do
128-
if [[ "$dep" != "@stdlib"* ]]; then
129-
continue
130-
fi
131-
dep=$(echo "$dep" | xargs)
132-
if ! find lib -name "*.js" -exec grep -q "$dep" {} + && ! grep -q -s "$dep" manifest.json && ! grep -q -s "$dep" include.gypi; then
133-
jq --indent 2 "del(.devDependencies[\"$dep\"])" ./package.json > ./package.json.tmp
134-
mv ./package.json.tmp ./package.json
135-
fi
136-
done
127+
128+
# Set `devDependencies` to an empty object:
129+
jq --indent 2 '.devDependencies = {}' ./package.json > ./package.json.tmp
130+
mv ./package.json.tmp ./package.json
137131
138132
# Remove CLI section:
139133
find . -type f -name '*.md' -print0 | xargs -0 perl -0777 -i -pe "s/(\* \* \*\n+)?<section class=\"cli\">[\s\S]+?<\!\-\- \/.cli \-\->//"

‎.github/workflows/test_bundles.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ jobs:
168168

169169
# Install Deno:
170170
- name: 'Install Deno'
171-
# Pin action to full length commit SHA corresponding to v1.1.2
172-
uses: denoland/setup-deno@61fe2df320078202e33d7d5ad347e7dcfa0e8f31
171+
# Pin action to full length commit SHA
172+
uses: denoland/setup-deno@041b854f97b325bd60e53e9dc2de9cb9f9ac0cba # v1.1.4
173173
with:
174174
deno-version: vx.x.x
175175

‎CONTRIBUTORS

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#
33
# Contributors listed in alphabetical order.
44

5+
Aditya Sapra <110766802+adityacodes30@users.noreply.github.com>
56
Ali Salesi <ali_sal1381@yahoo.com>
67
Amit Jimiwal <amitjimiwal45@gmail.com>
78
Athan Reines <kgryte@gmail.com>
@@ -13,6 +14,7 @@ Daniel Killenberger <daniel.killenberger@gmail.com>
1314
Dominik Moritz <domoritz@gmail.com>
1415
Dorrin Sotoudeh <dorrinsotoudeh123@gmail.com>
1516
Frank Kovacs <fran70kk@gmail.com>
17+
GUNJ JOSHI <gunjjoshi8372@gmail.com>
1618
Harshita Kalani <harshitakalani02@gmail.com>
1719
James Gelok <jdgelok@gmail.com>
1820
Jaysukh Makvana <jaysukhmakvana2004@gmail.com>
@@ -21,6 +23,7 @@ Joey Reed <joeyrreed@gmail.com>
2123
Jordan Gallivan <115050475+Jordan-Gallivan@users.noreply.github.com>
2224
Joris Labie <joris.labie1@gmail.com>
2325
Justin Dennison <justin1dennison@gmail.com>
26+
Karthik Prakash <116057817+skoriop@users.noreply.github.com>
2427
Marcus Fantham <mfantham@users.noreply.github.com>
2528
Matt Cochrane <matthew.cochrane.eng@gmail.com>
2629
Milan Raj <rajsite@users.noreply.github.com>
@@ -36,6 +39,7 @@ Roman Stetsyk <25715951+romanstetsyk@users.noreply.github.com>
3639
Ryan Seal <splrk@users.noreply.github.com>
3740
Seyyed Parsa Neshaei <spneshaei@users.noreply.github.com>
3841
Shraddheya Shendre <shendreshraddheya@gmail.com>
42+
Spandan Barve <114365550+marsian83@users.noreply.github.com>
3943
Stephannie Jiménez Gacha <steff456@hotmail.com>
4044
Yernar Yergaziyev <yernar.yergaziyev@erg.kz>
4145
orimiles5 <97595296+orimiles5@users.noreply.github.com>

‎README.md

+56-2
Original file line numberDiff line numberDiff line change
@@ -1655,6 +1655,60 @@ var count = context.count;
16551655
// returns 3
16561656
```
16571657

1658+
<a name="method-reduce"></a>
1659+
1660+
#### Complex64Array.prototype.reduce( reducerFn\[, initialValue] )
1661+
1662+
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.
1663+
1664+
```javascript
1665+
var realf = require( '@stdlib/complex-realf' );
1666+
var imagf = require( '@stdlib/complex-imagf' );
1667+
var caddf = require( '@stdlib/math-base-ops-caddf' );
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( caddf );
1676+
// returns <Complex64>
1677+
1678+
var re = realf( z );
1679+
// returns 6.0
1680+
1681+
var im = imagf( z );
1682+
// returns 6.0
1683+
```
1684+
1685+
The reducer function is provided four arguments:
1686+
1687+
- **acc**: accumulated result.
1688+
- **value**: current array element.
1689+
- **index**: current array element index.
1690+
- **arr**: the array on which this method was called.
1691+
1692+
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.
1693+
1694+
```javascript
1695+
var realf = require( '@stdlib/complex-realf' );
1696+
1697+
function reducer( acc, v ) {
1698+
acc += realf( v );
1699+
return acc;
1700+
}
1701+
1702+
var arr = new Complex64Array( 3 );
1703+
1704+
arr.set( [ 1.0, 1.0 ], 0 );
1705+
arr.set( [ 2.0, 2.0 ], 1 );
1706+
arr.set( [ 3.0, 3.0 ], 2 );
1707+
1708+
var z = arr.reduce( reducer, 0.0 );
1709+
// returns 6.0
1710+
```
1711+
16581712
<a name="method-reverse"></a>
16591713

16601714
#### Complex64Array.prototype.reverse()
@@ -2312,8 +2366,8 @@ Copyright &copy; 2016-2024. The Stdlib [Authors][stdlib-authors].
23122366
[npm-image]: http://img.shields.io/npm/v/@stdlib/array-complex64.svg
23132367
[npm-url]: https://npmjs.org/package/@stdlib/array-complex64
23142368

2315-
[test-image]: https://github.com/stdlib-js/array-complex64/actions/workflows/test.yml/badge.svg?branch=v0.2.0
2316-
[test-url]: https://github.com/stdlib-js/array-complex64/actions/workflows/test.yml?query=branch:v0.2.0
2369+
[test-image]: https://github.com/stdlib-js/array-complex64/actions/workflows/test.yml/badge.svg?branch=main
2370+
[test-url]: https://github.com/stdlib-js/array-complex64/actions/workflows/test.yml?query=branch:main
23172371

23182372
[coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/array-complex64/main.svg
23192373
[coverage-url]: https://codecov.io/github/stdlib-js/array-complex64?branch=main

‎benchmark/benchmark.reduce.js

+52
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-harness' );
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+
});

‎benchmark/benchmark.reduce.length.js

+104
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-harness' );
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();

‎dist/index.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/index.js.map

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎docs/types/index.d.ts

+83
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/main.js

+65
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
*

‎package.json

+32-31
Original file line numberDiff line numberDiff line change
@@ -41,48 +41,49 @@
4141
"@stdlib/array-base-assert-is-complex128array": "^0.2.0",
4242
"@stdlib/array-base-assert-is-complex64array": "^0.2.0",
4343
"@stdlib/array-base-getter": "^0.2.0",
44-
"@stdlib/array-float32": "^0.2.0",
45-
"@stdlib/assert-has-iterator-symbol-support": "^0.2.0",
46-
"@stdlib/assert-is-array": "^0.2.0",
44+
"@stdlib/array-float32": "^0.2.1",
45+
"@stdlib/assert-has-iterator-symbol-support": "^0.2.1",
46+
"@stdlib/assert-is-array": "^0.2.1",
4747
"@stdlib/assert-is-array-like-object": "^0.2.0",
48-
"@stdlib/assert-is-arraybuffer": "^0.2.0",
48+
"@stdlib/assert-is-arraybuffer": "^0.2.1",
4949
"@stdlib/assert-is-collection": "^0.2.0",
50-
"@stdlib/assert-is-complex-like": "^0.2.0",
51-
"@stdlib/assert-is-function": "^0.2.0",
50+
"@stdlib/assert-is-complex-like": "^0.2.1",
51+
"@stdlib/assert-is-function": "^0.2.1",
5252
"@stdlib/assert-is-nonnegative-integer": "^0.2.0",
53-
"@stdlib/assert-is-object": "^0.2.0",
54-
"@stdlib/assert-is-string": "^0.2.0",
55-
"@stdlib/complex-float32": "^0.2.0",
56-
"@stdlib/complex-imagf": "^0.2.0",
57-
"@stdlib/complex-realf": "^0.2.0",
53+
"@stdlib/assert-is-object": "^0.2.1",
54+
"@stdlib/assert-is-string": "^0.2.1",
55+
"@stdlib/complex-float32": "^0.2.1",
56+
"@stdlib/complex-imagf": "^0.2.1",
57+
"@stdlib/complex-realf": "^0.2.1",
5858
"@stdlib/math-base-assert-is-even": "^0.2.0",
5959
"@stdlib/math-base-assert-is-integer": "^0.2.0",
60-
"@stdlib/math-base-special-floor": "^0.2.0",
61-
"@stdlib/strided-base-reinterpret-complex128": "^0.2.0",
60+
"@stdlib/math-base-special-floor": "^0.2.1",
61+
"@stdlib/strided-base-reinterpret-complex128": "^0.2.1",
6262
"@stdlib/strided-base-reinterpret-complex64": "^0.2.0",
63-
"@stdlib/string-format": "^0.2.0",
64-
"@stdlib/symbol-iterator": "^0.2.0",
65-
"@stdlib/types": "^0.3.1",
66-
"@stdlib/utils-define-nonenumerable-read-only-accessor": "^0.2.0",
67-
"@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.0"
63+
"@stdlib/string-format": "^0.2.1",
64+
"@stdlib/symbol-iterator": "^0.2.1",
65+
"@stdlib/types": "^0.3.2",
66+
"@stdlib/utils-define-nonenumerable-read-only-accessor": "^0.2.2",
67+
"@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.1"
6868
},
6969
"devDependencies": {
7070
"@stdlib/array-buffer": "^0.2.0",
71-
"@stdlib/array-complex128": "^0.1.0",
72-
"@stdlib/assert-has-own-property": "^0.2.0",
73-
"@stdlib/assert-instance-of": "^0.2.0",
74-
"@stdlib/assert-is-boolean": "^0.2.0",
75-
"@stdlib/assert-is-complex64": "^0.2.0",
76-
"@stdlib/assert-is-complex64array": "^0.1.0",
71+
"@stdlib/array-complex128": "^0.2.0",
72+
"@stdlib/assert-has-own-property": "^0.2.1",
73+
"@stdlib/assert-instance-of": "^0.2.1",
74+
"@stdlib/assert-is-boolean": "^0.2.1",
75+
"@stdlib/assert-is-complex64": "^0.2.1",
76+
"@stdlib/assert-is-complex64array": "^0.2.0",
7777
"@stdlib/assert-is-integer": "^0.2.0",
78-
"@stdlib/assert-is-negative-zero": "^0.2.0",
79-
"@stdlib/assert-is-positive-zero": "^0.2.0",
80-
"@stdlib/complex-imag": "^0.2.0",
81-
"@stdlib/complex-real": "^0.2.0",
82-
"@stdlib/console-log-each": "^0.1.0",
78+
"@stdlib/assert-is-negative-zero": "^0.2.1",
79+
"@stdlib/assert-is-positive-zero": "^0.2.1",
80+
"@stdlib/complex-imag": "^0.2.1",
81+
"@stdlib/complex-real": "^0.2.1",
82+
"@stdlib/console-log-each": "^0.2.0",
83+
"@stdlib/math-base-ops-caddf": "^0.2.0",
8384
"@stdlib/math-base-special-pow": "^0.2.0",
84-
"@stdlib/random-base-randu": "^0.1.0",
85-
"@stdlib/utils-identity-function": "^0.2.0",
85+
"@stdlib/random-base-randu": "^0.2.0",
86+
"@stdlib/utils-identity-function": "^0.2.1",
8687
"proxyquire": "^2.0.0",
8788
"tape": "git+https://github.com/kgryte/tape.git#fix/globby",
8889
"istanbul": "^0.4.1",

‎test/test.reduce.js

+212
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
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 tape = require( 'tape' );
24+
var hasOwnProp = require( '@stdlib/assert-has-own-property' );
25+
var isFunction = require( '@stdlib/assert-is-function' );
26+
var caddf = require( '@stdlib/math-base-ops-caddf' );
27+
var instanceOf = require( '@stdlib/assert-instance-of' );
28+
var realf = require( '@stdlib/complex-realf' );
29+
var imagf = require( '@stdlib/complex-imagf' );
30+
var Complex64 = require('@stdlib/complex-float32');
31+
var Complex64Array = require( './../lib' );
32+
33+
34+
// TESTS //
35+
36+
tape( 'main export is a function', function test( t ) {
37+
t.ok( true, __filename );
38+
t.strictEqual( typeof Complex64Array, 'function', 'main export is a function' );
39+
t.end();
40+
});
41+
42+
tape( 'attached to the prototype of the main export is a `reduce` method', function test( t ) {
43+
t.strictEqual( hasOwnProp( Complex64Array.prototype, 'reduce' ), true, 'has property' );
44+
t.strictEqual( isFunction( Complex64Array.prototype.reduce ), true, 'has method' );
45+
t.end();
46+
});
47+
48+
tape( 'the method throws an error if invoked with a `this` context which is not a complex number array instance', function test( t ) {
49+
var values;
50+
var arr;
51+
var i;
52+
53+
arr = new Complex64Array( 5 );
54+
55+
values = [
56+
'5',
57+
5,
58+
NaN,
59+
true,
60+
false,
61+
null,
62+
void 0,
63+
{},
64+
[],
65+
function noop() {}
66+
];
67+
for ( i = 0; i < values.length; i++ ) {
68+
t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] );
69+
}
70+
t.end();
71+
72+
function badValue( value ) {
73+
return function badValue() {
74+
return arr.map.reduce( value, caddf );
75+
};
76+
}
77+
});
78+
79+
tape( 'the method throws an error if provided a first argument which is not a function', function test( t ) {
80+
var values;
81+
var arr;
82+
var i;
83+
84+
arr = new Complex64Array( 10 );
85+
86+
values = [
87+
'5',
88+
3.14,
89+
NaN,
90+
true,
91+
false,
92+
null,
93+
void 0,
94+
{},
95+
[]
96+
];
97+
for ( i = 0; i < values.length; i++ ) {
98+
t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] );
99+
}
100+
t.end();
101+
102+
function badValue( value ) {
103+
return function badValue() {
104+
return arr.reduce( value );
105+
};
106+
}
107+
});
108+
109+
tape( 'the method throws an error if not provided an initial value when operating on an empty complex number array', function test( t ) {
110+
var arr;
111+
112+
arr = new Complex64Array( 0 );
113+
t.throws( foo, Error, 'throws an error' );
114+
t.end();
115+
116+
function foo() {
117+
arr.reduce( caddf );
118+
}
119+
});
120+
121+
tape( 'the method uses the first element of the array as the initial value when an initial value is not provided', function test( t ) {
122+
var valueArray;
123+
var accArray;
124+
var expected;
125+
var actual;
126+
var arr;
127+
128+
arr = new Complex64Array( [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0 ] );
129+
accArray = [ 1.0, -1.0, 3.0, -3.0 ];
130+
valueArray = [ 2.0, -2.0, 3.0, -3.0 ];
131+
expected = new Complex64( 6.0, -6.0 );
132+
actual = arr.reduce( reducer );
133+
134+
t.strictEqual( instanceOf( actual, Complex64 ), true, 'returns expected value' );
135+
t.strictEqual( realf( actual ), realf( expected ), 'returns expected value' );
136+
t.strictEqual( imagf( actual ), imagf( expected ), 'returns expected value' );
137+
138+
t.end();
139+
140+
function reducer( acc, value, index ) {
141+
var ind = 2*(index-1);
142+
t.strictEqual( instanceOf( acc, Complex64 ), true, 'returns expected value' );
143+
t.strictEqual( realf( acc ), accArray[ ind ], 'returns expected value' );
144+
t.strictEqual( imagf( acc ), accArray[ ind+1 ], 'returns expected value' );
145+
t.strictEqual( instanceOf( value, Complex64 ), true, 'returns expected value' );
146+
t.strictEqual( realf( value ), valueArray[ ind ], 'returns expected value' );
147+
t.strictEqual( imagf( value ), valueArray[ ind+1 ], 'returns expected value' );
148+
return caddf( acc, value );
149+
}
150+
});
151+
152+
tape( 'the method supports providing an initial value as the second argument', function test( t ) {
153+
var valueArray;
154+
var accArray;
155+
var expected;
156+
var actual;
157+
var arr;
158+
159+
arr = new Complex64Array( [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0 ] );
160+
accArray = [ 2.0, -2.0, 3.0, -3.0, 5.0, -5.0 ];
161+
valueArray = [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0 ];
162+
expected = new Complex64( 8.0, -8.0 );
163+
actual = arr.reduce( reducer, new Complex64( 2.0, -2.0 ) );
164+
165+
t.strictEqual( instanceOf( actual, Complex64 ), true, 'returns expected value' );
166+
t.strictEqual( realf( actual ), realf( expected ), 'returns expected value' );
167+
t.strictEqual( imagf( actual ), imagf( expected ), 'returns expected value' );
168+
169+
t.end();
170+
171+
function reducer( acc, value, index ) {
172+
t.strictEqual( instanceOf( acc, Complex64 ), true, 'returns expected value' );
173+
t.strictEqual( realf( acc ), accArray[ 2*index ], 'returns expected value' );
174+
t.strictEqual( imagf( acc ), accArray[ (2*index)+1 ], 'returns expected value' );
175+
t.strictEqual( instanceOf( value, Complex64 ), true, 'returns expected value' );
176+
t.strictEqual( realf( value ), valueArray[ 2*index ], 'returns expected value' );
177+
t.strictEqual( imagf( value ), valueArray[ (2*index)+1 ], 'returns expected value' );
178+
return caddf( acc, value );
179+
}
180+
});
181+
182+
tape( 'the method returns the accumulated result', function test( t ) {
183+
var expected;
184+
var actual;
185+
var arr;
186+
187+
arr = new Complex64Array( [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0 ] );
188+
expected = new Complex64( 6.0, -6.0 );
189+
actual = arr.reduce( caddf );
190+
191+
t.strictEqual( instanceOf( actual, Complex64 ), true, 'returns expected value' );
192+
t.strictEqual( realf( actual ), realf( expected ), 'returns expected value' );
193+
t.strictEqual( imagf( actual ), imagf( expected ), 'returns expected value' );
194+
t.end();
195+
});
196+
197+
tape( 'the method supports returning real-valued results', function test( t ) {
198+
var expected;
199+
var actual;
200+
var arr;
201+
202+
arr = new Complex64Array( [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0 ] );
203+
expected = 6.0;
204+
actual = arr.reduce( reducer, 0.0 );
205+
206+
t.strictEqual( actual, expected, 'returns expected value' );
207+
t.end();
208+
209+
function reducer( acc, value ) {
210+
return acc + realf( value );
211+
}
212+
});

0 commit comments

Comments
 (0)
Please sign in to comment.