|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2026 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 scabs1 = require( '@stdlib/blas/base/scabs1' ); |
| 25 | +var cmulf = require( '@stdlib/complex/float32/base/mul' ); |
| 26 | +var caddf = require( '@stdlib/complex/float32/base/add' ); |
| 27 | +var conjf = require( '@stdlib/complex/float32/conj' ); |
| 28 | +var Complex64 = require( '@stdlib/complex/float32/ctor' ); |
| 29 | +var cfill = require( '@stdlib/blas/ext/base/cfill' ).ndarray; |
| 30 | +var cscal = require( '@stdlib/blas/base/cscal' ).ndarray; |
| 31 | + |
| 32 | + |
| 33 | +// FUNCTIONS // |
| 34 | + |
| 35 | +/** |
| 36 | +* Tests whether a provided string indicates to transpose a matrix. |
| 37 | +* |
| 38 | +* @private |
| 39 | +* @param {string} str - input string |
| 40 | +* @returns {boolean} boolean indicating whether to transpose a matrix |
| 41 | +* |
| 42 | +* @example |
| 43 | +* var bool = isTransposed( 'transpose' ); |
| 44 | +* // returns true |
| 45 | +* |
| 46 | +* @example |
| 47 | +* var bool = isTransposed( 'conjugate-transpose' ); |
| 48 | +* // returns true |
| 49 | +* |
| 50 | +* @example |
| 51 | +* var bool = isTransposed( 'no-transpose' ); |
| 52 | +* // returns false |
| 53 | +*/ |
| 54 | +function isTransposed( str ) { // NOTE: consider moving to a separate helper utility package |
| 55 | + return ( str !== 'no-transpose' ); |
| 56 | +} |
| 57 | + |
| 58 | + |
| 59 | +// MAIN // |
| 60 | + |
| 61 | +/** |
| 62 | +* Performs one of the matrix-vector operations `y = α*A*x + β*y` or `y = α*A^T*x + β*y`, where `α` and `β` are complex scalars, `x` and `y` are complex vectors, and `A` is an `M` by `N` complex matrix. |
| 63 | +* |
| 64 | +* @private |
| 65 | +* @param {string} trans - specifies whether `A` should be transposed, conjugate-transposed, or not transposed |
| 66 | +* @param {NonNegativeInteger} M - number of rows in the matrix `A` |
| 67 | +* @param {NonNegativeInteger} N - number of columns in the matrix `A` |
| 68 | +* @param {Complex64} alpha - scalar constant |
| 69 | +* @param {Complex64Array} A - input matrix |
| 70 | +* @param {integer} strideA1 - stride of the first dimension of `A` |
| 71 | +* @param {integer} strideA2 - stride of the second dimension of `A` |
| 72 | +* @param {NonNegativeInteger} offsetA - starting index for `A` |
| 73 | +* @param {Complex64Array} x - first input vector |
| 74 | +* @param {integer} strideX - `x` stride length |
| 75 | +* @param {NonNegativeInteger} offsetX - starting index for `x` |
| 76 | +* @param {Complex64} beta - scalar constant |
| 77 | +* @param {Complex64Array} y - second input vector |
| 78 | +* @param {integer} strideY - `y` stride length |
| 79 | +* @param {NonNegativeInteger} offsetY - starting index for `y` |
| 80 | +* @returns {Complex64Array} `y` |
| 81 | +* |
| 82 | +* @example |
| 83 | +* var Complex64Array = require( '@stdlib/array/complex64' ); |
| 84 | +* var Complex64 = require( '@stdlib/complex/float32/ctor' ); |
| 85 | +* |
| 86 | +* var A = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); |
| 87 | +* var x = new Complex64Array( [ 1.0, 0.0, 1.0, 0.0, 1.0, 0.0 ] ); |
| 88 | +* var y = new Complex64Array( [ 1.0, 0.0, 1.0, 0.0 ] ); |
| 89 | +* var alpha = new Complex64( 1.0, 0.0 ); |
| 90 | +* var beta = new Complex64( 1.0, 0.0 ); |
| 91 | +* |
| 92 | +* cgemv( 'no-transpose', 2, 3, alpha, A, 3, 1, 0, x, 1, 0, beta, y, 1, 0 ); |
| 93 | +* // y => <Complex64Array>[ 10.0, 12.0, 28.0, 30.0 ] |
| 94 | +*/ |
| 95 | +function cgemv( trans, M, N, alpha, A, strideA1, strideA2, offsetA, x, strideX, offsetX, beta, y, strideY, offsetY ) { // eslint-disable-line max-params, max-len |
| 96 | + var isrm; |
| 97 | + var xlen; |
| 98 | + var ylen; |
| 99 | + var tmp; |
| 100 | + var da0; |
| 101 | + var da1; |
| 102 | + var aij; |
| 103 | + var ix; |
| 104 | + var iy; |
| 105 | + var ia; |
| 106 | + var i1; |
| 107 | + var i0; |
| 108 | + |
| 109 | + isrm = isRowMajor( [ strideA1, strideA2 ] ); |
| 110 | + if ( isTransposed( trans ) ) { |
| 111 | + xlen = M; |
| 112 | + ylen = N; |
| 113 | + } else { |
| 114 | + xlen = N; |
| 115 | + ylen = M; |
| 116 | + } |
| 117 | + // y = beta*y |
| 118 | + if ( scabs1( beta ) === 0.0 ) { |
| 119 | + cfill( ylen, 0.0, y, strideY, offsetY ); |
| 120 | + } else if ( scabs1( beta ) !== 1.0 ) { |
| 121 | + cscal( ylen, beta, y, strideY, offsetY ); |
| 122 | + } |
| 123 | + if ( scabs1( alpha ) === 0.0 ) { |
| 124 | + return y; |
| 125 | + } |
| 126 | + // Form: y = α*A*x + y |
| 127 | + if ( |
| 128 | + ( !isrm && !isTransposed( trans ) ) || |
| 129 | + ( isrm && isTransposed( trans ) ) |
| 130 | + ) { |
| 131 | + if ( isrm ) { |
| 132 | + // For row-major matrices, the last dimension has the fastest changing index... |
| 133 | + da0 = strideA2; // offset increment for innermost loop |
| 134 | + da1 = strideA1 - ( ylen*strideA2 ); // offset increment for outermost loop |
| 135 | + } else { // isColMajor |
| 136 | + // For column-major matrices, the first dimension has the fastest changing index... |
| 137 | + da0 = strideA1; // offset increment for innermost loop |
| 138 | + da1 = strideA2 - ( ylen*strideA1 ); // offset increment for outermost loop |
| 139 | + } |
| 140 | + ia = offsetA; |
| 141 | + ix = offsetX; |
| 142 | + for ( i1 = 0; i1 < xlen; i1++ ) { |
| 143 | + tmp = cmulf( alpha, x.get( ix ) ); |
| 144 | + if ( scabs1( tmp ) === 0.0 ) { |
| 145 | + ia += da0 * ylen; |
| 146 | + } else { |
| 147 | + iy = offsetY; |
| 148 | + for ( i0 = 0; i0 < ylen; i0++ ) { |
| 149 | + aij = A.get( ia ); |
| 150 | + if ( trans === 'conjugate-transpose' ) { |
| 151 | + aij = conjf( aij ); |
| 152 | + } |
| 153 | + y.set( caddf( y.get( iy ), cmulf( aij, tmp ) ), iy ); |
| 154 | + iy += strideY; |
| 155 | + ia += da0; |
| 156 | + } |
| 157 | + } |
| 158 | + ix += strideX; |
| 159 | + ia += da1; |
| 160 | + } |
| 161 | + return y; |
| 162 | + } |
| 163 | + // Form: y = α*A^T*x + y |
| 164 | + |
| 165 | + // ( !isrm && isTransposed( trans ) ) || ( isrm && !isTransposed( trans ) ) |
| 166 | + if ( isrm ) { |
| 167 | + // For row-major matrices, the last dimension has the fastest changing index... |
| 168 | + da0 = strideA2; // offset increment for innermost loop |
| 169 | + da1 = strideA1 - ( xlen*strideA2 ); // offset increment for outermost loop |
| 170 | + } else { // isColMajor |
| 171 | + da0 = strideA1; // offset increment for innermost loop |
| 172 | + da1 = strideA2 - ( xlen*strideA1 ); // offset increment for outermost loop |
| 173 | + } |
| 174 | + ia = offsetA; |
| 175 | + iy = offsetY; |
| 176 | + for ( i1 = 0; i1 < ylen; i1++ ) { |
| 177 | + ix = offsetX; |
| 178 | + tmp = new Complex64( 0.0, 0.0 ); |
| 179 | + for ( i0 = 0; i0 < xlen; i0++ ) { |
| 180 | + aij = A.get( ia ); |
| 181 | + if ( trans === 'conjugate-transpose' ) { |
| 182 | + aij = conjf( aij ); |
| 183 | + } |
| 184 | + tmp = caddf( tmp, cmulf( aij, x.get( ix ) ) ); |
| 185 | + ix += strideX; |
| 186 | + ia += da0; |
| 187 | + } |
| 188 | + y.set( caddf( y.get( iy ), cmulf( alpha, tmp ) ), iy ); |
| 189 | + iy += strideY; |
| 190 | + ia += da1; |
| 191 | + } |
| 192 | + return y; |
| 193 | +} |
| 194 | + |
| 195 | + |
| 196 | +// EXPORTS // |
| 197 | + |
| 198 | +module.exports = cgemv; |
0 commit comments