Skip to content

Commit b7a453f

Browse files
committed
Add generalized BLAS level 1 routine for computing the dot product for ndarrays
1 parent 508ad04 commit b7a453f

File tree

13 files changed

+1516
-0
lines changed

13 files changed

+1516
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2020 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+
# gdot
22+
23+
> Calculate the dot product of two vectors.
24+
25+
<section class="intro">
26+
27+
The [dot product][dot-product] (or scalar product) is defined as
28+
29+
<!-- <equation class="equation" label="eq:dot_product" align="center" raw="\mathbf{x}\cdot\mathbf{y} = \sum_{i=0}^{N-1} x_i y_i = x_0 y_0 + x_1 y_1 + \ldots + x_{N-1} y_{N-1}" alt="Dot product definition."> -->
30+
31+
<div class="equation" align="center" data-raw-text="\mathbf{x}\cdot\mathbf{y} = \sum_{i=0}^{N-1} x_i y_i = x_0 y_0 + x_1 y_1 + \ldots + x_{N-1} y_{N-1}" data-equation="eq:dot_product">
32+
<img src="" alt="Dot product definition.">
33+
<br>
34+
</div>
35+
36+
<!-- </equation> -->
37+
38+
</section>
39+
40+
<!-- /.intro -->
41+
42+
<section class="usage">
43+
44+
## Usage
45+
46+
```javascript
47+
var gdot = require( '@stdlib/blas/gdot' );
48+
```
49+
50+
#### gdot( x, y )
51+
52+
Calculates the dot product of vectors `x` and `y`.
53+
54+
```javascript
55+
var Int32Array = require( '@stdlib/array/int32' );
56+
var array = require( '@stdlib/ndarray/array' );
57+
58+
var x = array( new Int32Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ) );
59+
var y = array( new Int32Array( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] ) );
60+
61+
var z = gdot( x, y );
62+
// returns -5.0
63+
```
64+
65+
The function has the following parameters:
66+
67+
- **x**: a 1-dimensional [`ndarray`][@stdlib/ndarray/array] or an array-like object.
68+
- **y**: a 1-dimensional [`ndarray`][@stdlib/ndarray/array] or an array-like object.
69+
70+
If provided empty vectors, the function returns `0.0`.
71+
72+
```javascript
73+
var z = gdot( [], [] );
74+
// returns 0.0
75+
```
76+
77+
</section>
78+
79+
<!-- /.usage -->
80+
81+
<section class="notes">
82+
83+
## Notes
84+
85+
- `gdot()` corresponds to the [BLAS][blas] level 1 function [`ddot`][ddot] with the exception that this implementation works with any array type, not just Float64Arrays.
86+
- For best performance, provide 1-dimensional [`ndarrays`][@stdlib/ndarray/array] whose underlying data type is either `float64` or `float32`.
87+
88+
</section>
89+
90+
<!-- /.notes -->
91+
92+
<section class="examples">
93+
94+
## Examples
95+
96+
<!-- eslint no-undef: "error" -->
97+
98+
```javascript
99+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
100+
var gdot = require( '@stdlib/blas/gdot' );
101+
102+
var rand1 = discreteUniform.factory( 0, 100 );
103+
var rand2 = discreteUniform.factory( 0, 10 );
104+
105+
var x = [];
106+
var y = [];
107+
var i;
108+
for ( i = 0; i < 10; i++ ) {
109+
x.push( rand1() );
110+
y.push( rand2() );
111+
}
112+
console.log( x );
113+
console.log( y );
114+
115+
var z = gdot( x, y );
116+
console.log( z );
117+
```
118+
119+
</section>
120+
121+
<!-- /.examples -->
122+
123+
<section class="links">
124+
125+
[dot-product]: https://en.wikipedia.org/wiki/Dot_product
126+
127+
[blas]: http://www.netlib.org/blas
128+
129+
[ddot]: http://www.netlib.org/lapack/explore-html/df/d28/group__single__blas__level1.html
130+
131+
[@stdlib/ndarray/array]: https://github.com/stdlib-js/stdlib
132+
133+
</section>
134+
135+
<!-- /.links -->
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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 randu = require( '@stdlib/random/base/randu' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var Float32Array = require( '@stdlib/array/float32' );
28+
var pkg = require( './../package.json' ).name;
29+
var gdot = require( './../lib/main.js' );
30+
31+
32+
// FUNCTIONS //
33+
34+
/**
35+
* Creates a benchmark function.
36+
*
37+
* @private
38+
* @param {PositiveInteger} len - array length
39+
* @returns {Function} benchmark function
40+
*/
41+
function createBenchmark( len ) {
42+
var x;
43+
var y;
44+
var i;
45+
46+
x = new Float32Array( len );
47+
y = new Float32Array( len );
48+
for ( i = 0; i < len; i++ ) {
49+
x[ i ] = ( randu()*10.0 ) - 20.0;
50+
y[ i ] = ( randu()*10.0 ) - 20.0;
51+
}
52+
return benchmark;
53+
54+
function benchmark( b ) {
55+
var d;
56+
var i;
57+
58+
b.tic();
59+
for ( i = 0; i < b.iterations; i++ ) {
60+
d = gdot( x, y );
61+
if ( isnan( d ) ) {
62+
b.fail( 'should not return NaN' );
63+
}
64+
}
65+
b.toc();
66+
if ( isnan( d ) ) {
67+
b.fail( 'should not return NaN' );
68+
}
69+
b.pass( 'benchmark finished' );
70+
b.end();
71+
}
72+
}
73+
74+
75+
// MAIN //
76+
77+
/**
78+
* Main execution sequence.
79+
*
80+
* @private
81+
*/
82+
function main() {
83+
var len;
84+
var min;
85+
var max;
86+
var f;
87+
var i;
88+
89+
min = 1; // 10^min
90+
max = 6; // 10^max
91+
92+
for ( i = min; i <= max; i++ ) {
93+
len = pow( 10, i );
94+
f = createBenchmark( len );
95+
bench( pkg+'::float32:len='+len, f );
96+
}
97+
}
98+
99+
main();
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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 randu = require( '@stdlib/random/base/randu' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var Float64Array = require( '@stdlib/array/float64' );
28+
var pkg = require( './../package.json' ).name;
29+
var gdot = require( './../lib/main.js' );
30+
31+
32+
// FUNCTIONS //
33+
34+
/**
35+
* Creates a benchmark function.
36+
*
37+
* @private
38+
* @param {PositiveInteger} len - array length
39+
* @returns {Function} benchmark function
40+
*/
41+
function createBenchmark( len ) {
42+
var x;
43+
var y;
44+
var i;
45+
46+
x = new Float64Array( len );
47+
y = new Float64Array( len );
48+
for ( i = 0; i < len; i++ ) {
49+
x[ i ] = ( randu()*10.0 ) - 20.0;
50+
y[ i ] = ( randu()*10.0 ) - 20.0;
51+
}
52+
return benchmark;
53+
54+
function benchmark( b ) {
55+
var d;
56+
var i;
57+
58+
b.tic();
59+
for ( i = 0; i < b.iterations; i++ ) {
60+
d = gdot( x, y );
61+
if ( isnan( d ) ) {
62+
b.fail( 'should not return NaN' );
63+
}
64+
}
65+
b.toc();
66+
if ( isnan( d ) ) {
67+
b.fail( 'should not return NaN' );
68+
}
69+
b.pass( 'benchmark finished' );
70+
b.end();
71+
}
72+
}
73+
74+
75+
// MAIN //
76+
77+
/**
78+
* Main execution sequence.
79+
*
80+
* @private
81+
*/
82+
function main() {
83+
var len;
84+
var min;
85+
var max;
86+
var f;
87+
var i;
88+
89+
min = 1; // 10^min
90+
max = 6; // 10^max
91+
92+
for ( i = min; i <= max; i++ ) {
93+
len = pow( 10, i );
94+
f = createBenchmark( len );
95+
bench( pkg+'::float64:len='+len, f );
96+
}
97+
}
98+
99+
main();

0 commit comments

Comments
 (0)