Skip to content

Commit 7c02336

Browse files
headlessNodekgryte
andauthored
feat: add blas/ext/base/glast-index-of-row
PR-URL: #11929 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#221
1 parent c3e3ffd commit 7c02336

35 files changed

Lines changed: 3208 additions & 0 deletions
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) 2026 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+
# glastIndexOfRow
22+
23+
> Return the index of the last row in an input matrix which has the same elements as a provided search vector.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var glastIndexOfRow = require( '@stdlib/blas/ext/base/glast-index-of-row' );
37+
```
38+
39+
#### glastIndexOfRow( order, M, N, A, LDA, x, strideX, workspace, strideW )
40+
41+
Returns the index of the last row in an input matrix which has the same elements as a provided search vector.
42+
43+
```javascript
44+
/*
45+
A = [
46+
[ 1.0, 2.0 ],
47+
[ 3.0, 4.0 ],
48+
[ 3.0, 4.0 ]
49+
]
50+
*/
51+
var A = [ 1.0, 2.0, 3.0, 4.0, 3.0, 4.0 ];
52+
53+
var x = [ 3.0, 4.0 ];
54+
var workspace = [ 0, 0, 0 ];
55+
var out = glastIndexOfRow( 'row-major', 3, 2, A, 2, x, 1, workspace, 1 );
56+
// returns 2
57+
```
58+
59+
The function has the following parameters:
60+
61+
- **order**: storage layout.
62+
- **M**: number of rows in `A`.
63+
- **N**: number of columns in `A`.
64+
- **A**: input matrix as a linear array.
65+
- **LDA**: stride length for the first dimension of `A` (a.k.a., leading dimension of the matrix `A`).
66+
- **x**: search vector.
67+
- **strideX**: stride length for `x`.
68+
- **workspace**: workspace array for tracking row match candidates. This parameter is ignored if the input matrix is stored in row-major order.
69+
- **strideW**: stride length for `workspace`.
70+
71+
When an input matrix is stored in row-major order, the workspace parameter is ignored, and, thus, one may provide an empty workspace array.
72+
73+
```javascript
74+
/*
75+
A = [
76+
[ 1.0, 2.0 ],
77+
[ 3.0, 4.0 ],
78+
[ 3.0, 4.0 ]
79+
]
80+
*/
81+
var A = [ 1.0, 2.0, 3.0, 4.0, 3.0, 4.0 ];
82+
83+
var x = [ 3.0, 4.0 ];
84+
var workspace = [];
85+
var out = glastIndexOfRow( 'row-major', 3, 2, A, 2, x, 1, workspace, 1 );
86+
// returns 2
87+
```
88+
89+
If the function is unable to find a matching row, the function returns `-1`.
90+
91+
```javascript
92+
var A = [ 1.0, 2.0, 3.0, 4.0, 3.0, 4.0 ];
93+
94+
var x = [ -3.0, -4.0 ];
95+
var workspace = [ 0, 0, 0 ];
96+
var out = glastIndexOfRow( 'row-major', 3, 2, A, 2, x, 1, workspace, 1 );
97+
// returns -1
98+
```
99+
100+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
101+
102+
<!-- eslint-disable stdlib/capitalized-comments -->
103+
104+
```javascript
105+
var Float64Array = require( '@stdlib/array/float64' );
106+
107+
// Initial arrays:
108+
var A0 = new Float64Array( [ 9999.0, 1.0, 2.0, 3.0, 4.0, 0.0, 0.0 ] );
109+
var x0 = new Float64Array( [ 9999.0, 3.0, 4.0 ] );
110+
111+
// Create offset views:
112+
var A1 = new Float64Array( A0.buffer, A0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
113+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
114+
115+
var workspace = [ 0, 0, 0 ];
116+
var out = glastIndexOfRow( 'row-major', 3, 2, A1, 2, x1, 1, workspace, 1 );
117+
// returns 1
118+
```
119+
120+
<!-- lint disable maximum-heading-length -->
121+
122+
#### glastIndexOfRow.ndarray( M, N, A, strideA1, strideA2, offsetA, x, strideX, offsetX, workspace, strideW, offsetW )
123+
124+
<!-- lint enable maximum-heading-length -->
125+
126+
Returns the index of the last row in an input matrix which has the same elements as a provided search vector using alternative indexing semantics.
127+
128+
```javascript
129+
/*
130+
A = [
131+
[ 1.0, 2.0 ],
132+
[ 3.0, 4.0 ],
133+
[ 3.0, 4.0 ]
134+
]
135+
*/
136+
var A = [ 1.0, 2.0, 3.0, 4.0, 3.0, 4.0 ];
137+
138+
var x = [ 3.0, 4.0 ];
139+
var workspace = [ 0, 0, 0 ];
140+
var out = glastIndexOfRow.ndarray( 3, 2, A, 2, 1, 0, x, 1, 0, workspace, 1, 0 );
141+
// returns 2
142+
```
143+
144+
The function has the following parameters:
145+
146+
- **M**: number of rows in `A`.
147+
- **N**: number of columns in `A`.
148+
- **A**: input matrix as a linear array.
149+
- **strideA1**: stride length for the first dimension of `A`.
150+
- **strideA2**: stride length for the second dimension of `A`.
151+
- **offsetA**: starting index for `A`.
152+
- **x**: search vector.
153+
- **strideX**: stride length for `x`.
154+
- **offsetX**: starting index for `x`.
155+
- **workspace**: workspace array for tracking row match candidates. This parameter is ignored if the input matrix is stored in row-major order.
156+
- **strideW**: stride length for `workspace`.
157+
- **offsetW**: starting index for `workspace`.
158+
159+
When an input matrix is stored in row-major order, the workspace parameter is ignored, and, thus, one may provide an empty workspace array.
160+
161+
```javascript
162+
/*
163+
A = [
164+
[ 1.0, 2.0 ],
165+
[ 3.0, 4.0 ],
166+
[ 3.0, 4.0 ]
167+
]
168+
*/
169+
var A = [ 1.0, 2.0, 3.0, 4.0, 3.0, 4.0 ];
170+
171+
var x = [ 3.0, 4.0 ];
172+
var workspace = [];
173+
var out = glastIndexOfRow.ndarray( 3, 2, A, 2, 1, 0, x, 1, 0, workspace, 1, 0 );
174+
// returns 2
175+
```
176+
177+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, offset parameters support indexing semantics based on starting indices. For example,
178+
179+
```javascript
180+
/*
181+
A = [
182+
[ 1.0, 2.0 ],
183+
[ 3.0, 4.0 ],
184+
[ 0.0, 0.0 ]
185+
]
186+
*/
187+
var A = [ 9999.0, 1.0, 2.0, 3.0, 4.0, 0.0, 0.0 ];
188+
189+
var x = [ 9999.0, 3.0, 4.0 ];
190+
var workspace = [ 0, 0, 0 ];
191+
var out = glastIndexOfRow.ndarray( 3, 2, A, 2, 1, 1, x, 1, 1, workspace, 1, 0 );
192+
// returns 1
193+
```
194+
195+
</section>
196+
197+
<!-- /.usage -->
198+
199+
<section class="notes">
200+
201+
## Notes
202+
203+
- If `M <= 0` or `N <= 0`, both functions return `-1`.
204+
- When searching for a matching row, the function checks for equality using the strict equality operator `===`. As a consequence, `NaN` values are considered distinct, and `-0` and `+0` are considered the same.
205+
- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).
206+
207+
</section>
208+
209+
<!-- /.notes -->
210+
211+
<section class="examples">
212+
213+
## Examples
214+
215+
<!-- eslint-disable max-len -->
216+
217+
<!-- eslint no-undef: "error" -->
218+
219+
```javascript
220+
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
221+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
222+
var glastIndexOfRow = require( '@stdlib/blas/ext/base/glast-index-of-row' );
223+
224+
var shape = [ 3, 3 ];
225+
var order = 'row-major';
226+
var strides = shape2strides( shape, order );
227+
228+
var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 4.0, 5.0, 6.0 ];
229+
console.log( ndarray2array( A, shape, strides, 0, order ) );
230+
231+
var x = [ 4.0, 5.0, 6.0 ];
232+
console.log( x );
233+
234+
var workspace = [ 0, 0, 0 ];
235+
236+
var out = glastIndexOfRow( order, shape[ 0 ], shape[ 1 ], A, strides[ 0 ], x, 1, workspace, 1 );
237+
console.log( out );
238+
```
239+
240+
</section>
241+
242+
<!-- /.examples -->
243+
244+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
245+
246+
<section class="related">
247+
248+
</section>
249+
250+
<!-- /.related -->
251+
252+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
253+
254+
<section class="links">
255+
256+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
257+
258+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
259+
260+
</section>
261+
262+
<!-- /.links -->
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
var glastIndexOfRow = require( './../lib' );
31+
32+
33+
// VARIABLES //
34+
35+
var LAYOUTS = [
36+
'row-major',
37+
'column-major'
38+
];
39+
40+
41+
// FUNCTIONS //
42+
43+
/**
44+
* Creates a benchmark function.
45+
*
46+
* @private
47+
* @param {string} order - storage layout
48+
* @param {PositiveInteger} N - number of elements along each dimension
49+
* @returns {Function} benchmark function
50+
*/
51+
function createBenchmark( order, N ) {
52+
var workspace = zeros( N, 'generic' );
53+
var A = zeros( N*N, 'generic' );
54+
var x = zeros( N, 'generic' );
55+
return benchmark;
56+
57+
/**
58+
* Benchmark function.
59+
*
60+
* @private
61+
* @param {Benchmark} b - benchmark instance
62+
*/
63+
function benchmark( b ) {
64+
var z;
65+
var i;
66+
67+
b.tic();
68+
for ( i = 0; i < b.iterations; i++ ) {
69+
x[ N-1 ] += 1;
70+
z = glastIndexOfRow( order, N, N, A, N, x, 1, workspace, 1 );
71+
if ( isnan( z ) ) {
72+
b.fail( 'should not return NaN' );
73+
}
74+
}
75+
b.toc();
76+
if ( isnan( z ) ) {
77+
b.fail( 'should not return NaN' );
78+
}
79+
b.pass( 'benchmark finished' );
80+
b.end();
81+
}
82+
}
83+
84+
85+
// MAIN //
86+
87+
/**
88+
* Main execution sequence.
89+
*
90+
* @private
91+
*/
92+
function main() {
93+
var min;
94+
var max;
95+
var ord;
96+
var N;
97+
var f;
98+
var i;
99+
var k;
100+
101+
min = 1; // 10^min
102+
max = 6; // 10^max
103+
104+
for ( k = 0; k < LAYOUTS.length; k++ ) {
105+
ord = LAYOUTS[ k ];
106+
for ( i = min; i <= max; i++ ) {
107+
N = floor( pow( pow( 10, i ), 1.0/2.0 ) );
108+
f = createBenchmark( ord, N );
109+
bench( format( '%s::square_matrix:order=%s,size=%d', pkg, ord, N*N ), f );
110+
}
111+
}
112+
}
113+
114+
main();

0 commit comments

Comments
 (0)