|
| 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 --> |
0 commit comments