Skip to content

Commit 5933ce5

Browse files
committed
refactor: add workspace
1 parent 79fe27e commit 5933ce5

13 files changed

Lines changed: 494 additions & 259 deletions

File tree

lib/node_modules/@stdlib/blas/ext/base/glast-index-of-row/README.md

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ limitations under the License.
3636
var glastIndexOfRow = require( '@stdlib/blas/ext/base/glast-index-of-row' );
3737
```
3838

39-
#### glastIndexOfRow( order, M, N, A, LDA, x, strideX )
39+
#### glastIndexOfRow( order, M, N, A, LDA, x, strideX, workspace, strideW )
4040

4141
Returns the index of the last row in an input matrix which has the same elements as a provided search vector.
4242

@@ -51,7 +51,8 @@ Returns the index of the last row in an input matrix which has the same elements
5151
var A = [ 1.0, 2.0, 3.0, 4.0, 3.0, 4.0 ];
5252

5353
var x = [ 3.0, 4.0 ];
54-
var out = glastIndexOfRow( 'row-major', 3, 2, A, 2, x, 1 );
54+
var workspace = [ 0, 0, 0 ];
55+
var out = glastIndexOfRow( 'row-major', 3, 2, A, 2, x, 1, workspace, 1 );
5556
// returns 2
5657
```
5758

@@ -64,14 +65,35 @@ The function has the following parameters:
6465
- **LDA**: stride length for the first dimension of `A` (a.k.a., leading dimension of the matrix `A`).
6566
- **x**: search vector.
6667
- **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+
```
6788

6889
If the function is unable to find a matching row, the function returns `-1`.
6990

7091
```javascript
7192
var A = [ 1.0, 2.0, 3.0, 4.0, 3.0, 4.0 ];
7293

7394
var x = [ -3.0, -4.0 ];
74-
var out = glastIndexOfRow( 'row-major', 3, 2, A, 2, x, 1 );
95+
var workspace = [ 0, 0, 0 ];
96+
var out = glastIndexOfRow( 'row-major', 3, 2, A, 2, x, 1, workspace, 1 );
7597
// returns -1
7698
```
7799

@@ -90,13 +112,14 @@ var x0 = new Float64Array( [ 9999.0, 3.0, 4.0 ] );
90112
var A1 = new Float64Array( A0.buffer, A0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
91113
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
92114

93-
var out = glastIndexOfRow( 'row-major', 3, 2, A1, 2, x1, 1 );
115+
var workspace = [ 0, 0, 0 ];
116+
var out = glastIndexOfRow( 'row-major', 3, 2, A1, 2, x1, 1, workspace, 1 );
94117
// returns 1
95118
```
96119

97120
<!-- lint disable maximum-heading-length -->
98121

99-
#### glastIndexOfRow.ndarray( M, N, A, strideA1, strideA2, offsetA, x, strideX, offsetX )
122+
#### glastIndexOfRow.ndarray( M, N, A, strideA1, strideA2, offsetA, x, strideX, offsetX, workspace, strideW, offsetW )
100123

101124
<!-- lint enable maximum-heading-length -->
102125

@@ -113,7 +136,8 @@ Returns the index of the last row in an input matrix which has the same elements
113136
var A = [ 1.0, 2.0, 3.0, 4.0, 3.0, 4.0 ];
114137

115138
var x = [ 3.0, 4.0 ];
116-
var out = glastIndexOfRow.ndarray( 3, 2, A, 2, 1, 0, x, 1, 0 );
139+
var workspace = [ 0, 0, 0 ];
140+
var out = glastIndexOfRow.ndarray( 3, 2, A, 2, 1, 0, x, 1, 0, workspace, 1, 0 );
117141
// returns 2
118142
```
119143

@@ -128,6 +152,27 @@ The function has the following parameters:
128152
- **x**: search vector.
129153
- **strideX**: stride length for `x`.
130154
- **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+
```
131176

132177
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,
133178

@@ -142,7 +187,8 @@ While [`typed array`][mdn-typed-array] views mandate a view offset based on the
142187
var A = [ 9999.0, 1.0, 2.0, 3.0, 4.0, 0.0, 0.0 ];
143188

144189
var x = [ 9999.0, 3.0, 4.0 ];
145-
var out = glastIndexOfRow.ndarray( 3, 2, A, 2, 1, 1, x, 1, 1 );
190+
var workspace = [ 0, 0, 0 ];
191+
var out = glastIndexOfRow.ndarray( 3, 2, A, 2, 1, 1, x, 1, 1, workspace, 1, 0 );
146192
// returns 1
147193
```
148194

@@ -185,7 +231,9 @@ console.log( ndarray2array( A, shape, strides, 0, order ) );
185231
var x = [ 4.0, 5.0, 6.0 ];
186232
console.log( x );
187233

188-
var out = glastIndexOfRow( order, shape[ 0 ], shape[ 1 ], A, strides[ 0 ], x, 1 );
234+
var workspace = [ 0, 0, 0 ];
235+
236+
var out = glastIndexOfRow( order, shape[ 0 ], shape[ 1 ], A, strides[ 0 ], x, 1, workspace, 1 );
189237
console.log( out );
190238
```
191239

lib/node_modules/@stdlib/blas/ext/base/glast-index-of-row/benchmark/benchmark.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ var LAYOUTS = [
4949
* @returns {Function} benchmark function
5050
*/
5151
function createBenchmark( order, N ) {
52+
var workspace = zeros( N, 'generic' );
5253
var A = zeros( N*N, 'generic' );
5354
var x = zeros( N, 'generic' );
5455
return benchmark;
@@ -66,7 +67,7 @@ function createBenchmark( order, N ) {
6667
b.tic();
6768
for ( i = 0; i < b.iterations; i++ ) {
6869
x[ N-1 ] += 1;
69-
z = glastIndexOfRow( order, N, N, A, N, x, 1 );
70+
z = glastIndexOfRow( order, N, N, A, N, x, 1, workspace, 1 );
7071
if ( isnan( z ) ) {
7172
b.fail( 'should not return NaN' );
7273
}

lib/node_modules/@stdlib/blas/ext/base/glast-index-of-row/benchmark/benchmark.ndarray.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ var LAYOUTS = [
5050
* @returns {Function} benchmark function
5151
*/
5252
function createBenchmark( order, N ) {
53+
var workspace = zeros( N, 'generic' );
5354
var A = zeros( N*N, 'generic' );
5455
var x = zeros( N, 'generic' );
5556
return benchmark;
@@ -77,7 +78,7 @@ function createBenchmark( order, N ) {
7778
b.tic();
7879
for ( i = 0; i < b.iterations; i++ ) {
7980
x[ N-1 ] += 1;
80-
z = glastIndexOfRow( N, N, A, sa1, sa2, 0, x, 1, 0 );
81+
z = glastIndexOfRow( N, N, A, sa1, sa2, 0, x, 1, 0, workspace, 1, 0 );
8182
if ( isnan( z ) ) {
8283
b.fail( 'should not return NaN' );
8384
}

lib/node_modules/@stdlib/blas/ext/base/glast-index-of-row/docs/repl.txt

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
{{alias}}( order, M, N, A, LDA, x, strideX )
2+
{{alias}}( order, M, N, A, LDA, x, strideX, workspace, strideW )
33
Returns the index of the last row in an input matrix which has the same
44
elements as a provided search vector.
55

@@ -34,6 +34,13 @@
3434
strideX: integer
3535
Stride length for `x`.
3636

37+
workspace: Array|TypedArray
38+
Workspace array for tracking row match candidates. This parameter is
39+
ignored if the input matrix is stored in row-major order.
40+
41+
strideW: integer
42+
Stride length for `workspace`.
43+
3744
Returns
3845
-------
3946
out: integer
@@ -43,11 +50,12 @@
4350
--------
4451
> var A = [ 1.0, 2.0, 3.0, 4.0, 3.0, 4.0 ];
4552
> var x = [ 3.0, 4.0 ];
46-
> {{alias}}( 'row-major', 3, 2, A, 2, x, 1 )
53+
> var w = [ 0, 0, 0 ];
54+
> {{alias}}( 'row-major', 3, 2, A, 2, x, 1, w, 1 )
4755
2
4856

4957

50-
{{alias}}.ndarray( M, N, A, strideA1, strideA2, offsetA, x, strideX, offsetX )
58+
{{alias}}.ndarray( M, N, A, sa1, sa2, oa, x, sx, ox, w, sw, ow )
5159
Returns the index of the last row in an input matrix which has the same
5260
elements as a provided search vector using alternative indexing semantics.
5361

@@ -69,24 +77,34 @@
6977
A: Array|TypedArray
7078
Input matrix `A`.
7179

72-
strideA1: integer
80+
sa1: integer
7381
Stride length for the first dimension of `A`.
7482

75-
strideA2: integer
83+
sa2: integer
7684
Stride length for the second dimension of `A`.
7785

78-
offsetA: integer
86+
oa: integer
7987
Starting index for `A`.
8088

8189
x: Array|TypedArray
8290
Search vector.
8391

84-
strideX: integer
92+
sx: integer
8593
Stride length for `x`.
8694

87-
offsetX: integer
95+
ox: integer
8896
Starting index for `x`.
8997

98+
w: Array|TypedArray
99+
Workspace array for tracking row match candidates. This parameter is
100+
ignored if the input matrix is stored in row-major order.
101+
102+
sw: integer
103+
Stride length for `w`.
104+
105+
ow: integer
106+
Starting index for `w`.
107+
90108
Returns
91109
-------
92110
out: integer
@@ -96,7 +114,8 @@
96114
--------
97115
> var A = [ 1.0, 2.0, 3.0, 4.0, 3.0, 4.0 ];
98116
> var x = [ 3.0, 4.0 ];
99-
> {{alias}}.ndarray( 3, 2, A, 2, 1, 0, x, 1, 0 )
117+
> var w = [ 0, 0, 0 ];
118+
> {{alias}}.ndarray( 3, 2, A, 2, 1, 0, x, 1, 0, w, 1, 0 )
100119
2
101120

102121
See Also

lib/node_modules/@stdlib/blas/ext/base/glast-index-of-row/docs/types/index.d.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ import { Layout } from '@stdlib/types/blas';
2828
*/
2929
type InputArray = Collection<unknown> | AccessorArrayLike<unknown>;
3030

31+
/**
32+
* Workspace array.
33+
*/
34+
type WorkspaceArray = Collection<unknown> | AccessorArrayLike<unknown>;
35+
3136
/**
3237
* Interface describing `glastIndexOfRow`.
3338
*/
@@ -38,6 +43,7 @@ interface Routine {
3843
* ## Notes
3944
*
4045
* - If the function is provided an empty matrix or if the function is unable to find a search vector, the function returns `-1` (i.e., an invalid index).
46+
* - The `workspace` array is only applicable when an input matrix is stored in column-major order. When the matrix is stored in row-major order, the workspace array is ignored.
4147
*
4248
* @param order - storage layout
4349
* @param M - number of rows in `A`
@@ -46,23 +52,27 @@ interface Routine {
4652
* @param LDA - stride length for the first dimension of `A` (a.k.a., leading dimension of the matrix `A`)
4753
* @param x - search vector
4854
* @param strideX - stride length for `x`
55+
* @param workspace - workspace array for tracking row match candidates
56+
* @param strideW - stride length for `workspace`
4957
* @returns row index
5058
*
5159
* @example
5260
* var A = [ 1.0, 2.0, 3.0, 4.0, 3.0, 4.0 ]; // => [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 3.0, 4.0 ] ]
5361
* var x = [ 3.0, 4.0 ];
62+
* var workspace = [ 0, 0, 0 ];
5463
*
55-
* var out = glastIndexOfRow( 'row-major', 3, 2, A, 2, x, 1 );
64+
* var out = glastIndexOfRow( 'row-major', 3, 2, A, 2, x, 1, workspace, 1 );
5665
* // returns 2
5766
*/
58-
( order: Layout, M: number, N: number, A: InputArray, LDA: number, x: InputArray, strideX: number ): number;
67+
( order: Layout, M: number, N: number, A: InputArray, LDA: number, x: InputArray, strideX: number, workspace: WorkspaceArray, strideW: number ): number;
5968

6069
/**
6170
* 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.
6271
*
6372
* ## Notes
6473
*
6574
* - If the function is provided an empty matrix or if the function is unable to find a search vector, the function returns `-1` (i.e., an invalid index).
75+
* - The `workspace` array is only applicable when an input matrix is stored in column-major order. When the matrix is stored in row-major order, the workspace array is ignored.
6676
*
6777
* @param M - number of rows in `A`
6878
* @param N - number of columns in `A`
@@ -73,16 +83,20 @@ interface Routine {
7383
* @param x - search vector
7484
* @param strideX - stride length for `x`
7585
* @param offsetX - starting index for `x`
86+
* @param workspace - workspace array for tracking row match candidates
87+
* @param strideW - stride length for `workspace`
88+
* @param offsetW - starting index for `workspace`
7689
* @returns row index
7790
*
7891
* @example
7992
* var A = [ 1.0, 2.0, 3.0, 4.0, 3.0, 4.0 ]; // => [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 3.0, 4.0 ] ]
8093
* var x = [ 3.0, 4.0 ];
94+
* var workspace = [ 0, 0, 0 ];
8195
*
82-
* var out = glastIndexOfRow.ndarray( 3, 2, A, 2, 1, 0, x, 1, 0 );
96+
* var out = glastIndexOfRow.ndarray( 3, 2, A, 2, 1, 0, x, 1, 0, workspace, 1, 0 );
8397
* // returns 2
8498
*/
85-
ndarray( M: number, N: number, A: InputArray, strideA1: number, strideA2: number, offsetA: number, x: InputArray, strideX: number, offsetX: number ): number;
99+
ndarray( M: number, N: number, A: InputArray, strideA1: number, strideA2: number, offsetA: number, x: InputArray, strideX: number, offsetX: number, workspace: WorkspaceArray, strideW: number, offsetW: number ): number;
86100
}
87101

88102
/**
@@ -91,6 +105,7 @@ interface Routine {
91105
* ## Notes
92106
*
93107
* - If the function is provided an empty matrix or if the function is unable to find a search vector, the function returns `-1` (i.e., an invalid index).
108+
* - The `workspace` array is only applicable when an input matrix is stored in column-major order. When the matrix is stored in row-major order, the workspace array is ignored.
94109
*
95110
* @param order - storage layout
96111
* @param M - number of rows in `A`
@@ -99,20 +114,24 @@ interface Routine {
99114
* @param LDA - stride length for the first dimension of `A` (a.k.a., leading dimension of the matrix `A`)
100115
* @param x - search vector
101116
* @param strideX - stride length for `x`
117+
* @param workspace - workspace array for tracking row match candidates
118+
* @param strideW - stride length for `workspace`
102119
* @returns row index
103120
*
104121
* @example
105122
* var A = [ 1.0, 2.0, 3.0, 4.0, 3.0, 4.0 ]; // => [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 3.0, 4.0 ] ]
106123
* var x = [ 3.0, 4.0 ];
124+
* var workspace = [ 0, 0, 0 ];
107125
*
108-
* var out = glastIndexOfRow( 'row-major', 3, 2, A, 2, x, 1 );
126+
* var out = glastIndexOfRow( 'row-major', 3, 2, A, 2, x, 1, workspace, 1 );
109127
* // returns 2
110128
*
111129
* @example
112130
* var A = [ 1.0, 2.0, 3.0, 4.0, 3.0, 4.0 ]; // => [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 3.0, 4.0 ] ]
113131
* var x = [ 3.0, 4.0 ];
132+
* var workspace = [ 0, 0, 0 ];
114133
*
115-
* var out = glastIndexOfRow.ndarray( 3, 2, A, 2, 1, 0, x, 1, 0 );
134+
* var out = glastIndexOfRow.ndarray( 3, 2, A, 2, 1, 0, x, 1, 0, workspace, 1, 0 );
116135
* // returns 2
117136
*/
118137
declare var glastIndexOfRow: Routine;

0 commit comments

Comments
 (0)