Skip to content

Commit b60ef62

Browse files
MSP20086stdlib-botPlaneshifter
authored
feat: add math/base/special/csc
PR-URL: #1410 Closes: #226 --------- Signed-off-by: Philipp Burckhardt <[email protected]> Co-authored-by: stdlib-bot <[email protected]> Co-authored-by: Philipp Burckhardt <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent 046d8c4 commit b60ef62

25 files changed

+1038
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
# Cosecant
22+
23+
> Evaluate the [cosecant][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 csc = require( '@stdlib/math/base/special/csc' );
35+
```
36+
37+
## csc( x )
38+
39+
Evaluates the [cosecant][trigonometric-functions] of `x` (in radians).
40+
41+
```javascript
42+
var v = csc( 0.0 );
43+
// returns Infinity
44+
45+
v = csc( 3.141592653589793/2.0 );
46+
// returns ~1.0
47+
48+
v = csc( -3.141592653589793/6.0 );
49+
// returns ~-2.0
50+
51+
v = csc( 3.141592653589793/6.0 );
52+
// returns ~2.0
53+
54+
v = csc( 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 csc = require( '@stdlib/math/base/special/csc' );
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( csc( x[ i ] ) );
78+
}
79+
```
80+
81+
</section>
82+
83+
<!-- /.examples -->
84+
85+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
86+
87+
<section class="related">
88+
89+
* * *
90+
91+
## See Also
92+
93+
<span class="package-name">[`@stdlib/math/base/special/sin`][@stdlib/math/base/special/sin]</span><span class="delimiter">: </span><span class="description">evaluate the sine of a number.</span>
94+
95+
</section>
96+
97+
<!-- /.related -->
98+
99+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
100+
101+
<section class="links">
102+
103+
[trigonometric-functions]: https://en.wikipedia.org/wiki/Trigonometric_functions
104+
105+
<!-- <related-links> -->
106+
107+
[@stdlib/math/base/special/sin]: https://github.com/stdlib-js/math/tree/main/base/special/sin
108+
109+
<!-- </related-links> -->
110+
111+
</section>
112+
113+
<!-- /.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 sin = require( '@stdlib/math/base/special/sin' );
27+
var pkg = require( './../package.json' ).name;
28+
var csc = 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 = csc( 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 / sin( 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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
{{alias}}( x )
3+
Computes the cosecant of a number.
4+
5+
Parameters
6+
----------
7+
x: number
8+
Input value (in radians).
9+
10+
Returns
11+
-------
12+
y: number
13+
Cosecant.
14+
15+
Examples
16+
--------
17+
> var y = {{alias}}( 0.0 )
18+
Infinity
19+
> y = {{alias}}( {{alias:@stdlib/constants/float64/pi}}/2.0 )
20+
~1.0
21+
> y = {{alias}}( -{{alias:@stdlib/constants/float64/pi}}/6.0 )
22+
~-2.0
23+
> y = {{alias}}( NaN )
24+
NaN
25+
26+
See Also
27+
--------
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Computes the cosecant of a number.
23+
*
24+
* @param x - input value (in radians)
25+
* @returns cosecant
26+
*
27+
* @example
28+
* var v = csc( 0.0 );
29+
* // returns Infinity
30+
*
31+
* @example
32+
* var v = csc( 3.141592653589793/2.0 );
33+
* // returns ~1.0
34+
*
35+
* @example
36+
* var v = csc( -3.141592653589793/6.0 );
37+
* // returns ~-2.0
38+
*
39+
* @example
40+
* var v = csc( NaN );
41+
* // returns NaN
42+
*/
43+
declare function csc( x: number ): number;
44+
45+
46+
// EXPORTS //
47+
48+
export = csc;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
import csc = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a number...
25+
{
26+
csc( 2 ); // $ExpectType number
27+
}
28+
29+
// The compiler throws an error if the function is provided a value other than a number...
30+
{
31+
csc( true ); // $ExpectError
32+
csc( false ); // $ExpectError
33+
csc( null ); // $ExpectError
34+
csc( undefined ); // $ExpectError
35+
csc( '5' ); // $ExpectError
36+
csc( [] ); // $ExpectError
37+
csc( {} ); // $ExpectError
38+
csc( ( x: number ): number => x ); // $ExpectError
39+
}
40+
41+
// The compiler throws an error if the function is provided insufficient arguments...
42+
{
43+
csc(); // $ExpectError
44+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
var linspace = require( '@stdlib/array/base/linspace' );
22+
var TWO_PI = require( '@stdlib/constants/float64/two-pi' );
23+
var csc = require( './../lib' );
24+
25+
var x = linspace( -TWO_PI, TWO_PI, 100 );
26+
27+
var i;
28+
for ( i = 0; i < x.length; i++ ) {
29+
console.log( 'csc(%d) = %d', x[i], csc( x[i] ) );
30+
}
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) 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+
/**
22+
* Evaluate the cosecant of a number.
23+
*
24+
* @module @stdlib/math/base/special/csc
25+
*
26+
* @example
27+
* var csc = require( '@stdlib/math/base/special/csc' );
28+
*
29+
* var v = csc( 0.0 );
30+
* // returns Infinity
31+
*
32+
* v = csc( 3.141592653589793/2.0 );
33+
* // returns ~1.0
34+
*
35+
* v = csc( -3.141592653589793/6.0 );
36+
* // returns ~-2.0
37+
*
38+
* v = csc( 3.141592653589793/6.0 );
39+
* // returns ~2.0
40+
*
41+
* v = csc( NaN );
42+
* // returns NaN
43+
*/
44+
45+
// MODULES //
46+
47+
var main = require('./main.js');
48+
49+
50+
// EXPORTS //
51+
52+
module.exports = main;

0 commit comments

Comments
 (0)