Skip to content

Commit 425861e

Browse files
committed
Add archavercosine
1 parent e82dc8c commit 425861e

File tree

14 files changed

+652
-0
lines changed

14 files changed

+652
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Archavercosine
2+
3+
> Compute the [inverse half-value versed cosine][archavercosine].
4+
5+
6+
<section class="intro">
7+
8+
The [inverse half-value versed cosine][archavercosine] is defined as
9+
10+
<!-- <equation class="equation" label="eq:archavercosine" align="center" raw="\operatorname{ahavercos}(\theta) = 2 \cdot \acos(\sqrt{\theta})" alt="Inverse half-value versed cosine."> -->
11+
12+
<div class="equation" align="center" data-raw-text="\operatorname{ahavercos}(\theta) = 2 \cdot \acos(\sqrt{\theta})" data-equation="eq:archavercosine">
13+
<img src="" alt="Inverse half-value versed cosine.">
14+
<br>
15+
</div>
16+
17+
<!-- </equation> -->
18+
19+
</section>
20+
21+
<!-- /.intro -->
22+
23+
24+
<section class="usage">
25+
26+
## Usage
27+
28+
``` javascript
29+
var ahavercos = require( '@stdlib/math/base/special/ahavercos' );
30+
```
31+
32+
#### ahavercos( x )
33+
34+
Computes the [inverse half-value versed cosine][archavercosine].
35+
36+
``` javascript
37+
var v = ahavercos( 0.0 );
38+
// returns ~3.1416
39+
40+
v = ahavercos( 1.0 );
41+
// returns 0.0
42+
43+
v = ahavercos( 0.5 );
44+
// returns ~1.5708
45+
```
46+
47+
If `x < 0`, `x > 1`, or `x` is `NaN`, the function returns `NaN`.
48+
49+
``` javascript
50+
var v = ahavercos( 1.5 );
51+
// returns NaN
52+
53+
v = ahavercos( -3.14 );
54+
// returns NaN
55+
56+
v = ahavercos( NaN );
57+
// returns NaN
58+
```
59+
60+
</section>
61+
62+
<!-- /.usage -->
63+
64+
65+
<section class="examples">
66+
67+
## Examples
68+
69+
``` javascript
70+
var linspace = require( '@stdlib/math/utils/linspace' );
71+
var ahavercos = require( '@stdlib/math/base/special/ahavercos' );
72+
73+
var x = linspace( 0.0, 1.0, 100 );
74+
var i;
75+
76+
for ( i = 0; i < x.length; i++ ) {
77+
console.log( ahavercos( x[ i ] ) );
78+
}
79+
```
80+
81+
</section>
82+
83+
<!-- /.examples -->
84+
85+
86+
<section class="links">
87+
88+
[archavercosine]: https://en.wikipedia.org/wiki/Versine
89+
90+
</section>
91+
92+
<!-- /.links -->
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
// MODULES //
4+
5+
var bench = require( '@stdlib/bench' );
6+
var randu = require( '@stdlib/math/base/random/randu' );
7+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
8+
var pkg = require( './../package.json' ).name;
9+
var ahavercos = require( './../lib' );
10+
11+
12+
// MAIN //
13+
14+
bench( pkg, function benchmark( b ) {
15+
var x;
16+
var y;
17+
var i;
18+
19+
b.tic();
20+
for ( i = 0; i < b.iterations; i++ ) {
21+
x = randu();
22+
y = ahavercos( x );
23+
if ( isnan( y ) ) {
24+
b.fail( 'should not return NaN' );
25+
}
26+
}
27+
b.toc();
28+
if ( isnan( y ) ) {
29+
b.fail( 'should not return NaN' );
30+
}
31+
b.pass( 'benchmark finished' );
32+
b.end();
33+
});
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
2+
# VARIABLES #
3+
4+
ifndef VERBOSE
5+
QUIET := @
6+
endif
7+
8+
# Determine the OS:
9+
#
10+
# [1]: https://en.wikipedia.org/wiki/Uname#Examples
11+
# [2]: http://stackoverflow.com/a/27776822/2225624
12+
OS ?= $(shell uname)
13+
ifneq (, $(findstring MINGW,$(OS)))
14+
OS := WINNT
15+
else
16+
ifneq (, $(findstring MSYS,$(OS)))
17+
OS := WINNT
18+
else
19+
ifneq (, $(findstring CYGWIN,$(OS)))
20+
OS := WINNT
21+
endif
22+
endif
23+
endif
24+
25+
# Define the program used for compiling C source files:
26+
ifdef C_COMPILER
27+
CC := $(C_COMPILER)
28+
else
29+
CC := gcc
30+
endif
31+
32+
# Define the command-line options when compiling C files:
33+
CFLAGS ?= \
34+
-std=c99 \
35+
-O3 \
36+
-Wall \
37+
-pedantic
38+
39+
# Determine whether to generate [position independent code][1]:
40+
#
41+
# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options
42+
# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option
43+
ifeq ($(OS), WINNT)
44+
fPIC ?=
45+
else
46+
fPIC ?= -fPIC
47+
endif
48+
49+
# List of C targets:
50+
c_targets := benchmark.out
51+
52+
53+
# TARGETS #
54+
55+
# Default target.
56+
#
57+
# This target is the default target.
58+
59+
all: $(c_targets)
60+
61+
.PHONY: all
62+
63+
64+
# Compile C source.
65+
#
66+
# This target compiles C source files.
67+
68+
$(c_targets): %.out: %.c
69+
$(QUIET) $(CC) $(CFLAGS) $(fPIC) -o $@ $< -lm
70+
71+
72+
# Run a benchmark.
73+
#
74+
# This target runs a benchmark.
75+
76+
run: $(c_targets)
77+
$(QUIET) ./$<
78+
79+
.PHONY: run
80+
81+
82+
# Perform clean-up.
83+
#
84+
# This target removes generated files.
85+
86+
clean:
87+
$(QUIET) -rm -f *.o *.out
88+
89+
.PHONY: clean
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/**
2+
* Benchmark `ahavercos`.
3+
*/
4+
#include <stdlib.h>
5+
#include <stdio.h>
6+
#include <math.h>
7+
#include <sys/time.h>
8+
9+
#define NAME "ahavercos"
10+
#define ITERATIONS 1000000
11+
#define REPEATS 3
12+
13+
/**
14+
* Prints the TAP version.
15+
*/
16+
void print_version() {
17+
printf( "TAP version 13\n" );
18+
}
19+
20+
/**
21+
* Prints the TAP summary.
22+
*
23+
* @param total total number of tests
24+
* @param passing total number of passing tests
25+
*/
26+
void print_summary( int total, int passing ) {
27+
printf( "#\n" );
28+
printf( "1..%d\n", total ); // TAP plan
29+
printf( "# total %d\n", total );
30+
printf( "# pass %d\n", passing );
31+
printf( "#\n" );
32+
printf( "# ok\n" );
33+
}
34+
35+
/**
36+
* Prints benchmarks results.
37+
*
38+
* @param elapsed elapsed time in seconds
39+
*/
40+
void print_results( double elapsed ) {
41+
double rate = (double)ITERATIONS / elapsed;
42+
printf( " ---\n" );
43+
printf( " iterations: %d\n", ITERATIONS );
44+
printf( " elapsed: %0.9f\n", elapsed );
45+
printf( " rate: %0.9f\n", rate );
46+
printf( " ...\n" );
47+
}
48+
49+
/**
50+
* Returns a clock time.
51+
*
52+
* @returns clock time
53+
*/
54+
double tic() {
55+
struct timeval now;
56+
gettimeofday( &now, NULL );
57+
return (double)now.tv_sec + (double)now.tv_usec/1.0e6;
58+
}
59+
60+
/**
61+
* Generates a random double on the interval [0,1].
62+
*
63+
* @return random double
64+
*/
65+
double rand_double() {
66+
int r = rand();
67+
return (double)r / ( (double)RAND_MAX + 1.0 );
68+
}
69+
70+
/**
71+
* Runs a benchmark.
72+
*
73+
* @return elapsed time in seconds
74+
*/
75+
double benchmark() {
76+
double elapsed;
77+
double x;
78+
double y;
79+
double t;
80+
int i;
81+
82+
t = tic();
83+
for ( i = 0; i < ITERATIONS; i++ ) {
84+
x = rand_double();
85+
y = 2.0 * acos( sqrt( x ) );
86+
if ( y != y ) {
87+
printf( "should not return NaN\n" );
88+
break;
89+
}
90+
}
91+
elapsed = tic() - t;
92+
if ( y != y ) {
93+
printf( "should not return NaN\n" );
94+
}
95+
return elapsed;
96+
}
97+
98+
/**
99+
* Main execution sequence.
100+
*/
101+
int main( void ) {
102+
double elapsed;
103+
int i;
104+
105+
// Use the current time to seed the random number generator:
106+
srand( time( NULL ) );
107+
108+
print_version();
109+
for ( i = 0; i < REPEATS; i++ ) {
110+
printf( "# c::%s\n", NAME );
111+
elapsed = benchmark();
112+
print_results( elapsed );
113+
printf( "ok %d benchmark finished\n", i+1 );
114+
}
115+
print_summary( REPEATS, REPEATS );
116+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
{{alias}}( x )
3+
Computes the inverse half-value versed cosine.
4+
5+
The inverse half-value versed cosine is defined as `2*acos(sqrt(x))`.
6+
7+
If `x < 0`, `x > 1`, or `x` is `NaN`, the function returns `NaN`.
8+
9+
Parameters
10+
----------
11+
x: number
12+
Input value.
13+
14+
Returns
15+
-------
16+
y: number
17+
Inverse half-value versed cosine.
18+
19+
Examples
20+
--------
21+
> var y = {{alias}}( 0.5 )
22+
~1.5708
23+
> y = {{alias}}( 0.0 )
24+
~3.1416
25+
26+
See Also
27+
--------
28+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
var linspace = require( '@stdlib/math/utils/linspace' );
4+
var ahavercos = require( './../lib' );
5+
6+
var x = linspace( 0.0, 1.0, 100 );
7+
var i;
8+
9+
for ( i = 0; i < x.length; i++ ) {
10+
console.log( 'ahavercos(%d) = %d', x[ i ], ahavercos( x[ i ] ) );
11+
}

0 commit comments

Comments
 (0)