Skip to content

Commit b0de299

Browse files
committed
feat: add benchmarks, examples, docs
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na ---
1 parent a246495 commit b0de299

File tree

7 files changed

+467
-0
lines changed

7 files changed

+467
-0
lines changed
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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+
# Chebyshev Polynomial of the first kind
22+
23+
> [Chebyshev Polynomial][chebyshev-polynomial] of the first kind.
24+
25+
<section class="intro">
26+
27+
The [Chebyshev Polynomial][chebyshev-polynomial] of the first kind is defined as
28+
29+
<!-- <equation class="equation" label="eq:chebyshev_polynomial" align="center" raw="T_n(x) = \cos( n \cos^{-1}(x) )" alt="Chebyshev Polynomial of the first kind."> -->
30+
31+
```math
32+
T_n(x) = \cos( n \cos^{-1}(x) )
33+
```
34+
35+
<!-- </equation> -->
36+
37+
where `n >= 0` is the polynomial degree.
38+
39+
The [Chebyshev Polynomial][chebyshev-polynomial] of the first kind is related to the Gaussian hypergeometric function via the following equation
40+
41+
<!-- <equation class="equation" label="eq:chebyshev_polynomial2" align="center" raw="T_n(x) = {}_2F_1\left( -n, n; \frac{1}{2}; \frac{1}{2}(1-x) \right)" alt="Chebyshev Polynomial of the first kind expressed in terms of the Gaussian hypergeometric function."> -->
42+
43+
```math
44+
T_n(x) = {}_2F_1\left( -n, n; \frac{1}{2}; \frac{1}{2}(1-x) \right)
45+
```
46+
47+
<!-- </equation> -->
48+
49+
</section>
50+
51+
<!-- /.intro -->
52+
53+
<section class="usage">
54+
55+
## Usage
56+
57+
```javascript
58+
var chebyshevtpoly = require( '@stdlib/math/base/special/chebyshev-t-polynomial' );
59+
```
60+
61+
#### chebyshevtpoly( n, x )
62+
63+
Evaluates the [Chebyshev Polynomial][chebyshev-polynomial] of the first kind for a degree `n` at a value `x`.
64+
65+
```javascript
66+
var y = chebyshevtpoly( 0.0, 0.5 );
67+
// returns 1.0
68+
69+
y = chebyshevtpoly( 1.0, -0.5 );
70+
// returns -0.5
71+
72+
y = chebyshevtpoly( 5.0, 0.5 );
73+
// returns 0.5
74+
```
75+
76+
If provided `NaN` as any argument, the function returns `NaN`.
77+
78+
```javascript
79+
var y = chebyshevtpoly( NaN, 1.0 );
80+
// returns NaN
81+
82+
y = chebyshevtpoly( 0.0, NaN );
83+
// returns NaN
84+
```
85+
86+
If provided a polynomial degree `n < 0`, the function returns `NaN`.
87+
88+
```javascript
89+
var y = chebyshevtpoly( -2.0, 0.5 );
90+
// returns NaN
91+
92+
y = chebyshevtpoly( -4.5, -0.5 );
93+
// returns NaN
94+
```
95+
96+
If provided a polynomial degree `n` which is not a nonnegative integer, the function returns `NaN`.
97+
98+
```javascript
99+
var y = chebyshevtpoly( 2.5, 0.5 );
100+
// returns NaN
101+
102+
y = chebyshevtpoly( 0.5, -0.5 );
103+
// returns NaN
104+
```
105+
106+
</section>
107+
108+
<!-- /.usage -->
109+
110+
<section class="examples">
111+
112+
## Examples
113+
114+
<!-- eslint no-undef: "error" -->
115+
116+
```javascript
117+
var randu = require( '@stdlib/random/base/randu' );
118+
var round = require( '@stdlib/math/base/special/round' );
119+
var chebyshevtpoly = require( '@stdlib/math/base/special/chebyshev-t-polynomial' );
120+
121+
var n;
122+
var x;
123+
var y;
124+
var i;
125+
126+
for ( i = 0; i < 10; i++ ) {
127+
n = round( randu()*10.0 );
128+
x = ( randu()*2.0 ) - 1.0;
129+
y = chebyshevtpoly( n, x );
130+
console.log( 'n: %d, x: %d, T_n(x): %d', n.toFixed( 4 ), x.toFixed( 4 ), y.toFixed( 4 ) );
131+
}
132+
```
133+
134+
</section>
135+
136+
<!-- /.examples -->
137+
138+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
139+
140+
<section class="related">
141+
142+
</section>
143+
144+
<!-- /.related -->
145+
146+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
147+
148+
<section class="links">
149+
150+
[chebyshev-polynomial]: https://en.wikipedia.org/wiki/Chebyshev_polynomials#Definitions
151+
152+
</section>
153+
154+
<!-- /.links -->
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var pkg = require( './../package.json' ).name;
28+
var chebyshevtpoly = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var n;
35+
var x;
36+
var y;
37+
var i;
38+
39+
n = discreteUniform( 100, 0, 50 );
40+
x = uniform( 100, -1.0, 1.0 );
41+
42+
b.tic();
43+
for ( i = 0; i < b.iterations; i++ ) {
44+
y = chebyshevtpoly( n[ i % n.length ], x[ i % x.length ] );
45+
if ( isnan( y ) ) {
46+
b.fail( 'should not return NaN' );
47+
}
48+
}
49+
b.toc();
50+
if ( isnan( y ) ) {
51+
b.fail( 'should not return NaN' );
52+
}
53+
b.pass( 'benchmark finished' );
54+
b.end();
55+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
{{alias}}( n, x )
3+
Evaluates the Chebyshev polynomial of the first kind
4+
for a degree `n` at a value `x`.
5+
6+
Parameters
7+
----------
8+
n: number
9+
Degree of the polynomial.
10+
11+
x: number
12+
Input value.
13+
14+
Returns
15+
-------
16+
out: number
17+
Polynomial value.
18+
19+
Examples
20+
--------
21+
> var y = {{alias}}( 0.0, 0.5 )
22+
1.0
23+
> y = {{alias}}( 1.0, -0.5 )
24+
-0.5
25+
> y = {{alias}}( 5.0, 0.5 )
26+
0.5
27+
> y = {{alias}}( 2.5, 0.5 )
28+
NaN
29+
> y = {{alias}}( -1.0, 0.5 )
30+
NaN
31+
> y = {{alias}}( NaN, 1.0 )
32+
NaN
33+
> y = {{alias}}( 1.0, NaN )
34+
NaN
35+
36+
See Also
37+
--------
38+
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) 2019 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+
* Evaluates the Chebyshev polynomial of the first kind for a degree `n` at a value `x`.
23+
*
24+
* @param n - degree of the polynomial
25+
* @param x - input value
26+
* @returns polynomial value
27+
*
28+
* @example
29+
* var y = chebyshevtpoly( 0.0, 0.5 );
30+
* // returns 1.0
31+
*
32+
* @example
33+
* var y = chebyshevtpoly( 1.0, -0.5 );
34+
* // returns -0.5
35+
*
36+
* @example
37+
* var y = chebyshevtpoly( 5.0, 0.5 );
38+
* // returns 0.5
39+
*
40+
* @example
41+
* var y = chebyshevtpoly( 2.5, 0.5 );
42+
* // returns NaN
43+
*
44+
* @example
45+
* var y = chebyshevtpoly( -1.0, 0.5 );
46+
* // returns NaN
47+
*
48+
* @example
49+
* var y = chebyshevtpoly( NaN, 1.0 );
50+
* // returns NaN
51+
*
52+
* @example
53+
* var y = chebyshevtpoly( 1.0, NaN );
54+
* // returns NaN
55+
*/
56+
declare function chebyshevtpoly( n: number, x: number ): number;
57+
58+
59+
// EXPORTS //
60+
61+
export = chebyshevtpoly;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2019 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 chebyshevtpoly = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a number...
25+
{
26+
chebyshevtpoly( 8.0, 1.0 ); // $ExpectType number
27+
}
28+
29+
// The compiler throws an error if the function is provided values other than two numbers...
30+
{
31+
chebyshevtpoly( true, 3 ); // $ExpectError
32+
chebyshevtpoly( false, 2 ); // $ExpectError
33+
chebyshevtpoly( '5', 1 ); // $ExpectError
34+
chebyshevtpoly( [], 1 ); // $ExpectError
35+
chebyshevtpoly( {}, 2 ); // $ExpectError
36+
chebyshevtpoly( ( x: number ): number => x, 2 ); // $ExpectError
37+
38+
chebyshevtpoly( 9, true ); // $ExpectError
39+
chebyshevtpoly( 9, false ); // $ExpectError
40+
chebyshevtpoly( 5, '5' ); // $ExpectError
41+
chebyshevtpoly( 8, [] ); // $ExpectError
42+
chebyshevtpoly( 9, {} ); // $ExpectError
43+
chebyshevtpoly( 8, ( x: number ): number => x ); // $ExpectError
44+
45+
chebyshevtpoly( [], true ); // $ExpectError
46+
chebyshevtpoly( {}, false ); // $ExpectError
47+
chebyshevtpoly( false, '5' ); // $ExpectError
48+
chebyshevtpoly( {}, [] ); // $ExpectError
49+
chebyshevtpoly( '5', ( x: number ): number => x ); // $ExpectError
50+
}
51+
52+
// The compiler throws an error if the function is provided insufficient arguments...
53+
{
54+
chebyshevtpoly(); // $ExpectError
55+
chebyshevtpoly( 3 ); // $ExpectError
56+
}

0 commit comments

Comments
 (0)