【问题标题】:side-effects using declare(var,constant) in Maxima在 Maxima 中使用 declare(var,constant) 的副作用
【发布时间】:2016-03-24 14:45:06
【问题描述】:

在最大值中,是否有以下行为?

第一个例子:

(%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);

第二个例子:

(%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

第三个例子:

(%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  ]

似乎declare(a,constant) 具有全局效应,在我看来,最大值似乎很奇怪。第二个和第三个示例完全符合我的预期。

还有没有类似的情况发生在maxima中?

【问题讨论】:

    标签: maxima


    【解决方案1】:

    Maxima 的范围概念很弱。基本上所有符号都在同一个范围内,所以当你声明 a 时,它是关于 a 的所有实例,甚至是函数参数的实例。

    Maxima 实际上是一个非常古老的程序,这是从未更新过的方面之一。有关于赋予 Maxima 更强的范围概念的讨论,但这需要一些时间。

    【讨论】:

    • 所以每当我加载像linearalgebra.mac 这样的包时,我都必须检查它的源代码中的函数定义中使用的变量吗?还是在进行任何声明之前确保在程序开头加载它就足够了?我经常在没有显式加载包的情况下使用函数,这样有点不方便。
    • 可能最好的做法是在声明之前加载你需要的包。这当然不是很令人满意,因为它只是因为 Maxima 无法区分不同范围内的符号才需要。
    猜你喜欢
    • 1970-01-01
    • 2011-10-12
    • 2016-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-19
    • 1970-01-01
    • 2015-01-16
    相关资源
    最近更新 更多