All files / gvander/lib base.js

41.29% Statements 64/155
100% Branches 1/1
0% Functions 0/1
41.29% Lines 64/155

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 1561x 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-len */
 
'use strict';
 
// MODULES //
 
var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
var isRowMajor = require( '@stdlib/ndarray/base/assert/is-row-major' );
var gfill = require( '@stdlib/blas/ext/base/gfill' ).ndarray;
var accessors = require( './accessors.js' );
 
 
// MAIN //
 
/**
* Generates a Vandermonde matrix using alternative indexing semantics.
*
* ## Notes
*
* -   The implementation uses recursive multiplication to generate successive powers, which carries risk of additional accumulated floating-point error; however, for most use cases, such additional error should be negligible and not problematic.
*
* @private
* @param {integer} mode - mode indicating whether to generate increasing or decreasing powers
* @param {NonNegativeInteger} M - number of rows in `out`
* @param {NonNegativeInteger} N - number of columns in `out`
* @param {NumericArray} x - input array
* @param {integer} strideX - stride length for `x`
* @param {NonNegativeInteger} offsetX - starting index for `x`
* @param {NumericArray} out - output matrix
* @param {integer} strideOut1 - stride length for the first dimension of `out`
* @param {integer} strideOut2 - stride length for the second dimension of `out`
* @param {NonNegativeInteger} offsetOut - starting index for `out`
* @returns {NumericArray} output matrix
*
* @example
* var x = [ 1.0, 2.0, 3.0 ];
* var out = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];
*
* gvander( 1, 3, 3, x, 1, 0, out, 3, 1, 0 );
* // out => [ 1.0, 1.0, 1.0, 1.0, 2.0, 4.0, 1.0, 3.0, 9.0 ]
*/
function gvander( mode, M, N, x, strideX, offsetX, out, strideOut1, strideOut2, offsetOut ) {
	var xobj;
	var oobj;
	var do0;
	var do1;
	var S0;
	var S1;
	var io;
	var ix;
	var i0;
	var i1;

	xobj = arraylike2object( x );
	oobj = arraylike2object( out );
	if ( xobj.accessorProtocol || oobj.accessorProtocol ) {
		return accessors( mode, M, N, xobj, strideX, offsetX, oobj, strideOut1, strideOut2, offsetOut );
	}

	// Note on variable naming convention: S#, do#, io, i# where # corresponds to the loop number, with `0` being the innermost loop...
	if ( isRowMajor( [ strideOut1, strideOut2 ] ) ) {
		S0 = N;
		S1 = M;
		do0 = strideOut2;
		do1 = strideOut1 - ( S0*strideOut2 );

		// Increasing: x^0, x^1, ..., x^(N-1)
		if ( mode > 0 ) {
			io = offsetOut;
			ix = offsetX;
			for ( i1 = 0; i1 < S1; i1++ ) {
				out[ io ] = 1.0;
				io += do0;
				for ( i0 = 1; i0 < S0; i0++ ) {
					out[ io ] = out[ io-do0 ] * x[ ix ];
					io += do0;
				}
				ix += strideX;
				io += do1;
			}
			return out;
		}
		// Decreasing: x^(N-1), x^(N-2), ..., x^0
		io = offsetOut + ( ( S1-1 ) * strideOut1 ) + ( ( S0-1 ) * strideOut2 );
		ix = offsetX + ( ( S1-1 ) * strideX );
		for ( i1 = S1-1; i1 >= 0; i1-- ) {
			out[ io ] = 1.0;
			io -= do0;
			for ( i0 = 1; i0 < S0; i0++ ) {
				out[ io ] = out[ io+do0 ] * x[ ix ];
				io -= do0;
			}
			ix -= strideX;
			io -= do1;
		}
		return out;
	}
	// Column-major...
	S0 = M;
	S1 = N;
	do0 = strideOut1;
	do1 = strideOut2 - ( S0*strideOut1 );

	// Increasing: column j contains x^j
	if ( mode > 0 ) {
		gfill( S0, 1.0, out, strideOut1, offsetOut );
		io = offsetOut + strideOut2;
		for ( i1 = 1; i1 < S1; i1++ ) {
			ix = offsetX;
			for ( i0 = 0; i0 < S0; i0++ ) {
				out[ io ] = out[ io-strideOut2 ] * x[ ix ];
				ix += strideX;
				io += do0;
			}
			io += do1;
		}
		return out;
	}
	// Decreasing: column 0 contains x^(N-1), last column all ones
	gfill( S0, 1.0, out, strideOut1, offsetOut + ( ( S1-1 ) * strideOut2 ) );
	io = offsetOut + ( ( S1-2 ) * strideOut2 ) + ( ( S0-1 ) * strideOut1 );
	for ( i1 = S1-2; i1 >= 0; i1-- ) {
		ix = offsetX + ( ( S0-1 ) * strideX );
		for ( i0 = S0-1; i0 >= 0; i0-- ) {
			out[ io ] = out[ io+strideOut2 ] * x[ ix ];
			ix -= strideX;
			io -= do0;
		}
		io -= do1;
	}
	return out;
}
 
 
// EXPORTS //
 
module.exports = gvander;