@stdlib/array-complex64 0.1.0 → 0.2.0

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,1037 @@ 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
+ * Reverses an array in-place.
1704
+ *
1705
+ * @name reverse
1706
+ * @memberof Complex64Array.prototype
1707
+ * @type {Function}
1708
+ * @throws {TypeError} `this` must be a complex number array
1709
+ * @returns {Complex64Array} reversed array
1710
+ *
1711
+ * @example
1712
+ * var realf = require( '@stdlib/complex-realf' );
1713
+ * var imagf = require( '@stdlib/complex-imagf' );
1714
+ *
1715
+ * var arr = new Complex64Array( 3 );
1716
+ *
1717
+ * arr.set( [ 1.0, 1.0 ], 0 );
1718
+ * arr.set( [ 2.0, 2.0 ], 1 );
1719
+ * arr.set( [ 3.0, 3.0 ], 2 );
1720
+ *
1721
+ * var out = arr.reverse();
1722
+ * // returns <Complex64Array>
1723
+ *
1724
+ * var z = out.get( 0 );
1725
+ * // returns <Complex64>
1726
+ *
1727
+ * var re = realf( z );
1728
+ * // returns 3.0
1729
+ *
1730
+ * var im = imagf( z );
1731
+ * // returns 3.0
1732
+ *
1733
+ * z = out.get( 1 );
1734
+ * // returns <Complex64>
1735
+ *
1736
+ * re = realf( z );
1737
+ * // returns 2.0
1738
+ *
1739
+ * im = imagf( z );
1740
+ * // returns 2.0
1741
+ *
1742
+ * z = out.get( 2 );
1743
+ * // returns <Complex64>
1744
+ *
1745
+ * re = realf( z );
1746
+ * // returns 1.0
1747
+ *
1748
+ * im = imagf( z );
1749
+ * // returns 1.0
1750
+ */
1751
+ setReadOnly( Complex64Array.prototype, 'reverse', function reverse() {
1752
+ var buf;
1753
+ var tmp;
1754
+ var len;
1755
+ var N;
1756
+ var i;
1757
+ var j;
1758
+ if ( !isComplexArray( this ) ) {
1759
+ throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
1760
+ }
1761
+ len = this._length;
1762
+ buf = this._buffer;
1763
+ N = floor( len / 2 );
1764
+ for ( i = 0; i < N; i++ ) {
1765
+ j = len - i - 1;
1766
+ tmp = buf[ (2*i) ];
1767
+ buf[ (2*i) ] = buf[ (2*j) ];
1768
+ buf[ (2*j) ] = tmp;
1769
+ tmp = buf[ (2*i)+1 ];
1770
+ buf[ (2*i)+1 ] = buf[ (2*j)+1 ];
1771
+ buf[ (2*j)+1 ] = tmp;
1772
+ }
1773
+ return this;
1774
+ });
1775
+
1776
+ /**
1777
+ * Sets an array element.
1778
+ *
1779
+ * ## Notes
1780
+ *
1781
+ * - 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:
1782
+ *
1783
+ * ```text
1784
+ * buf: ---------------------
1785
+ * src: ---------------------
1786
+ * ```
1787
+ *
1788
+ * 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.
1789
+ *
1790
+ * In the other overlapping scenario,
1791
+ *
1792
+ * ```text
1793
+ * buf: ---------------------
1794
+ * src: ---------------------
1795
+ * ```
1796
+ *
1797
+ * 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.
1798
+ *
1799
+ * @name set
1800
+ * @memberof Complex64Array.prototype
1801
+ * @type {Function}
1802
+ * @param {(Collection|Complex|ComplexArray)} value - value(s)
1803
+ * @param {NonNegativeInteger} [i=0] - element index at which to start writing values
1804
+ * @throws {TypeError} `this` must be a complex number array
1805
+ * @throws {TypeError} first argument must be either a complex number, an array-like object, or a complex number array
1806
+ * @throws {TypeError} index argument must be a nonnegative integer
1807
+ * @throws {RangeError} array-like objects must have a length which is a multiple of two
1808
+ * @throws {RangeError} index argument is out-of-bounds
1809
+ * @throws {RangeError} target array lacks sufficient storage to accommodate source values
1810
+ * @returns {void}
1811
+ *
1812
+ * @example
1813
+ * var realf = require( '@stdlib/complex-realf' );
1814
+ * var imagf = require( '@stdlib/complex-imagf' );
1815
+ *
1816
+ * var arr = new Complex64Array( 10 );
1817
+ *
1818
+ * var z = arr.get( 0 );
1819
+ * // returns <Complex64>
1820
+ *
1821
+ * var re = realf( z );
1822
+ * // returns 0.0
1823
+ *
1824
+ * var im = imagf( z );
1825
+ * // returns 0.0
1826
+ *
1827
+ * arr.set( [ 1.0, -1.0 ], 0 );
1828
+ *
1829
+ * z = arr.get( 0 );
1830
+ * // returns <Complex64>
1831
+ *
1832
+ * re = realf( z );
1833
+ * // returns 1.0
1834
+ *
1835
+ * im = imagf( z );
1836
+ * // returns -1.0
1837
+ */
1838
+ setReadOnly( Complex64Array.prototype, 'set', function set( value ) {
1839
+ /* eslint-disable no-underscore-dangle */
1840
+ var sbuf;
1841
+ var idx;
1842
+ var buf;
1843
+ var tmp;
1844
+ var flg;
1845
+ var N;
1846
+ var v;
1847
+ var i;
1848
+ var j;
1849
+ if ( !isComplexArray( this ) ) {
1850
+ throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
1851
+ }
1852
+ buf = this._buffer;
1853
+ if ( arguments.length > 1 ) {
1854
+ idx = arguments[ 1 ];
1855
+ if ( !isNonNegativeInteger( idx ) ) {
1856
+ throw new TypeError( format( 'invalid argument. Index argument must be a nonnegative integer. Value: `%s`.', idx ) );
1857
+ }
1858
+ } else {
1859
+ idx = 0;
1860
+ }
1861
+ if ( isComplexLike( value ) ) {
1862
+ if ( idx >= this._length ) {
1863
+ throw new RangeError( format( 'invalid argument. Index argument is out-of-bounds. Value: `%u`.', idx ) );
1864
+ }
1865
+ idx *= 2;
1866
+ buf[ idx ] = realf( value );
1867
+ buf[ idx+1 ] = imagf( value );
1868
+ return;
1869
+ }
1870
+ if ( isComplexArray( value ) ) {
1871
+ N = value._length;
1872
+ if ( idx+N > this._length ) {
1873
+ throw new RangeError( 'invalid arguments. Target array lacks sufficient storage to accommodate source values.' );
1874
+ }
1875
+ sbuf = value._buffer;
1876
+
1877
+ // Check for overlapping memory...
1878
+ j = buf.byteOffset + (idx*BYTES_PER_ELEMENT);
970
1879
  if (
971
1880
  sbuf.buffer === buf.buffer &&
972
1881
  (
@@ -989,70 +1898,539 @@ setReadOnly( Complex64Array.prototype, 'set', function set( value ) {
989
1898
  idx += 2; // stride
990
1899
  j += 2; // stride
991
1900
  }
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;
1901
+ return;
1902
+ }
1903
+ if ( isCollection( value ) ) {
1904
+ // Detect whether we've been provided an array of complex numbers...
1905
+ N = value.length;
1906
+ for ( i = 0; i < N; i++ ) {
1907
+ if ( !isComplexLike( value[ i ] ) ) {
1908
+ flg = true;
1909
+ break;
1910
+ }
1911
+ }
1912
+ // If an array does not contain only complex numbers, then we assume interleaved real and imaginary components...
1913
+ if ( flg ) {
1914
+ if ( !isEven( N ) ) {
1915
+ throw new RangeError( format( 'invalid argument. Array-like object arguments must have a length which is a multiple of two. Length: `%u`.', N ) );
1916
+ }
1917
+ if ( idx+(N/2) > this._length ) {
1918
+ throw new RangeError( 'invalid arguments. Target array lacks sufficient storage to accommodate source values.' );
1919
+ }
1920
+ sbuf = value;
1921
+
1922
+ // Check for overlapping memory...
1923
+ j = buf.byteOffset + (idx*BYTES_PER_ELEMENT);
1924
+ if (
1925
+ sbuf.buffer === buf.buffer &&
1926
+ (
1927
+ sbuf.byteOffset < j &&
1928
+ sbuf.byteOffset+sbuf.byteLength > j
1929
+ )
1930
+ ) {
1931
+ // We need to copy source values...
1932
+ tmp = new Float32Array( N );
1933
+ for ( i = 0; i < N; i++ ) {
1934
+ tmp[ i ] = sbuf[ i ]; // TODO: handle accessor arrays
1935
+ }
1936
+ sbuf = tmp;
1937
+ }
1938
+ idx *= 2;
1939
+ N /= 2;
1940
+ j = 0;
1941
+ for ( i = 0; i < N; i++ ) {
1942
+ buf[ idx ] = sbuf[ j ];
1943
+ buf[ idx+1 ] = sbuf[ j+1 ];
1944
+ idx += 2; // stride
1945
+ j += 2; // stride
1946
+ }
1947
+ return;
1948
+ }
1949
+ // If an array contains only complex numbers, then we need to extract real and imaginary components...
1950
+ if ( idx+N > this._length ) {
1951
+ throw new RangeError( 'invalid arguments. Target array lacks sufficient storage to accommodate source values.' );
1952
+ }
1953
+ idx *= 2;
1954
+ for ( i = 0; i < N; i++ ) {
1955
+ v = value[ i ];
1956
+ buf[ idx ] = realf( v );
1957
+ buf[ idx+1 ] = imagf( v );
1958
+ idx += 2; // stride
1959
+ }
1960
+ return;
1961
+ }
1962
+ 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 ) );
1963
+
1964
+ /* eslint-enable no-underscore-dangle */
1965
+ });
1966
+
1967
+ /**
1968
+ * Copies a portion of a typed array to a new typed array.
1969
+ *
1970
+ * @name slice
1971
+ * @memberof Complex64Array.prototype
1972
+ * @type {Function}
1973
+ * @param {integer} [start=0] - starting index (inclusive)
1974
+ * @param {integer} [end] - ending index (exclusive)
1975
+ * @throws {TypeError} `this` must be a complex number array
1976
+ * @throws {TypeError} first argument must be an integer
1977
+ * @throws {TypeError} second argument must be an integer
1978
+ * @returns {Complex64Array} complex number array
1979
+ *
1980
+ * @example
1981
+ * var realf = require( '@stdlib/complex-realf' );
1982
+ * var imagf = require( '@stdlib/complex-imagf' );
1983
+ *
1984
+ * var arr = new Complex64Array( 5 );
1985
+ *
1986
+ * arr.set( [ 1.0, -1.0 ], 0 );
1987
+ * arr.set( [ 2.0, -2.0 ], 1 );
1988
+ * arr.set( [ 3.0, -3.0 ], 2 );
1989
+ * arr.set( [ 4.0, -4.0 ], 3 );
1990
+ * arr.set( [ 5.0, -5.0 ], 4 );
1991
+ *
1992
+ * var out = arr.slice();
1993
+ * // returns <Complex64Array>
1994
+ *
1995
+ * var len = out.length;
1996
+ * // returns 5
1997
+ *
1998
+ * var z = out.get( 0 );
1999
+ * // returns <Complex64>
2000
+ *
2001
+ * var re = realf( z );
2002
+ * // returns 1.0
2003
+ *
2004
+ * var im = imagf( z );
2005
+ * // returns -1.0
2006
+ *
2007
+ * z = out.get( len-1 );
2008
+ * // returns <Complex64>
2009
+ *
2010
+ * re = realf( z );
2011
+ * // returns 5.0
2012
+ *
2013
+ * im = imagf( z );
2014
+ * // returns -5.0
2015
+ *
2016
+ * out = arr.slice( 1, -2 );
2017
+ * // returns <Complex64Array>
2018
+ *
2019
+ * len = out.length;
2020
+ * // returns 2
2021
+ *
2022
+ * z = out.get( 0 );
2023
+ * // returns <Complex64>
2024
+ *
2025
+ * re = realf( z );
2026
+ * // returns 2.0
2027
+ *
2028
+ * im = imagf( z );
2029
+ * // returns -2.0
2030
+ *
2031
+ * z = out.get( len-1 );
2032
+ * // returns <Complex64>
2033
+ *
2034
+ * re = realf( z );
2035
+ * // returns 3.0
2036
+ *
2037
+ * im = imagf( z );
2038
+ * // returns -3.0
2039
+ */
2040
+ setReadOnly( Complex64Array.prototype, 'slice', function slice( start, end ) {
2041
+ var outlen;
2042
+ var outbuf;
2043
+ var out;
2044
+ var idx;
2045
+ var buf;
2046
+ var len;
2047
+ var i;
2048
+ if ( !isComplexArray( this ) ) {
2049
+ throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
2050
+ }
2051
+ buf = this._buffer;
2052
+ len = this._length;
2053
+ if ( arguments.length === 0 ) {
2054
+ start = 0;
2055
+ end = len;
2056
+ } else {
2057
+ if ( !isInteger( start ) ) {
2058
+ throw new TypeError( format( 'invalid argument. First argument must be an integer. Value: `%s`.', start ) );
2059
+ }
2060
+ if ( start < 0 ) {
2061
+ start += len;
2062
+ if ( start < 0 ) {
2063
+ start = 0;
1001
2064
  }
1002
2065
  }
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.' );
2066
+ if ( arguments.length === 1 ) {
2067
+ end = len;
2068
+ } else {
2069
+ if ( !isInteger( end ) ) {
2070
+ throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', end ) );
1010
2071
  }
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 ];
2072
+ if ( end < 0 ) {
2073
+ end += len;
2074
+ if ( end < 0 ) {
2075
+ end = 0;
1026
2076
  }
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
2077
+ } else if ( end > len ) {
2078
+ end = len;
1037
2079
  }
1038
- return;
1039
2080
  }
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.' );
2081
+ }
2082
+ if ( start < end ) {
2083
+ outlen = end - start;
2084
+ } else {
2085
+ outlen = 0;
2086
+ }
2087
+ out = new this.constructor( outlen );
2088
+ outbuf = out._buffer; // eslint-disable-line no-underscore-dangle
2089
+ for ( i = 0; i < outlen; i++ ) {
2090
+ idx = 2*(i+start);
2091
+ outbuf[ 2*i ] = buf[ idx ];
2092
+ outbuf[ (2*i)+1 ] = buf[ idx+1 ];
2093
+ }
2094
+ return out;
2095
+ });
2096
+
2097
+ /**
2098
+ * Tests whether at least one element in an array passes a test implemented by a predicate function.
2099
+ *
2100
+ * @name some
2101
+ * @memberof Complex64Array.prototype
2102
+ * @type {Function}
2103
+ * @param {Function} predicate - test function
2104
+ * @param {*} [thisArg] - predicate function execution context
2105
+ * @throws {TypeError} `this` must be a complex number array
2106
+ * @throws {TypeError} first argument must be a function
2107
+ * @returns {boolean} boolean indicating whether at least one element passes a test
2108
+ *
2109
+ * @example
2110
+ * var realf = require( '@stdlib/complex-realf' );
2111
+ * var imagf = require( '@stdlib/complex-imagf' );
2112
+ *
2113
+ * function predicate( v ) {
2114
+ * return ( realf( v ) === imagf( v ) );
2115
+ * }
2116
+ *
2117
+ * var arr = new Complex64Array( 3 );
2118
+ *
2119
+ * arr.set( [ 1.0, -1.0 ], 0 );
2120
+ * arr.set( [ 2.0, 2.0 ], 1 );
2121
+ * arr.set( [ 3.0, -3.0 ], 2 );
2122
+ *
2123
+ * var bool = arr.some( predicate );
2124
+ * // returns true
2125
+ */
2126
+ setReadOnly( Complex64Array.prototype, 'some', function some( predicate, thisArg ) {
2127
+ var buf;
2128
+ var i;
2129
+ if ( !isComplexArray( this ) ) {
2130
+ throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
2131
+ }
2132
+ if ( !isFunction( predicate ) ) {
2133
+ throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) );
2134
+ }
2135
+ buf = this._buffer;
2136
+ for ( i = 0; i < this._length; i++ ) {
2137
+ if ( predicate.call( thisArg, getComplex64( buf, i ), i, this ) ) {
2138
+ return true;
1043
2139
  }
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
2140
+ }
2141
+ return false;
2142
+ });
2143
+
2144
+ /**
2145
+ * Creates a new typed array view over the same underlying `ArrayBuffer` and with the same underlying data type as the host array.
2146
+ *
2147
+ * @name subarray
2148
+ * @memberof Complex64Array.prototype
2149
+ * @type {Function}
2150
+ * @param {integer} [begin=0] - starting index (inclusive)
2151
+ * @param {integer} [end] - ending index (exclusive)
2152
+ * @throws {TypeError} `this` must be a complex number array
2153
+ * @throws {TypeError} first argument must be an integer
2154
+ * @throws {TypeError} second argument must be an integer
2155
+ * @returns {Complex64Array} subarray
2156
+ *
2157
+ * @example
2158
+ * var realf = require( '@stdlib/complex-realf' );
2159
+ * var imagf = require( '@stdlib/complex-imagf' );
2160
+ *
2161
+ * var arr = new Complex64Array( 5 );
2162
+ *
2163
+ * arr.set( [ 1.0, -1.0 ], 0 );
2164
+ * arr.set( [ 2.0, -2.0 ], 1 );
2165
+ * arr.set( [ 3.0, -3.0 ], 2 );
2166
+ * arr.set( [ 4.0, -4.0 ], 3 );
2167
+ * arr.set( [ 5.0, -5.0 ], 4 );
2168
+ *
2169
+ * var subarr = arr.subarray();
2170
+ * // returns <Complex64Array>
2171
+ *
2172
+ * var len = subarr.length;
2173
+ * // returns 5
2174
+ *
2175
+ * var z = subarr.get( 0 );
2176
+ * // returns <Complex64>
2177
+ *
2178
+ * var re = realf( z );
2179
+ * // returns 1.0
2180
+ *
2181
+ * var im = imagf( z );
2182
+ * // returns -1.0
2183
+ *
2184
+ * z = subarr.get( len-1 );
2185
+ * // returns <Complex64>
2186
+ *
2187
+ * re = realf( z );
2188
+ * // returns 5.0
2189
+ *
2190
+ * im = imagf( z );
2191
+ * // returns -5.0
2192
+ *
2193
+ * subarr = arr.subarray( 1, -2 );
2194
+ * // returns <Complex64Array>
2195
+ *
2196
+ * len = subarr.length;
2197
+ * // returns 2
2198
+ *
2199
+ * z = subarr.get( 0 );
2200
+ * // returns <Complex64>
2201
+ *
2202
+ * re = realf( z );
2203
+ * // returns 2.0
2204
+ *
2205
+ * im = imagf( z );
2206
+ * // returns -2.0
2207
+ *
2208
+ * z = subarr.get( len-1 );
2209
+ * // returns <Complex64>
2210
+ *
2211
+ * re = realf( z );
2212
+ * // returns 3.0
2213
+ *
2214
+ * im = imagf( z );
2215
+ * // returns -3.0
2216
+ */
2217
+ setReadOnly( Complex64Array.prototype, 'subarray', function subarray( begin, end ) {
2218
+ var offset;
2219
+ var buf;
2220
+ var len;
2221
+ if ( !isComplexArray( this ) ) {
2222
+ throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
2223
+ }
2224
+ buf = this._buffer;
2225
+ len = this._length;
2226
+ if ( arguments.length === 0 ) {
2227
+ begin = 0;
2228
+ end = len;
2229
+ } else {
2230
+ if ( !isInteger( begin ) ) {
2231
+ throw new TypeError( format( 'invalid argument. First argument must be an integer. Value: `%s`.', begin ) );
2232
+ }
2233
+ if ( begin < 0 ) {
2234
+ begin += len;
2235
+ if ( begin < 0 ) {
2236
+ begin = 0;
2237
+ }
2238
+ }
2239
+ if ( arguments.length === 1 ) {
2240
+ end = len;
2241
+ } else {
2242
+ if ( !isInteger( end ) ) {
2243
+ throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', end ) );
2244
+ }
2245
+ if ( end < 0 ) {
2246
+ end += len;
2247
+ if ( end < 0 ) {
2248
+ end = 0;
2249
+ }
2250
+ } else if ( end > len ) {
2251
+ end = len;
2252
+ }
1050
2253
  }
1051
- return;
1052
2254
  }
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 ) );
2255
+ if ( begin >= len ) {
2256
+ len = 0;
2257
+ offset = buf.byteLength;
2258
+ } else if ( begin >= end ) {
2259
+ len = 0;
2260
+ offset = buf.byteOffset + (begin*BYTES_PER_ELEMENT);
2261
+ } else {
2262
+ len = end - begin;
2263
+ offset = buf.byteOffset + ( begin*BYTES_PER_ELEMENT );
2264
+ }
2265
+ return new this.constructor( buf.buffer, offset, ( len < 0 ) ? 0 : len );
2266
+ });
1054
2267
 
1055
- /* eslint-enable no-underscore-dangle */
2268
+ /**
2269
+ * Returns a new typed array containing the elements in reversed order.
2270
+ *
2271
+ * @name toReversed
2272
+ * @memberof Complex64Array.prototype
2273
+ * @type {Function}
2274
+ * @throws {TypeError} `this` must be a complex number array
2275
+ * @returns {Complex64Array} reversed array
2276
+ *
2277
+ * @example
2278
+ * var realf = require( '@stdlib/complex-realf' );
2279
+ * var imagf = require( '@stdlib/complex-imagf' );
2280
+ *
2281
+ * var arr = new Complex64Array( 3 );
2282
+ *
2283
+ * arr.set( [ 1.0, 1.0 ], 0 );
2284
+ * arr.set( [ 2.0, 2.0 ], 1 );
2285
+ * arr.set( [ 3.0, 3.0 ], 2 );
2286
+ *
2287
+ * var out = arr.toReversed();
2288
+ * // returns <Complex64Array>
2289
+ *
2290
+ * var z = out.get( 0 );
2291
+ * // returns <Complex64>
2292
+ *
2293
+ * var re = realf( z );
2294
+ * // returns 3.0
2295
+ *
2296
+ * var im = imagf( z );
2297
+ * // returns 3.0
2298
+ *
2299
+ * z = out.get( 1 );
2300
+ * // returns <Complex64>
2301
+ *
2302
+ * re = realf( z );
2303
+ * // returns 2.0
2304
+ *
2305
+ * im = imagf( z );
2306
+ * // returns 2.0
2307
+ *
2308
+ * z = out.get( 2 );
2309
+ * // returns <Complex64>
2310
+ *
2311
+ * re = realf( z );
2312
+ * // returns 1.0
2313
+ *
2314
+ * im = imagf( z );
2315
+ * // returns 1.0
2316
+ */
2317
+ setReadOnly( Complex64Array.prototype, 'toReversed', function toReversed() {
2318
+ var outbuf;
2319
+ var out;
2320
+ var len;
2321
+ var buf;
2322
+ var i;
2323
+ var j;
2324
+ if ( !isComplexArray( this ) ) {
2325
+ throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
2326
+ }
2327
+ len = this._length;
2328
+ out = new this.constructor( len );
2329
+ buf = this._buffer;
2330
+ outbuf = out._buffer; // eslint-disable-line no-underscore-dangle
2331
+ for ( i = 0; i < len; i++ ) {
2332
+ j = len - i - 1;
2333
+ outbuf[ (2*i) ] = buf[ (2*j) ];
2334
+ outbuf[ (2*i)+1 ] = buf[ (2*j)+1 ];
2335
+ }
2336
+ return out;
2337
+ });
2338
+
2339
+ /**
2340
+ * Serializes an array as a string.
2341
+ *
2342
+ * @name toString
2343
+ * @memberof Complex64Array.prototype
2344
+ * @type {Function}
2345
+ * @throws {TypeError} `this` must be a complex number array
2346
+ * @returns {string} string representation
2347
+ *
2348
+ * @example
2349
+ * var arr = new Complex64Array( 2 );
2350
+ *
2351
+ * arr.set( [ 1.0, 1.0 ], 0 );
2352
+ * arr.set( [ 2.0, 2.0 ], 1 );
2353
+ *
2354
+ * var str = arr.toString();
2355
+ * // returns '1 + 1i,2 + 2i'
2356
+ */
2357
+ setReadOnly( Complex64Array.prototype, 'toString', function toString() {
2358
+ var out;
2359
+ var buf;
2360
+ var i;
2361
+ if ( !isComplexArray( this ) ) {
2362
+ throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
2363
+ }
2364
+ out = [];
2365
+ buf = this._buffer;
2366
+ for ( i = 0; i < this._length; i++ ) {
2367
+ out.push( getComplex64( buf, i ).toString() );
2368
+ }
2369
+ return out.join( ',' );
2370
+ });
2371
+
2372
+ /**
2373
+ * Returns a new typed array with the element at a provided index replaced with a provided value.
2374
+ *
2375
+ * @name with
2376
+ * @memberof Complex64Array.prototype
2377
+ * @type {Function}
2378
+ * @param {integer} index - element index
2379
+ * @param {ComplexLike} value - new value
2380
+ * @throws {TypeError} `this` must be a complex number array
2381
+ * @throws {TypeError} first argument must be an integer
2382
+ * @throws {RangeError} index argument is out-of-bounds
2383
+ * @throws {TypeError} second argument must be a complex number
2384
+ * @returns {Complex64Array} new typed array
2385
+ *
2386
+ * @example
2387
+ * var realf = require( '@stdlib/complex-realf' );
2388
+ * var imagf = require( '@stdlib/complex-imagf' );
2389
+ * var Complex64 = require( '@stdlib/complex-float32' );
2390
+ *
2391
+ * var arr = new Complex64Array( 3 );
2392
+ *
2393
+ * arr.set( [ 1.0, 1.0 ], 0 );
2394
+ * arr.set( [ 2.0, 2.0 ], 1 );
2395
+ * arr.set( [ 3.0, 3.0 ], 2 );
2396
+ *
2397
+ * var out = arr.with( 0, new Complex64( 4.0, 4.0 ) );
2398
+ * // returns <Complex64Array>
2399
+ *
2400
+ * var z = out.get( 0 );
2401
+ * // returns <Complex64>
2402
+ *
2403
+ * var re = realf( z );
2404
+ * // returns 4.0
2405
+ *
2406
+ * var im = imagf( z );
2407
+ * // returns 4.0
2408
+ */
2409
+ setReadOnly( Complex64Array.prototype, 'with', function copyWith( index, value ) {
2410
+ var buf;
2411
+ var out;
2412
+ var len;
2413
+ if ( !isComplexArray( this ) ) {
2414
+ throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
2415
+ }
2416
+ if ( !isInteger( index ) ) {
2417
+ throw new TypeError( format( 'invalid argument. First argument must be an integer. Value: `%s`.', index ) );
2418
+ }
2419
+ len = this._length;
2420
+ if ( index < 0 ) {
2421
+ index += len;
2422
+ }
2423
+ if ( index < 0 || index >= len ) {
2424
+ throw new RangeError( format( 'invalid argument. Index argument is out-of-bounds. Value: `%s`.', index ) );
2425
+ }
2426
+ if ( !isComplexLike( value ) ) {
2427
+ throw new TypeError( format( 'invalid argument. Second argument must be a complex number. Value: `%s`.', value ) );
2428
+ }
2429
+ out = new this.constructor( this._buffer );
2430
+ buf = out._buffer; // eslint-disable-line no-underscore-dangle
2431
+ buf[ 2*index ] = realf( value );
2432
+ buf[ (2*index)+1 ] = imagf( value );
2433
+ return out;
1056
2434
  });
1057
2435
 
1058
2436