Skip to content

Commit d9f3a19

Browse files
authored
feat: add blas/ext/base/gindex-of-column
PR-URL: #11240 Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent faaf3b3 commit d9f3a19

36 files changed

+2979
-0
lines changed
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
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+
# gindexOfColumn
22+
23+
> Return the index of the first column in an 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 gindexOfColumn = require( '@stdlib/blas/ext/base/gindex-of-column' );
31+
```
32+
33+
#### gindexOfColumn( order, M, N, A, LDA, x, strideX )
34+
35+
Returns the index of the first column in an 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 = [ 2.0, 4.0, 0.0 ];
48+
var out = gindexOfColumn( '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 column, 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 = [ -2.0, -4.0, 0.0 ];
68+
var out = gindexOfColumn( '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, 2.0, 4.0, 0.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 = gindexOfColumn( 'row-major', 3, 2, A1, 2, x1, 1 );
88+
// returns 1
89+
```
90+
91+
<!-- lint disable maximum-heading-length -->
92+
93+
#### gindexOfColumn.ndarray( M, N, A, strideA1, strideA2, offsetA, x, strideX, offsetX )
94+
95+
<!-- lint enable maximum-heading-length -->
96+
97+
Returns the index of the first column in an input matrix which has the same elements as a provided search vector using alternative indexing semantics.
98+
99+
```javascript
100+
/*
101+
A = [
102+
[ 1.0, 2.0 ],
103+
[ 3.0, 4.0 ],
104+
[ 0.0, 0.0 ]
105+
]
106+
*/
107+
var A = [ 1.0, 2.0, 3.0, 4.0, 0.0, 0.0 ];
108+
109+
var x = [ 2.0, 4.0, 0.0 ];
110+
var out = gindexOfColumn.ndarray( 3, 2, A, 2, 1, 0, x, 1, 0 );
111+
// returns 1
112+
```
113+
114+
The function has the following parameters:
115+
116+
- **M**: number of rows in `A`.
117+
- **N**: number of columns in `A`.
118+
- **A**: input matrix as a linear array.
119+
- **strideA1**: stride of the first dimension of `A`.
120+
- **strideA2**: stride of the second dimension of `A`.
121+
- **offsetA**: starting index for `A`.
122+
- **x**: search vector.
123+
- **strideX**: stride length of `x`.
124+
- **offsetX**: starting index for `x`.
125+
126+
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,
127+
128+
```javascript
129+
/*
130+
A = [
131+
[ 1.0, 2.0 ],
132+
[ 3.0, 4.0 ],
133+
[ 0.0, 0.0 ]
134+
]
135+
*/
136+
var A = [ 9999.0, 1.0, 2.0, 3.0, 4.0, 0.0, 0.0 ];
137+
138+
var x = [ 9999.0, 2.0, 4.0, 0.0 ];
139+
var out = gindexOfColumn.ndarray( 3, 2, A, 2, 1, 1, x, 1, 1 );
140+
// returns 1
141+
```
142+
143+
</section>
144+
145+
<!-- /.usage -->
146+
147+
<section class="notes">
148+
149+
## Notes
150+
151+
- When searching for a matching column, 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.
152+
- 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]).
153+
154+
</section>
155+
156+
<!-- /.notes -->
157+
158+
<section class="examples">
159+
160+
## Examples
161+
162+
<!-- eslint-disable max-len -->
163+
164+
<!-- eslint no-undef: "error" -->
165+
166+
```javascript
167+
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
168+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
169+
var gindexOfColumn = require( '@stdlib/blas/ext/base/gindex-of-column' );
170+
171+
var shape = [ 3, 3 ];
172+
var order = 'row-major';
173+
var strides = shape2strides( shape, order );
174+
175+
var A = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ];
176+
console.log( ndarray2array( A, shape, strides, 0, order ) );
177+
178+
var x = [ 2.0, 5.0, 8.0 ];
179+
console.log( x );
180+
181+
var out = gindexOfColumn( order, shape[ 0 ], shape[ 1 ], A, strides[ 0 ], x, 1, 0 );
182+
console.log( out );
183+
```
184+
185+
</section>
186+
187+
<!-- /.examples -->
188+
189+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
190+
191+
<section class="related">
192+
193+
</section>
194+
195+
<!-- /.related -->
196+
197+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
198+
199+
<section class="links">
200+
201+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
202+
203+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
204+
205+
</section>
206+
207+
<!-- /.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 gindexOfColumn = 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 = gindexOfColumn( 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)