Skip to content

Commit b597754

Browse files
Jaysukh-409kgryte
andauthored
feat: add at method to array/complex128
PR-URL: #1202 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Athan Reines <[email protected]>
1 parent e244066 commit b597754

File tree

5 files changed

+394
-5
lines changed

5 files changed

+394
-5
lines changed

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,56 @@ len = arr.length;
514514
// returns 2
515515
```
516516

517+
<a name="method-at"></a>
518+
519+
#### Complex128Array.prototype.at( i )
520+
521+
Returns an array element located at integer position (index) `i`, with support for both nonnegative and negative integer positions.
522+
523+
```javascript
524+
var real = require( '@stdlib/complex/real' );
525+
var imag = require( '@stdlib/complex/imag' );
526+
527+
var arr = new Complex128Array( 10 );
528+
529+
// Set the first, second, and last elements:
530+
arr.set( [ 1.0, -1.0 ], 0 );
531+
arr.set( [ 2.0, -2.0 ], 1 );
532+
arr.set( [ 9.0, -9.0 ], 9 );
533+
534+
// Get the first element:
535+
var z = arr.at( 0 );
536+
// returns <Complex128>
537+
538+
var re = real( z );
539+
// returns 1.0
540+
541+
var im = imag( z );
542+
// returns -1.0
543+
544+
// Get the last element:
545+
z = arr.at( -1 );
546+
// returns <Complex128>
547+
548+
re = real( z );
549+
// returns 9.0
550+
551+
im = imag( z );
552+
// returns -9.0
553+
```
554+
555+
If provided an out-of-bounds index, the method returns `undefined`.
556+
557+
```javascript
558+
var arr = new Complex128Array( 10 );
559+
560+
var z = arr.at( 100 );
561+
// returns undefined
562+
563+
z = arr.at( -100 );
564+
// returns undefined
565+
```
566+
517567
<a name="method-copy-within"></a>
518568

519569
#### Complex128Array.prototype.copyWithin( target, start\[, end] )
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var Complex128 = require( '@stdlib/complex/float64' );
25+
var isComplex128 = require( '@stdlib/assert/is-complex128' );
26+
var pkg = require( './../package.json' ).name;
27+
var Complex128Array = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+'::nonnegative_indices:at', function benchmark( b ) {
33+
var arr;
34+
var N;
35+
var z;
36+
var i;
37+
38+
arr = [];
39+
for ( i = 0; i < 10; i++ ) {
40+
arr.push( new Complex128( i, i ) );
41+
}
42+
arr = new Complex128Array( arr );
43+
N = arr.length;
44+
45+
b.tic();
46+
for ( i = 0; i < b.iterations; i++ ) {
47+
z = arr.at( i%N );
48+
if ( typeof z !== 'object' ) {
49+
b.fail( 'should return an object' );
50+
}
51+
}
52+
b.toc();
53+
if ( !isComplex128( z ) ) {
54+
b.fail( 'should return a complex number' );
55+
}
56+
b.pass( 'benchmark finished' );
57+
b.end();
58+
});
59+
60+
bench( pkg+'::negative_indices:at', function benchmark( b ) {
61+
var arr;
62+
var N;
63+
var z;
64+
var i;
65+
66+
arr = [];
67+
for ( i = 0; i < 10; i++ ) {
68+
arr.push( new Complex128( i, i ) );
69+
}
70+
arr = new Complex128Array( arr );
71+
N = arr.length;
72+
73+
b.tic();
74+
for ( i = 0; i < b.iterations; i++ ) {
75+
z = arr.at( -(i%N)-1 );
76+
if ( typeof z !== 'object' ) {
77+
b.fail( 'should return an object' );
78+
}
79+
}
80+
b.toc();
81+
if ( !isComplex128( z ) ) {
82+
b.fail( 'should return a complex number' );
83+
}
84+
b.pass( 'benchmark finished' );
85+
b.end();
86+
});

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,33 @@ declare class Complex128Array implements Complex128ArrayInterface {
101101
*/
102102
constructor( arg?: number | RealOrComplexTypedArray | ArrayLike<number | ComplexLike> | ArrayBuffer | Iterable<number | ComplexLike>, byteOffset?: number, length?: number );
103103

104+
/**
105+
* Returns an array element with support for both nonnegative and negative integer indices.
106+
*
107+
* @param i - element index
108+
* @throws index argument must be an integer
109+
* @returns array element
110+
*
111+
* @example
112+
* var arr = new Complex128Array( 10 );
113+
*
114+
* var z = arr.at( 0 );
115+
* // returns <Complex128>
116+
*
117+
* arr.set( [ 1.0, -1.0 ], 0 );
118+
* arr.set( [ 9.0, -9.0 ], 9 );
119+
*
120+
* z = arr.get( -1 )
121+
* // return <Complex128>
122+
*
123+
* z = arr.at( 100 );
124+
* // returns undefined
125+
*
126+
* z = arr.at( -100 );
127+
* // returns undefined
128+
*/
129+
at( i: number ): Complex128 | void;
130+
104131
/**
105132
* Length (in bytes) of the array.
106133
*

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

Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,19 @@ function isComplex128Array( value ) {
131131
);
132132
}
133133

134+
/**
135+
* Retrieves a complex number from a complex number array buffer.
136+
*
137+
* @private
138+
* @param {Float64Array} buf - array buffer
139+
* @param {NonNegativeInteger} idx - element index
140+
* @returns {Complex128} complex number
141+
*/
142+
function getComplex128( buf, idx ) {
143+
idx *= 2;
144+
return new Complex128( buf[ idx ], buf[ idx+1 ] );
145+
}
146+
134147

135148
// MAIN //
136149

@@ -552,6 +565,76 @@ setReadOnly( Complex128Array, 'of', function of() {
552565
return new this( args );
553566
});
554567

568+
/**
569+
* Returns an array element with support for both nonnegative and negative integer indices.
570+
*
571+
* @name at
572+
* @memberof Complex128Array.prototype
573+
* @type {Function}
574+
* @param {integer} idx - element index
575+
* @throws {TypeError} `this` must be a complex number array
576+
* @throws {TypeError} must provide an integer
577+
* @returns {(Complex128|void)} array element
578+
*
579+
* @example
580+
* var real = require( '@stdlib/complex/real' );
581+
* var imag = require( '@stdlib/complex/imag' );
582+
*
583+
* var arr = new Complex128Array( 10 );
584+
*
585+
* var z = arr.at( 0 );
586+
* // returns <Complex128>
587+
*
588+
* var re = real( z );
589+
* // returns 0.0
590+
*
591+
* var im = imag( z );
592+
* // returns 0.0
593+
*
594+
* arr.set( [ 1.0, -1.0 ], 0 );
595+
* arr.set( [ 2.0, -2.0 ], 1 );
596+
* arr.set( [ 9.0, -9.0 ], 9 );
597+
*
598+
* z = arr.at( 0 );
599+
* // returns <Complex128>
600+
*
601+
* re = real( z );
602+
* // returns 1.0
603+
*
604+
* im = imag( z );
605+
* // returns -1.0
606+
*
607+
* z = arr.at( -1 );
608+
* // returns <Complex128>
609+
*
610+
* re = real( z );
611+
* // returns 9.0
612+
*
613+
* im = imag( z );
614+
* // returns -9.0
615+
*
616+
* z = arr.at( 100 );
617+
* // returns undefined
618+
*
619+
* z = arr.at( -100 );
620+
* // returns undefined
621+
*/
622+
setReadOnly( Complex128Array.prototype, 'at', function at( idx ) {
623+
if ( !isComplexArray( this ) ) {
624+
throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
625+
}
626+
if ( !isInteger( idx ) ) {
627+
throw new TypeError( format( 'invalid argument. Must provide an integer. Value: `%s`.', idx ) );
628+
}
629+
if ( idx < 0 ) {
630+
idx += this._length;
631+
}
632+
if ( idx < 0 || idx >= this._length ) {
633+
return;
634+
}
635+
return getComplex128( this._buffer, idx );
636+
});
637+
555638
/**
556639
* Pointer to the underlying data buffer.
557640
*
@@ -830,7 +913,6 @@ setReadOnly( Complex128Array.prototype, 'entries', function entries() {
830913
* // returns undefined
831914
*/
832915
setReadOnly( Complex128Array.prototype, 'get', function get( idx ) {
833-
var buf;
834916
if ( !isComplexArray( this ) ) {
835917
throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
836918
}
@@ -840,9 +922,7 @@ setReadOnly( Complex128Array.prototype, 'get', function get( idx ) {
840922
if ( idx >= this._length ) {
841923
return;
842924
}
843-
buf = this._buffer;
844-
idx *= 2;
845-
return new Complex128( buf[ idx ], buf[ idx+1 ] );
925+
return getComplex128( this._buffer, idx );
846926
});
847927

848928
/**
@@ -886,7 +966,6 @@ setReadOnlyAccessor( Complex128Array.prototype, 'length', function get() {
886966
*
887967
* 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.
888968
*
889-
*
890969
* @name set
891970
* @memberof Complex128Array.prototype
892971
* @type {Function}

0 commit comments

Comments
 (0)