1

mpmath.mpffloat を渡してでルートを見つけたい場合、問題が発生しmp.findrootます。

この問題は、前の質問の続きです: Polynomial function cannot be solve by Python sympy

import sympy
omega = sympy.symbols('omega')
from sympy import I 
import mpmath as mp
import numpy as np

# Definition of simplified function
def function(omega):
    return sympy.Poly((-1.16*omega**4), omega)

# This function does not work
def doesntwork(omega):
    return (mp.mpf(str(np.real(sympy.lambdify( (I),function(omega).coeffs()[0])(1j)))) * omega ** 4)

# This function does work, but I need to handover the value due to its not constant
def doeswork(omega):
    return (mp.mpf('-1.16') * omega ** 4)

#print mp.findroot(doesntwork, 1)   # error message
print mp.findroot(doeswork, 1)      # result is obtained

係数を文字列で入力するmp.findrootとコマンドが動作しますが、自動手順で係数を読み込むと動作しません。mpmath.mpfこの問題は、文字列として認識されない入力が原因のようです。この単純な例のように係数が一定ではないため、この自動手順が必要です。

関数 dontwork のエラー メッセージは次のとおりです。

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-44-7ed2fe96e32b> in <module>()
----> 1 print mp.findroot(doesntwork, 1)   # error message
      2 #print mp.findroot(doeswork, 1)      # result is obtained

C:\Anaconda\lib\site-packages\mpmath\calculus\optimization.pyc in findroot(ctx, f, x0, solver, tol, verbose, verify, **kwargs)
    926         # detect multidimensional functions
    927         try:
--> 928             fx = f(*x0)
    929             multidimensional = isinstance(fx, (list, tuple, ctx.matrix))
    930         except TypeError:

<ipython-input-40-f85df4161c52> in doesntwork(omega)
      1 # This function does not work
      2 def doesntwork(omega):
----> 3     return (mp.mpf(str(np.real(sympy.lambdify( (I),function(omega).coeffs()[0])(1j)))) * omega ** 4)
      4 
      5 # This function does work, but I need to handover the value due to its not constant

<ipython-input-39-fb50e638fea4> in function(omega)
      1 # Definition of simplified function
      2 def function(omega):
----> 3     return sympy.Poly((-1.16*omega**4), omega)

C:\Anaconda\lib\site-packages\sympy\polys\polytools.pyc in __new__(cls, rep, *gens, **args)
     71     def __new__(cls, rep, *gens, **args):
     72         """Create a new polynomial instance out of something useful. """
---> 73         opt = options.build_options(gens, args)
     74 
     75         if 'order' in opt:

C:\Anaconda\lib\site-packages\sympy\polys\polyoptions.pyc in build_options(gens, args)
    729 
    730     if len(args) != 1 or 'opt' not in args or gens:
--> 731         return Options(gens, args)
    732     else:
    733         return args['opt']

C:\Anaconda\lib\site-packages\sympy\polys\polyoptions.pyc in __init__(self, gens, args, flags, strict)
    152                     self[option] = cls.preprocess(value)
    153 
--> 154         preprocess_options(args)
    155 
    156         for key, value in dict(defaults).items():

C:\Anaconda\lib\site-packages\sympy\polys\polyoptions.pyc in preprocess_options(args)
    150 
    151                 if value is not None:
--> 152                     self[option] = cls.preprocess(value)
    153 
    154         preprocess_options(args)

C:\Anaconda\lib\site-packages\sympy\polys\polyoptions.pyc in preprocess(cls, gens)
    290         elif has_dups(gens):
    291             raise GeneratorsError("duplicated generators: %s" % str(gens))
--> 292         elif any(gen.is_commutative is False for gen in gens):
    293             raise GeneratorsError("non-commutative generators: %s" % str(gens))
    294 

C:\Anaconda\lib\site-packages\sympy\polys\polyoptions.pyc in <genexpr>((gen,))
    290         elif has_dups(gens):
    291             raise GeneratorsError("duplicated generators: %s" % str(gens))
--> 292         elif any(gen.is_commutative is False for gen in gens):
    293             raise GeneratorsError("non-commutative generators: %s" % str(gens))
    294 

AttributeError: 'mpf' object has no attribute 'is_commutative'

ご協力ありがとうございました!

4

1 に答える 1

0

問題を解決しました。明らかに、係数が以前に変数に格納されている場合は異なります。

coefficient = str(np.real(sympy.lambdify( (I),function(omega).coeffs()[0])(1j)))
def doesntwork(omega):
    return (mp.mpf(coefficient) * omega ** 4)

print mp.findroot(doesntwork, 1) 

出力

0.00267117593442144 
于 2016-03-31T10:50:40.573 に答える