Skip to content

Commit 826ecf0

Browse files
aayush0325kgrytestdlib-bot
authored
feat: add C ndarray interface and refactor implementation for stats/base/dmaxabs
PR-URL: #4170 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com> Co-authored-by: stdlib-bot <82920195+stdlib-bot@users.noreply.github.com>
1 parent 86a3364 commit 826ecf0

24 files changed

+412
-291
lines changed

lib/node_modules/@stdlib/stats/base/dmaxabs/README.md

Lines changed: 131 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,36 +36,33 @@ limitations under the License.
3636
var dmaxabs = require( '@stdlib/stats/base/dmaxabs' );
3737
```
3838

39-
#### dmaxabs( N, x, stride )
39+
#### dmaxabs( N, x, strideX )
4040

4141
Computes the maximum absolute value of a double-precision floating-point strided array `x`.
4242

4343
```javascript
4444
var Float64Array = require( '@stdlib/array/float64' );
4545

4646
var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
47-
var N = x.length;
4847

49-
var v = dmaxabs( N, x, 1 );
48+
var v = dmaxabs( x.length, x, 1 );
5049
// returns 2.0
5150
```
5251

5352
The function has the following parameters:
5453

5554
- **N**: number of indexed elements.
5655
- **x**: input [`Float64Array`][@stdlib/array/float64].
57-
- **stride**: index increment for `x`.
56+
- **strideX**: stride length for `x`.
5857

59-
The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to compute the maximum absolute value of every other element in `x`,
58+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the maximum absolute value of every other element in `x`,
6059

6160
```javascript
6261
var Float64Array = require( '@stdlib/array/float64' );
63-
var floor = require( '@stdlib/math/base/special/floor' );
6462

6563
var x = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ] );
66-
var N = floor( x.length / 2 );
6764

68-
var v = dmaxabs( N, x, 2 );
65+
var v = dmaxabs( 4, x, 2 );
6966
// returns 4.0
7067
```
7168

@@ -86,34 +83,31 @@ var v = dmaxabs( N, x1, 2 );
8683
// returns 4.0
8784
```
8885

89-
#### dmaxabs.ndarray( N, x, stride, offset )
86+
#### dmaxabs.ndarray( N, x, strideX, offsetX )
9087

9188
Computes the maximum absolute value of a double-precision floating-point strided array using alternative indexing semantics.
9289

9390
```javascript
9491
var Float64Array = require( '@stdlib/array/float64' );
9592

9693
var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
97-
var N = x.length;
9894

99-
var v = dmaxabs.ndarray( N, x, 1, 0 );
95+
var v = dmaxabs.ndarray( x.length, x, 1, 0 );
10096
// returns 2.0
10197
```
10298

10399
The function has the following additional parameters:
104100

105-
- **offset**: starting index for `x`.
101+
- **offsetX**: starting index for `x`.
106102

107-
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, the `offset` parameter supports indexing semantics based on a starting index. For example, to calculate the maximum absolute value for every other value in `x` starting from the second value
103+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to calculate the maximum absolute value for every other element in `x` starting from the second element
108104

109105
```javascript
110106
var Float64Array = require( '@stdlib/array/float64' );
111-
var floor = require( '@stdlib/math/base/special/floor' );
112107

113108
var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
114-
var N = floor( x.length / 2 );
115109

116-
var v = dmaxabs.ndarray( N, x, 2, 1 );
110+
var v = dmaxabs.ndarray( 4, x, 2, 1 );
117111
// returns 4.0
118112
```
119113

@@ -138,18 +132,13 @@ var v = dmaxabs.ndarray( N, x, 2, 1 );
138132
<!-- eslint no-undef: "error" -->
139133

140134
```javascript
141-
var randu = require( '@stdlib/random/base/randu' );
142-
var round = require( '@stdlib/math/base/special/round' );
135+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
143136
var Float64Array = require( '@stdlib/array/float64' );
144137
var dmaxabs = require( '@stdlib/stats/base/dmaxabs' );
145138

146-
var x;
147-
var i;
148-
149-
x = new Float64Array( 10 );
150-
for ( i = 0; i < x.length; i++ ) {
151-
x[ i ] = round( (randu()*100.0) - 50.0 );
152-
}
139+
var x = discreteUniform( 10, -50, 50, {
140+
'dtype': 'float64'
141+
});
153142
console.log( x );
154143

155144
var v = dmaxabs( x.length, x, 1 );
@@ -160,6 +149,123 @@ console.log( v );
160149

161150
<!-- /.examples -->
162151

152+
<!-- C interface documentation. -->
153+
154+
* * *
155+
156+
<section class="c">
157+
158+
## C APIs
159+
160+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
161+
162+
<section class="intro">
163+
164+
</section>
165+
166+
<!-- /.intro -->
167+
168+
<!-- C usage documentation. -->
169+
170+
<section class="usage">
171+
172+
### Usage
173+
174+
```c
175+
#include "stdlib/stats/base/dmaxabs.h"
176+
```
177+
178+
#### stdlib_strided_dmaxabs( N, \*X, strideX )
179+
180+
Computes the maximum absolute value of a double-precision floating-point strided array.
181+
182+
```c
183+
const double x[] = { 1.0, -2.0, -3.0, 4.0, -5.0, -6.0, 7.0, 8.0 };
184+
185+
double v = stdlib_strided_dmaxabs( 4, x, 2 );
186+
// returns 7.0
187+
```
188+
189+
The function accepts the following arguments:
190+
191+
- **N**: `[in] CBLAS_INT` number of indexed elements.
192+
- **X**: `[in] double*` input array.
193+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
194+
195+
```c
196+
double stdlib_strided_dmaxabs( const CBLAS_INT N, const double *X, const CBLAS_INT strideX );
197+
```
198+
199+
#### stdlib_strided_dmaxabs_ndarray( N, \*X, strideX, offsetX )
200+
201+
Computes the maximum absolute value of a double-precision floating-point strided array using alternative indexing semantics.
202+
203+
```c
204+
const double x[] = { 1.0, -2.0, -3.0, 4.0, -5.0, -6.0, 7.0, 8.0 };
205+
206+
double v = stdlib_strided_dmaxabs_ndarray( 4, x, 2, 0 );
207+
// returns 7.0
208+
```
209+
210+
The function accepts the following arguments:
211+
212+
- **N**: `[in] CBLAS_INT` number of indexed elements.
213+
- **X**: `[in] double*` input array.
214+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
215+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
216+
217+
```c
218+
double stdlib_strided_dmaxabs_ndarray( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
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/stats/base/dmaxabs.h"
241+
#include <stdio.h>
242+
243+
int main( void ) {
244+
// Create a strided array:
245+
const double x[] = { 1.0, -2.0, -3.0, 4.0, -5.0, -6.0, 7.0, 8.0 };
246+
247+
// Specify the number of elements:
248+
const int N = 4;
249+
250+
// Specify the stride length:
251+
const int strideX = 2;
252+
253+
// Compute the maximum value:
254+
double v = stdlib_strided_dmaxabs( N, x, strideX );
255+
256+
// Print the result:
257+
printf( "maxabs: %lf\n", v );
258+
}
259+
```
260+
261+
</section>
262+
263+
<!-- /.examples -->
264+
265+
</section>
266+
267+
<!-- /.c -->
268+
163269
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
164270
165271
<section class="related">

lib/node_modules/@stdlib/stats/base/dmaxabs/benchmark/benchmark.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,20 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var pow = require( '@stdlib/math/base/special/pow' );
27-
var Float64Array = require( '@stdlib/array/float64' );
2827
var pkg = require( './../package.json' ).name;
2928
var dmaxabs = require( './../lib/dmaxabs.js' );
3029

3130

31+
// VARIABLES //
32+
33+
var options = {
34+
'dtype': 'float64'
35+
};
36+
37+
3238
// FUNCTIONS //
3339

3440
/**
@@ -39,13 +45,7 @@ var dmaxabs = require( './../lib/dmaxabs.js' );
3945
* @returns {Function} benchmark function
4046
*/
4147
function createBenchmark( len ) {
42-
var x;
43-
var i;
44-
45-
x = new Float64Array( len );
46-
for ( i = 0; i < x.length; i++ ) {
47-
x[ i ] = ( randu()*20.0 ) - 10.0;
48-
}
48+
var x = uniform( len, -10.0, 10.0, options );
4949
return benchmark;
5050

5151
function benchmark( b ) {

lib/node_modules/@stdlib/stats/base/dmaxabs/benchmark/benchmark.native.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@
2222

2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
25-
var randu = require( '@stdlib/random/base/randu' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
2626
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2727
var pow = require( '@stdlib/math/base/special/pow' );
28-
var Float64Array = require( '@stdlib/array/float64' );
2928
var tryRequire = require( '@stdlib/utils/try-require' );
3029
var pkg = require( './../package.json' ).name;
3130

@@ -36,6 +35,9 @@ var dmaxabs = tryRequire( resolve( __dirname, './../lib/dmaxabs.native.js' ) );
3635
var opts = {
3736
'skip': ( dmaxabs instanceof Error )
3837
};
38+
var options = {
39+
'dtype': 'float64'
40+
};
3941

4042

4143
// FUNCTIONS //
@@ -48,13 +50,7 @@ var opts = {
4850
* @returns {Function} benchmark function
4951
*/
5052
function createBenchmark( len ) {
51-
var x;
52-
var i;
53-
54-
x = new Float64Array( len );
55-
for ( i = 0; i < x.length; i++ ) {
56-
x[ i ] = ( randu()*20.0 ) - 10.0;
57-
}
53+
var x = uniform( len, -10.0, 10.0, options );
5854
return benchmark;
5955

6056
function benchmark( b ) {

lib/node_modules/@stdlib/stats/base/dmaxabs/benchmark/benchmark.ndarray.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,20 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var pow = require( '@stdlib/math/base/special/pow' );
27-
var Float64Array = require( '@stdlib/array/float64' );
2827
var pkg = require( './../package.json' ).name;
2928
var dmaxabs = require( './../lib/ndarray.js' );
3029

3130

31+
// VARIABLES //
32+
33+
var options = {
34+
'dtype': 'float64'
35+
};
36+
37+
3238
// FUNCTIONS //
3339

3440
/**
@@ -39,13 +45,7 @@ var dmaxabs = require( './../lib/ndarray.js' );
3945
* @returns {Function} benchmark function
4046
*/
4147
function createBenchmark( len ) {
42-
var x;
43-
var i;
44-
45-
x = new Float64Array( len );
46-
for ( i = 0; i < x.length; i++ ) {
47-
x[ i ] = ( randu()*20.0 ) - 10.0;
48-
}
48+
var x = uniform( len, -10.0, 10.0, options );
4949
return benchmark;
5050

5151
function benchmark( b ) {

lib/node_modules/@stdlib/stats/base/dmaxabs/benchmark/benchmark.ndarray.native.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@
2222

2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
25-
var randu = require( '@stdlib/random/base/randu' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
2626
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2727
var pow = require( '@stdlib/math/base/special/pow' );
28-
var Float64Array = require( '@stdlib/array/float64' );
2928
var tryRequire = require( '@stdlib/utils/try-require' );
3029
var pkg = require( './../package.json' ).name;
3130

@@ -36,6 +35,9 @@ var dmaxabs = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) );
3635
var opts = {
3736
'skip': ( dmaxabs instanceof Error )
3837
};
38+
var options = {
39+
'dtype': 'float64'
40+
};
3941

4042

4143
// FUNCTIONS //
@@ -48,13 +50,7 @@ var opts = {
4850
* @returns {Function} benchmark function
4951
*/
5052
function createBenchmark( len ) {
51-
var x;
52-
var i;
53-
54-
x = new Float64Array( len );
55-
for ( i = 0; i < x.length; i++ ) {
56-
x[ i ] = ( randu()*20.0 ) - 10.0;
57-
}
53+
var x = uniform( len, -10.0, 10.0, options );
5854
return benchmark;
5955

6056
function benchmark( b ) {

0 commit comments

Comments
 (0)