Skip to content

Commit c22da84

Browse files
aayush0325stdlib-bot
authored andcommitted
feat: add C ndarray interface and refactor implementation for stats/base/dmaxabssorted
PR-URL: stdlib-js#4181 Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent 5e50051 commit c22da84

24 files changed

+408
-277
lines changed

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

+131-24
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ limitations under the License.
3636
var dmaxabssorted = require( '@stdlib/stats/base/dmaxabssorted' );
3737
```
3838

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

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

@@ -56,18 +56,16 @@ The function has the following parameters:
5656

5757
- **N**: number of indexed elements.
5858
- **x**: sorted input [`Float64Array`][@stdlib/array/float64].
59-
- **stride**: index increment for `x`.
59+
- **strideX**: stride length for `x`.
6060

61-
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`,
61+
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`,
6262

6363
```javascript
6464
var Float64Array = require( '@stdlib/array/float64' );
65-
var floor = require( '@stdlib/math/base/special/floor' );
6665

6766
var x = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, 3.0, 3.0, 4.0, 2.0 ] );
68-
var N = floor( x.length / 2 );
6967

70-
var v = dmaxabssorted( N, x, 2 );
68+
var v = dmaxabssorted( 4, x, 2 );
7169
// returns 4.0
7270
```
7371

@@ -77,18 +75,15 @@ Note that indexing is relative to the first index. To introduce an offset, use [
7775

7876
```javascript
7977
var Float64Array = require( '@stdlib/array/float64' );
80-
var floor = require( '@stdlib/math/base/special/floor' );
8178

8279
var x0 = new Float64Array( [ 2.0, 1.0, 2.0, 2.0, -2.0, 2.0, 3.0, 4.0 ] );
8380
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
8481

85-
var N = floor( x0.length / 2 );
86-
87-
var v = dmaxabssorted( N, x1, 2 );
82+
var v = dmaxabssorted( 4, x1, 2 );
8883
// returns 4.0
8984
```
9085

91-
#### dmaxabssorted.ndarray( N, x, stride, offset )
86+
#### dmaxabssorted.ndarray( N, x, strideX, offsetX )
9287

9388
Computes the maximum absolute value of a sorted double-precision floating-point strided array using alternative indexing semantics.
9489

@@ -102,18 +97,16 @@ var v = dmaxabssorted.ndarray( x.length, x, 1, 0 );
10297

10398
The function has the following additional parameters:
10499

105-
- **offset**: starting index for `x`.
100+
- **offsetX**: starting index for `x`.
106101

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
102+
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
108103

109104
```javascript
110105
var Float64Array = require( '@stdlib/array/float64' );
111-
var floor = require( '@stdlib/math/base/special/floor' );
112106

113107
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 );
115108

116-
var v = dmaxabssorted.ndarray( N, x, 2, 1 );
109+
var v = dmaxabssorted.ndarray( 4, x, 2, 1 );
117110
// returns 4.0
118111
```
119112

@@ -139,16 +132,13 @@ var v = dmaxabssorted.ndarray( N, x, 2, 1 );
139132
<!-- eslint no-undef: "error" -->
140133

141134
```javascript
142-
var Float64Array = require( '@stdlib/array/float64' );
135+
var linspace = require( '@stdlib/array/linspace' );
143136
var dmaxabssorted = require( '@stdlib/stats/base/dmaxabssorted' );
144137

145-
var x;
146-
var i;
147-
148-
x = new Float64Array( 10 );
149-
for ( i = 0; i < x.length; i++ ) {
150-
x[ i ] = i - 5.0;
151-
}
138+
var options = {
139+
'dtype': 'float64'
140+
};
141+
var x = linspace( -5.0, 5.0, 10, options );
152142
console.log( x );
153143

154144
var v = dmaxabssorted( x.length, x, 1 );
@@ -159,6 +149,123 @@ console.log( v );
159149

160150
<!-- /.examples -->
161151

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/dmaxabssorted.h"
176+
```
177+
178+
#### stdlib_strided_dmaxabssorted( N, \*X, strideX )
179+
180+
Computes the maximum absolute value of a sorted 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_dmaxabssorted( 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_dmaxabssorted( const CBLAS_INT N, const double *X, const CBLAS_INT strideX );
197+
```
198+
199+
#### stdlib_strided_dmaxabssorted_ndarray( N, \*X, strideX, offsetX )
200+
201+
Computes the maximum absolute value of a sorted 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_dmaxabssorted_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_dmaxabssorted_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/dmaxabssorted.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 absolute value:
254+
double v = stdlib_strided_dmaxabssorted( 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+
162269
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
163270
164271
<section class="related">

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

+9-8
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,19 @@
2222

2323
var bench = require( '@stdlib/bench' );
2424
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var linspace = require( '@stdlib/array/linspace' );
2526
var pow = require( '@stdlib/math/base/special/pow' );
26-
var Float64Array = require( '@stdlib/array/float64' );
2727
var pkg = require( './../package.json' ).name;
2828
var dmaxabssorted = require( './../lib/dmaxabssorted.js' );
2929

3030

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

3340
/**
@@ -38,13 +45,7 @@ var dmaxabssorted = require( './../lib/dmaxabssorted.js' );
3845
* @returns {Function} benchmark function
3946
*/
4047
function createBenchmark( len ) {
41-
var x;
42-
var i;
43-
44-
x = new Float64Array( len );
45-
for ( i = 0; i < x.length; i++ ) {
46-
x[ i ] = i - (len/2);
47-
}
48+
var x = linspace( -len/2, len/2, len, options );
4849
return benchmark;
4950

5051
function benchmark( b ) {

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

+5-8
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var linspace = require( '@stdlib/array/linspace' );
2627
var pow = require( '@stdlib/math/base/special/pow' );
27-
var Float64Array = require( '@stdlib/array/float64' );
2828
var tryRequire = require( '@stdlib/utils/try-require' );
2929
var pkg = require( './../package.json' ).name;
3030

@@ -35,6 +35,9 @@ var dmaxabssorted = tryRequire( resolve( __dirname, './../lib/dmaxabssorted.nati
3535
var opts = {
3636
'skip': ( dmaxabssorted instanceof Error )
3737
};
38+
var options = {
39+
'dtype': 'float64'
40+
};
3841

3942

4043
// FUNCTIONS //
@@ -47,13 +50,7 @@ var opts = {
4750
* @returns {Function} benchmark function
4851
*/
4952
function createBenchmark( len ) {
50-
var x;
51-
var i;
52-
53-
x = new Float64Array( len );
54-
for ( i = 0; i < x.length; i++ ) {
55-
x[ i ] = i - (len/2);
56-
}
53+
var x = linspace( -len/2, len/2, len, options );
5754
return benchmark;
5855

5956
function benchmark( b ) {

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

+9-8
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,19 @@
2222

2323
var bench = require( '@stdlib/bench' );
2424
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var linspace = require( '@stdlib/array/linspace' );
2526
var pow = require( '@stdlib/math/base/special/pow' );
26-
var Float64Array = require( '@stdlib/array/float64' );
2727
var pkg = require( './../package.json' ).name;
2828
var dmaxabssorted = require( './../lib/ndarray.js' );
2929

3030

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

3340
/**
@@ -38,13 +45,7 @@ var dmaxabssorted = require( './../lib/ndarray.js' );
3845
* @returns {Function} benchmark function
3946
*/
4047
function createBenchmark( len ) {
41-
var x;
42-
var i;
43-
44-
x = new Float64Array( len );
45-
for ( i = 0; i < x.length; i++ ) {
46-
x[ i ] = i - (len/2);
47-
}
48+
var x = linspace( -len/2, len/2, len, options );
4849
return benchmark;
4950

5051
function benchmark( b ) {

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

+5-8
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var linspace = require( '@stdlib/array/linspace' );
2627
var pow = require( '@stdlib/math/base/special/pow' );
27-
var Float64Array = require( '@stdlib/array/float64' );
2828
var tryRequire = require( '@stdlib/utils/try-require' );
2929
var pkg = require( './../package.json' ).name;
3030

@@ -35,6 +35,9 @@ var dmaxabssorted = tryRequire( resolve( __dirname, './../lib/ndarray.native.js'
3535
var opts = {
3636
'skip': ( dmaxabssorted instanceof Error )
3737
};
38+
var options = {
39+
'dtype': 'float64'
40+
};
3841

3942

4043
// FUNCTIONS //
@@ -47,13 +50,7 @@ var opts = {
4750
* @returns {Function} benchmark function
4851
*/
4952
function createBenchmark( len ) {
50-
var x;
51-
var i;
52-
53-
x = new Float64Array( len );
54-
for ( i = 0; i < x.length; i++ ) {
55-
x[ i ] = i - (len/2);
56-
}
53+
var x = linspace( -len/2, len/2, len, options );
5754
return benchmark;
5855

5956
function benchmark( b ) {

0 commit comments

Comments
 (0)