Skip to content

Commit 3aa1209

Browse files
committed
fix: apply suggestions from code review
--- 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: na - 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 86e54db commit 3aa1209

6 files changed

Lines changed: 77 additions & 198 deletions

File tree

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,8 @@ var y = tile( x, [ 2, 2 ] );
6464

6565
## Notes
6666

67-
- When the number of repetitions is less than the number of input dimensions, the number of repetitions is left-padded with ones until the number of repetitions matches the number of input dimensions. When the number of repetitions exceeds the number of input dimensions, the input [ndarray][@stdlib/ndarray/base/ctor] is treated as if singleton dimensions were prepended.
68-
- The output [ndarray][@stdlib/ndarray/base/ctor] has rank equal to the greater of the input array rank and the number of repetitions.
69-
- The function always returns a new [ndarray][@stdlib/ndarray/base/ctor] having a newly allocated data buffer.
67+
- The number of repetitions must have at least as many elements as the number of input dimensions. When the number of repetitions exceeds the number of input dimensions, the input [ndarray][@stdlib/ndarray/base/ctor] is treated as if singleton dimensions were prepended.
68+
- The function always copies data to a new [ndarray][@stdlib/ndarray/base/ctor].
7069

7170
</section>
7271

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,12 @@
33
Returns an ndarray created by repeating the elements of an input ndarray
44
a specified number of times along each dimension.
55

6-
When the number of repetitions is less than the number of input dimensions,
7-
the number of repetitions is left-padded with ones until the number of
8-
repetitions matches the number of input dimensions. When the number of
9-
repetitions exceeds the number of input dimensions, the input array is
10-
treated as if singleton dimensions were prepended.
6+
The number of repetitions must have at least as many elements as the number
7+
of input dimensions. When the number of repetitions exceeds the number of
8+
input dimensions, the input array is treated as if singleton dimensions
9+
were prepended.
1110

12-
The output array has rank equal to the greater of the input array rank and
13-
the number of repetitions.
14-
15-
The function always returns a new ndarray having a newly allocated data
16-
buffer.
11+
The function always copies data to a new ndarray.
1712

1813
Parameters
1914
----------

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ import { ndarray } from '@stdlib/types/ndarray';
2828
*
2929
* ## Notes
3030
*
31-
* - When the number of repetitions is less than the number of input dimensions, the number of repetitions is left-padded with ones until the number of repetitions matches the number of input dimensions. When the number of repetitions exceeds the number of input dimensions, the input array is treated as if singleton dimensions were prepended.
32-
* - The output array has rank equal to the greater of the input array rank and the number of repetitions.
33-
* - The function always returns a new ndarray having a newly allocated data buffer.
31+
* - The number of repetitions must have at least as many elements as the number of input dimensions. When the number of repetitions exceeds the number of input dimensions, the input array is treated as if singleton dimensions were prepended.
32+
* - The function always copies data to a new ndarray.
3433
*
3534
* @param x - input array
3635
* @param reps - number of repetitions along each dimension
@@ -45,7 +44,7 @@ import { ndarray } from '@stdlib/types/ndarray';
4544
* var y = tile( x, [ 2, 2 ] );
4645
* // returns <ndarray>[ [ 1, 2, 1, 2 ], [ 3, 4, 3, 4 ], [ 1, 2, 1, 2 ], [ 3, 4, 3, 4 ] ]
4746
*/
48-
declare function tile( x: ndarray, reps: ArrayLike<number> ): ndarray;
47+
declare function tile<T extends ndarray>( x: T, reps: ArrayLike<number> ): T;
4948

5049

5150
// EXPORTS //

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

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,46 +16,30 @@
1616
* limitations under the License.
1717
*/
1818

19+
/* eslint-disable space-in-parens */
20+
1921
/// <reference types="@stdlib/types"/>
2022

21-
import { ndarray } from '@stdlib/types/ndarray';
23+
import zeros = require( '@stdlib/ndarray/zeros' );
2224
import tile = require( './index' );
2325

24-
/**
25-
* Mock function to create an ndarray-like object.
26-
*
27-
* @returns ndarray-like object
28-
*/
29-
function array(): ndarray {
30-
const obj: ndarray = {
31-
'byteLength': 80,
32-
'BYTES_PER_ELEMENT': 8,
33-
'data': new Float64Array( 10 ),
34-
'dtype': 'float64',
35-
'flags': {
36-
'ROW_MAJOR_CONTIGUOUS': true,
37-
'COLUMN_MAJOR_CONTIGUOUS': false
38-
},
39-
'length': 10,
40-
'ndims': 1,
41-
'offset': 0,
42-
'order': 'row-major',
43-
'shape': [ 10 ],
44-
'strides': [ 1 ],
45-
'get': (): number => 0,
46-
'set': (): ndarray => obj
47-
};
48-
return obj;
49-
}
50-
5126

5227
// TESTS //
5328

54-
// The function returns an ndarray...
29+
// The function returns an ndarray having the same type as the input ndarray...
5530
{
56-
const x = array();
57-
58-
tile( x, [ 2, 2, 2 ] ); // $ExpectType ndarray
31+
tile( zeros( [ 2, 2 ], { 'dtype': 'float64' } ), [ 2, 2 ] ); // $ExpectType float64ndarray
32+
tile( zeros( [ 2, 2 ], { 'dtype': 'float32' } ), [ 2, 2 ] ); // $ExpectType float32ndarray
33+
tile( zeros( [ 2, 2 ], { 'dtype': 'complex128' } ), [ 2, 2 ] ); // $ExpectType complex128ndarray
34+
tile( zeros( [ 2, 2 ], { 'dtype': 'complex64' } ), [ 2, 2 ] ); // $ExpectType complex64ndarray
35+
tile( zeros( [ 2, 2 ], { 'dtype': 'int32' } ), [ 2, 2 ] ); // $ExpectType int32ndarray
36+
tile( zeros( [ 2, 2 ], { 'dtype': 'int16' } ), [ 2, 2 ] ); // $ExpectType int16ndarray
37+
tile( zeros( [ 2, 2 ], { 'dtype': 'int8' } ), [ 2, 2 ] ); // $ExpectType int8ndarray
38+
tile( zeros( [ 2, 2 ], { 'dtype': 'uint32' } ), [ 2, 2 ] ); // $ExpectType uint32ndarray
39+
tile( zeros( [ 2, 2 ], { 'dtype': 'uint16' } ), [ 2, 2 ] ); // $ExpectType uint16ndarray
40+
tile( zeros( [ 2, 2 ], { 'dtype': 'uint8' } ), [ 2, 2 ] ); // $ExpectType uint8ndarray
41+
tile( zeros( [ 2, 2 ], { 'dtype': 'uint8c' } ), [ 2, 2 ] ); // $ExpectType uint8cndarray
42+
tile( zeros( [ 2, 2 ], { 'dtype': 'generic' } ), [ 2, 2 ] ); // $ExpectType genericndarray<number>
5943
}
6044

6145
// The compiler throws an error if the function is not provided a first argument which is an ndarray...
@@ -72,7 +56,9 @@ function array(): ndarray {
7256

7357
// The compiler throws an error if the function is not provided a second argument which is an array-like object containing numbers...
7458
{
75-
const x = array();
59+
const x = zeros( [ 2, 2 ], {
60+
'dtype': 'float64'
61+
});
7662

7763
tile( x, '5' ); // $ExpectError
7864
tile( x, 5 ); // $ExpectError
@@ -86,7 +72,9 @@ function array(): ndarray {
8672

8773
// The compiler throws an error if the function is provided an unsupported number of arguments...
8874
{
89-
const x = array();
75+
const x = zeros( [ 2, 2 ], {
76+
'dtype': 'float64'
77+
});
9078

9179
tile(); // $ExpectError
9280
tile( x ); // $ExpectError

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

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ var getOffset = require( '@stdlib/ndarray/base/offset' );
3232
var getOrder = require( '@stdlib/ndarray/base/order' );
3333
var getDType = require( '@stdlib/ndarray/base/dtype' );
3434
var getData = require( '@stdlib/ndarray/base/data-buffer' );
35+
var format = require( '@stdlib/string/format' );
3536
var zeros = require( '@stdlib/array/base/zeros' );
3637

3738

@@ -42,12 +43,12 @@ var zeros = require( '@stdlib/array/base/zeros' );
4243
*
4344
* ## Notes
4445
*
45-
* - When the number of repetitions is less than the number of input dimensions, the number of repetitions is left-padded with ones until the number of repetitions matches the number of input dimensions. When the number of repetitions exceeds the number of input dimensions, the input array is treated as if singleton dimensions were prepended.
46-
* - The output array has rank equal to the greater of the input array rank and the number of repetitions.
47-
* - The function always returns a new ndarray having a newly allocated data buffer.
46+
* - The number of repetitions must have at least as many elements as the number of input dimensions. When the number of repetitions exceeds the number of input dimensions, the input array is treated as if singleton dimensions were prepended.
47+
* - The function always copies data to a new ndarray.
4848
*
4949
* @param {ndarray} x - input array
5050
* @param {NonNegativeIntegerArray} reps - number of repetitions along each dimension
51+
* @throws {RangeError} second argument must have at least as many elements as the number of input dimensions
5152
* @returns {ndarray} output array
5253
*
5354
* @example
@@ -66,64 +67,63 @@ function tile( x, reps ) {
6667
var sho;
6768
var shi;
6869
var sto;
69-
var ord;
7070
var buf;
7171
var out;
72-
var Dr;
7372
var Ds;
7473
var si;
7574
var sx;
7675
var v1;
7776
var v2;
78-
var K;
7977
var L;
8078
var M;
8179
var N;
82-
var r;
80+
var o;
8381
var s;
8482
var t;
8583
var i;
84+
var j;
8685

8786
shx = getShape( x, false );
8887
sx = getStrides( x, false );
8988
dtype = getDType( x );
9089
N = reps.length;
9190
M = shx.length;
9291

93-
// Determine the output rank, which is the greater of the number of input dimensions and the number of repetitions. Compute the implicit left-padding amounts for the repetitions and input shape, respectively...
94-
K = ( N > M ) ? N : M;
95-
Dr = K - N;
96-
Ds = K - M;
92+
if ( N < M ) {
93+
throw new RangeError( format( 'invalid argument. Second argument must have at least as many elements as the number of dimensions of the first argument. Number of input dimensions: `%u`. Number of repetitions: `%u`.', M, N ) );
94+
}
95+
// Compute the implicit left-padding amount for the input shape:
96+
Ds = N - M;
9797

98-
// Infer the storage order from the input strides, falling back to the stated order when the input is non-contiguous...
99-
ord = strides2order( sx );
100-
if ( ord === 2 ) {
101-
order = 'column-major';
102-
} else if ( ord === 1 || ord === 3 ) {
98+
o = strides2order( sx );
99+
if ( o === 0 || o === 3 ) {
100+
// Fallback to stated layout when unable to infer the underlying physical layout:
101+
order = getOrder( x );
102+
} else if ( o === 1 ) {
103103
order = 'row-major';
104104
} else {
105-
order = getOrder( x );
105+
order = 'column-major';
106106
}
107-
// Compute an interleaved 2K-D shape and strides for broadcasting the input, along with the final K-D output shape. By inserting a repetition axis next to each original axis, a contiguous 2K-D buffer can be reinterpreted as the K-D tiled output without an additional copy...
108-
shi = zeros( 2*K );
109-
si = zeros( 2*K );
110-
sho = zeros( K );
111-
for ( i = 0; i < K; i++ ) {
112-
r = ( i < Dr ) ? 1 : reps[ i - Dr ];
113-
s = ( i < Ds ) ? 1 : shx[ i - Ds ];
114-
t = ( i < Ds ) ? 0 : sx[ i - Ds ];
115-
sho[ i ] = r * s;
107+
// Compute an interleaved 2N-D shape and strides for broadcasting the input, along with the final N-D output shape. By inserting a repetition axis next to each original axis, a contiguous 2N-D buffer can be reinterpreted as the N-D tiled output without an additional copy...
108+
shi = zeros( 2*N );
109+
si = zeros( 2*N );
110+
sho = zeros( N );
111+
for ( i = 0; i < N; i++ ) {
112+
j = i - Ds;
113+
s = ( j < 0 ) ? 1 : shx[ j ];
114+
t = ( j < 0 ) ? 0 : sx[ j ];
115+
sho[ i ] = reps[ i ] * s;
116116
if ( order === 'row-major' ) {
117-
shi[ 2*i ] = r;
117+
shi[ 2*i ] = reps[ i ];
118118
shi[ (2*i)+1 ] = s;
119119
si[ (2*i)+1 ] = t;
120120
} else {
121121
shi[ 2*i ] = s;
122-
shi[ (2*i)+1 ] = r;
122+
shi[ (2*i)+1 ] = reps[ i ];
123123
si[ 2*i ] = t;
124124
}
125125
}
126-
if ( K > 0 ) {
126+
if ( N > 0 ) {
127127
L = numel( sho );
128128
sto = shape2strides( sho, order );
129129
} else {
@@ -133,7 +133,7 @@ function tile( x, reps ) {
133133
buf = buffer( dtype, L );
134134
out = new x.constructor( dtype, buf, sho, sto, 0, order );
135135

136-
// Assign the broadcasted input to a 2K-D view over the output data buffer...
136+
// Assign the broadcasted input to a 2N-D view over the output data buffer...
137137
v1 = ndarray( dtype, getData( x ), shi, si, getOffset( x ), order );
138138
v2 = ndarray( dtype, buf, shi, shape2strides( shi, order ), 0, order );
139139
assign( [ v1, v2 ] );

0 commit comments

Comments
 (0)