Skip to content

Commit 613fee5

Browse files
authored
refactor: reduce code duplication and add comments
Signed-off-by: Athan <kgryte@gmail.com>
1 parent dd84fbc commit 613fee5

1 file changed

Lines changed: 9 additions & 9 deletions

File tree

  • lib/node_modules/@stdlib/blas/ext/base/ddiff/lib

lib/node_modules/@stdlib/blas/ext/base/ddiff/lib/ndarray.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,11 @@ function ddiff( N, k, x, strideX, offsetX, N1, prepend, strideP, offsetP, N2, ap
7676
if ( total <= 1 ) {
7777
return out;
7878
}
79-
80-
// If k >= total number of elements, the k-th forward difference results in an empty array, so this function is a no-op...
79+
// If `k` is greater than or equal to the total number of elements, the k-th forward difference results in an empty array, so this function is a no-op...
8180
if ( k >= total ) {
8281
return out;
8382
}
84-
83+
// If `k` is equal to zero, there are no differences to compute, so we merely copy the various arrays into the output array...
8584
if ( k === 0 ) {
8685
// Copy `prepend` into output array:
8786
dcopy( N1, prepend, strideP, offsetP, out, strideOut, offsetOut );
@@ -96,21 +95,22 @@ function ddiff( N, k, x, strideX, offsetX, N1, prepend, strideP, offsetP, N2, ap
9695

9796
return out;
9897
}
98+
// Compute the first forward difference:
99+
base( N, x, strideX, offsetX, N1, prepend, strideP, offsetP, N2, append, strideA, offsetA, workspace, strideW, offsetW );
99100

101+
// If `k` is one, we're finished...
100102
if ( k === 1 ) {
101-
base( N, x, strideX, offsetX, N1, prepend, strideP, offsetP, N2, append, strideA, offsetA, out, strideOut, offsetOut );
102103
return out;
103104
}
104-
105-
base( N, x, strideX, offsetX, N1, prepend, strideP, offsetP, N2, append, strideA, offsetA, workspace, strideW, offsetW );
106-
105+
// Otherwise, recursively compute forward differences...
107106
n = total - 1;
108-
for ( i = 1; i < k - 1; i++ ) {
107+
for ( i = 1; i < k-1; i++ ) {
109108
base( n, workspace, strideW, offsetW, 0, prepend, strideP, offsetP, 0, append, strideA, offsetA, workspace, strideW, offsetW );
110109
n -= 1;
111110
}
112-
111+
// For the last forward difference, ensure that results are written to the output array:
113112
base( n, workspace, strideW, offsetW, 0, prepend, strideP, offsetP, 0, append, strideA, offsetA, out, strideOut, offsetOut );
113+
114114
return out;
115115
}
116116

0 commit comments

Comments
 (0)