NotImplementedError in Sympy's solve

Kur*_*eek 5 python sympy

I'm reading an article about Bloom filters, https://en.wikipedia.org/wiki/Bloom_filter, in which an expression is derived for the optimal number of hash functions. I'd like to reproduce the computation for the simplified case that m = n, that is, I'd like to determine the minimum of the function

(1-exp(-x))**x
Run Code Online (Sandbox Code Playgroud)

which, from the article, should occur at x = ln(2). I tried doing this with sympy as follows:

In [1]: from sympy import *

In [2]: x, y, z = symbols('x y z')

In [3]: init_printing(use_unicode=True)

In [8]: from sympy.solvers import solve

In [9]: solve(diff((1-exp(-x))**x,x), x)
Run Code Online (Sandbox Code Playgroud)

However, I get a

NotImplementedError: multiple generators [x, exp(x), log(1 - exp(-x))]
No algorithms are implemented to solve equation x*exp(-x)/(1 - exp(-x)) + log(1 - exp(-x))
Run Code Online (Sandbox Code Playgroud)

I would just like to double-check whether Sympy really cannot solve this problem? Perhaps I need to add additional constraints/assumptions on x?

Tri*_*eid 2

当您遇到无法通过操作符号来求解方程(解析求解)的问题时,仍然可以通过尝试不同的数字并获得(或非常接近)正确答案(数值求解)来求解该问题。 )。

您可以将 sympy 解转换为基于 numpy 的函数,并使用 scipy 进行数值求解。

from sympy import lambdify
from scipy.optimize import fsolve

func_np = sp.lambdify(x, diff((1-exp(-x))**x,x), modules=['numpy'])
solution = fsolve(func_np, 0.5)
Run Code Online (Sandbox Code Playgroud)

方程的解为0.69314718,这正是您所期望的。