Skip to content

Commit de5a92e

Browse files
committed
feat: add blas/ext/base/gindex-of-row
--- 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 status: passed - 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 ---
1 parent 160d653 commit de5a92e

36 files changed

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

0 commit comments

Comments
 (0)