All files base.js

99.25% Statements 267/269
93.93% Branches 31/33
100% Functions 3/3
99.25% Lines 267/269

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 266 267 268 269 2703x 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 108x 108x 108x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 52x 52x 52x 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 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 10x 10x 56x 46x 46x 46x 56x 56x 56x 28x 28x 8x 8x 56x 4x 4x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 56x 31x 56x 26x 5x 5x 5x 26x 21x 21x 21x 21x 26x 26x 26x 26x 52x 52x 52x 52x 52x 52x 52x 52x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 52x     52x 52x 52x 26x 26x 26x 26x 26x 56x 21x 21x 21x 56x 5x 5x 5x 5x 56x 56x 56x 56x 52x 52x 52x 52x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 26x 56x 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.
*/
 
'use strict';
 
// MODULES //
 
var isRowMajor = require( '@stdlib/ndarray/base/assert/is-row-major' );
var zfill = require( '@stdlib/blas/ext/base/zfill' ).ndarray;
var zscal = require( '@stdlib/blas/base/zscal' ).ndarray;
var reinterpret = require( '@stdlib/strided/base/reinterpret-complex128' );
var real = require( '@stdlib/complex/float64/real' );
var imag = require( '@stdlib/complex/float64/imag' );
var mul = require( '@stdlib/complex/float64/base/mul' );
var Complex128 = require( '@stdlib/complex/float64/ctor' );
 
 
// FUNCTIONS //
 
/**
* Tests whether a provided string indicates to transpose a matrix.
*
* @private
* @param {string} str - input string
* @returns {boolean} boolean indicating whether to transpose a matrix
*
* @example
* var bool = isTransposed( 'transpose' );
* // returns true
*
* @example
* var bool = isTransposed( 'conjugate-transpose' );
* // returns true
*
* @example
* var bool = isTransposed( 'no-transpose' );
* // returns false
*/
function isTransposed( str ) {
	return ( str !== 'no-transpose' );
}
 
/**
* Tests whether to use conjugation when transposing.
*
* @private
* @param {string} str - input string
* @returns {boolean} boolean indicating whether to conjugate
*
* @example
* var bool = isConjugate( 'conjugate-transpose' );
* // returns true
*
* @example
* var bool = isConjugate( 'transpose' );
* // returns false
*/
function isConjugate( str ) {
	return ( str === 'conjugate-transpose' );
}
 
 
// MAIN //
 
/**
* Performs one of the matrix-vector operations `y = α*A*x + β*y`, `y = α*A^T*x + β*y`, or `y = α*A^H*x + β*y`, where `α` and `β` are scalars, `x` and `y` are vectors, and `A` is an `M` by `N` matrix.
*
* @private
* @param {string} trans - specifies whether `A` should be transposed, conjugate-transposed, or not transposed
* @param {NonNegativeInteger} M - number of rows in the matrix `A`
* @param {NonNegativeInteger} N - number of columns in the matrix `A`
* @param {Complex128} alpha - scalar constant
* @param {Complex128Array} A - input matrix
* @param {integer} strideA1 - stride of the first dimension of `A`
* @param {integer} strideA2 - stride of the second dimension of `A`
* @param {NonNegativeInteger} offsetA - starting index for `A`
* @param {Complex128Array} x - first input vector
* @param {integer} strideX - `x` stride length
* @param {NonNegativeInteger} offsetX - starting index for `x`
* @param {Complex128} beta - scalar constant
* @param {Complex128Array} y - second input vector
* @param {integer} strideY - `y` stride length
* @param {NonNegativeInteger} offsetY - starting index for `y`
* @returns {Complex128Array} `y`
*
* @example
* var Complex128Array = require( '@stdlib/array/complex128' );
* var Complex128 = require( '@stdlib/complex/float64/ctor' );
*
* var A = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
* var x = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
* var y = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0 ] );
*
* zgemv( 'no-transpose', 2, 3, new Complex128( 1.0, 0.0 ), A, 3, 1, 0, x, 1, 0, new Complex128( 1.0, 0.0 ), y, 1, 0 );
*/
function zgemv( trans, M, N, alpha, A, strideA1, strideA2, offsetA, x, strideX, offsetX, beta, y, strideY, offsetY ) { // eslint-disable-line max-params, max-len
	var viewA;
	var viewX;
	var viewY;
	var isrm;
	var xlen;
	var ylen;
	var tmp;
	var are;
	var aim;
	var da0;
	var da1;
	var vre;
	var vim;
	var tre;
	var tim;
	var sgn;
	var sa;
	var ix;
	var iy;
	var ia;
	var i1;
	var i0;
 
	// Note on variable naming convention: da#, i# where # corresponds to the loop number, with `0` being the innermost loop...
 
	isrm = isRowMajor( [ strideA1, strideA2 ] );
	if ( isTransposed( trans ) ) {
		xlen = M;
		ylen = N;
	} else {
		xlen = N;
		ylen = M;
	}
 
	// y = beta*y
	if ( real( beta ) === 0.0 && imag( beta ) === 0.0 ) {
		zfill( ylen, new Complex128( 0.0, 0.0 ), y, strideY, offsetY );
	} else if ( real( beta ) !== 1.0 || imag( beta ) !== 0.0 ) {
		zscal( ylen, beta, y, strideY, offsetY );
	}
	if ( real( alpha ) === 0.0 && imag( alpha ) === 0.0 ) {
		return y;
	}
 
	// Reinterpret complex arrays as real arrays:
	viewA = reinterpret( A, 0 );
	viewX = reinterpret( x, 0 );
	viewY = reinterpret( y, 0 );
 
	// Adjust strides and offsets for real arrays (each complex number = 2 doubles):
	sa = strideA1 * 2;
	strideA2 *= 2;
	strideX *= 2;
	strideY *= 2;
	offsetA *= 2;
	offsetX *= 2;
	offsetY *= 2;
 
	// Form: y = α*A*x + y
	if (
		( !isrm && !isTransposed( trans ) ) ||
		( isrm && isTransposed( trans ) )
	) {
		if ( isrm ) {
			// For row-major matrices, the last dimension has the fastest changing index...
			da0 = strideA2;                     // offset increment for innermost loop
			da1 = sa - ( ylen * strideA2 );       // offset increment for outermost loop
		} else { // isColMajor
			// For column-major matrices, the first dimension has the fastest changing index...
			da0 = sa;                           // offset increment for innermost loop
			da1 = strideA2 - ( ylen * sa );       // offset increment for outermost loop
		}
		sgn = ( isConjugate( trans ) ) ? -1.0 : 1.0;
		ia = offsetA;
		ix = offsetX;
		for ( i1 = 0; i1 < xlen; i1++ ) {
			// tmp = alpha * x[ ix ]
			tmp = mul( alpha, x.get( ix / 2 ) );
			tre = real( tmp );
			tim = imag( tmp );
 
			if ( tre !== 0.0 || tim !== 0.0 ) {
				iy = offsetY;
				for ( i0 = 0; i0 < ylen; i0++ ) {
					// y[ iy ] += A[ ia ] * tmp
					are = viewA[ ia ];
					aim = viewA[ ia + 1 ] * sgn;
					vre = viewY[ iy ];
					vim = viewY[ iy + 1 ];
 
					// Complex multiply: ( are + aim*i ) * ( tre + tim*i )
					viewY[ iy ] = vre + ( ( are * tre ) - ( aim * tim ) );
					viewY[ iy + 1 ] = vim + ( ( are * tim ) + ( aim * tre ) );
 
					iy += strideY;
					ia += da0;
				}
			} else {
				ia += da0 * ylen;
			}
			ix += strideX;
			ia += da1;
		}
		return y;
	}
 
	// Form: y = α*A^T*x + y  or  y = α*A^H*x + y
	// ( !isrm && isTransposed( trans ) ) || ( isrm && !isTransposed( trans ) )
	if ( isrm ) {
		// For row-major matrices, the last dimension has the fastest changing index...
		da0 = strideA2;                     // offset increment for innermost loop
		da1 = sa - ( xlen * strideA2 );       // offset increment for outermost loop
	} else { // isColMajor
		// For column-major matrices, the first dimension has the fastest changing index...
		da0 = sa;                           // offset increment for innermost loop
		da1 = strideA2 - ( xlen * sa );       // offset increment for outermost loop
	}
	sgn = ( isConjugate( trans ) ) ? -1.0 : 1.0;
	ia = offsetA;
	iy = offsetY;
	for ( i1 = 0; i1 < ylen; i1++ ) {
		tre = 0.0;
		tim = 0.0;
		ix = offsetX;
		for ( i0 = 0; i0 < xlen; i0++ ) {
			are = viewA[ ia ];
			aim = viewA[ ia + 1 ] * sgn;
 
			vre = viewX[ ix ];
			vim = viewX[ ix + 1 ];
 
			// tmp += A[ ia ] * x[ ix ]  (complex multiply and accumulate)
			tre += ( ( are * vre ) - ( aim * vim ) );
			tim += ( ( are * vim ) + ( aim * vre ) );
 
			ix += strideX;
			ia += da0;
		}
		// y[ iy ] += alpha * tmp
		vre = viewY[ iy ];
		vim = viewY[ iy + 1 ];
 
		// alpha * ( tre + tim*i )
		are = real( alpha );
		aim = imag( alpha );
		viewY[ iy ] = vre + ( ( are * tre ) - ( aim * tim ) );
		viewY[ iy + 1 ] = vim + ( ( are * tim ) + ( aim * tre ) );
 
		iy += strideY;
		ia += da1;
	}
	return y;
}
 
 
// EXPORTS //
 
module.exports = zgemv;