Skip to content

Commit ae76c53

Browse files
committed
docs: add z_x example
--- 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: passed - 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 86cff92 commit ae76c53

File tree

2 files changed

+271
-0
lines changed

2 files changed

+271
-0
lines changed
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
#/
2+
# @license Apache-2.0
3+
#
4+
# Copyright (c) 2025 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+
# VARIABLES #
20+
21+
ifndef VERBOSE
22+
QUIET := @
23+
else
24+
QUIET :=
25+
endif
26+
27+
# Determine the OS ([1][1], [2][2]).
28+
#
29+
# [1]: https://en.wikipedia.org/wiki/Uname#Examples
30+
# [2]: http://stackoverflow.com/a/27776822/2225624
31+
OS ?= $(shell uname)
32+
ifneq (, $(findstring MINGW,$(OS)))
33+
OS := WINNT
34+
else
35+
ifneq (, $(findstring MSYS,$(OS)))
36+
OS := WINNT
37+
else
38+
ifneq (, $(findstring CYGWIN,$(OS)))
39+
OS := WINNT
40+
else
41+
ifneq (, $(findstring Windows_NT,$(OS)))
42+
OS := WINNT
43+
endif
44+
endif
45+
endif
46+
endif
47+
48+
# Define the program used for compiling C source files:
49+
ifdef C_COMPILER
50+
CC := $(C_COMPILER)
51+
else
52+
CC := gcc
53+
endif
54+
55+
# Define the command-line options when compiling C files:
56+
CFLAGS ?= \
57+
-std=c99 \
58+
-O3 \
59+
-Wall \
60+
-pedantic \
61+
-march=native \
62+
-flto
63+
64+
# Determine whether to generate position independent code ([1][1], [2][2]).
65+
#
66+
# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options
67+
# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option
68+
ifeq ($(OS), WINNT)
69+
fPIC ?=
70+
else
71+
fPIC ?= -fPIC
72+
endif
73+
74+
# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`):
75+
INCLUDE ?=
76+
77+
# List of source files:
78+
SOURCE_FILES ?=
79+
80+
# List of libraries (e.g., `-lopenblas -lpthread`):
81+
LIBRARIES ?=
82+
83+
# List of library paths (e.g., `-L /foo/bar -L /beep/boop`):
84+
LIBPATH ?=
85+
86+
# List of C targets:
87+
c_targets := example.out
88+
89+
90+
# RULES #
91+
92+
#/
93+
# Compiles source files.
94+
#
95+
# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`)
96+
# @param {string} [CFLAGS] - C compiler options
97+
# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`)
98+
# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`)
99+
# @param {string} [SOURCE_FILES] - list of source files
100+
# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`)
101+
# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`)
102+
#
103+
# @example
104+
# make
105+
#
106+
# @example
107+
# make all
108+
#/
109+
all: $(c_targets)
110+
111+
.PHONY: all
112+
113+
#/
114+
# Compiles C source files.
115+
#
116+
# @private
117+
# @param {string} CC - C compiler (e.g., `gcc`)
118+
# @param {string} CFLAGS - C compiler options
119+
# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`)
120+
# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar`)
121+
# @param {string} SOURCE_FILES - list of source files
122+
# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`)
123+
# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`)
124+
#/
125+
$(c_targets): %.out: %.c
126+
$(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES)
127+
128+
#/
129+
# Runs compiled examples.
130+
#
131+
# @example
132+
# make run
133+
#/
134+
run: $(c_targets)
135+
$(QUIET) ./$<
136+
137+
.PHONY: run
138+
139+
#/
140+
# Removes generated files.
141+
#
142+
# @example
143+
# make clean
144+
#/
145+
clean:
146+
$(QUIET) -rm -f *.o *.out
147+
148+
.PHONY: clean
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
#include "stdlib/ndarray/base/every.h"
20+
#include "stdlib/ndarray/dtypes.h"
21+
#include "stdlib/ndarray/index_modes.h"
22+
#include "stdlib/ndarray/orders.h"
23+
#include "stdlib/ndarray/ctor.h"
24+
#include "stdlib/complex/float64/ctor.h"
25+
#include <stdbool.h>
26+
#include <stdint.h>
27+
#include <stdlib.h>
28+
#include <stdio.h>
29+
#include <inttypes.h>
30+
31+
static void print_ndarray_contents( const struct ndarray *x ) {
32+
int64_t i;
33+
int8_t s;
34+
bool v;
35+
36+
for ( i = 0; i < stdlib_ndarray_length( x ); i++ ) {
37+
s = stdlib_ndarray_iget_bool( x, i, &v );
38+
if ( s != 0 ) {
39+
fprintf( stderr, "Unable to resolve data element.\n" );
40+
exit( EXIT_FAILURE );
41+
}
42+
fprintf( stdout, "data[%"PRId64"] = %s\n", i, ( v ) ? "true" : "false" );
43+
}
44+
}
45+
46+
int main( void ) {
47+
// Define the ndarray data types:
48+
enum STDLIB_NDARRAY_DTYPE xdtype = STDLIB_NDARRAY_COMPLEX128;
49+
enum STDLIB_NDARRAY_DTYPE ydtype = STDLIB_NDARRAY_BOOL;
50+
51+
// Create underlying byte arrays:
52+
stdlib_complex128_t xvalues[] = {
53+
stdlib_complex128( 1.0, 2.0 ),
54+
stdlib_complex128( 2.0, 0.0 ),
55+
stdlib_complex128( 3.0, 0.0 ),
56+
stdlib_complex128( 4.0, 0.0 ),
57+
stdlib_complex128( 0.0, 1.0 ),
58+
stdlib_complex128( 6.0, 0.0 ),
59+
stdlib_complex128( 7.0, 0.0 ),
60+
stdlib_complex128( 8.0, 0.0 )
61+
};
62+
63+
// cppcheck-suppress invalidPointerCast
64+
uint8_t *xbuf = (uint8_t *)xvalues;
65+
uint8_t ybuf[] = { 0 };
66+
67+
// Define the number of dimensions:
68+
int64_t ndims = 3;
69+
70+
// Define the array shapes:
71+
int64_t shx[] = { 2, 2, 2 };
72+
int64_t *shy = NULL;
73+
74+
// Define the strides:
75+
int64_t sx[] = { 64, 32, 16 };
76+
int64_t sy[] = { 0 };
77+
78+
// Define the offsets:
79+
int64_t ox = 0;
80+
int64_t oy = 0;
81+
82+
// Define the array order:
83+
enum STDLIB_NDARRAY_ORDER order = STDLIB_NDARRAY_ROW_MAJOR;
84+
85+
// Specify the index mode:
86+
enum STDLIB_NDARRAY_INDEX_MODE imode = STDLIB_NDARRAY_INDEX_ERROR;
87+
88+
// Specify the subscript index modes:
89+
int8_t submodes[] = { imode };
90+
int64_t nsubmodes = 1;
91+
92+
// Create an input ndarray:
93+
struct ndarray *x = stdlib_ndarray_allocate( xdtype, xbuf, ndims, shx, sx, ox, order, imode, nsubmodes, submodes );
94+
if ( x == NULL ) {
95+
fprintf( stderr, "Error allocating memory.\n" );
96+
exit( EXIT_FAILURE );
97+
}
98+
99+
// Create an output ndarray:
100+
struct ndarray *y = stdlib_ndarray_allocate( ydtype, ybuf, 0, shy, sy, oy, order, imode, nsubmodes, submodes );
101+
if ( y == NULL ) {
102+
fprintf( stderr, "Error allocating memory.\n" );
103+
exit( EXIT_FAILURE );
104+
}
105+
106+
// Define an array containing the ndarrays:
107+
struct ndarray *arrays[] = { x, y };
108+
109+
// Test elements:
110+
int8_t status = stdlib_ndarray_every_z_x( arrays, NULL );
111+
if ( status != 0 ) {
112+
fprintf( stderr, "Error during computation.\n" );
113+
exit( EXIT_FAILURE );
114+
}
115+
116+
// Print the results:
117+
print_ndarray_contents( y );
118+
fprintf( stdout, "\n" );
119+
120+
// Free allocated memory:
121+
stdlib_ndarray_free( x );
122+
stdlib_ndarray_free( y );
123+
}

0 commit comments

Comments
 (0)