-
-
Notifications
You must be signed in to change notification settings - Fork 843
feat: add lapack/base/dlascl
#7515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,285 @@ | ||
/** | ||
* @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-statements, max-len */ | ||
|
||
'use strict'; | ||
|
||
// MODULES // | ||
|
||
var isRowMajor = require( '@stdlib/ndarray/base/assert/is-row-major' ); | ||
var abs = require( '@stdlib/math/base/special/abs' ); | ||
var dlamch = require( '@stdlib/lapack/base/dlamch' ); | ||
var loopOrder = require( '@stdlib/ndarray/base/nullary-loop-interchange-order' ); | ||
var max = require( '@stdlib/math/base/special/fast/max' ); | ||
var min = require( '@stdlib/math/base/special/fast/min' ); | ||
|
||
|
||
// VARIABLES // | ||
|
||
var smlnum = dlamch( 'safe minimum' ); | ||
var bignum = 1.0 / smlnum; | ||
|
||
|
||
// MAIN // | ||
|
||
/** | ||
* Multiplies a real M by N matrix `A` by a real scalar `CTO/CFROM`. | ||
* | ||
* @private | ||
* @param {string} type - specifies the type of matrix `A` | ||
* @param {NonNegativeInteger} KL - lower band width of `A`. Referenced only if type is `symmetric-banded-lower` or `banded`. | ||
* @param {NonNegativeInteger} KU - upper band width of `A`. Referenced only if type is `symmetric-banded-upper` or `banded`. | ||
* @param {number} CFROM - the matrix `A` are multiplied by `CTO / CFROM` | ||
* @param {number} CTO - the matrix `A` are multiplied by `CTO / CFROM` | ||
* @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` | ||
* @returns {Float64Array} scaled matrix `A` | ||
* | ||
* @example | ||
* var Float64Array = require( '@stdlib/array/float64' ); | ||
* | ||
* 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 ] ] | ||
* | ||
* dlascl( 'general', 0, 0, 1.0, 2.0, 3, 2, A, 2, 1, 0 ); | ||
* // A => <Float64Array>[ 2.0, 4.0, 6.0, 8.0, 10.0, 12.0 ] | ||
*/ | ||
function dlascl( type, KL, KU, CFROM, CTO, M, N, A, strideA1, strideA2, offsetA ) { // eslint-disable-line max-params | ||
var cfromc; | ||
var cfrom1; | ||
var ctoc; | ||
var cto1; | ||
var done; | ||
var mul; | ||
var da1; | ||
var da0; | ||
var S1; | ||
var S0; | ||
var ia; | ||
var i0; | ||
var i1; | ||
var k3; | ||
var k4; | ||
var k1; | ||
var k2; | ||
var sh; | ||
var sa; | ||
var o; | ||
|
||
if ( N === 0 || M === 0 ) { | ||
return A; | ||
} | ||
|
||
done = false; | ||
|
||
cfromc = CFROM; | ||
ctoc = CTO; | ||
|
||
while ( !done ) { | ||
cfrom1 = CTO * smlnum; | ||
if ( cfrom1 === cfromc ) { | ||
// cfromc is Infinity, multiply by a correctly signed zero for finite ctoc or NaN | ||
Check warning on line 100 in lib/node_modules/@stdlib/lapack/base/dlascl/lib/base.js
|
||
mul = ctoc / cfromc; | ||
done = true; | ||
cto1 = ctoc; | ||
} else { | ||
cto1 = ctoc / bignum; | ||
if ( cto1 === ctoc ) { | ||
// ctoc is either zero or Infinity, thus ctoc itself is a correct multiplication factor | ||
Check warning on line 107 in lib/node_modules/@stdlib/lapack/base/dlascl/lib/base.js
|
||
mul = ctoc; | ||
done = true; | ||
cfromc = 1.0; | ||
} else if ( abs( cfrom1 ) > abs( ctoc ) && ctoc !== 0.0 ) { | ||
mul = smlnum; | ||
done = false; | ||
ctoc = cto1; | ||
} else if ( abs( cto1 ) > abs( cfromc ) ) { | ||
mul = bignum; | ||
done = false; | ||
ctoc = cto1; | ||
} else { | ||
mul = ctoc / cfromc; | ||
done = true; | ||
} | ||
} | ||
|
||
if ( type === 'general' ) { | ||
// Full matrix | ||
|
||
// 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 ] *= mul; | ||
ia += da0; | ||
} | ||
ia += da1; | ||
} | ||
} | ||
|
||
if ( type === 'upper' ) { | ||
// Upper triangular matrix | ||
ia = offsetA; | ||
if ( isRowMajor( [ strideA1, strideA2 ] ) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This row-major check can be lifted out of the |
||
for ( i1 = 0; i1 < M; i1++ ) { | ||
for ( i0 = i1; i0 < N; i0++ ) { | ||
A[ ia+(i0*strideA2) ] *= mul; | ||
} | ||
ia += strideA1; | ||
} | ||
} else { | ||
for ( i1 = 0; i1 < N; i1++ ) { | ||
for ( i0 = 0; i0 <= min( i1, M-1 ); i0++ ) { | ||
A[ ia+(i0*strideA1) ] *= mul; | ||
} | ||
ia += strideA2; | ||
} | ||
} | ||
} | ||
|
||
if ( type === 'lower' ) { | ||
// Lower triangular matrix | ||
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) ] *= mul; | ||
} | ||
ia += strideA1; | ||
} | ||
} else { | ||
for ( i1 = 0; i1 < N; i1++ ) { | ||
for ( i0 = i1; i0 < M; i0++ ) { | ||
A[ ia+(i0*strideA1) ] *= mul; | ||
} | ||
ia += strideA2; | ||
} | ||
} | ||
} | ||
|
||
if ( type === 'upper-hessenberg' ) { | ||
if ( isRowMajor( [ strideA1, strideA2 ] ) ) { | ||
ia = offsetA; | ||
for ( i1 = 0; i1 < M; i1++ ) { | ||
for ( i0 = 0; i0 <= min( i1+1, N-1 ); i0++ ) { | ||
A[ ia+(i0*strideA2) ] *= mul; | ||
} | ||
ia += strideA1; | ||
} | ||
} else { | ||
ia = offsetA; | ||
for ( i0 = 0; i0 < N; i0++ ) { | ||
for ( i1 = 0; i1 <= min( i0+1, M-1 ); i1++ ) { | ||
A[ ia+(i1*strideA1) ] *= mul; | ||
} | ||
ia += strideA2; | ||
} | ||
} | ||
} | ||
|
||
if ( type === 'symmetric-banded-lower' ) { | ||
if ( isRowMajor( [ strideA1, strideA2 ] ) ) { | ||
ia = offsetA; | ||
k3 = KL + 1; | ||
k4 = N; | ||
for ( i1 = 0; i1 < M; i1++ ) { | ||
for ( i0 = max( 0, i1 - KL ); i0 < min( k4, i1 + 1 ); i0++ ) { | ||
A[ ia+( ( i1-i0 ) * strideA2) ] *= mul; | ||
} | ||
ia += strideA1; | ||
} | ||
} else { | ||
ia = offsetA; | ||
for ( i1 = 0; i1 < N; i1++ ) { | ||
for ( i0 = 0; i0 < min( k3, k4 - i1 ); i0++ ) { | ||
A[ ia+(i0*strideA1) ] *= mul; | ||
} | ||
ia += strideA2; | ||
} | ||
} | ||
} | ||
|
||
if ( type === 'symmetric-banded-upper' ) { | ||
k1 = KU + 1; | ||
ia = offsetA; | ||
|
||
if ( isRowMajor( [ strideA1, strideA2 ] ) ) { | ||
for ( i1 = 0; i1 < M; i1++ ) { | ||
for ( i0 = i1; i0 < min( N, i1 + k1 ); i0++ ) { | ||
A[ ia + ( (i0-i1) * strideA2) ] *= mul; | ||
} | ||
ia += strideA1; | ||
} | ||
} else { | ||
for ( i1 = 0; i1 < N; i1++ ) { | ||
for ( i0 = max( k1 - i1, 0 ); i0 < k1; i0++ ) { | ||
A[ ia+(i0*strideA1) ] *= mul; | ||
} | ||
ia += strideA2; | ||
} | ||
} | ||
} | ||
|
||
if ( type === 'banded' ) { | ||
k1 = KL + KU + 2; | ||
k2 = KL + 1; | ||
k3 = ( 2 * KL ) + KU + 1; | ||
k4 = KL + KU + 1 + M; | ||
ia = offsetA; | ||
if ( isRowMajor( [ strideA1, strideA2 ] ) ) { | ||
for ( i1 = 0; i1 < M; i1++ ) { | ||
for ( i0 = max( 0, i1 - KL ); i0 <= min( N - 1, i1 + KU ); i0++ ) { | ||
A[ ia + ( ( i0 - i1 + KL ) * strideA2 ) ] *= mul; | ||
} | ||
ia += strideA1; | ||
} | ||
} else { | ||
for ( i1 = 0; i1 < N; i1++ ) { | ||
for ( i0 = max( k1 - i1, k2 ); i0 <= min( k3, k4-i1 ); i0++ ) { | ||
A[ ia+(i0*strideA1) ] *= mul; | ||
} | ||
ia += strideA2; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return A; | ||
} | ||
|
||
|
||
// EXPORTS // | ||
|
||
module.exports = dlascl; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/** | ||
* @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 isLayout = require( '@stdlib/blas/base/assert/is-layout' ); | ||
var isRowMajor = require( '@stdlib/ndarray/base/assert/is-row-major-string' ); | ||
var isColumnMajor = require( '@stdlib/ndarray/base/assert/is-column-major-string' ); | ||
var max = require( '@stdlib/math/base/special/max' ); | ||
var format = require( '@stdlib/string/format' ); | ||
var base = require( './base.js' ); | ||
|
||
|
||
// MAIN // | ||
|
||
/** | ||
* Multiplies a real M by N matrix `A` by a real scalar `CTO/CFROM`. | ||
* | ||
* @param {string} order - storage layout | ||
* @param {string} type - specifies the type of matrix `A` | ||
* @param {NonNegativeInteger} KL - lower band width of `A`. Referenced only if type is `symmetric-banded-lower` or `banded`. | ||
* @param {NonNegativeInteger} KU - upper band width of `A`. Referenced only if type is `symmetric-banded-upper` or `banded`. | ||
* @param {number} CFROM - the matrix `A` are multiplied by `CTO / CFROM` | ||
* @param {number} CTO - the matrix `A` are multiplied by `CTO / CFROM` | ||
* @param {NonNegativeInteger} M - number of rows in matrix `A` | ||
* @param {NonNegativeInteger} N - number of columns in matrix `A` | ||
* @param {Float64Array} A - input matrix | ||
* @param {PositiveInteger} LDA - stride of the first dimension of `A` (a.k.a., leading dimension of the matrix `A`) | ||
* @throws {TypeError} first argument must be a valid order | ||
* @throws {RangeError} fourth argument must be greater than or equal to max(1,N) | ||
* @returns {Float64Array} scaled matrix `A` | ||
* | ||
* @example | ||
* var Float64Array = require( '@stdlib/array/float64' ); | ||
* | ||
* 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 ] ] | ||
* | ||
* dlascl( 'row-major', 'general', 0, 0, 1.0, 2.0, 3, 2, A, 2 ); | ||
* // A => <Float64Array>[ 2.0, 4.0, 6.0, 8.0, 10.0, 12.0 ] | ||
*/ | ||
function dlascl( order, type, KL, KU, CFROM, CTO, M, N, A, LDA ) { | ||
var sa1; | ||
var sa2; | ||
var s; | ||
|
||
if ( !isLayout( order ) ) { | ||
throw new TypeError( format( 'invalid argument. First argument must be a valid order. Value: `%s`.', order ) ); | ||
} | ||
|
||
if ( type !== 'general' && type !== 'upper' && type !== 'lower' && type !== 'upper-hessenberg' && type !== 'symmetric-banded-lower' && type !== 'symmetric-banded-upper' && type !== 'banded' ) { | ||
throw new TypeError( format( 'invalid argument. First argument must be a valid matrix type. Value: `%s`.', type ) ); | ||
} | ||
|
||
if ( type === 'general' || type === 'upper' || type === 'lower' || type === 'upper-hessenberg' ) { | ||
if ( isRowMajor( order ) ) { | ||
s = N; | ||
} else { | ||
s = M; | ||
} | ||
if ( LDA < max( 1, s ) ) { | ||
throw new RangeError( format( 'invalid argument. Fifth argument must be greater than or equal to max(1,%d). Value: `%d`.', s, LDA ) ); | ||
} | ||
} else if ( type === 'symmetric-banded-lower' ) { | ||
if ( LDA < max( 1, KL+1 ) ) { | ||
throw new RangeError( format( 'invalid argument. Fifth argument must be greater than or equal to max(1,%d). Value: `%d`.', KL+1, LDA ) ); | ||
} | ||
} else if ( type === 'symmetric-banded-upper' ) { | ||
if ( LDA < max( 1, KU+1 ) ) { | ||
throw new RangeError( format( 'invalid argument. Fifth argument must be greater than or equal to max(1,%d). Value: `%d`.', KU+1, LDA ) ); | ||
} | ||
} else if ( LDA < max( 1, (2*KL) + KU +1 ) ) { | ||
throw new RangeError( format( 'invalid argument. Fifth argument must be greater than or equal to max(1,%d). Value: `%d`.', (2*KL) + KU +1, LDA ) ); | ||
} | ||
|
||
if ( isColumnMajor( order ) ) { | ||
sa1 = 1; | ||
sa2 = LDA; | ||
} else { // order === 'row-major' | ||
sa1 = LDA; | ||
sa2 = 1; | ||
} | ||
|
||
return base( type, KL, KU, CFROM, CTO, M, N, A, sa1, sa2, 0 ); | ||
} | ||
|
||
|
||
// EXPORTS // | ||
|
||
module.exports = dlascl; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment for below. No need to perform additional branching below if we do not need to.