Skip to content

Commit 6d7317a

Browse files
feat: add math/base/special/sincosd
PR-URL: #5837 Co-authored-by: Gunj Joshi <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]> Reviewed-by: Gunj Joshi <[email protected]> Signed-off-by: Gunj Joshi <[email protected]>
1 parent fea4b5b commit 6d7317a

37 files changed

+3167
-0
lines changed
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
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+
# sincosd
22+
23+
> Simultaneously compute the [sine][@stdlib/math/base/special/sind] and [cosine][@stdlib/math/base/special/cosd] of an angle measured in degrees.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var sincosd = require( '@stdlib/math/base/special/sincosd' );
31+
```
32+
33+
#### sincosd( x )
34+
35+
Simultaneously computes the [sine][@stdlib/math/base/special/sind] and [cosine][@stdlib/math/base/special/cosd] of an angle measured in degrees.
36+
37+
```javascript
38+
var v = sincosd( 0.0 );
39+
// returns [ ~0.0, ~1.0 ]
40+
41+
v = sincosd( 90.0 );
42+
// returns [ ~1.0, ~0.0 ]
43+
44+
v = sincosd( -30.0 );
45+
// returns [ ~-0.5, ~0.866 ]
46+
```
47+
48+
#### sincosd.assign( x, out, stride, offset )
49+
50+
Simultaneously computes the [sine][@stdlib/math/base/special/sind] and [cosine][@stdlib/math/base/special/cosd] of an angle measured in degrees and assigns the results to a provided output array.
51+
52+
```javascript
53+
var Float64Array = require( '@stdlib/array/float64' );
54+
55+
var out = new Float64Array( 2 );
56+
57+
var v = sincosd.assign( 0.0, out, 1, 0 );
58+
// returns <Float64Array>[ ~0.0, ~1.0 ]
59+
60+
var bool = ( v === out );
61+
// returns true
62+
```
63+
64+
</section>
65+
66+
<!-- /.usage -->
67+
68+
<section class="examples">
69+
70+
## Examples
71+
72+
<!-- eslint no-undef: "error" -->
73+
74+
```javascript
75+
var linspace = require( '@stdlib/array/base/linspace' );
76+
var sincosd = require( '@stdlib/math/base/special/sincosd' );
77+
78+
var x = linspace( 0.0, 180.0, 100 );
79+
80+
var y;
81+
var i;
82+
for ( i = 0; i < x.length; i++ ) {
83+
y = sincosd( x[ i ] );
84+
console.log( 'sincosd(%d) = [ %d, %d ]', x[ i ], y[ 0 ], y[ 1 ] );
85+
}
86+
```
87+
88+
</section>
89+
90+
<!-- /.examples -->
91+
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/sincosd.h"
116+
```
117+
118+
#### stdlib_base_sincosd( x, &sine, &cosine )
119+
120+
Simultaneously computes the [sine][@stdlib/math/base/special/sind] and [cosine][@stdlib/math/base/special/cosd] of an angle measured in degrees.
121+
122+
```c
123+
double cosine;
124+
double sine;
125+
126+
stdlib_base_sincosd( 4.0, &sine, &cosine );
127+
```
128+
129+
The function accepts the following arguments:
130+
131+
- **x**: `[in] double` input value.
132+
- **sine**: `[out] double*` destination for the sine.
133+
- **cosine**: `[out] double*` destination for the cosine.
134+
135+
```c
136+
void stdlib_base_sincosd( const double x, double *sine, double *cosine );
137+
```
138+
139+
</section>
140+
141+
<!-- /.usage -->
142+
143+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
144+
145+
<section class="notes">
146+
147+
</section>
148+
149+
<!-- /.notes -->
150+
151+
<!-- C API usage examples. -->
152+
153+
<section class="examples">
154+
155+
### Examples
156+
157+
```c
158+
#include "stdlib/math/base/special/sincosd.h"
159+
#include <stdio.h>
160+
161+
int main( void ) {
162+
const double x[] = { 0.0, 90.0, 180.0, 360.0 };
163+
164+
double cosine;
165+
double sine;
166+
int i;
167+
for ( i = 0; i < 4; i++ ) {
168+
stdlib_base_sincosd( x[ i ], &sine, &cosine );
169+
printf( "x: %lf => sine: %lf, cosine: %lf\n", x[ i ], sine, cosine );
170+
}
171+
}
172+
```
173+
174+
</section>
175+
176+
<!-- /.examples -->
177+
178+
</section>
179+
180+
<!-- /.c -->
181+
182+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
183+
184+
<section class="related">
185+
186+
</section>
187+
188+
<!-- /.related -->
189+
190+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
191+
192+
<section class="links">
193+
194+
[@stdlib/math/base/special/cosd]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/cosd
195+
196+
[@stdlib/math/base/special/sind]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/sind
197+
198+
<!-- <related-links> -->
199+
200+
<!-- </related-links> -->
201+
202+
</section>
203+
204+
<!-- /.links -->
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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 sind = require( '@stdlib/math/base/special/sind' );
27+
var cosd = require( '@stdlib/math/base/special/cosd' );
28+
var pkg = require( './../package.json' ).name;
29+
var sincosd = require( './../lib' );
30+
31+
32+
// MAIN //
33+
34+
bench( pkg, function benchmark( b ) {
35+
var x;
36+
var y;
37+
var i;
38+
39+
x = uniform( 100, -10.0, 10.0 );
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
y = sincosd( x[ i%x.length ] );
44+
if ( isnan( y[0] ) || isnan( y[1] ) ) {
45+
b.fail( 'should not return NaN' );
46+
}
47+
}
48+
b.toc();
49+
if ( isnan( y[0] ) || isnan( y[1] ) ) {
50+
b.fail( 'should not return NaN' );
51+
}
52+
b.pass( 'benchmark finished' );
53+
b.end();
54+
});
55+
56+
bench( pkg+'::separate-evaluation', function benchmark( b ) {
57+
var x;
58+
var y;
59+
var i;
60+
61+
x = uniform( 100, -10.0, 10.0 );
62+
y = [ 0.0, 0.0 ];
63+
64+
b.tic();
65+
for ( i = 0; i < b.iterations; i++ ) {
66+
y = [ sind( x[ i%x.length ] ), cosd( x[ i%x.length ] ) ];
67+
if ( isnan( y[0] ) || isnan( y[1] ) ) {
68+
b.fail( 'should not return NaN' );
69+
}
70+
}
71+
b.toc();
72+
if ( isnan( y[0] ) || isnan( y[1] ) ) {
73+
b.fail( 'should not return NaN' );
74+
}
75+
b.pass( 'benchmark finished' );
76+
b.end();
77+
});
78+
79+
bench( pkg+':assign', function benchmark( b ) {
80+
var x;
81+
var y;
82+
var i;
83+
84+
x = uniform( 100, -10.0, 10.0 );
85+
y = [ 0.0, 0.0 ];
86+
87+
b.tic();
88+
for ( i = 0; i < b.iterations; i++ ) {
89+
sincosd.assign( x[ i%x.length ], y, 1, 0 );
90+
if ( isnan( y[0] ) || isnan( y[1] ) ) {
91+
b.fail( 'should not return NaN' );
92+
}
93+
}
94+
b.toc();
95+
if ( isnan( y[0] ) || isnan( y[1] ) ) {
96+
b.fail( 'should not return NaN' );
97+
}
98+
b.pass( 'benchmark finished' );
99+
b.end();
100+
});
101+
102+
bench( pkg+'::separate-evaluation,in-place', function benchmark( b ) {
103+
var x;
104+
var y;
105+
var i;
106+
107+
x = uniform( 100, -10.0, 10.0 );
108+
y = [ 0.0, 0.0 ];
109+
110+
b.tic();
111+
for ( i = 0; i < b.iterations; i++ ) {
112+
y[0] = sind( x[ i%x.length ] );
113+
y[1] = cosd( x[ i%x.length ] );
114+
if ( isnan( y[0] ) || isnan( y[1] ) ) {
115+
b.fail( 'should not return NaN' );
116+
}
117+
}
118+
b.toc();
119+
if ( isnan( y[0] ) || isnan( y[1] ) ) {
120+
b.fail( 'should not return NaN' );
121+
}
122+
b.pass( 'benchmark finished' );
123+
b.end();
124+
});
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 sincosd = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( sincosd 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, -10.0, 10.0 );
47+
48+
b.tic();
49+
for ( i = 0; i < b.iterations; i++ ) {
50+
y = sincosd( x[ i%x.length ] );
51+
if ( isnan( y[ 0 ] ) || isnan( y[ 1 ] ) ) {
52+
b.fail( 'should not return NaN' );
53+
}
54+
}
55+
b.toc();
56+
if ( isnan( y[ 0 ] ) || isnan( y[ 1 ] ) ) {
57+
b.fail( 'should not return NaN' );
58+
}
59+
b.pass( 'benchmark finished' );
60+
b.end();
61+
});

0 commit comments

Comments
 (0)