side-effects using declare(var,constant) in Maxima

半腔热情 提交于 2019-12-11 13:46:12

问题


In maxima, is the following behavior intended?

First example:

(%i1) declare(a,constant);
(%o1)                                done
(%i2) constantp(a);
(%o2)                                true
(%i3) square(a):=a^2;

define: in definition of square, found bad argument a
 -- an error. To debug this try: debugmode(true);
(%i4) load("linearalgebra.mac");

define: in definition of dotproduct, found bad argument a
 -- an error. To debug this try: debugmode(true);

Second example:

(%i1) a:5;
(%o1)                                  5
(%i2) constantp(a);
(%o2)                                true
(%i3) square(a):=a^2;
                                              2
(%o3)                           square(a) := a
(%i4) square(a);
(%o4)                                 25

Third example:

(%i1) declare(a,scalar);
(%o1)                                done
(%i2) mat_f(a,b):=a.b - b.a;
(%o2)                    mat_f(a, b) := a . b - b . a
(%i4) mat_f(matrix([1,2],[3,4]),matrix([3,4],[1,2]));
                                [ - 10  - 14 ]
(%o4)                           [            ]
                                [  6     10  ]

It seems like declare(a,constant) has a global effect which to me seems strange in maxima. The second and third example work exactly how I would expect it.

Also are there any similar cases where something like this happens in maxima?


回答1:


Maxima has a very weak notion of scope. Essentially all symbols are in the same scope, so when you make a declaration about a, it is about all instances of a, even the ones which are function arguments.

Maxima is actually a very old program and this is one of those aspects which has never been updated. There is discussion about giving Maxima a stronger notion of scope, but that will take some time.



来源:https://stackoverflow.com/questions/36202981/side-effects-using-declarevar-constant-in-maxima

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!