All files / blas/base/strsm/lib base.js

92.5% Statements 370/400
93.54% Branches 116/124
100% Functions 2/2
92.5% Lines 370/400

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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 4003x 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 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x           2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 8x 8x 8x 4x 4x 2x 2x 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 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 4x 4x 72x 34x 34x 34x 34x 34x 34x 34x 34x 72x 34x 34x 34x 34x 34x 34x 34x 34x 68x 72x 2x 2x 2x 66x 72x 62x 72x 8x 16x 8x 8x 8x 16x 16x 16x 8x 16x 8x 8x 8x 16x 16x 16x 8x 8x 16x           16x 8x 8x 58x 72x 54x 72x 8x 16x 16x         16x 32x 32x 32x 32x 16x 16x 32x 16x 16x 16x 32x 32x 16x 8x 8x 50x 72x 46x 72x 8x 16x 32x 32x     32x 16x 8x 16x 16x 16x 16x 16x 16x 8x 16x 8x 8x 8x 16x 16x 16x 8x 16x 8x 8x 42x 72x 37x 72x 9x 18x 18x         18x 36x 36x 18x 18x 18x 36x 16x 16x 16x 36x 36x 18x 9x 9x 33x 72x 29x 72x 8x 16x 16x 32x 32x 32x 16x 16x 16x 32x 16x 16x 32x 32x 16x 8x 8x 25x 72x 21x 72x 8x 16x 32x 32x     32x 16x 8x 16x 16x 16x 16x 16x 16x 8x 16x 8x 8x 8x 16x 16x 16x 8x 16x 8x 8x 17x 72x 13x 72x 9x 18x 18x         18x 36x 36x 36x 36x 16x 16x 36x 18x 18x 18x 36x 36x 18x 9x 9x 8x 72x 16x 16x 16x 16x 8x 8x 16x 16x 8x 16x 8x 8x 8x 8x 16x 16x 8x 8x 16x         16x 8x 72x 3x 3x 3x 3x 3x
/**
* @license Apache-2.0
*
* Copyright (c) 2024 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-statements, max-depth, max-lines-per-function */
 
'use strict';
 
// MODULES //
 
var isRowMajor = require( '@stdlib/ndarray/base/assert/is-row-major' );
var f32 = require( '@stdlib/number/float64/base/to-float32' );
 
 
// FUNCTIONS
 
/**
* Fills a matrix with zeros.
*
* @private
* @param {NonNegativeInteger} M - number of rows
* @param {NonNegativeInteger} N - number of columns
* @param {Float32Array} X - matrix to fill
* @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 for `X`
* @returns {Float32Array} input matrix
*
* @example
* var Float32Array = require( '@stdlib/array/float32' );
*
* var X = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
*
* zeros( 2, 3, X, 3, 1, 0 );
* // X => <Float32Array>[ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ]
*
* @example
* var Float32Array = require( '@stdlib/array/float32' );
*
* var X = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
*
* zeros( 2, 3, X, 1, 2, 0 );
* // X => <Float32Array>[ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ]
*/
function zeros( M, N, X, strideX1, strideX2, offsetX ) { // TODO: consider moving to a separate package
	var dx0;
	var dx1;
	var S0;
	var S1;
	var i0;
	var i1;
	var ix;
 
	if ( isRowMajor( [ strideX1, strideX2 ] ) ) {
		// For row-major matrices, the last dimension has the fastest changing index...
		S0 = N;
		S1 = M;
		dx0 = strideX2;                   // offset increment for innermost loop
		dx1 = strideX1 - ( S0*strideX2 ); // offset increment for outermost loop
	} else { // column-major
		// For column-major matrices, the first dimension has the fastest changing index...
		S0 = M;
		S1 = N;
		dx0 = strideX1;                   // offset increment for innermost loop
		dx1 = strideX2 - ( S0*strideX1 ); // offset increment for outermost loop
	}
	ix = offsetX;
	for ( i1 = 0; i1 < S1; i1++ ) {
		for ( i0 = 0; i0 < S0; i0++ ) {
			X[ ix ] = 0.0;
			ix += dx0;
		}
		ix += dx1;
	}
	return X;
}
 
 
// MAIN //
 
/**
* Solve matrix equation `op(A) * X = alpha * B` or `X * op(A) = alpha * B` where `alpha` is a scalar, `X` and `B` are `M` by `N` matrices, `A` is a unit, or non-unit, upper or lower triangular matrix and `op(A)` is one of `op(A) = A` or `op(A) = A^T`. The matrix `X` is overwritten on `B`.
*
* @private
* @param {string} side - specifies whether `op( A )` appears on the left or right of `X`
* @param {string} uplo - specifies whether the upper or lower triangular part of the matrix `A` is supplied
* @param {string} transa - specifies the form of `op( A )` to be used in matrix multiplication
* @param {string} diag - specifies whether or not `A` is unit triangular
* @param {NonNegativeInteger} M - number of rows in `B`
* @param {NonNegativeInteger} N - number of columns in `B`
* @param {number} alpha - scalar constant
* @param {Float32Array} A - input matrix `A`
* @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 {Float32Array} B - input 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 for `B`
* @returns {Float32Array} `B`
*
* @example
* var Float32Array = require( '@stdlib/array/float32' );
*
* var A = new Float32Array( [ 1.0, 3.0, 0.0, 4.0 ] );
* var B = new Float32Array( [ 5.0, 6.0, 0.0, 8.0 ] );
*
* strsm( 'left', 'upper', 'no-transpose', 'non-unit', 2, 2, 6.0, A, 2, 1, 0, B, 2, 1, 0 );
* // B => <Float32Array>[ 30.0, 0.0, 0.0, 12.0 ]
*/
function strsm( side, uplo, transa, diag, M, N, alpha, A, strideA1, strideA2, offsetA, B, strideB1, strideB2, offsetB ) { // eslint-disable-line max-params
	var nonunit;
    var isrma;
    var isrmb;
	var tmp;
	var oa2;
	var ob2;
    var sa0;
    var sa1;
    var sb0;
    var sb1;
	var oa;
	var ob;
	var i;
	var j;
	var k;
 
	// Note on variable naming convention: sa#, ix#, i# where # corresponds to the loop number, with `0` being the innermost loop...
 
    isrma = isRowMajor( [ strideA1, strideA2 ] );
    isrmb = isRowMajor( [ strideB1, strideB2 ] );
	nonunit = ( diag === 'non-unit' );
 
	if ( M === 0 || N === 0 ) {
		return B;
	}
	if ( isrma ) {
		// For row-major matrices, the last dimension has the fastest changing index...
		sa0 = strideA2; // stride for innermost loop
		sa1 = strideA1; // stride for outermost loop
	} else { // isColMajor
		// For column-major matrices, the first dimension has the fastest changing index...
		sa0 = strideA1; // stride for innermost loop
		sa1 = strideA2; // stride for outermost loop
	}
    if ( isrmb ) {
		// For row-major matrices, the last dimension has the fastest changing index...
		sb0 = strideB2; // stride for innermost loop
		sb1 = strideB1; // stride for outermost loop
	} else { // isColMajor
		// For column-major matrices, the first dimension has the fastest changing index...
		sb0 = strideB1; // stride for innermost loop
		sb1 = strideB2; // stride for outermost loop
	}
 
	if ( alpha === 0.0 ) {
		zeros( M, N, B, sb0, sb1, offsetB );
		return B;
	}
	if (
		( isrma && side === 'left' && uplo === 'upper' && transa === 'no-transpose' ) ||
		( !isrma && side === 'right' && uplo === 'lower' && transa === 'no-transpose' )
	) {
		for ( k = N - 1; k >= 0; k-- ) {
			if ( nonunit ) {
				oa2 = offsetA + ( k * sa1 ) + ( k * sa0 );
				tmp = f32( 1.0 / A[ oa2 ] );
				for ( i = 0; i < M; i++ ) {
					ob2 = offsetB + ( i * sb0 ) + ( k * sb1 );
					B[ ob2 ] = f32( B[ ob2 ] * tmp );
				}
			}
			for ( j = 0; j < k; j++ ) {
				oa2 = offsetA + ( j * sa1 ) + ( k * sa0 );
				if ( A[ oa2 ] !== 0.0 ) {
					for ( i = 0; i < M; i++ ) {
						ob = offsetB + ( i * sb0 );
						B[ ob + j * sb1 ] -= f32( A[ oa2 ] * B[ ob + k * sb1 ] );
					}
				}
			}
			if ( alpha !== 1.0 ) {
				for ( i = 0; i < M; i++ ) {
					ob2 = offsetB + ( i * sb0 ) + ( k * sb1 );
					B[ ob2 ] = f32( B[ ob2 ] * alpha );
				}
			}
		}
		return B;
	}
	if (
		( isrma && side === 'left' && uplo === 'lower' && transa === 'no-transpose' ) ||
		( !isrma && side === 'right' && uplo === 'upper' && transa === 'no-transpose' )
	) {
		for ( j = 0; j < N; j++ ) {
			ob = offsetB + ( j * sb0 );
			if ( alpha !== 1.0 ) {
				for ( i = 0; i < M; i++ ) {
					B[ ob + ( i * sb1 ) ] = f32( B[ ob + ( i * sb1 ) ] * alpha );
				}
			}
			for ( k = 0; k < M; k++ ) {
				oa2 = offsetA + ( k * sa1 ) + ( k * sa0 );
				ob2 = ob + ( k * sb1 );
				if ( B[ ob2 ] !== 0.0 ) {
					if ( nonunit ) {
						B[ ob2 ] = f32( B[ ob2 ] / A[ oa2 ] );
					}
					for ( i = k + 1; i < M; i++ ) {
						oa2 = offsetA + ( i * sa1 ) + ( k * sa0 );
						B[ ob + ( i * sb1 ) ] -= f32( B[ ob2 ] * A[ oa2 ] );
					}
				}
			}
		}
		return B;
	}
	if (
		( isrma && side === 'left' && uplo === 'upper' && transa !== 'no-transpose' ) ||
		( !isrma && side === 'right' && uplo === 'lower' && transa !== 'no-transpose' )
	) {
		for ( j = 0; j < N; j++ ) {
			for ( i = 0; i < M; i++ ) {
				ob2 = offsetB + ( i * sb0 ) + ( j * sb1 );
				if ( alpha !== 1.0 ) {
					B[ ob2 ] = f32( B[ ob2 ] * alpha );
				}
			}
			for ( k = 0; k < j; k++ ) {
				for ( i = 0; i < M; i++ ) {
					ob = offsetB + ( i * sb0 );
					oa2 = offsetA + ( k * sa1 ) + ( j * sa0 );
					if ( A[ oa2 ] !== 0.0 ) {
						B[ ob + ( j * sb1 ) ] -= f32( A[ oa2 ] * B[ ob + ( k * sb1 ) ] );
					}
				}
			}
			if ( nonunit ) {
				oa2 = offsetA + ( j * sa1 ) + (j * sa0 );
				tmp = f32( 1.0 / A[ oa2 ] );
				for ( i = 0; i < M; i++ ) {
					ob2 = offsetB + ( i * sb0 ) + ( j * sb1 );
					B[ ob2 ] = f32( B[ ob2 ] * tmp );
				}
			}
		}
		return B;
	}
	if (
		( isrma && side === 'left' && uplo === 'lower' && transa !== 'no-transpose' ) ||
		( !isrma && side === 'right' && uplo === 'upper' && transa === 'transpose' )
	) {
		for ( j = 0; j < N; j++ ) {
			ob = offsetB + ( j * sb0 );
			if ( alpha !== 1.0 ) {
				for ( i = 0; i < M; i++ ) {
					B[ ob + ( i * sb1 ) ] = f32( B[ ob + ( i * sb1 ) ] * alpha );
				}
			}
			for ( i = M - 1; i >= 0; i-- ) {
				ob2 = ob + ( i * sb1 );
				for ( k = i + 1; k < M; k++ ) {
					oa2 = offsetA + ( i * sa0 ) + ( k * sa1 );
					B[ ob2 ] -= f32( A[ oa2 ] * B[ ob + ( k * sb1 ) ] );
				}
				if ( nonunit ) {
					oa2 = offsetA + ( i * sa0 ) + ( i * sa1 );
					B[ ob2 ] = f32( B[ ob2 ] / A[ oa2 ] );
				}
				B[ ob + ( i * sb1 ) ] = B[ ob2 ];
			}
		}
		return B;
	}
	if (
		( isrma && side === 'right' && uplo === 'upper' && transa === 'no-transpose' ) ||
		( !isrma && side === 'left' && uplo === 'lower' && transa === 'no-transpose' )
	) {
		for ( j = 0; j < N; j++ ) {
			ob = offsetB + ( j * sb1 );
			for ( i = 0; i < M; i++ ) {
				oa2 = offsetA + ( i * sa1 ) + ( i * sa0 );
				tmp = f32( B[ ob + ( i * sb0 ) ] * alpha );
				for ( k = 0; k < i; k++ ) {
					oa = offsetA + ( k * sa1 );
					tmp -= f32( A[ oa + ( i * sa0 ) ] * B[ ob + ( k * sb0 ) ] );
				}
				if ( nonunit ) {
					tmp = f32( tmp / A[ oa2 ] );
				}
				B[ ob + ( i * sb0 ) ] = tmp;
			}
		}
		return B;
	}
	if (
		( isrma && side === 'right' && uplo === 'lower' && transa === 'no-transpose' ) ||
		( !isrma && side === 'left' && uplo === 'upper' && transa === 'no-transpose' )
	) {
        for ( j = N - 1; j >= 0; j-- ) {
            for ( i = 0; i < M; i++ ) {
                ob2 = offsetB + ( i * sb1 ) + ( j * sb0 );
                if ( alpha !== 1.0 ) {
                    B[ ob2 ] = f32( B[ ob2 ] * alpha );
                }
            }
            for ( k = j + 1; k < N; k++ ) {
                for ( i = 0; i < M; i++ ) {
                    ob2 = offsetB + i * sb1;
                    oa2 = offsetA + k * sa1;
                    if ( A[ oa2 + j * sa0 ] !== 0.0 ) {
                        B[ ob2 + j * sb0 ] -= f32( A[ oa2 + j * sa0 ] * B[ ob2 + k * sb0 ] );
                    }
                }
            }
            if ( nonunit ) {
                oa2 = offsetA + j * sa1 + j * sa0;
                tmp = f32( 1.0 / A[ oa2 ] );
                for ( i = 0; i < M; i++ ) {
                    ob2 = offsetB + ( i * sb1 ) + ( j * sb0 );
                    B[ ob2 ] = f32( B[ ob2 ] * tmp );
                }
            }
        }
        return B;
    }
	if (
		( isrma && side === 'right' && uplo === 'upper' && transa !== 'no-transpose' ) ||
		( !isrma && side === 'left' && uplo === 'lower' && transa !== 'no-transpose' )
	) {
		for ( j = 0; j < N; j++ ) {
			ob = offsetB + ( j * sb1 );
			if ( alpha !== 1.0 ) {
				for ( i = 0; i < M; i++ ) {
					B[ ob + ( i * sb0 ) ] = f32( B[ ob + ( i * sb0 ) ] * alpha );
				}
			}
			for ( k = M - 1; k >= 0; k-- ) {
				oa2 = offsetA + ( k * sa1 ) + ( k * sa0 );
				ob2 = ob + ( k * sb0 );
				if ( B[ ob2 ] !== 0.0 ) {
					if ( nonunit ) {
						B[ ob2 ] = f32( B[ ob2 ] / A[ oa2 ] );
					}
					for ( i = 0; i < k; i++ ) {
						oa = offsetA + ( i * sa1 ) + ( k * sa0 );
						B[ ob + ( i * sb0 ) ] -= f32( B[ ob2 ] * A[ oa ] );
					}
				}
			}
		}
		return B;
	}
	// ( isrma && side === 'right' && uplo === 'lower' && transa !== 'no-transpose' ) || ( !isrma && side === 'left' && uplo === 'upper' && transa !== 'no-transpose' )
	for ( k = 0; k < N; k++ ) {
		ob = offsetB + ( k * sb0 );
		oa = offsetA + ( k * sa1 );
		oa2 = oa + ( k * sa0 );
		if ( nonunit ) {
			tmp = f32( 1.0 / A[ oa2 ] );
			for ( i = 0; i < M; i++ ) {
				B[ ob + ( i * sb1 ) ] = f32( B[ ob + ( i * sb1 ) ] * tmp );
			}
		}
		for ( j = k + 1; j < N; j++ ) {
			ob2 = offsetB + ( j * sb0 );
			oa2 = offsetA + j * sa1 + k * sa0;
			if ( A[ oa2 ] !== 0.0 ) {
				for ( i = 0; i < M; i++ ) {
					B[ ob2 + ( i * sb1 ) ] -= f32( A[ oa2 ] * B[ ob + ( i * sb1 ) ] );
				}
			}
		}
		if ( alpha !== 1.0 ) {
			for ( i = 0; i < M; i++ ) {
				B[ ob + ( i * sb1 ) ] = f32( B[ ob + ( i * sb1 ) ] * alpha );
			}
		}
	}
	return B;
}
 
 
// EXPORTS //
 
module.exports = strsm;