| 1 |
/* |
| 2 |
Package fourier_sec, to study piecewise defined functions |
| 3 |
(sectionally defined, hence the name). |
| 4 |
|
| 5 |
Author: Jose A. Vallejo |
| 6 |
Faculty of Sciences |
| 7 |
Universidad Autonoma de San Luis Potosi (Mexico) |
| 8 |
http://galia.fc.uaslp.mx/~jvallejo |
| 9 |
|
| 10 |
It is assumed that the functions are defined using the format |
| 11 |
if ... then ... else ... |
| 12 |
as, for example, |
| 13 |
(%i1) absolute(x):=if (x<=0) then -x elseif (x>0) then x$ |
| 14 |
(it will not work with 'abs'). |
| 15 |
It provides three functions: |
| 16 |
1. 'paritycheck', to check if a given piecewise defined |
| 17 |
function is even or odd (or none). For instance, |
| 18 |
(%i2) paritycheck(absolute(x),x); |
| 19 |
(%o2) 0 |
| 20 |
A '0' means 'even', '1' is 'odd', and '-1' is 'none' |
| 21 |
2. 'fourier_sec_coeff', to compute the Fourier sine and cosine |
| 22 |
coefficients. The function must have compact support.For |
| 23 |
instance, for the unit step we have: |
| 24 |
(%i3) h(x):=if (-%pi<=x and x<0) then 0 elseif (0<=x and x<=%pi) then 1$ |
| 25 |
(%i4) fourier_sec_coeff(h(x),x); |
| 26 |
(%o4) [1/2,0,-((-1)^n-1)/(%pi*n)] |
| 27 |
3. 'fourier_sec_series', to compute the Fourier series, truncated |
| 28 |
or not. The series for the unit step to the 10th order: |
| 29 |
(%i5) fourier_sec_series(h(x),x,10); |
| 30 |
(%o5) (2*sin(9*x))/(9*%pi)+(2*sin(7*x))/(7*%pi)+(2*sin(5*x))/(5*%pi)+(2*sin(3*x))/(3*%pi)+(2*sin(x))/%pi+1/2 |
| 31 |
and the whole Fourier series |
| 32 |
(%i6) fourier_sec_series(h(x),x,inf); |
| 33 |
(%o6) (sum(((1/n-(-1)^n/n)*sin(n*x))/%pi,n,1,inf))+1/2 |
| 34 |
*/ |
| 35 |
|
| 36 |
|
| 37 |
load(fourie)$ |
| 38 |
|
| 39 |
load(simplify_sum)$ |
| 40 |
|
| 41 |
paritycheck(funvar,x):=block([subintervals,subvalues,tmp1,tmp2,token1,token0,tmp3, |
| 42 |
subvalues_list,subintervals_list,icentral:0,middle,minusmiddle,token2, |
| 43 |
side_subintervals_list,L,side_subvalues_list, |
| 44 |
zero_subintervals,tmp4,tmp5,non_zero_side_subvalues_list, |
| 45 |
non_zero_side_subintervals_list,LL,token3,expr1,expr2], |
| 46 |
local(M,N,P,Q,count1,count2,count3,count4), |
| 47 |
subintervals:makelist(part(funvar,i),i,makelist(2*k-1,k,1,(length(funvar)-2)/2)), |
| 48 |
subvalues:makelist(part(funvar,i),i,makelist(2*k,k,1,(length(funvar)-2)/2)), |
| 49 |
for j:1 thru length(subintervals) do (if operatorp(subintervals[j],["<",">","<=",">="]) then tmp1[j]:1 else tmp1[j]:0), |
| 50 |
tmp1:makelist(tmp1[j],j,1,length(subintervals)), |
| 51 |
tmp2:sublist_indices(tmp1,lambda([x],x=1)), |
| 52 |
|
| 53 |
/* if length(tmp2)=0 all the subintervals in the domain of funvar are bounded! */ |
| 54 |
|
| 55 |
if is(equal(length(tmp2),0)) then |
| 56 |
( |
| 57 |
subvalues_list:copylist(subvalues), |
| 58 |
tmp3:copylist(subintervals), |
| 59 |
for j:1 thru length(tmp3) do |
| 60 |
( |
| 61 |
M[j]:makelist(part(tmp3[j],k),k,1,length(tmp3[j])), |
| 62 |
for m:1 thru 2 do N[j,m]:makelist(part(M[j],m,n),n,1,length(M[j])), |
| 63 |
P[j]:append(N[j,1],N[j,2]),Q[j]:sort(delete(x,P[j]),"<") |
| 64 |
), |
| 65 |
subintervals_list:makelist(Q[j],j,1,length(tmp3)), |
| 66 |
for i:1 thru length(subintervals_list) do (if is(lmin(subintervals_list[i])*lmax(subintervals_list[i])<0) then icentral:i), |
| 67 |
if is(icentral>0) then middle:subvalues_list[icentral], |
| 68 |
if is(icentral>0) then |
| 69 |
(if evenfunp(middle,x) then token2:0 elseif oddfunp(middle,x) then token2:1 else return(-1)) |
| 70 |
else |
| 71 |
|
| 72 |
/* now we analyze what happens if icentral=0, so there are only side intervals */ |
| 73 |
|
| 74 |
( |
| 75 |
/* as before, there must be an even number of symmetric intervals, otherwise -1 */ |
| 76 |
side_subintervals_list:copylist(subintervals_list), |
| 77 |
if not(evenp(length(side_subintervals_list))) then return(-1), |
| 78 |
L:length(side_subintervals_list)/2, |
| 79 |
for k:1 thru L do count1[k]:charfun(is(equal(side_subintervals_list[k],reverse(map("-",side_subintervals_list[2*L+1-k]))))), |
| 80 |
if sum(count1[j],j,1,L)#L then return(-1), |
| 81 |
side_subvalues_list:copylist(subvalues_list), |
| 82 |
for k:1 thru L do count2[k]:charfun(is(equalp(side_subvalues_list[k],ratsubst(-x,x,side_subvalues_list[2*L+1-k])))), |
| 83 |
if is(equal(sum(count2[j],j,1,L),L)) then token3:0, |
| 84 |
for k:1 thru L do count3[k]:charfun(is(equalp(side_subvalues_list[k],-ratsubst(-x,x,side_subvalues_list[2*L+1-k])))), |
| 85 |
if is(equal(sum(count3[j],j,1,L),L)) then token3:1, |
| 86 |
if is(not(equalp(token3,0)) and not(equalp(token3,1))) then return(-1) , |
| 87 |
return(token3) |
| 88 |
), |
| 89 |
|
| 90 |
/* and what happens if icentral#0 */ |
| 91 |
|
| 92 |
side_subintervals_list:delete(subintervals_list[icentral],subintervals_list), |
| 93 |
if is(equal(length(side_subintervals_list),0)) then (if is(equal(token1,token2)) then return(token1*token2) else return(-1)), |
| 94 |
if not(evenp(length(side_subintervals_list))) then return(-1), |
| 95 |
L:length(side_subintervals_list)/2, |
| 96 |
for k:1 thru L do count1[k]:charfun(is(equal(side_subintervals_list[k],reverse(map("-",side_subintervals_list[2*L+1-k]))))), |
| 97 |
if sum(count1[j],j,1,L)#L then return(-1), |
| 98 |
side_subvalues_list:delete(subvalues_list[icentral],subvalues_list), |
| 99 |
|
| 100 |
/* remove those subintervals in which funvar vanishes */ |
| 101 |
|
| 102 |
zero_subintervals:sublist_indices(side_subvalues_list,lambda([x],x=0)), |
| 103 |
tmp4:copylist(side_subintervals_list), |
| 104 |
for j:1 thru length(zero_subintervals) do |
| 105 |
(tmp4:delete(side_subintervals_list[zero_subintervals[j]],tmp4)), |
| 106 |
non_zero_side_subintervals_list:tmp4, |
| 107 |
tmp5:copylist(side_subvalues_list), |
| 108 |
for j:1 thru length(zero_subintervals) do |
| 109 |
(tmp5:delete(side_subvalues_list[zero_subintervals[j]],tmp5)), |
| 110 |
non_zero_side_subvalues_list:tmp5, |
| 111 |
|
| 112 |
/* if length(non_zero_side_subintervals_list)=0 then we are done just with token2 */ |
| 113 |
|
| 114 |
if is(equal(length(non_zero_side_subintervals_list),0)) then return(token2), |
| 115 |
|
| 116 |
/* otherwise, we must also take into account token3 */ |
| 117 |
LL:length(non_zero_side_subintervals_list)/2, |
| 118 |
for k:1 thru LL do count2[k]:charfun(is(equalp(non_zero_side_subvalues_list[k],ratsubst(-x,x,non_zero_side_subvalues_list[2*LL+1-k])))), |
| 119 |
if is(equal(sum(count2[j],j,1,LL),LL)) then token3:0, |
| 120 |
for k:1 thru LL do count3[k]:charfun(is(equalp(non_zero_side_subvalues_list[k],-ratsubst(-x,x,non_zero_side_subvalues_list[2*LL+1-k])))), |
| 121 |
if is(equal(sum(count3[j],j,1,LL),LL)) then token3:1, |
| 122 |
if is(not(equal(token3,0) or equal(token3,1))) then return(-1) elseif |
| 123 |
is(token2#token3) then return(-1) |
| 124 |
elseif is(equal(token2,0)) then return(0) |
| 125 |
elseif is(equal(token2,1)) then return(1) |
| 126 |
|
| 127 |
), |
| 128 |
|
| 129 |
/* we continue here with unbounded intervals */ |
| 130 |
|
| 131 |
expr1:subvalues[tmp2[1]], |
| 132 |
expr2:ratsubst(-x,x,subvalues[tmp2[2]]), |
| 133 |
if is(equalp(expr1,expr2)) then token1:0 |
| 134 |
elseif is(equalp(expr1,-expr2)) then token1:1 |
| 135 |
else return(-1), |
| 136 |
|
| 137 |
/* the problem when defining token1 is that if the asymptotic value is 0, then it is always token1:0 */ |
| 138 |
/* although the function could be odd. To cope with this, we define token0 below */ |
| 139 |
|
| 140 |
tmp3:makelist(subintervals[i],i,sublist_indices(tmp1,lambda([x],x=0))), |
| 141 |
if is(equal(length(tmp3),0)) then return(token1), |
| 142 |
subvalues_list:makelist(subvalues[i],i,sublist_indices(tmp1,lambda([x],x=0))), |
| 143 |
token0:if member(0,makelist(subvalues[i],i,tmp2)) then 0 else 7, |
| 144 |
for j:1 thru length(tmp3) do |
| 145 |
( |
| 146 |
M[j]:makelist(part(tmp3[j],k),k,1,length(tmp3[j])), |
| 147 |
for m:1 thru 2 do N[j,m]:makelist(part(M[j],m,n),n,1,length(M[j])), |
| 148 |
P[j]:append(N[j,1],N[j,2]),Q[j]:sort(delete(x,P[j]),"<") |
| 149 |
), |
| 150 |
subintervals_list:makelist(Q[j],j,1,length(tmp3)), |
| 151 |
for i:1 thru length(subintervals_list) do (if is(lmin(subintervals_list[i])*lmax(subintervals_list[i])<0) then icentral:i), |
| 152 |
if is(icentral>0) then middle:subvalues_list[icentral], |
| 153 |
if is(icentral>0) then |
| 154 |
(if evenfunp(middle,x) then token2:0 elseif oddfunp(middle,x) then token2:1 else return(-1)) |
| 155 |
else |
| 156 |
|
| 157 |
/* now we analyze what happens if icentral=0, so there are only non-bounded intervals and side intervals */ |
| 158 |
|
| 159 |
( |
| 160 |
/* as before, there must be an even number of symmetric intervals, otherwise -1 */ |
| 161 |
side_subintervals_list:copylist(subintervals_list), |
| 162 |
if not(evenp(length(side_subintervals_list))) then return(-1), |
| 163 |
L:length(side_subintervals_list)/2, |
| 164 |
for k:1 thru L do count1[k]:charfun(is(equal(side_subintervals_list[k],reverse(map("-",side_subintervals_list[2*L+1-k]))))), |
| 165 |
if sum(count1[j],j,1,L)#L then return(-1), |
| 166 |
side_subvalues_list:copylist(subvalues_list), |
| 167 |
for k:1 thru L do count2[k]:charfun(is(equalp(side_subvalues_list[k],ratsubst(-x,x,side_subvalues_list[2*L+1-k])))), |
| 168 |
if is(equal(sum(count2[j],j,1,L),L)) then token3:0, |
| 169 |
for k:1 thru L do count3[k]:charfun(is(equalp(side_subvalues_list[k],-ratsubst(-x,x,side_subvalues_list[2*L+1-k])))), |
| 170 |
if is(equal(sum(count3[j],j,1,L),L)) then token3:1, |
| 171 |
if is(not(equal(token3,0)) and not(equal(token3,1))) then return(-1) elseif |
| 172 |
is(not(equal(token1,token3)) and not(equal(token0,0))) then return(-1) |
| 173 |
elseif is( equal(token0,0) and equal(token3,0)) then return(0) |
| 174 |
elseif is( equal(token0,0) and equal(token3,1)) then return(1) |
| 175 |
elseif is(not(equal(token0,0)) and equal(token1,token3)) then return(token3) |
| 176 |
elseif is(not(equal(token0,0)) and not(equal(token1,token3))) then return(-1) |
| 177 |
), |
| 178 |
|
| 179 |
/* and what happens if icentral#0 */ |
| 180 |
|
| 181 |
side_subintervals_list:delete(subintervals_list[icentral],subintervals_list), |
| 182 |
if is(equal(length(side_subintervals_list),0)) then (if is(equal(token1,token2)) then return(token1*token2) else return(-1)), |
| 183 |
if not(evenp(length(side_subintervals_list))) then return(-1), |
| 184 |
L:length(side_subintervals_list)/2, |
| 185 |
for k:1 thru L do count1[k]:charfun(is(equal(side_subintervals_list[k],reverse(map("-",side_subintervals_list[2*L+1-k]))))), |
| 186 |
if sum(count1[j],j,1,L)#L then return(-1), |
| 187 |
side_subvalues_list:delete(subvalues_list[icentral],subvalues_list), |
| 188 |
|
| 189 |
/* remove those subintervals in which funvar vanishes */ |
| 190 |
|
| 191 |
zero_subintervals:sublist_indices(side_subvalues_list,lambda([x],x=0)), |
| 192 |
tmp4:copylist(side_subintervals_list), |
| 193 |
for j:1 thru length(zero_subintervals) do |
| 194 |
(tmp4:delete(side_subintervals_list[zero_subintervals[j]],tmp4)), |
| 195 |
non_zero_side_subintervals_list:tmp4, |
| 196 |
tmp5:copylist(side_subvalues_list), |
| 197 |
for j:1 thru length(zero_subintervals) do |
| 198 |
(tmp5:delete(side_subvalues_list[zero_subintervals[j]],tmp5)), |
| 199 |
non_zero_side_subvalues_list:tmp5, |
| 200 |
|
| 201 |
/* if length(non_zero_side_subintervals_list)=0 then we are done just with token1 and token2 */ |
| 202 |
|
| 203 |
if is(equal(length(non_zero_side_subintervals_list),0)) then (if is(equal(token1,token2)) then return(token1*token2) else return(-1)), |
| 204 |
|
| 205 |
/* otherwise, we must also take into account token3 */ |
| 206 |
LL:length(non_zero_side_subintervals_list)/2, |
| 207 |
for k:1 thru LL do count2[k]:charfun(is(equalp(non_zero_side_subvalues_list[k],ratsubst(-x,x,non_zero_side_subvalues_list[2*LL+1-k])))), |
| 208 |
if is(equal(sum(count2[j],j,1,LL),LL)) then token3:0, |
| 209 |
for k:1 thru LL do count3[k]:charfun(is(equalp(non_zero_side_subvalues_list[k],-ratsubst(-x,x,non_zero_side_subvalues_list[2*LL+1-k])))), |
| 210 |
if is(equal(sum(count3[j],j,1,LL),LL)) then token3:1, |
| 211 |
if is(not(equal(token0,0)) and is(not(equal(token1,token2)) or not(equal(token1,token3)) or not(equal(token2,token3))) ) then return(-1) |
| 212 |
elseif is(not(equal(token0,0)) and is(equal(token1,0)) ) then return(0) |
| 213 |
elseif is(not(equal(token0,0)) and is(equal(token1,1)) ) then return(1) |
| 214 |
elseif is( equal(token0,0) and equal(token3,0) and equal(token2,0)) then return(0) |
| 215 |
elseif is( equal(token0,0) and equal(token3,1) and equal(token2,1)) then return(1) |
| 216 |
else return(-1) |
| 217 |
|
| 218 |
)$ |
| 219 |
|
| 220 |
fourier_sec_coeff(fuvar,x):= |
| 221 |
block([pp,LL,lm,a0,coeff], |
| 222 |
local(a,b,n,MM,NN,PP,QQ), |
| 223 |
declare(n,integer), |
| 224 |
pp:((length(fuvar)/2)-1), |
| 225 |
LL:makelist(part(fuvar,i),i,makelist(2*s-1,s,1,pp)), |
| 226 |
for j:1 thru length(LL) step 1 do |
| 227 |
( |
| 228 |
MM[j]:makelist(part(LL[j],r),r,1,length(LL[j])), |
| 229 |
for r:1 thru 2 do NN(j,r):=makelist(part(MM[j],r,k),k,1,length(MM[j])), |
| 230 |
PP[j]:append(NN(j,1),NN(j,2)),QQ[j]:delete(x,PP[j]) |
| 231 |
), |
| 232 |
for i:1 thru pp step 1 do partsums[i]:sort(QQ[i],"<"), |
| 233 |
for i:1 thru pp step 1 do partfunc[i]:part(fuvar,2*i), |
| 234 |
lm:lmax(unique(flatten(makelist(QQ[q],q,1,length(LL))))), |
| 235 |
a0:(1/(2*lm))*sum(integrate(partfunc[i],x,partsums[i][1],partsums[i][2]),i,1,pp), |
| 236 |
if is(equal(paritycheck(fuvar,x),1)) then a(n):=0 else a(n):=(1/lm)*sum(adefint(partfunc[i]*cos(%pi*n*x/lm),x,partsums[i][1],partsums[i][2]),i,1,pp), |
| 237 |
if is(equal(paritycheck(fuvar,x),0)) then b(n):=0 else b(n):=(1/lm)*sum(adefint(partfunc[i]*sin(%pi*n*x/lm),x,partsums[i][1],partsums[i][2]),i,1,pp), |
| 238 |
coeff:[a0,simplify_sum(a(n)),simplify_sum(b(n))], |
| 239 |
factor(ratsimp(coeff)) |
| 240 |
)$ |
| 241 |
|
| 242 |
fourier_sec_series(fuvar,x,u):= |
| 243 |
block([pp,LL,lm,a0,coeff], |
| 244 |
local(a,b,n,MM,NN,PP,QQ), |
| 245 |
declare(n,integer), |
| 246 |
pp:((length(fuvar)/2)-1), |
| 247 |
LL:makelist(part(fuvar,i),i,makelist(2*s-1,s,1,pp)), |
| 248 |
for j:1 thru length(LL) step 1 do |
| 249 |
( |
| 250 |
MM[j]:makelist(part(LL[j],r),r,1,length(LL[j])), |
| 251 |
for r:1 thru 2 do NN(j,r):=makelist(part(MM[j],r,k),k,1,length(MM[j])), |
| 252 |
PP[j]:append(NN(j,1),NN(j,2)),QQ[j]:delete(x,PP[j]) |
| 253 |
), |
| 254 |
for i:1 thru pp step 1 do partsums[i]:sort(QQ[i],"<"), |
| 255 |
for i:1 thru pp step 1 do partfunc[i]:part(fuvar,2*i), |
| 256 |
lm:lmax(unique(flatten(makelist(QQ[q],q,1,length(LL))))), |
| 257 |
a0:(1/(2*lm))*sum(integrate(partfunc[i],x,partsums[i][1],partsums[i][2]),i,1,pp), |
| 258 |
if is(equal(paritycheck(fuvar,x),1)) then a(n):=0 else a(n):=(1/lm)*sum(adefint(partfunc[i]*cos(%pi*n*x/lm),x,partsums[i][1],partsums[i][2]),i,1,pp), |
| 259 |
if is(equal(paritycheck(fuvar,x),0)) then b(n):=0 else b(n):=(1/lm)*sum(adefint(partfunc[i]*sin(%pi*n*x/lm),x,partsums[i][1],partsums[i][2]),i,1,pp), |
| 260 |
a0+intosum(sum(a(n)*cos(%pi*n*x/lm),n,1,u))+intosum(sum(b(n)*sin(%pi*n*x/lm),n,1,u)))$ |