function - 如何在 sympy 中定义自定义函数?
问题描述
我会在 Sympy 中定义如下函数:
f = x 对于 x>=0 和 f = 1/x**2 对于 x<0
我尝试实现如下自定义函数:
class CustomFunction(Function):
@classmethod
def eval(cls, arg):
arg = sympify(arg)
if arg.is_number:
if arg>=0:
return ((x).subs(x,arg))
else:
return (1/x**2).subs(x, arg)
if isinstance(arg, exp):
# What do I have to write here?
pass
我会绘制这个函数,并像 sympy 中的任何其他函数一样使用它。拜托,你能帮我解决这个问题吗?
解决方案
推荐阅读
- sql - 按值将列拆分为新列 SQL
- php - 检查引导程序最后一列是偶数还是奇数
- javascript - 是否可以看到 React Native 的异步存储电流限制?
- c# - 有没有办法设置查询中 linq 选择中返回的每个元素的排序顺序
- java - 带有 Angular URL 重写的嵌入式 Jetty
- typescript - 打字稿运行时错误:无法读取未定义的属性“长度”
- excel - Excel SUMPRODUCT ISNUMBER 搜索范围并忽略范围内的空空白单元格
- python - 如何将输入限制为一个字母?
- java - java如何防止从外部库输出错误
- domain-driven-design - ABAP DDD 如何通过 RFC 从聚合中正确实现添加/更新/删除子项?