Skip to content

Commit d529cbd

Browse files
committed
fix: simplify and fix branching logic and use assertion utility
--- 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: na - task: lint_package_json status: na - task: lint_repl_help status: na - 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: passed - 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: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 2970a90 commit d529cbd

17 files changed

+198
-287
lines changed

lib/node_modules/@stdlib/stats/strided/dcovmatmtk/lib/base.js

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,9 @@ function dcovmatmtk( orient, uplo, M, N, correction, means, strideM, offsetM, A,
198198
var full;
199199
var nobs;
200200
var sa1;
201-
var sa2;
201+
var sa0;
202202
var sb1;
203-
var sb2;
203+
var sb0;
204204
var sb;
205205
var oa;
206206
var om;
@@ -213,36 +213,37 @@ function dcovmatmtk( orient, uplo, M, N, correction, means, strideM, offsetM, A,
213213
// Determine the memory layouts of `B`:
214214
isrmb = isRowMajor( [ strideB1, strideB2 ] );
215215

216-
// Check whether we need to swap the strides of `B` when writing to the upper or lower triangular part of `B`...
217-
if (
218-
( isrmb && uplo === 'upper' ) ||
219-
( !isrmb && uplo === 'lower' )
220-
) {
221-
// Triangular part is aligned with (i.e., cache optimal for) the layout of `B`, so no need to transpose the strides...
216+
// Determine the outer and inner loop strides when writing to the upper or lower triangular part of `B`...
217+
if ( uplo === 'upper') {
218+
// Writing to the upper triangular part is cache optimal for a row-major `B`, but not for a column-major `B`...
219+
sb0 = strideB2;
222220
sb1 = strideB1;
223-
sb2 = strideB2;
224-
} else if (
225-
( isrmb && uplo === 'lower' ) ||
226-
( !isrmb && uplo === 'upper' )
227-
) {
228-
// When writing to a triangular part which is not aligned with the layout of `B`, transpose the strides...
221+
} else if ( uplo === 'lower' ) {
222+
// Writing to the lower triangular part is cache optimal for a column-major `B`, but not for a row-major `B`...
223+
sb0 = strideB1;
229224
sb1 = strideB2;
230-
sb2 = strideB1;
231-
} else { // uplo !== 'lower' && uplo !== 'upper'
232-
// When computing the full covariance matrix, write initial covariances according to the layout of `B`...
225+
} else {
226+
// When computing the full covariance matrix, write covariances in a manner which is cache optimal for the layout of `B`...
233227
full = true;
234-
sb1 = strideB1;
235-
sb2 = strideB2;
228+
if ( isrmb ) {
229+
// For row-major matrices, the last dimension has the fastest changing index...
230+
sb0 = strideB2;
231+
sb1 = strideB1;
232+
} else {
233+
// For column-major matrices, the first dimension has the fastest changing index...
234+
sb0 = strideB1;
235+
sb1 = strideB2;
236+
}
236237
}
237238
// Resolve loop variables...
238239
if ( orient === 'rows' ) {
240+
sa0 = strideA2;
239241
sa1 = strideA1;
240-
sa2 = strideA2;
241242
nsamples = M;
242243
nobs = N;
243244
} else { // orient === 'columns'
245+
sa0 = strideA1;
244246
sa1 = strideA2;
245-
sa2 = strideA1;
246247
nsamples = N;
247248
nobs = M;
248249
}
@@ -252,7 +253,7 @@ function dcovmatmtk( orient, uplo, M, N, correction, means, strideM, offsetM, A,
252253
ib = offsetB;
253254
im = offsetM;
254255
for ( i0 = 0; i0 < nsamples; i0++ ) {
255-
B[ ib ] = dvarmtk( nobs, correction, means[ im ], A, sa2, ia );
256+
B[ ib ] = dvarmtk( nobs, correction, means[ im ], A, sa0, ia );
256257
ia += sa1;
257258
ib += sb;
258259
im += strideM;
@@ -261,21 +262,27 @@ function dcovmatmtk( orient, uplo, M, N, correction, means, strideM, offsetM, A,
261262
oa = offsetA;
262263
om = offsetM;
263264
for ( i1 = 0; i1 < nsamples-1; i1++ ) {
264-
ib = offsetB + ( sb1*i1 ) + ( sb2*i1 );
265+
ib = offsetB + ( sb1*i1 ) + ( sb0*i1 );
265266
ia = oa;
266267
im = om;
267268
for ( i0 = i1+1; i0 < nsamples; i0++ ) {
268269
im += strideM;
269270
ia += sa1;
270-
ib += sb2;
271-
B[ ib ] = dcovarmtk( nobs, correction, means[ om ], A, sa2, oa, means[ im ], A, sa2, ia );
271+
ib += sb0;
272+
B[ ib ] = dcovarmtk( nobs, correction, means[ om ], A, sa0, oa, means[ im ], A, sa0, ia );
272273
}
273274
oa += sa1;
274275
om += strideM;
275276
}
276277
// Copy the covariances to the other triangular part if the full covariance matrix is desired...
277278
if ( full ) {
278-
dlacpy( ( isrmb ) ? 'upper' : 'lower', nsamples, nsamples, B, sb1, sb2, offsetB, B, sb2, sb1, offsetB );
279+
if ( isrmb ) {
280+
// Copy the upper triangular part to the lower triangular part:
281+
dlacpy( 'upper', nsamples, nsamples, B, sb1, sb0, offsetB, B, sb0, sb1, offsetB );
282+
} else {
283+
// Copy the lower triangular part to the upper triangular part:
284+
dlacpy( 'lower', nsamples, nsamples, B, sb0, sb1, offsetB, B, sb1, sb0, offsetB );
285+
}
279286
}
280287
return B;
281288
}

lib/node_modules/@stdlib/stats/strided/dcovmatmtk/src/main.c

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ void API_SUFFIX(stdlib_strided_dcovmatmtk_ndarray)( const CBLAS_ORIENT orient, c
142142
CBLAS_INT nsamples;
143143
CBLAS_INT nobs;
144144
CBLAS_INT sa1;
145-
CBLAS_INT sa2;
145+
CBLAS_INT sa0;
146146
CBLAS_INT sb1;
147-
CBLAS_INT sb2;
147+
CBLAS_INT sb0;
148148
CBLAS_INT sb;
149149
CBLAS_INT oa;
150150
CBLAS_INT om;
@@ -176,35 +176,36 @@ void API_SUFFIX(stdlib_strided_dcovmatmtk_ndarray)( const CBLAS_ORIENT orient, c
176176

177177
// Check whether we need to swap the strides of `B` when writing to the upper or lower triangular part of `B`...
178178
full = false;
179-
if (
180-
( isrmb && uplo == CblasUpper ) ||
181-
( !isrmb && uplo == CblasLower )
182-
) {
183-
// Triangular part is aligned with (i.e., cache optimal for) the layout of `B`, so no need to transpose the strides...
179+
if ( uplo == CblasUpper ) {
180+
// Writing to the upper triangular part is cache optimal for a row-major `B`, but not for a column-major `B`...
181+
sb0 = strideB2;
184182
sb1 = strideB1;
185-
sb2 = strideB2;
186-
} else if (
187-
( isrmb && uplo == CblasLower ) ||
188-
( !isrmb && uplo == CblasUpper )
189-
) {
190-
// When writing to a triangular part which is not aligned with the layout of `B`, transpose the strides...
183+
} else if ( uplo == CblasLower ) {
184+
// Writing to the lower triangular part is cache optimal for a column-major `B`, but not for a row-major `B`...
185+
sb0 = strideB1;
191186
sb1 = strideB2;
192-
sb2 = strideB1;
193-
} else { // uplo !== CblasLower && uplo !== CblasUpper
194-
// When computing the full covariance matrix, write initial covariances according to the layout of `B`...
187+
} else {
188+
// When computing the full covariance matrix, write covariances in a manner which is cache optimal for the layout of `B`...
195189
full = true;
196-
sb1 = strideB1;
197-
sb2 = strideB2;
190+
if ( isrmb ) {
191+
// For row-major matrices, the last dimension has the fastest changing index...
192+
sb0 = strideB2;
193+
sb1 = strideB1;
194+
} else {
195+
// For column-major matrices, the first dimension has the fastest changing index...
196+
sb0 = strideB1;
197+
sb1 = strideB2;
198+
}
198199
}
199200
// Resolve loop variables...
200201
if ( orient == CblasRows ) {
202+
sa0 = strideA2;
201203
sa1 = strideA1;
202-
sa2 = strideA2;
203204
nsamples = M;
204205
nobs = N;
205206
} else { // orient == CblasCols
207+
sa0 = strideA1;
206208
sa1 = strideA2;
207-
sa2 = strideA1;
208209
nsamples = N;
209210
nobs = M;
210211
}
@@ -214,7 +215,7 @@ void API_SUFFIX(stdlib_strided_dcovmatmtk_ndarray)( const CBLAS_ORIENT orient, c
214215
ib = offsetB;
215216
im = offsetM;
216217
for ( i0 = 0; i0 < nsamples; i0++ ) {
217-
B[ ib ] = API_SUFFIX(stdlib_strided_dvarmtk_ndarray)( nobs, correction, Means[ im ], A, sa2, ia );
218+
B[ ib ] = API_SUFFIX(stdlib_strided_dvarmtk_ndarray)( nobs, correction, Means[ im ], A, sa0, ia );
218219
ia += sa1;
219220
ib += sb;
220221
im += strideM;
@@ -223,20 +224,26 @@ void API_SUFFIX(stdlib_strided_dcovmatmtk_ndarray)( const CBLAS_ORIENT orient, c
223224
oa = offsetA;
224225
om = offsetM;
225226
for ( i1 = 0; i1 < nsamples-1; i1++ ) {
226-
ib = offsetB + ( sb1*i1 ) + ( sb2*i1 );
227+
ib = offsetB + ( sb1*i1 ) + ( sb0*i1 );
227228
ia = oa;
228229
im = om;
229230
for ( i0 = i1+1; i0 < nsamples; i0++ ) {
230231
im += strideM;
231232
ia += sa1;
232-
ib += sb2;
233-
B[ ib ] = API_SUFFIX(stdlib_strided_dcovarmtk_ndarray)( nobs, correction, Means[ om ], A, sa2, oa, Means[ im ], A, sa2, ia );
233+
ib += sb0;
234+
B[ ib ] = API_SUFFIX(stdlib_strided_dcovarmtk_ndarray)( nobs, correction, Means[ om ], A, sa0, oa, Means[ im ], A, sa0, ia );
234235
}
235236
oa += sa1;
236237
om += strideM;
237238
}
238239
// Copy the covariances to the other triangular part if the full covariance matrix is desired...
239240
if ( full ) {
240-
API_SUFFIX(c_dlacpy_ndarray)( ( isrmb ) ? LAPACK_UPPER_TRIANGLE : LAPACK_LOWER_TRIANGLE, nsamples, nsamples, B, sb1, sb2, offsetB, B, sb2, sb1, offsetB );
241+
if ( isrmb ) {
242+
// Copy the upper triangular part to the lower triangular part:
243+
API_SUFFIX(c_dlacpy_ndarray)( LAPACK_UPPER_TRIANGLE, nsamples, nsamples, B, sb1, sb0, offsetB, B, sb0, sb1, offsetB );
244+
} else {
245+
// Copy the lower triangular part to the upper triangular part:
246+
API_SUFFIX(c_dlacpy_ndarray)( LAPACK_LOWER_TRIANGLE, nsamples, nsamples, B, sb0, sb1, offsetB, B, sb1, sb0, offsetB );
247+
}
241248
}
242249
}

lib/node_modules/@stdlib/stats/strided/dcovmatmtk/test/fixtures/column_major.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@
4242
"offsetB": 0,
4343

4444
"B_out": [
45-
1.0, 0.7,
46-
0.7, 1.0
45+
1.0,
46+
0.7,
47+
48+
0.7,
49+
1.0
4750
]
4851
}

lib/node_modules/@stdlib/stats/strided/dcovmatmtk/test/fixtures/column_major_corrected.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@
4242
"offsetB": 0,
4343

4444
"B_out": [
45-
1.0, 0.7,
46-
0.7, 1.0
45+
1.0,
46+
0.7,
47+
48+
0.7,
49+
1.0
4750
]
4851
}

lib/node_modules/@stdlib/stats/strided/dcovmatmtk/test/fixtures/column_major_lower.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@
4242
"offsetB": 0,
4343

4444
"B_out": [
45-
1.0, 0.0,
46-
0.7, 1.0
45+
1.0,
46+
0.7,
47+
48+
0.0,
49+
1.0
4750
]
4851
}

lib/node_modules/@stdlib/stats/strided/dcovmatmtk/test/fixtures/column_major_oa.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@
4545
"offsetB": 0,
4646

4747
"B_out": [
48-
1.0, 0.7,
49-
0.7, 1.0
48+
1.0,
49+
0.7,
50+
51+
0.7,
52+
1.0
5053
]
5154
}

lib/node_modules/@stdlib/stats/strided/dcovmatmtk/test/fixtures/column_major_ob.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@
4444
"B_out": [
4545
0.0, 0.0,
4646

47-
1.0, 0.7,
48-
0.7, 1.0
47+
1.0,
48+
0.7,
49+
50+
0.7,
51+
1.0
4952
]
5053
}

lib/node_modules/@stdlib/stats/strided/dcovmatmtk/test/fixtures/column_major_om.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@
4242
"offsetB": 0,
4343

4444
"B_out": [
45-
1.0, 0.7,
46-
0.7, 1.0
45+
1.0,
46+
0.7,
47+
48+
0.7,
49+
1.0
4750
]
4851
}

lib/node_modules/@stdlib/stats/strided/dcovmatmtk/test/fixtures/column_major_rows.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@
4242
"offsetB": 0,
4343

4444
"B_out": [
45-
1.0, 0.7,
46-
0.7, 1.0
45+
1.0,
46+
0.7,
47+
48+
0.7,
49+
1.0
4750
]
4851
}

lib/node_modules/@stdlib/stats/strided/dcovmatmtk/test/fixtures/column_major_sa1_sa2.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@
4848
"offsetB": 0,
4949

5050
"B_out": [
51-
1.0, 0.7,
52-
0.7, 1.0
51+
1.0,
52+
0.7,
53+
54+
0.7,
55+
1.0
5356
]
5457
}

0 commit comments

Comments
 (0)