Skip to content

feat: add lapack/base/dlasr#12643

Open
prajjwalbajpai wants to merge 1 commit into
stdlib-js:developfrom
prajjwalbajpai:dlasr
Open

feat: add lapack/base/dlasr#12643
prajjwalbajpai wants to merge 1 commit into
stdlib-js:developfrom
prajjwalbajpai:dlasr

Conversation

@prajjwalbajpai
Copy link
Copy Markdown
Contributor

Description

What is the purpose of this pull request?

This pull request:

  • adds lapack/base/dlasr

Related Issues

Does this pull request have any related issues?

No.

Questions

Any questions for reviewers of this pull request?

No.

Other

Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.

No.

Checklist

Please ensure the following tasks are completed before submitting this pull request.

AI Assistance

When authoring the changes proposed in this PR, did you use any kind of AI assistance?

  • Yes
  • No

If you answered "yes" above, how did you use AI assistance?

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

Disclosure

If you answered "yes" to using AI assistance, please provide a short disclosure indicating how you used AI assistance. This helps reviewers determine how much scrutiny to apply when reviewing your contribution. Example disclosures: "This PR was written primarily by Claude Code." or "I consulted ChatGPT to understand the codebase, but the proposed changes were fully authored manually by myself.".

I consulted ChatGPT for testcases to get 100% code coverage.


@stdlib-js/reviewers

---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
  - task: lint_filenames
    status: passed
  - task: lint_editorconfig
    status: passed
  - task: lint_markdown_pkg_readmes
    status: passed
  - task: lint_markdown_docs
    status: na
  - task: lint_markdown
    status: na
  - task: lint_package_json
    status: passed
  - task: lint_repl_help
    status: passed
  - task: lint_javascript_src
    status: passed
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: passed
  - task: lint_javascript_tests
    status: passed
  - task: lint_javascript_benchmarks
    status: passed
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: na
  - task: lint_c_examples
    status: na
  - task: lint_c_benchmarks
    status: na
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: passed
  - task: lint_typescript_tests
    status: passed
  - task: lint_license_headers
    status: passed
---
@prajjwalbajpai prajjwalbajpai requested a review from a team June 6, 2026 14:18
@stdlib-bot stdlib-bot added LAPACK Issue or pull request related to the Linear Algebra Package (LAPACK). Needs Review A pull request which needs code review. labels Jun 6, 2026
@prajjwalbajpai prajjwalbajpai added GSoC Google Summer of Code. gsoc: 2026 Google Summer of Code (2026). labels Jun 6, 2026
@stdlib-bot
Copy link
Copy Markdown
Contributor

Coverage Report

Package Statements Branches Functions Lines
lapack/base/dlasr $\color{green}719/719$
$\color{green}+100.00\%$
$\color{green}109/109$
$\color{green}+100.00\%$
$\color{green}7/7$
$\color{green}+100.00\%$
$\color{green}719/719$
$\color{green}+100.00\%$

The above coverage report was generated for the changes in this PR.

Comment on lines +40 to +45
function isPivot( pivot ) {
if ( pivot === 'variable' || pivot === 'top' || pivot === 'bottom' ) {
return true;
}
return false;
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No explicit functions like this. Use array/base/assert/contains to generate an assertion function similar to

var contains = require( '@stdlib/array/base/assert/contains' ).factory;

var isPivot = contains( [ 'variable', 'top', 'bottom' ] );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, given that this is duplicated in ndarray.js, you should move it to a separate file is_pivot.js so that it can be imported in both implementations.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same applies to isDirection.

Copy link
Copy Markdown
Member

@kgryte kgryte Jun 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ultimately, we should move these assertion utilities to separate independent packages, so moving to a separate file is a step in that direction.

Comment on lines +118 to +121
if ( ( isColumnMajor( order ) ) && LDA < max( 1, M ) ) {
throw new RangeError( format( 'invalid argument. Tenth argument must be greater than or equal to max(1,`M`). Value: `%d`.', LDA ) );
}
if ( order === 'column-major' ) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if ( ( isColumnMajor( order ) ) && LDA < max( 1, M ) ) {
throw new RangeError( format( 'invalid argument. Tenth argument must be greater than or equal to max(1,`M`). Value: `%d`.', LDA ) );
}
if ( order === 'column-major' ) {
if ( isColumnMajor( order ) ) {
if ( LDA < max( 1, M ) ) {
throw new RangeError( format( 'invalid argument. Tenth argument must be greater than or equal to max(1,`M`). Value: `%d`.', LDA ) );
}

Consolidate your conditionals (a) and (b) why are you comparing a string literal when you use isColumnMajor in the conditional just before? We try to avoid explicit string comparisons for a reason and defer to dedicated utilities. Why? Because string literals are a significant source of bundle bloat, as they cannot be minified away.

if ( order === 'column-major' ) {
sa1 = 1;
sa2 = LDA;
} else {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have an LDA check for column-major. There is not a similar check for row-major? I would double-check that is the case by looking at LAPACKE.

Comment on lines +137 to +139
Similarly, when `PIVOT = 'bottom'` (bottom pivot), the rotation is performed for the
plane `(k,z)`, where `z = M` if `SIDE = 'L'` and `z = N` if `SIDE = 'R'`.
In this case, `P(k)` has the form
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Similarly, when `PIVOT = 'bottom'` (bottom pivot), the rotation is performed for the
plane `(k,z)`, where `z = M` if `SIDE = 'L'` and `z = N` if `SIDE = 'R'`.
In this case, `P(k)` has the form
Similarly, when `PIVOT = 'bottom'` (bottom pivot), the rotation is performed for the plane `(k,z)`, where `z = M` if `SIDE = 'L'` and `z = N` if `SIDE = 'R'`. In this case, `P(k)` has the form

Don't line wrap Markdown.

var A = new Float64Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] );

var out = dlasr( 'column-major', 'left', 'variable', 'forward', 4, 3, C, S, A, 4 );
// returns <Float64Array>[ 2, ~-1.443, ~4.276, ~2.373, 7.6, ~-3.74, ~9.054, ~4.504, 13.2, ~-6.037, ~13.832, ~6.634 ]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// returns <Float64Array>[ 2, ~-1.443, ~4.276, ~2.373, 7.6, ~-3.74, ~9.054, ~4.504, 13.2, ~-6.037, ~13.832, ~6.634 ]
// returns <Float64Array>[ 2.0, ~-1.443, ~4.276, ~2.373, 7.6, ~-3.74, ~9.054, ~4.504, 13.2, ~-6.037, ~13.832, ~6.634 ]

var S = new Float64Array( S0.buffer, A0.BYTES_PER_ELEMENT*1 ); // start at 2nd element

dlasr( 'column-major', 'left', 'variable', 'forward', 3, 2, C, S, A, 3 );
// A0 => <Float64Array>[ 0, 2, ~-1.443, ~2.814, 6.2, ~-3.166, ~5.343 ]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// A0 => <Float64Array>[ 0, 2, ~-1.443, ~2.814, 6.2, ~-3.166, ~5.343 ]
// A0 => <Float64Array>[ 0.0, 2.0, ~-1.443, ~2.814, 6.2, ~-3.166, ~5.343 ]

This is a Float64Array. Array contents should always be documented as decimal values.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applies here and throughout this PR.

Comment on lines +76 to +96
// Variable pivot, forward direction
idx0 = offsetC;
idx1 = offsetS;
idx2 = offsetA;
for ( j = 0; j < M - 1; j++ ) {
ctemp = C[ idx0 ];
stemp = S[ idx1 ];
if ( ctemp !== 1.0 || stemp !== 0.0 ) {
idx3 = idx2;
for ( i = 0; i < N; i++ ) {
idx4 = idx3 + strideA1;
temp = A[ idx4 ];
A[ idx4 ] = ( ctemp * temp ) - ( stemp * A[ idx3 ] );
A[ idx3 ] = ( stemp * temp ) + ( ctemp * A[ idx3 ] );
idx3 += strideA2;
}
}
idx0 += strideC;
idx1 += strideS;
idx2 += strideA1;
}
Copy link
Copy Markdown
Member

@kgryte kgryte Jun 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it is, this function is going to be nearly impossible for an optimizing compiler to optimize due to all the nested conditionals. Instead, move each of the loop logic to a function in this file, using the naming convention sidePivotDirection. So, here,

function leftVariableForward( M, N, C, strideC, offsetC, S, strideS, offsetS, A, strideA1, strideA2, offsetA ) {
	var ctemp;
	var stemp;
	var temp;
	var idx0;
	var idx1;
	var idx2;
	var idx3;
	var idx4;
	var i;
	var j;

	// Variable pivot, forward direction
	idx0 = offsetC;
	idx1 = offsetS;
	idx2 = offsetA;
	for ( j = 0; j < M - 1; j++ ) {
		ctemp = C[ idx0 ];
		stemp = S[ idx1 ];
		if ( ctemp !== 1.0 || stemp !== 0.0 ) {
			idx3 = idx2;
			for ( i = 0; i < N; i++ ) {
				idx4 = idx3 + strideA1;
				temp = A[ idx4 ];
				A[ idx4 ] = ( ctemp * temp ) - ( stemp * A[ idx3 ] );
				A[ idx3 ] = ( stemp * temp ) + ( ctemp * A[ idx3 ] );
				idx3 += strideA2;
			}
		}
		idx0 += strideC;
		idx1 += strideS;
		idx2 += strideA1;
	}
	return A;
}

Then, create a hash table which maps each function to a separate of parameters.

{
	'left': {
		'variable': {
			'forward': leftVariableForward,
			// ...
		}
		// ...
	// ...
}

But even more clever is make the hash table a strided array in order to avoid nested property look-up:

var TABLE = [ leftVariableForward, leftVariableBackward, leftTopForward, leftTopBackward, leftBottomForward, leftBottomBackward, rightVariableForward, ... ]

Define strides.

var strideSide = 6;
var stridePivot = 2;
var strideDirection = 1;

Then compute the index into your cache:

var SIDE_INDICES = {
	'left': 0,
	'right': 1
};
var PIVOT_INDICES = {
	'variable': 0,
	'top': 1,
	'bottom': 2
};
var DIRECTION_INDICES = {
	'forward': 0,
	'backward': 1
};

var sidx = SIDE_INDICES[ side ];
var pidx = PIVOT_INDICES[ pivot ];
var didx = DIRECTION_INDICES[ direction ];

var idx = ( sidx*strideSide ) + ( pidx*stridePivot ) + ( didx*strideDirection );
return TABLE[ idx ]( M, N, C, strideC, offsetC, S, strideS, offsetS, A, strideA1, strideA2, offsetA );

With this, all your nested loops go away and each set of nested loops can be independently optimized by the optimizer compiler(s).

Copy link
Copy Markdown
Member

@kgryte kgryte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left initial comments.

@kgryte kgryte added Feature Issue or pull request for adding a new feature. Needs Changes Pull request which needs changes before being merged. and removed Needs Review A pull request which needs code review. labels Jun 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Feature Issue or pull request for adding a new feature. GSoC Google Summer of Code. gsoc: 2026 Google Summer of Code (2026). LAPACK Issue or pull request related to the Linear Algebra Package (LAPACK). Needs Changes Pull request which needs changes before being merged.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants