All files / lapack/base/dlaset/lib base.js

100% Statements 237/237
100% Branches 23/23
100% Functions 4/4
100% Lines 237/237

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 2383x 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 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 36x 108x 108x 108x 36x 36x 12x 12x 12x 36x 36x 36x 12x 12x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 12x 12x 12x 12x 12x 12x 12x 6x 18x 36x 36x 18x 18x 6x 6x 18x 36x 36x 18x 18x 6x 12x 12x 12x 36x 36x 36x 12x 12x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 12x 12x 12x 12x 12x 12x 12x 6x 18x 36x 36x 18x 18x 6x 6x 18x 36x 36x 18x 18x 6x 12x 12x 36x 36x 36x 12x 12x 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 36x 36x 12x 12x 36x 12x 12x 12x 36x 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.
*/
 
'use strict';
 
// MODULES //
 
var isRowMajor = require( '@stdlib/ndarray/base/assert/is-row-major' );
var loopOrder = require( '@stdlib/ndarray/base/nullary-loop-interchange-order' );
var min = require( '@stdlib/math/base/special/fast/min' );
 
 
// FUNCTIONS //
 
/**
* Initialises an M by N matrix `A` to `beta` on the diagonals and `alpha` on the off diagonals, setting all of matrix `A`.
*
* @private
* @param {NonNegativeInteger} M - number of rows in matrix `A`
* @param {NonNegativeInteger} N - number of columns in matrix `A`
* @param {Float64Array} 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 {number} alpha - number to set on off diagonal elements
* @param {number} beta - number to set on diagonal elements
* @returns {Float64Array} `A`
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
*
* var A = new Float64Array( 4 );
*
* setAll( 2, 2, A, 2, 1, 0, 0.0, 1.0 );
* // A => <Float64Array>[ 1.0, 0.0, 0.0, 1.0 ]
*/
function setAll( M, N, A, strideA1, strideA2, offsetA, alpha, beta ) {
	var da0;
	var da1;
	var sh;
	var S0;
	var S1;
	var sa;
	var ia;
	var i0;
	var i1;
	var o;
 
	// Resolve the loop interchange order:
	o = loopOrder( [ M, N ], [ strideA1, strideA2 ] );
	sh = o.sh;
	sa = o.sx;
 
	// Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments...
	S0 = sh[ 0 ];
	S1 = sh[ 1 ];
	da0 = sa[ 0 ];
	da1 = sa[ 1 ] - ( S0*sa[0] );
 
	// Set the pointers to the first indexed elements in the respective matrices...
	ia = offsetA;
 
	// Iterate over the matrix dimensions...
	for ( i1 = 0; i1 < S1; i1++ ) {
		for ( i0 = 0; i0 < S0; i0++ ) {
			A[ ia ] = alpha;
			ia += da0;
		}
		ia += da1;
	}
 
	ia = offsetA;
	for ( i1 = 0; i1 < min( M, N ); i1++ ) {
		A[ ia ] = beta;
		ia += strideA1 + strideA2;
	}
	return A;
}
 
/**
* Initialises an M by N matrix `A` to `beta` on the diagonals and `alpha` on the off diagonals, setting upper triangular elements of `A`.
*
* @private
* @param {NonNegativeInteger} M - number of rows in matrix `A`
* @param {NonNegativeInteger} N - number of columns in matrix `A`
* @param {Float64Array} 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 {number} alpha - number to set on off diagonal elements
* @param {number} beta - number to set on diagonal elements
* @returns {Float64Array} `A`
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
*
* var A = new Float64Array( 4 );
*
* setUpper( 2, 2, A, 2, 1, 0, 1.0, 2.0 );
* // A => <Float64Array>[ 2.0, 1.0, 0.0, 2.0 ]
*/
function setUpper( M, N, A, strideA1, strideA2, offsetA, alpha, beta ) {
	var ia;
	var i0;
	var i1;
 
	ia = offsetA;
	if ( isRowMajor( [ strideA1, strideA2 ] ) ) {
		for ( i1 = 0; i1 < M; i1++ ) {
			for ( i0 = i1; i0 < N; i0++ ) {
				A[ ia+(i0*strideA2) ] = alpha;
			}
			ia += strideA1;
		}
	} else {
		for ( i1 = 0; i1 < N; i1++ ) {
			for ( i0 = 0; i0 <= min( i1, M-1 ); i0++ ) {
				A[ ia+(i0*strideA1) ] = alpha;
			}
			ia += strideA2;
		}
	}
 
	ia = offsetA;
	for ( i1 = 0; i1 < min( M, N ); i1++ ) {
		A[ ia ] = beta;
		ia += strideA1 + strideA2;
	}
	return A;
}
 
/**
* Initialises an M by N matrix `A` to `beta` on the diagonals and `alpha` on the off diagonals, setting upper triangular elements of `A`.
*
* @private
* @param {NonNegativeInteger} M - number of rows in matrix `A`
* @param {NonNegativeInteger} N - number of columns in matrix `A`
* @param {Float64Array} 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 {number} alpha - number to set on off diagonal elements
* @param {number} beta - number to set on diagonal elements
* @returns {Float64Array} `A`
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
*
* var A = new Float64Array( 4 );
*
* setLower( 2, 2, A, 2, 1, 0, 1.0, 2.0 );
* // A => <Float64Array>[ 2.0, 0.0, 1.0, 2.0 ]
*/
function setLower( M, N, A, strideA1, strideA2, offsetA, alpha, beta ) {
	var ia;
	var i0;
	var i1;
 
	ia = offsetA;
	if ( isRowMajor( [ strideA1, strideA2 ] ) ) {
		for ( i1 = 0; i1 < M; i1++ ) {
			for ( i0 = 0; i0 <= min( i1, N-1 ); i0++ ) {
				A[ ia+(i0*strideA2) ] = alpha;
			}
			ia += strideA1;
		}
	} else {
		for ( i1 = 0; i1 < N; i1++ ) {
			for ( i0 = i1; i0 < M; i0++ ) {
				A[ ia+(i0*strideA1) ] = alpha;
			}
			ia += strideA2;
		}
	}
	ia = offsetA;
	for ( i1 = 0; i1 < min( M, N ); i1++ ) {
		A[ ia ] = beta;
		ia += strideA1 + strideA2;
	}
	return A;
}
 
 
// MAIN //
 
/**
* Initialises an M by N matrix `A` to `beta` on the diagonals and `alpha` on the off diagonals.
*
* @private
* @param {string} uplo - specifies whether to set the upper or lower triangular/trapezoidal part of matrix `A`
* @param {NonNegativeInteger} M - number of rows in matrix `A`
* @param {NonNegativeInteger} N - number of columns in matrix `A`
* @param {Float64Array} 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 {number} alpha - number to set on off diagonal elements
* @param {number} beta - number to set on diagonal elements
* @returns {Float64Array} `A`
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
*
* var A = new Float64Array( 4 );
*
* dlaset( 'all', 2, 2, A, 2, 1, 0, 0.0, 1.0 );
* // A => <Float64Array>[ 1.0, 0.0, 0.0, 1.0 ]
*/
function dlaset( uplo, M, N, A, strideA1, strideA2, offsetA, alpha, beta ) {
	if ( uplo === 'upper' ) {
		return setUpper( M, N, A, strideA1, strideA2, offsetA, alpha, beta );
	}
	if ( uplo === 'lower' ) {
		return setLower( M, N, A, strideA1, strideA2, offsetA, alpha, beta );
	}
	return setAll( M, N, A, strideA1, strideA2, offsetA, alpha, beta );
}
 
 
// EXPORTS //
 
module.exports = dlaset;