All files ndarray.js

99.22% Statements 257/259
97.43% Branches 38/39
100% Functions 2/2
99.22% Lines 257/259

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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 2603x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x     29x 29x 10x 10x 10x 10x 40x 40x 40x 40x 40x 40x 10x 10x 19x 29x 15x 15x 15x 15x 4x 4x 4x 4x 4x 4x 15x 13x 13x 13x 13x 15x 2x 2x 2x 2x 2x 29x 2x 2x 2x 2x 2x 2x 19x 29x 15x 15x 2x 2x 2x 2x 2x 2x 2x 2x 2x 15x 13x 13x 34x 34x 34x 34x 34x 34x 13x 15x 13x 13x 13x 13x 13x 15x 19x 29x 17x 17x 6x 6x 6x 6x 6x 6x 17x 19x 29x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 12x 12x 45x 45x 45x 45x 45x 45x 57x 20x 20x 20x 25x 57x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 23x 57x 19x 19x 19x 4x 4x 4x 4x 4x 57x 2x 2x 2x 4x 4x 4x 4x 57x 3x 3x 3x 3x 3x  
/**
* @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 gcopy = require( '@stdlib/blas/base/gcopy' ).ndarray;
var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
var accessors = require( './accessors.js' );
 
 
// FUNCTIONS //
 
/**
* Calculates the forward difference of a strided array using alternative indexing semantics.
*
* @private
* @param {PositiveInteger} N - number of indexed elements
* @param {NumericArray} x - input array
* @param {integer} strideX - stride length for `x`
* @param {NonNegativeInteger} offsetX - starting index for `x`
* @param {NonNegativeInteger} N1 - number of indexed elements for `prepend`
* @param {NumericArray} prepend - prepend array
* @param {integer} strideP - stride length for `prepend`
* @param {NonNegativeInteger} offsetP - starting index for `prepend`
* @param {NonNegativeInteger} N2 - number of indexed elements for `append`
* @param {NumericArray} append - append array
* @param {integer} strideA - stride length for `append`
* @param {NonNegativeInteger} offsetA - starting index for `append`
* @param {NumericArray} out - output array
* @param {integer} strideOut - stride length for `out`
* @param {NonNegativeInteger} offsetOut - starting index for `out`
* @returns {NumericArray} output array
*/
function base( N, x, strideX, offsetX, N1, prepend, strideP, offsetP, N2, append, strideA, offsetA, out, strideOut, offsetOut ) {
	var total;
	var prev;
	var curr;
	var ix;
	var io;
	var ip;
	var ia;
	var i;
 
	total = N + N1 + N2;
	if ( total <= 1 ) {
		return out;
	}
	// Compute forward differences:
	if ( N1 === 0 && N2 === 0 ) {
		ix = offsetX;
		io = offsetOut;
		prev = x[ ix ];
		for ( i = 1; i < N; i++ ) {
			ix += strideX;
			curr = x[ ix ];
			out[ io ] = curr - prev;
			prev = curr;
			io += strideOut;
		}
		return out;
	}
	// Compute forward differences over the list of prepended values:
	if ( N1 > 0 ) {
		io = offsetOut;
		ip = offsetP;
		prev = prepend[ ip ];
		for ( i = 1; i < N1; i++ ) {
			ip += strideP;
			curr = prepend[ ip ];
			out[ io ] = curr - prev;
			prev = curr;
			io += strideOut;
		}
		if ( N > 0 ) {
			curr = x[ offsetX ];
			out[ io ] = curr - prev;
			prev = curr;
			io += strideOut;
		} else if ( N2 > 0 ) {
			curr = append[ offsetA ];
			out[ io ] = curr - prev;
			prev = curr;
			io += strideOut;
		}
	} else if ( N > 0 ) {
		prev = x[ offsetX ];
		io = offsetOut;
	} else {
		prev = append[ offsetA ];
		io = offsetOut;
	}
	// Compute forward differences over the input array:
	if ( N > 0 ) {
		ix = offsetX;
		if ( N1 === 0 ) {
			prev = x[ ix ];
			ix += strideX;
			for ( i = 1; i < N; i++ ) {
				curr = x[ ix ];
				out[ io ] = curr - prev;
				prev = curr;
				io += strideOut;
				ix += strideX;
			}
		} else {
			ix += strideX;
			for ( i = 1; i < N; i++ ) {
				curr = x[ ix ];
				out[ io ] = curr - prev;
				prev = curr;
				io += strideOut;
				ix += strideX;
			}
		}
		if ( N2 > 0 ) {
			curr = append[ offsetA ];
			out[ io ] = curr - prev;
			prev = curr;
			io += strideOut;
		}
	}
	// Compute forward differences over the list of appended values:
	if ( N2 > 0 ) {
		ia = offsetA + strideA;
		for ( i = 1; i < N2; i++ ) {
			curr = append[ ia ];
			out[ io ] = curr - prev;
			prev = curr;
			io += strideOut;
			ia += strideA;
		}
	}
	return out;
}
 
 
// MAIN //
 
/**
* Calculates the k-th discrete forward difference of a strided array using alternative indexing semantics.
*
* @param {PositiveInteger} N - number of indexed elements
* @param {PositiveInteger} k - number of times to recursively compute differences
* @param {NumericArray} x - input array
* @param {integer} strideX - stride length for `x`
* @param {NonNegativeInteger} offsetX - starting index for `x`
* @param {NonNegativeInteger} N1 - number of indexed elements for `prepend`
* @param {NumericArray} prepend - prepend array
* @param {integer} strideP - stride length for `prepend`
* @param {NonNegativeInteger} offsetP - starting index for `prepend`
* @param {NonNegativeInteger} N2 - number of indexed elements for `append`
* @param {NumericArray} append - append array
* @param {integer} strideA - stride length for `append`
* @param {NonNegativeInteger} offsetA - starting index for `append`
* @param {NumericArray} out - output array
* @param {integer} strideOut - stride length for `out`
* @param {NonNegativeInteger} offsetOut - starting index for `out`
* @param {NumericArray} workspace - workspace array
* @param {integer} strideW - stride length for `workspace`
* @param {NonNegativeInteger} offsetW - starting index for `workspace`
* @returns {NumericArray} output array
*
* @example
* var x = [ 2.0, 4.0, 7.0, 11.0, 16.0 ];
* var p = [ 1.0 ];
* var a = [ 22.0 ];
* var out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ];
* var w = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];
*
* gdiff( x.length, 2, x, 1, 0, 1, p, 1, 0, 1, a, 1, 0, out, 1, 0, w, 1, 0 );
*
* console.log( out );
* // => [ 1.0, 1.0, 1.0, 1.0, 1.0 ]
*/
function gdiff( N, k, x, strideX, offsetX, N1, prepend, strideP, offsetP, N2, append, strideA, offsetA, out, strideOut, offsetOut, workspace, strideW, offsetW ) {
	var total;
	var ox;
	var op;
	var oa;
	var oo;
	var ow;
	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;
	}
	// Dispatch to the accessor path when any array uses the accessor protocol...
	ox = arraylike2object( x );
	op = arraylike2object( prepend );
	oa = arraylike2object( append );
	oo = arraylike2object( out );
	ow = arraylike2object( workspace );
	if ( ox.accessorProtocol || op.accessorProtocol || oa.accessorProtocol || oo.accessorProtocol || ow.accessorProtocol ) {
		accessors( N, k, ox, strideX, offsetX, N1, op, strideP, offsetP, N2, oa, strideA, offsetA, oo, strideOut, offsetOut, ow, strideW, offsetW );
		return oo.data;
	}
	// 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:
		gcopy( N1, prepend, strideP, offsetP, out, strideOut, offsetOut );
 
		// Copy `x` into output array:
		io = offsetOut + ( N1 * strideOut );
		gcopy( N, x, strideX, offsetX, out, strideOut, io );
 
		// Copy `append` into output array:
		io = offsetOut + ( ( N1 + N ) * strideOut );
		gcopy( 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 = gdiff;