Skip to content

Commit dd8fbbc

Browse files
committed
feat: add blas/ext/base/glast-index-of-row
1 parent 8238153 commit dd8fbbc

36 files changed

Lines changed: 2753 additions & 0 deletions
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
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 )
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 out = glastIndexOfRow( 'row-major', 3, 2, A, 2, x, 1 );
55+
// returns 2
56+
```
57+
58+
The function has the following parameters:
59+
60+
- **order**: storage layout.
61+
- **M**: number of rows in `A`.
62+
- **N**: number of columns in `A`.
63+
- **A**: input matrix as a linear array.
64+
- **LDA**: stride of the first dimension of `A` (a.k.a., leading dimension of the matrix `A`).
65+
- **x**: search vector.
66+
- **strideX**: stride length of `x`.
67+
68+
If the function is unable to find a matching row, the function returns `-1`.
69+
70+
```javascript
71+
var A = [ 1.0, 2.0, 3.0, 4.0, 3.0, 4.0 ];
72+
73+
var x = [ -3.0, -4.0 ];
74+
var out = glastIndexOfRow( 'row-major', 3, 2, A, 2, x, 1 );
75+
// returns -1
76+
```
77+
78+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
79+
80+
<!-- eslint-disable stdlib/capitalized-comments -->
81+
82+
```javascript
83+
var Float64Array = require( '@stdlib/array/float64' );
84+
85+
// Initial arrays:
86+
var A0 = new Float64Array( [ 9999.0, 1.0, 2.0, 3.0, 4.0, 0.0, 0.0 ] );
87+
var x0 = new Float64Array( [ 9999.0, 3.0, 4.0 ] );
88+
89+
// Create offset views:
90+
var A1 = new Float64Array( A0.buffer, A0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
91+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
92+
93+
var out = glastIndexOfRow( 'row-major', 3, 2, A1, 2, x1, 1 );
94+
// returns 1
95+
```
96+
97+
#### glastIndexOfRow.ndarray( M, N, A, strideA1, strideA2, offsetA, x, strideX, offsetX )
98+
99+
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.
100+
101+
```javascript
102+
/*
103+
A = [
104+
[ 1.0, 2.0 ],
105+
[ 3.0, 4.0 ],
106+
[ 3.0, 4.0 ]
107+
]
108+
*/
109+
var A = [ 1.0, 2.0, 3.0, 4.0, 3.0, 4.0 ];
110+
111+
var x = [ 3.0, 4.0 ];
112+
var out = glastIndexOfRow.ndarray( 3, 2, A, 2, 1, 0, x, 1, 0 );
113+
// returns 2
114+
```
115+
116+
The function has the following parameters:
117+
118+
- **M**: number of rows in `A`.
119+
- **N**: number of columns in `A`.
120+
- **A**: input matrix as a linear array.
121+
- **strideA1**: stride of the first dimension of `A`.
122+
- **strideA2**: stride of the second dimension of `A`.
123+
- **offsetA**: starting index for `A`.
124+
- **x**: search vector.
125+
- **strideX**: stride length of `x`.
126+
- **offsetX**: starting index for `x`.
127+
128+
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,
129+
130+
```javascript
131+
/*
132+
A = [
133+
[ 1.0, 2.0 ],
134+
[ 3.0, 4.0 ],
135+
[ 0.0, 0.0 ]
136+
]
137+
*/
138+
var A = [ 9999.0, 1.0, 2.0, 3.0, 4.0, 0.0, 0.0 ];
139+
140+
var x = [ 9999.0, 3.0, 4.0 ];
141+
var out = glastIndexOfRow.ndarray( 3, 2, A, 2, 1, 1, x, 1, 1 );
142+
// returns 1
143+
```
144+
145+
</section>
146+
147+
<!-- /.usage -->
148+
149+
<section class="notes">
150+
151+
## Notes
152+
153+
- If `M <= 0` or `N <= 0`, both functions return `-1`.
154+
- 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.
155+
- 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]).
156+
157+
</section>
158+
159+
<!-- /.notes -->
160+
161+
<section class="examples">
162+
163+
## Examples
164+
165+
<!-- eslint-disable max-len -->
166+
167+
<!-- eslint no-undef: "error" -->
168+
169+
```javascript
170+
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
171+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
172+
var glastIndexOfRow = require( '@stdlib/blas/ext/base/glast-index-of-row' );
173+
174+
var shape = [ 3, 3 ];
175+
var order = 'row-major';
176+
var strides = shape2strides( shape, order );
177+
178+
var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 4.0, 5.0, 6.0 ];
179+
console.log( ndarray2array( A, shape, strides, 0, order ) );
180+
181+
var x = [ 4.0, 5.0, 6.0 ];
182+
console.log( x );
183+
184+
var out = glastIndexOfRow( order, shape[ 0 ], shape[ 1 ], A, strides[ 0 ], x, 1 );
185+
console.log( out );
186+
```
187+
188+
</section>
189+
190+
<!-- /.examples -->
191+
192+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
193+
194+
<section class="related">
195+
196+
</section>
197+
198+
<!-- /.related -->
199+
200+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
201+
202+
<section class="links">
203+
204+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
205+
206+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
207+
208+
</section>
209+
210+
<!-- /.links -->
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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 A = zeros( N*N, 'generic' );
53+
var x = zeros( N, 'generic' );
54+
return benchmark;
55+
56+
/**
57+
* Benchmark function.
58+
*
59+
* @private
60+
* @param {Benchmark} b - benchmark instance
61+
*/
62+
function benchmark( b ) {
63+
var z;
64+
var i;
65+
66+
b.tic();
67+
for ( i = 0; i < b.iterations; i++ ) {
68+
x[ N-1 ] += 1;
69+
z = glastIndexOfRow( order, N, N, A, N, x, 1 );
70+
if ( isnan( z ) ) {
71+
b.fail( 'should not return NaN' );
72+
}
73+
}
74+
b.toc();
75+
if ( isnan( z ) ) {
76+
b.fail( 'should not return NaN' );
77+
}
78+
b.pass( 'benchmark finished' );
79+
b.end();
80+
}
81+
}
82+
83+
84+
// MAIN //
85+
86+
/**
87+
* Main execution sequence.
88+
*
89+
* @private
90+
*/
91+
function main() {
92+
var min;
93+
var max;
94+
var ord;
95+
var N;
96+
var f;
97+
var i;
98+
var k;
99+
100+
min = 1; // 10^min
101+
max = 6; // 10^max
102+
103+
for ( k = 0; k < LAYOUTS.length; k++ ) {
104+
ord = LAYOUTS[ k ];
105+
for ( i = min; i <= max; i++ ) {
106+
N = floor( pow( pow( 10, i ), 1.0/2.0 ) );
107+
f = createBenchmark( ord, N );
108+
bench( format( '%s::square_matrix:order=%s,size=%d', pkg, ord, N*N ), f );
109+
}
110+
}
111+
}
112+
113+
main();

0 commit comments

Comments
 (0)