Skip to content

Commit 46918f4

Browse files
committed
Add C implementation
1 parent 8dfe681 commit 46918f4

File tree

15 files changed

+729
-30
lines changed

15 files changed

+729
-30
lines changed

lib/node_modules/@stdlib/math/base/special/cabs/README.md

+91-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ limitations under the License.
2020

2121
# Absolute Value
2222

23-
> Compute an [absolute value][absolute-value] of a double-precision [complex][@stdlib/complex/float64] floating-point number.
23+
> Compute the [absolute value][absolute-value] of a double-precision [complex][@stdlib/complex/float64] floating-point number.
2424
2525
<section class="intro">
2626

@@ -89,6 +89,96 @@ for ( i = 0; i < 100; i++ ) {
8989

9090
<!-- /.examples -->
9191

92+
<!-- C interface documentation. -->
93+
94+
* * *
95+
96+
<section class="c">
97+
98+
## C APIs
99+
100+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
101+
102+
<section class="intro">
103+
104+
</section>
105+
106+
<!-- /.intro -->
107+
108+
<!-- C usage documentation. -->
109+
110+
<section class="usage">
111+
112+
### Usage
113+
114+
```c
115+
#include "stdlib/math/base/special/cabs.h"
116+
```
117+
118+
#### stdlib_base_cabs( z )
119+
120+
Computes an [absolute value][absolute-value] of a double-precision complex floating-point number.
121+
122+
```c
123+
#include <complex.h>
124+
125+
double y = stdlib_base_cabs( 5.0+3.0*I );
126+
// returns ~5.83
127+
```
128+
129+
The function accepts the following arguments:
130+
131+
- **z**: `[in] double complex` input value.
132+
133+
```c
134+
double stdlib_base_cabs( const double complex z );
135+
```
136+
137+
</section>
138+
139+
<!-- /.usage -->
140+
141+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
142+
143+
<section class="notes">
144+
145+
</section>
146+
147+
<!-- /.notes -->
148+
149+
<!-- C API usage examples. -->
150+
151+
<section class="examples">
152+
153+
### Examples
154+
155+
```c
156+
#include "stdlib/math/base/special/cabs.h"
157+
#include <stdio.h>
158+
#include <complex.h>
159+
160+
int main() {
161+
double complex x[] = { 3.14+1.0*I, -3.14-1.0*I, 0.0+0.0*I, 0.0/0.0+0.0/0.0*I };
162+
163+
double complex v;
164+
double y;
165+
int i;
166+
for ( i = 0; i < 4; i++ ) {
167+
v = x[ i ];
168+
y = stdlib_base_cabs( v );
169+
printf( "f(%lf + %lf) = %lf\n", creal( v ), cimag( v ), y );
170+
}
171+
}
172+
```
173+
174+
</section>
175+
176+
<!-- /.examples -->
177+
178+
</section>
179+
180+
<!-- /.c -->
181+
92182
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
93183

94184
<section class="related">

lib/node_modules/@stdlib/math/base/special/cabs/benchmark/benchmark.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2018 The Stdlib Authors.
4+
* Copyright (c) 2021 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.

lib/node_modules/@stdlib/math/base/special/cabs/benchmark/c/Makefile

+39-20
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#/
22
# @license Apache-2.0
33
#
4-
# Copyright (c) 2018 The Stdlib Authors.
4+
# Copyright (c) 2021 The Stdlib Authors.
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
@@ -16,14 +16,15 @@
1616
# limitations under the License.
1717
#/
1818

19-
2019
# VARIABLES #
2120

2221
ifndef VERBOSE
2322
QUIET := @
23+
else
24+
QUIET :=
2425
endif
2526

26-
# Determine the OS:
27+
# Determine the OS ([1][1], [2][2]).
2728
#
2829
# [1]: https://en.wikipedia.org/wiki/Uname#Examples
2930
# [2]: http://stackoverflow.com/a/27776822/2225624
@@ -36,6 +37,10 @@ ifneq (, $(findstring MSYS,$(OS)))
3637
else
3738
ifneq (, $(findstring CYGWIN,$(OS)))
3839
OS := WINNT
40+
else
41+
ifneq (, $(findstring Windows_NT,$(OS)))
42+
OS := WINNT
43+
endif
3944
endif
4045
endif
4146
endif
@@ -54,7 +59,7 @@ CFLAGS ?= \
5459
-Wall \
5560
-pedantic
5661

57-
# Determine whether to generate [position independent code][1]:
62+
# Determine whether to generate position independent code ([1][1], [2][2]).
5863
#
5964
# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options
6065
# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option
@@ -68,39 +73,53 @@ endif
6873
c_targets := benchmark.out
6974

7075

71-
# TARGETS #
76+
# RULES #
7277

73-
# Default target.
78+
#/
79+
# Compiles C source files.
7480
#
75-
# This target is the default target.
76-
81+
# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`)
82+
# @param {string} [CFLAGS] - C compiler options
83+
# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code (e.g., `-fPIC`)
84+
#
85+
# @example
86+
# make
87+
#
88+
# @example
89+
# make all
90+
#/
7791
all: $(c_targets)
7892

7993
.PHONY: all
8094

81-
82-
# Compile C source.
95+
#/
96+
# Compiles C source files.
8397
#
84-
# This target compiles C source files.
85-
98+
# @private
99+
# @param {string} CC - C compiler
100+
# @param {string} CFLAGS - C compiler flags
101+
# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code
102+
#/
86103
$(c_targets): %.out: %.c
87104
$(QUIET) $(CC) $(CFLAGS) $(fPIC) -o $@ $< -lm
88105

89-
90-
# Run a benchmark.
106+
#/
107+
# Runs compiled benchmarks.
91108
#
92-
# This target runs a benchmark.
93-
109+
# @example
110+
# make run
111+
#/
94112
run: $(c_targets)
95113
$(QUIET) ./$<
96114

97115
.PHONY: run
98116

99-
100-
# Perform clean-up.
117+
#/
118+
# Removes generated files.
101119
#
102-
# This target removes generated files.
103-
120+
# @example
121+
# make clean
122+
#/
104123
clean:
105124
$(QUIET) -rm -f *.o *.out
106125

lib/node_modules/@stdlib/math/base/special/cabs/benchmark/c/benchmark.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ double tic() {
7777
}
7878

7979
/**
80-
* Generates a random double on the interval [0,1].
80+
* Generates a random number on the interval [0,1].
8181
*
82-
* @return random double
82+
* @return random number
8383
*/
8484
double rand_double() {
8585
int r = rand();
@@ -92,6 +92,7 @@ double rand_double() {
9292
* @return elapsed time in seconds
9393
*/
9494
double benchmark() {
95+
double complex z;
9596
double elapsed;
9697
double re;
9798
double im;
@@ -103,7 +104,7 @@ double benchmark() {
103104
for ( i = 0; i < ITERATIONS; i++ ) {
104105
re = ( 1000.0*rand_double() ) - 500.0;
105106
im = ( 1000.0*rand_double() ) - 500.0;
106-
double complex z = re + im*I;
107+
z = re + im*I;
107108
y = cabs( z );
108109
if ( y != y ) {
109110
printf( "should not return NaN\n" );

0 commit comments

Comments
 (0)