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

100% Statements 328/328
100% Branches 63/63
100% Functions 3/3
100% Lines 328/328

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 3293x 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 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 3x 3x 9x 27x 27x 27x 9x 9x 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 1x 1x 1x 1x 1x 1x 3x 3x 9x 27x 27x 27x 9x 9x 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 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x 9x 9x 76x 43x 43x 43x 43x 43x 43x 43x 63x 24x 24x 24x 24x 24x 24x 24x 24x 76x 3x 76x 3x 3x 67x 76x 6x 6x 61x 61x 61x 76x 53x 76x 13x 39x 117x 117x 117x 117x 117x 117x 234x 234x 234x 117x 117x 117x 39x 13x 13x 48x 76x 28x 76x 26x 175x 10225x 10225x 495225x 495225x 495225x 10225x 505450x 505450x 505450x 10225x 10225x 10225x 175x 26x 26x 22x 76x 16x 76x 12x 133x 10099x 10099x 495099x 495099x 495099x 10099x 505198x 505198x 505198x 10099x 10099x 10099x 133x 12x 12x 10x 76x 30x 90x 90x 90x 90x 90x 90x 180x 180x 180x 90x 90x 90x 30x 10x 76x 3x 3x 3x 3x 3x  
/**
* @license Apache-2.0
*
* Copyright (c) 2025 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-params */
 
'use strict';
 
// MODULES //
 
var isRowMajor = require( '@stdlib/ndarray/base/assert/is-row-major' );
 
 
// FUNCTIONS //
 
/**
* Fills a matrix with zeros.
*
* @private
* @param {NonNegativeInteger} M - number of rows
* @param {NonNegativeInteger} N - number of columns
* @param {Float64Array} 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 {Float64Array} input matrix
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
*
* var X = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
*
* zeros( 2, 3, X, 3, 1, 0 );
* // X => <Float64Array>[ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ]
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
*
* var X = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
*
* zeros( 2, 3, X, 1, 2, 0 );
* // X => <Float64Array>[ 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;
}
 
/**
* Scales each element in a matrix by a scalar `β`.
*
* @private
* @param {NonNegativeInteger} M - number of rows
* @param {NonNegativeInteger} N - number of columns
* @param {number} beta - scalar
* @param {Float64Array} 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 {Float64Array} input matrix
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
*
* var X = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
*
* scal( 2, 3, 5.0, X, 3, 1, 0 );
* // X => <Float64Array>[ 5.0, 10.0, 15.0, 20.0, 25.0, 30.0 ]
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
*
* var X = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
*
* scal( 2, 3, 5.0, X, 1, 2, 0 );
* // X => <Float64Array>[ 5.0, 10.0, 15.0, 20.0, 25.0, 30.0 ]
*/
function scal( M, N, beta, 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 ] *= beta;
			ix += dx0;
		}
		ix += dx1;
	}
	return X;
}
 
 
// MAIN //
 
/**
* Performs the matrix-matrix operation `C = α*A*B + β*C` or `C = α*B*A + β*C` where `α` and `β` are scalars, `A` is a symmetric matrix and B and C are `M` by `N` matrices.
*
* @private
* @param {string} side - specifies whether `A` appears on the left or right of `B`
* @param {string} uplo - specifies whether the upper or lower triangular part of the symmetric matrix `A` is supplied
* @param {NonNegativeInteger} M - number of rows in the matrix `C`
* @param {NonNegativeInteger} N - number of columns in the matrix `C`
* @param {number} alpha - scalar constant
* @param {Float64Array} A - first 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 {Float64Array} B - second matrix
* @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`
* @param {number} beta - scalar constant
* @param {Float64Array} C - third matrix
* @param {integer} strideC1 - stride of the first dimension of `C`
* @param {integer} strideC2 - stride of the second dimension of `C`
* @param {NonNegativeInteger} offsetC - starting index for `C`
* @returns {Float64Array} `C`
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
*
* var A = new Float64Array( [ 1.0, 1.0, 1.0, 1.0, 2.0, 1.0, 1.0, 1.0, 3.0 ] );
* var B = new Float64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
* var C = new Float64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
*
* dsymm( 'left', 'lower', 3, 3, 1.0, A, 3, 1, 0, B, 3, 1, 0, 1.0, C, 3, 1, 0 );
* // C => <Float64Array>[ 4.0, 4.0, 4.0, 5.0, 5.0, 5.0, 6.0, 6.0, 6.0 ]
*/
function dsymm( side, uplo, M, N, alpha, A, strideA1, strideA2, offsetA, B, strideB1, strideB2, offsetB, beta, C, strideC1, strideC2, offsetC ) {
	var isrma;
	var tmp;
	var sa0;
	var sa1;
	var sb0;
	var sb1;
	var sc0;
	var sc1;
	var oa2;
	var idx;
	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 ] );
 
	// Check whether we can early return...
	if ( M === 0 || N === 0 || ( ( beta === 1.0 ) && ( alpha === 0.0 ) ) ) {
		return C;
	}
	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
		sb0 = strideB2;
		sb1 = strideB1;
		sc0 = strideC2;
		sc1 = strideC1;
	} 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
		sb0 = strideB1;
		sb1 = strideB2;
		sc0 = strideC1;
		sc1 = strideC2;
	}
	if ( beta === 0.0 ) {
		C = zeros( M, N, C, strideC1, strideC2, offsetC );
	} else if ( beta !== 1.0 ) {
		C = scal( M, N, beta, C, strideC1, strideC2, offsetC );
	}
	// Check whether we can early return...
	if ( alpha === 0.0 ) {
		return C;
	}
	oa = offsetA;
	ob = offsetB;
	if (
		( isrma && side === 'left' && uplo === 'upper' ) ||
		( !isrma && side === 'right' && uplo === 'lower' )
	) {
		for ( i = 0; i < M; i++ ) {
			for ( j = 0; j < N; j++ ) {
				tmp = 0.0;
				for ( k = 0; k < i; k++ ) {
					oa2 = A[ oa + ( k * sa1 ) + ( i * sa0 ) ];
					tmp += oa2 * B[ ob + ( k * sb1 ) + ( j * sb0 ) ];
				}
				for ( k = i; k < M; k++ ) {
					oa2 = A[ oa + ( i * sa1 ) + ( k * sa0 ) ];
					tmp += oa2 * B[ ob + ( k * sb1 ) + ( j * sb0 ) ];
				}
				idx = offsetC + ( i * sc1 ) + ( j * sc0 );
				C[ idx ] += alpha * tmp;
			}
		}
		return C;
	}
	if (
		( isrma && side === 'left' && uplo === 'lower' ) ||
		( !isrma && side === 'right' && uplo === 'upper' )
	) {
		for ( i = 0; i < M; i++ ) {
			for ( j = 0; j < N; j++ ) {
				tmp = 0.0;
				for ( k = 0; k < i; k++ ) {
					oa2 = A[ oa + ( i * sa1 ) + ( k * sa0 ) ];
					tmp += oa2 * B[ ob + ( k * sb1 ) + ( j * sb0 ) ];
				}
				for ( k = i; k < M; k++ ) {
					oa2 = A[ oa + ( k * sa1 ) + ( i * sa0 ) ];
					tmp += oa2 * B[ ob + ( k * sb1 ) + ( j * sb0 ) ];
				}
				idx = offsetC + ( i * sc1 ) + ( j * sc0 );
				C[ idx ] += alpha * tmp;
			}
		}
		return C;
	}
	if (
		( isrma && side === 'right' && uplo === 'upper' ) ||
		( !isrma && side === 'left' && uplo === 'lower' )
	) {
		for ( i = 0; i < M; i++ ) {
			for ( j = 0; j < N; j++ ) {
				tmp = 0.0;
				for ( k = 0; k < j; k++ ) {
					oa2 = A[ oa + ( k * sa1 ) + ( j * sa0 ) ];
					tmp += B[ ob + ( i * sb1 ) + ( k * sb0 ) ] * oa2;
				}
				for ( k = j; k < N; k++ ) {
					oa2 = A[ oa + ( j * sa1 ) + ( k * sa0 ) ];
					tmp += B[ ob + ( i * sb1 ) + ( k * sb0 ) ] * oa2;
				}
				idx = offsetC + ( i * sc1 ) + ( j * sc0 );
				C[ idx ] += alpha * tmp;
			}
		}
		return C;
	}
	// ( isrma && side === 'right' && uplo === 'lower' ) || ( !isrma && side === 'left' && uplo === 'upper' )
	for ( i = 0; i < M; i++ ) {
		for ( j = 0; j < N; j++ ) {
			tmp = 0.0;
			for ( k = 0; k < j; k++ ) {
				oa2 = A[ oa + ( j * sa1 ) + ( k * sa0 ) ];
				tmp += B[ ob + ( i * sb1 ) + ( k * sb0 ) ] * oa2;
			}
			for ( k = j; k < N; k++ ) {
				oa2 = A[ oa + ( k * sa1 ) + ( j * sa0 ) ];
				tmp += B[ ob + ( i * sb1 ) + ( k * sb0 ) ] * oa2;
			}
			idx = offsetC + ( i * sc1 ) + ( j * sc0 );
			C[ idx ] += alpha * tmp;
		}
	}
	return C;
}
 
 
// EXPORTS //
 
module.exports = dsymm;