Skip to content

Commit 85df83e

Browse files
committed
fix: apply suggestions from code review
1 parent ecb64b1 commit 85df83e

6 files changed

Lines changed: 156 additions & 36 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ var y = diagonal( x, [ 0, 1 ], 0, false );
8585
The function accepts the following arguments:
8686

8787
- **x**: input ndarray.
88-
- **dims**: dimension indices defining the plane in which to extract the diagonal.
88+
- **dims**: dimension indices defining the plane from which to extract the diagonal.
8989
- **k**: diagonal offset.
9090
- **writable**: boolean indicating whether the returned ndarray should be writable.
9191

lib/node_modules/@stdlib/ndarray/base/diagonal/benchmark/benchmark.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var diagonal = require( './../lib' );
3131

3232
// MAIN //
3333

34-
bench( format( '%s::2d,base', pkg ), function benchmark( b ) {
34+
bench( format( '%s::ndims=2,ctor=base', pkg ), function benchmark( b ) {
3535
var values;
3636
var v;
3737
var i;
@@ -59,7 +59,7 @@ bench( format( '%s::2d,base', pkg ), function benchmark( b ) {
5959
b.end();
6060
});
6161

62-
bench( format( '%s::2d,non-base', pkg ), function benchmark( b ) {
62+
bench( format( '%s::ndims=2,ctor=non-base', pkg ), function benchmark( b ) {
6363
var values;
6464
var v;
6565
var i;
@@ -91,7 +91,7 @@ bench( format( '%s::2d,non-base', pkg ), function benchmark( b ) {
9191
b.end();
9292
});
9393

94-
bench( format( '%s::3d,base', pkg ), function benchmark( b ) {
94+
bench( format( '%s::ndims=3,ctor=base', pkg ), function benchmark( b ) {
9595
var values;
9696
var v;
9797
var i;
@@ -119,7 +119,7 @@ bench( format( '%s::3d,base', pkg ), function benchmark( b ) {
119119
b.end();
120120
});
121121

122-
bench( format( '%s::3d,non-base', pkg ), function benchmark( b ) {
122+
bench( format( '%s::ndims=3,ctor=non-base', pkg ), function benchmark( b ) {
123123
var values;
124124
var v;
125125
var i;
@@ -151,7 +151,7 @@ bench( format( '%s::3d,non-base', pkg ), function benchmark( b ) {
151151
b.end();
152152
});
153153

154-
bench( format( '%s::4d,base', pkg ), function benchmark( b ) {
154+
bench( format( '%s::ndims=4,ctor=base', pkg ), function benchmark( b ) {
155155
var values;
156156
var v;
157157
var i;
@@ -179,7 +179,7 @@ bench( format( '%s::4d,base', pkg ), function benchmark( b ) {
179179
b.end();
180180
});
181181

182-
bench( format( '%s::4d,non-base', pkg ), function benchmark( b ) {
182+
bench( format( '%s::ndims=4,ctor=non-base', pkg ), function benchmark( b ) {
183183
var values;
184184
var v;
185185
var i;
@@ -211,7 +211,7 @@ bench( format( '%s::4d,non-base', pkg ), function benchmark( b ) {
211211
b.end();
212212
});
213213

214-
bench( format( '%s::5d,base', pkg ), function benchmark( b ) {
214+
bench( format( '%s::ndims=5,ctor=base', pkg ), function benchmark( b ) {
215215
var values;
216216
var v;
217217
var i;
@@ -239,7 +239,7 @@ bench( format( '%s::5d,base', pkg ), function benchmark( b ) {
239239
b.end();
240240
});
241241

242-
bench( format( '%s::5d,non-base', pkg ), function benchmark( b ) {
242+
bench( format( '%s::ndims=5,ctor=non-base', pkg ), function benchmark( b ) {
243243
var values;
244244
var v;
245245
var i;

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
element specifies the row-like dimension. The second element specifies
77
the column-like dimension.
88

9-
Each provided dimension index must reside on the interval [-ndims,ndims-1].
9+
Each provided dimension index must reside on the interval
10+
[-ndims, ndims-1].
1011

1112
The diagonal offset `k` is interpreted as `column - row`. Accordingly,
1213
when `k = 0`, the function returns the main diagonal; when `k > 0`, the
@@ -26,7 +27,7 @@
2627
Input array.
2728

2829
dims: ArrayLikeObject<integer>
29-
Dimension indices defining the plane in which to extract the diagonal.
30+
Dimension indices defining the plane from which to extract the diagonal.
3031

3132
k: integer
3233
Diagonal offset.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import { typedndarray } from '@stdlib/types/ndarray';
3535
* - The `writable` parameter **only** applies to ndarray constructors supporting **read-only** instances.
3636
*
3737
* @param x - input array
38-
* @param dims - dimension indices defining the plane in which to extract the diagonal
38+
* @param dims - dimension indices defining the plane from which to extract the diagonal
3939
* @param k - diagonal offset
4040
* @param writable - boolean indicating whether the returned ndarray should be writable
4141
* @returns ndarray view

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

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@
2020

2121
// MODULES //
2222

23-
var toNormalizedIndices = require( '@stdlib/ndarray/base/to-normalized-indices' );
24-
var getDType = require( '@stdlib/ndarray/dtype' );
25-
var getShape = require( '@stdlib/ndarray/shape' );
26-
var getStrides = require( '@stdlib/ndarray/strides' );
27-
var getOffset = require( '@stdlib/ndarray/offset' );
28-
var getOrder = require( '@stdlib/ndarray/order' );
29-
var getData = require( '@stdlib/ndarray/data-buffer' );
23+
var toUniqueNormalizedIndices = require( '@stdlib/ndarray/base/to-unique-normalized-indices' );
24+
var getDType = require( '@stdlib/ndarray/base/dtype' );
25+
var getShape = require( '@stdlib/ndarray/base/shape' );
26+
var getStrides = require( '@stdlib/ndarray/base/strides' );
27+
var getOffset = require( '@stdlib/ndarray/base/offset' );
28+
var getOrder = require( '@stdlib/ndarray/base/order' );
29+
var getData = require( '@stdlib/ndarray/base/data-buffer' );
30+
var join = require( '@stdlib/array/base/join' );
31+
var min = require( '@stdlib/math/base/special/min' );
32+
var format = require( '@stdlib/string/format' );
3033

3134

3235
// MAIN //
@@ -43,9 +46,13 @@ var getData = require( '@stdlib/ndarray/data-buffer' );
4346
* - The `writable` parameter **only** applies to ndarray constructors supporting **read-only** instances.
4447
*
4548
* @param {ndarray} x - input array
46-
* @param {IntegerArray} dims - dimension indices defining the plane in which to extract the diagonal
49+
* @param {IntegerArray} dims - dimension indices defining the plane from which to extract the diagonal
4750
* @param {integer} k - diagonal offset
4851
* @param {boolean} writable - boolean indicating whether the returned ndarray should be writable
52+
* @throws {RangeError} must provide exactly two dimension indices
53+
* @throws {RangeError} input ndarray must have at least two dimensions
54+
* @throws {RangeError} must provide valid dimension indices
55+
* @throws {Error} must provide unique dimension indices
4956
* @returns {ndarray} ndarray view
5057
*
5158
* @example
@@ -60,47 +67,59 @@ var getData = require( '@stdlib/ndarray/data-buffer' );
6067
function diagonal( x, dims, k, writable ) {
6168
var strides;
6269
var offset;
70+
var ndims;
6371
var shape;
72+
var rows;
73+
var cols;
6474
var sh;
6575
var st;
6676
var sr;
6777
var sc;
68-
var kp;
69-
var kn;
78+
var ro;
79+
var co;
7080
var d;
71-
var N;
7281
var L;
7382
var i;
7483

84+
if ( dims.length !== 2 ) {
85+
throw new RangeError( format( 'invalid argument. Must provide exactly two dimension indices. Value: `[%s]`.', join( dims, ', ' ) ) );
86+
}
7587
sh = getShape( x );
88+
ndims = sh.length;
89+
if ( ndims < 2 ) {
90+
throw new RangeError( format( 'invalid argument. Input ndarray must have at least two dimensions. Number of dimensions: %d.', ndims ) );
91+
}
7692
st = getStrides( x );
77-
N = sh.length;
78-
79-
// Normalize the dimension indices:
80-
d = toNormalizedIndices( dims, N-1 );
8193

94+
// Resolve dimension indices...
95+
d = toUniqueNormalizedIndices( dims, ndims-1 );
96+
if ( d === null ) {
97+
throw new RangeError( format( 'invalid argument. Specified dimension index is out-of-bounds. Must be on the interval: [-%u, %u]. Value: `[%s]`.', ndims, ndims-1, join( dims, ', ' ) ) );
98+
}
99+
if ( d.length !== dims.length ) {
100+
throw new Error( format( 'invalid argument. Must provide unique dimension indices. Value: `[%s]`.', join( dims, ', ' ) ) );
101+
}
82102
sr = st[ d[0] ];
83103
sc = st[ d[1] ];
84104

85-
// Resolve the positive and negative parts of `k`:
86-
kp = ( k > 0 ) ? k : 0;
87-
kn = kp - k;
105+
// Resolve the row and column offsets corresponding to the diagonal offset `k = column - row`:
106+
co = ( k > 0 ) ? k : 0;
107+
ro = co - k;
88108

89109
// Compute the length of the diagonal (clamped to be non-negative):
90-
L = sh[ d[0] ] - kn;
91-
if ( sh[ d[1] ] - kp < L ) {
92-
L = sh[ d[1] ] - kp;
93-
}
110+
rows = sh[ d[0] ] - ro;
111+
cols = sh[ d[1] ] - co;
112+
L = min( rows, cols );
94113
if ( L < 0 ) {
95114
L = 0;
96115
}
97116
// Adjust the offset so that we point to the first element along the specified diagonal:
98-
offset = getOffset( x ) + ( kp*sc ) + ( kn*sr );
117+
offset = getOffset( x ) + ( co*sc ) + ( ro*sr );
99118

100119
// Drop the specified dimensions and append the diagonal dimension:
101120
shape = [];
102121
strides = [];
103-
for ( i = 0; i < N; i++ ) {
122+
for ( i = 0; i < ndims; i++ ) {
104123
if ( i === d[0] || i === d[1] ) {
105124
continue;
106125
}

lib/node_modules/@stdlib/ndarray/base/diagonal/test/test.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
// MODULES //
2222

2323
var tape = require( 'tape' );
24+
var Float64Array = require( '@stdlib/array/float64' );
2425
var array = require( '@stdlib/ndarray/array' );
2526
var ndarray = require( '@stdlib/ndarray/base/ctor' );
2627
var ndarray2array = require( '@stdlib/ndarray/to-array' );
@@ -61,6 +62,105 @@ tape( 'the function returns a view of the main diagonal of a square matrix', fun
6162
t.end();
6263
});
6364

65+
tape( 'the function throws an error if not provided exactly two dimension indices', function test( t ) {
66+
var values;
67+
var x;
68+
var i;
69+
70+
x = new ndarray( 'float64', new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' );
71+
72+
values = [
73+
[],
74+
[ 0 ],
75+
[ 0, 1, 0 ],
76+
[ 0, 1, 0, 1 ]
77+
];
78+
for ( i = 0; i < values.length; i++ ) {
79+
t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ].length + ' dimension indices' );
80+
}
81+
t.end();
82+
83+
function badValue( dims ) {
84+
return function badValue() {
85+
diagonal( x, dims, 0, false );
86+
};
87+
}
88+
});
89+
90+
tape( 'the function throws an error if provided an input ndarray with fewer than two dimensions', function test( t ) {
91+
var values;
92+
var i;
93+
94+
values = [
95+
new ndarray( 'float64', new Float64Array( [ 5.0 ] ), [], [ 0 ], 0, 'row-major' ),
96+
new ndarray( 'float64', new Float64Array( [ 1.0, 2.0, 3.0 ] ), [ 3 ], [ 1 ], 0, 'row-major' )
97+
];
98+
for ( i = 0; i < values.length; i++ ) {
99+
t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided an input ndarray with '+values[ i ].ndims+' dimensions' );
100+
}
101+
t.end();
102+
103+
function badValue( x ) {
104+
return function badValue() {
105+
diagonal( x, [ 0, 1 ], 0, false );
106+
};
107+
}
108+
});
109+
110+
tape( 'the function throws an error if provided out-of-bounds dimension indices', function test( t ) {
111+
var values;
112+
var x;
113+
var i;
114+
115+
x = new ndarray( 'float64', new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' );
116+
117+
values = [
118+
[ 0, 2 ],
119+
[ 2, 0 ],
120+
[ -3, 0 ],
121+
[ 0, -3 ],
122+
[ 10, 0 ],
123+
[ 0, 10 ]
124+
];
125+
for ( i = 0; i < values.length; i++ ) {
126+
t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided dimension indices ['+values[ i ]+']' );
127+
}
128+
t.end();
129+
130+
function badValue( dims ) {
131+
return function badValue() {
132+
diagonal( x, dims, 0, false );
133+
};
134+
}
135+
});
136+
137+
tape( 'the function throws an error if provided duplicate dimension indices', function test( t ) {
138+
var values;
139+
var x;
140+
var i;
141+
142+
x = new ndarray( 'float64', new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ), [ 2, 2, 2 ], [ 4, 2, 1 ], 0, 'row-major' );
143+
144+
values = [
145+
[ 0, 0 ],
146+
[ 1, 1 ],
147+
[ 2, 2 ],
148+
[ 0, -3 ],
149+
[ -2, 1 ],
150+
[ -1, 2 ]
151+
];
152+
for ( i = 0; i < values.length; i++ ) {
153+
t.throws( badValue( values[ i ] ), Error, 'throws an error when provided dimension indices ['+values[ i ]+']' );
154+
}
155+
t.end();
156+
157+
function badValue( dims ) {
158+
return function badValue() {
159+
diagonal( x, dims, 0, false );
160+
};
161+
}
162+
});
163+
64164
tape( 'the function returns a view of the super-diagonal of a square matrix', function test( t ) {
65165
var expected;
66166
var x;

0 commit comments

Comments
 (0)