Skip to content
Open
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 @@ -80,6 +80,30 @@ bench( format( '%s:dtype=float32', pkg ), function benchmark( b ) {
b.end();
});

bench( format( '%s:dtype=float16', pkg ), function benchmark( b ) {
var x;
var o;
var i;

x = empty( [ 2 ] );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
o = scalar2ndarrayLike( x, i, {
'dtype': 'float16'
});
if ( o.length !== 1 ) {
b.fail( 'should have length 1' );
}
}
b.toc();
if ( !isndarrayLike( o ) ) {
b.fail( 'should return an ndarray' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( format( '%s:dtype=complex128', pkg ), function benchmark( b ) {
var x;
var o;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/// <reference types="@stdlib/types"/>

import { ComplexLike } from '@stdlib/types/complex';
import { ndarray, float64ndarray, float32ndarray, int32ndarray, int16ndarray, int8ndarray, uint32ndarray, uint16ndarray, uint8ndarray, uint8cndarray, complex128ndarray, complex64ndarray, genericndarray, boolndarray, DataType, Order, Float64DataType, Float32DataType, Complex128DataType, Complex64DataType, BooleanDataType, Int32DataType, Int16DataType, Int8DataType, Uint32DataType, Uint16DataType, Uint8DataType, Uint8cDataType, GenericDataType } from '@stdlib/types/ndarray';
import { ndarray, float64ndarray, float32ndarray, float16ndarray, int32ndarray, int16ndarray, int8ndarray, uint32ndarray, uint16ndarray, uint8ndarray, uint8cndarray, complex128ndarray, complex64ndarray, genericndarray, boolndarray, DataType, Order, Float64DataType, Float32DataType, Float16DataType, Complex128DataType, Complex64DataType, BooleanDataType, Int32DataType, Int16DataType, Int8DataType, Uint32DataType, Uint16DataType, Uint8DataType, Uint8cDataType, GenericDataType } from '@stdlib/types/ndarray';

/**
* Interface defining common options.
Expand All @@ -41,7 +41,7 @@
/**
* Interface defining options.
*/
interface Options extends BaseOptions {

Check failure on line 44 in lib/node_modules/@stdlib/ndarray/from-scalar-like/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

'Options' is defined but never used
/**
* Output array data type.
*/
Expand All @@ -68,6 +68,16 @@
dtype: Float32DataType;
}

/**
* Interface defining options when `dtype` is `'float16'`.
*/
interface Float16Options extends BaseOptions {
/**
* Output array data type.
*/
dtype: Float16DataType;
}

/**
* Interface defining options when `dtype` is `'complex128'`.
*/
Expand Down Expand Up @@ -292,6 +302,64 @@
*/
declare function scalar2ndarrayLike( x: ndarray, value: number, options: Float32Options ): float32ndarray;

/**
* Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray.
*
* @param x - input ndarray
* @param value - scalar value
* @param options - function options
* @returns zero-dimensional ndarray
*
* @example
* var getShape = require( '@stdlib/ndarray/shape' );
* var getDType = require( '@stdlib/ndarray/dtype' );
* var empty = require( '@stdlib/ndarray/empty' );
*
* var x = empty( [ 2 ], {
* 'dtype': 'float16'
* });
* // returns <ndarray>
*
* var out = scalar2ndarrayLike( x, 1.0 );
* // returns <ndarray>[ 1.0 ]
*
* var sh = getShape( out );
* // returns []
*
* var dt = String( getDType( out ) );
* // returns 'float16'
*/
declare function scalar2ndarrayLike( x: float16ndarray, value: number, options?: BaseOptions ): float16ndarray;

/**
* Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray.
*
* @param x - input ndarray
* @param value - scalar value
* @param options - function options
* @returns zero-dimensional ndarray
*
* @example
* var getShape = require( '@stdlib/ndarray/shape' );
* var getDType = require( '@stdlib/ndarray/dtype' );
* var empty = require( '@stdlib/ndarray/empty' );
*
* var x = empty( [ 2 ] );
* // returns <ndarray>
*
* var out = scalar2ndarrayLike( x, 1.0, {
* 'dtype': 'float16'
* });
* // returns <ndarray>[ 1.0 ]
*
* var sh = getShape( out );
* // returns []
*
* var dt = String( getDType( out ) );
* // returns 'float16'
*/
declare function scalar2ndarrayLike( x: ndarray, value: number, options: Float16Options ): float16ndarray;

/**
* Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray.
*
Expand Down Expand Up @@ -899,7 +967,7 @@
* var dt = String( getDType( out ) );
* // returns 'generic'
*/
declare function scalar2ndarrayLike<T = unknown>( x: genericndarray<any>, value: T, options?: BaseOptions ): genericndarray<T>;

Check warning on line 970 in lib/node_modules/@stdlib/ndarray/from-scalar-like/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

/**
* Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray.
Expand Down Expand Up @@ -930,7 +998,7 @@
*/
declare function scalar2ndarrayLike<T = unknown>( x: ndarray, value: T, options?: GenericOptions ): genericndarray<T>;


Check failure on line 1001 in lib/node_modules/@stdlib/ndarray/from-scalar-like/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

File has too many lines (1004). Maximum allowed is 1000
// EXPORTS //

export = scalar2ndarrayLike;
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import scalar2ndarrayLike = require( './index' );

scalar2ndarrayLike( x, 1.0, { 'dtype': 'float64' } ); // $ExpectType float64ndarray
scalar2ndarrayLike( x, 1.0, { 'dtype': 'float32' } ); // $ExpectType float32ndarray
scalar2ndarrayLike( x, 1.0, { 'dtype': 'float16' } ); // $ExpectType float16ndarray
scalar2ndarrayLike( x, 1.0, { 'dtype': 'complex128' } ); // $ExpectType complex128ndarray
scalar2ndarrayLike( x, 1.0, { 'dtype': 'complex64' } ); // $ExpectType complex64ndarray
scalar2ndarrayLike( x, true, { 'dtype': 'bool' } ); // $ExpectType boolndarray
Expand Down
24 changes: 24 additions & 0 deletions lib/node_modules/@stdlib/ndarray/from-scalar-like/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
var tape = require( 'tape' );
var Float64Array = require( '@stdlib/array/float64' );
var Float32Array = require( '@stdlib/array/float32' );
var Float16Array = require( '@stdlib/array/float16' );
var Int32Array = require( '@stdlib/array/int32' );
var Uint32Array = require( '@stdlib/array/uint32' );
var Int16Array = require( '@stdlib/array/int16' );
Expand Down Expand Up @@ -414,6 +415,29 @@ tape( 'the function returns a zero-dimensional ndarray (dtype=float32)', functio
t.end();
});

tape( 'the function returns a zero-dimensional ndarray (dtype=float16)', function test( t ) {
var expected;
var arr;
var x;

x = empty( [ 2 ] );

arr = scalar2ndarrayLike( x, 1.0, {
'dtype': 'float16'
});
expected = new Float16Array( [ 1.0 ] );

t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' );
t.strictEqual( String( getDType( arr ) ), 'float16', 'returns expected value' );
t.deepEqual( getShape( arr ), [], 'returns expected value' );
t.strictEqual( instanceOf( getData( arr ), Float16Array ), true, 'returns expected value' );
t.deepEqual( getData( arr ), expected, 'returns expected value' );
t.strictEqual( getOrder( arr ), 'row-major', 'returns expected value' );
t.strictEqual( numel( arr ), 1, 'returns expected value' );

t.end();
});

tape( 'the function returns a zero-dimensional ndarray (dtype=int32)', function test( t ) {
var expected;
var arr;
Expand Down
Loading