Skip to content

feat: add math/base/special/chebyshev-t-polynomial #5774

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 11 commits into
base: develop
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<!--

@license Apache-2.0

Copyright (c) 2025 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# chebyshevtpoly

> [Chebyshev Polynomial][chebyshev-polynomial] of the first kind.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
> [Chebyshev Polynomial][chebyshev-polynomial] of the first kind.
> [Chebyshev polynomial][chebyshev-polynomial] of the first kind.

"polynomial" does not need to be capitalized. Applies here and elsewhere throughout this package.


<section class="intro">

The [Chebyshev Polynomial][chebyshev-polynomial] of the first kind is defined as

<!-- <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."> -->

```math
T_n(x) = \cos( n \cos^{-1}(x) )
```

<!-- </equation> -->

where `n` is the polynomial degree.

The [Chebyshev Polynomial][chebyshev-polynomial] of the first kind is related to the Gaussian hypergeometric function via the following equation
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The [Chebyshev Polynomial][chebyshev-polynomial] of the first kind is related to the Gaussian hypergeometric function via the following equation
The [Chebyshev Polynomial][chebyshev-polynomial] of the first kind is related to the [Gaussian hypergeometric function][@stdlib/math/base/special/hyp2f1] via the following equation

You'll need to add the link defn below in the links section. Also double-check that I used the correct package name.


<!-- <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."> -->

```math
T_n(x) = {}_2F_1\left( -n, n; \frac{1}{2}; \frac{1}{2}(1-x) \right)
```

<!-- </equation> -->

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var chebyshevtpoly = require( '@stdlib/math/base/special/chebyshev-t-polynomial' );
```

#### chebyshevtpoly( n, x )

Evaluates the [Chebyshev Polynomial][chebyshev-polynomial] of the first kind for a degree `n` at a value `x`.

```javascript
var y = chebyshevtpoly( 0.0, 0.5 );
// returns 1.0

y = chebyshevtpoly( 1.0, -0.5 );
// returns -0.5

y = chebyshevtpoly( 5.0, 0.5 );
// returns 0.5
```

If provided `NaN` as any argument, the function returns `NaN`.

```javascript
var y = chebyshevtpoly( NaN, 1.0 );
// returns NaN

y = chebyshevtpoly( 0.0, NaN );
// returns NaN
```

If provided a polynomial degree `n < 0`, the function returns `NaN`.

```javascript
var y = chebyshevtpoly( -2.0, 0.5 );
// returns NaN

y = chebyshevtpoly( -4.5, -0.5 );
// returns NaN
```

If provided a polynomial degree `n` which is not a nonnegative integer, the function returns `NaN`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we restrict to only integer degrees? Why not follow SciPy and state that non-integer values are resolved relative to the hypergeometric function?


```javascript
var y = chebyshevtpoly( 2.5, 0.5 );
// returns NaN

y = chebyshevtpoly( 0.5, -0.5 );
// returns NaN
```

</section>

<!-- /.usage -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var randu = require( '@stdlib/random/base/randu' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can update this example to use console/log-each-map.

var round = require( '@stdlib/math/base/special/round' );
var chebyshevtpoly = require( '@stdlib/math/base/special/chebyshev-t-polynomial' );

var n;
var x;
var y;
var i;

for ( i = 0; i < 10; i++ ) {
n = round( randu()*10.0 );
x = ( randu()*2.0 ) - 1.0;
y = chebyshevtpoly( n, x );
console.log( 'n: %d, x: %d, T_n(x): %d', n.toFixed( 4 ), x.toFixed( 4 ), y.toFixed( 4 ) );
}
```

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[chebyshev-polynomial]: https://en.wikipedia.org/wiki/Chebyshev_polynomials#Definitions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can drop #Definitions here.


</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var uniform = require( '@stdlib/random/array/uniform' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;
var chebyshevtpoly = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var n;
var x;
var y;
var i;

n = discreteUniform( 100, 0, 50 );
x = uniform( 100, -1.0, 1.0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = chebyshevtpoly( n[ i % n.length ], x[ i % x.length ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

{{alias}}( n, x )
Evaluates the Chebyshev polynomial of the first kind
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your line wrapping is off here.

for a degree `n` at a value `x`.

Parameters
----------
n: number
Degree of the polynomial.

x: number
Input value.

Returns
-------
out: number
Polynomial value.

Examples
--------
> var y = {{alias}}( 0.0, 0.5 )
1.0
> y = {{alias}}( 1.0, -0.5 )
-0.5
> y = {{alias}}( 5.0, 0.5 )
0.5
> y = {{alias}}( 2.5, 0.5 )
NaN
> y = {{alias}}( -1.0, 0.5 )
NaN
> y = {{alias}}( NaN, 1.0 )
NaN
> y = {{alias}}( 1.0, NaN )
NaN

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// TypeScript Version: 4.1

/**
* Evaluates the Chebyshev polynomial of the first kind for a degree `n` at a value `x`.
*
* @param n - degree of the polynomial
* @param x - input value
* @returns polynomial value
*
* @example
* var y = chebyshevtpoly( 0.0, 0.5 );
* // returns 1.0
*
* @example
* var y = chebyshevtpoly( 1.0, -0.5 );
* // returns -0.5
*
* @example
* var y = chebyshevtpoly( 5.0, 0.5 );
* // returns 0.5
*
* @example
* var y = chebyshevtpoly( 2.5, 0.5 );
* // returns NaN
*
* @example
* var y = chebyshevtpoly( -1.0, 0.5 );
* // returns NaN
*
* @example
* var y = chebyshevtpoly( NaN, 1.0 );
* // returns NaN
*
* @example
* var y = chebyshevtpoly( 1.0, NaN );
* // returns NaN
*/
declare function chebyshevtpoly( n: number, x: number ): number;


// EXPORTS //

export = chebyshevtpoly;
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import chebyshevtpoly = require( './index' );


// TESTS //

// The function returns a number...
{
chebyshevtpoly( 8.0, 1.0 ); // $ExpectType number
}

// The compiler throws an error if the function is provided values other than two numbers...
{
chebyshevtpoly( true, 3 ); // $ExpectError
chebyshevtpoly( false, 2 ); // $ExpectError
chebyshevtpoly( '5', 1 ); // $ExpectError
chebyshevtpoly( [], 1 ); // $ExpectError
chebyshevtpoly( {}, 2 ); // $ExpectError
chebyshevtpoly( ( x: number ): number => x, 2 ); // $ExpectError

chebyshevtpoly( 9, true ); // $ExpectError
chebyshevtpoly( 9, false ); // $ExpectError
chebyshevtpoly( 5, '5' ); // $ExpectError
chebyshevtpoly( 8, [] ); // $ExpectError
chebyshevtpoly( 9, {} ); // $ExpectError
chebyshevtpoly( 8, ( x: number ): number => x ); // $ExpectError

chebyshevtpoly( [], true ); // $ExpectError
chebyshevtpoly( {}, false ); // $ExpectError
chebyshevtpoly( false, '5' ); // $ExpectError
chebyshevtpoly( {}, [] ); // $ExpectError
chebyshevtpoly( '5', ( x: number ): number => x ); // $ExpectError
}

// The compiler throws an error if the function is provided insufficient arguments...
{
chebyshevtpoly(); // $ExpectError
chebyshevtpoly( 3 ); // $ExpectError
}
Loading