python - 在 python 中找到临界值,有限集的问题
问题描述
我编写了用于在 Python 中查找临界值的程序。
我的代码:
from sympy import *
def find_critical_points(f, x):
fd = diff(f)
dRoots = solveset(fd, x)
a = Rational(float(dRoots))
return a
我为此写了测试:
x = Symbol('x')
lst = find_critical_points(x**4+x**3, x)
assert lst == [-3/4,0]
lst = find_critical_points(x,x)
assert lst == []
Python返回错误:
float() 参数必须是字符串或数字,而不是 'FiniteSet'
请帮助解决此错误。
解决方案
问题是它float()
需要一个字符串或一个数字,然后对其进行解析或将其转换为浮点数。
稍加研究,我们可以发现FiniteSet
可以直接转入一个 Python 列表。所以你的代码可以是这样的:
from sympy import *
def find_critical_points(f, x):
fd = diff(f)
dRoots = solveset(fd, x)
# a = Rational(float(dRoots))
return list(dRoots)
x = Symbol('x')
lst = find_critical_points(x**4+x**3, x)
assert lst == [-3/4,0]
lst = find_critical_points(x,x)
assert lst == []
推荐阅读
- python - Django-Dramatiq - 如何配置 Dramatiq_abort
- reactjs - React 输入中的 Onchange 导致输入在每个字母上重新呈现
- ios - 滚动视图混淆选项卡之间的内容大小
- javascript - 在 react js 中对齐我的网页的输入字段。似乎无法理解我必须遵循的对齐方式。附布局
- minecraft - 找不到 Minecraft Education Edition 的文档
- python - 如何在 UTF-8 文件的开头去除垃圾字符
- flutter - 颤振分析错误太多
- web-worker - 通过blob throwing importscripts加载的来自不同域的Webworker无法加载
- java - 错误 - java.lang.ClassCastException:com.google.android.material.appbar.AppBarLayout 无法转换为 androidx.appcompat.widget.Toolbar
- react-native - 从嵌套屏幕导航到不同选项卡堆栈中的另一个嵌套屏幕(反应导航)