Skip to content

Commit 231adcc

Browse files
ShabiShett07kgrytestdlib-bot
authored
feat: add JavaScript implementation and clean-up blas/base/dger
PR-URL: #6395 Ref: #2039 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent 8711466 commit 231adcc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+6900
-330
lines changed

lib/node_modules/@stdlib/blas/base/dger/README.md

Lines changed: 218 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,10 @@ limitations under the License.
2020

2121
# dger
2222

23-
> TODO.
23+
> Perform the rank 1 operation `A = α*x*y^T + A`.
2424
2525
<section class="intro">
2626

27-
TODO
28-
2927
</section>
3028

3129
<!-- /.intro -->
@@ -38,15 +36,102 @@ TODO
3836
var dger = require( '@stdlib/blas/base/dger' );
3937
```
4038

41-
#### TODO
39+
#### dger( ord, M, N, α, x, sx, y, sy, A, lda )
40+
41+
Performs the rank 1 operation `A = α*x*y^T + A`, where `α` is a scalar, `x` is an `M` element vector, `y` is an `N` element vector, and `A` is an `M` by `N` matrix.
42+
43+
```javascript
44+
var Float64Array = require( '@stdlib/array/float64' );
45+
46+
var A = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
47+
var x = new Float64Array( [ 1.0, 1.0 ] );
48+
var y = new Float64Array( [ 1.0, 1.0, 1.0 ] );
49+
50+
dger( 'row-major', 2, 3, 1.0, x, 1, y, 1, A, 3 );
51+
// A => <Float64Array>[ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 ]
52+
```
53+
54+
The function has the following parameters:
55+
56+
- **ord**: storage layout.
57+
- **M**: number of rows in the matrix `A`.
58+
- **N**: number of columns in the matrix `A`.
59+
- **α**: scalar constant.
60+
- **x**: input [`Float64Array`][mdn-float64array].
61+
- **sx**: stride length for `x`.
62+
- **y**: output [`Float64Array`][mdn-float64array].
63+
- **sy**: stride length for `y`.
64+
- **A**: input matrix stored in linear memory as a [`Float64Array`][mdn-float64array].
65+
- **lda**: stride of the first dimension of `A` (leading dimension of `A`).
66+
67+
The stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to iterate over every other element in `x` and `y`,
68+
69+
```javascript
70+
var Float64Array = require( '@stdlib/array/float64' );
71+
72+
var A = new Float64Array( [ 1.0, 4.0, 2.0, 5.0, 3.0, 6.0 ] );
73+
var x = new Float64Array( [ 1.0, 0.0, 1.0, 0.0 ] );
74+
var y = new Float64Array( [ 1.0, 0.0, 1.0, 0.0, 1.0, 0.0 ] );
75+
76+
dger( 'column-major', 2, 3, 1.0, x, 2, y, 2, A, 2 );
77+
// A => <Float64Array>[ 2.0, 5.0, 3.0, 6.0, 4.0, 7.0 ]
78+
```
79+
80+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
4281

43-
TODO
82+
<!-- eslint-disable stdlib/capitalized-comments -->
4483

4584
```javascript
46-
/* to-do */
85+
var Float64Array = require( '@stdlib/array/float64' );
86+
87+
// Initial arrays...
88+
var x0 = new Float64Array( [ 0.0, 1.0, 1.0 ] );
89+
var y0 = new Float64Array( [ 0.0, 1.0, 1.0, 1.0 ] );
90+
var A = new Float64Array( [ 1.0, 4.0, 2.0, 5.0, 3.0, 6.0 ] );
91+
92+
// Create offset views...
93+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
94+
var y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
95+
96+
dger( 'column-major', 2, 3, 1.0, x1, -1, y1, -1, A, 2 );
97+
// A => <Float64Array>[ 2.0, 5.0, 3.0, 6.0, 4.0, 7.0 ]
4798
```
4899

49-
TODO
100+
#### dger.ndarray( M, N, α, x, sx, ox, y, sy, oy, A, sa1, sa2, oa )
101+
102+
Performs the rank 1 operation `A = α*x*y^T + A`, using alternative indexing semantics and where `α` is a scalar, `x` is an `M` element vector, `y` is an `N` element vector, and `A` is an `M` by `N` matrix.
103+
104+
```javascript
105+
var Float64Array = require( '@stdlib/array/float64' );
106+
107+
var A = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
108+
var x = new Float64Array( [ 1.0, 1.0 ] );
109+
var y = new Float64Array( [ 1.0, 1.0, 1.0 ] );
110+
111+
dger.ndarray( 2, 3, 1.0, x, 1, 0, y, 1, 0, A, 3, 1, 0 );
112+
// A => <Float64Array>[ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 ]
113+
```
114+
115+
The function has the following additional parameters:
116+
117+
- **sa1**: stride of the first dimension of `A`.
118+
- **sa2**: stride of the second dimension of `A`.
119+
- **oa**: starting index for `A`.
120+
- **ox**: starting index for `x`.
121+
- **oy**: starting index for `y`.
122+
123+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameters support indexing semantics based on starting indices. For example,
124+
125+
```javascript
126+
var Float64Array = require( '@stdlib/array/float64' );
127+
128+
var A = new Float64Array( [ 0.0, 0.0, 1.0, 4.0, 2.0, 5.0, 3.0, 6.0 ] );
129+
var x = new Float64Array( [ 0.0, 1.0, 0.0, 1.0, 0.0 ] );
130+
var y = new Float64Array( [ 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0 ] );
131+
132+
dger.ndarray( 2, 3, 1.0, x, 2, 1, y, 2, 1, A, 1, 2, 2 );
133+
// A => <Float64Array>[ 0.0, 0.0, 2.0, 5.0, 3.0, 6.0, 4.0, 7.0 ]
134+
```
50135

51136
</section>
52137

@@ -56,7 +141,7 @@ TODO
56141

57142
## Notes
58143

59-
- `dger()` corresponds to the [BLAS][blas] level 2 function [`dger`][dger].
144+
- `dger()` corresponds to the [BLAS][blas] level 2 function [`dger`][blas-dger].
60145

61146
</section>
62147

@@ -69,7 +154,26 @@ TODO
69154
<!-- eslint no-undef: "error" -->
70155

71156
```javascript
72-
/* to-do */
157+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
158+
var dger = require( '@stdlib/blas/base/dger' );
159+
160+
var opts = {
161+
'dtype': 'float64'
162+
};
163+
164+
var M = 3;
165+
var N = 5;
166+
167+
var A = discreteUniform( M*N, 0, 255, opts );
168+
var x = discreteUniform( M, 0, 255, opts );
169+
var y = discreteUniform( N, 0, 255, opts );
170+
171+
dger( 'row-major', M, N, 1.0, x, 1, y, 1, A, N );
172+
console.log( A );
173+
174+
dger.ndarray( M, N, 1.0, x, 1, 0, y, 1, 0, A, 1, M, 0 );
175+
console.log(A);
176+
73177
```
74178

75179
</section>
@@ -102,18 +206,80 @@ TODO
102206
#include "stdlib/blas/base/dger.h"
103207
```
104208

105-
#### TODO
209+
#### c_dger( layout, M, N, alpha, \*X, strideX, \*Y, strideY, \*A, LDA )
106210

107-
TODO.
211+
Performs the rank 1 operation `A = alpha*x*y^T + A`, where `alpha` is a scalar, `x` is an `M` element vector, `y` is an `N` element vector, and `A` is an `M`-by-`N` matrix.
108212

109213
```c
110-
TODO
214+
#include "stdlib/blas/base/shared.h"
215+
216+
double A[ 3*4 ] = {
217+
0.0, 0.0, 0.0, 0.0,
218+
0.0, 0.0, 0.0, 0.0,
219+
0.0, 0.0, 0.0, 0.0
220+
};
221+
222+
const double x[ 3 ] = { 1.0, 4.0, 0.0 };
223+
const double y[ 4 ] = { 0.0, 1.0, 2.0, 3.0 };
224+
225+
c_dger( CblasRowMajor, 3, 4, 1.0, x, 1, y, 1, A, 4 );
111226
```
112227
113-
TODO
228+
The function accepts the following arguments:
229+
230+
- **layout**: `[in] CBLAS_LAYOUT` storage layout.
231+
- **M**: `[in] CBLAS_INT` number of rows in the matrix `A`.
232+
- **N**: `[in] CBLAS_INT` number of columns in the matrix `A`.
233+
- **alpha**: `[in] double` scalar constant.
234+
- **X**: `[in] double*` an `M` element vector.
235+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
236+
- **Y**: `[in] double*` an `N` element vector.
237+
- **strideY**: `[in] CBLAS_INT` stride length for `Y`.
238+
- **A**: `[inout] double*` input matrix.
239+
- **LDA**: `[in] CBLAS_INT` stride of the first dimension of `A` (a.k.a., leading dimension of the matrix `A`).
114240
115241
```c
116-
TODO
242+
void c_dger( const CBLAS_LAYOUT layout, const CBLAS_INT M, const CBLAS_INT N, const double alpha, const double *X, const CBLAS_INT strideX, const double *Y, const CBLAS_INT strideY, double *A, const CBLAS_INT LDA );
243+
```
244+
245+
#### c_dger_ndarray( M, N, alpha, \*X, sx, ox, \*Y, sy, oy, \*A, sa1, sa2, oa )
246+
247+
Performs the rank 1 operation `A = alpha*x*y^T + A`, using alternative indexing semantics and where `alpha` is a scalar, `x` is an `M` element vector, `y` is an `N` element vector, and `A` is an `M`-by-`N` matrix.
248+
249+
```c
250+
#include "stdlib/blas/base/shared.h"
251+
252+
double A[ 3*4 ] = {
253+
0.0, 0.0, 0.0, 0.0,
254+
0.0, 0.0, 0.0, 0.0,
255+
0.0, 0.0, 0.0, 0.0
256+
};
257+
258+
const double x[ 3 ] = { 1.0, 4.0, 0.0 };
259+
const double y[ 4 ] = { 0.0, 1.0, 2.0, 3.0 };
260+
261+
c_dger_ndarray( 3, 4, 1.0, x, 1, 0, y, 1, 0, A, 4, 1, 0 );
262+
```
263+
264+
The function accepts the following arguments:
265+
266+
- **layout**: `[in] CBLAS_LAYOUT` storage layout.
267+
- **M**: `[in] CBLAS_INT` number of rows in the matrix `A`.
268+
- **N**: `[in] CBLAS_INT` number of columns in the matrix `A`.
269+
- **alpha**: `[in] double` scalar constant.
270+
- **X**: `[in] double*` an `M` element vector.
271+
- **sx**: `[in] CBLAS_INT` stride length for `X`.
272+
- **ox**: `[in] CBLAS_INT` starting index for `X`.
273+
- **Y**: `[in] double*` an `N` element vector.
274+
- **sy**: `[in] CBLAS_INT` stride length for `Y`.
275+
- **oy**: `[in] CBLAS_INT` starting index for `Y`.
276+
- **A**: `[inout] double*` input matrix.
277+
- **sa1**: `[in] CBLAS_INT` stride of the first dimension of `A`.
278+
- **sa2**: `[in] CBLAS_INT` stride of the second dimension of `A`.
279+
- **oa**: `[in] CBLAS_INT` starting index for `A`.
280+
281+
```c
282+
void c_dger( onst CBLAS_INT M, const CBLAS_INT N, const double alpha, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, const double *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY, double *A, const CBLAS_INT strideA1, const CBLAS_INT strideA2, const CBLAS_INT offsetA );
117283
```
118284

119285
</section>
@@ -135,7 +301,39 @@ TODO
135301
### Examples
136302

137303
```c
138-
TODO
304+
#include "stdlib/blas/base/dger.h"
305+
#include "stdlib/blas/base/shared.h"
306+
#include <stdio.h>
307+
308+
int main( void ) {
309+
// Define a 3x4 matrix stored in row-major order:
310+
double A[ 3*4 ] = {
311+
0.0, 0.0, 0.0, 0.0,
312+
0.0, 0.0, 0.0, 0.0,
313+
0.0, 0.0, 0.0, 0.0
314+
};
315+
// Define `x` and `y^T` vectors:
316+
const double x[ 3 ] = { 1.0, 4.0, 0.0 }; // M
317+
const double y[ 4 ] = { 0.0, 1.0, 2.0, 3.0 }; // N
318+
319+
// Specify the number of rows and columns:
320+
const int M = 3;
321+
const int N = 4;
322+
323+
// Specify stride lengths:
324+
const int strideX = 1;
325+
const int strideY = 1;
326+
327+
// Perform operation:
328+
c_dger( CblasRowMajor, M, N, 1.0, x, strideX, y, strideY, A, N );
329+
330+
// Print the result:
331+
for ( int i = 0; i < M; i++ ) {
332+
for ( int j = 0; j < N; j++ ) {
333+
printf( "A[%i,%i] = %lf\n", i, j, A[ (i*N)+j ] );
334+
}
335+
}
336+
}
139337
```
140338
141339
</section>
@@ -160,7 +358,11 @@ TODO
160358
161359
[blas]: http://www.netlib.org/blas
162360
163-
[dger]: https://www.netlib.org/lapack/explore-html/dc/da8/dger_8f_source.html
361+
[blas-dger]: https://www.netlib.org/lapack/explore-html-3.6.1/d7/d15/group__double__blas__level2_ga458222e01b4d348e9b52b9343d52f828.html
362+
363+
[mdn-float64array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float64Array
364+
365+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
164366
165367
</section>
166368

0 commit comments

Comments
 (0)