Skip to content

Commit a1a5edf

Browse files
committed
Add strided interface to compute the squared absolute value over a float64 array
1 parent 14c5d2f commit a1a5edf

33 files changed

+3935
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,284 @@
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+
# dabs2
22+
23+
> Compute the [squared absolute value][@stdlib/math/base/special/abs2] for each element in a double-precision floating-point strided array.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var dabs2 = require( '@stdlib/math/strided/special/dabs2' );
37+
```
38+
39+
#### dabs2( N, x, strideX, y, strideY )
40+
41+
Computes the [squared absolute value][@stdlib/math/base/special/abs2] for each element in a double-precision floating-point strided array `x` and assigns the results to elements in a double-precision floating-point strided array `y`.
42+
43+
```javascript
44+
var Float64Array = require( '@stdlib/array/float64' );
45+
46+
var x = new Float64Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] );
47+
48+
// Compute the squared absolute values in-place:
49+
dabs2( x.length, x, 1, x, 1 );
50+
// x => <Float64Array>[ 4.0, 1.0, 9.0, 25.0, 16.0, 0.0, 1.0, 9.0 ]
51+
```
52+
53+
The function accepts the following arguments:
54+
55+
- **N**: number of indexed elements.
56+
- **x**: input [`Float64Array`][@stdlib/array/float64].
57+
- **strideX**: index increment for `x`.
58+
- **y**: output [`Float64Array`][@stdlib/array/float64].
59+
- **strideY**: index increment for `y`.
60+
61+
The `N` and `stride` parameters determine which elements in `x` and `y` are accessed at runtime. For example, to index every other value in `x` and to index the first `N` elements of `y` in reverse order,
62+
63+
```javascript
64+
var Float64Array = require( '@stdlib/array/float64' );
65+
var floor = require( '@stdlib/math/base/special/floor' );
66+
67+
var x = new Float64Array( [ -1.0, -2.0, -3.0, -4.0, -5.0, -6.0 ] );
68+
var y = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
69+
70+
var N = floor( x.length / 2 );
71+
72+
dabs2( N, x, 2, y, -1 );
73+
// y => <Float64Array>[ 25.0, 9.0, 1.0, 0.0, 0.0, 0.0 ]
74+
```
75+
76+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][@stdlib/array/float64] views.
77+
78+
```javascript
79+
var Float64Array = require( '@stdlib/array/float64' );
80+
var floor = require( '@stdlib/math/base/special/floor' );
81+
82+
// Initial arrays...
83+
var x0 = new Float64Array( [ -1.0, -2.0, -3.0, -4.0, -5.0, -6.0 ] );
84+
var y0 = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
85+
86+
// Create offset views...
87+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
88+
var y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*3 ); // start at 4th element
89+
90+
var N = floor( x0.length / 2 );
91+
92+
dabs2( N, x1, -2, y1, 1 );
93+
// y0 => <Float64Array>[ 0.0, 0.0, 0.0, 36.0, 16.0, 4.0 ]
94+
```
95+
96+
#### dabs2.ndarray( N, x, strideX, offsetX, y, strideY, offsetY )
97+
98+
Computes the [squared absolute value][@stdlib/math/base/special/abs2] for each element in a double-precision floating-point strided array `x` and assigns the results to elements in a double-precision floating-point strided array `y` using alternative indexing semantics.
99+
100+
```javascript
101+
var Float64Array = require( '@stdlib/array/float64' );
102+
103+
var x = new Float64Array( [ -1.0, -2.0, -3.0, -4.0, -5.0 ] );
104+
var y = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0 ] );
105+
106+
dabs2.ndarray( x.length, x, 1, 0, y, 1, 0 );
107+
// y => <Float64Array>[ 1.0, 4.0, 9.0, 16.0, 25.0 ]
108+
```
109+
110+
The function accepts the following additional arguments:
111+
112+
- **offsetX**: starting index for `x`.
113+
- **offsetY**: starting index for `y`.
114+
115+
While [`typed array`][@stdlib/array/float64] views mandate a view offset based on the underlying `buffer`, the `offsetX` and `offsetY` parameters support indexing semantics based on starting indices. For example, to index every other value in `x` starting from the second value and to index the last `N` elements in `y`,
116+
117+
```javascript
118+
var Float64Array = require( '@stdlib/array/float64' );
119+
var floor = require( '@stdlib/math/base/special/floor' );
120+
121+
var x = new Float64Array( [ -1.0, -2.0, -3.0, -4.0, -5.0, -6.0 ] );
122+
var y = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
123+
124+
var N = floor( x.length / 2 );
125+
126+
dabs2.ndarray( N, x, 2, 1, y, -1, y.length-1 );
127+
// y => <Float64Array>[ 0.0, 0.0, 0.0, 36.0, 16.0, 4.0 ]
128+
```
129+
130+
</section>
131+
132+
<!-- /.usage -->
133+
134+
<section class="notes">
135+
136+
</section>
137+
138+
<!-- /.notes -->
139+
140+
<section class="examples">
141+
142+
## Examples
143+
144+
<!-- eslint no-undef: "error" -->
145+
146+
```javascript
147+
var round = require( '@stdlib/math/base/special/round' );
148+
var randu = require( '@stdlib/random/base/randu' );
149+
var Float64Array = require( '@stdlib/array/float64' );
150+
var dabs2 = require( '@stdlib/math/strided/special/dabs2' );
151+
152+
var x = new Float64Array( 10 );
153+
var y = new Float64Array( 10 );
154+
155+
var i;
156+
for ( i = 0; i < x.length; i++ ) {
157+
x[ i ] = round( (randu()*200.0) - 100.0 );
158+
}
159+
console.log( x );
160+
console.log( y );
161+
162+
dabs2.ndarray( x.length, x, 1, 0, y, -1, y.length-1 );
163+
console.log( y );
164+
```
165+
166+
</section>
167+
168+
<!-- /.examples -->
169+
170+
<!-- C interface documentation. -->
171+
172+
* * *
173+
174+
<section class="c">
175+
176+
## C APIs
177+
178+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
179+
180+
<section class="intro">
181+
182+
</section>
183+
184+
<!-- /.intro -->
185+
186+
<!-- C usage documentation. -->
187+
188+
<section class="usage">
189+
190+
### Usage
191+
192+
```c
193+
#include "stdlib/math/strided/special/dabs2.h"
194+
```
195+
196+
#### stdlib_strided_dabs2( N, \*X, strideX, \*Y, strideY )
197+
198+
Computes the squared absolute value for each element in a double-precision floating-point strided array `X` and assigns the results to elements in a double-precision floating-point strided array `Y`.
199+
200+
```c
201+
double X[] = { -1.0, -2.0, -3.0, -4.0, -5.0, -6.0, -7.0, -8.0 };
202+
double Y[] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
203+
204+
int64_t N = 4;
205+
206+
stdlib_strided_dabs2( N, X, 2, Y, 2 );
207+
```
208+
209+
The function accepts the following arguments:
210+
211+
- **N**: `[in] int64_t` number of indexed elements.
212+
- **X**: `[in] double*` input array.
213+
- **strideX** `[in] int64_t` index increment for `X`.
214+
- **Y**: `[out] double*` output array.
215+
- **strideY**: `[in] int64_t` index increment for `Y`.
216+
217+
```c
218+
void stdlib_strided_dabs2( const int64_t N, const double *X, const int64_t strideX, double *Y, const int64_t strideY );
219+
```
220+
221+
</section>
222+
223+
<!-- /.usage -->
224+
225+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
226+
227+
<section class="notes">
228+
229+
</section>
230+
231+
<!-- /.notes -->
232+
233+
<!-- C API usage examples. -->
234+
235+
<section class="examples">
236+
237+
### Examples
238+
239+
```c
240+
#include "stdlib/math/strided/special/dabs2.h"
241+
#include <stdint.h>
242+
#include <stdio.h>
243+
244+
int main() {
245+
// Create an input strided array:
246+
double X[] = { -1.0, -2.0, -3.0, -4.0, -5.0, -6.0, -7.0, -8.0 };
247+
248+
// Create an output strided array:
249+
double Y[] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
250+
251+
// Specify the number of elements:
252+
int64_t N = 4;
253+
254+
// Specify the stride lengths:
255+
int64_t strideX = 2;
256+
int64_t strideY = 2;
257+
258+
// Compute the squared absolute value element-wise:
259+
stdlib_strided_dabs2( N, X, strideX, Y, strideY );
260+
261+
// Print the result:
262+
for ( int i = 0; i < 8; i++ ) {
263+
printf( "Y[ %i ] = %lf\n", i, Y[ i ] );
264+
}
265+
}
266+
```
267+
268+
</section>
269+
270+
<!-- /.examples -->
271+
272+
</section>
273+
274+
<!-- /.c -->
275+
276+
<section class="links">
277+
278+
[@stdlib/array/float64]: https://github.com/stdlib-js/stdlib
279+
280+
[@stdlib/math/base/special/abs2]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/abs2
281+
282+
</section>
283+
284+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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 dabs2 = 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 < x.length; i++ ) {
49+
x[ i ] = ( randu()*200.0 ) - 100.0;
50+
}
51+
return benchmark;
52+
53+
/**
54+
* Benchmark function.
55+
*
56+
* @private
57+
* @param {Benchmark} b - benchmark instance
58+
*/
59+
function benchmark( b ) {
60+
var z;
61+
var i;
62+
63+
b.tic();
64+
for ( i = 0; i < b.iterations; i++ ) {
65+
z = dabs2( x.length, x, 1, y, 1 );
66+
if ( isnan( z[ i%len ] ) ) {
67+
b.fail( 'should not return NaN' );
68+
}
69+
}
70+
b.toc();
71+
if ( isnan( z[ i%len ] ) ) {
72+
b.fail( 'should not return NaN' );
73+
}
74+
b.pass( 'benchmark finished' );
75+
b.end();
76+
}
77+
}
78+
79+
80+
// MAIN //
81+
82+
/**
83+
* Main execution sequence.
84+
*
85+
* @private
86+
*/
87+
function main() {
88+
var len;
89+
var min;
90+
var max;
91+
var f;
92+
var i;
93+
94+
min = 1; // 10^min
95+
max = 6; // 10^max
96+
97+
for ( i = min; i <= max; i++ ) {
98+
len = pow( 10, i );
99+
f = createBenchmark( len );
100+
bench( pkg+':len='+len, f );
101+
}
102+
}
103+
104+
main();

0 commit comments

Comments
 (0)