Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,38 @@ Returns the index of the first truthy element in a one-dimensional double-precis

```javascript
var Complex128Vector = require( '@stdlib/ndarray/vector/complex128' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );

var x = new Complex128Vector( [ 0.0, 0.0, 1.0, 2.0, 4.0, 5.0 ] );

var idx = zindexOfTruthy( [ x ] );
var fromIndex = scalar2ndarray( 0, {
'dtype': 'generic'
});

var idx = zindexOfTruthy( [ x, fromIndex ] );
// returns 1
```

The function has the following parameters:

- **arrays**: array-like object containing a one-dimensional input ndarray.
- **arrays**: array-like object containing the following ndarrays:

- a one-dimensional input ndarray.
- a zero-dimensional ndarray containing the index from which to begin searching.

If the function is unable to find a truthy element, the function returns `-1`.

```javascript
var Complex128Vector = require( '@stdlib/ndarray/vector/complex128' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );

var x = new Complex128Vector( [ 0.0, 0.0, 0.0, 0.0 ] );

var idx = zindexOfTruthy( [ x ] );
var fromIndex = scalar2ndarray( 0, {
'dtype': 'generic'
});

var idx = zindexOfTruthy( [ x, fromIndex ] );
// returns -1
```

Expand All @@ -74,6 +87,7 @@ var idx = zindexOfTruthy( [ x ] );

- A complex number is truthy when at least one of its real or imaginary components is truthy.
- The function treats `NaN` values as falsy.
- If a specified starting search index is negative, the function resolves the starting search index by counting backward from the last element (where `-1` refers to the last element).

</section>

Expand All @@ -88,6 +102,7 @@ var idx = zindexOfTruthy( [ x ] );
```javascript
var bernoulli = require( '@stdlib/random/array/bernoulli' );
var Complex128Vector = require( '@stdlib/ndarray/vector/complex128' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var zindexOfTruthy = require( '@stdlib/blas/ext/base/ndarray/zindex-of-truthy' );

Expand All @@ -97,7 +112,11 @@ var buf = bernoulli( 10*2, 0.7, {
var x = new Complex128Vector( buf.buffer );
console.log( ndarray2array( x ) );

var idx = zindexOfTruthy( [ x ] );
var fromIndex = scalar2ndarray( 0, {
'dtype': 'generic'
});

var idx = zindexOfTruthy( [ x, fromIndex ] );
console.log( idx );
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var bench = require( '@stdlib/bench' );
var pow = require( '@stdlib/math/base/special/pow' );
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
var zeros = require( '@stdlib/ndarray/zeros' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var zindexOfTruthy = require( './../lib' );
Expand All @@ -46,7 +47,13 @@ var options = {
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x = zeros( [ len ], options );
var fromIndex;
var x;

x = zeros( [ len ], options );
fromIndex = scalar2ndarray( 0, {
'dtype': 'generic'
});
return benchmark;

/**
Expand All @@ -61,7 +68,7 @@ function createBenchmark( len ) {

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = zindexOfTruthy( [ x ] );
out = zindexOfTruthy( [ x, fromIndex ] );
if ( out !== out ) {
b.fail( 'should return an integer' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,25 @@
Returns the index of the first truthy element in a one-dimensional double-
precision complex floating-point ndarray.

If unable to find a truthy element, the function returns `-1`.

A complex number is truthy when at least one of its real or imaginary
components is truthy.

The function treats `NaN` values as falsy.

If a specified starting search index is negative, the function resolves the
starting search index by counting backward from the last element (where `-1`
refers to the last element).

If unable to find a truthy element, the function returns `-1`.

Parameters
----------
arrays: ArrayLikeObject<ndarray>
Array-like object containing a one-dimensional input ndarray.
Array-like object containing the following ndarrays:

- a one-dimensional input ndarray.
- a zero-dimensional ndarray containing the index from which to begin
searching.

Returns
-------
Expand All @@ -23,7 +31,8 @@
Examples
--------
> var x = new {{alias:@stdlib/ndarray/vector/complex128}}( [ 0.0, 0.0, 1.0, 2.0 ] );
> {{alias}}( [ x ] )
> var i = {{alias:@stdlib/ndarray/from-scalar}}( 0, { 'dtype': 'generic' } );
> {{alias}}( [ x, i ] )
1

See Also
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,35 @@

/// <reference types="@stdlib/types"/>

import { complex128ndarray } from '@stdlib/types/ndarray';
import { complex128ndarray, typedndarray } from '@stdlib/types/ndarray';

/**
* Returns the index of the first truthy element in a one-dimensional double-precision complex floating-point ndarray.
*
* ## Notes
*
* - The function expects the following ndarrays:
*
* - a one-dimensional input ndarray.
* - a zero-dimensional ndarray containing the index from which to begin searching.
*
* @param arrays - array-like object containing ndarrays
* @returns index
*
* @example
* var Complex128Vector = require( '@stdlib/ndarray/vector/complex128' );
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
*
* var x = new Complex128Vector( [ 0.0, 0.0, 1.0, 2.0, 4.0, 5.0 ] );
*
* var idx = zindexOfTruthy( [ x ] );
* var fromIndex = scalar2ndarray( 0, {
* 'dtype': 'generic'
* });
*
* var idx = zindexOfTruthy( [ x, fromIndex ] );
* // returns 1
*/
declare function zindexOfTruthy( arrays: [ complex128ndarray ] ): number;
declare function zindexOfTruthy( arrays: [ complex128ndarray, typedndarray<number> ] ): number;


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
/* eslint-disable space-in-parens */

import zeros = require( '@stdlib/ndarray/zeros' );
import scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
import zindexOfTruthy = require( './index' );


Expand All @@ -29,8 +30,11 @@ import zindexOfTruthy = require( './index' );
const x = zeros( [ 10 ], {
'dtype': 'complex128'
});
const fromIndex = scalar2ndarray( 0, {
'dtype': 'generic'
});

zindexOfTruthy( [ x ] ); // $ExpectType number
zindexOfTruthy( [ x, fromIndex ] ); // $ExpectType number
}

// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays...
Expand All @@ -51,7 +55,10 @@ import zindexOfTruthy = require( './index' );
const x = zeros( [ 10 ], {
'dtype': 'complex128'
});
const fromIndex = scalar2ndarray( 0, {
'dtype': 'generic'
});

zindexOfTruthy(); // $ExpectError
zindexOfTruthy( [ x ], {} ); // $ExpectError
zindexOfTruthy( [ x, fromIndex ], {} ); // $ExpectError
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

var bernoulli = require( '@stdlib/random/array/bernoulli' );
var Complex128Vector = require( '@stdlib/ndarray/vector/complex128' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var zindexOfTruthy = require( './../lib' );

Expand All @@ -29,5 +30,9 @@ var buf = bernoulli( 10*2, 0.7, {
var x = new Complex128Vector( buf.buffer );
console.log( ndarray2array( x ) );

var idx = zindexOfTruthy( [ x ] );
var fromIndex = scalar2ndarray( 0, {
'dtype': 'generic'
});

var idx = zindexOfTruthy( [ x, fromIndex ] );
console.log( idx );
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@
*
* @example
* var Complex128Vector = require( '@stdlib/ndarray/vector/complex128' );
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
* var zindexOfTruthy = require( '@stdlib/blas/ext/base/ndarray/zindex-of-truthy' );
*
* var x = new Complex128Vector( [ 0.0, 0.0, 1.0, 2.0, 4.0, 5.0 ] );
*
* var idx = zindexOfTruthy( [ x ] );
* var fromIndex = scalar2ndarray( 0, {
* 'dtype': 'generic'
* });
*
* var idx = zindexOfTruthy( [ x, fromIndex ] );
* // returns 1
*/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@

// MODULES //

var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' );
var numelDimension = require( '@stdlib/ndarray/base/numel-dimension' );
var getStride = require( '@stdlib/ndarray/base/stride' );
var getOffset = require( '@stdlib/ndarray/base/offset' );
var getData = require( '@stdlib/ndarray/base/data-buffer' );
var clipIndex = require( '@stdlib/ndarray/base/clip-index' );
var strided = require( '@stdlib/blas/ext/base/zindex-of-truthy' ).ndarray;


Expand All @@ -37,21 +39,50 @@ var strided = require( '@stdlib/blas/ext/base/zindex-of-truthy' ).ndarray;
* - The function expects the following ndarrays:
*
* - a one-dimensional input ndarray.
* - a zero-dimensional ndarray containing the index from which to begin searching.
*
* @param {ArrayLikeObject<Object>} arrays - array-like object containing ndarrays
* @returns {integer} index
*
* @example
* var Complex128Vector = require( '@stdlib/ndarray/vector/complex128' );
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
*
* var x = new Complex128Vector( [ 0.0, 0.0, 1.0, 2.0, 4.0, 5.0 ] );
*
* var idx = zindexOfTruthy( [ x ] );
* var fromIndex = scalar2ndarray( 0, {
* 'dtype': 'generic'
* });
*
* var idx = zindexOfTruthy( [ x, fromIndex ] );
* // returns 1
*/
function zindexOfTruthy( arrays ) {
var x = arrays[ 0 ];
return strided( numelDimension( x, 0 ), getData( x ), getStride( x, 0 ), getOffset( x ) ); // eslint-disable-line max-len
var fromIndex;
var stride;
var offset;
var idx;
var N;
var x;

x = arrays[ 0 ];
fromIndex = ndarraylike2scalar( arrays[ 1 ] );

N = numelDimension( x, 0 );
fromIndex = clipIndex( fromIndex, N );
if ( fromIndex >= N ) {
return -1;
}
N -= fromIndex;

stride = getStride( x, 0 );
offset = getOffset( x ) + ( stride*fromIndex );

idx = strided( N, getData( x ), stride, offset );
if ( idx >= 0 ) {
idx += fromIndex;
}
return idx;
}


Expand Down
Loading