File: chebysh.e

package info (click to toggle)
euler 1.61.0-12
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 5,164 kB
  • sloc: ansic: 24,761; sh: 8,314; makefile: 141; cpp: 47; php: 1
file content (52 lines) | stat: -rw-r--r-- 1,035 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
comment
Tschebycheff Polynome
T(n,x); Computes T_n(x)
Trek(n,x); a recursiv version (for test purposes)
Tpoly(n); Computes the Chebyshev polynomials T_n
endcomment

function T(n,x)
## T(n,x) computes T_n(x) the trigonometric way.
	s=size(x,n);
	y=x;
	signum=-mod(n,2)*2+1;
	loop 1 to s(2);
		z=x{#};
		if z>1;
			w=(z+sqrt(z^2-1))^n{#};
			y{#}=(w+1/w)/2;
		else;
			if z<-1;
				w=(-z+sqrt(z^2-1))^n{#};
				y{#}=signum{#}*(w+1/w)/2;
			else;
				y{#}=cos(n{#}*acos(z));
			endif;
		endif;
	end;
	return y;
endfunction

function Trek(n,x)
## Computes the Chebyshev polynomial via the recursion formula.
	if (n==0); return ones(size(x)); endif;
	if (n==1); return x; endif;
	z=ones(size(x))_x;
	loop 3 to n+1;
		z=z_(2*x*z[#-1]-z[#-2]);
	end;
	return z[n+1];
endfunction

function Tpoly (n)
## Computes the coefficients of the n-th chebyshev polynomial
## recursively.
	t0=[1]; t1=[0,1];
	if (n==0); return t0; endif;
	if (n==1); return t1; endif;
	loop 2 to n
		t=(0|2*t1[1:#])-(t0|[0,0]);
		t0=t1; t1=t;
	end;
	return t;
endfunction