rtoy / maxima

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

plot2d not ignoring errors within functions #4201

Closed rtoy closed 4 months ago

rtoy commented 4 months ago

Imported from SourceForge on 2024-07-09 19:42:39 Created by macrakis on 2021-10-26 18:50:53 Original: https://sourceforge.net/p/maxima/bugs/3881


plot2d correctly ignores isolated numeric evaluation errors when the first argument is an expression, but when it is a function, the error stops evaluation and no plot is made.

ff(x):=gamma(x)$
plot2d(ff,[x,-1,1]) => Maxima encountered a Lisp error: gamma: gamma(-1.0) is undefined. (no plot)

plot2d(lambda([x],gamma(x)),[x,-1,1]) => Maxima encountered a Lisp error: gamma: gamma(-1.0) is undefined. (no plot)

but

plot2d(gamma(x),[x,-1,1]) => plot2d: expression evaluates to non-numeric value somewhere in plotting range. (but makes plot with the good points)

All three should function the same.

Strangely, if the function is 1/x, it works fine.

Maxima 5.45.1 / SBCL 2.0.0 / Windows 10

Also fails using the current version of plot.lisp on sourceforge (https://sourceforge.net/p/maxima/code/ci/1e9aaad02f86b4acce3cbd375a65499440ffabda/tree/src/plot.lisp)

rtoy commented 4 months ago

Imported from SourceForge on 2024-07-09 19:42:41 Created by macrakis on 2021-10-26 18:51:58 Original: https://sourceforge.net/p/maxima/bugs/3881/#a86f


rtoy commented 4 months ago

Imported from SourceForge on 2024-07-09 19:42:44 Created by macrakis on 2021-10-26 18:54:13 Original: https://sourceforge.net/p/maxima/bugs/3881/#1bb4


Diff:


--- old
+++ new
@@ -12,6 +12,8 @@

 All three should function the same.

+Strangely, if the function is 1/x, it works fine.
+
 Maxima 5.45.1 / SBCL 2.0.0 / Windows 10

 Also fails using the current version of plot.lisp on sourceforge (https://sourceforge.net/p/maxima/code/ci/1e9aaad02f86b4acce3cbd375a65499440ffabda/tree/src/plot.lisp)
rtoy commented 4 months ago

Imported from SourceForge on 2024-07-09 19:42:48 Created by villate on 2022-02-08 16:12:52 Original: https://sourceforge.net/p/maxima/bugs/3881/#6d43


This might have to do with the gamma function. Observe that the following 3 plot commands give the same result:

gg(x):=1/x+1/(x+1)$
plot2d(gg,[x,-1,1])$
plot2d(1/x+1/(x+1),[x,-1,1])$
plot2d(lambda([x],1/x+1/(x+1)),[x,-1,1])$

Notice also the following behavior of coerce-float-fun:

(funcall (coerce-float-fun #$gamma(x)$ #$[x]$) -1)
            -2.5653050788007548e16
(funcall (coerce-float-fun #$gamma(x)$ #$[x]$) -1.0)
             gamma: gamma(-1.0) is undefined.
rtoy commented 4 months ago

Imported from SourceForge on 2024-07-09 19:42:51 Created by robert_dodier on 2022-03-02 07:46:12 Original: https://sourceforge.net/p/maxima/bugs/3881/#4d03


I don't think this behavior is specific to gamma. There is some logic in %COERCE-FLOAT-FUN to catch errors in the constructed function, but it doesn't catch errors generated by MERROR, which is how errors are triggered in many places in Maxima (and in SIMPGAMMA in particular).

I tried tinkering with it to try to get it to catch the error thrown by MERROR but couldn't get it to work. I'm probably overlooking something simple.

rtoy commented 4 months ago

Imported from SourceForge on 2024-07-09 19:42:55 Created by kjak on 2022-03-09 20:32:11 Original: https://sourceforge.net/p/maxima/bugs/3881/#c739


I didn't get any response about my patch, so I'll commit something like it tomorrow if there are no objections.

One possible change -- which I'll include if I don't hear anything otherwise -- is binding errormsg to false around the ERRCATCH forms. My posted patch does not do this, and it causes the error messages to be printed before the "plot2d: expression evaluates to non-numeric value somewhere in plotting range." message.

Currently (without my patch) error messages aren't printed, so we have this:

(%i1) plot2d(lambda([x],log(abs(x))),[x,-1,1])$
plot2d: expression evaluates to non-numeric value somewhere in plotting range.

(%i2) plot2d(lambda([x],1/x),[x,-1,1])$
plot2d: expression evaluates to non-numeric value somewhere in plotting range.

But I actually prefer having the error messages printed, so something like this:

(%i4) plot2d(lambda([x],1/x),[x,-1,1])$
expt: undefined: 0 to a negative exponent.
plot2d: expression evaluates to non-numeric value somewhere in plotting range.

(%i5) plot2d(lambda([x],log(abs(x))),[x,-1,1])$
log: encountered log(0).
plot2d: expression evaluates to non-numeric value somewhere in plotting range.

(%i6) plot2d(lambda([x],gamma(x)),[x,-1,1])$
gamma: gamma(-1.0) is undefined.
gamma: gamma(0.0) is undefined.
plot2d: expression evaluates to non-numeric value somewhere in plotting range.

Presumably we want to keep the current behavior though.

rtoy commented 4 months ago

Imported from SourceForge on 2024-07-09 19:42:58 Created by peterpall on 2022-03-09 21:46:07 Original: https://sourceforge.net/p/maxima/bugs/3881/#aba6


Without the error messages it sometimes is hard to find out what went wrong causing the non-numeric values.

rtoy commented 4 months ago

Imported from SourceForge on 2024-07-09 19:43:02 Created by kjak on 2022-03-09 21:55:25 Original: https://sourceforge.net/p/maxima/bugs/3881/#aba6/495b


Yes, that's one of the reasons why I prefer to print the error messages.

rtoy commented 4 months ago

Imported from SourceForge on 2024-07-09 19:43:06 Created by villate on 2022-03-10 08:19:48 Original: https://sourceforge.net/p/maxima/bugs/3881/#d3c5


Kris: please go ahead and commit your patch. I actually prefer those extra error messages.