|
| 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 | +/* eslint-disable max-statements, max-len */ |
| 20 | + |
| 21 | +'use strict'; |
| 22 | + |
| 23 | +// MODULES // |
| 24 | + |
| 25 | +var isRowMajor = require( '@stdlib/ndarray/base/assert/is-row-major' ); |
| 26 | +var abs = require( '@stdlib/math/base/special/abs' ); |
| 27 | +var dlamch = require( '@stdlib/lapack/base/dlamch' ); |
| 28 | +var loopOrder = require( '@stdlib/ndarray/base/nullary-loop-interchange-order' ); |
| 29 | +var max = require( '@stdlib/math/base/special/fast/max' ); |
| 30 | +var min = require( '@stdlib/math/base/special/fast/min' ); |
| 31 | + |
| 32 | + |
| 33 | +// VARIABLES // |
| 34 | + |
| 35 | +var smlnum = dlamch( 'safe minimum' ); |
| 36 | +var bignum = 1.0 / smlnum; |
| 37 | + |
| 38 | + |
| 39 | +// MAIN // |
| 40 | + |
| 41 | +/** |
| 42 | +* Performs a series of row interchanges on a matrix `A` using pivot indices stored in `IPIV`. |
| 43 | +* |
| 44 | +* @private |
| 45 | +* @param {string} type - specifies the type of matrix `A` |
| 46 | +* @param {NonNegativeInteger} KL - lower band width of `A`. Referenced only if type is `symmetric-banded-lower` or `banded`. |
| 47 | +* @param {NonNegativeInteger} KU - upper band width of `A`. Referenced only if type is `symmetric-banded-upper` or `banded`. |
| 48 | +* @param {number} CFROM - selected elements in `A` are multiplied by `CTO / CFROM` |
| 49 | +* @param {number} CTO - selected elements in `A` are multiplied by `CTO / CFROM` |
| 50 | +* @param {NonNegativeInteger} M - number of rows in matrix `A` |
| 51 | +* @param {NonNegativeInteger} N - number of columns in matrix `A` |
| 52 | +* @param {Float64Array} A - input matrix |
| 53 | +* @param {integer} strideA1 - stride of the first dimension of `A` |
| 54 | +* @param {integer} strideA2 - stride of the second dimension of `A` |
| 55 | +* @param {NonNegativeInteger} offsetA - starting index for `A` |
| 56 | +* @returns {Float64Array} scaled matrix `A` |
| 57 | +* |
| 58 | +* @example |
| 59 | +* var Float64Array = require( '@stdlib/array/float64' ); |
| 60 | +* |
| 61 | +* var A = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); // => [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] |
| 62 | +* |
| 63 | +* dlascl( 'general', 0, 0, 1.0, 2.0, 3, 2, A, 2, 1, 0 ); |
| 64 | +* // A => <Float64Array>[ 2.0, 4.0, 6.0, 8.0, 10.0, 12.0 ] |
| 65 | +*/ |
| 66 | +function dlascl( type, KL, KU, CFROM, CTO, M, N, A, strideA1, strideA2, offsetA ) { // eslint-disable-line max-params |
| 67 | + var cfromc; |
| 68 | + var cfrom1; |
| 69 | + var ctoc; |
| 70 | + var cto1; |
| 71 | + var done; |
| 72 | + var mul; |
| 73 | + var da1; |
| 74 | + var da0; |
| 75 | + var S1; |
| 76 | + var S0; |
| 77 | + var ia; |
| 78 | + var i0; |
| 79 | + var i1; |
| 80 | + var k3; |
| 81 | + var k4; |
| 82 | + var k1; |
| 83 | + var k2; |
| 84 | + var sh; |
| 85 | + var sa; |
| 86 | + var o; |
| 87 | + |
| 88 | + if ( N === 0 || M === 0 ) { |
| 89 | + return A; |
| 90 | + } |
| 91 | + |
| 92 | + done = false; |
| 93 | + |
| 94 | + cfromc = CFROM; |
| 95 | + ctoc = CTO; |
| 96 | + |
| 97 | + while ( !done ) { |
| 98 | + cfrom1 = CTO * smlnum; |
| 99 | + if ( cfrom1 === cfromc ) { |
| 100 | + // cfromc is Infinity, multiply by a correctly signed zero for finite ctoc or NaN |
| 101 | + mul = ctoc / cfromc; |
| 102 | + done = true; |
| 103 | + cto1 = ctoc; |
| 104 | + } else { |
| 105 | + cto1 = ctoc / bignum; |
| 106 | + if ( cto1 === ctoc ) { |
| 107 | + // ctoc is either zero or Infinity, thus ctoc itself is a correct multiplication factor |
| 108 | + mul = ctoc; |
| 109 | + done = true; |
| 110 | + cfromc = 1.0; |
| 111 | + } else if ( abs( cfrom1 ) > abs( ctoc ) && ctoc !== 0.0 ) { |
| 112 | + mul = smlnum; |
| 113 | + done = false; |
| 114 | + ctoc = cto1; |
| 115 | + } else if ( abs( cto1 ) > abs( cfromc ) ) { |
| 116 | + mul = bignum; |
| 117 | + done = false; |
| 118 | + ctoc = cto1; |
| 119 | + } else { |
| 120 | + mul = ctoc / cfromc; |
| 121 | + done = true; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + if ( type === 'general' ) { |
| 126 | + // Full matrix |
| 127 | + |
| 128 | + // Resolve the loop interchange order: |
| 129 | + o = loopOrder( [ M, N ], [ strideA1, strideA2 ] ); |
| 130 | + sh = o.sh; |
| 131 | + sa = o.sx; |
| 132 | + |
| 133 | + // Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments... |
| 134 | + S0 = sh[ 0 ]; |
| 135 | + S1 = sh[ 1 ]; |
| 136 | + da0 = sa[ 0 ]; |
| 137 | + da1 = sa[ 1 ] - ( S0*sa[0] ); |
| 138 | + |
| 139 | + // Set the pointers to the first indexed elements in the respective matrices... |
| 140 | + ia = offsetA; |
| 141 | + |
| 142 | + // Iterate over the matrix dimensions... |
| 143 | + for ( i1 = 0; i1 < S1; i1++ ) { |
| 144 | + for ( i0 = 0; i0 < S0; i0++ ) { |
| 145 | + A[ ia ] *= mul; |
| 146 | + ia += da0; |
| 147 | + } |
| 148 | + ia += da1; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + if ( type === 'upper' ) { |
| 153 | + // Upper triangular matrix |
| 154 | + ia = offsetA; |
| 155 | + if ( isRowMajor( [ strideA1, strideA2 ] ) ) { |
| 156 | + for ( i1 = 0; i1 < M; i1++ ) { |
| 157 | + for ( i0 = i1; i0 < N; i0++ ) { |
| 158 | + A[ ia+(i0*strideA2) ] *= mul; |
| 159 | + } |
| 160 | + ia += strideA1; |
| 161 | + } |
| 162 | + } else { |
| 163 | + for ( i1 = 0; i1 < N; i1++ ) { |
| 164 | + for ( i0 = 0; i0 <= min( i1, M-1 ); i0++ ) { |
| 165 | + A[ ia+(i0*strideA1) ] *= mul; |
| 166 | + } |
| 167 | + ia += strideA2; |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + if ( type === 'lower' ) { |
| 173 | + // Lower triangular matrix |
| 174 | + ia = offsetA; |
| 175 | + if ( isRowMajor( [ strideA1, strideA2 ] ) ) { |
| 176 | + for ( i1 = 0; i1 < M; i1++ ) { |
| 177 | + for ( i0 = 0; i0 <= min( i1, N-1 ); i0++ ) { |
| 178 | + A[ ia+(i0*strideA2) ] *= mul; |
| 179 | + } |
| 180 | + ia += strideA1; |
| 181 | + } |
| 182 | + } else { |
| 183 | + for ( i1 = 0; i1 < N; i1++ ) { |
| 184 | + for ( i0 = i1; i0 < M; i0++ ) { |
| 185 | + A[ ia+(i0*strideA1) ] *= mul; |
| 186 | + } |
| 187 | + ia += strideA2; |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + if ( type === 'upper-hessenberg' ) { |
| 193 | + if ( isRowMajor( [ strideA1, strideA2 ] ) ) { |
| 194 | + ia = offsetA; |
| 195 | + for ( i1 = 0; i1 < M; i1++ ) { |
| 196 | + for ( i0 = 0; i0 <= min( i1+1, N-1 ); i0++ ) { |
| 197 | + A[ ia+(i0*strideA2) ] *= mul; |
| 198 | + } |
| 199 | + ia += strideA1; |
| 200 | + } |
| 201 | + } else { |
| 202 | + ia = offsetA; |
| 203 | + for ( i0 = 0; i0 < N; i0++ ) { |
| 204 | + for ( i1 = 0; i1 <= min( i0+1, M-1 ); i1++ ) { |
| 205 | + A[ ia+(i1*strideA1) ] *= mul; |
| 206 | + } |
| 207 | + ia += strideA2; |
| 208 | + } |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + if ( type === 'symmetric-banded-lower' ) { |
| 213 | + if ( isRowMajor( [ strideA1, strideA2 ] ) ) { |
| 214 | + ia = offsetA; |
| 215 | + k3 = KL + 1; |
| 216 | + k4 = N; |
| 217 | + for ( i1 = 0; i1 < M; i1++ ) { |
| 218 | + for ( i0 = max( 0, i1 - KL ); i0 < min( k4, i1 + 1 ); i0++ ) { |
| 219 | + A[ ia+( ( i1-i0 ) * strideA2) ] *= mul; |
| 220 | + } |
| 221 | + ia += strideA1; |
| 222 | + } |
| 223 | + } else { |
| 224 | + ia = offsetA; |
| 225 | + for ( i1 = 0; i1 < N; i1++ ) { |
| 226 | + for ( i0 = 0; i0 < min( k3, k4 - i1 ); i0++ ) { |
| 227 | + A[ ia+(i0*strideA1) ] *= mul; |
| 228 | + } |
| 229 | + ia += strideA2; |
| 230 | + } |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + if ( type === 'symmetric-banded-upper' ) { |
| 235 | + k1 = KU + 1; |
| 236 | + ia = offsetA; |
| 237 | + |
| 238 | + if ( isRowMajor( [ strideA1, strideA2 ] ) ) { |
| 239 | + for ( i1 = 0; i1 < M; i1++ ) { |
| 240 | + for ( i0 = i1; i0 < min( N, i1 + k1 ); i0++ ) { |
| 241 | + A[ ia + ( (i0-i1) * strideA2) ] *= mul; |
| 242 | + } |
| 243 | + ia += strideA1; |
| 244 | + } |
| 245 | + } else { |
| 246 | + for ( i1 = 0; i1 < N; i1++ ) { |
| 247 | + for ( i0 = max( k1 - i1, 0 ); i0 < k1; i0++ ) { |
| 248 | + A[ ia+(i0*strideA1) ] *= mul; |
| 249 | + } |
| 250 | + ia += strideA2; |
| 251 | + } |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | + if ( type === 'banded' ) { |
| 256 | + k1 = KL + KU + 2; |
| 257 | + k2 = KL + 1; |
| 258 | + k3 = ( 2 * KL ) + KU + 1; |
| 259 | + k4 = KL + KU + 1 + M; |
| 260 | + ia = offsetA; |
| 261 | + if ( isRowMajor( [ strideA1, strideA2 ] ) ) { |
| 262 | + for ( i1 = 0; i1 < M; i1++ ) { |
| 263 | + for ( i0 = max( 0, i1 - KL ); i0 <= min( N - 1, i1 + KU ); i0++ ) { |
| 264 | + A[ ia + ( ( i0 - i1 + KL ) * strideA2 ) ] *= mul; |
| 265 | + } |
| 266 | + ia += strideA1; |
| 267 | + } |
| 268 | + } else { |
| 269 | + for ( i1 = 0; i1 < N; i1++ ) { |
| 270 | + for ( i0 = max( k1 - i1, k2 ); i0 <= min( k3, k4-i1 ); i0++ ) { |
| 271 | + A[ ia+(i0*strideA1) ] *= mul; |
| 272 | + } |
| 273 | + ia += strideA2; |
| 274 | + } |
| 275 | + } |
| 276 | + } |
| 277 | + } |
| 278 | + |
| 279 | + return A; |
| 280 | +} |
| 281 | + |
| 282 | + |
| 283 | +// EXPORTS // |
| 284 | + |
| 285 | +module.exports = dlascl; |
0 commit comments