Skip to content

Commit f17f40a

Browse files
MSP20086stdlib-botgunjjoshianandkaranubc
authored
feat: add math/base/special/sech
PR-URL: #1716 Closes: #228 Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]> Co-authored-by: Gunj Joshi <[email protected]> Co-authored-by: Karan Anand <[email protected]> Reviewed-by: Gunj Joshi <[email protected]> Reviewed-by: Karan Anand <[email protected]>
1 parent 9d89154 commit f17f40a

File tree

31 files changed

+2127
-0
lines changed

31 files changed

+2127
-0
lines changed
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# sech
22+
23+
> Compute the [hyperbolic cosecant][hyperbolic-functions] of a double-precision floating-point number.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var sech = require( '@stdlib/math/base/special/sech' );
31+
```
32+
33+
#### sech( x )
34+
35+
Computes the [hyperbolic cosecant][hyperbolic-functions] of a double-precision floating-point number.
36+
37+
```javascript
38+
var v = sech( 0.0 );
39+
// returns 1.0
40+
41+
v = sech( 2.0 );
42+
// returns ~0.2658
43+
44+
v = sech( -2.0 );
45+
// returns ~0.2658
46+
47+
v = sech( NaN );
48+
// returns NaN
49+
```
50+
51+
</section>
52+
53+
<!-- /.usage -->
54+
55+
<section class="examples">
56+
57+
## Examples
58+
59+
<!-- eslint no-undef: "error" -->
60+
61+
```javascript
62+
var uniform = require( '@stdlib/random/array/uniform' );
63+
var logEachMap = require( '@stdlib/console/log-each-map' );
64+
var sech = require( '@stdlib/math/base/special/sech' );
65+
66+
var opts = {
67+
'dtype': 'float64'
68+
};
69+
var x = uniform( 100, -5.0, 5.0, opts );
70+
71+
logEachMap( 'sech(%0.4f) = %0.4f', x, sech );
72+
```
73+
74+
</section>
75+
76+
<!-- /.examples -->
77+
78+
<!-- C interface documentation. -->
79+
80+
* * *
81+
82+
<section class="c">
83+
84+
## C APIs
85+
86+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
87+
88+
<section class="intro">
89+
90+
</section>
91+
92+
<!-- /.intro -->
93+
94+
<!-- C usage documentation. -->
95+
96+
<section class="usage">
97+
98+
### Usage
99+
100+
```c
101+
#include "stdlib/math/base/special/sech.h"
102+
```
103+
104+
#### stdlib_base_sech( x )
105+
106+
Computes the [hyperbolic cosecant][hyperbolic-functions] of double-precision floating-point number.
107+
108+
```c
109+
double out = stdlib_base_sech( 2.0 );
110+
// returns ~0.2658
111+
112+
out = stdlib_base_sech( -2.0 );
113+
// returns ~0.2658
114+
```
115+
116+
The function accepts the following arguments:
117+
118+
- **x**: `[in] double` input value.
119+
120+
```c
121+
double stdlib_base_sech( const double x );
122+
```
123+
124+
</section>
125+
126+
<!-- /.usage -->
127+
128+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
129+
130+
<section class="notes">
131+
132+
</section>
133+
134+
<!-- /.notes -->
135+
136+
<!-- C API usage examples. -->
137+
138+
<section class="examples">
139+
140+
### Examples
141+
142+
```c
143+
#include "stdlib/math/base/special/sech.h"
144+
#include <stdio.h>
145+
146+
int main( void ) {
147+
const double x[] = { -4.0, -3.11, -2.22, -1.33, -0.44, 0.44, 1.33, 2.22, 3.11, 4.0 };
148+
149+
double v;
150+
int i;
151+
for ( i = 0; i < 10; i++ ) {
152+
v = stdlib_base_sech( x[ i ] );
153+
printf( "sech(%lf) = %lf\n", x[ i ], v );
154+
}
155+
}
156+
```
157+
158+
</section>
159+
160+
<!-- /.examples -->
161+
162+
</section>
163+
164+
<!-- /.c -->
165+
166+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
167+
168+
<section class="related">
169+
170+
<!-- /.related -->
171+
172+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
173+
174+
<section class="links">
175+
176+
[hyperbolic-functions]: https://en.wikipedia.org/wiki/Hyperbolic_functions
177+
178+
<!-- <related-links> -->
179+
180+
<!-- </related-links> -->
181+
182+
</section>
183+
184+
<!-- /.links -->
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pkg = require( './../package.json' ).name;
27+
var sech = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var x;
34+
var y;
35+
var i;
36+
37+
x = uniform( 100, -5.0, 5.0 );
38+
39+
b.tic();
40+
for ( i = 0; i < b.iterations; i++ ) {
41+
y = sech( x[ i%x.length ] );
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+
});
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
'use strict';
20+
21+
// MODULES //
22+
23+
var resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
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 sech = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( sech 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+
x = uniform( 100, -5.0, 5.0 );
47+
48+
b.tic();
49+
for ( i = 0; i < b.iterations; i++ ) {
50+
y = sech( x[ i%x.length ] );
51+
if ( isnan( y ) ) {
52+
b.fail( 'should not return NaN' );
53+
}
54+
}
55+
b.toc();
56+
if ( isnan( y ) ) {
57+
b.fail( 'should not return NaN' );
58+
}
59+
b.pass( 'benchmark finished' );
60+
b.end();
61+
});

0 commit comments

Comments
 (0)