Skip to content

Commit ed4c8d6

Browse files
AbhijitRaut04kgrytePlaneshifter
authored
feat: add support for secant functionality math/base/special/sec
PR-URL: #3027 Closes: #225 Co-authored-by: Athan Reines <[email protected]> Co-authored-by: Philipp Burckhardt <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Athan Reines <[email protected]>
1 parent b18921a commit ed4c8d6

39 files changed

+26662
-0
lines changed
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# Secant
22+
23+
> Evaluate the [secant][trigonometric-functions] of a number.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<section class="usage">
30+
31+
## Usage
32+
33+
```javascript
34+
var sec = require( '@stdlib/math/base/special/sec' );
35+
```
36+
37+
## sec( x )
38+
39+
Evaluates the [secant][trigonometric-functions] of `x` (in radians).
40+
41+
```javascript
42+
var v = sec( 0.0 );
43+
// returns 1.0
44+
45+
v = sec( 3.141592653589793/2.0 );
46+
// returns 16331239353195370.0
47+
48+
v = sec( -3.141592653589793/3.0 );
49+
// returns ~1.9999999999999996
50+
51+
v = sec( 3.141592653589793/3.0 );
52+
// returns ~1.9999999999999996
53+
54+
v = sec( NaN );
55+
// returns NaN
56+
```
57+
58+
</section>
59+
60+
<!-- /.usage -->
61+
62+
<section class="examples">
63+
64+
## Examples
65+
66+
<!-- eslint no-undef: "error" -->
67+
68+
```javascript
69+
var linspace = require( '@stdlib/array/base/linspace' );
70+
var PI = require( '@stdlib/constants/float64/pi' );
71+
var sec = require( '@stdlib/math/base/special/sec' );
72+
73+
var x = linspace( -PI/2.0, PI/2.0, 100 );
74+
75+
var i;
76+
for ( i = 0; i < x.length; i++ ) {
77+
console.log( sec( x[ i ] ) );
78+
}
79+
```
80+
81+
</section>
82+
83+
<!-- /.examples -->
84+
85+
<!-- C interface documentation. -->
86+
87+
* * *
88+
89+
<section class="c">
90+
91+
## C APIs
92+
93+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
94+
95+
<section class="intro">
96+
97+
</section>
98+
99+
<!-- /.intro -->
100+
101+
<!-- C usage documentation. -->
102+
103+
<section class="usage">
104+
105+
### Usage
106+
107+
```c
108+
#include "stdlib/math/base/special/sec.h"
109+
```
110+
111+
#### stdlib_base_sec( x )
112+
113+
Evaluates the [secant][trigonometric-functions] of `x` (in radians).
114+
115+
```c
116+
double out = stdlib_base_sec( 0.0 );
117+
// returns 1.0
118+
119+
out = stdlib_base_cos( 3.141592653589793 / 2.0 );
120+
// returns 6.123233995736766e-17
121+
```
122+
123+
The function accepts the following arguments:
124+
125+
- **x**: `[in] double` input value.
126+
127+
```c
128+
double stdlib_base_sec( const double x );
129+
```
130+
131+
</section>
132+
133+
<!-- /.usage -->
134+
135+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
136+
137+
<section class="notes">
138+
139+
</section>
140+
141+
<!-- /.notes -->
142+
143+
<!-- C API usage examples. -->
144+
145+
<section class="examples">
146+
147+
### Examples
148+
149+
```c
150+
#include "stdlib/math/base/special/sec.h"
151+
#include <stdio.h>
152+
153+
int main( void ) {
154+
const double x[] = { 0.523, 0.785, 1.047, 3.14 };
155+
156+
double y;
157+
int i;
158+
for ( i = 0; i < 4; i++ ) {
159+
y = stdlib_base_sec( x[ i ] );
160+
printf( "sec(%lf) = %lf\n", x[ i ], y );
161+
}
162+
}
163+
```
164+
165+
</section>
166+
167+
<!-- /.examples -->
168+
169+
</section>
170+
171+
<!-- /.c -->
172+
173+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
174+
175+
<section class="related">
176+
177+
</section>
178+
179+
<!-- /.related -->
180+
181+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
182+
183+
<section class="links">
184+
185+
[trigonometric-functions]: https://en.wikipedia.org/wiki/Trigonometric_functions
186+
187+
<!-- <related-links> -->
188+
189+
<!-- </related-links> -->
190+
191+
</section>
192+
193+
<!-- /.links -->
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var randu = require( '@stdlib/random/base/randu' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var cos = require( '@stdlib/math/base/special/cos' );
27+
var pkg = require( './../package.json' ).name;
28+
var sec = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var x;
35+
var y;
36+
var i;
37+
38+
b.tic();
39+
for ( i = 0; i < b.iterations; i++ ) {
40+
x = (randu() * 20.0) - 10.0;
41+
y = sec( x );
42+
if ( isnan( y ) ) {
43+
b.fail( 'should not return NaN' );
44+
}
45+
}
46+
b.toc();
47+
if ( isnan( y ) ) {
48+
b.fail( 'should not return NaN' );
49+
}
50+
b.pass( 'benchmark finished' );
51+
b.end();
52+
});
53+
54+
bench( pkg + '::built-in', function benchmark( b ) {
55+
var x;
56+
var y;
57+
var i;
58+
59+
b.tic();
60+
for ( i = 0; i < b.iterations; i++ ) {
61+
x = (randu() * 20.0) - 10.0;
62+
y = 1.0 / cos( x );
63+
if ( isnan( y ) ) {
64+
b.fail( 'should not return NaN' );
65+
}
66+
}
67+
b.toc();
68+
if ( isnan( y ) ) {
69+
b.fail( 'should not return NaN' );
70+
}
71+
b.pass( 'benchmark finished' );
72+
b.end();
73+
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
'use strict';
20+
21+
// MODULES //
22+
23+
var resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var randu = require( '@stdlib/random/base/randu' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var tryRequire = require( '@stdlib/utils/try-require' );
28+
var pkg = require( './../package.json' ).name;
29+
30+
31+
// VARIABLES //
32+
33+
var sec = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( sec instanceof Error )
36+
};
37+
38+
39+
// MAIN //
40+
41+
bench( pkg+'::native', opts, function benchmark( b ) {
42+
var x;
43+
var y;
44+
var i;
45+
46+
b.tic();
47+
for ( i = 0; i < b.iterations; i++ ) {
48+
x = (randu() * 20.0) - 10.0;
49+
y = sec( x );
50+
if ( isnan( y ) ) {
51+
b.fail( 'should not return NaN' );
52+
}
53+
}
54+
b.toc();
55+
if ( isnan( y ) ) {
56+
b.fail( 'should not return NaN' );
57+
}
58+
b.pass( 'benchmark finished' );
59+
b.end();
60+
});

0 commit comments

Comments
 (0)