Skip to content

Commit 16ce439

Browse files
committed
refactor: update examples and return the input ndarray
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 508b9b9 commit 16ce439

8 files changed

Lines changed: 123 additions & 151 deletions

File tree

lib/node_modules/@stdlib/ndarray/base/fill/README.md

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -43,39 +43,21 @@ Fills an input ndarray with a specified value.
4343
<!-- eslint-disable max-len -->
4444

4545
```javascript
46-
var Float64Array = require( '@stdlib/array/float64' );
46+
var array = require( '@stdlib/ndarray/array' );
4747

48-
// Create a data buffer:
49-
var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
48+
var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] );
49+
// returns <ndarray>[ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ]
5050

51-
// Define the shape of the input array:
52-
var shape = [ 3, 1, 2 ];
51+
var out = fill( x, 10.0 );
52+
// returns <ndarray>[ [ [ 10.0, 10.0 ] ], [ [ 10.0, 10.0 ] ], [ [ 10.0, 10.0 ] ] ]
5353

54-
// Define the array strides:
55-
var sx = [ 2, 2, 1 ];
56-
57-
// Define the index offset:
58-
var ox = 0;
59-
60-
// Create the input ndarray-like object:
61-
var x = {
62-
'dtype': 'float64',
63-
'data': xbuf,
64-
'shape': shape,
65-
'strides': sx,
66-
'offset': ox,
67-
'order': 'row-major'
68-
};
69-
70-
fill( x, 10.0 );
71-
72-
console.log( x.data );
73-
// => <Float64Array>[ 10.0, 10.0, 10.0, 10.0, 10.0, 10.0 ]
54+
var bool = ( out === x );
55+
// returns true
7456
```
7557

7658
The function accepts the following arguments:
7759

78-
- **x**: array-like object containing an input ndarray.
60+
- **x**: input ndarray.
7961
- **value**: scalar value.
8062

8163
A provided ndarray should be an object with the following properties:
@@ -110,22 +92,17 @@ A provided ndarray should be an object with the following properties:
11092
<!-- eslint no-undef: "error" -->
11193

11294
```javascript
113-
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
114-
var filledarrayBy = require( '@stdlib/array/filled-by' );
115-
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
95+
var discreteUniform = require( '@stdlib/random/discrete-uniform' );
96+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
11697
var fill = require( '@stdlib/ndarray/base/fill' );
11798

118-
var x = {
119-
'dtype': 'generic',
120-
'data': filledarrayBy( 10, 'generic', discreteUniform( -100, 100 ) ),
121-
'shape': [ 5, 2 ],
122-
'strides': [ 2, 1 ],
123-
'offset': 0,
124-
'order': 'row-major'
125-
};
99+
var x = discreteUniform( [ 5, 2 ], -100, 100, {
100+
'dtype': 'generic'
101+
});
102+
console.log( ndarray2array( x ) );
126103

127104
fill( x, 10.0 );
128-
console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
105+
console.log( ndarray2array( x ) );
129106
```
130107

131108
</section>

lib/node_modules/@stdlib/ndarray/base/fill/docs/repl.txt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
Parameters
1212
----------
1313
x: ndarrayLike
14-
Input ndarray-like object.
14+
Input ndarray.
1515

1616
value: any
1717
Scalar value. Must be able to safely cast to the input ndarray data
@@ -20,15 +20,20 @@
2020
same kind (e.g., a scalar double-precision floating-point number can be
2121
used to fill a 'float32' input ndarray).
2222

23+
24+
Returns
25+
-------
26+
out: ndarrayLike
27+
Input ndarray.
28+
2329
Examples
2430
--------
2531
> var opts = { 'dtype': 'float64' };
2632
> var x = {{alias:@stdlib/ndarray/zeros}}( [ 2, 2 ], opts );
27-
> x.get( 0, 0 )
28-
0.0
29-
> {{alias}}( x, 10.0 );
30-
> x.get( 0, 0 )
31-
10.0
33+
> var out = {{alias}}( x, 10.0 )
34+
<ndarray>[ [ 10.0, 10.0 ], [ 10.0, 10.0 ] ]
35+
> var bool = ( out === x )
36+
true
3237

3338
See Also
3439
--------

lib/node_modules/@stdlib/ndarray/base/fill/docs/types/index.d.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
import { typedndarray, complexndarray, genericndarray } from '@stdlib/types/ndarray';
2424
import { ComplexLike } from '@stdlib/types/complex';
2525

26+
/**
27+
* Input ndarray.
28+
*/
29+
type InputArray<T> = typedndarray<T> | genericndarray<T>;
30+
2631
/**
2732
* Fills an input ndarray with a specified value.
2833
*
@@ -33,6 +38,7 @@ import { ComplexLike } from '@stdlib/types/complex';
3338
*
3439
* @param x - input ndarray
3540
* @param value - scalar value
41+
* @returns input ndarray
3642
*
3743
* @example
3844
* var Complex128Array = require( '@stdlib/array/complex128' );
@@ -51,20 +57,23 @@ import { ComplexLike } from '@stdlib/types/complex';
5157
*
5258
* // Create the input ndarray-like object:
5359
* var x = {
54-
* 'dtype': 'float64',
60+
* 'dtype': 'complex128',
5561
* 'data': xbuf,
5662
* 'shape': shape,
5763
* 'strides': sx,
5864
* 'offset': ox,
5965
* 'order': 'row-major'
6066
* };
6167
*
62-
* fill( x, 10.0 );
68+
* var out = fill( x, 10.0 );
6369
*
6470
* console.log( x.data );
6571
* // => <Complex128Array>[ 10.0, 0.0, 10.0, 0.0, 10.0, 0.0, 10.0, 0.0, 10.0, 0.0, 10.0, 0.0 ]
72+
*
73+
* var bool = ( out === x );
74+
* // returns true
6675
*/
67-
declare function fill( x: complexndarray, value: number | ComplexLike ): void;
76+
declare function fill<T extends complexndarray = complexndarray>( x: T, value: number | ComplexLike ): T;
6877

6978
/**
7079
* Fills an input ndarray with a specified value.
@@ -75,6 +84,7 @@ declare function fill( x: complexndarray, value: number | ComplexLike ): void;
7584
*
7685
* @param x - input ndarray
7786
* @param value - scalar value
87+
* @returns input ndarray
7888
*
7989
* @example
8090
* var Float64Array = require( '@stdlib/array/float64' );
@@ -101,12 +111,15 @@ declare function fill( x: complexndarray, value: number | ComplexLike ): void;
101111
* 'order': 'row-major'
102112
* };
103113
*
104-
* fill( x, 10.0 );
114+
* var out = fill( x, 10.0 );
105115
*
106116
* console.log( x.data );
107117
* // => <Float64Array>[ 10.0, 10.0, 10.0, 10.0, 10.0, 10.0 ]
118+
*
119+
* var bool = ( out === x );
120+
* // returns true
108121
*/
109-
declare function fill<T = unknown>( x: typedndarray<T> | genericndarray<T>, value: T ): void;
122+
declare function fill<T = unknown, U extends InputArray<T> = InputArray<T>>( x: U, value: T ): U;
110123

111124

112125
// EXPORTS //

lib/node_modules/@stdlib/ndarray/base/fill/docs/types/test.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@ import fill = require( './index' );
2222

2323
// TESTS //
2424

25-
// The function returns `undefined`...
25+
// The function returns an ndarray...
2626
{
27-
fill( zeros( 'float64', [ 2, 2 ], 'row-major' ), 10.0 ); // $ExpectType void
28-
fill( zeros( 'complex128', [ 2, 2 ], 'row-major' ), 10.0 ); // $ExpectType void
29-
fill( zeros( 'complex128', [ 2, 2 ], 'row-major' ), { 're': 10.0, 'im': 10.0 } ); // $ExpectType void
27+
fill( zeros( 'float64', [ 2, 2 ], 'row-major' ), 10.0 ); // $ExpectType float64ndarray
28+
fill( zeros( 'complex128', [ 2, 2 ], 'row-major' ), 10.0 ); // $ExpectType complex128ndarray
29+
fill( zeros( 'complex128', [ 2, 2 ], 'row-major' ), { 're': 10.0, 'im': 10.0 } ); // $ExpectType complex128ndarray
3030

31-
fill( zeros( 'generic', [ 2, 2 ], 'row-major' ), 10.0 ); // $ExpectType void
32-
fill<number | string>( zeros( 'generic', [ 2, 2 ], 'row-major' ), 'beep' ); // $ExpectType void
31+
fill( zeros( 'generic', [ 2, 2 ], 'row-major' ), 10.0 ); // $ExpectType genericndarray<number>
3332
}
3433

3534
// The compiler throws an error if the function is provided a first argument which is not an ndarray-like object...

lib/node_modules/@stdlib/ndarray/base/fill/examples/index.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,14 @@
1818

1919
'use strict';
2020

21-
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
22-
var filledarrayBy = require( '@stdlib/array/filled-by' );
23-
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
21+
var discreteUniform = require( '@stdlib/random/discrete-uniform' );
22+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
2423
var fill = require( './../lib' );
2524

26-
var x = {
27-
'dtype': 'generic',
28-
'data': filledarrayBy( 10, 'generic', discreteUniform( -100, 100 ) ),
29-
'shape': [ 5, 2 ],
30-
'strides': [ 2, 1 ],
31-
'offset': 0,
32-
'order': 'row-major'
33-
};
25+
var x = discreteUniform( [ 5, 2 ], -100, 100, {
26+
'dtype': 'generic'
27+
});
28+
console.log( ndarray2array( x ) );
3429

3530
fill( x, 10.0 );
36-
console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
31+
console.log( ndarray2array( x ) );

lib/node_modules/@stdlib/ndarray/base/fill/lib/index.js

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,35 +24,17 @@
2424
* @module @stdlib/ndarray/base/fill
2525
*
2626
* @example
27-
* var Float64Array = require( '@stdlib/array/float64' );
27+
* var array = require( '@stdlib/ndarray/array' );
2828
* var fill = require( '@stdlib/ndarray/base/fill' );
2929
*
30-
* // Create a data buffer:
31-
* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
30+
* var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] );
31+
* // returns <ndarray>[ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ]
3232
*
33-
* // Define the shape of the input array:
34-
* var shape = [ 3, 1, 2 ];
33+
* var out = fill( x, 10.0 );
34+
* // returns <ndarray>[ [ [ 10.0, 10.0 ] ], [ [ 10.0, 10.0 ] ], [ [ 10.0, 10.0 ] ] ]
3535
*
36-
* // Define the array strides:
37-
* var sx = [ 2, 2, 1 ];
38-
*
39-
* // Define the index offset:
40-
* var ox = 0;
41-
*
42-
* // Create the input ndarray-like object:
43-
* var x = {
44-
* 'dtype': 'float64',
45-
* 'data': xbuf,
46-
* 'shape': shape,
47-
* 'strides': sx,
48-
* 'offset': ox,
49-
* 'order': 'row-major'
50-
* };
51-
*
52-
* fill( x, 10.0 );
53-
*
54-
* console.log( x.data );
55-
* // => <Float64Array>[ 10.0, 10.0, 10.0, 10.0, 10.0, 10.0 ]
36+
* var bool = ( out === x );
37+
* // returns true
5638
*/
5739

5840
// MODULES //

lib/node_modules/@stdlib/ndarray/base/fill/lib/main.js

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
var isScalarMostlySafeCompatible = require( '@stdlib/ndarray/base/assert/is-scalar-mostly-safe-compatible' ); // eslint-disable-line id-length
2424
var broadcastScalar = require( '@stdlib/ndarray/base/broadcast-scalar' );
25-
var getDtype = require( '@stdlib/ndarray/base/dtype' );
25+
var getDType = require( '@stdlib/ndarray/base/dtype' );
2626
var getShape = require( '@stdlib/ndarray/base/shape' );
2727
var getOrder = require( '@stdlib/ndarray/base/order' );
2828
var assign = require( '@stdlib/ndarray/base/assign' );
@@ -34,52 +34,28 @@ var format = require( '@stdlib/string/format' );
3434
/**
3535
* Fills an input ndarray with a specified value.
3636
*
37-
* @param {ndarrayLike} x - ndarray-like object
38-
* @param {string} x.dtype - data type
39-
* @param {Collection} x.data - data buffer
40-
* @param {NonNegativeIntegerArray} x.shape - dimensions
41-
* @param {IntegerArray} x.strides - stride lengths
42-
* @param {NonNegativeInteger} x.offset - index offset
43-
* @param {string} x.order - specifies whether `x` is row-major (C-style) or column-major (Fortran-style)
37+
* @param {ndarrayLike} x - input ndarray
4438
* @param {*} value - scalar value
4539
* @throws {TypeError} second argument cannot be safely cast to the input array data type
46-
* @returns {void}
40+
* @returns {ndarrayLike} input ndarray
4741
*
4842
* @example
49-
* var Float64Array = require( '@stdlib/array/float64' );
43+
* var array = require( '@stdlib/ndarray/array' );
5044
*
51-
* // Create a data buffer:
52-
* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
45+
* var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] );
46+
* // returns <ndarray>[ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ]
5347
*
54-
* // Define the shape of the input array:
55-
* var shape = [ 3, 1, 2 ];
48+
* var out = fill( x, 10.0 );
49+
* // returns <ndarray>[ [ [ 10.0, 10.0 ] ], [ [ 10.0, 10.0 ] ], [ [ 10.0, 10.0 ] ] ]
5650
*
57-
* // Define the array strides:
58-
* var sx = [ 2, 2, 1 ];
59-
*
60-
* // Define the index offset:
61-
* var ox = 0;
62-
*
63-
* // Create the input ndarray-like object:
64-
* var x = {
65-
* 'dtype': 'float64',
66-
* 'data': xbuf,
67-
* 'shape': shape,
68-
* 'strides': sx,
69-
* 'offset': ox,
70-
* 'order': 'row-major'
71-
* };
72-
*
73-
* fill( x, 10.0 );
74-
*
75-
* console.log( x.data );
76-
* // => <Float64Array>[ 10.0, 10.0, 10.0, 10.0, 10.0, 10.0 ]
51+
* var bool = ( out === x );
52+
* // returns true
7753
*/
7854
function fill( x, value ) {
7955
var dt;
8056
var v;
8157

82-
dt = getDtype( x );
58+
dt = getDType( x );
8359

8460
// Safe casts are always allowed and allow same kind casts (i.e., downcasts) only when the output data type is floating-point...
8561
if ( !isScalarMostlySafeCompatible( value, dt ) ) {
@@ -90,6 +66,8 @@ function fill( x, value ) {
9066

9167
// Assign the fill value to each element of the input ndarray:
9268
assign( [ v, x ] ); // TODO: consider replacing with ndarray/base/assign-scalar in order to avoid zero-dimensional ndarray creation and subsequent broadcasting
69+
70+
return x;
9371
}
9472

9573

0 commit comments

Comments
 (0)