Skip to content

Commit 59ab26d

Browse files
committed
docs: add c_x_as_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 ae76c53 commit 59ab26d

File tree

2 files changed

+277
-0
lines changed

2 files changed

+277
-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: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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/float32/ctor.h"
25+
#include "stdlib/complex/float64/real.h"
26+
#include "stdlib/complex/float64/imag.h"
27+
#include <stdbool.h>
28+
#include <stdint.h>
29+
#include <stdlib.h>
30+
#include <stdio.h>
31+
#include <inttypes.h>
32+
33+
static void print_ndarray_contents( const struct ndarray *x ) {
34+
int64_t i;
35+
int8_t s;
36+
bool v;
37+
38+
for ( i = 0; i < stdlib_ndarray_length( x ); i++ ) {
39+
s = stdlib_ndarray_iget_bool( x, i, &v );
40+
if ( s != 0 ) {
41+
fprintf( stderr, "Unable to resolve data element.\n" );
42+
exit( EXIT_FAILURE );
43+
}
44+
fprintf( stdout, "data[%"PRId64"] = %s\n", i, ( v ) ? "true" : "false" );
45+
}
46+
}
47+
48+
static bool fcn( const stdlib_complex128_t x ) {
49+
return ( stdlib_complex128_real( x ) > 0.0 && stdlib_complex128_imag( x ) > 0.0 );
50+
}
51+
52+
int main( void ) {
53+
// Define the ndarray data types:
54+
enum STDLIB_NDARRAY_DTYPE xdtype = STDLIB_NDARRAY_COMPLEX64;
55+
enum STDLIB_NDARRAY_DTYPE ydtype = STDLIB_NDARRAY_BOOL;
56+
57+
// Create underlying byte arrays:
58+
stdlib_complex64_t xvalues[] = {
59+
stdlib_complex64( 1.0f, 1.0f ),
60+
stdlib_complex64( 2.0f, 2.0f ),
61+
stdlib_complex64( 3.0f, 3.0f ),
62+
stdlib_complex64( 4.0f, 4.0f ),
63+
stdlib_complex64( 5.0f, 5.0f ),
64+
stdlib_complex64( 6.0f, 6.0f ),
65+
stdlib_complex64( 7.0f, 7.0f ),
66+
stdlib_complex64( 8.0f, 0.0f )
67+
};
68+
69+
// cppcheck-suppress invalidPointerCast
70+
uint8_t *xbuf = (uint8_t *)xvalues;
71+
uint8_t ybuf[] = { 0 };
72+
73+
// Define the number of dimensions:
74+
int64_t ndims = 3;
75+
76+
// Define the array shapes:
77+
int64_t shx[] = { 2, 2, 2 };
78+
int64_t *shy = NULL;
79+
80+
// Define the strides:
81+
int64_t sx[] = { 32, 16, 8 };
82+
int64_t sy[] = { 0 };
83+
84+
// Define the offsets:
85+
int64_t ox = 0;
86+
int64_t oy = 0;
87+
88+
// Define the array order:
89+
enum STDLIB_NDARRAY_ORDER order = STDLIB_NDARRAY_ROW_MAJOR;
90+
91+
// Specify the index mode:
92+
enum STDLIB_NDARRAY_INDEX_MODE imode = STDLIB_NDARRAY_INDEX_ERROR;
93+
94+
// Specify the subscript index modes:
95+
int8_t submodes[] = { imode };
96+
int64_t nsubmodes = 1;
97+
98+
// Create an input ndarray:
99+
struct ndarray *x = stdlib_ndarray_allocate( xdtype, xbuf, ndims, shx, sx, ox, order, imode, nsubmodes, submodes );
100+
if ( x == NULL ) {
101+
fprintf( stderr, "Error allocating memory.\n" );
102+
exit( EXIT_FAILURE );
103+
}
104+
105+
// Create an output ndarray:
106+
struct ndarray *y = stdlib_ndarray_allocate( ydtype, ybuf, 0, shy, sy, oy, order, imode, nsubmodes, submodes );
107+
if ( y == NULL ) {
108+
fprintf( stderr, "Error allocating memory.\n" );
109+
exit( EXIT_FAILURE );
110+
}
111+
112+
// Define an array containing the ndarrays:
113+
struct ndarray *arrays[] = { x, y };
114+
115+
// Test elements:
116+
int8_t status = stdlib_ndarray_every_by_c_x_as_z_x( arrays, (void *)fcn );
117+
if ( status != 0 ) {
118+
fprintf( stderr, "Error during computation.\n" );
119+
exit( EXIT_FAILURE );
120+
}
121+
122+
// Print the results:
123+
print_ndarray_contents( y );
124+
fprintf( stdout, "\n" );
125+
126+
// Free allocated memory:
127+
stdlib_ndarray_free( x );
128+
stdlib_ndarray_free( y );
129+
}

0 commit comments

Comments
 (0)