Last active
August 29, 2015 14:05
-
-
Save fredrik-johansson/80aa4df800f7ee6b87f3 to your computer and use it in GitHub Desktop.
chebyshev polynomial in nemo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function chebyshev_t_pair{S <: Ring}(n::Int, x::S) | |
if n == 0 | |
return one(S), x | |
elseif n == 1 | |
return x, one(S) | |
elseif n < 0 | |
a, b = chebyshev_t_pair(1-n, x) | |
return b, a | |
elseif iseven(n) | |
a, b = chebyshev_t_pair(n>>1, x) | |
return 2*(a*a) - 1, 2*(a*b) - x | |
else | |
a, b = chebyshev_t_pair((n>>1)+1, x) | |
return 2*(a*b) - x, 2*(b*b) - 1 | |
end | |
end | |
function chebyshev_t{S <: Ring}(n::Int, x::S) | |
if n == 0 | |
return one(S) | |
elseif n == 1 | |
return x | |
elseif n < 0 | |
return chebyshev_t(-n, x) | |
elseif iseven(n) | |
a = chebyshev_t(n>>1, x) | |
return 2*(a*a) - 1 | |
else | |
a, b = chebyshev_t_pair((n>>1)+1, x) | |
return 2*(a*b) - x | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment