All files base.js

100% Statements 265/265
100% Branches 38/38
100% Functions 1/1
100% Lines 265/265

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 260 261 262 263 264 265 2663x 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 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 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 2x 2x 23x 23x 23x 23x 23x 25x 16x 16x 11x 27x 42x 42x 27x 27x 16x 5x 10x 30x 30x 10x 10x 5x 25x 3x 3x 2x 6x 12x 12x 6x 6x 3x 1x 2x 6x 6x 2x 2x 1x 3x 23x 23x 23x 25x 19x 25x 3x 4x 1x 1x 1x 22x 22x 25x 17x 17x 17x 17x 17x 17x 17x 25x 5x 5x 5x 5x 5x 5x 5x 5x 22x 22x 25x 3x 3x 3x 6x 6x 6x 6x 3x 3x 19x 19x 25x 12x 12x 12x 12x 12x 12x 12x 12x 20x 20x 20x 12x 12x 12x 12x 12x 12x 12x 12x 12x 20x 20x 20x 20x 20x 12x 12x 12x 12x 12x 12x 12x 12x 12x 20x 20x 20x 25x 7x 7x 7x 7x 7x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 7x 19x 19x 25x 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-len, max-params */
 
'use strict';
 
// MODULES //
 
var isRowMajor = require( '@stdlib/ndarray/base/assert/is-row-major' );
 
 
// MAIN //
 
/**
* Performs a matrix-matrix product of the form `B := alpha * A * X + beta * B` where `A` is a tridiagonal matrix, `B` and `X` are N by NRHS matrices, and `alpha` and `beta` are real scalars, each of which may be `0`, `1`, or `-1`.
*
* ## Notes
*
* -   `DL` should have `N-1` indexed elements representing the sub-diagonal of `A`.
* -   `D` should have `N` indexed elements representing the diagonal of `A`.
* -   `DU` should have `N-1` indexed elements representing the super-diagonal of `A`.
* -   `X` is an `N` by `NRHS` input matrix.
* -   `B` is an `N` by `NRHS` input/output matrix.
* -   `alpha` must be `0`, `1`, or `-1`; otherwise, it is assumed to be `0`.
* -   `beta` must be `0`, `1`, or `-1`; otherwise, it is assumed to be `1`.
*
* @private
* @param {string} trans - specifies the operation applied to `A`
* @param {NonNegativeInteger} N - order of the matrix `A`
* @param {NonNegativeInteger} NRHS - number of right hand sides (columns of `X` and `B`)
* @param {number} alpha - scalar alpha
* @param {Float64Array} DL - sub-diagonal of `A`
* @param {integer} strideDL - stride length for `DL`
* @param {NonNegativeInteger} offsetDL - starting index of `DL`
* @param {Float64Array} D - diagonal of `A`
* @param {integer} strideD - stride length for `D`
* @param {NonNegativeInteger} offsetD - starting index of `D`
* @param {Float64Array} DU - super-diagonal of `A`
* @param {integer} strideDU - stride length for `DU`
* @param {NonNegativeInteger} offsetDU - starting index of `DU`
* @param {Float64Array} X - input matrix `X`
* @param {integer} strideX1 - stride of the first dimension of `X`
* @param {integer} strideX2 - stride of the second dimension of `X`
* @param {NonNegativeInteger} offsetX - starting index of `X`
* @param {number} beta - scalar beta
* @param {Float64Array} B - input/output matrix `B`
* @param {integer} strideB1 - stride of the first dimension of `B`
* @param {integer} strideB2 - stride of the second dimension of `B`
* @param {NonNegativeInteger} offsetB - starting index of `B`
* @returns {Float64Array} `B`
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
*
* var DL = new Float64Array( [ 1.0, 1.0 ] );
* var D = new Float64Array( [ 2.0, 3.0, 4.0 ] );
* var DU = new Float64Array( [ 1.0, 1.0 ] );
* var X = new Float64Array( [ 1.0, 1.0, 1.0 ] );
* var B = new Float64Array( [ 0.0, 0.0, 0.0 ] );
*
* dlagtm( 'no-transpose', 3, 1, 1.0, DL, 1, 0, D, 1, 0, DU, 1, 0, X, 1, 3, 0, 0.0, B, 1, 3, 0 );
* // B => <Float64Array>[ 3.0, 5.0, 5.0 ]
*/
function dlagtm( trans, N, NRHS, alpha, DL, strideDL, offsetDL, D, strideD, offsetD, DU, strideDU, offsetDU, X, strideX1, strideX2, offsetX, beta, B, strideB1, strideB2, offsetB ) {
	var sd1;
	var sd3;
	var od1;
	var od3;
	var id1;
	var id2;
	var id3;
	var d1;
	var d3;
	var rm;
	var ib;
	var ix;
	var i0;
	var i1;
	var s;
	var v;
 
	if ( N === 0 ) {
		return B;
	}
 
	// Determine whether the memory layout is row-major:
	rm = isRowMajor( [ strideB1, strideB2 ] );
 
	// Multiply B by beta if beta is not 1:
	if ( beta === 0.0 ) {
		ib = offsetB;
		if ( rm ) {
			for ( i1 = 0; i1 < N; i1++ ) {
				for ( i0 = 0; i0 < NRHS; i0++ ) {
					B[ ib + (i0*strideB2) ] = 0.0;
				}
				ib += strideB1;
			}
		} else {
			for ( i1 = 0; i1 < NRHS; i1++ ) {
				for ( i0 = 0; i0 < N; i0++ ) {
					B[ ib + (i0*strideB1) ] = 0.0;
				}
				ib += strideB2;
			}
		}
	} else if ( beta === -1.0 ) {
		ib = offsetB;
		if ( rm ) {
			for ( i1 = 0; i1 < N; i1++ ) {
				for ( i0 = 0; i0 < NRHS; i0++ ) {
					B[ ib + (i0*strideB2) ] = -B[ ib + (i0*strideB2) ];
				}
				ib += strideB1;
			}
		} else {
			for ( i1 = 0; i1 < NRHS; i1++ ) {
				for ( i0 = 0; i0 < N; i0++ ) {
					B[ ib + (i0*strideB1) ] = -B[ ib + (i0*strideB1) ];
				}
				ib += strideB2;
			}
		}
	}
	// If beta === 1.0 or any other value, B remains unchanged
 
	// Determine the sign from alpha:
	if ( alpha === 1.0 ) {
		s = 1.0;
	} else if ( alpha === -1.0 ) {
		s = -1.0;
	} else {
		// If alpha is not 1.0 or -1.0, treat as 0.0 (no multiplication):
		return B;
	}
 
	// Resolve the tridiagonal arrays based on the transpose operation:
	if ( trans === 'no-transpose' ) {
		// A*X: DL is sub-diagonal, DU is super-diagonal
		d1 = DL;
		sd1 = strideDL;
		od1 = offsetDL;
		d3 = DU;
		sd3 = strideDU;
		od3 = offsetDU;
	} else {
		// A^T*X: roles of DL and DU are swapped
		d1 = DU;
		sd1 = strideDU;
		od1 = offsetDU;
		d3 = DL;
		sd3 = strideDL;
		od3 = offsetDL;
	}
 
	// Handle N === 1 separately (no sub-diagonal or super-diagonal contribution):
	if ( N === 1 ) {
		ib = offsetB;
		ix = offsetX;
		for ( i0 = 0; i0 < NRHS; i0++ ) {
			B[ ib ] += s * D[ offsetD ] * X[ ix ];
			ib += strideB2;
			ix += strideX2;
		}
		return B;
	}
 
	// General case: N >= 2
	if ( rm ) {
		// Row-major: outer loop over rows, inner loop over columns
		ib = offsetB;
		ix = offsetX;
		id2 = offsetD;
		id3 = od3;
 
		// First row:
		for ( i0 = 0; i0 < NRHS; i0++ ) {
			v = ( D[ id2 ] * X[ ix + (i0*strideX2) ] ) + ( d3[ id3 ] * X[ ix + strideX1 + (i0*strideX2) ] );
			B[ ib + (i0*strideB2) ] += s * v;
		}
		ib += strideB1;
		ix += strideX1;
		id2 += strideD;
		id3 += sd3;
		id1 = od1;
 
		// Interior rows:
		for ( i1 = 1; i1 < N - 1; i1++ ) {
			for ( i0 = 0; i0 < NRHS; i0++ ) {
				v = d1[ id1 ] * X[ ix - strideX1 + (i0*strideX2) ];
				v += D[ id2 ] * X[ ix + (i0*strideX2) ];
				v += d3[ id3 ] * X[ ix + strideX1 + (i0*strideX2) ];
				B[ ib + (i0*strideB2) ] += s * v;
			}
			ib += strideB1;
			ix += strideX1;
			id1 += sd1;
			id2 += strideD;
			id3 += sd3;
		}
 
		// Last row:
		for ( i0 = 0; i0 < NRHS; i0++ ) {
			v = ( d1[ id1 ] * X[ ix - strideX1 + (i0*strideX2) ] ) + ( D[ id2 ] * X[ ix + (i0*strideX2) ] );
			B[ ib + (i0*strideB2) ] += s * v;
		}
	} else {
		// Column-major: outer loop over columns, inner loop over rows
		ib = offsetB;
		ix = offsetX;
 
		for ( i1 = 0; i1 < NRHS; i1++ ) {
			id1 = od1;
			id2 = offsetD;
			id3 = od3;
 
			// First row:
			v = ( D[ id2 ] * X[ ix ] ) + ( d3[ id3 ] * X[ ix + strideX1 ] );
			B[ ib ] += s * v;
			id2 += strideD;
			id3 += sd3;
 
			// Interior rows:
			for ( i0 = 1; i0 < N - 1; i0++ ) {
				v = d1[ id1 ] * X[ ix + ((i0-1)*strideX1) ];
				v += D[ id2 ] * X[ ix + (i0*strideX1) ];
				v += d3[ id3 ] * X[ ix + ((i0+1)*strideX1) ];
				B[ ib + (i0*strideB1) ] += s * v;
				id1 += sd1;
				id2 += strideD;
				id3 += sd3;
			}
 
			// Last row:
			v = ( d1[ id1 ] * X[ ix + ((N-2)*strideX1) ] ) + ( D[ id2 ] * X[ ix + ((N-1)*strideX1) ] );
			B[ ib + ((N-1)*strideB1) ] += s * v;
 
			ib += strideB2;
			ix += strideX2;
		}
	}
 
	return B;
}
 
 
// EXPORTS //
 
module.exports = dlagtm;