|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2025 The Stdlib Authors. |
| 5 | +* |
| 6 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +* you may not use this file except in compliance with the License. |
| 8 | +* You may obtain a copy of the License at |
| 9 | +* |
| 10 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +* |
| 12 | +* Unless required by applicable law or agreed to in writing, software |
| 13 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +* See the License for the specific language governing permissions and |
| 16 | +* limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +'use strict'; |
| 20 | + |
| 21 | +// MODULES // |
| 22 | + |
| 23 | +var isRowMajor = require( '@stdlib/ndarray/base/assert/is-row-major' ); |
| 24 | +var max = require( '@stdlib/math/base/special/max' ); |
| 25 | +var min = require( '@stdlib/math/base/special/min' ); |
| 26 | + |
| 27 | + |
| 28 | +// MAIN // |
| 29 | + |
| 30 | +/** |
| 31 | +* Performs one of the matrix-vector operations `x = A*x` or `x = A^T*x` where `x` is an `N` element vector and `A` is an `N` by `N` unit, or non-unit, upper or lower triangular band matrix, with ( `K` + 1 ) diagonals. |
| 32 | +* |
| 33 | +* @private |
| 34 | +* @param {string} uplo - specifies whether `A` is an upper or lower triangular matrix |
| 35 | +* @param {string} trans - specifies whether `A` should be transposed, conjugate-transposed, or not transposed |
| 36 | +* @param {string} diag - specifies whether `A` has a unit diagonal |
| 37 | +* @param {NonNegativeInteger} N - number of elements along each dimension of `A` |
| 38 | +* @param {NonNegativeInteger} K - number of super-diagonals or sub-diagonals of the matrix `A` |
| 39 | +* @param {Float64Array} A - input matrix |
| 40 | +* @param {integer} strideA1 - stride of the first dimension of `A` |
| 41 | +* @param {integer} strideA2 - stride of the second dimension of `A` |
| 42 | +* @param {NonNegativeInteger} offsetA - starting index for `A` |
| 43 | +* @param {Float64Array} x - input vector |
| 44 | +* @param {integer} strideX - `x` stride length |
| 45 | +* @param {NonNegativeInteger} offsetX - starting index for `x` |
| 46 | +* @returns {Float64Array} `x` |
| 47 | +* |
| 48 | +* @example |
| 49 | +* var Float64Array = require( '@stdlib/array/float64' ); |
| 50 | +* |
| 51 | +* var A = new Float64Array( [ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 ] ); |
| 52 | +* var x = new Float64Array( [ 1.0, 2.0, 3.0 ] ); |
| 53 | +* |
| 54 | +* dtbmv( 'upper', 'no-transpose', 'unit', 3, 1, A, 2, 1, 0, x, 1, 0 ); |
| 55 | +* // x => <Float64Array>[ 3.0, 11.0, 3.0 ] |
| 56 | +*/ |
| 57 | +function dtbmv( uplo, trans, diag, N, K, A, strideA1, strideA2, offsetA, x, strideX, offsetX ) { // eslint-disable-line max-params, max-len |
| 58 | + var nonunit; |
| 59 | + var isrm; |
| 60 | + var idx; |
| 61 | + var tmp; |
| 62 | + var sa0; |
| 63 | + var sa1; |
| 64 | + var ix0; |
| 65 | + var ix1; |
| 66 | + var i0; |
| 67 | + var i1; |
| 68 | + var oa; |
| 69 | + var ox; |
| 70 | + |
| 71 | + // Note on variable naming convention: sa#, ix#, i# where # corresponds to the loop number, with `0` being the innermost loop... |
| 72 | + |
| 73 | + isrm = isRowMajor( [ strideA1, strideA2 ] ); |
| 74 | + nonunit = ( diag === 'non-unit' ); |
| 75 | + |
| 76 | + if ( isrm ) { |
| 77 | + // For row-major matrices, the last dimension has the fastest changing index... |
| 78 | + sa0 = strideA2; // stride for innermost loop |
| 79 | + sa1 = strideA1; // stride for outermost loop |
| 80 | + } else { // isColMajor |
| 81 | + // For column-major matrices, the first dimension has the fastest changing index... |
| 82 | + sa0 = strideA1; // stride for innermost loop |
| 83 | + sa1 = strideA2; // stride for outermost loop |
| 84 | + } |
| 85 | + ox = offsetX; |
| 86 | + if ( |
| 87 | + ( !isrm && trans === 'no-transpose' && uplo === 'upper' ) || |
| 88 | + ( isrm && trans !== 'no-transpose' && uplo === 'lower' ) |
| 89 | + ) { |
| 90 | + ix1 = ox; |
| 91 | + for ( i1 = 0; i1 < N; i1++ ) { |
| 92 | + oa = offsetA + ( sa1 * i1 ); |
| 93 | + tmp = x[ ix1 ]; |
| 94 | + if ( nonunit ) { |
| 95 | + tmp = A[ oa + ( sa0 * K ) ] * x[ ix1 ]; |
| 96 | + } |
| 97 | + for ( i0 = i1 + 1; i0 <= min( N - 1, i1 + K ); i0++ ) { |
| 98 | + ix0 = ox + ( i0 * strideX ); |
| 99 | + idx = oa + ( sa0 * ( K + i0 - i1 ) ); |
| 100 | + tmp += A[ idx ] * x[ ix0 ]; |
| 101 | + } |
| 102 | + x[ ix1 ] = tmp; |
| 103 | + ix1 += strideX; |
| 104 | + } |
| 105 | + return x; |
| 106 | + } |
| 107 | + if ( |
| 108 | + ( !isrm && trans === 'no-transpose' && uplo === 'lower' ) || |
| 109 | + ( isrm && trans !== 'no-transpose' && uplo === 'upper' ) |
| 110 | + ) { |
| 111 | + ix1 = ox + ( (N - 1) * strideX ); |
| 112 | + for ( i1 = N - 1; i1 >= 0; i1-- ) { |
| 113 | + oa = offsetA; |
| 114 | + tmp = x[ ix1 ]; |
| 115 | + if ( nonunit ) { |
| 116 | + tmp = A[ oa + ( sa1 * i1 ) ] * x[ ix1 ]; |
| 117 | + } |
| 118 | + for ( i0 = max( 0, i1 - K ); i0 < i1; i0++ ) { |
| 119 | + idx = oa + ( sa1 * i0 ) + ( sa0 * ( i1 - i0 ) ); |
| 120 | + ix0 = ox + ( i0 * strideX ); |
| 121 | + tmp += A[ idx ] * x[ ix0 ]; |
| 122 | + } |
| 123 | + x[ ix1 ] = tmp; |
| 124 | + ix1 -= strideX; |
| 125 | + } |
| 126 | + return x; |
| 127 | + } |
| 128 | + if ( |
| 129 | + ( !isrm && trans !== 'no-transpose' && uplo === 'upper' ) || |
| 130 | + ( isrm && trans === 'no-transpose' && uplo === 'lower' ) |
| 131 | + ) { |
| 132 | + ix1 = x + ( ( N - 1 ) * strideX ); |
| 133 | + for ( i1 = N - 1; i1 >= 0; i1-- ) { |
| 134 | + oa = offsetA + ( sa1 * i1 ); |
| 135 | + tmp = x[ ix1 ]; |
| 136 | + if ( nonunit ) { |
| 137 | + tmp = A[ oa + ( sa0 * K ) ] * x[ ix1 ]; |
| 138 | + } |
| 139 | + for ( i0 = max( 0, i1 - K ); i0 < i1; i0++ ) { |
| 140 | + idx = oa + ( sa0 * ( K + i0 - i1 ) ); |
| 141 | + ix0 = ox + ( i0 * strideX ); |
| 142 | + tmp += A[ idx ] * x[ ix0 ]; |
| 143 | + } |
| 144 | + x[ ix1 ] = tmp; |
| 145 | + ix1 -= strideX; |
| 146 | + } |
| 147 | + return x; |
| 148 | + } |
| 149 | + // ( !isrm && trans !== 'no-transpose' && uplo === 'lower' ) || ( isrm && trans === 'no-transpose' && uplo === 'upper' ) |
| 150 | + ix1 = ox; |
| 151 | + for ( i1 = 0; i1 < N; i1++ ) { |
| 152 | + oa = offsetA + ( sa1 * i1 ); |
| 153 | + tmp = x[ ix1 ]; |
| 154 | + if ( nonunit ) { |
| 155 | + tmp = A[ oa ] * x[ ix1 ]; |
| 156 | + } |
| 157 | + for ( i0 = i1 + 1; i0 <= min( N - 1, i1 + K ); i0++ ) { |
| 158 | + ix0 = ox + ( i0 * strideX ); |
| 159 | + idx = oa + ( sa0 * ( i0 - i1 ) ); |
| 160 | + tmp += A[ idx ] * x[ ix0 ]; |
| 161 | + } |
| 162 | + x[ ix1 ] = tmp; |
| 163 | + ix1 += strideX; |
| 164 | + } |
| 165 | + return x; |
| 166 | +} |
| 167 | + |
| 168 | + |
| 169 | +// EXPORTS // |
| 170 | + |
| 171 | +module.exports = dtbmv; |
0 commit comments