Skip to content

Commit f3c3d72

Browse files
aayush0325kgryte
andauthored
feat: add lapack/base/iladlr
PR-URL: #7076 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 5f8c1b8 commit f3c3d72

36 files changed

+2493
-0
lines changed
Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# iladlr
22+
23+
> Find the index of the last non-zero row in a matrix `A`.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var iladlr = require( '@stdlib/lapack/base/iladlr' );
31+
```
32+
33+
#### iladlr( order, M, N, A, LDA )
34+
35+
Returns the index of the last non-zero row in a matrix `A`.
36+
37+
```javascript
38+
var Float64Array = require( '@stdlib/array/float64' );
39+
40+
var A = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 0.0, 0.0 ] );
41+
42+
/*
43+
A = [
44+
[ 1.0, 2.0 ],
45+
[ 3.0, 4.0 ],
46+
[ 0.0, 0.0 ]
47+
]
48+
*/
49+
50+
var out = iladlr( 'row-major', 3, 2, A, 2 );
51+
// returns 1
52+
```
53+
54+
The function has the following parameters:
55+
56+
- **order**: storage layout.
57+
- **M**: number of rows in `A`.
58+
- **N**: number of columns in `A`.
59+
- **A**: input [`Float64Array`][mdn-float64array].
60+
- **LDA**: stride of the first dimension of `A` (a.k.a., leading dimension of the matrix `A`).
61+
62+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
63+
64+
<!-- eslint-disable stdlib/capitalized-comments -->
65+
66+
```javascript
67+
var Float64Array = require( '@stdlib/array/float64' );
68+
69+
// Initial array:
70+
var A0 = new Float64Array( [ 9999.0, 1.0, 2.0, 3.0, 4.0, 0.0, 0.0 ] );
71+
72+
// Create an offset view:
73+
var A1 = new Float64Array( A0.buffer, A0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
74+
75+
var out = iladlr( 'row-major', 3, 2, A1, 2 );
76+
// returns 1
77+
```
78+
79+
#### iladlr.ndarray( M, N, A, strideA1, strideA2, offsetA )
80+
81+
Returns the index of the last non-zero row in a matrix `A` using alternative indexing semantics.
82+
83+
```javascript
84+
var Float64Array = require( '@stdlib/array/float64' );
85+
86+
var A = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 0.0, 0.0 ] );
87+
88+
/*
89+
A = [
90+
[ 1.0, 2.0 ],
91+
[ 3.0, 4.0 ],
92+
[ 0.0, 0.0 ]
93+
]
94+
*/
95+
96+
var out = iladlr.ndarray( 3, 2, A, 2, 1, 0 );
97+
// returns 1
98+
```
99+
100+
The function has the following parameters:
101+
102+
- **M**: number of rows in `A`.
103+
- **N**: number of columns in `A`.
104+
- **A**: input [`Float64Array`][mdn-float64array].
105+
- **strideA1**: stride of the first dimension of `A`.
106+
- **strideA2**: stride of the second dimension of `A`.
107+
- **offsetA**: starting index for `A`.
108+
109+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example,
110+
111+
```javascript
112+
var Float64Array = require( '@stdlib/array/float64' );
113+
114+
var A = new Float64Array( [ 9999.0, 1.0, 2.0, 3.0, 4.0, 0.0, 0.0 ] );
115+
116+
/*
117+
A = [
118+
[ 1.0, 2.0 ],
119+
[ 3.0, 4.0 ],
120+
[ 0.0, 0.0 ]
121+
]
122+
*/
123+
124+
var out = iladlr.ndarray( 3, 2, A, 2, 1, 1 );
125+
// returns 1
126+
```
127+
128+
</section>
129+
130+
<!-- /.usage -->
131+
132+
<section class="notes">
133+
134+
## Notes
135+
136+
- This routine is commonly used throughout LAPACK to shrink work domains (e.g., before bulge-chasing, deflation, or when trimming Householder panels), thus ensuring that higher-level routines operate only on numerically relevant sub-matrices.
137+
- `iladlr()` corresponds to the [LAPACK][lapack] routine [`iladlr`][lapack-iladlr].
138+
139+
</section>
140+
141+
<!-- /.notes -->
142+
143+
<section class="examples">
144+
145+
## Examples
146+
147+
<!-- eslint no-undef: "error" -->
148+
149+
```javascript
150+
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
151+
var Float64Array = require( '@stdlib/array/float64' );
152+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
153+
var iladlr = require( '@stdlib/lapack/base/iladlr' );
154+
155+
var shape = [ 3, 3 ];
156+
var order = 'row-major';
157+
var strides = shape2strides( shape, order );
158+
159+
var A = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 0.0, 0.0, 0.0 ] );
160+
console.log( ndarray2array( A, shape, strides, 0, order ) );
161+
162+
var out = iladlr( order, shape[ 0 ], shape[ 1 ], A, strides[ 0 ] );
163+
console.log( out );
164+
```
165+
166+
</section>
167+
168+
<!-- /.examples -->
169+
170+
<!-- C interface documentation. -->
171+
172+
* * *
173+
174+
<section class="c">
175+
176+
## C APIs
177+
178+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
179+
180+
<section class="intro">
181+
182+
</section>
183+
184+
<!-- /.intro -->
185+
186+
<!-- C usage documentation. -->
187+
188+
<section class="usage">
189+
190+
### Usage
191+
192+
```c
193+
TODO
194+
```
195+
196+
#### TODO
197+
198+
TODO.
199+
200+
```c
201+
TODO
202+
```
203+
204+
TODO
205+
206+
```c
207+
TODO
208+
```
209+
210+
</section>
211+
212+
<!-- /.usage -->
213+
214+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
215+
216+
<section class="notes">
217+
218+
</section>
219+
220+
<!-- /.notes -->
221+
222+
<!-- C API usage examples. -->
223+
224+
<section class="examples">
225+
226+
### Examples
227+
228+
```c
229+
TODO
230+
```
231+
232+
</section>
233+
234+
<!-- /.examples -->
235+
236+
</section>
237+
238+
<!-- /.c -->
239+
240+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
241+
242+
<section class="related">
243+
244+
</section>
245+
246+
<!-- /.related -->
247+
248+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
249+
250+
<section class="links">
251+
252+
[lapack]: https://www.netlib.org/lapack/explore-html/
253+
254+
[lapack-iladlr]: https://www.netlib.org/lapack/explore-html/da/d60/group__ilalr_gadb53a9bd5cc7a6e3bbca7bf4eca32208.html
255+
256+
[mdn-float64array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float64Array
257+
258+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
259+
260+
</section>
261+
262+
<!-- /.links -->
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var zeros = require( '@stdlib/array/zeros' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var floor = require( '@stdlib/math/base/special/floor' );
28+
var pkg = require( './../package.json' ).name;
29+
var iladlr = require( './../lib/iladlr.js' );
30+
31+
32+
// VARIABLES //
33+
34+
var LAYOUTS = [
35+
'row-major',
36+
'column-major'
37+
];
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Creates a benchmark function.
44+
*
45+
* @private
46+
* @param {string} order - storage layout
47+
* @param {PositiveInteger} N - number of elements along each dimension
48+
* @returns {Function} benchmark function
49+
*/
50+
function createBenchmark( order, N ) {
51+
var A = zeros( N*N, 'float64' );
52+
return benchmark;
53+
54+
/**
55+
* Benchmark function.
56+
*
57+
* @private
58+
* @param {Benchmark} b - benchmark instance
59+
*/
60+
function benchmark( b ) {
61+
var z;
62+
var i;
63+
64+
b.tic();
65+
for ( i = 0; i < b.iterations; i++ ) {
66+
A[ 0 ] = i;
67+
z = iladlr( order, N, N, A, N );
68+
if ( isnan( z ) ) {
69+
b.fail( 'should not return NaN' );
70+
}
71+
}
72+
b.toc();
73+
if ( isnan( z ) ) {
74+
b.fail( 'should not return NaN' );
75+
}
76+
b.pass( 'benchmark finished' );
77+
b.end();
78+
}
79+
}
80+
81+
82+
// MAIN //
83+
84+
/**
85+
* Main execution sequence.
86+
*
87+
* @private
88+
*/
89+
function main() {
90+
var min;
91+
var max;
92+
var ord;
93+
var N;
94+
var f;
95+
var i;
96+
var k;
97+
98+
min = 1; // 10^min
99+
max = 6; // 10^max
100+
101+
for ( k = 0; k < LAYOUTS.length; k++ ) {
102+
ord = LAYOUTS[ k ];
103+
for ( i = min; i <= max; i++ ) {
104+
N = floor( pow( pow( 10, i ), 1.0/2.0 ) );
105+
f = createBenchmark( ord, N );
106+
bench( pkg+'::square_matrix:order='+ord+',size='+(N*N), f );
107+
}
108+
}
109+
}
110+
111+
main();

0 commit comments

Comments
 (0)