-
-
Notifications
You must be signed in to change notification settings - Fork 844
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
Open
aayush0325
wants to merge
22
commits into
stdlib-js:develop
Choose a base branch
from
aayush0325:lapack-dlascl
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+10,615
−0
Open
Changes from 3 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
10e8866
feat: add lapack/base/dlascl
aayush0325 5e0cb56
feat: add main exports
aayush0325 543cbc7
test: add initial tests
aayush0325 6420a10
test: update imports
aayush0325 9e43ff5
test: add some tests
aayush0325 9cf127e
test: update lda validation
aayush0325 938d704
docs: add ts files
aayush0325 ec09f0f
docs: add examples
aayush0325 1d595e0
docs: add readme
aayush0325 f72cc1b
docs: add readme
aayush0325 213300c
docs: add package.json
aayush0325 bd61b1e
docs: add repl.txt
aayush0325 cd982b0
bench: add benchmarks
aayush0325 e833c89
test: add tests
aayush0325 1b9c79e
test: add tests
aayush0325 31b4286
test: add all tests
aayush0325 1940240
test: add ndarray tests
aayush0325 f60ca96
chore: cleanup
aayush0325 2f26be3
refactor: split base into functions
aayush0325 b30f33f
refactor: add cont statements
aayush0325 382580e
Merge remote-tracking branch 'upstream/develop' into lapack-dlascl
stdlib-bot bd9df6b
refactor: optimise loops
aayush0325 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
285 changes: 285 additions & 0 deletions
285
lib/node_modules/@stdlib/lapack/base/dlascl/lib/base.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ] ) ) { | ||
aayush0325 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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; |
106 changes: 106 additions & 0 deletions
106
lib/node_modules/@stdlib/lapack/base/dlascl/lib/dlascl.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.