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 @@ -69,6 +69,25 @@ 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, 'float16', 'row-major' );
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 { genericndarray, float64ndarray, float32ndarray, int32ndarray, int16ndarray, int8ndarray, uint32ndarray, uint16ndarray, uint8ndarray, uint8cndarray, complex128ndarray, complex64ndarray, boolndarray, Float64DataType, Float32DataType, Complex128DataType, Complex64DataType, Int32DataType, Int16DataType, Int8DataType, Uint32DataType, Uint16DataType, Uint8DataType, Uint8cDataType, BooleanDataType, DataType, Order } from '@stdlib/types/ndarray';
import { genericndarray, float64ndarray, float32ndarray, float16ndarray, int32ndarray, int16ndarray, int8ndarray, uint32ndarray, uint16ndarray, uint8ndarray, uint8cndarray, complex128ndarray, complex64ndarray, boolndarray, Float64DataType, Float32DataType, Float16DataType, Complex128DataType, Complex64DataType, Int32DataType, Int16DataType, Int8DataType, Uint32DataType, Uint16DataType, Uint8DataType, Uint8cDataType, BooleanDataType, DataType, Order } from '@stdlib/types/ndarray';

/**
* Returns a zero-dimensional ndarray containing a provided scalar value.
Expand Down Expand Up @@ -75,6 +75,32 @@ declare function scalar2ndarray( value: number, dtype: Float64DataType, order: O
*/
declare function scalar2ndarray( value: number, dtype: Float32DataType, order: Order ): float32ndarray;

/**
* Returns a zero-dimensional ndarray containing a provided scalar value.
*
* @param value - scalar value
* @param dtype - array data type
* @param order - memory layout (row-major or column-major)
* @returns zero-dimensional ndarray
*
* @example
* var getShape = require( '@stdlib/ndarray/shape' );
* var getDType = require( '@stdlib/ndarray/dtype' );
*
* var x = scalar2ndarray( 1.0, 'float16', 'row-major' );
* // returns <ndarray>
*
* var sh = getShape( x );
* // returns []
*
* var dt = String( getDType( x ) );
* // returns 'float16'
*
* var v = x.get();
* // returns 1.0
*/
declare function scalar2ndarray( value: number, dtype: Float16DataType, order: Order ): float16ndarray;

/**
* Returns a zero-dimensional ndarray containing a provided scalar value.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import scalar2ndarray = require( './index' );
{
scalar2ndarray( 1.0, 'float64', 'row-major' ); // $ExpectType float64ndarray
scalar2ndarray( 1.0, 'float32', 'row-major' ); // $ExpectType float32ndarray
scalar2ndarray( 1.0, 'float16', 'row-major' ); // $ExpectType float16ndarray
scalar2ndarray( 1.0, 'complex128', 'row-major' ); // $ExpectType complex128ndarray
scalar2ndarray( 1.0, 'complex64', 'row-major' ); // $ExpectType complex64ndarray
scalar2ndarray( 1.0, 'int32', 'row-major' ); // $ExpectType int32ndarray
Expand Down
19 changes: 19 additions & 0 deletions lib/node_modules/@stdlib/ndarray/base/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 @@ -120,6 +121,24 @@ 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, 'float16', 'column-major' );

t.strictEqual( instanceOf( arr, base ), 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 ), 'column-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