Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | /**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-disable max-params, max-len */
'use strict';
// MODULES //
var dcopy = require( '@stdlib/blas/base/dcopy' ).ndarray;
var base = require( './base.js' );
// MAIN //
/**
* Calculates the k-th discrete forward difference of a double-precision floating-point strided array using alternative indexing semantics.
*
* @param {PositiveInteger} N - number of indexed elements
* @param {PositiveInteger} k - number of times to recursively compute differences
* @param {Float64Array} x - input array
* @param {integer} strideX - stride length for `x`
* @param {PositiveInteger} offsetX - starting index for `x`
* @param {PositiveInteger} N1 - number of indexed elements for `prepend`
* @param {Float64Array} prepend - prepend array
* @param {integer} strideP - stride length for `prepend`
* @param {PositiveInteger} offsetP - starting index for `prepend`
* @param {PositiveInteger} N2 - number of indexed elements for `append`
* @param {Float64Array} append - append array
* @param {integer} strideA - stride length for `append`
* @param {PositiveInteger} offsetA - starting index for `append`
* @param {Float64Array} out - output array
* @param {integer} strideOut - stride length for `out`
* @param {PositiveInteger} offsetOut - starting index for `out`
* @param {Float64Array} workspace - workspace array
* @param {integer} strideW - stride length for `workspace`
* @param {PositiveInteger} offsetW - starting index for `workspace`
* @returns {Float64Array} output array
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
*
* var x = new Float64Array( [ 2.0, 4.0, 7.0, 11.0, 16.0 ] );
* var p = new Float64Array( [ 1.0 ] );
* var a = new Float64Array( [ 22.0 ] );
* var out = new Float64Array( 5 );
* var w = new Float64Array( 6 );
*
* ddiff( x.length, 2, x, 1, 0, 1, p, 1, 0, 1, a, 1, 0, out, 1, 0, w, 1, 0 );
*
* console.log( out );
* // out => <Float64Array>[ 1.0, 1.0, 1.0, 1.0, 1.0 ]
*/
function ddiff( N, k, x, strideX, offsetX, N1, prepend, strideP, offsetP, N2, append, strideA, offsetA, out, strideOut, offsetOut, workspace, strideW, offsetW ) {
var total;
var io;
var n;
var i;
total = N + N1 + N2;
// 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...
if ( total <= 1 || k >= total ) {
return out;
}
// If `k` is equal to zero, there are no differences to compute, so we merely copy the various arrays into the output array...
if ( k === 0 ) {
// Copy `prepend` into output array:
dcopy( N1, prepend, strideP, offsetP, out, strideOut, offsetOut );
// Copy `x` into output array:
io = offsetOut + ( N1 * strideOut );
dcopy( N, x, strideX, offsetX, out, strideOut, io );
// Copy `append` into output array:
io = offsetOut + ( ( N1 + N ) * strideOut );
dcopy( N2, append, strideA, offsetA, out, strideOut, io );
return out;
}
// If `k` is equal to one, we can compute the forward difference while writing directly to the output array...
if ( k === 1 ) {
base( N, x, strideX, offsetX, N1, prepend, strideP, offsetP, N2, append, strideA, offsetA, out, strideOut, offsetOut );
return out;
}
// Compute the first forward difference:
base( N, x, strideX, offsetX, N1, prepend, strideP, offsetP, N2, append, strideA, offsetA, workspace, strideW, offsetW );
// Recursively compute the next forward differences...
n = total - 1;
for ( i = 1; i < k-1; i++ ) {
base( n, workspace, strideW, offsetW, 0, prepend, strideP, offsetP, 0, append, strideA, offsetA, workspace, strideW, offsetW );
n -= 1;
}
// For the last forward difference, ensure that results are written to the output array:
base( n, workspace, strideW, offsetW, 0, prepend, strideP, offsetP, 0, append, strideA, offsetA, out, strideOut, offsetOut );
return out;
}
// EXPORTS //
module.exports = ddiff;
|