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

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

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

bench( format( '%s:dtype=complex128', pkg ), function benchmark( b ) {
var x;
var v;
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 { float64ndarray, float32ndarray, int32ndarray, int16ndarray, int8ndarray, uint32ndarray, uint16ndarray, uint8ndarray, uint8cndarray, complex128ndarray, complex64ndarray, genericndarray, boolndarray, DataType, Order } from '@stdlib/types/ndarray';
import { float64ndarray, float32ndarray, float16ndarray, int32ndarray, int16ndarray, int8ndarray, uint32ndarray, uint16ndarray, uint8ndarray, uint8cndarray, complex128ndarray, complex64ndarray, genericndarray, boolndarray, DataType, Order } from '@stdlib/types/ndarray';

/**
* Interface defining common options.
Expand Down Expand Up @@ -68,6 +68,16 @@
dtype: 'float32';
}

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

/**
* Interface defining options when `dtype` is `'complex128'`.
*/
Expand Down Expand Up @@ -216,6 +226,30 @@
*/
declare function scalar2ndarray( value: number, options: Float32Options ): float32ndarray;

/**
* Returns a zero-dimensional ndarray containing a provided scalar value.
*
* @param value - scalar value
* @param options - options
* @returns zero-dimensional ndarray
*
* @example
* var x = scalar2ndarray( 1.0, {
* 'dtype': 'float16'
* });
* // returns <ndarray>
*
* var sh = x.shape;
* // returns []
*
* var dt = x.dtype;
* // returns 'float16'
*
* var v = x.get();
* // returns 1.0
*/
declare function scalar2ndarray( value: number, options: Float16Options ): float16ndarray;

/**
* Returns a zero-dimensional ndarray containing a provided scalar value.
*
Expand Down Expand Up @@ -504,7 +538,7 @@
* var v = x.get();
* // returns 1.0
*/
declare function scalar2ndarray<T = any>( value: T, options?: Options ): genericndarray<T>;

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

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import scalar2ndarray = require( './index' );

scalar2ndarray( 1.0, { 'dtype': 'float64' } ); // $ExpectType float64ndarray
scalar2ndarray( 1.0, { 'dtype': 'float32' } ); // $ExpectType float32ndarray
scalar2ndarray( 1.0, { 'dtype': 'float16' } ); // $ExpectType float16ndarray
scalar2ndarray( 1.0, { 'dtype': 'complex128' } ); // $ExpectType complex128ndarray
scalar2ndarray( 1.0, { 'dtype': 'complex64' } ); // $ExpectType complex64ndarray
scalar2ndarray( true, { 'dtype': 'bool' } ); // $ExpectType boolndarray
Expand Down
21 changes: 21 additions & 0 deletions lib/node_modules/@stdlib/ndarray/from-scalar/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 @@ -321,6 +322,26 @@ 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;

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

t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' );
t.strictEqual( arr.dtype, 'float16', 'returns expected value' );
t.deepEqual( arr.shape, [], 'returns expected value' );
t.strictEqual( instanceOf( arr.data, Float16Array ), true, 'returns expected value' );
t.deepEqual( arr.data, expected, 'returns expected value' );
t.strictEqual( arr.order, 'row-major', 'returns expected value' );
t.strictEqual( arr.length, 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