Debian Codegraph @

texmf-dist/scripts/ketcindy/ketlib/maximaL/pdefourier.mac

raw
1 /*
2 Fourier analysis Maxima package
3
4 Authors: Jose A. Vallejo
5 Faculty of Sciences
6 Universidad Autonoma de San Luis Potosi (Mexico)
7 http://galia.fc.uaslp.mx/~jvallejo
8
9 Emmanuel Roque
10 Faculty of Sciences
11 Universidad Autonoma de San Luis Potosi (Mexico)
12 */
13
14 load(fourie)$
15 load(simplify_sum)$
16 load(draw)$
17 /*The following rules are intented to work with intervals in "a canonical way"
18 leq,geq,lss,grtr are for bounded intervals
19
20
21 Note: Instead of using constantp inside of matchdeclare, numberp but this approach won't work if %pi is used, so probably is not an option since this is a Fourier package and %pi will appear most of the time. Another possible approach is to use freeof(var) instead of constantp
22 */
23 matchdeclare(constn, constantp)$
24 defrule(leq,constn>=xx,xx<=constn)$
25 defrule(geq,xx>=constn,constn<=xx)$
26 defrule(lss,constn>xx,xx<constn)$
27 defrule(grtr,xx>constn,constn<xx)$
28
29 /* fun_parts(expr,var)
30 Input: piecewise defined function written in one of the following ways
31
32 if x>=a_0 and x<=a_1 then expr1 elseif x>a_1 and x<= a_2 then expr2 ... elseif x>a_n and x<=a_{n+1} then expr_{n+1}
33
34 if x<=a_0 then expr0 elseif x>a0 and x<=a1 then expr1 ... elseif x>=a_n then exprn
35
36 Output: A list [[a0,a1,expr1],[a1,a2,expr2,]...]
37
38 Important notes:
39 -In all cases it is expected that a_i<a_{i+1}
40 Writing (x<=7 and x>=6) instead of (x>=6 and x<=7) does not work.
41 -If in the function definition some of the intervals are empty (e.g x>=3 and x<=-3) returns error.
42 -Use of else is currently unsupported, the expr after else is ignored! Use elseif instead.
43 -functionality with logical operator "or" is currently unsupported, will be treated same as "and".
44 -fun_parts does not check if the intervals are disjoint!
45 */
46
47 fun_parts(expr,var):=block(
48 [subint,subval,tmp,ll,tmp1,tmp2,xx,ans,tmp3,tmpb,leftU,rightU],
49 if not piecewisep(expr) then error("The function is not piecewise defined") else (
50 xx:var,
51 ll:(length(expr)-2)/2,
52 tmp:makelist(inpart(expr,i),i,makelist(2*k-1,k,1,ll)),
53 subval:makelist(inpart(expr,i),i,makelist(2*k,k,1,ll)),
54 tmp1:makelist(operatorp(tmp[j],["<",">","<=",">="]),j,1,ll),
55 tmp2:sublist_indices(tmp1,lambda([x],x=true)),
56 /*if tmp2 is an empty list then all the intervals in the domain
57 of the function are bounded */
58 if emptyp(tmp2) then(
59 /*Get rid of logical operators*/
60 tmp:makelist(makelist(inpart(tmp[k],i),i,1,2),k,1,ll),
61 /*Use canonical ordering*/
62 tmp:apply1(tmp,leq,geq,lss,grtr),
63 /*Extract subintervals*/
64 subint:makelist(makelist(inpart(tmp[i][j],j),j,1,2),i,1,ll),
65 /*Check for ill defined subintervals*/
66 tmp3:map(lambda([L],lfreeof(L,var)),subint),
67 if not emptyp(sublist_indices(tmp3,lambda([x],x=false))) then
68 error("Check function definition, some interval(s) appear to be empty or ill-defined. Read documentation for further details.")
69 else (ans:makelist([subint[k],subval[k]],k,1,ll),
70 return(ans) )
71 )
72 /*Function is not bounded*/
73 else(
74 if not is(tmp2=[1,ll]) then error("Check function definition, some interval(s) appear to be empty or ill-defined. Read documentation for further details.")
75 else (if is(ll=2) then(
76 tmp:apply1(tmp,leq,geq,lss,grtr),
77 leftU:[[minf,inpart(tmp[1],2)],subval[1]],
78 rightU:[[inpart(tmp[2],1),inf],subval[2]],
79 ans:[leftU,rightU],
80 return(ans)
81 )
82 elseif is(ll>2) then(
83 leftU:apply1(tmp[1],leq,geq,lss,grtr),
84 rightU:apply1(tmp[ll],leq,geq,lss,grtr),
85 leftU:[[minf,inpart(leftU,2)],subval[1]],
86 rightU:[[inpart(rightU,1),inf],subval[ll]],
87 /*Get rid of logical operators in the bounded intervals*/
88 tmpb:makelist(makelist(inpart(tmp[k],i),i,1,2),k,2,ll-1),
89 /*Use canonical ordering*/
90 tmpb:apply1(tmpb,leq,geq,lss,grtr),
91 /*Extract bounded subintervals*/
92 subint:makelist(makelist(inpart(tmpb[i][j],j),j,1,2),i,1,ll-2),
93 /*Check for ill-defined subintervals*/
94 tmp3:map(lambda([L],lfreeof(L,var)),subint),
95 if not emptyp(sublist_indices(tmp3,lambda([x],x=false))) then
96 error("Check function definition, some interval(s) appear to be empty or ill-defined. Read documentation for further details.")
97 else ( ans:append([leftU],makelist([subint[k],subval[k+1]],k,1,ll-2),[rightU]),
98 return(ans))
99 )
100
101 )
102 )
103 ))$
104
105 /*bint_comp(L1,L2,var)
106 Bounded intervals comparison
107 Input: Two lists, L1 and L2, each one having the following format
108 [[a_i,b_i],expr_i]
109 Output: A flag used by parityL
110 */
111
112 bint_comp(L1,L2,var):=block(
113 if is(L1[1][1]=-L2[1][2]) and is(L1[1][2]=-L2[1][1]) then(
114 if is(L1[2]=0) and is(L2[2]=0) then return('zero)
115 elseif equalp(L1[2],ratsubst(-var,var,L2[2])) then return('even)
116 elseif equalp(L1[2],-ratsubst(-var,var,L2[2])) then return('odd) else return('none)
117 )
118 else return('none)
119 )$
120 /* uint_comp(L1,L2,var)
121 Unbounded intervals comparison
122 Input: Two list, L1 and L2, corresponding to unbounded intervals in
123 the following format
124 [[minf,b_1],expr1] or [[a_2,inf],expr2]
125 Output: A flag used by parityL
126 It is assumed that parity check has already checked if a_1,b_2 are equal to minf,inf respectively.
127 */
128 uint_comp(L1,L2,var):=block(
129 if is(L1[1][2]=-L2[1][1]) then(
130 if is(L1[2]=0) and is(L2[2]=0) then return('zero)
131 elseif equalp(L1[2],ratsubst(-var,var,L2[2])) then return('even)
132 elseif equalp(L1[2],-ratsubst(-var,var,L2[2])) then return('odd) else return('none)
133 )
134 else return('none)
135 )$
136
137 /*parityb(L,var)
138 Input: A list returned by fun_parts of a bounded function
139 Output: A list used by parityL
140 */
141 parityb(L,var):=block(
142 [ll,icentral,aux,ans,llaux,Laux,paux],
143 ll:length(L),
144 /* Trivial case, just check if the only interval has
145 an even expression, an odd expression or neither of them.*/
146 if is(ll=1) and is(L[1][1][1]=-L[1][1][2]) then(
147 if evenfunp(L[1][2],var) then return('even)
148 elseif oddfunp(L[1][2],var) then return('odd)
149 else return('none)
150 )
151 elseif is(ll>1) then(
152 icentral:0,
153 /*Search if there is a central interval*/
154 for i:1 thru ll do (if is(L[i][1][1]*L[i][1][2]<0) then icentral:i),
155 /*Case 1: icentral=0 */
156 if is(icentral=0) then(
157 if not evenp(ll) then return('none) else(
158 aux:makelist(bint_comp(L[i],L[ll+1-i],var),i,1,ll/2),
159 if is(length(sublist_indices(aux,lambda([x],x=even or x=zero)))=ll/2)
160 then return('even)
161 elseif is(length(sublist_indices(aux,lambda([x],x=odd or x=zero)))=ll/2)
162 then return('odd)
163 else return('none)
164 )
165 )
166 /*icentral>0*/
167 elseif is(icentral>0) and oddp(ll) then(
168 /*Check parity of central element*/
169 if is(L[icentral][1][1]=-L[icentral][1][2]) then (
170 if evenfunp(L[icentral][2],var) then paux:'even
171 elseif oddfunp(L[icentral][2],var) then paux:'odd
172 else return('none),
173 Laux:delete(L[icentral],L),
174 aux:makelist(bint_comp(Laux[i],Laux[ll-i],var),i,1,(ll-1)/2),
175 if is(length(sublist_indices(aux,lambda([x],x=even or x=zero)))=(ll-1)/2) and is(paux=even)
176 then return('even)
177 elseif is(length(sublist_indices(aux,lambda([x],x=odd or x=zero)))=(ll-1)/2) and is(paux=odd)
178 then return('odd)
179 else return('none)
180 )
181 else return('none)
182 )
183 ))$
184
185
186
187 /*parityL(L,var)
188
189 Input: A list returned by fun_parts
190 Output: the parity of the function
191
192 Important notes:
193 -
194 */
195 parityL(L,var):=block(
196 [aux1,aux2,Laux,ll],
197 ll:length(L),
198 if (not boundedp(L)) and is(ll=2) then(
199 if is(uint_comp(L[1],L[2],var)=zero) or is(uint_comp(L[1],L[2],var)=even) then return('even)
200 elseif is(uint_comp(L[1],L[2],var)=odd) then
201 return('odd)
202 else return('none)
203 )
204 /*Check if there are unbounded intervals*/
205 elseif (not boundedp(L)) and is(ll>2) then(
206 aux1:uint_comp(L[1],L[ll],var),
207 Laux:delete(L[1],L),
208 Laux:delete(L[ll],Laux),
209 aux2:parityb(Laux,var),
210 if (is(aux1=zero) and is(aux2=even)) or
211 (is(aux1=even) and is(aux2=even)) then
212 return('even) elseif (is(aux1=zero) and is(aux2=odd)) or
213 (is(aux1=odd) and is(aux2=odd)) then return('odd)
214 else return('none)
215 )
216 else return(parityb(L,var))
217
218 )$
219 boundedp(L):=block(
220 [ll],
221 ll:length(L),
222 if is(L[1][1][1]=minf) and is (L[ll][1][2]=inf)
223 then return(false)
224 else return(true)
225 )$
226
227 piecewisep(expr):=if atom(expr) then false else is(inpart(expr,0)="if")$
228 paritycheck(expr,var):=block(
229 if piecewisep(expr) then return(parityL(fun_parts(expr,var),var))
230 elseif listp(expr) then return(parityL(expr,var))
231 else (
232 if equalp(ratsubst(-var,var,expr),expr) then return('even)
233 elseif equalp(ratsubst(-var,var,expr),-expr) then return('odd)
234 else return('none)
235 )
236 )$
237
238 /*Check if the domain is bounded and symmetric [-L,L]
239 symbintp(L)
240 intput: a list L returned by fun_parts
241 output: true or false
242 */
243
244 symbintp(L):=if boundedp(L) and is(L[1][1][1]=-L[length(L)][1][2]) then L[length(L)][1][2] else false$
245
246 /*integratepw(L,var)
247
248 Integrate the list form of a piecewise defined function
249
250 */
251
252 integratepw(L,var):=block(
253 [ll],
254 ll:length(L),
255 return(sum(integrate(L[i][2],var,L[i][1][1],L[i][1][2]),i,1,ll))
256 )$
257
258 /* sum2list(expr)
259 Auxiliary function
260 Input: An expression
261 Output: A list whose elements are the terms of expression
262
263 Important notes: sum2list does not check by itself if op(expr)="+" or not*/
264
265 sum2list(expr):= if is(expr=0) then [0] elseif is(nterms(expr)=1) then [expr] else args(expr)$
266
267 /*secsum2bl(L)
268 Input: A list as in fun_parts output format
269 Output:
270 */
271 secsum2bl(L):=block(
272 [ll,laux],
273 ll:length(L),
274 laux:makelist(i,i,1,ll),
275 create_list([L[i][1],y],i,laux,y,sum2list(L[i][2]))
276 )$
277
278 /*Match rules for special cases*/
279 matchdeclare(nexp,lambda([e],e#0 and nonnegintegerp(e)))$
280 matchdeclare(nfreq,lambda([e],e#0 and nonnegintegerp(e)))$
281 matchdeclare(const,lambda([e],e#0 and freeof(xargument,e)))$
282 /*matchdeclare(argtrig,lambda([e],e#0 and constantp(e)))$
283 defmatch(powx,const*xargument^nexp,xargument)$*/
284 defmatch(powxsin,const*xargument^nexp*sin(nfreq*%pi*xargument/pargument),xargument,pargument)$
285 defmatch(powxcos,const*xargument^nexp*cos(nfreq*%pi*xargument/pargument),xargument,pargument)$
286 defmatch(multsin,const*sin(nfreq*%pi*xargument/pargument),xargument,pargument)$
287 defmatch(multcos,const*cos(nfreq*%pi*xargument/pargument),xargument,pargument)$
288 /*powxcos_int
289 Computes \int_a^b (x^r*cos(n*%pi*x/L)dx for r>=0
290 */
291 powxcos_int(pow,freq,p,a,b):=block(
292 if is(freq=0) then (b^(pow+1)-a^(pow+1))/(pow+1)
293 elseif is(pow=0) then (p/(%pi*freq))*(sin(freq*%pi*b/p)-sin(freq*%pi*a/p))
294 elseif is(pow>0) then (p/(%pi*freq))*(b^pow*sin(freq*%pi*b/p)-a^pow*sin(freq*%pi*a/p))-(p*pow/(freq*%pi)*powxsin_int(pow-1,freq,p,a,b))
295 )$
296 /*powxsin_int
297 Computes \int_a^b x^r*sin(n*%pi*x/L) dx for r>=0 */
298 powxsin_int(pow,freq,p,a,b):=block(
299 if is(freq=0) then 0
300 elseif is(pow=0) then (p/(%pi*freq))*(cos(freq*%pi*a/p)-cos(freq*%pi*b/p))
301 elseif is(pow>0) then (p/(%pi*freq))*(a^pow*cos(freq*%pi*a/p)-b^pow*cos(freq*%pi*b/p))+(p*pow/(freq*%pi)*powxcos_int(pow-1,freq,p,a,b))
302 )$
303 /*Heuristic a_n
304
305 */
306 heuristic_an(expr,var,p,a,b):=block(
307 [n,ans],
308 declare(n,integer),
309 if listp(powxsin(expr,var,p)) then (
310 if is(a=-b) and evenp(nexp) then 0 else(
311 ans:(const/(2*p))*powxsin_int(nexp,nfreq+n,p,a,b)+ev((const/(2*p))*powxsin_int(nexp,nfreq-n,p,a,b),noeval,n),
312 remove(n,integer),
313 ans
314 )
315 )
316 elseif listp(powxcos(expr,var,p)) then (
317 if is(a=-b) and oddp(nexp) then 0 else(
318 ans:(const/(2*p))*powxcos_int(nexp,nfreq+n,p,a,b)+ev((const/(2*p))*powxcos_int(nexp,nfreq-n,p,a,b),noeval,n),
319 remove(n,integer),
320 ans)
321 )
322 elseif listp(multsin(expr,var,p)) then(
323 if is(a=-b) then 0 else(
324 ans:(const/(2*p))*powxsin_int(0,nfreq+n,p,a,b)+ev((const/(2*p))*powxsin_int(0,nfreq-n,p,a,b),noeval,n),
325 remove(n,integer),
326 ans
327 )
328 )
329 elseif listp(multcos(expr,var,p)) then (
330 ans:(const/(2*p))*powxcos_int(0,nfreq+n,p,a,b)+ev((const/(2*p))*powxcos_int(0,nfreq-n,p,a,b),noeval,n),
331 remove(n,integer),
332 ans
333 )
334 else(
335 ans:adefint(expr*cos(n*%pi*var/p),var,a,b)/p,
336 remove(n,integer),
337 ans
338 )
339 )$
340 /*Heuristic b_n
341
342 */
343 heuristic_bn(expr,var,p,a,b):=block(
344 [n,ans],
345 declare(n,integer),
346 if listp(powxsin(expr,var,p)) then (
347 if is(a=-b) and oddp(nexp) then 0 else(
348 ans:(-const/(2*p))*powxcos_int(nexp,nfreq+n,p,a,b)+ev((const/(2*p))*powxcos_int(nexp,nfreq-n,p,a,b),noeval,n),
349 remove(n,integer),
350 ans
351 )
352 )
353 elseif listp(powxcos(expr,var,p)) then (
354 if is(a=-b) and evenp(nexp) then 0 else(
355 ans:(const/(2*p))*powxsin_int(nexp,nfreq+n,p,a,b)+ev((const/(2*p))*powxsin_int(nexp,n-nfreq,p,a,b),noeval,n),
356 remove(n,integer),
357 ans
358 )
359 )
360 elseif listp(multsin(expr,var,p)) then(
361 ans:(-const/(2*p))*powxcos_int(0,nfreq+n,p,a,b)+ev((const/(2*p))*powxcos_int(0,nfreq-n,p,a,b),noeval,n),
362 remove(n,integer),
363 ans
364 )
365 elseif listp(multcos(expr,var,p)) then (
366 if is(a=-b) then 0 else(
367 ans:(const/(2*p))*powxsin_int(0,nfreq+n,p,a,b)+ev((const/(2*p))*powxsin_int(0,n-nfreq,p,a,b),noeval,n),
368 remove(n,integer),
369 ans
370 )
371 )
372 else(
373 ans:adefint(expr*sin(n*%pi*var/p),var,a,b)/p,
374 remove(n,integer),
375 ans
376 )
377 )$
378
379 /*foucoeffpw(L,var)
380 Fourier coefficients of a piecewise defined function
381 Currently it only works with functions defined on an interval of the form [-p,p].
382 The output is a list of the form [a0,a1,b1,an,bn]
383 */
384
385 foucoeffpw(L,var):=block(
386 [llaux,ll,a0,an,bn,answ,lm],
387 ll:length(L),
388 lm:(L[ll][1][2]-L[1][1][1])/2, /*For more general intervals*/
389 Laux:secsum2bl(L),
390 llaux:length(Laux),
391 if is(parityL(L,var)=odd) then (
392 a0:0, an:0,
393 bn:sum(heuristic_bn(Laux[i][2],var,lm,Laux[i][1][1],Laux[i][1][2]),i,1,llaux),
394 answ:[a0,an,simplify_sum(bn)],
395 return(ratsimp(answ)))
396 elseif is(parityL(L,var)=even) then (
397 a0:(1/(2*lm))*integratepw(L,var),
398 an:sum(heuristic_an(Laux[i][2],var,lm,Laux[i][1][1],Laux[i][1][2]),i,1,llaux),
399 bn:0,
400 answ:[simplify_sum(a0),simplify_sum(an),bn],
401 return(ratsimp(answ)))
402 else(
403 a0:(1/(2*lm))*integratepw(L,var),
404 an:sum(heuristic_an(Laux[i][2],var,lm,Laux[i][1][1],Laux[i][1][2]),i,1,llaux),
405 bn:sum(heuristic_bn(Laux[i][2],var,lm,Laux[i][1][1],Laux[i][1][2]),i,1,llaux),
406 answ:[a0,an,bn],
407 answ:map(simplify_sum,answ),
408 return(ratsimp(answ) ))
409 )$
410
411 foucoeffterm(term,var,p):=block(
412 [a0,bn,an,ans],
413 if listp(term) then(
414 /* if is(symbintp(term,var)=p) then */
415 return(foucoeffpw(term,var))
416 /* else error("Domain of the piecewise function is not valid. Read the documentation for further details")*/
417 )
418 elseif piecewisep(term) then(
419 /*Check if the domain is valid*/
420 /*if is(symbintp(fun_parts(term,var))=p) then*/
421 if is(boundedp(fun_parts(term,var))) then
422 return(foucoeffpw(map(lambda([e],[e[1],expand(trigrat(e[2]))]),fun_parts(term,var)),var))
423 else error("Domain of the piecewise function is not valid. Read the documentation for further details")
424 )
425 else(
426 /*Check parity of term*/
427 if is(paritycheck(term,var)=even) then(
428 a0:ratsimp((1/(2*p))*adefint(term,var,-p,p)),
429 an:heuristic_an(term,var,p,-p,p),
430 bn:0,
431 ans:ratsimp([a0,an,bn]),
432 return(ans)
433 )
434 elseif is(paritycheck(term,var)=odd) then(
435 a0:0, an:0,
436 bn:heuristic_bn(term,var,p,-p,p),
437 ans:ratsimp([a0,an,bn]),
438 return(ans)
439 )
440 else(
441 a0:ratsimp((1/(2*p))*adefint(term,var,-p,p)),
442 an:heuristic_an(term,var,p,-p,p),
443 bn:heuristic_bn(term,var,p,-p,p),
444 ans:ratsimp([a0,an,bn]),
445 return(ans)
446 )
447 )
448 )$
449
450 list2bl(L):=block(
451 [ll,laux],
452 ll:length(L),
453 laux:makelist(i,i,1,ll),
454 create_list(y,i,laux,y,sum2list(L[i]))
455 )$
456
457 trigpattern(term,var,p):=block(
458 if listp(powxsin(term,var,p)) then return(nfreq)
459 elseif listp(powxcos(term,var,p)) then return(nfreq)
460 elseif listp(multcos(term,var,p)) then return(nfreq)
461 elseif listp(multsin(term,var,p)) then return(nfreq)
462 else return(false)
463 )$
464
465 searchpoles_term(term,var,p):=block(
466 [L,Laux,indx_aux,indx],
467 if listp(term) then(
468 L:secsum2bl(term),
469 indx_aux:map(lambda([e],trigpattern(e[2],var,p)),L),
470 indx:unique(sublist(indx_aux,integerp)),
471 return(indx)
472 )
473 elseif piecewisep(term) then(
474 Laux:map(lambda([e],[e[1],expand(trigrat(e[2]))]),fun_parts(term,var)),
475 L:secsum2bl(Laux),
476 indx_aux:map(lambda([e],trigpattern(e[2],var,p)),L),
477 indx:unique(sublist(indx_aux,integerp)),
478 return(indx)
479 ) else(
480 indx_aux:trigpattern(term,var,p),
481 if integerp(indx_aux) then return([indx_aux])
482 else return([])
483 )
484 )$
485
486 searchpoles(expr,var,p):=block(
487 [L,Laux,indx],
488 Laux:sum2list(expand(expr)),
489 Laux:map(lambda([e],if not piecewisep(e) then expand(trigrat(e)) else e),Laux),
490 L:list2bl(Laux),
491 indx:apply(append,map(lambda([e],searchpoles_term(e,var,p)),L)),
492 return(unique(indx))
493 )$
494
495 fouriercoeff(expr,var,p):=block(
496 [ans,Llist,Laux,coeffpoles,poles,n],
497 Laux:sum2list(expand(expr)),
498 Llist:map(lambda([e],if not piecewisep(e) then expand(trigrat(e)) else e),Laux),
499 Llist:list2bl(Llist),
500 ans:lsum(y,y,map(lambda([e],foucoeffterm(e,var,p)),Llist)),
501 ans:ratsimp(ans),
502 poles:searchpoles(expr,var,p),
503 coeffpoles:makelist(at([i,ans[2],ans[3]],n=i),i,poles),
504 declare(n,integer),
505 coeffpoles:ev(coeffpoles),
506 ans:ev(ans),
507 remove(n,integer),
508 return(ratsimp([ans,coeffpoles]))
509 )$
510
511 fouriercoeff_expand(fcoeff,var,p,NN):=block(
512 [ans,a0,an,bn,poles,indx_poles,polessum,sl_indx,laux],
513 [[a0,an,bn],poles]:fcoeff,
514 an:ev(an),
515 bn:ev(bn),
516 if emptyp(poles) then(
517 ans:a0+sum(at(an*cos(n*%pi*var/p)+bn*sin(n*%pi*var/p),n=n),n,1,NN),
518 return(ans)
519 ) else(
520 indx_poles:makelist(poles[i][1],i,1,length(poles)),
521 if is(NN=inf) then(
522 polessum:sum(poles[i][2]*cos(poles[i][1]*%pi*var/p)+poles[i][3]*sin(poles[i][1]*%pi*var/p),i,1,length(poles)),
523 ans:a0+polessum+sum(at(an*cos(n*%pi*var/p)+bn*sin(n*%pi*var/p),n=n),n,1,inf),
524 if is(an=0) and is(bn=0) then return(ans),
525 print("The sum is over \\N -",setify(indx_poles)),
526 return(ans)
527 )
528 else(
529 sl_indx:sublist_indices(indx_poles,lambda([e],is(e<=NN))),
530 polessum:lsum(poles[i][2]*cos(poles[i][1]*%pi*var/p)+poles[i][3]*sin(poles[i][1]*%pi*var/p),i,sl_indx),
531 laux:listify(setdifference(setify(makelist(i,i,1,NN)),setify(indx_poles))),
532 ans:a0+polessum+lsum(at(an*cos(n*%pi*var/p)+bn*sin(n*%pi*var/p),n=i),i,laux),
533 return(ans)
534 )
535 )
536 )$
537
538 fourier_series(expr,var,p,NN):=block(
539 [fcoeff,ans],
540 fcoeff:fouriercoeff(expr,var,p),
541 ans:fouriercoeff_expand(fcoeff,var,p,NN),
542 ans
543 )$
544
545 fourier_amplitudes(expr,var,p,N):=block(
546 [a0,an,bn,ans,poles,indx_poles,polelist,sl_indx,laux],
547 [[a0,an,bn],poles]:fouriercoeff(expr,var,p),
548 an:ev(an),
549 bn:ev(bn),
550 if emptyp(poles) then(
551 ans:makelist(at([n,sqrt(an^2+bn^2)],n=i),i,1,N),
552 /*ans:float(ans),*/
553 return(ans)) else(
554 indx_poles:makelist(poles[i][1],i,1,length(poles)),
555 sl_indx:sublist_indices(indx_poles,lambda([e],is(e<N))),
556 polelist:makelist([poles[i][1],sqrt(poles[i][2]^2+poles[i][3]^2)],i,sl_indx),
557 laux:listify(setdifference(setify(makelist(i,i,1,N)),setify(indx_poles))),
558 ans:append(polelist,makelist(at([n,sqrt(an^2+bn^2)],n=i),i,laux)),
559 /*ans:float(ans),*/
560 ans:sort(ans,lambda([a,b],a[1]<b[1])),
561 return(ans)
562 )
563 )$
564
565 fourier_freq_list(expr,var,p,N):=block(
566 [modules],
567 modules:float(fourier_amplitudes(expr,var,p,N))
568 )$
569
570 fourier_freq(expr,var,p,N):=block(
571 [modules],
572 modules:float(fourier_amplitudes(expr,var,p,N)),
573 draw2d(points_joined=impulses,line_width=4,color="blue",points(modules),
574 xlabel="w(n)=nw_0",ylabel="|c_n|",axis_top=false,axis_right=false,xtics=1,user_preamble="set grid ytics")
575 )$
576
577 wxfourier_freq(expr,var,p,N):=block(
578 [modules],
579 modules:float(fourier_amplitudes(expr,var,p,N)),
580 wxdraw2d(points_joined=impulses,line_width=4,color="blue",points(modules),
581 xlabel="w(n)=nw_0",ylabel="|c_n|",axis_top=false,axis_right=false,xtics=1,user_preamble="set grid ytics")
582 )$
583
584 atan_fourier(an,bn):=if is(an=0) and is(bn=0) then 0 else atan2(an,bn)$
585
586 fourier_phshift(expr,var,p,N):=block(
587 [a0,an,bn,ans,poles,indx_poles,polelist,sl_indx,laux],
588 [[a0,an,bn],poles]:fouriercoeff(expr,var,p),
589 an:ev(an),
590 bn:ev(bn),
591 if emptyp(poles) then(
592 ans:makelist([i,atan_fourier(at(bn,n=i),at(an,n=i))],i,1,N),
593 /*ans:float(ans),*/
594 return(ans)) else(
595 indx_poles:makelist(poles[i][1],i,1,length(poles)),
596 sl_indx:sublist_indices(indx_poles,lambda([e],is(e<N))),
597 polelist:makelist([poles[i][1],atan_fourier(poles[i][3],poles[i][2])],i,sl_indx),
598 laux:listify(setdifference(setify(makelist(i,i,1,N)),setify(indx_poles))),
599 ans:append(polelist,makelist([i,atan_fourier(at(bn,n=i),at(an,n=i))],i,laux)),
600 /*ans:float(ans),*/
601 ans:sort(ans,lambda([a,b],a[1]<b[1])),
602 return(ans)
603 )
604 )$
605
606
607
608 fourier_harm(expr,var,p,N):=block(
609 [amplitudes,phshift,ans],
610 amplitudes:fourier_amplitudes(expr,var,p,N),
611 phshift:fourier_phshift(expr,var,p,N),
612 ans:makelist(amplitudes[i][2]*cos(i*%pi*var/p-phshift[i][2]),i,1,N),
613 return(ans)
614 )$
615
616 chop(expr,[N]):=if emptyp(N) then scanmap(lambda([x],if is(numberp(x)) then (if is(abs(x)<1.0*10^(-12)) then 0.0 else x) else x),expr)
617 else scanmap(lambda([x],if is(numberp(x)) then (if is(abs(x)<1.0*10^(-N[1])) then 0.0 else x) else x),expr)$
618
619 oddextension_pwterm(L,var):=block(
620 [Laux],
621 Laux:[[-L[1][2],-L[1][1]],-ratsubst(-x,x,L[2])],
622 Laux
623 )$
624
625 oddextensionpw(L,var):=block(
626 [Lodd],
627 Lodd:append(reverse(map(lambda([e],oddextension_pwterm(e,var)),L)),L),
628 Lodd
629 )$
630
631 evenextension_pwterm(L,var):=block(
632 [Laux],
633 Laux:[[-L[1][2],-L[1][1]],ratsubst(-x,x,L[2])],
634 Laux
635 )$
636
637 evenextensionpw(L,var):=block(
638 [Lodd],
639 Lodd:append(reverse(map(lambda([e],evenextension_pwterm(e,var)),L)),L),
640 Lodd
641 )$
642
643 odd_extension(expr,var,p):=block(
644 [Laux],
645 if piecewisep(expr) then(
646 Laux:fun_parts(expr,var),
647 if is(Laux[1][1][1]=0) and is(Laux[length(Laux)][1][2]=p) then return(oddextensionpw(Laux,var))
648 else error("The domain must be of the form [0,p]")
649 )
650 else(
651 Laux:[[[0,p],expr]],
652 return(oddextensionpw(Laux,var))
653 )
654 )$
655
656 even_extension(expr,var,p):=block(
657 [Laux],
658 if piecewisep(expr) then(
659 Laux:fun_parts(expr,var),
660 if is(Laux[1][1][1]=0) and is(Laux[length(Laux)][1][2]=p) then return(evenextensionpw(Laux,var))
661 else error("The domain must be of the form [0,p]")
662 )
663 else(
664 Laux:[[[0,p],expr]],
665 return(evenextensionpw(Laux,var))
666 )
667 )$
668
669 /*foucoscoeff(expr,var,p):=block(
670 [Laux,fcoeff,a0,an,bn,poles],
671 Laux:even_extension(expr,var,p),
672 [[a0,an,bn],poles]:fouriercoeff(Laux,var,p),
673 fcoeff:[[a0,an],map(lambda([e],[e[1],e[2]]),poles)],
674 return(fcoeff)
675 )$
676
677 fousincoeff(expr,var,p):=block(
678 [Laux,fcoeff,a0,an,bn,poles],
679 Laux:odd_extension(expr,var,p),
680 [[a0,an,bn],poles]:fouriercoeff(Laux,var,p),
681 fcoeff:[[bn],map(lambda([e],[e[1],e[3]]),poles)],
682 return(fcoeff)
683 )$
684 */
685
686 /*Here we have the new implementation of fouriersincoeff and fouriercoscoeff*/
687
688 fouriersincoeffpw(Llist,var):=block(
689 [llaux,ll,bn,answ,lm],
690 ll:length(Llist),
691 lm:(Llist[ll][1][2]-Llist[1][1][1]), /*No need to divide by 2*/
692 Laux:secsum2bl(Llist),
693 llaux:length(Laux),
694 bn:2*sum(heuristic_bn(Laux[i][2],var,lm,Laux[i][1][1],Laux[i][1][2]),i,1,llaux),
695 answ:[bn],
696 answ:map(simplify_sum,answ),
697 return(ratsimp(answ) )
698 )$
699
700 fouriercoscoeffpw(Llist,var):=block(
701 [llaux,ll,a0,an,answ,lm],
702 ll:length(Llist),
703 lm:(Llist[ll][1][2]-Llist[1][1][1]), /*No need to divide by 2*/
704 Laux:secsum2bl(Llist),
705 llaux:length(Laux),
706 a0:(1/(lm))*integratepw(Llist,var),
707 an:2*sum(heuristic_an(Laux[i][2],var,lm,Laux[i][1][1],Laux[i][1][2]),i,1,llaux),
708 answ:[a0,an],
709 answ:map(simplify_sum,answ),
710 return(ratsimp(answ) )
711 )$
712
713 fouriersincoeffterm(term,var,p):=block(
714 [bn,ans],
715 if listp(term) then(
716 /* if is(symbintp(term,var)=p) then */
717 return(fouriersincoeffpw(term,var))
718 /* else error("Domain of the piecewise function is not valid. Read the documentation for further details")*/
719 )
720 elseif piecewisep(term) then(
721 /*Check if the domain is valid*/
722 /*if is(symbintp(fun_parts(term,var))=p) then*/
723 if is(boundedp(fun_parts(term,var))) then
724 return(fouriersincoeffpw(map(lambda([e],[e[1],expand(trigrat(e[2]))]),fun_parts(term,var)),var))
725 else error("Domain of the piecewise function is not valid. Read the documentation for further details")
726 )
727 else(
728 bn:2*heuristic_bn(term,var,p,0,p),
729 ans:ratsimp([bn]),
730 return(ans)
731 )
732 )$
733
734 fouriercoscoeffterm(term,var,p):=block(
735 [a0,an,ans],
736 if listp(term) then(
737 /* if is(symbintp(term,var)=p) then */
738 return(fouriercoscoeffpw(term,var))
739 /* else error("Domain of the piecewise function is not valid. Read the documentation for further details")*/
740 )
741 elseif piecewisep(term) then(
742 /*Check if the domain is valid*/
743 /*if is(symbintp(fun_parts(term,var))=p) then*/
744 if is(boundedp(fun_parts(term,var))) then
745 return(fouriercoscoeffpw(map(lambda([e],[e[1],expand(trigrat(e[2]))]),fun_parts(term,var)),var))
746 else error("Domain of the piecewise function is not valid. Read the documentation for further details")
747 )
748 else(
749 a0:ratsimp((1/p)*adefint(term,var,0,p)),
750 an:2*heuristic_an(term,var,p,0,p),
751 ans:ratsimp([a0,an]),
752 return(ans)
753 )
754 )$
755
756 fouriersincoeff(expr,var,p):=block(
757 [ans,Llist,Laux,coeffpoles,poles,n],
758 Laux:sum2list(expand(expr)),
759 Llist:map(lambda([e],if not piecewisep(e) then expand(trigrat(e)) else e),Laux),
760 Llist:list2bl(Llist),
761 ans:lsum(y,y,map(lambda([e],fouriersincoeffterm(e,var,p)),Llist)),
762 ans:ratsimp(ans),
763 poles:searchpoles(expr,var,p),
764 /*Now there is only a list of the form [bn]*/
765 coeffpoles:makelist(at([i,ans[1]],n=i),i,poles),
766 declare(n,integer),
767 coeffpoles:ev(coeffpoles),
768 ans:ev(ans),
769 remove(n,integer),
770 return(ratsimp([ans,coeffpoles]))
771 )$
772
773 fouriercoscoeff(expr,var,p):=block(
774 [ans,Llist,Laux,coeffpoles,poles,n],
775 Laux:sum2list(expand(expr)),
776 Llist:map(lambda([e],if not piecewisep(e) then expand(trigrat(e)) else e),Laux),
777 Llist:list2bl(Llist),
778 ans:lsum(y,y,map(lambda([e],fouriercoscoeffterm(e,var,p)),Llist)),
779 ans:ratsimp(ans),
780 poles:searchpoles(expr,var,p),
781 /*Now there is only a list of the form [a0,an]*/
782 coeffpoles:makelist(at([i,ans[2]],n=i),i,poles),
783 declare(n,integer),
784 coeffpoles:ev(coeffpoles),
785 ans:ev(ans),
786 remove(n,integer),
787 return(ratsimp([ans,coeffpoles]))
788 )$
789
790 fouriersincoeff_expand(sincoeff,var,p,NN):=block(
791 [ans,bn,poles,indx_poles,polessum,sl_indx,laux],
792 [[bn],poles]:sincoeff,
793 bn:ev(bn),
794 if emptyp(poles) then(
795 ans:sum(at(bn*sin(n*%pi*var/p),n=n),n,1,NN),
796 return(ans)
797 ) else(
798 indx_poles:makelist(poles[i][1],i,1,length(poles)),
799 if is(NN=inf) then(
800 polessum:sum(poles[i][2]*sin(poles[i][1]*%pi*var/p),i,1,length(poles)),
801 ans:polessum+sum(at(bn*sin(n*%pi*var/p),n=n),n,1,inf),
802 if is(bn=0) then return(ans),
803 print("The sum is over \\N-",setify(indx_poles)),
804 return(ans)
805 )
806 else(
807 sl_indx:sublist_indices(indx_poles,lambda([e],is(e<=NN))),
808 polessum:lsum(poles[i][2]*sin(poles[i][1]*%pi*var/p),i,sl_indx),
809 laux:listify(setdifference(setify(makelist(i,i,1,NN)),setify(indx_poles))),
810 ans:polessum+lsum(at(bn*sin(n*%pi*var/p),n=i),i,laux),
811 return(ans)
812 )
813 )
814 )$
815
816 fouriercoscoeff_expand(coscoeff,var,p,NN):=block(
817 [ans,a0,an,poles,indx_poles,polessum,sl_indx,laux],
818 [[a0,an],poles]:coscoeff,
819 an:ev(an),
820 if emptyp(poles) then(
821 ans:a0+sum(at(an*cos(n*%pi*var/p),n=n),n,1,NN),
822 return(ans)
823 ) else(
824 indx_poles:makelist(poles[i][1],i,1,length(poles)),
825 if is(NN=inf) then(
826 polessum:sum(poles[i][2]*cos(poles[i][1]*%pi*var/p),i,1,length(poles)),
827 ans:a0+polessum+sum(at(an*cos(n*%pi*var/p),n=n),n,1,inf),
828 if is(an=0) then return(ans),
829 print("The sum is over \\N -",setify(indx_poles)),
830 return(ans)
831 )
832 else(
833 sl_indx:sublist_indices(indx_poles,lambda([e],is(e<=NN))),
834 polessum:lsum(poles[i][2]*cos(poles[i][1]*%pi*var/p),i,sl_indx),
835 laux:listify(setdifference(setify(makelist(i,i,1,NN)),setify(indx_poles))),
836 ans:a0+polessum+lsum(at(an*cos(n*%pi*var/p),n=i),i,laux),
837 return(ans)
838 )
839 )
840 )$
841
842 fouriersin_series(expr,var,p,NN):=block(
843 [ans,sincoeff],
844 sincoeff:fouriersincoeff(expr,var,p),
845 ans:fouriersincoeff_expand(sincoeff,var,p,NN),
846 ans
847 )$
848
849 fouriercos_series(expr,var,p,NN):=block(
850 [ans,coscoeff],
851 coscoeff:fouriercoscoeff(expr,var,p),
852 ans:fouriercoscoeff_expand(coscoeff,var,p,NN),
853 ans
854 )$
855
856 real2complex_fcoeff(fcoeff):=block(
857 [c0,cn,cpoles,rpoles,a0,an,bn],
858 [[a0,an,bn],rpoles]:fcoeff,
859 c0:a0,
860 cn:(an-%i*bn)/2,
861 cpoles:map(lambda([e],[e[1],(e[2]-%i*e[3])/2]),rpoles),
862 ratsimp([[c0,cn],cpoles])
863 )$
864
865 cfouriercoeff(expr,var,p):=real2complex_fcoeff(fouriercoeff(expr,var,p))$
866
867 cfouriercoeff_expand(cfcoeff,var,p,NN):=block(
868 [ans,c0,cn,cpoles,indx_cpoles,cpolessumpos,cpolessumneg,sl_indx,laux],
869 [[c0,cn],cpoles]:cfcoeff,
870 an:ev(cn),
871 if emptyp(cpoles) then(
872 if is(NN=inf) then(
873 ans:c0+sum(at(cn*exp(%i*n*%pi*var/p),n=n),n,minf,inf),
874 print("The sum is over \\N-\{0\}"),
875 return(ans)
876 ) else(
877 ans:c0+sum(at(conjugate(cn)*exp(%i*-n*%pi*var/p),n=n),n,1,NN)
878 +sum(at(cn*exp(%i*n*%pi*var/p),n=n),n,1,NN),
879 return(ratsimp(demoivre(ans))))
880 ) else(
881 indx_cpoles:map(first,cpoles),
882 if is(NN=inf) then(
883 cpolessumpos:sum(cpoles[i][2]*exp(%i*cpoles[i][1]*%pi*var/p),i,1,length(cpoles)),
884 cpolessumneg:sum(conjugate(cpoles[i][2])*exp(-%i*cpoles[i][1]*%pi*var/p),i,1,length(cpoles)),
885 ans:c0+cpolessumpos+cpolessumneg+sum(at(cn*exp(%i*n*%pi*var/p),n=n),n,minf,inf),
886 if is(cn=0) then return(ans),
887 print("The sum is over \\N -",setify(append(indx_cpoles,[0]))),
888 return(ans)
889 )
890 else(
891 sl_indx:sublist_indices(indx_cpoles,lambda([e],is(e<=NN))),
892 cpolessumpos:lsum(cpoles[i][2]*exp(%i*cpoles[i][1]*%pi*var/p),i,sl_indx),
893 cpolessumneg:lsum(conjugate(cpoles[i][2])*exp(-%i*cpoles[i][1]*%pi*var/p),i,sl_indx),
894 laux:listify(setdifference(setify(makelist(i,i,1,NN)),setify(indx_cpoles))),
895 ans:c0+cpolessumpos+cpolessumneg+lsum(at(cn*exp(%i*n*%pi*var/p),n=i),i,laux)
896 +lsum(at(conjugate(cn)*exp(-%i*n*%pi*var/p),n=i),i,laux),
897 return(ratsimp(demoivre(ans)))
898 )
899 )
900 )$
901
902 cfourier_series(expr,var,p,NN):=cfouriercoeff_expand(cfouriercoeff(expr,var,p),var,p,NN)$
903
904 evalpole_scn(bfn,polesf,indx_pf,indx):=block([n],
905 if member(indx,indx_pf) then return(assoc(indx,polesf))
906 else return(at(bfn,n=indx))
907 )$
908
909 fourier_heatcoeff(Qexpr,fexpr,var,p,kap):=block(
910 [bnf,bnQ,polesf,polesQ,indxpf,indxpQ,indxp,n,un,polesU,ans],
911 /* [[bnf],polesf]:fousincoeff(fexpr,var,p),
912 [[bnQ],polesQ]:fousincoeff(Qexpr,var,p), */
913 [[bnf],polesf]:fouriersincoeff(fexpr,var,p),
914 [[bnQ],polesQ]:fouriersincoeff(Qexpr,var,p),
915 declare(n,integer),
916 un:bnf*exp(-(n*%pi/p)^2*kap*t)+integrate(at(bnQ,t=s)*exp(-(n*%pi/p)^2*kap*(t-s)),s,0,t),
917 if emptyp(polesf) and emptyp(polesQ) then (ans:[[un],[]], remove(n,integer), return(ratsimp(ans)))
918 /*now let's deal with the poles*/
919 else(
920 indxpf:map(first,polesf), indxpQ:map(first,polesQ),
921 indxp:unique(append(indxpf,indxpQ)),
922 polesU:map(lambda([e],[e,evalpole_scn(bnf,polesf,indxpf,e)*exp(-(e*%pi/p)^2*kap*t)
923 +integrate(at(evalpole_scn(bnQ,polesQ,indxpQ,e),t=s)*exp(-(e*%pi/p)^2*kap*(t-s)),s,0,t)]),indxp),
924 ans:[[un],polesU],
925 remove(n,integer),
926 return(ratsimp(ans))
927 )
928 )$
929
930 fourier_heat(Qexpr,fexpr,var,p,kap,NN):=fouriersincoeff_expand(fourier_heatcoeff(Qexpr,fexpr,var,p,kap),var,p,NN)$
931
932 fourier_wavecoeff(Qexpr,fexpr,gexpr,var,p,A):=block(
933 [indxp,indxpf,indxpQ,indxpg,n,polesf,polesg,polesQ,bnf,bnQ,bng,Ln,un,polesU],
934 Ln:(n*%pi*A)/p,
935 /* [[bnf],polesf]:fousincoeff(fexpr,var,p),
936 [[bng],polesg]:fousincoeff(gexpr,var,p),
937 [[bnQ],polesQ]:fousincoeff(Qexpr,var,p), */
938 [[bnf],polesf]:fouriersincoeff(fexpr,var,p),
939 [[bng],polesg]:fouriersincoeff(gexpr,var,p),
940 [[bnQ],polesQ]:fouriersincoeff(Qexpr,var,p),
941 bng:bng/Ln, polesg:map(lambda([e],[e[1],e[2]*at(1/Ln,n=e[1])]),polesg),
942 declare(n,integer),
943 un:bnf*cos(Ln*t)+bng*sin(Ln*t)+(1/Ln)*integrate(at(bnQ,t=s)*sin(Ln*(t-s)),s,0,t),
944 if emptyp(polesf) and emptyp(polesQ) and emptyp(polesg) then (ans:[[un],[]], remove(n,integer), return(ratsimp(ans)))
945 /*now let's deal with the poles*/
946 else(
947 indxpf:map(first,polesf), indxpQ:map(first,polesQ), indxpg:map(first,polesg),
948 indxp:unique(append(indxpf,indxpQ,indxpg)),
949 polesU:map(lambda([e],[e,evalpole_scn(bnf,polesf,indxpf,e)*cos(e*%pi*A*t/p)+
950 evalpole_scn(bng,polesg,indxpg,e)*sin(e*%pi*A*t/p)+
951 (p/(e*%pi*A))*integrate(at(evalpole_scn(bnQ,polesQ,indxpQ,e),t=s)*sin(e*%pi*A*(t-s)/p),s,0,t)]),indxp),
952 ans:[[un],polesU],
953 remove(n,integer),
954 return(ratsimp(ans))
955 )
956 )$
957
958 fourier_wave(Qexpr,fexpr,gexpr,var,p,A,NN):=fouriersincoeff_expand(fourier_wavecoeff(Qexpr,fexpr,gexpr,var,p,A),var,p,NN)$
959
960 dirichlet_heat(Qexpr,fexpr,h1expr,h2expr,var,p,kap,NN):=block(
961 [Qaux,faux,vaux],
962 vaux:(h1expr-h2expr)*var/p-h1expr,
963 Qaux:Qexpr+diff(vaux,t),
964 faux:fexpr+at(vaux,t=0),
965 fourier_heat(Qaux,faux,var,p,kap,NN)-vaux
966 )$
967
968 dirichlet_wave(Qexpr,fexpr,gexpr,h1expr,h2expr,var,p,A,NN):=block(
969 [Qaux,faux,gaux,vaux],
970 vaux:(h1expr-h2expr)*var/p-h1expr,
971 Qaux:Qexpr+diff(vaux,t,2),
972 faux:fexpr+at(vaux,t=0),
973 gaux:gexpr+at(diff(vaux,t),t=0),
974 fourier_wave(Qaux,faux,gaux,var,p,A,NN)-vaux
975 )$
976
977 dirichlet_laplace_disk(a,fexpr,var,NN):=block(
978 [cn,c0,cpoles,n],
979 [[c0,cn],cpoles]:cfouriercoeff(fexpr,var,%pi),
980 cn:(r/a)^abs(n)*cn,
981 cpoles:map(lambda([e],[e[1],(r/a)^e[1]*e[2]]),cpoles),
982 cfouriercoeff_expand([[c0,cn],cpoles],var,%pi,NN)
983 )$
984
985 neumann_laplace_disk(a,fexpr,var,NN):=block([a0,an,bn,poles,n],
986 [[a0,an,bn],poles]:fouriercoeff(fexpr,var,%pi),
987 if not is(a0=0) then return("The solution does not exist.")
988 else(
989 an:(a/n)*an*(r/a)^n,
990 bn:(a/n)*bn*(r/a)^n,
991 poles:map(lambda([e],[e[1],e[2]*(a/e[1])*(r/a)^e[1],e[3]*(a/e[1])*(r/a)^e[1]]),poles),
992 return(fouriercoeff_expand([[a0,an,bn],poles],var,%pi,NN))
993 )
994 )$
995
996 dirichlet_laplace_annulus(a,b,fexpr,gexpr,var,NN):=block(
997 [n,cnf,c0f,cng,c0g,cpolesf,cpolesg,c0aux,cnaux,cpolesaux,
998 indxpf,indxpg,indxp,ans],
999 [[c0f,cnf],cpolesf]:cfouriercoeff(fexpr,var,%pi),
1000 [[c0g,cng],cpolesg]:cfouriercoeff(gexpr,var,%pi),
1001 c0aux:(c0g*log(b)-c0f*log(a))*log(b/a)+(c0f-c0g)*log(r)/log(b/a),
1002 cnaux:(b^abs(n)*r^abs(n)-a^abs(2*n)*b^abs(n)*r^(-abs(n)))*cnf/(b^abs(2*n)-a^abs(2*n))+
1003 cng*(b^abs(2*n)*a^abs(n)*r^(-abs(n))-a^abs(n)*r^abs(n))/(b^abs(2*n)-a^abs(2*n)),
1004 if emptyp(cpolesf) and emptyp(cpolesg) then(
1005 ans:cfouriercoeff_expand(ratsimp([[c0aux,cnaux],[]]),var,%pi,NN),
1006 return(ans)
1007 ) /*now let's deal with the poles*/
1008 else(
1009 indxpf:map(first,cpolesf), indxpg:map(first,cpolesg),
1010 indxp:unique(append(indxpf,indxpg)),
1011 cpolesaux:map(lambda([e],[e,(b^e*r^e-a^(2*e)*b^e*r^(-e))*evalpole_scn(cnf,cpolesf,indxpf,e)/(b^(2*e)-a^(2*e))+
1012 evalpole_scn(cng,cpolesg,indxpg,e)*(b^(2*e)*a^e*r^(-e)-a^e*r^e)/(b^(2*e)-a^(2*e))]),indxp),
1013 ans:cfouriercoeff_expand(ratsimp([[c0aux,cnaux],cpolesaux]),var,%pi,NN),
1014 return(ans)
1015 )
1016 )$
1017
1018 dirichlet_laplace_wedge(R,a,fexpr,var,NN):=block(
1019 [bnf,polesf,bnaux,polesaux,n,p:a],
1020 [[bnf],polesf]:fouriersincoeff(fexpr,var,p),
1021 bnaux:bnf*(r/R)^(n*%pi/a),
1022 if emptyp(polesf) then return(fouriersincoeff_expand([[bnaux],[]],var,p,NN))
1023 else(
1024 polesaux:map(lambda([e],[e[1],e[2]*(r/R)^(e[1]*%pi/a)]),polesf),
1025 return(fouriersincoeff_expand([[bnaux],polesaux],var,p,NN))
1026 )
1027 )$
1028
1029 neumann_laplace_wedge(R,a,fexpr,var,NN):=block(
1030 [bnf,polesf,bnaux,polesaux,n,p:a],
1031 [[bnf],polesf]:fouriersincoeff(fexpr,var,p),
1032 bnaux:bnf*(r/R)^(n*%pi/a)*(R*a/(n*%pi)),
1033 if emptyp(polesf) then return(fouriersincoeff_expand([[bnaux],[]],var,p,NN))
1034 else(
1035 polesaux:map(lambda([e],[e[1],e[2]*(r/R)^(e[1]*%pi/a)*(R*a/(e[1]*%pi))]),polesf),
1036 return(fouriersincoeff_expand([[bnaux],polesaux],var,p,NN))
1037 )
1038 )$
1039
1040 dirichlet_laplace_rectangle(a,b,f1expr,f2expr,g1expr,g2expr,var1,var2,NN):=block(
1041 [n,f1n,polesf1,f2n,polesf2,g1n,polesg1,g2n,polesg2,indxpf1,indxpf2,indxpg1,indxpg2,
1042 indxp,Fnaux,Gnaux,polesFaux,polesGaux,ans],
1043 [[f1n],polesf1]:fouriersincoeff(f1expr,var1,a),
1044 [[f2n],polesf2]:fouriersincoeff(f2expr,var1,a),
1045 [[g1n],polesg1]:fouriersincoeff(g1expr,var2,b),
1046 [[g2n],polesg2]:fouriersincoeff(g2expr,var2,b),
1047 Fnaux:f1n*sinh(n*%pi*(b-var2)/a)/sinh(n*%pi*b/a)+f2n*sinh(n*%pi*var2/a)/sinh(n*%pi*b/a),
1048 Gnaux:g1n*sinh(n*%pi*(a-var1)/b)/sinh(n*%pi*a/b)+g2n*sinh(n*%pi*var1/b)/sinh(n*%pi*a/b),
1049 if emptyp(polesf1) and emptyp(polesf2) and emptyp(polesg1) and emptyp(polesg2) then(
1050 ans:fouriersincoeff_expand([[Fnaux],[]],var1,a,NN)+fouriersincoeff_expand([[Gnaux],[]],var2,b,NN),
1051 return(ans)
1052 )
1053 else(
1054 indxpf1:map(first,polesf1), indxpf2:map(first,polesf2),
1055 indxpg1:map(first,polesg1), indxpg2:map(first,polesg2),
1056 indxp:unique(append(indxpf1,indxpf2,indxpg1,indxpg2)),
1057 polesFaux:map(lambda([e],[e,evalpole_scn(f1n,polesf1,indxpf1,e)*sinh(e*%pi*(b-var2)/a)/sinh(e*%pi*b/a)
1058 +evalpole_scn(f2n,polesf2,indxpf2,e)*sinh(e*%pi*var2/a)/sinh(e*%pi*b/a)]),indxp),
1059 polesGaux:map(lambda([e],[e,evalpole_scn(g1n,polesg1,indxpg1,e)*sinh(e*%pi*(a-var1)/b)/sinh(e*%pi*a/b)
1060 +evalpole_scn(g2n,polesg2,indxpg2,e)*sinh(e*%pi*var1/b)/sinh(e*%pi*a/b)]),indxp),
1061 ans:fouriersincoeff_expand([[Fnaux],polesFaux],var1,a,NN)+fouriersincoeff_expand([[Gnaux],polesGaux],var2,b,NN),
1062 return(ans)
1063 )
1064 )$
1065
1066 neumann_laplace_rectangle(a,b,f1expr,f2expr,g1expr,g2expr,var1,var2,NN):=block(
1067 [n,f1n,polesf1,f2n,polesf2,g1n,polesg1,g2n,polesg2,indxpf1,indxpf2,indxpg1,indxpg2,
1068 indxp,Fnaux,Gnaux,polesFaux,polesGaux,C,ans,f10,g10,f20,g20],
1069 [[f10,f1n],polesf1]:fouriercoscoeff(f1expr,var1,a),
1070 [[f20,f2n],polesf2]:fouriercoscoeff(f2expr,var1,a),
1071 [[g10,g1n],polesg1]:fouriercoscoeff(g1expr,var2,b),
1072 [[g20,g2n],polesg2]:fouriercoscoeff(g2expr,var2,b),
1073 Fnaux:(-a/(n*%pi))*f1n*cosh(n*%pi*(b-var2)/a)/sinh(n*%pi*b/a)+(a/(n*%pi))*f2n*cosh(n*%pi*var2/a)/sinh(n*%pi*b/a),
1074 Gnaux:(-b/(n*%pi))*g1n*cosh(n*%pi*(a-var1)/b)/sinh(n*%pi*a/b)+(b/(n*%pi))*g2n*cosh(n*%pi*var1/b)/sinh(n*%pi*a/b),
1075 if emptyp(polesf1) and emptyp(polesf2) and emptyp(polesg1) and emptyp(polesg2) then(
1076 ans:fouriercoscoeff_expand([[0,Fnaux],[]],var1,a,NN)+fouriercoscoeff_expand([[0,Gnaux],[]],var2,b,NN),
1077 return(ans+C)
1078 )
1079 else(
1080 indxpf1:map(first,polesf1), indxpf2:map(first,polesf2),
1081 indxpg1:map(first,polesg1), indxpg2:map(first,polesg2),
1082 indxp:unique(append(indxpf1,indxpf2,indxpg1,indxpg2)),
1083 polesFaux:map(lambda([e],[e,(-a/(n*%pi))*evalpole_scn(f1n,polesf1,indxpf1,e)*cosh(e*%pi*(b-var2)/a)/sinh(e*%pi*b/a)
1084 +(a/(n*%pi))*evalpole_scn(f2n,polesf2,indxpf2,e)*cosh(e*%pi*var2/a)/sinh(e*%pi*b/a)]),indxp),
1085 polesGaux:map(lambda([e],[e,(-b/(n*%pi))*evalpole_scn(g1n,polesg1,indxpg1,e)*cosh(e*%pi*(a-var1)/b)/sinh(e*%pi*a/b)
1086 +(b/(n*%pi))*evalpole_scn(g2n,polesg2,indxpg2,e)*cosh(e*%pi*var1/b)/sinh(e*%pi*a/b)]),indxp),
1087 ans:fouriercoscoeff_expand([[0,Fnaux],polesFaux],var1,a,NN)+fouriercoscoeff_expand([[0,Gnaux],polesGaux],var2,b,NN),
1088 return(ans+C)
1089 )
1090 )$
1091
1092 /*Beggining of 2D Wave equation*/
1093
1094 BesselJZeros(m,k,[u]):=block([a,alpha,A,answ],local(a,alpha,A),
1095 if is(not(m>-1)) then return("The order m must be m>-1"),
1096 if (is(not(integerp(k))) or is(not(k > 0))) then return("k must be an integer k>0"),
1097 a[i]:=m+2*i,
1098 alpha[i,j]:=if is(equal(i,j)) then 2/((a[i]-1)*(a[i]+1))
1099 elseif is(equal(j,i-1)) then 1/((a[i]-1)*sqrt((a[i]-2)*a[i]))
1100 elseif is(equal(j,i+1)) then 1/((a[i+1]-1)*sqrt((a[i+1]-2)*a[i+1]))
1101 else 0,
1102 A:genmatrix(alpha,ceiling(k/0.64)+10,ceiling(k/0.64)+10),
1103 answ:2/sqrt(first(eigens_by_jacobi(A,floatfield))),
1104 if (is(not(equal(length(u),0))) and is(equal(first(u),all))) then
1105 firstn(sort(answ),k)
1106 elseif (is(not(equal(length(u),0))) and is(not(equal(first(u),all)))) then
1107 return("Do you want the first k roots? In this case, the option is 'all'")
1108 else
1109 last(firstn(sort(answ),k))
1110 )$
1111
1112 simpgen(l1,l2):=append(endcons(last(l1)+first(l2),rest(l1,-1)),rest(l2,1))$
1113
1114 pairing(L1,L2):=xreduce(append,map(lambda([x],map(lambda([y],[x,y]),L2)),L1))$
1115
1116 nintegrate(f,m,[I]):=block([n:length(args(lhs(apply(fundef,[f])))),tmp:xreduce(simpgen,makelist([1,4,1],j,1,m/2)),x,deltax,p,w,ww,xx],local(x,deltax,p,w),
1117 if is(not(evenp(m))) then return ("The number of subdivisions m must be even")
1118 elseif is(not(equal(length(I),n))) then return("The number of variables xi and intervals [ai,bi] must match") else
1119 (
1120 for i:1 thru n do x[i]:args(lhs(apply(fundef,[f])))[i],
1121 for i:1 thru n do deltax[i]:(I[i][2]-I[i][1])/m,
1122 for j:1 thru n do p[j]:makelist(I[j][1]+k*deltax[j],k,0,m),
1123 for j:1 thru n do w[j]:(deltax[j]/3)*tmp,
1124 if is(equal(n,1)) then
1125 ww:w[1]
1126 else
1127 ww:map(lambda([x],apply("*",x)),map(flatten,xreduce(pairing,makelist(w[j],j,1,n)))),
1128 if is(equal(n,1)) then
1129 xx:p[1]
1130 else
1131 xx:map(flatten,xreduce(pairing,makelist(p[j],j,1,n))),
1132 if is(equal(n,1)) then
1133 bfloat(apply("+",ww*map(f,xx)))
1134 else
1135 bfloat(apply("+",ww*map(lambda([x],apply(f,x)),xx)))
1136 )
1137 )$
1138
1139 a0(n,a,f):=block([r:args(lhs(apply(fundef,[f])))[1],theta:args(lhs(apply(fundef,[f])))[2],lam:BesselJZeros(0,n)/a,p:%pi,ff,k],local(ff),
1140 define(funmake(ff,[r,theta]),apply(f,[r,theta])*r*bessel_j(0,lam*r)),
1141 k:1/(p*a^2*(bessel_j(1,lam))^2),
1142 k*nintegrate(ff,14,[0,a],[0,2*p])
1143 )$
1144
1145 a(m,n,a,f):=block([r:args(lhs(apply(fundef,[f])))[1],theta:args(lhs(apply(fundef,[f])))[2],lam:BesselJZeros(0,n)/a,p:%pi,ff,k],local(ff),
1146 define(funmake(ff,[r,theta]),apply(f,[r,theta])*r*bessel_j(0,lam*r)*cos(m*theta)),
1147 k:2/(p*a^2*(bessel_j(m+1,lam))^2),
1148 k*nintegrate(ff,14,[0,a],[0,2*p])
1149 )$
1150
1151 b(m,n,a,f):=block(
1152 [r:args(lhs(apply(fundef,[f])))[1],theta:args(lhs(apply(fundef,[f])))[2],lam:BesselJZeros(0,n)/a,p:%pi,ff,k],local(ff),
1153 define(funmake(ff,[r,theta]),apply(f,[r,theta])*r*bessel_j(0,lam*r)*sin(m*theta)),
1154 k:2/(p*a^2*(bessel_j(m+1,lam))^2),
1155 k*nintegrate(ff,14,[0,a],[0,2*p])
1156 )$
1157
1158 as0(n,a,c,f):=block([r:args(lhs(apply(fundef,[f])))[1],theta:args(lhs(apply(fundef,[f])))[2],lam:BesselJZeros(0,n)/a,p:%pi,ff,k],local(ff),
1159 define(funmake(ff,[r,theta]),apply(f,[r,theta])*r*bessel_j(0,lam*r)),
1160 k:1/(c*p*lam*a^2*(bessel_j(1,lam))^2),
1161 k*nintegrate(ff,14,[0,a],[0,2*p])
1162 )$
1163
1164 as(m,n,a,c,f):=block([r:args(lhs(apply(fundef,[f])))[1],theta:args(lhs(apply(fundef,[f])))[2],lam:BesselJZeros(0,n)/a,p:%pi,ff,k],local(ff),
1165 define(funmake(ff,[r,theta]),apply(f,[r,theta])*r*bessel_j(0,lam*r)*cos(m*theta)),
1166 k:2/(c*p*lam*a^2*(bessel_j(m+1,lam))^2),
1167 k*nintegrate(ff,14,[0,a],[0,2*p])
1168 )$
1169
1170 bs(m,n,a,c,f):=block([r:args(lhs(apply(fundef,[f])))[1],theta:args(lhs(apply(fundef,[f])))[2],lam:BesselJZeros(0,n)/a,p:%pi,ff,k],local(ff),
1171 define(funmake(ff,[r,theta]),apply(f,[r,theta])*r*bessel_j(0,lam*r)*sin(m*theta)),
1172 k:2/(c*p*lam*a^2*(bessel_j(m+1,lam))^2),
1173 k*nintegrate(ff,14,[0,a],[0,2*p])
1174 )$
1175
1176 wave2d_disk(c,a,f,g,k,l):=block([A0,A,B,As0,As,Bs,lamb],local(A0,A,B,As0,As,Bs,lamb,t),
1177 A0[i]:=a0(i,a,f),A[i,j]:=a(i,j,a,f),B[i,j]:=b(i,j,a,f),As0[i]:=as0(i,a,c,g),As[i,j]:=as(i,j,a,c,g),Bs[i,j]:=bs(i,j,a,c,g),
1178 lamb[i,j]:=BesselJZeros(i,j)/a,
1179 sum(A0[n]*bessel_j(0,lamb[0,n]*r)*cos(c*lamb[0,n]*t),n,1,l)
1180 +sum(sum(bessel_j(m,lamb[m,n]*r)*(A[m,n]*cos(m*theta)+B[m,n]*sin(m*theta))*cos(c*lamb[m,n]*t),n,1,l),m,1,k)
1181 +sum(As0[n]*bessel_j(0,lamb[0,n]*r)*sin(c*lamb[0,n]*t),n,1,l)
1182 +sum(sum(bessel_j(m,lamb[m,n]*r)*(As[m,n]*cos(m*theta)+Bs[m,n]*sin(m*theta))*sin(c*lamb[m,n]*t),n,1,l),m,1,k)
1183 )$
1184
1185 Arect(f,a,b,m,n):=block([x:args(lhs(apply(fundef,[f])))[1],y:args(lhs(apply(fundef,[f])))[2]],
1186 define(funmake(ff,[x,y]),apply(f,[x,y])*sin(m*%pi*x/a)*sin(m*%pi*y/b)),
1187 4*(nintegrate(ff,14,[0,a],[0,b]))/(a*b)
1188 )$
1189
1190 Brect(g,a,b,c,m,n):=block([x:args(lhs(apply(fundef,[f])))[1],y:args(lhs(apply(fundef,[f])))[2],lamb],local(lamb),
1191 lamb[m,n]:=((m/a)^2+(n/b)^2)/(%pi^2),
1192 define(funmake(gg,[x,y]),apply(g,[x,y])*sin(m*%pi*x/a)*sin(m*%pi*y/b)),
1193 4*(nintegrate(gg,14,[0,a],[0,b]))/(a*b*c*sqrt(lam[m,n]))
1194 )$
1195
1196 wave2d_rectangle(c,a,b,f,g,k,l):=block([x:args(lhs(apply(fundef,[f])))[1],y:args(lhs(apply(fundef,[f])))[2],lamb,t],local(lamb,t),
1197 lamb[m,n]:=((m/a)^2+(n/b)^2)/(%pi^2),
1198 sum(sum((Arect(f,a,b,m,n)*cos(sqrt(lamb[0,n])*c*t)+Brect(g,a,b,c,m,n)*sin(sqrt(lamb[0,n])*c*t))*sin(m*%pi*x/a)*sin(n*%pi*y/b),m,1,k),n,1,l)
1199 )$