File: trigfit.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 (43 lines) | stat: -rw-r--r-- 1,056 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
.. These functions handle triginometric interpolation and best fit.

function trigeval (a,x)
## Evaluates the trigonometric polynomial at x.
## a are the cos and then the sin coefficients.
	m=(cols(a)-1)/2;
	n=cols(a);
	A=dup(0:m,cols(x)); B=dup(x,m+1)'; F1=cos(A*B);
	A=dup(1:m,cols(x)); B=dup(x,m)'; F2=sin(A*B);
	return ((F1|F2).a')';
endfunction

function triginterpol (x,y)
## Interpolates a trigonometric polynomial to x and y.
## Returns the coefficients. First the cos and then the sin
## coeffients. Returns a row vector.
	m=(cols(x)-1)/2;
	n=cols(x);
	A=dup(0:m,n); B=dup(x,m+1)'; F1=cos(A*B);
	A=dup(1:m,n); B=dup(x,m)'; F2=sin(A*B);
	return ((F1|F2)\y')';
endfunction

function trigfit (x,y,deg)
## Fits a triginometric polynomial of degree deg to (x,y).
## x must be ordered!
	T=x/sqrt(sum(x^2));
	loop 1 to deg;
		t=cos(#*x);
		t=t-(T.t')'.T;
		t=t/sqrt(sum(t^2));
		T=T_t;
		t=sin(#*x);
		t=t-t.T'.T;
		t=t/sqrt(sum(t^2));
		T=T_t;
	end
	d=y.T'.T;
	i=1:cols(x); j=i[linspace(1,cols(x),2*deg)];
	return triginterpol(x[j],d[j]);
endfunction