Skip to content

Commit c283fa2

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: na - task: lint_license_headers status: passed ---
1 parent e234d8a commit c283fa2

4 files changed

Lines changed: 61 additions & 42 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ The function accepts the following arguments:
7474
- If `k > 0`, the function rotates the matrix clockwise.
7575
- If `k < 0`, the function rotates the matrix counterclockwise.
7676
- The returned ndarray is a **view** of the input ndarray. Accordingly, writing to the original ndarray will **mutate** the returned ndarray and vice versa.
77-
- If provided an ndarray with fewer than two dimensions, the function returns the input array unchanged.
77+
- If provided an ndarray with fewer than two dimensions, the function returns a new view of the input ndarray.
7878

7979
</section>
8080

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
function rotates the matrix counterclockwise.
77

88
If provided an ndarray with fewer than two dimensions, the function returns
9-
the input array unchanged.
9+
a new view of the input ndarray.
1010

1111
The returned ndarray is a *view* of the input ndarray. Accordingly, writing
1212
to the original ndarray will mutate the returned ndarray and vice versa.

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

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -59,44 +59,43 @@ function rotr90( x, k, writable ) {
5959

6060
sh = getShape( x, true );
6161
d = sh.length;
62-
if ( d < 2 ) {
63-
return x;
64-
}
6562
st = getStrides( x, true );
6663
offset = getOffset( x );
6764

68-
// Normalize `k` to the interval [0, 3]:
69-
k %= 4;
70-
if ( k < 0 ) {
71-
k += 4;
72-
}
73-
// Cache the original shape and stride values for the last two dimensions:
74-
M = sh[ d-2 ];
75-
N = sh[ d-1 ];
76-
sm = st[ d-2 ];
77-
sn = st[ d-1 ];
65+
if ( d >= 2 ) {
66+
// Normalize `k` to the interval [0, 3]:
67+
k %= 4;
68+
if ( k < 0 ) {
69+
k += 4;
70+
}
71+
// Cache the original shape and stride values for the last two dimensions:
72+
M = sh[ d-2 ];
73+
N = sh[ d-1 ];
74+
sm = st[ d-2 ];
75+
sn = st[ d-1 ];
7876

79-
// Case: rotate 90 deg clockwise
80-
if ( k === 1 ) {
81-
sh[ d-2 ] = N;
82-
sh[ d-1 ] = M;
83-
st[ d-2 ] = sn;
84-
st[ d-1 ] = -sm;
85-
offset += ( M - 1 ) * sm;
86-
}
87-
// Case: rotate 180 deg clockwise (i.e., reverse both dimensions)
88-
else if ( k === 2 ) {
89-
st[ d-2 ] = -sm;
90-
st[ d-1 ] = -sn;
91-
offset += ( ( M - 1 ) * sm ) + ( ( N - 1 ) * sn );
92-
}
93-
// Case: rotate 270 deg clockwise
94-
else if ( k === 3 ) {
95-
sh[ d-2 ] = N;
96-
sh[ d-1 ] = M;
97-
st[ d-2 ] = -sn;
98-
st[ d-1 ] = sm;
99-
offset += ( N - 1 ) * sn;
77+
// Case: rotate 90 deg clockwise
78+
if ( k === 1 ) {
79+
sh[ d-2 ] = N;
80+
sh[ d-1 ] = M;
81+
st[ d-2 ] = sn;
82+
st[ d-1 ] = -sm;
83+
offset += ( M - 1 ) * sm;
84+
}
85+
// Case: rotate 180 deg clockwise (i.e., reverse both dimensions)
86+
else if ( k === 2 ) {
87+
st[ d-2 ] = -sm;
88+
st[ d-1 ] = -sn;
89+
offset += ( ( M - 1 ) * sm ) + ( ( N - 1 ) * sn );
90+
}
91+
// Case: rotate 270 deg clockwise
92+
else if ( k === 3 ) {
93+
sh[ d-2 ] = N;
94+
sh[ d-1 ] = M;
95+
st[ d-2 ] = -sn;
96+
st[ d-1 ] = sm;
97+
offset += ( N - 1 ) * sn;
98+
}
10099
}
101100
return new x.constructor( getDType( x ), getData( x ), sh, st, offset, getOrder( x ), { // eslint-disable-line max-len
102101
'readonly': !writable

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

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,32 +51,52 @@ tape( 'main export is a function', function test( t ) {
5151
t.end();
5252
});
5353

54-
tape( 'the function returns the input array unchanged if provided an ndarray having fewer than two dimensions (0d)', function test( t ) {
54+
tape( 'the function returns a view if provided an ndarray having fewer than two dimensions (0d)', function test( t ) {
5555
var arr;
5656
var x;
5757

5858
x = new ndarray( 'float64', new Float64Array( [ 5.0 ] ), [], [ 0 ], 0, 'row-major' );
5959
arr = rotr90( x, 1, false );
60-
t.strictEqual( arr, x, 'returns expected value' );
60+
t.notEqual( arr, x, 'returns expected value' );
61+
t.strictEqual( getData( arr ), getData( x ), 'returns expected value' );
62+
t.strictEqual( arr.get(), 5.0, 'returns expected value' );
63+
t.deepEqual( getShape( arr ), [], 'returns expected value' );
64+
t.deepEqual( getStrides( arr ), [ 0 ], 'returns expected value' );
65+
t.strictEqual( getOrder( arr ), getOrder( x ), 'returns expected value' );
6166

6267
x = new base( 'float64', new Float64Array( [ 5.0 ] ), [], [ 0 ], 0, 'row-major' );
6368
arr = rotr90( x, 1, false );
64-
t.strictEqual( arr, x, 'returns expected value' );
69+
t.notEqual( arr, x, 'returns expected value' );
70+
t.strictEqual( getData( arr ), getData( x ), 'returns expected value' );
71+
t.strictEqual( arr.get(), 5.0, 'returns expected value' );
72+
t.deepEqual( getShape( arr ), [], 'returns expected value' );
73+
t.deepEqual( getStrides( arr ), [ 0 ], 'returns expected value' );
74+
t.strictEqual( getOrder( arr ), getOrder( x ), 'returns expected value' );
6575

6676
t.end();
6777
});
6878

69-
tape( 'the function returns the input array unchanged if provided an ndarray having fewer than two dimensions (1d)', function test( t ) {
79+
tape( 'the function returns a view if provided an ndarray having fewer than two dimensions (1d)', function test( t ) {
7080
var arr;
7181
var x;
7282

7383
x = new ndarray( 'float64', new Float64Array( [ 1.0, 2.0, 3.0 ] ), [ 3 ], [ 1 ], 0, 'row-major' );
7484
arr = rotr90( x, 1, false );
75-
t.strictEqual( arr, x, 'returns expected value' );
85+
t.notEqual( arr, x, 'returns expected value' );
86+
t.strictEqual( getData( arr ), getData( x ), 'returns expected value' );
87+
t.deepEqual( ndarray2array( arr ), [ 1.0, 2.0, 3.0 ], 'returns expected value' );
88+
t.deepEqual( getShape( arr ), [ 3 ], 'returns expected value' );
89+
t.deepEqual( getStrides( arr ), [ 1 ], 'returns expected value' );
90+
t.strictEqual( getOrder( arr ), getOrder( x ), 'returns expected value' );
7691

7792
x = new base( 'float64', new Float64Array( [ 1.0, 2.0, 3.0 ] ), [ 3 ], [ 1 ], 0, 'row-major' );
7893
arr = rotr90( x, 1, false );
79-
t.strictEqual( arr, x, 'returns expected value' );
94+
t.notEqual( arr, x, 'returns expected value' );
95+
t.strictEqual( getData( arr ), getData( x ), 'returns expected value' );
96+
t.deepEqual( ndarray2array( arr ), [ 1.0, 2.0, 3.0 ], 'returns expected value' );
97+
t.deepEqual( getShape( arr ), [ 3 ], 'returns expected value' );
98+
t.deepEqual( getStrides( arr ), [ 1 ], 'returns expected value' );
99+
t.strictEqual( getOrder( arr ), getOrder( x ), 'returns expected value' );
80100

81101
t.end();
82102
});

0 commit comments

Comments
 (0)