Python How can I evaluate a Chebishev polynomial in python?

AI Thread Summary
The discussion centers on constructing a Python function to evaluate Chebyshev polynomials of order k at a given x. The user initially attempted to use the `chebval` function from NumPy but found it inadequate for their needs, particularly for the third-order polynomial. They shared a code snippet that works for Legendre polynomials but faced challenges adapting it for Chebyshev polynomials due to differences in their recurrence relationships. The recurrence relation for Chebyshev polynomials is highlighted, indicating that T_n(x) can be computed using the last two terms. The user expressed a need for a function that directly evaluates the polynomial of order n based on previous orders, mentioning numerical errors encountered when using the cosine method for evaluation. Another participant pointed out that for x > 1, the evaluation should utilize hyperbolic functions, suggesting this might resolve the numerical issues.
confused_engineer
Messages
34
Reaction score
1
TL;DR Summary
I can't find a python function which provides me with the evaluation of a Chebishev polynomial of a concrete order at a concrete point
Hello everyone. I need to construct in python a function which returns the evaluation of a Chebishev polynomial of order k evaluated in x. I have tested the function chebval form these documents, but it doesn't provide what I look for, since I have tested the third one, 4t^3-3t and
Python:
import numpy as np
import numpy.polynomial.chebyshev as cheb

gfg = cheb.chebval((3), (3))
does not return 4*(3)^3-3*3, but instead it returns 3. I have a code which does this but for Legendre polynomials, but I cannot reproduce it whith these because the recurrence relationship uses the last two terms, not the first two as Legendre's. The code is as follow.
Code:
import numpy as np
import numpy.polynomial.chebyshev as cheb

gfg = cheb.chebval((3), (3))
 print(gfg)
x=5
K=2

pn2 = 2*(x)**K+3print(pn2)

So, if someone could tell me how to do this but with Chebishev's polynomials I would be most grateful.

Thanks for reading.
 
Last edited:
Technology news on Phys.org
The recurrence relation for Chebyshev polynomials of the first kind is ##T_{n}(x) = 2xT_{n-1}(x) - T_{n-2}(x)##. So to calculate ##T_{n}(x)##, you just need a loop. Start with ##T_0=1##, and ##T_1=x## and iterate until you get ##T_n##.
 
Last edited:
tnich said:
The recurrence relation for Chebyshev polynomials of the first kind is ##T_{n}(x) = 2xT_{n-1}(x) - T_{n-2}(x)##. So to calculate ##T_{n}(x)##, you just need a loop. Start with ##T_0=1##, and ##T_1=x## and iterate until you get ##T_n##.
Thanks for the answer, but I am afraid that is exactly the problem. I need a function that returns the evaluation of a polynomial of order n evaluated at x. For that, I need to calculate the polynomial of order n based on the polynomials of order (n-1) and (n-2). Currently, I am using mt.cos(n*mt.acos(x)) to evaluate the polynomial, but a numerical error arrises.

Is there a way to calculate the Chevishev polynomial of order n based on the first two?
 
confused_engineer said:
Currently, I am using mt.cos(n*mt.acos(x)) to evaluate the polynomial, but a numerical error arrises.
Your example was ##n=3##, ##x=3##. At least according to Wikipedia, ##T_n(x)=\cosh(n\cosh^{-1}(x))## for ##x>1##, rather than ##\cos## and ##\cos^{-1}## as it is for ##-1\leq x\leq 1##. Does that fix your numerical error?
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
16
Views
2K
Replies
15
Views
2K
Replies
9
Views
3K
Replies
10
Views
3K
Replies
2
Views
1K
Back
Top