rtoy / maxima

A Clone of Maxima's repo
Other
0 stars 0 forks source link

fourie package fail for square wave coefficients #2707

Open rtoy opened 1 week ago

rtoy commented 1 week ago

Imported from SourceForge on 2024-07-06 18:12:55 Created by qm-eq-gr on 2024-04-10 12:13:25 Original: https://sourceforge.net/p/maxima/bugs/4286


I'm resubmitting this bug because I noticed that an earlier version of Maxima (5.45.1) works, but the current git version has the bug. Also, my first attempt to submit the bug report did not show up. I think it should have been 4272, but that number is missing in the bug list.

(Note: setup_autoload("abs_integrate.mac",unit_step) is in my maxima-init)

This works fine:

Maxima 5.45.1 https://maxima.sourceforge.io
using Lisp SBCL 2.3.6-2.fc39
(%i1) f(x):=2*unit_step(x)-1;
(%o1)                     f(x) := 2 unit_step(x) - 1
(%i2) load(fourie);
(%o2)         /usr/share/maxima/5.45.1/share/calculus/fourie.mac
(%i7) fourier(f(x),x,1);
(%t7) a[0] = 0
(%t8) a[n] = 0
(%t9) b[n] = 2/(%pi*n)-(2*cos(%pi*n))/(%pi*n)

Current git does not:

Maxima branch_5_47_base_1083_ga2775dcfb https://maxima.sourceforge.io
using Lisp SBCL 2.4.3
This shows the bug:
(%i15) fourier(f(x),x,1);
(%t15) a[0] = 0
(%t16) a[n] = -(('limit(signum(x)*sin(%pi*n*x),x,-1,plus))/(%pi*n))
            +('limit(signum(x)*sin(%pi*n*x),x,1,minus))/(%pi*n)
(%t17) b[n] = -('limit(1/(%pi*n)+signum(x)/(%pi*n)
                                -(signum(x)*cos(%pi*n*x))/(%pi*n),x,-1,plus))
            +'limit(1/(%pi*n)+signum(x)/(%pi*n)
                             -(signum(x)*cos(%pi*n*x))/(%pi*n),x,1,minus)
rtoy commented 1 week ago

Imported from SourceForge on 2024-07-06 18:12:56 Created by qm-eq-gr on 2024-04-10 14:45:17 Original: https://sourceforge.net/p/maxima/bugs/4286/#dd3a


I built 5.46.0 and it fails there, so some change between 5.45.1 and 5.46.0 must have introduced an error. It fails differently for 5.46.0 and git master.

Maxima 5.46.0 https://maxima.sourceforge.io
using Lisp SBCL 2.4.3
(%i2) f(x):=2*unit_step(x)-1 $
(%i3) load(fourie);
(%o3) "/home/packages/SOURCE/maxima-code/sbcl_install_5.46.0/share/maxima/5.46.0/share/calculus/fourie.mac"
(%i4) fourier(f(x),x,1);
(%t4) a[0] = 0
(%t5) a[n] = -(2*sin(%pi*n))/(%pi*n)
(%t6) b[n] = 0
rtoy commented 1 week ago

Imported from SourceForge on 2024-07-06 18:13:00 Created by robert_dodier on 2024-04-11 06:10:27 Original: https://sourceforge.net/p/maxima/bugs/4286/#9c0b


rtoy commented 1 week ago

Imported from SourceForge on 2024-07-06 18:13:03 Created by robert_dodier on 2024-04-11 06:10:27 Original: https://sourceforge.net/p/maxima/bugs/4286/#749c


Thanks for the bug report. I notice that for the current Git version, ev(%t16, nouns) and ev(%t17, nouns) yield the same things as a[n] and b[n] in 5.45.1, so it seems like the results are constructed correctly, but for some reason the limits were not actually calculated.

I believe the incorrect results in 5.46.0 are due to limit bugs which have been fixed recently.

rtoy commented 1 week ago

Imported from SourceForge on 2024-07-06 18:13:07 Created by qm-eq-gr on 2024-04-11 14:44:28 Original: https://sourceforge.net/p/maxima/bugs/4286/#6d3e


Thank you. I didn't know to do that. Please mark this as closed.

rtoy commented 1 week ago

Imported from SourceForge on 2024-07-06 18:13:10 Created by robert_dodier on 2024-04-14 19:20:14 Original: https://sourceforge.net/p/maxima/bugs/4286/#7c67


Eric, I think the various behaviors observed are due to changes in limit and maybe ldefint in different versions of Maxima. The simplest related example which I can find is the following. I am working with the current version (5a08d75).

By the way, Maxima has a built-in signum function so I'll work with that instead of 2*unit_step(x) - 1.

(%i4) ldefint(signum(x)*sin(x),x,0,%pi);
                  ⌠                               ⌠
                  ⎮                               ⎮
(%o4)   limit     ⎮ signum(x) sin(x) dx - limit   ⎮ signum(x) sin(x) dx
        x -> %pi- ⎮                       x -> 0+ ⎮
                  ⌡                               ⌡

Verbifying the limit and integrate noun expressions doesn't change the result:

(%i5) ev (%, nouns);
                  ⌠                               ⌠
                  ⎮                               ⎮
(%o5)   limit     ⎮ signum(x) sin(x) dx - limit   ⎮ signum(x) sin(x) dx
        x -> %pi- ⎮                       x -> 0+ ⎮
                  ⌡                               ⌡

However, after loading the share package abs_integrate, I see that Maxima can figure out a result for those.

(%i6) load (abs_integrate) $

(%i7) ev (%o4, nouns);
(%o7)                                  2

Going back to the Fourier decomposition. Bear in mind that abs_integrate is loaded already.

(%i11) load (fourie) $

(%i12) fourier (signum(x), x, 1);
(%t12)                               a  = 0
                                      0

(%t13)                               a  = 0
                                      n

                        2 signum(x)   2 signum(x) cos(%pi n x)
               limit   (─────────── - ────────────────────────)
               x -> 1-     %pi n               %pi n
(%t14) b  = 2 (────────────────────────────────────────────────
        n                             2
                                        2 signum(x)   2 signum(x) cos(%pi n x)
                               limit   (─────────── - ────────────────────────)
                               x -> 0+     %pi n               %pi n
                             - ────────────────────────────────────────────────)
                                                      2

(%o14)                         [%t12, %t13, %t14]

As in the preceding example with just signum, Maxima needs an extra nudge to complete the calculation.

(%i15) ev (%t14, nouns);
                                  2     2 cos(%pi n)
(%o15)                     b  = ───── - ────────────
                            n   %pi n      %pi n

I think the current behavior of Maxima is not incorrect, although it's less useful when it returns stuff that needs further evaluation. I guess it could be a bug that Maxima lost the ability to calculate some limits along the way from 5.45 onward to the present. I will look at changes in the behavior of limit and see what the changes are, and whether they amount to bugs. I will leave this open while I look at that and then close this one, and maybe open another ticket for anything I find.

rtoy commented 1 week ago

Imported from SourceForge on 2024-07-06 18:13:14 Created by robert_dodier on 2024-04-14 19:20:43 Original: https://sourceforge.net/p/maxima/bugs/4286/#59d3


rtoy commented 1 week ago

Imported from SourceForge on 2024-07-06 18:13:17 Created by robert_dodier on 2024-04-16 05:57:43 Original: https://sourceforge.net/p/maxima/bugs/4286/#14f7


rtoy commented 1 week ago

Imported from SourceForge on 2024-07-06 18:13:21 Created by robert_dodier on 2024-04-16 05:57:44 Original: https://sourceforge.net/p/maxima/bugs/4286/#c880


I looked at the behavior of Maxima 5.45 for the function f(x) := 2*unit_step(x) - 1 and from tracing the limit code, Maxima is not being careful enough with limits of antiderivatives, but the incorrectly handled terms cancel out, so, fortuitously, the result comes out correct. I think the current behavior is more correct, although the presence of noun expressions makes it harder to handle the results.

I'll close this ticket. We should open a new ticket if problems are found in fourier, limit, etc.