@stdlib/array-complex64 0.1.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/main.js CHANGED
@@ -28,10 +28,13 @@ var isCollection = require( '@stdlib/assert-is-collection' );
28
28
  var isArrayBuffer = require( '@stdlib/assert-is-arraybuffer' );
29
29
  var isObject = require( '@stdlib/assert-is-object' );
30
30
  var isArray = require( '@stdlib/assert-is-array' );
31
+ var isString = require( '@stdlib/assert-is-string' ).isPrimitive;
31
32
  var isFunction = require( '@stdlib/assert-is-function' );
32
33
  var isComplexLike = require( '@stdlib/assert-is-complex-like' );
33
34
  var isEven = require( '@stdlib/math-base-assert-is-even' );
34
35
  var isInteger = require( '@stdlib/math-base-assert-is-integer' );
36
+ var isComplex64Array = require( '@stdlib/array-base-assert-is-complex64array' );
37
+ var isComplex128Array = require( '@stdlib/array-base-assert-is-complex128array' );
35
38
  var hasIteratorSymbolSupport = require( '@stdlib/assert-has-iterator-symbol-support' );
36
39
  var ITERATOR_SYMBOL = require( '@stdlib/symbol-iterator' );
37
40
  var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );
@@ -41,6 +44,7 @@ var Complex64 = require( '@stdlib/complex-float32' );
41
44
  var format = require( '@stdlib/string-format' );
42
45
  var realf = require( '@stdlib/complex-realf' );
43
46
  var imagf = require( '@stdlib/complex-imagf' );
47
+ var floor = require( '@stdlib/math-base-special-floor' );
44
48
  var reinterpret64 = require( '@stdlib/strided-base-reinterpret-complex64' );
45
49
  var reinterpret128 = require( '@stdlib/strided-base-reinterpret-complex128' );
46
50
  var getter = require( '@stdlib/array-base-getter' );
@@ -100,35 +104,16 @@ function isComplexArrayConstructor( value ) {
100
104
  }
101
105
 
102
106
  /**
103
- * Returns a boolean indicating if a value is a `Complex64Array`.
107
+ * Retrieves a complex number from a complex number array buffer.
104
108
  *
105
109
  * @private
106
- * @param {*} value - value to test
107
- * @returns {boolean} boolean indicating if a value is a `Complex64Array`
108
- */
109
- function isComplex64Array( value ) {
110
- return (
111
- typeof value === 'object' &&
112
- value !== null &&
113
- value.constructor.name === 'Complex64Array' &&
114
- value.BYTES_PER_ELEMENT === BYTES_PER_ELEMENT
115
- );
116
- }
117
-
118
- /**
119
- * Returns a boolean indicating if a value is a `Complex128Array`.
120
- *
121
- * @private
122
- * @param {*} value - value to test
123
- * @returns {boolean} boolean indicating if a value is a `Complex128Array`
110
+ * @param {Float32Array} buf - array buffer
111
+ * @param {NonNegativeInteger} idx - element index
112
+ * @returns {Complex64} complex number
124
113
  */
125
- function isComplex128Array( value ) {
126
- return (
127
- typeof value === 'object' &&
128
- value !== null &&
129
- value.constructor.name === 'Complex128Array' &&
130
- value.BYTES_PER_ELEMENT === BYTES_PER_ELEMENT*2
131
- );
114
+ function getComplex64( buf, idx ) {
115
+ idx *= 2;
116
+ return new Complex64( buf[ idx ], buf[ idx+1 ] );
132
117
  }
133
118
 
134
119
 
@@ -268,7 +253,7 @@ function Complex64Array() {
268
253
  }
269
254
  buf = buf[ ITERATOR_SYMBOL ]();
270
255
  if ( !isFunction( buf.next ) ) {
271
- throw new TypeError( format( 'invalid argument. Must provide a length, ArrayBuffer, typed array, array-like object, or an iterable. Value: `%s`.', buf ) );
256
+ throw new TypeError( format( 'invalid argument. Must provide a length, ArrayBuffer, typed array, array-like object, or an iterable. Value: `%s`.', buf ) ); // FIXME: `buf` is what is returned from above, NOT the original value
272
257
  }
273
258
  buf = fromIterator( buf );
274
259
  if ( buf instanceof Error ) {
@@ -552,6 +537,75 @@ setReadOnly( Complex64Array, 'of', function of() {
552
537
  return new this( args );
553
538
  });
554
539
 
540
+ /**
541
+ * Returns an array element with support for both nonnegative and negative integer indices.
542
+ *
543
+ * @name at
544
+ * @memberof Complex64Array.prototype
545
+ * @type {Function}
546
+ * @param {integer} idx - element index
547
+ * @throws {TypeError} `this` must be a complex number array
548
+ * @throws {TypeError} must provide an integer
549
+ * @returns {(Complex64|void)} array element
550
+ *
551
+ * @example
552
+ * var arr = new Complex64Array( 10 );
553
+ * var realf = require( '@stdlib/complex-realf' );
554
+ * var imagf = require( '@stdlib/complex-imagf' );
555
+ *
556
+ * var z = arr.at( 0 );
557
+ * // returns <Complex64>
558
+ *
559
+ * var re = realf( z );
560
+ * // returns 0.0
561
+ *
562
+ * var im = imagf( z );
563
+ * // returns 0.0
564
+ *
565
+ * arr.set( [ 1.0, -1.0 ], 0 );
566
+ * arr.set( [ 2.0, -2.0 ], 1 );
567
+ * arr.set( [ 9.0, -9.0 ], 9 );
568
+ *
569
+ * z = arr.at( 0 );
570
+ * // returns <Complex64>
571
+ *
572
+ * re = realf( z );
573
+ * // returns 1.0
574
+ *
575
+ * im = imagf( z );
576
+ * // returns -1.0
577
+ *
578
+ * z = arr.at( -1 );
579
+ * // returns <Complex64>
580
+ *
581
+ * re = realf( z );
582
+ * // returns 9.0
583
+ *
584
+ * im = imagf( z );
585
+ * // returns -9.0
586
+ *
587
+ * z = arr.at( 100 );
588
+ * // returns undefined
589
+ *
590
+ * z = arr.at( -100 );
591
+ * // returns undefined
592
+ */
593
+ setReadOnly( Complex64Array.prototype, 'at', function at( idx ) {
594
+ if ( !isComplexArray( this ) ) {
595
+ throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
596
+ }
597
+ if ( !isInteger( idx ) ) {
598
+ throw new TypeError( format( 'invalid argument. Must provide an integer. Value: `%s`.', idx ) );
599
+ }
600
+ if ( idx < 0 ) {
601
+ idx += this._length;
602
+ }
603
+ if ( idx < 0 || idx >= this._length ) {
604
+ return;
605
+ }
606
+ return getComplex64( this._buffer, idx );
607
+ });
608
+
555
609
  /**
556
610
  * Pointer to the underlying data buffer.
557
611
  *
@@ -791,182 +845,1102 @@ setReadOnly( Complex64Array.prototype, 'entries', function entries() {
791
845
  });
792
846
 
793
847
  /**
794
- * Returns an array element.
848
+ * Tests whether all elements in an array pass a test implemented by a predicate function.
795
849
  *
796
- * @name get
850
+ * @name every
797
851
  * @memberof Complex64Array.prototype
798
852
  * @type {Function}
799
- * @param {NonNegativeInteger} idx - element index
853
+ * @param {Function} predicate - test function
854
+ * @param {*} [thisArg] - predicate function execution context
800
855
  * @throws {TypeError} `this` must be a complex number array
801
- * @throws {TypeError} must provide a nonnegative integer
802
- * @returns {(Complex64|void)} array element
856
+ * @throws {TypeError} first argument must be a function
857
+ * @returns {boolean} boolean indicating whether all elements pass a test
803
858
  *
804
859
  * @example
805
- * var arr = new Complex64Array( 10 );
806
860
  * var realf = require( '@stdlib/complex-realf' );
807
861
  * var imagf = require( '@stdlib/complex-imagf' );
808
862
  *
809
- * var z = arr.get( 0 );
863
+ * function predicate( v ) {
864
+ * return ( realf( v ) === imagf( v ) );
865
+ * }
866
+ *
867
+ * var arr = new Complex64Array( 3 );
868
+ *
869
+ * arr.set( [ 1.0, 1.0 ], 0 );
870
+ * arr.set( [ 2.0, 2.0 ], 1 );
871
+ * arr.set( [ 3.0, 3.0 ], 2 );
872
+ *
873
+ * var bool = arr.every( predicate );
874
+ * // returns true
875
+ */
876
+ setReadOnly( Complex64Array.prototype, 'every', function every( predicate, thisArg ) {
877
+ var buf;
878
+ var i;
879
+ if ( !isComplexArray( this ) ) {
880
+ throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
881
+ }
882
+ if ( !isFunction( predicate ) ) {
883
+ throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
884
+ }
885
+ buf = this._buffer;
886
+ for ( i = 0; i < this._length; i++ ) {
887
+ if ( !predicate.call( thisArg, getComplex64( buf, i ), i, this ) ) {
888
+ return false;
889
+ }
890
+ }
891
+ return true;
892
+ });
893
+
894
+ /**
895
+ * Returns a modified typed array filled with a fill value.
896
+ *
897
+ * @name fill
898
+ * @memberof Complex64Array.prototype
899
+ * @type {Function}
900
+ * @param {ComplexLike} value - fill value
901
+ * @param {integer} [start=0] - starting index (inclusive)
902
+ * @param {integer} [end] - ending index (exclusive)
903
+ * @throws {TypeError} `this` must be a complex number array
904
+ * @throws {TypeError} first argument must be a complex number
905
+ * @throws {TypeError} second argument must be an integer
906
+ * @throws {TypeError} third argument must be an integer
907
+ *
908
+ * @example
909
+ * var realf = require( '@stdlib/complex-realf' );
910
+ * var imagf = require( '@stdlib/complex-imagf' );
911
+ *
912
+ * var arr = new Complex64Array( 3 );
913
+ *
914
+ * arr.fill( new Complex64( 1.0, 1.0 ), 1 );
915
+ *
916
+ * var z = arr.get( 1 );
810
917
  * // returns <Complex64>
811
918
  *
812
919
  * var re = realf( z );
813
- * // returns 0.0
920
+ * // returns 1.0
814
921
  *
815
922
  * var im = imagf( z );
816
- * // returns 0.0
817
- *
818
- * arr.set( [ 1.0, -1.0 ], 0 );
923
+ * // returns 1.0
819
924
  *
820
- * z = arr.get( 0 );
925
+ * z = arr.get( 1 );
821
926
  * // returns <Complex64>
822
927
  *
823
928
  * re = realf( z );
824
929
  * // returns 1.0
825
930
  *
826
931
  * im = imagf( z );
827
- * // returns -1.0
828
- *
829
- * z = arr.get( 100 );
830
- * // returns undefined
932
+ * // returns 1.0
831
933
  */
832
- setReadOnly( Complex64Array.prototype, 'get', function get( idx ) {
934
+ setReadOnly( Complex64Array.prototype, 'fill', function fill( value, start, end ) {
833
935
  var buf;
936
+ var len;
937
+ var idx;
938
+ var re;
939
+ var im;
940
+ var i;
834
941
  if ( !isComplexArray( this ) ) {
835
942
  throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
836
943
  }
837
- if ( !isNonNegativeInteger( idx ) ) {
838
- throw new TypeError( format( 'invalid argument. Must provide a nonnegative integer. Value: `%s`.', idx ) );
839
- }
840
- if ( idx >= this._length ) {
841
- return;
944
+ if ( !isComplexLike( value ) ) {
945
+ throw new TypeError( format( 'invalid argument. First argument must be a complex number. Value: `%s`.', value ) );
842
946
  }
843
947
  buf = this._buffer;
844
- idx *= 2;
845
- return new Complex64( buf[ idx ], buf[ idx+1 ] );
948
+ len = this._length;
949
+ if ( arguments.length > 1 ) {
950
+ if ( !isInteger( start ) ) {
951
+ throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', start ) );
952
+ }
953
+ if ( start < 0 ) {
954
+ start += len;
955
+ if ( start < 0 ) {
956
+ start = 0;
957
+ }
958
+ }
959
+ if ( arguments.length > 2 ) {
960
+ if ( !isInteger( end ) ) {
961
+ throw new TypeError( format( 'invalid argument. Third argument must be an integer. Value: `%s`.', end ) );
962
+ }
963
+ if ( end < 0 ) {
964
+ end += len;
965
+ if ( end < 0 ) {
966
+ end = 0;
967
+ }
968
+ }
969
+ if ( end > len ) {
970
+ end = len;
971
+ }
972
+ } else {
973
+ end = len;
974
+ }
975
+ } else {
976
+ start = 0;
977
+ end = len;
978
+ }
979
+ re = realf( value );
980
+ im = imagf( value );
981
+ for ( i = start; i < end; i++ ) {
982
+ idx = 2*i;
983
+ buf[ idx ] = re;
984
+ buf[ idx+1 ] = im;
985
+ }
986
+ return this;
846
987
  });
847
988
 
848
989
  /**
849
- * Number of array elements.
990
+ * Returns a new array containing the elements of an array which pass a test implemented by a predicate function.
850
991
  *
851
- * @name length
992
+ * @name filter
852
993
  * @memberof Complex64Array.prototype
853
- * @readonly
854
- * @type {NonNegativeInteger}
994
+ * @type {Function}
995
+ * @param {Function} predicate - test function
996
+ * @param {*} [thisArg] - predicate function execution context
997
+ * @throws {TypeError} `this` must be a complex number array
998
+ * @throws {TypeError} first argument must be a function
999
+ * @returns {Complex64Array} complex number array
855
1000
  *
856
1001
  * @example
857
- * var arr = new Complex64Array( 10 );
858
- *
859
- * var len = arr.length;
860
- * // returns 10
861
- */
862
- setReadOnlyAccessor( Complex64Array.prototype, 'length', function get() {
863
- return this._length;
864
- });
865
-
866
- /**
867
- * Sets an array element.
1002
+ * var realf = require( '@stdlib/complex-realf' );
1003
+ * var imagf = require( '@stdlib/complex-imagf' );
868
1004
  *
869
- * ## Notes
1005
+ * function predicate( v ) {
1006
+ * return ( realf( v ) === imagf( v ) );
1007
+ * }
870
1008
  *
871
- * - When provided a typed array, real or complex, we must check whether the source array shares the same buffer as the target array and whether the underlying memory overlaps. In particular, we are concerned with the following scenario:
1009
+ * var arr = new Complex64Array( 3 );
872
1010
  *
873
- * ```text
874
- * buf: ---------------------
875
- * src: ---------------------
876
- * ```
1011
+ * arr.set( [ 1.0, -1.0 ], 0 );
1012
+ * arr.set( [ 2.0, 2.0 ], 1 );
1013
+ * arr.set( [ 3.0, -3.0 ], 2 );
877
1014
  *
878
- * In the above, as we copy values from `src`, we will overwrite values in the `src` view, resulting in duplicated values copied into the end of `buf`, which is not intended. Hence, to avoid overwriting source values, we must **copy** source values to a temporary array.
1015
+ * var out = arr.filter( predicate );
1016
+ * // returns <Complex64Array>
879
1017
  *
880
- * In the other overlapping scenario,
1018
+ * var len = out.length;
1019
+ * // returns 1
881
1020
  *
882
- * ```text
883
- * buf: ---------------------
884
- * src: ---------------------
885
- * ```
1021
+ * var z = out.get( 0 );
1022
+ * // returns <Complex64>
886
1023
  *
887
- * by the time we begin copying into the overlapping region, we are copying from the end of `src`, a non-overlapping region, which means we don't run the risk of copying copied values, rather than the original `src` values as intended.
1024
+ * var re = realf( z );
1025
+ * // returns 2.0
888
1026
  *
1027
+ * var im = imagf( z );
1028
+ * // returns 2.0
1029
+ */
1030
+ setReadOnly( Complex64Array.prototype, 'filter', function filter( predicate, thisArg ) {
1031
+ var buf;
1032
+ var out;
1033
+ var i;
1034
+ var z;
1035
+ if ( !isComplexArray( this ) ) {
1036
+ throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
1037
+ }
1038
+ if ( !isFunction( predicate ) ) {
1039
+ throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
1040
+ }
1041
+ buf = this._buffer;
1042
+ out = [];
1043
+ for ( i = 0; i < this._length; i++ ) {
1044
+ z = getComplex64( buf, i );
1045
+ if ( predicate.call( thisArg, z, i, this ) ) {
1046
+ out.push( z );
1047
+ }
1048
+ }
1049
+ return new this.constructor( out );
1050
+ });
1051
+
1052
+ /**
1053
+ * Returns the first element in an array for which a predicate function returns a truthy value.
889
1054
  *
890
- * @name set
1055
+ * @name find
891
1056
  * @memberof Complex64Array.prototype
892
1057
  * @type {Function}
893
- * @param {(Collection|Complex|ComplexArray)} value - value(s)
894
- * @param {NonNegativeInteger} [i=0] - element index at which to start writing values
1058
+ * @param {Function} predicate - test function
1059
+ * @param {*} [thisArg] - predicate function execution context
895
1060
  * @throws {TypeError} `this` must be a complex number array
896
- * @throws {TypeError} first argument must be either a complex number, an array-like object, or a complex number array
897
- * @throws {TypeError} index argument must be a nonnegative integer
898
- * @throws {RangeError} array-like objects must have a length which is a multiple of two
899
- * @throws {RangeError} index argument is out-of-bounds
900
- * @throws {RangeError} target array lacks sufficient storage to accommodate source values
901
- * @returns {void}
1061
+ * @throws {TypeError} first argument must be a function
1062
+ * @returns {(Complex64|void)} array element or undefined
902
1063
  *
903
1064
  * @example
904
1065
  * var realf = require( '@stdlib/complex-realf' );
905
1066
  * var imagf = require( '@stdlib/complex-imagf' );
1067
+ * var Complex64 = require( '@stdlib/complex-float32' );
906
1068
  *
907
- * var arr = new Complex64Array( 10 );
908
- *
909
- * var z = arr.get( 0 );
910
- * // returns <Complex64>
911
- *
912
- * var re = realf( z );
913
- * // returns 0.0
1069
+ * function predicate( v ) {
1070
+ * return ( realf( v ) === imagf( v ) );
1071
+ * }
914
1072
  *
915
- * var im = imagf( z );
916
- * // returns 0.0
1073
+ * var arr = new Complex64Array( 3 );
917
1074
  *
918
- * arr.set( [ 1.0, -1.0 ], 0 );
1075
+ * arr.set( [ 1.0, 1.0 ], 0 );
1076
+ * arr.set( [ 2.0, 2.0 ], 1 );
1077
+ * arr.set( [ 3.0, 3.0 ], 2 );
919
1078
  *
920
- * z = arr.get( 0 );
1079
+ * var z = arr.find( predicate );
921
1080
  * // returns <Complex64>
922
1081
  *
923
- * re = realf( z );
1082
+ * var re = realf( z );
924
1083
  * // returns 1.0
925
1084
  *
926
- * im = imagf( z );
927
- * // returns -1.0
1085
+ * var im = imagf( z );
1086
+ * // returns 1.0
928
1087
  */
929
- setReadOnly( Complex64Array.prototype, 'set', function set( value ) {
930
- /* eslint-disable no-underscore-dangle */
931
- var sbuf;
932
- var idx;
1088
+ setReadOnly( Complex64Array.prototype, 'find', function find( predicate, thisArg ) {
933
1089
  var buf;
934
- var tmp;
935
- var flg;
936
- var N;
937
- var v;
938
1090
  var i;
939
- var j;
1091
+ var z;
940
1092
  if ( !isComplexArray( this ) ) {
941
1093
  throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
942
1094
  }
943
- buf = this._buffer;
944
- if ( arguments.length > 1 ) {
945
- idx = arguments[ 1 ];
946
- if ( !isNonNegativeInteger( idx ) ) {
947
- throw new TypeError( format( 'invalid argument. Index argument must be a nonnegative integer. Value: `%s`.', idx ) );
948
- }
949
- } else {
950
- idx = 0;
1095
+ if ( !isFunction( predicate ) ) {
1096
+ throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
951
1097
  }
952
- if ( isComplexLike( value ) ) {
953
- if ( idx >= this._length ) {
954
- throw new RangeError( format( 'invalid argument. Index argument is out-of-bounds. Value: `%u`.', idx ) );
1098
+ buf = this._buffer;
1099
+ for ( i = 0; i < this._length; i++ ) {
1100
+ z = getComplex64( buf, i );
1101
+ if ( predicate.call( thisArg, z, i, this ) ) {
1102
+ return z;
955
1103
  }
956
- idx *= 2;
957
- buf[ idx ] = realf( value );
958
- buf[ idx+1 ] = imagf( value );
959
- return;
960
1104
  }
961
- if ( isComplexArray( value ) ) {
962
- N = value._length;
963
- if ( idx+N > this._length ) {
964
- throw new RangeError( 'invalid arguments. Target array lacks sufficient storage to accommodate source values.' );
965
- }
966
- sbuf = value._buffer;
1105
+ });
967
1106
 
968
- // Check for overlapping memory...
969
- j = buf.byteOffset + (idx*BYTES_PER_ELEMENT);
1107
+ /**
1108
+ * Returns the index of the first element in an array for which a predicate function returns a truthy value.
1109
+ *
1110
+ * @name findIndex
1111
+ * @memberof Complex64Array.prototype
1112
+ * @type {Function}
1113
+ * @param {Function} predicate - test function
1114
+ * @param {*} [thisArg] - predicate function execution context
1115
+ * @throws {TypeError} `this` must be a complex number array
1116
+ * @throws {TypeError} first argument must be a function
1117
+ * @returns {integer} index or -1
1118
+ *
1119
+ * @example
1120
+ * var Complex64 = require( '@stdlib/complex-float32' );
1121
+ * var realf = require( '@stdlib/complex-realf' );
1122
+ * var imagf = require( '@stdlib/complex-imagf' );
1123
+ *
1124
+ * function predicate( v ) {
1125
+ * return ( realf( v ) === imagf( v ) );
1126
+ * }
1127
+ *
1128
+ * var arr = new Complex64Array( 3 );
1129
+ *
1130
+ * arr.set( [ 1.0, -1.0 ], 0 );
1131
+ * arr.set( [ 2.0, -2.0 ], 1 );
1132
+ * arr.set( [ 3.0, 3.0 ], 2 );
1133
+ *
1134
+ * var idx = arr.findIndex( predicate );
1135
+ * // returns 2
1136
+ */
1137
+ setReadOnly( Complex64Array.prototype, 'findIndex', function findIndex( predicate, thisArg ) {
1138
+ var buf;
1139
+ var i;
1140
+ var z;
1141
+ if ( !isComplexArray( this ) ) {
1142
+ throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
1143
+ }
1144
+ if ( !isFunction( predicate ) ) {
1145
+ throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
1146
+ }
1147
+ buf = this._buffer;
1148
+ for ( i = 0; i < this._length; i++ ) {
1149
+ z = getComplex64( buf, i );
1150
+ if ( predicate.call( thisArg, z, i, this ) ) {
1151
+ return i;
1152
+ }
1153
+ }
1154
+ return -1;
1155
+ });
1156
+
1157
+ /**
1158
+ * Returns the last element in an array for which a predicate function returns a truthy value.
1159
+ *
1160
+ * @name findLast
1161
+ * @memberof Complex64Array.prototype
1162
+ * @type {Function}
1163
+ * @param {Function} predicate - test function
1164
+ * @param {*} [thisArg] - predicate function execution context
1165
+ * @throws {TypeError} `this` must be a complex number array
1166
+ * @throws {TypeError} first argument must be a function
1167
+ * @returns {(Complex64|void)} array element or undefined
1168
+ *
1169
+ * @example
1170
+ * var realf = require( '@stdlib/complex-realf' );
1171
+ * var imagf = require( '@stdlib/complex-imagf' );
1172
+ * var Complex64 = require( '@stdlib/complex-float32' );
1173
+ *
1174
+ * function predicate( v ) {
1175
+ * return ( realf( v ) === imagf( v ) );
1176
+ * }
1177
+ *
1178
+ * var arr = new Complex64Array( 3 );
1179
+ *
1180
+ * arr.set( [ 1.0, 1.0 ], 0 );
1181
+ * arr.set( [ 2.0, 2.0 ], 1 );
1182
+ * arr.set( [ 3.0, 3.0 ], 2 );
1183
+ *
1184
+ * var z = arr.findLast( predicate );
1185
+ * // returns <Complex64>
1186
+ *
1187
+ * var re = realf( z );
1188
+ * // returns 3.0
1189
+ *
1190
+ * var im = imagf( z );
1191
+ * // returns 3.0
1192
+ */
1193
+ setReadOnly( Complex64Array.prototype, 'findLast', function findLast( predicate, thisArg ) {
1194
+ var buf;
1195
+ var i;
1196
+ var z;
1197
+ if ( !isComplexArray( this ) ) {
1198
+ throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
1199
+ }
1200
+ if ( !isFunction( predicate ) ) {
1201
+ throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
1202
+ }
1203
+ buf = this._buffer;
1204
+ for ( i = this._length-1; i >= 0; i-- ) {
1205
+ z = getComplex64( buf, i );
1206
+ if ( predicate.call( thisArg, z, i, this ) ) {
1207
+ return z;
1208
+ }
1209
+ }
1210
+ });
1211
+
1212
+ /**
1213
+ * Returns the index of the last element in an array for which a predicate function returns a truthy value.
1214
+ *
1215
+ * @name findLastIndex
1216
+ * @memberof Complex64Array.prototype
1217
+ * @type {Function}
1218
+ * @param {Function} predicate - test function
1219
+ * @param {*} [thisArg] - predicate function execution context
1220
+ * @throws {TypeError} `this` must be a complex number array
1221
+ * @throws {TypeError} first argument must be a function
1222
+ * @returns {integer} index or -1
1223
+ *
1224
+ * @example
1225
+ * var Complex64 = require( '@stdlib/complex-float32' );
1226
+ * var realf = require( '@stdlib/complex-realf' );
1227
+ * var imagf = require( '@stdlib/complex-imagf' );
1228
+ *
1229
+ * function predicate( v ) {
1230
+ * return ( realf( v ) === imagf( v ) );
1231
+ * }
1232
+ *
1233
+ * var arr = new Complex64Array( 3 );
1234
+ *
1235
+ * arr.set( [ 1.0, 1.0 ], 0 );
1236
+ * arr.set( [ 2.0, 2.0 ], 1 );
1237
+ * arr.set( [ 3.0, -3.0 ], 2 );
1238
+ *
1239
+ * var idx = arr.findLastIndex( predicate );
1240
+ * // returns 1
1241
+ */
1242
+ setReadOnly( Complex64Array.prototype, 'findLastIndex', function findLastIndex( predicate, thisArg ) {
1243
+ var buf;
1244
+ var i;
1245
+ var z;
1246
+ if ( !isComplexArray( this ) ) {
1247
+ throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
1248
+ }
1249
+ if ( !isFunction( predicate ) ) {
1250
+ throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
1251
+ }
1252
+ buf = this._buffer;
1253
+ for ( i = this._length-1; i >= 0; i-- ) {
1254
+ z = getComplex64( buf, i );
1255
+ if ( predicate.call( thisArg, z, i, this ) ) {
1256
+ return i;
1257
+ }
1258
+ }
1259
+ return -1;
1260
+ });
1261
+
1262
+ /**
1263
+ * Invokes a function once for each array element.
1264
+ *
1265
+ * @name forEach
1266
+ * @memberof Complex64Array.prototype
1267
+ * @type {Function}
1268
+ * @param {Function} fcn - function to invoke
1269
+ * @param {*} [thisArg] - function invocation context
1270
+ * @throws {TypeError} `this` must be a complex number array
1271
+ * @throws {TypeError} first argument must be a function
1272
+ *
1273
+ * @example
1274
+ * var Complex64 = require( '@stdlib/complex-float32' );
1275
+ *
1276
+ * function log( v, i ) {
1277
+ * console.log( '%s: %s', i, v.toString() );
1278
+ * }
1279
+ *
1280
+ * var arr = new Complex64Array( 3 );
1281
+ *
1282
+ * arr.set( [ 1.0, 1.0 ], 0 );
1283
+ * arr.set( [ 2.0, 2.0 ], 1 );
1284
+ * arr.set( [ 3.0, 3.0 ], 2 );
1285
+ *
1286
+ * arr.forEach( log );
1287
+ */
1288
+ setReadOnly( Complex64Array.prototype, 'forEach', function forEach( fcn, thisArg ) {
1289
+ var buf;
1290
+ var i;
1291
+ var z;
1292
+ if ( !isComplexArray( this ) ) {
1293
+ throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
1294
+ }
1295
+ if ( !isFunction( fcn ) ) {
1296
+ throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', fcn ) );
1297
+ }
1298
+ buf = this._buffer;
1299
+ for ( i = 0; i < this._length; i++ ) {
1300
+ z = getComplex64( buf, i );
1301
+ fcn.call( thisArg, z, i, this );
1302
+ }
1303
+ });
1304
+
1305
+ /**
1306
+ * Returns an array element.
1307
+ *
1308
+ * @name get
1309
+ * @memberof Complex64Array.prototype
1310
+ * @type {Function}
1311
+ * @param {NonNegativeInteger} idx - element index
1312
+ * @throws {TypeError} `this` must be a complex number array
1313
+ * @throws {TypeError} must provide a nonnegative integer
1314
+ * @returns {(Complex64|void)} array element
1315
+ *
1316
+ * @example
1317
+ * var arr = new Complex64Array( 10 );
1318
+ * var realf = require( '@stdlib/complex-realf' );
1319
+ * var imagf = require( '@stdlib/complex-imagf' );
1320
+ *
1321
+ * var z = arr.get( 0 );
1322
+ * // returns <Complex64>
1323
+ *
1324
+ * var re = realf( z );
1325
+ * // returns 0.0
1326
+ *
1327
+ * var im = imagf( z );
1328
+ * // returns 0.0
1329
+ *
1330
+ * arr.set( [ 1.0, -1.0 ], 0 );
1331
+ *
1332
+ * z = arr.get( 0 );
1333
+ * // returns <Complex64>
1334
+ *
1335
+ * re = realf( z );
1336
+ * // returns 1.0
1337
+ *
1338
+ * im = imagf( z );
1339
+ * // returns -1.0
1340
+ *
1341
+ * z = arr.get( 100 );
1342
+ * // returns undefined
1343
+ */
1344
+ setReadOnly( Complex64Array.prototype, 'get', function get( idx ) {
1345
+ if ( !isComplexArray( this ) ) {
1346
+ throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
1347
+ }
1348
+ if ( !isNonNegativeInteger( idx ) ) {
1349
+ throw new TypeError( format( 'invalid argument. Must provide a nonnegative integer. Value: `%s`.', idx ) );
1350
+ }
1351
+ if ( idx >= this._length ) {
1352
+ return;
1353
+ }
1354
+ return getComplex64( this._buffer, idx );
1355
+ });
1356
+
1357
+ /**
1358
+ * Returns a boolean indicating whether an array includes a provided value.
1359
+ *
1360
+ * @name includes
1361
+ * @memberof Complex64Array.prototype
1362
+ * @type {Function}
1363
+ * @param {ComplexLike} searchElement - search element
1364
+ * @param {integer} [fromIndex=0] - starting index (inclusive)
1365
+ * @throws {TypeError} `this` must be a complex number array
1366
+ * @throws {TypeError} first argument must be a complex number
1367
+ * @throws {TypeError} second argument must be an integer
1368
+ * @returns {boolean} boolean indicating whether an array includes a provided value
1369
+ *
1370
+ * @example
1371
+ * var Complex64 = require( '@stdlib/complex-float32' );
1372
+ *
1373
+ * var arr = new Complex64Array( 5 );
1374
+ *
1375
+ * arr.set( [ 1.0, -1.0 ], 0 );
1376
+ * arr.set( [ 2.0, -2.0 ], 1 );
1377
+ * arr.set( [ 3.0, -3.0 ], 2 );
1378
+ * arr.set( [ 4.0, -4.0 ], 3 );
1379
+ * arr.set( [ 5.0, -5.0 ], 4 );
1380
+ *
1381
+ * var bool = arr.includes( new Complex64( 3.0, -3.0 ) );
1382
+ * // returns true
1383
+ *
1384
+ * bool = arr.includes( new Complex64( 3.0, -3.0 ), 3 );
1385
+ * // returns false
1386
+ *
1387
+ * bool = arr.includes( new Complex64( 4.0, -4.0 ), -3 );
1388
+ * // returns true
1389
+ */
1390
+ setReadOnly( Complex64Array.prototype, 'includes', function includes( searchElement, fromIndex ) {
1391
+ var buf;
1392
+ var idx;
1393
+ var re;
1394
+ var im;
1395
+ var i;
1396
+ if ( !isComplexArray( this ) ) {
1397
+ throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
1398
+ }
1399
+ if ( !isComplexLike( searchElement ) ) {
1400
+ throw new TypeError( format( 'invalid argument. First argument must be a complex number. Value: `%s`.', searchElement ) );
1401
+ }
1402
+ if ( arguments.length > 1 ) {
1403
+ if ( !isInteger( fromIndex ) ) {
1404
+ throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', fromIndex ) );
1405
+ }
1406
+ if ( fromIndex < 0 ) {
1407
+ fromIndex += this._length;
1408
+ if ( fromIndex < 0 ) {
1409
+ fromIndex = 0;
1410
+ }
1411
+ }
1412
+ } else {
1413
+ fromIndex = 0;
1414
+ }
1415
+ re = realf( searchElement );
1416
+ im = imagf( searchElement );
1417
+ buf = this._buffer;
1418
+ for ( i = fromIndex; i < this._length; i++ ) {
1419
+ idx = 2 * i;
1420
+ if ( re === buf[ idx ] && im === buf[ idx+1 ] ) {
1421
+ return true;
1422
+ }
1423
+ }
1424
+ return false;
1425
+ });
1426
+
1427
+ /**
1428
+ * Returns the first index at which a given element can be found.
1429
+ *
1430
+ * @name indexOf
1431
+ * @memberof Complex64Array.prototype
1432
+ * @type {Function}
1433
+ * @param {ComplexLike} searchElement - element to find
1434
+ * @param {integer} [fromIndex=0] - starting index (inclusive)
1435
+ * @throws {TypeError} `this` must be a complex number array
1436
+ * @throws {TypeError} first argument must be a complex number
1437
+ * @throws {TypeError} second argument must be an integer
1438
+ * @returns {integer} index or -1
1439
+ *
1440
+ * @example
1441
+ * var Complex64 = require( '@stdlib/complex-float32' );
1442
+ *
1443
+ * var arr = new Complex64Array( 10 );
1444
+ *
1445
+ * arr.set( [ 1.0, -1.0 ], 0 );
1446
+ * arr.set( [ 2.0, -2.0 ], 1 );
1447
+ * arr.set( [ 3.0, -3.0 ], 2 );
1448
+ * arr.set( [ 4.0, -4.0 ], 3 );
1449
+ * arr.set( [ 5.0, -5.0 ], 4 );
1450
+ *
1451
+ * var idx = arr.indexOf( new Complex64( 3.0, -3.0 ) );
1452
+ * // returns 2
1453
+ *
1454
+ * idx = arr.indexOf( new Complex64( 3.0, -3.0 ), 3 );
1455
+ * // returns -1
1456
+ *
1457
+ * idx = arr.indexOf( new Complex64( 4.0, -4.0 ), -3 );
1458
+ * // returns -1
1459
+ */
1460
+ setReadOnly( Complex64Array.prototype, 'indexOf', function indexOf( searchElement, fromIndex ) {
1461
+ var buf;
1462
+ var idx;
1463
+ var re;
1464
+ var im;
1465
+ var i;
1466
+ if ( !isComplexArray( this ) ) {
1467
+ throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
1468
+ }
1469
+ if ( !isComplexLike( searchElement ) ) {
1470
+ throw new TypeError( format( 'invalid argument. First argument must be a complex number. Value: `%s`.', searchElement ) );
1471
+ }
1472
+ if ( arguments.length > 1 ) {
1473
+ if ( !isInteger( fromIndex ) ) {
1474
+ throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', fromIndex ) );
1475
+ }
1476
+ if ( fromIndex < 0 ) {
1477
+ fromIndex += this._length;
1478
+ if ( fromIndex < 0 ) {
1479
+ fromIndex = 0;
1480
+ }
1481
+ }
1482
+ } else {
1483
+ fromIndex = 0;
1484
+ }
1485
+ re = realf( searchElement );
1486
+ im = imagf( searchElement );
1487
+ buf = this._buffer;
1488
+ for ( i = fromIndex; i < this._length; i++ ) {
1489
+ idx = 2 * i;
1490
+ if ( re === buf[ idx ] && im === buf[ idx+1 ] ) {
1491
+ return i;
1492
+ }
1493
+ }
1494
+ return -1;
1495
+ });
1496
+
1497
+ /**
1498
+ * Returns a new string by concatenating all array elements.
1499
+ *
1500
+ * @name join
1501
+ * @memberof Complex64Array.prototype
1502
+ * @type {Function}
1503
+ * @param {string} [separator=','] - element separator
1504
+ * @throws {TypeError} `this` must be a complex number array
1505
+ * @throws {TypeError} first argument must be a string
1506
+ * @returns {string} string representation
1507
+ *
1508
+ * @example
1509
+ * var arr = new Complex64Array( 2 );
1510
+ *
1511
+ * arr.set( [ 1.0, 1.0 ], 0 );
1512
+ * arr.set( [ 2.0, 2.0 ], 1 );
1513
+ *
1514
+ * var str = arr.join();
1515
+ * // returns '1 + 1i,2 + 2i'
1516
+ *
1517
+ * str = arr.join( '/' );
1518
+ * // returns '1 + 1i/2 + 2i'
1519
+ */
1520
+ setReadOnly( Complex64Array.prototype, 'join', function join( separator ) {
1521
+ var out;
1522
+ var buf;
1523
+ var sep;
1524
+ var i;
1525
+ if ( !isComplexArray( this ) ) {
1526
+ throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
1527
+ }
1528
+ if ( arguments.length === 0 ) {
1529
+ sep = ',';
1530
+ } else if ( isString( separator ) ) {
1531
+ sep = separator;
1532
+ } else {
1533
+ throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', separator ) );
1534
+ }
1535
+ out = [];
1536
+ buf = this._buffer;
1537
+ for ( i = 0; i < this._length; i++ ) {
1538
+ out.push( getComplex64( buf, i ).toString() );
1539
+ }
1540
+ return out.join( sep );
1541
+ });
1542
+
1543
+ /**
1544
+ * Returns the last index at which a given element can be found.
1545
+ *
1546
+ * @name lastIndexOf
1547
+ * @memberof Complex64Array.prototype
1548
+ * @type {Function}
1549
+ * @param {ComplexLike} searchElement - element to find
1550
+ * @param {integer} [fromIndex] - index at which to start searching backward (inclusive)
1551
+ * @throws {TypeError} `this` must be a complex number array
1552
+ * @throws {TypeError} first argument must be a complex number
1553
+ * @throws {TypeError} second argument must be an integer
1554
+ * @returns {integer} index or -1
1555
+ *
1556
+ * @example
1557
+ * var Complex64 = require( '@stdlib/complex-float32' );
1558
+ *
1559
+ * var arr = new Complex64Array( 5 );
1560
+ *
1561
+ * arr.set( [ 1.0, -1.0 ], 0 );
1562
+ * arr.set( [ 2.0, -2.0 ], 1 );
1563
+ * arr.set( [ 3.0, -3.0 ], 2 );
1564
+ * arr.set( [ 4.0, -4.0 ], 3 );
1565
+ * arr.set( [ 3.0, -3.0 ], 4 );
1566
+ *
1567
+ * var idx = arr.lastIndexOf( new Complex64( 3.0, -3.0 ) );
1568
+ * // returns 4
1569
+ *
1570
+ * idx = arr.lastIndexOf( new Complex64( 3.0, -3.0 ), 3 );
1571
+ * // returns 2
1572
+ *
1573
+ * idx = arr.lastIndexOf( new Complex64( 5.0, -5.0 ), 3 );
1574
+ * // returns -1
1575
+ *
1576
+ * idx = arr.lastIndexOf( new Complex64( 2.0, -2.0 ), -3 );
1577
+ * // returns 1
1578
+ */
1579
+ setReadOnly( Complex64Array.prototype, 'lastIndexOf', function lastIndexOf( searchElement, fromIndex ) {
1580
+ var buf;
1581
+ var idx;
1582
+ var re;
1583
+ var im;
1584
+ var i;
1585
+ if ( !isComplexArray( this ) ) {
1586
+ throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
1587
+ }
1588
+ if ( !isComplexLike( searchElement ) ) {
1589
+ throw new TypeError( format( 'invalid argument. First argument must be a complex number. Value: `%s`.', searchElement ) );
1590
+ }
1591
+ if ( arguments.length > 1 ) {
1592
+ if ( !isInteger( fromIndex ) ) {
1593
+ throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', fromIndex ) );
1594
+ }
1595
+ if ( fromIndex >= this._length ) {
1596
+ fromIndex = this._length - 1;
1597
+ } else if ( fromIndex < 0 ) {
1598
+ fromIndex += this._length;
1599
+ }
1600
+ } else {
1601
+ fromIndex = this._length - 1;
1602
+ }
1603
+ re = realf( searchElement );
1604
+ im = imagf( searchElement );
1605
+ buf = this._buffer;
1606
+ for ( i = fromIndex; i >= 0; i-- ) {
1607
+ idx = 2 * i;
1608
+ if ( re === buf[ idx ] && im === buf[ idx+1 ] ) {
1609
+ return i;
1610
+ }
1611
+ }
1612
+ return -1;
1613
+ });
1614
+
1615
+ /**
1616
+ * Number of array elements.
1617
+ *
1618
+ * @name length
1619
+ * @memberof Complex64Array.prototype
1620
+ * @readonly
1621
+ * @type {NonNegativeInteger}
1622
+ *
1623
+ * @example
1624
+ * var arr = new Complex64Array( 10 );
1625
+ *
1626
+ * var len = arr.length;
1627
+ * // returns 10
1628
+ */
1629
+ setReadOnlyAccessor( Complex64Array.prototype, 'length', function get() {
1630
+ return this._length;
1631
+ });
1632
+
1633
+ /**
1634
+ * Returns a new array with each element being the result of a provided callback function.
1635
+ *
1636
+ * @name map
1637
+ * @memberof Complex64Array.prototype
1638
+ * @type {Function}
1639
+ * @param {Function} fcn - callback function
1640
+ * @param {*} [thisArg] - callback function execution context
1641
+ * @throws {TypeError} `this` must be a complex number array
1642
+ * @throws {TypeError} first argument must be a function
1643
+ * @returns {Complex64Array} complex number array
1644
+ *
1645
+ * @example
1646
+ * var Complex64 = require( '@stdlib/complex-float32' );
1647
+ * var realf = require( '@stdlib/complex-realf' );
1648
+ * var imagf = require( '@stdlib/complex-imagf' );
1649
+ *
1650
+ * function scale( v, i ) {
1651
+ * return new Complex64( 2.0*realf( v ), 2.0*imagf( v ) );
1652
+ * }
1653
+ *
1654
+ * var arr = new Complex64Array( 3 );
1655
+ *
1656
+ * arr.set( [ 1.0, -1.0 ], 0 );
1657
+ * arr.set( [ 2.0, -2.0 ], 1 );
1658
+ * arr.set( [ 3.0, -3.0 ], 2 );
1659
+ *
1660
+ * var out = arr.map( scale );
1661
+ * // returns <Complex64Array>
1662
+ *
1663
+ * var z = out.get( 0 );
1664
+ * // returns <Complex64>
1665
+ *
1666
+ * var re = realf( z );
1667
+ * // returns 2
1668
+ *
1669
+ * var im = imagf( z );
1670
+ * // returns -2
1671
+ */
1672
+ setReadOnly( Complex64Array.prototype, 'map', function map( fcn, thisArg ) {
1673
+ var outbuf;
1674
+ var buf;
1675
+ var out;
1676
+ var i;
1677
+ var v;
1678
+ if ( !isComplexArray( this ) ) {
1679
+ throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
1680
+ }
1681
+ if ( !isFunction( fcn ) ) {
1682
+ throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', fcn ) );
1683
+ }
1684
+ buf = this._buffer;
1685
+ out = new this.constructor( this._length );
1686
+ outbuf = out._buffer; // eslint-disable-line no-underscore-dangle
1687
+ for ( i = 0; i < this._length; i++ ) {
1688
+ v = fcn.call( thisArg, getComplex64( buf, i ), i, this );
1689
+ if ( isComplexLike( v ) ) {
1690
+ outbuf[ 2*i ] = realf( v );
1691
+ outbuf[ (2*i)+1 ] = imagf( v );
1692
+ } else if ( isArrayLikeObject( v ) && v.length === 2 ) {
1693
+ outbuf[ 2*i ] = v[ 0 ];
1694
+ outbuf[ (2*i)+1 ] = v[ 1 ];
1695
+ } else {
1696
+ throw new TypeError( format( 'invalid argument. Callback must return either a two-element array containing real and imaginary components or a complex number. Value: `%s`.', v ) );
1697
+ }
1698
+ }
1699
+ return out;
1700
+ });
1701
+
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
+
1767
+ /**
1768
+ * Reverses an array in-place.
1769
+ *
1770
+ * @name reverse
1771
+ * @memberof Complex64Array.prototype
1772
+ * @type {Function}
1773
+ * @throws {TypeError} `this` must be a complex number array
1774
+ * @returns {Complex64Array} reversed array
1775
+ *
1776
+ * @example
1777
+ * var realf = require( '@stdlib/complex-realf' );
1778
+ * var imagf = require( '@stdlib/complex-imagf' );
1779
+ *
1780
+ * var arr = new Complex64Array( 3 );
1781
+ *
1782
+ * arr.set( [ 1.0, 1.0 ], 0 );
1783
+ * arr.set( [ 2.0, 2.0 ], 1 );
1784
+ * arr.set( [ 3.0, 3.0 ], 2 );
1785
+ *
1786
+ * var out = arr.reverse();
1787
+ * // returns <Complex64Array>
1788
+ *
1789
+ * var z = out.get( 0 );
1790
+ * // returns <Complex64>
1791
+ *
1792
+ * var re = realf( z );
1793
+ * // returns 3.0
1794
+ *
1795
+ * var im = imagf( z );
1796
+ * // returns 3.0
1797
+ *
1798
+ * z = out.get( 1 );
1799
+ * // returns <Complex64>
1800
+ *
1801
+ * re = realf( z );
1802
+ * // returns 2.0
1803
+ *
1804
+ * im = imagf( z );
1805
+ * // returns 2.0
1806
+ *
1807
+ * z = out.get( 2 );
1808
+ * // returns <Complex64>
1809
+ *
1810
+ * re = realf( z );
1811
+ * // returns 1.0
1812
+ *
1813
+ * im = imagf( z );
1814
+ * // returns 1.0
1815
+ */
1816
+ setReadOnly( Complex64Array.prototype, 'reverse', function reverse() {
1817
+ var buf;
1818
+ var tmp;
1819
+ var len;
1820
+ var N;
1821
+ var i;
1822
+ var j;
1823
+ if ( !isComplexArray( this ) ) {
1824
+ throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
1825
+ }
1826
+ len = this._length;
1827
+ buf = this._buffer;
1828
+ N = floor( len / 2 );
1829
+ for ( i = 0; i < N; i++ ) {
1830
+ j = len - i - 1;
1831
+ tmp = buf[ (2*i) ];
1832
+ buf[ (2*i) ] = buf[ (2*j) ];
1833
+ buf[ (2*j) ] = tmp;
1834
+ tmp = buf[ (2*i)+1 ];
1835
+ buf[ (2*i)+1 ] = buf[ (2*j)+1 ];
1836
+ buf[ (2*j)+1 ] = tmp;
1837
+ }
1838
+ return this;
1839
+ });
1840
+
1841
+ /**
1842
+ * Sets an array element.
1843
+ *
1844
+ * ## Notes
1845
+ *
1846
+ * - When provided a typed array, real or complex, we must check whether the source array shares the same buffer as the target array and whether the underlying memory overlaps. In particular, we are concerned with the following scenario:
1847
+ *
1848
+ * ```text
1849
+ * buf: ---------------------
1850
+ * src: ---------------------
1851
+ * ```
1852
+ *
1853
+ * In the above, as we copy values from `src`, we will overwrite values in the `src` view, resulting in duplicated values copied into the end of `buf`, which is not intended. Hence, to avoid overwriting source values, we must **copy** source values to a temporary array.
1854
+ *
1855
+ * In the other overlapping scenario,
1856
+ *
1857
+ * ```text
1858
+ * buf: ---------------------
1859
+ * src: ---------------------
1860
+ * ```
1861
+ *
1862
+ * by the time we begin copying into the overlapping region, we are copying from the end of `src`, a non-overlapping region, which means we don't run the risk of copying copied values, rather than the original `src` values, as intended.
1863
+ *
1864
+ * @name set
1865
+ * @memberof Complex64Array.prototype
1866
+ * @type {Function}
1867
+ * @param {(Collection|Complex|ComplexArray)} value - value(s)
1868
+ * @param {NonNegativeInteger} [i=0] - element index at which to start writing values
1869
+ * @throws {TypeError} `this` must be a complex number array
1870
+ * @throws {TypeError} first argument must be either a complex number, an array-like object, or a complex number array
1871
+ * @throws {TypeError} index argument must be a nonnegative integer
1872
+ * @throws {RangeError} array-like objects must have a length which is a multiple of two
1873
+ * @throws {RangeError} index argument is out-of-bounds
1874
+ * @throws {RangeError} target array lacks sufficient storage to accommodate source values
1875
+ * @returns {void}
1876
+ *
1877
+ * @example
1878
+ * var realf = require( '@stdlib/complex-realf' );
1879
+ * var imagf = require( '@stdlib/complex-imagf' );
1880
+ *
1881
+ * var arr = new Complex64Array( 10 );
1882
+ *
1883
+ * var z = arr.get( 0 );
1884
+ * // returns <Complex64>
1885
+ *
1886
+ * var re = realf( z );
1887
+ * // returns 0.0
1888
+ *
1889
+ * var im = imagf( z );
1890
+ * // returns 0.0
1891
+ *
1892
+ * arr.set( [ 1.0, -1.0 ], 0 );
1893
+ *
1894
+ * z = arr.get( 0 );
1895
+ * // returns <Complex64>
1896
+ *
1897
+ * re = realf( z );
1898
+ * // returns 1.0
1899
+ *
1900
+ * im = imagf( z );
1901
+ * // returns -1.0
1902
+ */
1903
+ setReadOnly( Complex64Array.prototype, 'set', function set( value ) {
1904
+ /* eslint-disable no-underscore-dangle */
1905
+ var sbuf;
1906
+ var idx;
1907
+ var buf;
1908
+ var tmp;
1909
+ var flg;
1910
+ var N;
1911
+ var v;
1912
+ var i;
1913
+ var j;
1914
+ if ( !isComplexArray( this ) ) {
1915
+ throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
1916
+ }
1917
+ buf = this._buffer;
1918
+ if ( arguments.length > 1 ) {
1919
+ idx = arguments[ 1 ];
1920
+ if ( !isNonNegativeInteger( idx ) ) {
1921
+ throw new TypeError( format( 'invalid argument. Index argument must be a nonnegative integer. Value: `%s`.', idx ) );
1922
+ }
1923
+ } else {
1924
+ idx = 0;
1925
+ }
1926
+ if ( isComplexLike( value ) ) {
1927
+ if ( idx >= this._length ) {
1928
+ throw new RangeError( format( 'invalid argument. Index argument is out-of-bounds. Value: `%u`.', idx ) );
1929
+ }
1930
+ idx *= 2;
1931
+ buf[ idx ] = realf( value );
1932
+ buf[ idx+1 ] = imagf( value );
1933
+ return;
1934
+ }
1935
+ if ( isComplexArray( value ) ) {
1936
+ N = value._length;
1937
+ if ( idx+N > this._length ) {
1938
+ throw new RangeError( 'invalid arguments. Target array lacks sufficient storage to accommodate source values.' );
1939
+ }
1940
+ sbuf = value._buffer;
1941
+
1942
+ // Check for overlapping memory...
1943
+ j = buf.byteOffset + (idx*BYTES_PER_ELEMENT);
970
1944
  if (
971
1945
  sbuf.buffer === buf.buffer &&
972
1946
  (
@@ -989,70 +1963,539 @@ setReadOnly( Complex64Array.prototype, 'set', function set( value ) {
989
1963
  idx += 2; // stride
990
1964
  j += 2; // stride
991
1965
  }
992
- return;
993
- }
994
- if ( isCollection( value ) ) {
995
- // Detect whether we've been provided an array of complex numbers...
996
- N = value.length;
997
- for ( i = 0; i < N; i++ ) {
998
- if ( !isComplexLike( value[ i ] ) ) {
999
- flg = true;
1000
- break;
1966
+ return;
1967
+ }
1968
+ if ( isCollection( value ) ) {
1969
+ // Detect whether we've been provided an array of complex numbers...
1970
+ N = value.length;
1971
+ for ( i = 0; i < N; i++ ) {
1972
+ if ( !isComplexLike( value[ i ] ) ) {
1973
+ flg = true;
1974
+ break;
1975
+ }
1976
+ }
1977
+ // If an array does not contain only complex numbers, then we assume interleaved real and imaginary components...
1978
+ if ( flg ) {
1979
+ if ( !isEven( N ) ) {
1980
+ throw new RangeError( format( 'invalid argument. Array-like object arguments must have a length which is a multiple of two. Length: `%u`.', N ) );
1981
+ }
1982
+ if ( idx+(N/2) > this._length ) {
1983
+ throw new RangeError( 'invalid arguments. Target array lacks sufficient storage to accommodate source values.' );
1984
+ }
1985
+ sbuf = value;
1986
+
1987
+ // Check for overlapping memory...
1988
+ j = buf.byteOffset + (idx*BYTES_PER_ELEMENT);
1989
+ if (
1990
+ sbuf.buffer === buf.buffer &&
1991
+ (
1992
+ sbuf.byteOffset < j &&
1993
+ sbuf.byteOffset+sbuf.byteLength > j
1994
+ )
1995
+ ) {
1996
+ // We need to copy source values...
1997
+ tmp = new Float32Array( N );
1998
+ for ( i = 0; i < N; i++ ) {
1999
+ tmp[ i ] = sbuf[ i ]; // TODO: handle accessor arrays
2000
+ }
2001
+ sbuf = tmp;
2002
+ }
2003
+ idx *= 2;
2004
+ N /= 2;
2005
+ j = 0;
2006
+ for ( i = 0; i < N; i++ ) {
2007
+ buf[ idx ] = sbuf[ j ];
2008
+ buf[ idx+1 ] = sbuf[ j+1 ];
2009
+ idx += 2; // stride
2010
+ j += 2; // stride
2011
+ }
2012
+ return;
2013
+ }
2014
+ // If an array contains only complex numbers, then we need to extract real and imaginary components...
2015
+ if ( idx+N > this._length ) {
2016
+ throw new RangeError( 'invalid arguments. Target array lacks sufficient storage to accommodate source values.' );
2017
+ }
2018
+ idx *= 2;
2019
+ for ( i = 0; i < N; i++ ) {
2020
+ v = value[ i ];
2021
+ buf[ idx ] = realf( v );
2022
+ buf[ idx+1 ] = imagf( v );
2023
+ idx += 2; // stride
2024
+ }
2025
+ return;
2026
+ }
2027
+ throw new TypeError( format( 'invalid argument. First argument must be either a complex number, an array-like object, or a complex number array. Value: `%s`.', value ) );
2028
+
2029
+ /* eslint-enable no-underscore-dangle */
2030
+ });
2031
+
2032
+ /**
2033
+ * Copies a portion of a typed array to a new typed array.
2034
+ *
2035
+ * @name slice
2036
+ * @memberof Complex64Array.prototype
2037
+ * @type {Function}
2038
+ * @param {integer} [start=0] - starting index (inclusive)
2039
+ * @param {integer} [end] - ending index (exclusive)
2040
+ * @throws {TypeError} `this` must be a complex number array
2041
+ * @throws {TypeError} first argument must be an integer
2042
+ * @throws {TypeError} second argument must be an integer
2043
+ * @returns {Complex64Array} complex number array
2044
+ *
2045
+ * @example
2046
+ * var realf = require( '@stdlib/complex-realf' );
2047
+ * var imagf = require( '@stdlib/complex-imagf' );
2048
+ *
2049
+ * var arr = new Complex64Array( 5 );
2050
+ *
2051
+ * arr.set( [ 1.0, -1.0 ], 0 );
2052
+ * arr.set( [ 2.0, -2.0 ], 1 );
2053
+ * arr.set( [ 3.0, -3.0 ], 2 );
2054
+ * arr.set( [ 4.0, -4.0 ], 3 );
2055
+ * arr.set( [ 5.0, -5.0 ], 4 );
2056
+ *
2057
+ * var out = arr.slice();
2058
+ * // returns <Complex64Array>
2059
+ *
2060
+ * var len = out.length;
2061
+ * // returns 5
2062
+ *
2063
+ * var z = out.get( 0 );
2064
+ * // returns <Complex64>
2065
+ *
2066
+ * var re = realf( z );
2067
+ * // returns 1.0
2068
+ *
2069
+ * var im = imagf( z );
2070
+ * // returns -1.0
2071
+ *
2072
+ * z = out.get( len-1 );
2073
+ * // returns <Complex64>
2074
+ *
2075
+ * re = realf( z );
2076
+ * // returns 5.0
2077
+ *
2078
+ * im = imagf( z );
2079
+ * // returns -5.0
2080
+ *
2081
+ * out = arr.slice( 1, -2 );
2082
+ * // returns <Complex64Array>
2083
+ *
2084
+ * len = out.length;
2085
+ * // returns 2
2086
+ *
2087
+ * z = out.get( 0 );
2088
+ * // returns <Complex64>
2089
+ *
2090
+ * re = realf( z );
2091
+ * // returns 2.0
2092
+ *
2093
+ * im = imagf( z );
2094
+ * // returns -2.0
2095
+ *
2096
+ * z = out.get( len-1 );
2097
+ * // returns <Complex64>
2098
+ *
2099
+ * re = realf( z );
2100
+ * // returns 3.0
2101
+ *
2102
+ * im = imagf( z );
2103
+ * // returns -3.0
2104
+ */
2105
+ setReadOnly( Complex64Array.prototype, 'slice', function slice( start, end ) {
2106
+ var outlen;
2107
+ var outbuf;
2108
+ var out;
2109
+ var idx;
2110
+ var buf;
2111
+ var len;
2112
+ var i;
2113
+ if ( !isComplexArray( this ) ) {
2114
+ throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
2115
+ }
2116
+ buf = this._buffer;
2117
+ len = this._length;
2118
+ if ( arguments.length === 0 ) {
2119
+ start = 0;
2120
+ end = len;
2121
+ } else {
2122
+ if ( !isInteger( start ) ) {
2123
+ throw new TypeError( format( 'invalid argument. First argument must be an integer. Value: `%s`.', start ) );
2124
+ }
2125
+ if ( start < 0 ) {
2126
+ start += len;
2127
+ if ( start < 0 ) {
2128
+ start = 0;
1001
2129
  }
1002
2130
  }
1003
- // If an array does not contain only complex numbers, then we assume interleaved real and imaginary components...
1004
- if ( flg ) {
1005
- if ( !isEven( N ) ) {
1006
- throw new RangeError( format( 'invalid argument. Array-like object arguments must have a length which is a multiple of two. Length: `%u`.', N ) );
1007
- }
1008
- if ( idx+(N/2) > this._length ) {
1009
- throw new RangeError( 'invalid arguments. Target array lacks sufficient storage to accommodate source values.' );
2131
+ if ( arguments.length === 1 ) {
2132
+ end = len;
2133
+ } else {
2134
+ if ( !isInteger( end ) ) {
2135
+ throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', end ) );
1010
2136
  }
1011
- sbuf = value;
1012
-
1013
- // Check for overlapping memory...
1014
- j = buf.byteOffset + (idx*BYTES_PER_ELEMENT);
1015
- if (
1016
- sbuf.buffer === buf.buffer &&
1017
- (
1018
- sbuf.byteOffset < j &&
1019
- sbuf.byteOffset+sbuf.byteLength > j
1020
- )
1021
- ) {
1022
- // We need to copy source values...
1023
- tmp = new Float32Array( N );
1024
- for ( i = 0; i < N; i++ ) {
1025
- tmp[ i ] = sbuf[ i ];
2137
+ if ( end < 0 ) {
2138
+ end += len;
2139
+ if ( end < 0 ) {
2140
+ end = 0;
1026
2141
  }
1027
- sbuf = tmp;
1028
- }
1029
- idx *= 2;
1030
- N /= 2;
1031
- j = 0;
1032
- for ( i = 0; i < N; i++ ) {
1033
- buf[ idx ] = sbuf[ j ];
1034
- buf[ idx+1 ] = sbuf[ j+1 ];
1035
- idx += 2; // stride
1036
- j += 2; // stride
2142
+ } else if ( end > len ) {
2143
+ end = len;
1037
2144
  }
1038
- return;
1039
2145
  }
1040
- // If an array contains only complex numbers, then we need to extract real and imaginary components...
1041
- if ( idx+N > this._length ) {
1042
- throw new RangeError( 'invalid arguments. Target array lacks sufficient storage to accommodate source values.' );
2146
+ }
2147
+ if ( start < end ) {
2148
+ outlen = end - start;
2149
+ } else {
2150
+ outlen = 0;
2151
+ }
2152
+ out = new this.constructor( outlen );
2153
+ outbuf = out._buffer; // eslint-disable-line no-underscore-dangle
2154
+ for ( i = 0; i < outlen; i++ ) {
2155
+ idx = 2*(i+start);
2156
+ outbuf[ 2*i ] = buf[ idx ];
2157
+ outbuf[ (2*i)+1 ] = buf[ idx+1 ];
2158
+ }
2159
+ return out;
2160
+ });
2161
+
2162
+ /**
2163
+ * Tests whether at least one element in an array passes a test implemented by a predicate function.
2164
+ *
2165
+ * @name some
2166
+ * @memberof Complex64Array.prototype
2167
+ * @type {Function}
2168
+ * @param {Function} predicate - test function
2169
+ * @param {*} [thisArg] - predicate function execution context
2170
+ * @throws {TypeError} `this` must be a complex number array
2171
+ * @throws {TypeError} first argument must be a function
2172
+ * @returns {boolean} boolean indicating whether at least one element passes a test
2173
+ *
2174
+ * @example
2175
+ * var realf = require( '@stdlib/complex-realf' );
2176
+ * var imagf = require( '@stdlib/complex-imagf' );
2177
+ *
2178
+ * function predicate( v ) {
2179
+ * return ( realf( v ) === imagf( v ) );
2180
+ * }
2181
+ *
2182
+ * var arr = new Complex64Array( 3 );
2183
+ *
2184
+ * arr.set( [ 1.0, -1.0 ], 0 );
2185
+ * arr.set( [ 2.0, 2.0 ], 1 );
2186
+ * arr.set( [ 3.0, -3.0 ], 2 );
2187
+ *
2188
+ * var bool = arr.some( predicate );
2189
+ * // returns true
2190
+ */
2191
+ setReadOnly( Complex64Array.prototype, 'some', function some( predicate, thisArg ) {
2192
+ var buf;
2193
+ var i;
2194
+ if ( !isComplexArray( this ) ) {
2195
+ throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
2196
+ }
2197
+ if ( !isFunction( predicate ) ) {
2198
+ throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
2199
+ }
2200
+ buf = this._buffer;
2201
+ for ( i = 0; i < this._length; i++ ) {
2202
+ if ( predicate.call( thisArg, getComplex64( buf, i ), i, this ) ) {
2203
+ return true;
1043
2204
  }
1044
- idx *= 2;
1045
- for ( i = 0; i < N; i++ ) {
1046
- v = value[ i ];
1047
- buf[ idx ] = realf( v );
1048
- buf[ idx+1 ] = imagf( v );
1049
- idx += 2; // stride
2205
+ }
2206
+ return false;
2207
+ });
2208
+
2209
+ /**
2210
+ * Creates a new typed array view over the same underlying `ArrayBuffer` and with the same underlying data type as the host array.
2211
+ *
2212
+ * @name subarray
2213
+ * @memberof Complex64Array.prototype
2214
+ * @type {Function}
2215
+ * @param {integer} [begin=0] - starting index (inclusive)
2216
+ * @param {integer} [end] - ending index (exclusive)
2217
+ * @throws {TypeError} `this` must be a complex number array
2218
+ * @throws {TypeError} first argument must be an integer
2219
+ * @throws {TypeError} second argument must be an integer
2220
+ * @returns {Complex64Array} subarray
2221
+ *
2222
+ * @example
2223
+ * var realf = require( '@stdlib/complex-realf' );
2224
+ * var imagf = require( '@stdlib/complex-imagf' );
2225
+ *
2226
+ * var arr = new Complex64Array( 5 );
2227
+ *
2228
+ * arr.set( [ 1.0, -1.0 ], 0 );
2229
+ * arr.set( [ 2.0, -2.0 ], 1 );
2230
+ * arr.set( [ 3.0, -3.0 ], 2 );
2231
+ * arr.set( [ 4.0, -4.0 ], 3 );
2232
+ * arr.set( [ 5.0, -5.0 ], 4 );
2233
+ *
2234
+ * var subarr = arr.subarray();
2235
+ * // returns <Complex64Array>
2236
+ *
2237
+ * var len = subarr.length;
2238
+ * // returns 5
2239
+ *
2240
+ * var z = subarr.get( 0 );
2241
+ * // returns <Complex64>
2242
+ *
2243
+ * var re = realf( z );
2244
+ * // returns 1.0
2245
+ *
2246
+ * var im = imagf( z );
2247
+ * // returns -1.0
2248
+ *
2249
+ * z = subarr.get( len-1 );
2250
+ * // returns <Complex64>
2251
+ *
2252
+ * re = realf( z );
2253
+ * // returns 5.0
2254
+ *
2255
+ * im = imagf( z );
2256
+ * // returns -5.0
2257
+ *
2258
+ * subarr = arr.subarray( 1, -2 );
2259
+ * // returns <Complex64Array>
2260
+ *
2261
+ * len = subarr.length;
2262
+ * // returns 2
2263
+ *
2264
+ * z = subarr.get( 0 );
2265
+ * // returns <Complex64>
2266
+ *
2267
+ * re = realf( z );
2268
+ * // returns 2.0
2269
+ *
2270
+ * im = imagf( z );
2271
+ * // returns -2.0
2272
+ *
2273
+ * z = subarr.get( len-1 );
2274
+ * // returns <Complex64>
2275
+ *
2276
+ * re = realf( z );
2277
+ * // returns 3.0
2278
+ *
2279
+ * im = imagf( z );
2280
+ * // returns -3.0
2281
+ */
2282
+ setReadOnly( Complex64Array.prototype, 'subarray', function subarray( begin, end ) {
2283
+ var offset;
2284
+ var buf;
2285
+ var len;
2286
+ if ( !isComplexArray( this ) ) {
2287
+ throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
2288
+ }
2289
+ buf = this._buffer;
2290
+ len = this._length;
2291
+ if ( arguments.length === 0 ) {
2292
+ begin = 0;
2293
+ end = len;
2294
+ } else {
2295
+ if ( !isInteger( begin ) ) {
2296
+ throw new TypeError( format( 'invalid argument. First argument must be an integer. Value: `%s`.', begin ) );
2297
+ }
2298
+ if ( begin < 0 ) {
2299
+ begin += len;
2300
+ if ( begin < 0 ) {
2301
+ begin = 0;
2302
+ }
2303
+ }
2304
+ if ( arguments.length === 1 ) {
2305
+ end = len;
2306
+ } else {
2307
+ if ( !isInteger( end ) ) {
2308
+ throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', end ) );
2309
+ }
2310
+ if ( end < 0 ) {
2311
+ end += len;
2312
+ if ( end < 0 ) {
2313
+ end = 0;
2314
+ }
2315
+ } else if ( end > len ) {
2316
+ end = len;
2317
+ }
1050
2318
  }
1051
- return;
1052
2319
  }
1053
- throw new TypeError( format( 'invalid argument. First argument must be either a complex number, an array-like object, or a complex number array. Value: `%s`.', value ) );
2320
+ if ( begin >= len ) {
2321
+ len = 0;
2322
+ offset = buf.byteLength;
2323
+ } else if ( begin >= end ) {
2324
+ len = 0;
2325
+ offset = buf.byteOffset + (begin*BYTES_PER_ELEMENT);
2326
+ } else {
2327
+ len = end - begin;
2328
+ offset = buf.byteOffset + ( begin*BYTES_PER_ELEMENT );
2329
+ }
2330
+ return new this.constructor( buf.buffer, offset, ( len < 0 ) ? 0 : len );
2331
+ });
1054
2332
 
1055
- /* eslint-enable no-underscore-dangle */
2333
+ /**
2334
+ * Returns a new typed array containing the elements in reversed order.
2335
+ *
2336
+ * @name toReversed
2337
+ * @memberof Complex64Array.prototype
2338
+ * @type {Function}
2339
+ * @throws {TypeError} `this` must be a complex number array
2340
+ * @returns {Complex64Array} reversed array
2341
+ *
2342
+ * @example
2343
+ * var realf = require( '@stdlib/complex-realf' );
2344
+ * var imagf = require( '@stdlib/complex-imagf' );
2345
+ *
2346
+ * var arr = new Complex64Array( 3 );
2347
+ *
2348
+ * arr.set( [ 1.0, 1.0 ], 0 );
2349
+ * arr.set( [ 2.0, 2.0 ], 1 );
2350
+ * arr.set( [ 3.0, 3.0 ], 2 );
2351
+ *
2352
+ * var out = arr.toReversed();
2353
+ * // returns <Complex64Array>
2354
+ *
2355
+ * var z = out.get( 0 );
2356
+ * // returns <Complex64>
2357
+ *
2358
+ * var re = realf( z );
2359
+ * // returns 3.0
2360
+ *
2361
+ * var im = imagf( z );
2362
+ * // returns 3.0
2363
+ *
2364
+ * z = out.get( 1 );
2365
+ * // returns <Complex64>
2366
+ *
2367
+ * re = realf( z );
2368
+ * // returns 2.0
2369
+ *
2370
+ * im = imagf( z );
2371
+ * // returns 2.0
2372
+ *
2373
+ * z = out.get( 2 );
2374
+ * // returns <Complex64>
2375
+ *
2376
+ * re = realf( z );
2377
+ * // returns 1.0
2378
+ *
2379
+ * im = imagf( z );
2380
+ * // returns 1.0
2381
+ */
2382
+ setReadOnly( Complex64Array.prototype, 'toReversed', function toReversed() {
2383
+ var outbuf;
2384
+ var out;
2385
+ var len;
2386
+ var buf;
2387
+ var i;
2388
+ var j;
2389
+ if ( !isComplexArray( this ) ) {
2390
+ throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
2391
+ }
2392
+ len = this._length;
2393
+ out = new this.constructor( len );
2394
+ buf = this._buffer;
2395
+ outbuf = out._buffer; // eslint-disable-line no-underscore-dangle
2396
+ for ( i = 0; i < len; i++ ) {
2397
+ j = len - i - 1;
2398
+ outbuf[ (2*i) ] = buf[ (2*j) ];
2399
+ outbuf[ (2*i)+1 ] = buf[ (2*j)+1 ];
2400
+ }
2401
+ return out;
2402
+ });
2403
+
2404
+ /**
2405
+ * Serializes an array as a string.
2406
+ *
2407
+ * @name toString
2408
+ * @memberof Complex64Array.prototype
2409
+ * @type {Function}
2410
+ * @throws {TypeError} `this` must be a complex number array
2411
+ * @returns {string} string representation
2412
+ *
2413
+ * @example
2414
+ * var arr = new Complex64Array( 2 );
2415
+ *
2416
+ * arr.set( [ 1.0, 1.0 ], 0 );
2417
+ * arr.set( [ 2.0, 2.0 ], 1 );
2418
+ *
2419
+ * var str = arr.toString();
2420
+ * // returns '1 + 1i,2 + 2i'
2421
+ */
2422
+ setReadOnly( Complex64Array.prototype, 'toString', function toString() {
2423
+ var out;
2424
+ var buf;
2425
+ var i;
2426
+ if ( !isComplexArray( this ) ) {
2427
+ throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
2428
+ }
2429
+ out = [];
2430
+ buf = this._buffer;
2431
+ for ( i = 0; i < this._length; i++ ) {
2432
+ out.push( getComplex64( buf, i ).toString() );
2433
+ }
2434
+ return out.join( ',' );
2435
+ });
2436
+
2437
+ /**
2438
+ * Returns a new typed array with the element at a provided index replaced with a provided value.
2439
+ *
2440
+ * @name with
2441
+ * @memberof Complex64Array.prototype
2442
+ * @type {Function}
2443
+ * @param {integer} index - element index
2444
+ * @param {ComplexLike} value - new value
2445
+ * @throws {TypeError} `this` must be a complex number array
2446
+ * @throws {TypeError} first argument must be an integer
2447
+ * @throws {RangeError} index argument is out-of-bounds
2448
+ * @throws {TypeError} second argument must be a complex number
2449
+ * @returns {Complex64Array} new typed array
2450
+ *
2451
+ * @example
2452
+ * var realf = require( '@stdlib/complex-realf' );
2453
+ * var imagf = require( '@stdlib/complex-imagf' );
2454
+ * var Complex64 = require( '@stdlib/complex-float32' );
2455
+ *
2456
+ * var arr = new Complex64Array( 3 );
2457
+ *
2458
+ * arr.set( [ 1.0, 1.0 ], 0 );
2459
+ * arr.set( [ 2.0, 2.0 ], 1 );
2460
+ * arr.set( [ 3.0, 3.0 ], 2 );
2461
+ *
2462
+ * var out = arr.with( 0, new Complex64( 4.0, 4.0 ) );
2463
+ * // returns <Complex64Array>
2464
+ *
2465
+ * var z = out.get( 0 );
2466
+ * // returns <Complex64>
2467
+ *
2468
+ * var re = realf( z );
2469
+ * // returns 4.0
2470
+ *
2471
+ * var im = imagf( z );
2472
+ * // returns 4.0
2473
+ */
2474
+ setReadOnly( Complex64Array.prototype, 'with', function copyWith( index, value ) {
2475
+ var buf;
2476
+ var out;
2477
+ var len;
2478
+ if ( !isComplexArray( this ) ) {
2479
+ throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
2480
+ }
2481
+ if ( !isInteger( index ) ) {
2482
+ throw new TypeError( format( 'invalid argument. First argument must be an integer. Value: `%s`.', index ) );
2483
+ }
2484
+ len = this._length;
2485
+ if ( index < 0 ) {
2486
+ index += len;
2487
+ }
2488
+ if ( index < 0 || index >= len ) {
2489
+ throw new RangeError( format( 'invalid argument. Index argument is out-of-bounds. Value: `%s`.', index ) );
2490
+ }
2491
+ if ( !isComplexLike( value ) ) {
2492
+ throw new TypeError( format( 'invalid argument. Second argument must be a complex number. Value: `%s`.', value ) );
2493
+ }
2494
+ out = new this.constructor( this._buffer );
2495
+ buf = out._buffer; // eslint-disable-line no-underscore-dangle
2496
+ buf[ 2*index ] = realf( value );
2497
+ buf[ (2*index)+1 ] = imagf( value );
2498
+ return out;
1056
2499
  });
1057
2500
 
1058
2501