Skip to content

Commit 01f0a28

Browse files
kgrytesaurabhraghuvanshii
authored andcommitted
build: add docs template
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent eba17fb commit 01f0a28

File tree

1 file changed

+107
-0
lines changed
  • lib/node_modules/@stdlib/ndarray/base/unary-accumulate/scripts/templates

1 file changed

+107
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#### stdlib_ndarray_accumulate_{{SIGNATURE}}( \*arrays\[], \*fcn )
2+
3+
Performs a reduction over elements in an input ndarray.
4+
5+
```c
6+
#include "stdlib/ndarray/dtypes.h"
7+
#include "stdlib/ndarray/index_modes.h"
8+
#include "stdlib/ndarray/orders.h"
9+
#include "stdlib/ndarray/ctor.h"{{EXAMPLE_INCLUDES}}
10+
#include <stdint.h>
11+
#include <stdlib.h>
12+
#include <stdio.h>
13+
14+
// Define the ndarray data types:
15+
enum STDLIB_NDARRAY_DTYPE xdtype = STDLIB_NDARRAY_{{INPUT_NDARRAY_1_DTYPE_UPPER}};
16+
enum STDLIB_NDARRAY_DTYPE idtype = STDLIB_NDARRAY_{{OUTPUT_NDARRAY_DTYPE_UPPER}};
17+
enum STDLIB_NDARRAY_DTYPE odtype = STDLIB_NDARRAY_{{OUTPUT_NDARRAY_DTYPE_UPPER}};
18+
19+
// Create underlying byte arrays:
20+
double xvalues[] = { {{INPUT_NDARRAY_1_VALUES_2D}} };
21+
double ivalues[] = { {{INITIAL_NDARRAY_VALUES_2D}} };
22+
double ovalues[] = { {{OUTPUT_NDARRAY_VALUES_2D}} };
23+
24+
uint8_t *xbuf = (uint8_t *)xvalues;
25+
uint8_t *ibuf = (uint8_t *)ivalues;
26+
uint8_t *obuf = (uint8_t *)ovalues;
27+
28+
// Define the number of dimensions:
29+
int64_t ndims = 2;
30+
31+
// Define the array shapes:
32+
int64_t xsh[] = { {{NDARRAY_SHAPE_2D}} };
33+
int64_t ish[] = {};
34+
int64_t osh[] = {};
35+
36+
// Define the strides:
37+
int64_t sx[] = { {{INPUT_NDARRAY_1_STRIDES_2D}} };
38+
int64_t si[] = { 0 };
39+
int64_t so[] = { 0 };
40+
41+
// Define the offsets:
42+
int64_t ox = 0;
43+
int64_t oi = 0;
44+
int64_t oo = 0;
45+
46+
// Define the array order:
47+
enum STDLIB_NDARRAY_ORDER order = STDLIB_NDARRAY_ROW_MAJOR;
48+
49+
// Specify the index mode:
50+
enum STDLIB_NDARRAY_INDEX_MODE imode = STDLIB_NDARRAY_INDEX_ERROR;
51+
52+
// Specify the subscript index modes:
53+
int8_t submodes[] = { imode };
54+
int64_t nsubmodes = 1;
55+
56+
// Create an input ndarray:
57+
struct ndarray *x = stdlib_ndarray_allocate( xdtype, xbuf, ndims, shape, sx, ox, order, imode, nsubmodes, submodes );
58+
if ( x == NULL ) {
59+
fprintf( stderr, "Error allocating memory.\n" );
60+
exit( EXIT_FAILURE );
61+
}
62+
63+
// Create a zero-dimensional initial value ndarray:
64+
struct ndarray *initial = stdlib_ndarray_allocate( idtype, ibuf, ndims, ish, si, oi, order, imode, nsubmodes, submodes );
65+
if ( initial == NULL ) {
66+
fprintf( stderr, "Error allocating memory.\n" );
67+
exit( EXIT_FAILURE );
68+
}
69+
70+
// Create a zero-dimensional output ndarray:
71+
struct ndarray *out = stdlib_ndarray_allocate( odtype, obuf, ndims, osh, so, oo, order, imode, nsubmodes, submodes );
72+
if ( out == NULL ) {
73+
fprintf( stderr, "Error allocating memory.\n" );
74+
exit( EXIT_FAILURE );
75+
}
76+
77+
// Create an array containing the ndarrays:
78+
struct ndarray *arrays[] = { x, initial, out };
79+
80+
// Define a callback:
81+
static {{CALLBACK_RETURN_DTYPE}} fcn( const {{CALLBACK_PARAM_1_DTYPE}} acc, const {{CALLBACK_PARAM_2_DTYPE}} x ) {
82+
{{CALLBACK_BODY}}
83+
}
84+
85+
// Apply the callback:
86+
int8_t status = stdlib_ndarray_accumulate_{{SIGNATURE}}( arrays, (void *)fcn );
87+
if ( status != 0 ) {
88+
fprintf( stderr, "Error during computation.\n" );
89+
exit( EXIT_FAILURE );
90+
}
91+
92+
// ...
93+
94+
// Free allocated memory:
95+
stdlib_ndarray_free( x );
96+
stdlib_ndarray_free( initial );
97+
stdlib_ndarray_free( out );
98+
```
99+
100+
The function accepts the following arguments:
101+
102+
- **arrays**: `[inout] struct ndarray**` array whose first element is a pointer to an input ndarray, second element is a pointer to a zero-dimensional initial value ndarray, and last element is a pointer to a zero-dimensional output ndarray.
103+
- **fcn**: `[in] void*` a `{{CALLBACK_RETURN_DTYPE}} (*f)({{CALLBACK_PARAM_1_DTYPE}}, {{CALLBACK_PARAM_2_DTYPE}})` function to apply provided as a `void` pointer.
104+
105+
```c
106+
int8_t stdlib_ndarray_accumulate_{{SIGNATURE}}( struct ndarray *arrays[], void *fcn );
107+
```

0 commit comments

Comments
 (0)