Sympy失败了,wxMaxima没有

时间:2016-02-22 07:56:57

标签: python sympy maxima

我试图用wxMaxima和sympy解决以下无限积分:

integrate(r^2*sqrt(R^2-r^2),r)

在千里马,我确实得到了答案但没有同情。我不懂为什么。我是Python的重要用户,并且喜欢在Python中进行符号数学,但由于sympy没有解决这个问题,我仍然坚持使用Maxima。

我做错了还是Maxiam更好? (我也在Mathematica中解决了同样问题)

我在wxMaxima中得到了以下答案:

f:r^2*sqrt(R^2-r^2);
g:integrate(f,r);

给出了这个答案:

g:(R^4*asin(r/abs(R)))/8-(r*(R^2-r^2)^(3/2))/4+(r*R^2*sqrt(R^2-r^2))/8  

它看起来很难看,但忘掉了。这里的观点是,同情不能解决这个问题。试着用这段代码解决同样问题:

import sympy as sy
import math
R,r = sy.symbols('R r')
g = sy.integrate(r**2*(R**2-r**2)**0.5,r)
print g

给出此错误消息:

Traceback (most recent call last):
  File "E:\UD\Software\BendStiffener\calculate-moment\moment-calculation-equations\sympy-test.py", line 4, in <module>
    g = sy.integrate(r**2*(R**2-r**2)**0.5,r)
  File "C:\Python27\lib\site-packages\sympy\utilities\decorator.py", line 35, in threaded_func
    return func(expr, *args, **kwargs)
  File "C:\Python27\lib\site-packages\sympy\integrals\integrals.py", line 1232, in integrate
    risch=risch, manual=manual)
  File "C:\Python27\lib\site-packages\sympy\integrals\integrals.py", line 487, in doit
    conds=conds)
  File "C:\Python27\lib\site-packages\sympy\integrals\integrals.py", line 876, in _eval_integral
    h = meijerint_indefinite(g, x)
  File "C:\Python27\lib\site-packages\sympy\integrals\meijerint.py", line 1596, in meijerint_indefinite
    res = _meijerint_indefinite_1(f.subs(x, x + a), x)
  File "C:\Python27\lib\site-packages\sympy\integrals\meijerint.py", line 1646, in _meijerint_indefinite_1
    r = hyperexpand(r.subs(t, a*x**b))
  File "C:\Python27\lib\site-packages\sympy\simplify\hyperexpand.py", line 2482, in hyperexpand
    return f.replace(hyper, do_replace).replace(meijerg, do_meijer)
  File "C:\Python27\lib\site-packages\sympy\core\basic.py", line 1351, in replace
    rv = bottom_up(self, rec_replace, atoms=True)
  File "C:\Python27\lib\site-packages\sympy\simplify\simplify.py", line 4082, in bottom_up
    rv = F(rv)
  File "C:\Python27\lib\site-packages\sympy\core\basic.py", line 1336, in rec_replace
    new = _value(expr, result)
  File "C:\Python27\lib\site-packages\sympy\core\basic.py", line 1280, in <lambda>
    _value = lambda expr, result: value(*expr.args)
  File "C:\Python27\lib\site-packages\sympy\simplify\hyperexpand.py", line 2479, in do_meijer
    allow_hyper, rewrite=rewrite)
  File "C:\Python27\lib\site-packages\sympy\simplify\hyperexpand.py", line 2375, in _meijergexpand
    t, 1/z0)
  File "C:\Python27\lib\site-packages\sympy\simplify\hyperexpand.py", line 2335, in do_slater
    resid = residue(integrand, s, b_ + r)
  File "C:\Python27\lib\site-packages\sympy\series\residues.py", line 57, in residue
    s = expr.series(x, n=0)
  File "C:\Python27\lib\site-packages\sympy\core\expr.py", line 2435, in series
    rv = self.subs(x, xpos).series(xpos, x0, n, dir, logx=logx)
  File "C:\Python27\lib\site-packages\sympy\core\expr.py", line 2442, in series
    s1 = self._eval_nseries(x, n=n, logx=logx)
  File "C:\Python27\lib\site-packages\sympy\core\mul.py", line 1446, in _eval_nseries
    terms = [t.nseries(x, n=n, logx=logx) for t in self.args]
  File "C:\Python27\lib\site-packages\sympy\core\expr.py", line 2639, in nseries
    return self._eval_nseries(x, n=n, logx=logx)
  File "C:\Python27\lib\site-packages\sympy\functions\special\gamma_functions.py", line 168, in _eval_nseries
    return super(gamma, self)._eval_nseries(x, n, logx)
  File "C:\Python27\lib\site-packages\sympy\core\function.py", line 598, in _eval_nseries
    term = e.subs(x, S.Zero)
  File "C:\Python27\lib\site-packages\sympy\core\basic.py", line 892, in subs
    rv = rv._subs(old, new, **kwargs)
  File "C:\Python27\lib\site-packages\sympy\core\basic.py", line 1006, in _subs
    rv = fallback(self, old, new)
  File "C:\Python27\lib\site-packages\sympy\core\basic.py", line 983, in fallback
    rv = self.func(*args)
  File "C:\Python27\lib\site-packages\sympy\core\function.py", line 382, in __new__
    return result.evalf(mlib.libmpf.prec_to_dps(pr))
  File "C:\Python27\lib\site-packages\sympy\core\evalf.py", line 1317, in evalf
    result = evalf(self, prec + 4, options)
  File "C:\Python27\lib\site-packages\sympy\core\evalf.py", line 1217, in evalf
    re, im = x._eval_evalf(prec).as_real_imag()
  File "C:\Python27\lib\site-packages\sympy\core\function.py", line 486, in _eval_evalf
    v = func(*args)
  File "C:\Python27\lib\site-packages\sympy\mpmath\ctx_mp_python.py", line 993, in f
    return ctx.make_mpf(mpf_f(x._mpf_, prec, rounding))
  File "C:\Python27\lib\site-packages\sympy\mpmath\libmp\gammazeta.py", line 1947, in mpf_gamma
    raise ValueError("gamma function pole")
ValueError: gamma function pole

2 个答案:

答案 0 :(得分:2)

当你略微改写你的等式时,你会得到一个解决方案:

import sympy as sy
import math
R, r = sy.symbols('R r')

g = sy.integrate(r**2 * sy.sqrt((R**2 - r**2)), r)

print g.simplify()

因此,我使用expr**0.5而不是sy.sqrt(expr)。这给了

Piecewise((I*R*(-R**3*acosh(r/R) - R**2*r*sqrt((-R**2 + r**2)/R**2) + 2*r**3*sqrt((-R**2 + r**2)/R**2))/8, Abs(r**2/R**2) > 1), (R*(R**3*asin(r/R) - R**2*r*sqrt(1 - r**2/R**2) + 2*r**3*sqrt(1 - r**2/R**2))/8, True))

这是否与Maxima的结果相同很难分辨,因为sympy为您提供了两个部分的解决方案,这取决于sqrt中的argumnt是大于还是小于0.您可以尝试使用实际界限并检查你是否得到了与Maxima解决方案相同的结果,并且结果的第二部分会给你。

您可以像这样访问解决方案的第二部分:

g.args[1][0]

给你:

 R**4*asin(r/R)/8 - R**3*r/(8*sqrt(1 - r**2/R**2)) + 3*R*r**3/(8*sqrt(1 - r**2/R**2)) - r**5/(4*R*sqrt(1 - r**2/R**2))

您也可以通过以下方式获得简化版本:

 g.args[1][0].simplify()

给你:

R*(R**3*asin(r/R) - R**2*r*sqrt(1 - r**2/R**2) + 2*r**3*sqrt(1 - r**2/R**2))/8

现在看起来与Maxima获得的结果非常相似。

答案 1 :(得分:1)

一般来说,SymPy在有理数上的表现要好于浮点数,尤其是,当这些数字是幂时。

这是因为“好的”封闭式解决方案通常只存在于确切的权力。考虑一下这个

之间的区别
return {
        restrict: 'A',
        require: 'ngModel',
        scope: {
            options: '=',
            model: '=ngModel'
        },
        link: function ($scope, $el, $attrs) {
            scope = $scope;
            $scope.$watch(
                function () {
                    return $el[0].id;
                },
                function (elementId) {
                    $('#' + elementId).on('switch-change', function () {
                        scope.model[this.id] = $(this).find('input').is(':checked');
                        $scope.$apply();
                    });

                    $('#' + elementId)['bootstrapSwitch']();
                    $('#' + elementId).bootstrapSwitch('setOnLabel', _($('#' + elementId).attr('data-on-label')));
                    $('#' + elementId).bootstrapSwitch('setOffLabel', _($('#' + elementId).attr('data-off-label')));
                });
            $scope.$watchCollection('model', function(newval){
                if(typeof newval !== 'undefined' && !$.isEmptyObject(newval)){
                    // do some stuff here
                }
            });
        }
    }

和这个

In [39]: integrate(r**2*sqrt(R**2-r**2), r)
Out[39]:
⎧     4      ⎛r⎞
⎪  ⅈ⋅R ⋅acosh⎜─⎟            3                      3                  5             │ 2│
⎪            ⎝R⎠         ⅈ⋅R ⋅r             3⋅ⅈ⋅R⋅r                ⅈ⋅r              │r │
⎪- ───────────── + ───────────────── - ───────────────── + ───────────────────  for │──│ > 1
⎪        8                 _________           _________             _________      │ 2│
⎪                         ╱       2           ╱       2             ╱       2       │R │
⎪                        ╱       r           ╱       r             ╱       r
⎪                  8⋅   ╱   -1 + ──    8⋅   ╱   -1 + ──    4⋅R⋅   ╱   -1 + ──
⎪                      ╱          2        ╱          2          ╱          2
⎪                    ╲╱          R       ╲╱          R         ╲╱          R
⎨
⎪     4     ⎛r⎞
⎪    R ⋅asin⎜─⎟          3                     3                 5
⎪           ⎝R⎠         R ⋅r              3⋅R⋅r                 r
⎪    ────────── - ──────────────── + ──────────────── - ──────────────────       otherwise
⎪        8                ________           ________             ________
⎪                        ╱      2           ╱      2             ╱      2
⎪                       ╱      r           ╱      r             ╱      r
⎪                 8⋅   ╱   1 - ──    8⋅   ╱   1 - ──    4⋅R⋅   ╱   1 - ──
⎪                     ╱         2        ╱         2          ╱         2
⎩                   ╲╱         R       ╲╱         R         ╲╱         R

功率几乎为0.5,但答案需要使用特殊功能来表示。 SymPy可能没有注意到0.5应该是1/2(并且浮点数的不精确性对此没有帮助)。

话虽如此,我认为这是一个SymPy错误,特别是因为它可以计算In [40]: integrate(r**2*(R**2-r**2)**0.5001, r) Out[40]: ⎛ │ 2 2⋅ⅈ⋅π⎞ 1.0002 3 ┌─ ⎜-0.5001, 3/2 │ r ⋅ℯ ⎟ 0.333333333333333⋅R ⋅r ⋅ ├─ ⎜ │ ─────────⎟ 2╵ 1 ⎜ 5/2 │ 2 ⎟ ⎝ │ R ⎠