@stdlib/array-complex64 0.0.5 → 0.0.6

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
@@ -1,4 +1,4 @@
1
- /* eslint-disable no-restricted-syntax, max-lines */
1
+ /* eslint-disable no-restricted-syntax, max-lines, no-invalid-this */
2
2
 
3
3
  /**
4
4
  * @license Apache-2.0
@@ -34,11 +34,15 @@ var isEven = require( '@stdlib/math-base-assert-is-even' );
34
34
  var isInteger = require( '@stdlib/math-base-assert-is-integer' );
35
35
  var hasIteratorSymbolSupport = require( '@stdlib/assert-has-iterator-symbol-support' );
36
36
  var ITERATOR_SYMBOL = require( '@stdlib/symbol-iterator' );
37
- var defineProperty = require( '@stdlib/utils-define-property' );
37
+ var setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );
38
+ var setReadOnlyAccessor = require( '@stdlib/utils-define-nonenumerable-read-only-accessor' );
38
39
  var Float32Array = require( '@stdlib/array-float32' );
39
40
  var Complex64 = require( '@stdlib/complex-float32' );
40
- var real = require( '@stdlib/complex-real' );
41
- var imag = require( '@stdlib/complex-imag' );
41
+ var realf = require( '@stdlib/complex-realf' );
42
+ var imagf = require( '@stdlib/complex-imagf' );
43
+ var reinterpret64 = require( '@stdlib/strided-base-reinterpret-complex64' );
44
+ var reinterpret128 = require( '@stdlib/strided-base-reinterpret-complex128' );
45
+ var arraylike2object = require( '@stdlib/array-base-arraylike2object' );
42
46
  var fromIterator = require( './from_iterator.js' );
43
47
  var fromIteratorMap = require( './from_iterator_map.js' );
44
48
  var fromArray = require( './from_array.js' );
@@ -93,6 +97,38 @@ function isComplexArrayConstructor( value ) {
93
97
  );
94
98
  }
95
99
 
100
+ /**
101
+ * Returns a boolean indicating if a value is a `Complex64Array`.
102
+ *
103
+ * @private
104
+ * @param {*} value - value to test
105
+ * @returns {boolean} boolean indicating if a value is a `Complex64Array`
106
+ */
107
+ function isComplex64Array( value ) {
108
+ return (
109
+ typeof value === 'object' &&
110
+ value !== null &&
111
+ value.constructor.name === 'Complex64Array' &&
112
+ value.BYTES_PER_ELEMENT === BYTES_PER_ELEMENT
113
+ );
114
+ }
115
+
116
+ /**
117
+ * Returns a boolean indicating if a value is a `Complex128Array`.
118
+ *
119
+ * @private
120
+ * @param {*} value - value to test
121
+ * @returns {boolean} boolean indicating if a value is a `Complex128Array`
122
+ */
123
+ function isComplex128Array( value ) {
124
+ return (
125
+ typeof value === 'object' &&
126
+ value !== null &&
127
+ value.constructor.name === 'Complex128Array' &&
128
+ value.BYTES_PER_ELEMENT === BYTES_PER_ELEMENT*2
129
+ );
130
+ }
131
+
96
132
 
97
133
  // MAIN //
98
134
 
@@ -100,7 +136,7 @@ function isComplexArrayConstructor( value ) {
100
136
  * 64-bit complex number array constructor.
101
137
  *
102
138
  * @constructor
103
- * @param {(NonNegativeInteger|TypedArray|ArrayLikeObject|ArrayBuffer|Iterable)} [arg] - length, typed array, array-like object, or buffer
139
+ * @param {(NonNegativeInteger|Collection|ArrayBuffer|Iterable)} [arg] - length, typed array, array-like object, buffer, or an iterable
104
140
  * @param {NonNegativeInteger} [byteOffset=0] - byte offset
105
141
  * @param {NonNegativeInteger} [length] - view length
106
142
  * @throws {RangeError} ArrayBuffer byte length must be a multiple of `8`
@@ -205,7 +241,11 @@ function Complex64Array() {
205
241
  buf = new Float32Array( arguments[0] );
206
242
  }
207
243
  } else {
208
- if ( !isEven( len ) ) {
244
+ if ( isComplex64Array( buf ) ) {
245
+ buf = reinterpret64( buf, 0 );
246
+ } else if ( isComplex128Array( buf ) ) {
247
+ buf = reinterpret128( buf, 0 );
248
+ } else if ( !isEven( len ) ) {
209
249
  throw new RangeError( 'invalid argument. Array-like object and typed array input arguments must have a length which is a multiple of two. Length: `'+len+'`.' );
210
250
  }
211
251
  buf = new Float32Array( buf );
@@ -265,18 +305,8 @@ function Complex64Array() {
265
305
  buf = new Float32Array( buf, byteOffset, len*2 );
266
306
  }
267
307
  }
268
- defineProperty( this, '_buffer', {
269
- 'configurable': false,
270
- 'enumerable': false,
271
- 'writable': false,
272
- 'value': buf
273
- });
274
- defineProperty( this, '_length', {
275
- 'configurable': false,
276
- 'enumerable': false,
277
- 'writable': false,
278
- 'value': buf.length / 2
279
- });
308
+ setReadOnly( this, '_buffer', buf );
309
+ setReadOnly( this, '_length', buf.length/2 );
280
310
 
281
311
  return this;
282
312
  }
@@ -286,6 +316,7 @@ function Complex64Array() {
286
316
  *
287
317
  * @name BYTES_PER_ELEMENT
288
318
  * @memberof Complex64Array
319
+ * @readonly
289
320
  * @type {PositiveInteger}
290
321
  * @default 8
291
322
  *
@@ -293,18 +324,14 @@ function Complex64Array() {
293
324
  * var nbytes = Complex64Array.BYTES_PER_ELEMENT;
294
325
  * // returns 8
295
326
  */
296
- defineProperty( Complex64Array, 'BYTES_PER_ELEMENT', {
297
- 'configurable': false,
298
- 'enumerable': false,
299
- 'writable': false,
300
- 'value': BYTES_PER_ELEMENT
301
- });
327
+ setReadOnly( Complex64Array, 'BYTES_PER_ELEMENT', BYTES_PER_ELEMENT );
302
328
 
303
329
  /**
304
330
  * Constructor name.
305
331
  *
306
332
  * @name name
307
333
  * @memberof Complex64Array
334
+ * @readonly
308
335
  * @type {string}
309
336
  * @default 'Complex64Array'
310
337
  *
@@ -312,12 +339,7 @@ defineProperty( Complex64Array, 'BYTES_PER_ELEMENT', {
312
339
  * var str = Complex64Array.name;
313
340
  * // returns 'Complex64Array'
314
341
  */
315
- defineProperty( Complex64Array, 'name', {
316
- 'configurable': false,
317
- 'enumerable': false,
318
- 'writable': false,
319
- 'value': 'Complex64Array'
320
- });
342
+ setReadOnly( Complex64Array, 'name', 'Complex64Array' );
321
343
 
322
344
  /**
323
345
  * Creates a new 64-bit complex number array from an array-like object or an iterable.
@@ -325,7 +347,7 @@ defineProperty( Complex64Array, 'name', {
325
347
  * @name from
326
348
  * @memberof Complex64Array
327
349
  * @type {Function}
328
- * @param {(ArrayLikeObject|Iterable)} src - array-like object or iterable
350
+ * @param {(Collection|Iterable)} src - array-like object or iterable
329
351
  * @param {Function} [clbk] - callback to invoke for each source element
330
352
  * @param {*} [thisArg] - context
331
353
  * @throws {TypeError} `this` context must be a constructor
@@ -355,11 +377,11 @@ defineProperty( Complex64Array, 'name', {
355
377
  *
356
378
  * @example
357
379
  * var Complex64 = require( '@stdlib/complex-float32' );
358
- * var real = require( '@stdlib/complex-real' );
359
- * var imag = require( '@stdlib/complex-imag' );
380
+ * var realf = require( '@stdlib/complex-realf' );
381
+ * var imagf = require( '@stdlib/complex-imagf' );
360
382
  *
361
383
  * function clbk( v ) {
362
- * return new Complex64( real(v)*2.0, imag(v)*2.0 );
384
+ * return new Complex64( realf(v)*2.0, imagf(v)*2.0 );
363
385
  * }
364
386
  *
365
387
  * var arr = Complex64Array.from( [ new Complex64( 1.0, 1.0 ) ], clbk );
@@ -368,107 +390,126 @@ defineProperty( Complex64Array, 'name', {
368
390
  * var len = arr.length;
369
391
  * // returns 1
370
392
  */
371
- defineProperty( Complex64Array, 'from', {
372
- 'configurable': false,
373
- 'enumerable': false,
374
- 'writable': false,
375
- 'value': function from( src ) {
376
- var thisArg;
377
- var nargs;
378
- var clbk;
379
- var out;
380
- var buf;
381
- var tmp;
382
- var len;
383
- var flg;
384
- var v;
385
- var i;
386
- var j;
387
- if ( !isFunction( this ) ) {
388
- throw new TypeError( 'invalid invocation. `this` context must be a constructor.' );
393
+ setReadOnly( Complex64Array, 'from', function from( src ) {
394
+ var thisArg;
395
+ var nargs;
396
+ var clbk;
397
+ var out;
398
+ var buf;
399
+ var tmp;
400
+ var len;
401
+ var flg;
402
+ var v;
403
+ var i;
404
+ var j;
405
+ if ( !isFunction( this ) ) {
406
+ throw new TypeError( 'invalid invocation. `this` context must be a constructor.' );
407
+ }
408
+ if ( !isComplexArrayConstructor( this ) ) {
409
+ throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
410
+ }
411
+ nargs = arguments.length;
412
+ if ( nargs > 1 ) {
413
+ clbk = arguments[ 1 ];
414
+ if ( !isFunction( clbk ) ) {
415
+ throw new TypeError( 'invalid argument. Second argument must be a function. Value: `'+clbk+'`.' );
389
416
  }
390
- if ( !isComplexArrayConstructor( this ) ) {
391
- throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
417
+ if ( nargs > 2 ) {
418
+ thisArg = arguments[ 2 ];
392
419
  }
393
- nargs = arguments.length;
394
- if ( nargs > 1 ) {
395
- clbk = arguments[ 1 ];
396
- if ( !isFunction( clbk ) ) {
397
- throw new TypeError( 'invalid argument. Second argument must be a function. Value: `'+clbk+'`.' );
398
- }
399
- if ( nargs > 2 ) {
400
- thisArg = arguments[ 2 ];
420
+ }
421
+ if ( isComplexArray( src ) ) {
422
+ len = src.length;
423
+ if ( clbk ) {
424
+ out = new this( len );
425
+ buf = out._buffer; // eslint-disable-line no-underscore-dangle
426
+ j = 0;
427
+ for ( i = 0; i < len; i++ ) {
428
+ v = clbk.call( thisArg, src.get( i ), i );
429
+ if ( isComplexLike( v ) ) {
430
+ buf[ j ] = realf( v );
431
+ buf[ j+1 ] = imagf( v );
432
+ } else if ( isArrayLikeObject( v ) && v.length >= 2 ) {
433
+ buf[ j ] = v[ 0 ];
434
+ buf[ j+1 ] = v[ 1 ];
435
+ } else {
436
+ throw new TypeError( 'invalid argument. Callback must return either a two-element array containing real and imaginary components or a complex number. Value: `'+v+'`.' );
437
+ }
438
+ j += 2; // stride
401
439
  }
440
+ return out;
402
441
  }
403
- if ( isCollection( src ) ) {
404
- if ( clbk ) {
405
- // Note: array contents affect how we iterate over a provided data source. If only complex numbers, we can extract real and imaginary components. Otherwise, we assume a strided array where real and imaginary components are interleaved. In the former case, we expect a callback to return real and imaginary components (possibly as a complex number). In the latter case, we expect a callback to return *either* a real or imaginary component.
442
+ return new this( src );
443
+ }
444
+ if ( isCollection( src ) ) {
445
+ if ( clbk ) {
446
+ // Note: array contents affect how we iterate over a provided data source. If only complex number objects, we can extract real and imaginary components. Otherwise, for non-complex number arrays (e.g., `Float64Array`, etc), we assume a strided array where real and imaginary components are interleaved. In the former case, we expect a callback to return real and imaginary components (possibly as a complex number). In the latter case, we expect a callback to return *either* a real or imaginary component.
406
447
 
407
- // Detect whether we've been provided an array of complex numbers...
408
- len = src.length;
409
- for ( i = 0; i < len; i++ ) {
410
- if ( !isComplexLike( src[ i ] ) ) {
411
- flg = true;
412
- break;
413
- }
448
+ len = src.length;
449
+ tmp = arraylike2object( src );
450
+
451
+ // Detect whether we've been provided an array which returns complex number objects...
452
+ for ( i = 0; i < len; i++ ) {
453
+ if ( !isComplexLike( tmp.getter( src, i ) ) ) {
454
+ flg = true;
455
+ break;
414
456
  }
415
- // If an array does not contain only complex numbers, then we assume interleaved real and imaginary components...
416
- if ( flg ) {
417
- if ( !isEven( len ) ) {
418
- throw new RangeError( 'invalid argument. First argument must have a length which is a multiple of two. Length: `'+len+'`.' );
419
- }
420
- out = new this( len/2 );
421
- buf = out._buffer; // eslint-disable-line no-underscore-dangle
422
- for ( i = 0; i < len; i++ ) {
423
- buf[ i ] = clbk.call( thisArg, src[ i ], i );
424
- }
457
+ }
458
+ // If an array does not contain only complex number objects, then we assume interleaved real and imaginary components...
459
+ if ( flg ) {
460
+ if ( !isEven( len ) ) {
461
+ throw new RangeError( 'invalid argument. First argument must have a length which is a multiple of two. Length: `'+len+'`.' );
425
462
  }
426
- // If an array contains only complex numbers, then we need to extract real and imaginary components...
427
- else {
428
- out = new this( len );
429
- buf = out._buffer; // eslint-disable-line no-underscore-dangle
430
- j = 0;
431
- for ( i = 0; i < len; i++ ) {
432
- v = clbk.call( thisArg, src[ i ], i );
433
- if ( isComplexLike( v ) ) {
434
- buf[ j ] = real( v );
435
- buf[ j+1 ] = imag( v );
436
- } else if ( isArrayLikeObject( v ) && v.length >= 2 ) {
437
- buf[ j ] = v[ 0 ];
438
- buf[ j+1 ] = v[ 1 ];
439
- } else {
440
- throw new TypeError( 'invalid argument. Callback must return either a two-element array containing real and imaginary components or a complex number. Value: `'+v+'`.' );
441
- }
442
- j += 2; // stride
443
- }
463
+ out = new this( len/2 );
464
+ buf = out._buffer; // eslint-disable-line no-underscore-dangle
465
+ for ( i = 0; i < len; i++ ) {
466
+ buf[ i ] = clbk.call( thisArg, tmp.getter( src, i ), i );
444
467
  }
445
- } else {
446
- out = new this( src );
468
+ return out;
447
469
  }
448
- } else if ( isObject( src ) && HAS_ITERATOR_SYMBOL && isFunction( src[ ITERATOR_SYMBOL ] ) ) { // eslint-disable-line max-len
449
- buf = src[ ITERATOR_SYMBOL ]();
450
- if ( !isFunction( buf.next ) ) {
451
- throw new TypeError( 'invalid argument. First argument must be an array-like object or an iterable.' );
452
- }
453
- if ( clbk ) {
454
- tmp = fromIteratorMap( buf, clbk, thisArg );
455
- } else {
456
- tmp = fromIterator( buf );
457
- }
458
- if ( tmp instanceof Error ) {
459
- throw tmp;
460
- }
461
- len = tmp.length / 2;
470
+ // If an array contains only complex number objects, then we need to extract real and imaginary components...
462
471
  out = new this( len );
463
472
  buf = out._buffer; // eslint-disable-line no-underscore-dangle
473
+ j = 0;
464
474
  for ( i = 0; i < len; i++ ) {
465
- buf[ i ] = tmp[ i ];
475
+ v = clbk.call( thisArg, tmp.getter( src, i ), i );
476
+ if ( isComplexLike( v ) ) {
477
+ buf[ j ] = realf( v );
478
+ buf[ j+1 ] = imagf( v );
479
+ } else if ( isArrayLikeObject( v ) && v.length >= 2 ) {
480
+ buf[ j ] = v[ 0 ];
481
+ buf[ j+1 ] = v[ 1 ];
482
+ } else {
483
+ throw new TypeError( 'invalid argument. Callback must return either a two-element array containing real and imaginary components or a complex number. Value: `'+v+'`.' );
484
+ }
485
+ j += 2; // stride
466
486
  }
487
+ return out;
488
+ }
489
+ return new this( src );
490
+ }
491
+ if ( isObject( src ) && HAS_ITERATOR_SYMBOL && isFunction( src[ ITERATOR_SYMBOL ] ) ) { // eslint-disable-line max-len
492
+ buf = src[ ITERATOR_SYMBOL ]();
493
+ if ( !isFunction( buf.next ) ) {
494
+ throw new TypeError( 'invalid argument. First argument must be an array-like object or an iterable.' );
495
+ }
496
+ if ( clbk ) {
497
+ tmp = fromIteratorMap( buf, clbk, thisArg );
467
498
  } else {
468
- throw new TypeError( 'invalid argument. First argument must be an array-like object or an iterable. Value: `'+src+'`.' );
499
+ tmp = fromIterator( buf );
500
+ }
501
+ if ( tmp instanceof Error ) {
502
+ throw tmp;
503
+ }
504
+ len = tmp.length / 2;
505
+ out = new this( len );
506
+ buf = out._buffer; // eslint-disable-line no-underscore-dangle
507
+ for ( i = 0; i < len; i++ ) {
508
+ buf[ i ] = tmp[ i ];
469
509
  }
470
510
  return out;
471
511
  }
512
+ throw new TypeError( 'invalid argument. First argument must be an array-like object or an iterable. Value: `'+src+'`.' );
472
513
  });
473
514
 
474
515
  /**
@@ -489,25 +530,20 @@ defineProperty( Complex64Array, 'from', {
489
530
  * var len = arr.length;
490
531
  * // returns 2
491
532
  */
492
- defineProperty( Complex64Array, 'of', {
493
- 'configurable': false,
494
- 'enumerable': false,
495
- 'writable': false,
496
- 'value': function of() {
497
- var args;
498
- var i;
499
- if ( !isFunction( this ) ) {
500
- throw new TypeError( 'invalid invocation. `this` context must be a constructor.' );
501
- }
502
- if ( !isComplexArrayConstructor( this ) ) {
503
- throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
504
- }
505
- args = [];
506
- for ( i = 0; i < arguments.length; i++ ) {
507
- args.push( arguments[ i ] );
508
- }
509
- return new this( args );
533
+ setReadOnly( Complex64Array, 'of', function of() {
534
+ var args;
535
+ var i;
536
+ if ( !isFunction( this ) ) {
537
+ throw new TypeError( 'invalid invocation. `this` context must be a constructor.' );
510
538
  }
539
+ if ( !isComplexArrayConstructor( this ) ) {
540
+ throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
541
+ }
542
+ args = [];
543
+ for ( i = 0; i < arguments.length; i++ ) {
544
+ args.push( arguments[ i ] );
545
+ }
546
+ return new this( args );
511
547
  });
512
548
 
513
549
  /**
@@ -515,6 +551,7 @@ defineProperty( Complex64Array, 'of', {
515
551
  *
516
552
  * @name buffer
517
553
  * @memberof Complex64Array.prototype
554
+ * @readonly
518
555
  * @type {ArrayBuffer}
519
556
  *
520
557
  * @example
@@ -523,12 +560,8 @@ defineProperty( Complex64Array, 'of', {
523
560
  * var buf = arr.buffer;
524
561
  * // returns <ArrayBuffer>
525
562
  */
526
- defineProperty( Complex64Array.prototype, 'buffer', {
527
- 'configurable': false,
528
- 'enumerable': false,
529
- 'get': function get() {
530
- return this._buffer.buffer;
531
- }
563
+ setReadOnlyAccessor( Complex64Array.prototype, 'buffer', function get() {
564
+ return this._buffer.buffer;
532
565
  });
533
566
 
534
567
  /**
@@ -536,6 +569,7 @@ defineProperty( Complex64Array.prototype, 'buffer', {
536
569
  *
537
570
  * @name byteLength
538
571
  * @memberof Complex64Array.prototype
572
+ * @readonly
539
573
  * @type {NonNegativeInteger}
540
574
  *
541
575
  * @example
@@ -544,12 +578,8 @@ defineProperty( Complex64Array.prototype, 'buffer', {
544
578
  * var byteLength = arr.byteLength;
545
579
  * // returns 80
546
580
  */
547
- defineProperty( Complex64Array.prototype, 'byteLength', {
548
- 'configurable': false,
549
- 'enumerable': false,
550
- 'get': function get() {
551
- return this._buffer.byteLength;
552
- }
581
+ setReadOnlyAccessor( Complex64Array.prototype, 'byteLength', function get() {
582
+ return this._buffer.byteLength;
553
583
  });
554
584
 
555
585
  /**
@@ -557,6 +587,7 @@ defineProperty( Complex64Array.prototype, 'byteLength', {
557
587
  *
558
588
  * @name byteOffset
559
589
  * @memberof Complex64Array.prototype
590
+ * @readonly
560
591
  * @type {NonNegativeInteger}
561
592
  *
562
593
  * @example
@@ -565,12 +596,8 @@ defineProperty( Complex64Array.prototype, 'byteLength', {
565
596
  * var byteOffset = arr.byteOffset;
566
597
  * // returns 0
567
598
  */
568
- defineProperty( Complex64Array.prototype, 'byteOffset', {
569
- 'configurable': false,
570
- 'enumerable': false,
571
- 'get': function get() {
572
- return this._buffer.byteOffset;
573
- }
599
+ setReadOnlyAccessor( Complex64Array.prototype, 'byteOffset', function get() {
600
+ return this._buffer.byteOffset;
574
601
  });
575
602
 
576
603
  /**
@@ -578,6 +605,7 @@ defineProperty( Complex64Array.prototype, 'byteOffset', {
578
605
  *
579
606
  * @name BYTES_PER_ELEMENT
580
607
  * @memberof Complex64Array.prototype
608
+ * @readonly
581
609
  * @type {PositiveInteger}
582
610
  * @default 8
583
611
  *
@@ -587,12 +615,7 @@ defineProperty( Complex64Array.prototype, 'byteOffset', {
587
615
  * var nbytes = arr.BYTES_PER_ELEMENT;
588
616
  * // returns 8
589
617
  */
590
- defineProperty( Complex64Array.prototype, 'BYTES_PER_ELEMENT', {
591
- 'configurable': false,
592
- 'enumerable': false,
593
- 'writable': false,
594
- 'value': Complex64Array.BYTES_PER_ELEMENT
595
- });
618
+ setReadOnly( Complex64Array.prototype, 'BYTES_PER_ELEMENT', Complex64Array.BYTES_PER_ELEMENT );
596
619
 
597
620
  /**
598
621
  * Copies a sequence of elements within the array to the position starting at `target`.
@@ -608,8 +631,8 @@ defineProperty( Complex64Array.prototype, 'BYTES_PER_ELEMENT', {
608
631
  *
609
632
  * @example
610
633
  * var Complex64 = require( '@stdlib/complex-float32' );
611
- * var real = require( '@stdlib/complex-real' );
612
- * var imag = require( '@stdlib/complex-imag' );
634
+ * var realf = require( '@stdlib/complex-realf' );
635
+ * var imagf = require( '@stdlib/complex-imagf' );
613
636
  *
614
637
  * var arr = new Complex64Array( 4 );
615
638
  *
@@ -625,28 +648,23 @@ defineProperty( Complex64Array.prototype, 'BYTES_PER_ELEMENT', {
625
648
  * // Get the last array element:
626
649
  * var z = arr.get( 3 );
627
650
  *
628
- * var re = real( z );
651
+ * var re = realf( z );
629
652
  * // returns 2.0
630
653
  *
631
- * var im = imag( z );
654
+ * var im = imagf( z );
632
655
  * // returns 2.0
633
656
  */
634
- defineProperty( Complex64Array.prototype, 'copyWithin', {
635
- 'configurable': false,
636
- 'enumerable': false,
637
- 'writable': false,
638
- 'value': function copyWithin( target, start ) {
639
- if ( !isComplexArray( this ) ) {
640
- throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
641
- }
642
- // FIXME: prefer a functional `copyWithin` implementation which addresses lack of universal browser support (e.g., IE11 and Safari) or ensure that typed arrays are polyfilled
643
- if ( arguments.length === 2 ) {
644
- this._buffer.copyWithin( target*2, start*2 );
645
- } else {
646
- this._buffer.copyWithin( target*2, start*2, arguments[2]*2 );
647
- }
648
- return this;
657
+ setReadOnly( Complex64Array.prototype, 'copyWithin', function copyWithin( target, start ) {
658
+ if ( !isComplexArray( this ) ) {
659
+ throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
660
+ }
661
+ // FIXME: prefer a functional `copyWithin` implementation which addresses lack of universal browser support (e.g., IE11 and Safari) or ensure that typed arrays are polyfilled
662
+ if ( arguments.length === 2 ) {
663
+ this._buffer.copyWithin( target*2, start*2 );
664
+ } else {
665
+ this._buffer.copyWithin( target*2, start*2, arguments[2]*2 );
649
666
  }
667
+ return this;
650
668
  });
651
669
 
652
670
  /**
@@ -684,104 +702,85 @@ defineProperty( Complex64Array.prototype, 'copyWithin', {
684
702
  * var bool = it.next().done;
685
703
  * // returns true
686
704
  */
687
- defineProperty( Complex64Array.prototype, 'entries', {
688
- 'configurable': false,
689
- 'enumerable': false,
690
- 'writable': false,
691
- 'value': function entries() {
692
- var buffer;
693
- var self;
694
- var iter;
695
- var len;
696
- var FLG;
697
- var i;
698
- var j;
699
- if ( !isComplexArray( this ) ) {
700
- throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
701
- }
702
- self = this;
703
- buffer = this._buffer;
704
- len = this._length;
705
+ setReadOnly( Complex64Array.prototype, 'entries', function entries() {
706
+ var buffer;
707
+ var self;
708
+ var iter;
709
+ var len;
710
+ var FLG;
711
+ var i;
712
+ var j;
713
+ if ( !isComplexArray( this ) ) {
714
+ throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
715
+ }
716
+ self = this;
717
+ buffer = this._buffer;
718
+ len = this._length;
705
719
 
706
- // Initialize the iteration indices:
707
- i = -1;
708
- j = -2;
720
+ // Initialize the iteration indices:
721
+ i = -1;
722
+ j = -2;
709
723
 
710
- // Create an iterator protocol-compliant object:
711
- iter = {};
712
- defineProperty( iter, 'next', {
713
- 'configurable': false,
714
- 'enumerable': false,
715
- 'writable': false,
716
- 'value': next
717
- });
718
- defineProperty( iter, 'return', {
719
- 'configurable': false,
720
- 'enumerable': false,
721
- 'writable': false,
722
- 'value': end
723
- });
724
- if ( ITERATOR_SYMBOL ) {
725
- defineProperty( iter, ITERATOR_SYMBOL, {
726
- 'configurable': false,
727
- 'enumerable': false,
728
- 'writable': false,
729
- 'value': factory
730
- });
731
- }
732
- return iter;
724
+ // Create an iterator protocol-compliant object:
725
+ iter = {};
726
+ setReadOnly( iter, 'next', next );
727
+ setReadOnly( iter, 'return', end );
733
728
 
734
- /**
735
- * Returns an iterator protocol-compliant object containing the next iterated value.
736
- *
737
- * @private
738
- * @returns {Object} iterator protocol-compliant object
739
- */
740
- function next() {
741
- var z;
742
- i += 1;
743
- if ( FLG || i >= len ) {
744
- return {
745
- 'done': true
746
- };
747
- }
748
- j += 2;
749
- z = new Complex64( buffer[ j ], buffer[ j+1 ] );
729
+ if ( ITERATOR_SYMBOL ) {
730
+ setReadOnly( iter, ITERATOR_SYMBOL, factory );
731
+ }
732
+ return iter;
733
+
734
+ /**
735
+ * Returns an iterator protocol-compliant object containing the next iterated value.
736
+ *
737
+ * @private
738
+ * @returns {Object} iterator protocol-compliant object
739
+ */
740
+ function next() {
741
+ var z;
742
+ i += 1;
743
+ if ( FLG || i >= len ) {
750
744
  return {
751
- 'value': [ i, z ],
752
- 'done': false
745
+ 'done': true
753
746
  };
754
747
  }
748
+ j += 2;
749
+ z = new Complex64( buffer[ j ], buffer[ j+1 ] );
750
+ return {
751
+ 'value': [ i, z ],
752
+ 'done': false
753
+ };
754
+ }
755
755
 
756
- /**
757
- * Finishes an iterator.
758
- *
759
- * @private
760
- * @param {*} [value] - value to return
761
- * @returns {Object} iterator protocol-compliant object
762
- */
763
- function end( value ) {
764
- FLG = true;
765
- if ( arguments.length ) {
766
- return {
767
- 'value': value,
768
- 'done': true
769
- };
770
- }
756
+ /**
757
+ * Finishes an iterator.
758
+ *
759
+ * @private
760
+ * @param {*} [value] - value to return
761
+ * @returns {Object} iterator protocol-compliant object
762
+ */
763
+ function end( value ) {
764
+ FLG = true;
765
+ if ( arguments.length ) {
771
766
  return {
767
+ 'value': value,
772
768
  'done': true
773
769
  };
774
770
  }
771
+ return {
772
+ 'done': true
773
+ };
774
+ }
775
775
 
776
- /**
777
- * Returns a new iterator.
778
- *
779
- * @private
780
- * @returns {Iterator} iterator
781
- */
782
- function factory() {
783
- return self.entries();
784
- }
776
+ /**
777
+ * Returns a new iterator.
778
+ *
779
+ * @private
780
+ * @returns {Iterator} iterator
781
+ */
782
+ function factory() {
783
+ return self.entries();
785
784
  }
786
785
  });
787
786
 
@@ -791,63 +790,53 @@ defineProperty( Complex64Array.prototype, 'entries', {
791
790
  * @name get
792
791
  * @memberof Complex64Array.prototype
793
792
  * @type {Function}
794
- * @param {ArrayLikeObject} [out] - output array
795
- * @param {NonNegativeInteger} i - element index
793
+ * @param {NonNegativeInteger} idx - element index
796
794
  * @throws {TypeError} `this` must be a complex number array
797
- * @throws {TypeError} index argument must be a nonnegative integer
798
- * @throws {TypeError} output argument must be an array-like object
799
- * @returns {(Complex64|ArrayLikeObject|void)} array element
795
+ * @throws {TypeError} must provide a nonnegative integer
796
+ * @returns {(Complex64|void)} array element
800
797
  *
801
798
  * @example
802
799
  * var arr = new Complex64Array( 10 );
800
+ * var realf = require( '@stdlib/complex-realf' );
801
+ * var imagf = require( '@stdlib/complex-imagf' );
803
802
  *
804
803
  * var z = arr.get( 0 );
805
804
  * // returns <Complex64>
806
805
  *
806
+ * var re = realf( z );
807
+ * // returns 0.0
808
+ *
809
+ * var im = imagf( z );
810
+ * // returns 0.0
811
+ *
807
812
  * arr.set( [ 1.0, -1.0 ], 0 );
808
813
  *
809
- * z = arr.get( [ 0.0, 0.0 ], 0 );
810
- * // returns [ 1.0, -1.0 ]
814
+ * z = arr.get( 0 );
815
+ * // returns <Complex64>
816
+ *
817
+ * re = realf( z );
818
+ * // returns 1.0
819
+ *
820
+ * im = imagf( z );
821
+ * // returns -1.0
811
822
  *
812
823
  * z = arr.get( 100 );
813
824
  * // returns undefined
814
825
  */
815
- defineProperty( Complex64Array.prototype, 'get', {
816
- 'configurable': false,
817
- 'enumerable': false,
818
- 'writable': false,
819
- 'value': function get( i ) {
820
- var idx;
821
- var out;
822
- var buf;
823
-
824
- if ( !isComplexArray( this ) ) {
825
- throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
826
- }
827
- buf = this._buffer;
828
- if ( arguments.length > 1 ) {
829
- idx = arguments[ 1 ];
830
- out = i;
831
- if ( !isArrayLikeObject( out ) || out.length < 2 ) {
832
- throw new TypeError( 'invalid argument. Output argument must be an array-like object. Value: `'+out+'`.' );
833
- }
834
- } else {
835
- idx = i;
836
- }
837
- if ( !isNonNegativeInteger( idx ) ) {
838
- throw new TypeError( 'invalid argument. Index argument must be a nonnegative integer. Value: `'+idx+'`.' );
839
- }
840
- if ( idx >= this._length ) {
841
- return;
842
- }
843
- idx *= 2;
844
- if ( out ) {
845
- out[ 0 ] = buf[ idx ];
846
- out[ 1 ] = buf[ idx+1 ];
847
- return out;
848
- }
849
- return new Complex64( buf[ idx ], buf[ idx+1 ] );
826
+ setReadOnly( Complex64Array.prototype, 'get', function get( idx ) {
827
+ var buf;
828
+ if ( !isComplexArray( this ) ) {
829
+ throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
830
+ }
831
+ if ( !isNonNegativeInteger( idx ) ) {
832
+ throw new TypeError( 'invalid argument. Must provide a nonnegative integer. Value: `'+idx+'`.' );
833
+ }
834
+ if ( idx >= this._length ) {
835
+ return;
850
836
  }
837
+ buf = this._buffer;
838
+ idx *= 2;
839
+ return new Complex64( buf[ idx ], buf[ idx+1 ] );
851
840
  });
852
841
 
853
842
  /**
@@ -855,6 +844,7 @@ defineProperty( Complex64Array.prototype, 'get', {
855
844
  *
856
845
  * @name length
857
846
  * @memberof Complex64Array.prototype
847
+ * @readonly
858
848
  * @type {NonNegativeInteger}
859
849
  *
860
850
  * @example
@@ -863,12 +853,8 @@ defineProperty( Complex64Array.prototype, 'get', {
863
853
  * var len = arr.length;
864
854
  * // returns 10
865
855
  */
866
- defineProperty( Complex64Array.prototype, 'length', {
867
- 'configurable': false,
868
- 'enumerable': false,
869
- 'get': function get() {
870
- return this._length;
871
- }
856
+ setReadOnlyAccessor( Complex64Array.prototype, 'length', function get() {
857
+ return this._length;
872
858
  });
873
859
 
874
860
  /**
@@ -909,18 +895,18 @@ defineProperty( Complex64Array.prototype, 'length', {
909
895
  * @returns {void}
910
896
  *
911
897
  * @example
912
- * var real = require( '@stdlib/complex-real' );
913
- * var imag = require( '@stdlib/complex-imag' );
898
+ * var realf = require( '@stdlib/complex-realf' );
899
+ * var imagf = require( '@stdlib/complex-imagf' );
914
900
  *
915
901
  * var arr = new Complex64Array( 10 );
916
902
  *
917
903
  * var z = arr.get( 0 );
918
904
  * // returns <Complex64>
919
905
  *
920
- * var re = real( z );
906
+ * var re = realf( z );
921
907
  * // returns 0.0
922
908
  *
923
- * var im = imag( z );
909
+ * var im = imagf( z );
924
910
  * // returns 0.0
925
911
  *
926
912
  * arr.set( [ 1.0, -1.0 ], 0 );
@@ -928,54 +914,95 @@ defineProperty( Complex64Array.prototype, 'length', {
928
914
  * z = arr.get( 0 );
929
915
  * // returns <Complex64>
930
916
  *
931
- * re = real( z );
917
+ * re = realf( z );
932
918
  * // returns 1.0
933
919
  *
934
- * im = imag( z );
920
+ * im = imagf( z );
935
921
  * // returns -1.0
936
922
  */
937
- defineProperty( Complex64Array.prototype, 'set', {
938
- 'configurable': false,
939
- 'enumerable': false,
940
- 'writable': false,
941
- 'value': function set( value ) {
942
- /* eslint-disable no-underscore-dangle */
943
- var sbuf;
944
- var idx;
945
- var buf;
946
- var tmp;
947
- var flg;
948
- var N;
949
- var v;
950
- var i;
951
- var j;
952
- if ( !isComplexArray( this ) ) {
953
- throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
923
+ setReadOnly( Complex64Array.prototype, 'set', function set( value ) {
924
+ /* eslint-disable no-underscore-dangle */
925
+ var sbuf;
926
+ var idx;
927
+ var buf;
928
+ var tmp;
929
+ var flg;
930
+ var N;
931
+ var v;
932
+ var i;
933
+ var j;
934
+ if ( !isComplexArray( this ) ) {
935
+ throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
936
+ }
937
+ buf = this._buffer;
938
+ if ( arguments.length > 1 ) {
939
+ idx = arguments[ 1 ];
940
+ if ( !isNonNegativeInteger( idx ) ) {
941
+ throw new TypeError( 'invalid argument. Index argument must be a nonnegative integer. Value: `'+idx+'`.' );
954
942
  }
955
- buf = this._buffer;
956
- if ( arguments.length > 1 ) {
957
- idx = arguments[ 1 ];
958
- if ( !isNonNegativeInteger( idx ) ) {
959
- throw new TypeError( 'invalid argument. Index argument must be a nonnegative integer. Value: `'+idx+'`.' );
943
+ } else {
944
+ idx = 0;
945
+ }
946
+ if ( isComplexLike( value ) ) {
947
+ if ( idx >= this._length ) {
948
+ throw new RangeError( 'invalid argument. Index argument is out-of-bounds. Value: `'+idx+'`.' );
949
+ }
950
+ idx *= 2;
951
+ buf[ idx ] = realf( value );
952
+ buf[ idx+1 ] = imagf( value );
953
+ return;
954
+ }
955
+ if ( isComplexArray( value ) ) {
956
+ N = value._length;
957
+ if ( idx+N > this._length ) {
958
+ throw new RangeError( 'invalid arguments. Target array lacks sufficient storage to accommodate source values.' );
959
+ }
960
+ sbuf = value._buffer;
961
+
962
+ // Check for overlapping memory...
963
+ j = buf.byteOffset + (idx*BYTES_PER_ELEMENT);
964
+ if (
965
+ sbuf.buffer === buf.buffer &&
966
+ (
967
+ sbuf.byteOffset < j &&
968
+ sbuf.byteOffset+sbuf.byteLength > j
969
+ )
970
+ ) {
971
+ // We need to copy source values...
972
+ tmp = new Float32Array( sbuf.length );
973
+ for ( i = 0; i < sbuf.length; i++ ) {
974
+ tmp[ i ] = sbuf[ i ];
960
975
  }
961
- } else {
962
- idx = 0;
976
+ sbuf = tmp;
963
977
  }
964
- if ( isComplexLike( value ) ) {
965
- if ( idx >= this._length ) {
966
- throw new RangeError( 'invalid argument. Index argument is out-of-bounds. Value: `'+idx+'`.' );
978
+ idx *= 2;
979
+ j = 0;
980
+ for ( i = 0; i < N; i++ ) {
981
+ buf[ idx ] = sbuf[ j ];
982
+ buf[ idx+1 ] = sbuf[ j+1 ];
983
+ idx += 2; // stride
984
+ j += 2; // stride
985
+ }
986
+ return;
987
+ }
988
+ if ( isCollection( value ) ) {
989
+ // Detect whether we've been provided an array of complex numbers...
990
+ N = value.length;
991
+ for ( i = 0; i < N; i++ ) {
992
+ if ( !isComplexLike( value[ i ] ) ) {
993
+ flg = true;
994
+ break;
967
995
  }
968
- idx *= 2;
969
- buf[ idx ] = real( value );
970
- buf[ idx+1 ] = imag( value );
971
- return;
972
996
  }
973
- if ( isComplexArray( value ) ) {
974
- N = value._length;
975
- if ( idx+N > this._length ) {
997
+ // If an array does not contain only complex numbers, then we assume interleaved real and imaginary components...
998
+ if ( flg ) {
999
+ if ( !isEven( N ) ) {
1000
+ throw new RangeError( 'invalid argument. Array-like object arguments must have a length which is a multiple of two. Length: `'+N+'`.' );
1001
+ }
1002
+ if ( idx+(N/2) > this._length ) {
976
1003
  throw new RangeError( 'invalid arguments. Target array lacks sufficient storage to accommodate source values.' );
977
1004
  }
978
- sbuf = value._buffer;
1005
+ sbuf = value;
979
1006
 
980
1007
  // Check for overlapping memory...
981
1008
  j = buf.byteOffset + (idx*BYTES_PER_ELEMENT);
@@ -987,13 +1014,14 @@ defineProperty( Complex64Array.prototype, 'set', {
987
1014
  )
988
1015
  ) {
989
1016
  // We need to copy source values...
990
- tmp = new Float32Array( sbuf.length );
991
- for ( i = 0; i < sbuf.length; i++ ) {
1017
+ tmp = new Float32Array( N );
1018
+ for ( i = 0; i < N; i++ ) {
992
1019
  tmp[ i ] = sbuf[ i ];
993
1020
  }
994
1021
  sbuf = tmp;
995
1022
  }
996
1023
  idx *= 2;
1024
+ N /= 2;
997
1025
  j = 0;
998
1026
  for ( i = 0; i < N; i++ ) {
999
1027
  buf[ idx ] = sbuf[ j ];
@@ -1003,69 +1031,22 @@ defineProperty( Complex64Array.prototype, 'set', {
1003
1031
  }
1004
1032
  return;
1005
1033
  }
1006
- if ( isCollection( value ) ) {
1007
- // Detect whether we've been provided an array of complex numbers...
1008
- N = value.length;
1009
- for ( i = 0; i < N; i++ ) {
1010
- if ( !isComplexLike( value[ i ] ) ) {
1011
- flg = true;
1012
- break;
1013
- }
1014
- }
1015
- // If an array does not contain only complex numbers, then we assume interleaved real and imaginary components...
1016
- if ( flg ) {
1017
- if ( !isEven( N ) ) {
1018
- throw new RangeError( 'invalid argument. Array-like object arguments must have a length which is a multiple of two. Length: `'+N+'`.' );
1019
- }
1020
- if ( idx+(N/2) > this._length ) {
1021
- throw new RangeError( 'invalid arguments. Target array lacks sufficient storage to accommodate source values.' );
1022
- }
1023
- sbuf = value;
1024
-
1025
- // Check for overlapping memory...
1026
- j = buf.byteOffset + (idx*BYTES_PER_ELEMENT);
1027
- if (
1028
- sbuf.buffer === buf.buffer &&
1029
- (
1030
- sbuf.byteOffset < j &&
1031
- sbuf.byteOffset+sbuf.byteLength > j
1032
- )
1033
- ) {
1034
- // We need to copy source values...
1035
- tmp = new Float32Array( N );
1036
- for ( i = 0; i < N; i++ ) {
1037
- tmp[ i ] = sbuf[ i ];
1038
- }
1039
- sbuf = tmp;
1040
- }
1041
- idx *= 2;
1042
- N /= 2;
1043
- j = 0;
1044
- for ( i = 0; i < N; i++ ) {
1045
- buf[ idx ] = sbuf[ j ];
1046
- buf[ idx+1 ] = sbuf[ j+1 ];
1047
- idx += 2; // stride
1048
- j += 2; // stride
1049
- }
1050
- return;
1051
- }
1052
- // If an array contains only complex numbers, then we need to extract real and imaginary components...
1053
- if ( idx+N > this._length ) {
1054
- throw new RangeError( 'invalid arguments. Target array lacks sufficient storage to accommodate source values.' );
1055
- }
1056
- idx *= 2;
1057
- for ( i = 0; i < N; i++ ) {
1058
- v = value[ i ];
1059
- buf[ idx ] = real( v );
1060
- buf[ idx+1 ] = imag( v );
1061
- idx += 2; // stride
1062
- }
1063
- return;
1034
+ // If an array contains only complex numbers, then we need to extract real and imaginary components...
1035
+ if ( idx+N > this._length ) {
1036
+ throw new RangeError( 'invalid arguments. Target array lacks sufficient storage to accommodate source values.' );
1064
1037
  }
1065
- throw new TypeError( 'invalid argument. First argument must be either a complex number, an array-like object, or a complex number array. Value: `'+value+'`.' );
1066
-
1067
- /* eslint-enable no-underscore-dangle */
1038
+ idx *= 2;
1039
+ for ( i = 0; i < N; i++ ) {
1040
+ v = value[ i ];
1041
+ buf[ idx ] = realf( v );
1042
+ buf[ idx+1 ] = imagf( v );
1043
+ idx += 2; // stride
1044
+ }
1045
+ return;
1068
1046
  }
1047
+ throw new TypeError( 'invalid argument. First argument must be either a complex number, an array-like object, or a complex number array. Value: `'+value+'`.' );
1048
+
1049
+ /* eslint-enable no-underscore-dangle */
1069
1050
  });
1070
1051
 
1071
1052