Skip to content

Commit

Permalink
Auto-generated commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stdlib-bot committed Dec 16, 2023
1 parent d235c18 commit 267d64e
Show file tree
Hide file tree
Showing 8 changed files with 387 additions and 5 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Dorrin Sotoudeh <[email protected]>
Frank Kovacs <[email protected]>
Harshita Kalani <[email protected]>
James Gelok <[email protected]>
Jaysukh Makvana <[email protected]>
Jithin KS <[email protected]>
Joey Reed <[email protected]>
Jordan Gallivan <[email protected]>
Expand Down
52 changes: 51 additions & 1 deletion complex64/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ var bool = it.next().done;

#### Complex64Array.prototype.get( i )

Returns an array element located at position (index) `i`.
Returns an array element located at a nonnegative integer position (index) `i`.

```javascript
var realf = require( '@stdlib/complex/realf' );
Expand Down Expand Up @@ -783,6 +783,56 @@ var z = arr.get( 100 );
// returns undefined
```

<a name="method-at"></a>

#### Complex64Array.prototype.at( i )

Returns an array element located at integer position (index) `i`, with support for both nonnegative and negative integer positions.

```javascript
var realf = require( '@stdlib/complex/realf' );
var imagf = require( '@stdlib/complex/imagf' );

var arr = new Complex64Array( 10 );

// Set the first, second, and last elements:
arr.set( [ 1.0, -1.0 ], 0 );
arr.set( [ 2.0, -2.0 ], 1 );
arr.set( [ 9.0, -9.0 ], 9 );

// Get the first element:
var z = arr.at( 0 );
// returns <Complex64>

var re = realf( z );
// returns 1.0

var im = imagf( z );
// returns -1.0

// Get the last element:
z = arr.at( -1 );
// returns <Complex64>

re = realf( z );
// returns 9.0

im = imagf( z );
// returns -9.0
```

If provided an out-of-bounds index, the method returns `undefined`.

```javascript
var arr = new Complex64Array( 10 );

var z = arr.at( 100 );
// returns undefined

z = arr.at( -100 );
// returns undefined
```

<a name="method-set"></a>

#### Complex64Array.prototype.set( z\[, i] )
Expand Down
86 changes: 86 additions & 0 deletions complex64/benchmark/benchmark.at.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2023 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var Complex64 = require( '@stdlib/complex/float32' );
var isComplex64 = require( '@stdlib/assert/is-complex64' );
var pkg = require( './../package.json' ).name;
var Complex64Array = require( './../lib' );


// MAIN //

bench( pkg+'::nonnegative_indices:at', function benchmark( b ) {
var arr;
var N;
var z;
var i;

arr = [];
for ( i = 0; i < 10; i++ ) {
arr.push( new Complex64( i, i ) );
}
arr = new Complex64Array( arr );
N = arr.length;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
z = arr.at( i%N );
if ( typeof z !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( !isComplex64( z ) ) {
b.fail( 'should return a complex number' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::negative_indices:at', function benchmark( b ) {
var arr;
var N;
var z;
var i;

arr = [];
for ( i = 0; i < 10; i++ ) {
arr.push( new Complex64( i, i ) );
}
arr = new Complex64Array( arr );
N = arr.length;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
z = arr.at( -(i%N)-1 );
if ( typeof z !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( !isComplex64( z ) ) {
b.fail( 'should return a complex number' );
}
b.pass( 'benchmark finished' );
b.end();
});
27 changes: 27 additions & 0 deletions complex64/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,33 @@ declare class Complex64Array implements Complex64ArrayInterface {
*/
get( i: number ): Complex64 | void;

/**
* Returns an array element with support for both nonnegative and negative integer indices.
*
* @param i - element index
* @throws index argument must be a integer
* @returns array element
*
* @example
* var arr = new Complex64Array( 10 );
*
* var z = arr.at( 0 );
* // returns <Complex64>
*
* arr.set( [ 1.0, -1.0 ], 0 );
* arr.set( [ 9.0, -9.0 ], 9 );
*
* z = arr.get( -1 )
* // return <Complex64>
*
* z = arr.at( 100 );
* // returns undefined
*
* z = arr.at( -100 );
* // returns undefined
*/
at( i: number ): Complex64 | void;

/**
* Sets an array element.
*
Expand Down
73 changes: 72 additions & 1 deletion complex64/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,78 @@ setReadOnly( Complex64Array.prototype, 'get', function get( idx ) {
return new Complex64( buf[ idx ], buf[ idx+1 ] );
});

/**
* Returns an array element with support for both nonnegative and negative integer indices.
*
* @name at
* @memberof Complex64Array.prototype
* @type {Function}
* @param {integer} idx - element index
* @throws {TypeError} `this` must be a complex number array
* @throws {TypeError} must provide a integer
* @returns {(Complex64|void)} array element
*
* @example
* var arr = new Complex64Array( 10 );
* var realf = require( '@stdlib/complex/realf' );
* var imagf = require( '@stdlib/complex/imagf' );
*
* var z = arr.at( 0 );
* // returns <Complex64>
*
* var re = realf( z );
* // returns 0.0
*
* var im = imagf( z );
* // returns 0.0
*
* arr.set( [ 1.0, -1.0 ], 0 );
* arr.set( [ 2.0, -2.0 ], 1 );
* arr.set( [ 9.0, -9.0 ], 9 );
*
* z = arr.at( 0 );
* // returns <Complex64>
*
* re = realf( z );
* // returns 1.0
*
* im = imagf( z );
* // returns -1.0
*
* z = arr.at( -1 );
* // returns <Complex64>
*
* re = realf( z );
* // returns 9.0
*
* im = imagf( z );
* // returns -9.0
*
* z = arr.at( 100 );
* // returns undefined
*
* z = arr.at( -100 );
* // returns undefined
*/
setReadOnly( Complex64Array.prototype, 'at', function at( idx ) {
var buf;
if ( !isComplexArray( this ) ) {
throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
}
if ( !isInteger( idx ) ) {
throw new TypeError( format( 'invalid argument. Must provide a integer. Value: `%s`.', idx ) );
}
if ( idx < 0 ) {
idx += this._length;
}
if ( idx < 0 || idx >= this._length ) {
return;
}
buf = this._buffer;
idx *= 2;
return new Complex64( buf[ idx ], buf[ idx+1 ] );
});

/**
* Number of array elements.
*
Expand Down Expand Up @@ -886,7 +958,6 @@ setReadOnlyAccessor( Complex64Array.prototype, 'length', function get() {
*
* 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.
*
*
* @name set
* @memberof Complex64Array.prototype
* @type {Function}
Expand Down
Loading

0 comments on commit 267d64e

Please sign in to comment.