我有一个小的网络应用程序,需要3点,并计算抛物线函数或指数函数.下面是抛物线的代码:

@app.route("/calculate/parabola", methods=["POST"])
def calculate_parabola():
    content = request.get_json()
    p1_X = content["p1_X"]
    p1_Y = content["p1_Y"]
    p2_X = content["p2_X"]
    p2_Y = content["p2_Y"]
    p3_X = content["p3_X"]
    p3_Y = content["p3_Y"]
    
    a, b, c = symbols("a,b,c")
    eq1 = Eq((a*(p1_X**2)+b*p1_X+c),p1_Y)
    eq2 = Eq((a*(p2_X**2)+b*p2_X+c), p2_Y)
    eq3 = Eq((a*(p3_X**2)+b*p3_X+c), p3_Y)

    eq_result = solve((eq1, eq2, eq3), (a,b,c))
    print(eq_result)

    returnJSON = {
        "function": f"{eq_result[a]}*x^2+{eq_result[b]}*x+{eq_result[c]}",
        "success": 1
    }
    return returnJSON

这很好用.But here's the problem:

@app.route("/calculate/exponential-function", methods=["POST"])
def calculate_exponential_function():
    print("calculating...")
    content = request.get_json()
    p1_X = content["p1_X"]
    p1_Y = content["p1_Y"]
    p2_X = content["p2_X"]
    p2_Y = content["p2_Y"]
    p3_X = content["p3_X"]
    p3_Y = content["p3_Y"]
    
    a, b, c = symbols("a,b,c", real=True)
    # eq1 = Eq((a*(b**p1_X)+c), p1_Y)
    # eq2 = Eq((a*(b**p2_X)+c), p2_Y)
    # eq3 = Eq((a*(b**p3_X)+c), p3_Y)

    eq1 = Eq((a*(b**p1_X)+c), p1_Y)
    eq2 = Eq((a*(b**p2_X)+c), p2_Y)
    eq3 = Eq((a*(b**p3_X)+c), p3_Y)


    # eq_result = solve((eq1, eq2, eq3), (a,b,c))
    eq_result = solve((eq1, eq2, eq3), (a,b,c))

    print(eq_result)

    returnJSON = {}

    if(eq_result == []):
        returnJSON = {
        "success": 0
        }
    else:
        returnJSON = {
        "function": f"{eq_result[a]}*({eq_result[b]}**x)+{eq_result[c]}",
        #"function": f"{eq_result[a]}*x+{eq_result[c]}",
        "success": 1
        }

    return returnJSON

如果一切正常,"eq_result"应该是这样的: {a: 5/6, b: -1/6, c: 1} 但是当执行calculate_exponential_function()时,"eq_result"输出(例如): [(-27/5, -2/3, 32/5), (1, 2, 0)] 我做错什么了吗?如果你需要更多的信息就告诉我.

推荐答案

solve()在它返回的内容上是非常奇怪的,但对于这种奇怪的感觉相当友好(如果有的话,返回不同的,不兼容的对象而不是None是完全非pythonic和令人惊讶的,尽管它基本上返回的结果是list.

来自Docs https://docs.sympy.org/latest/explanation/solve_output.html

solve()函数的输出看起来非常笨拙,因为它可能会任意返回六种不同类型的输出中的一种(除了引发错误).其原因是历史性的,偏向于人类互动,而不是程序化的使用.输出的类型将取决于等式的类型(以及输入它们的方式)和提供的符号的数量(以及提供它们的方式).

您可以通过设置dict=True来使其表现得更好(尽管输出仍然是一个包含零个或一个条目的列表(或者可能有多个条目,但我不确定对于这种情况是否可行)).

>>> x, y = symbols("x y")
>>> solve(x + y, {x, y})
[{x: -y}]
>>> solve([x + y], {x, y})  # ???
{x: -y}
>>> solve([x + y], {x, y}, dict=True)  # always list of dict(s)
[{x: -y}]

solve()

如果传递的符号是要查找的解,输出将根据传递的符号数、是否传递表达式列表以及是否求解线性系统而有所不同.使用dict=Trueset=True可以获得均匀的输出.

更令人困惑的是,solve()dict=True(或set=True)不会返回本身的值,因为它们不是"有趣的",所以在我的简短实验中,如果你想要完整的集合,需要把它们放在结果dict中

>>> x, y = symbols("x y")
>>> solve(x + y, {x, y}, dict=True)  # doesn't bother with y=y or y=-x
[{x: -y}]
>>> solve(x + y, {y}, dict=True)     # force solving for y
[{y: -x}]

齐心协力!

def calculate_exponential_function():
    content = request.get_json()
    p1_X = content["p1_X"]
    p1_Y = content["p1_Y"]
    p2_X = content["p2_X"]
    p2_Y = content["p2_Y"]
    p3_X = content["p3_X"]
    p3_Y = content["p3_Y"]
    
    a, b, c = symbols("a b c", real=True)

    eq1 = Eq((a*(b**p1_X)+c), p1_Y)
    eq2 = Eq((a*(b**p2_X)+c), p2_Y)
    eq3 = Eq((a*(b**p3_X)+c), p3_Y)

    eq_result = solve((eq1, eq2, eq3), {a,b,c}, dict=True)

    if not eq_result:  # no solution: empty list
        return {
            "success": 0,
        }

    eq_result = eq_result[0]  # TODO consider case of multiple results

    # add missing keys (alt: dict of values and .update())
    for key in (a, b, c):
        if key not in eq_result:
            eq_result[key] = key

    return {
        "function": f"{eq_result[a]}*({eq_result[b]}**x)+{eq_result[c]}",
        "success": 1,
    }

Python相关问答推荐

如何在具有重复数据的pandas中对groupby进行总和,同时保留其他列

pandas DataFrame GroupBy.diff函数的意外输出

为什么这个带有List输入的简单numba函数这么慢

聚合具有重复元素的Python字典列表,并添加具有重复元素数量的新键

log 1 p numpy的意外行为

Julia CSV for Python中的等效性Pandas index_col参数

如何合并两个列表,并获得每个索引值最高的列表名称?

基于行条件计算(pandas)

如何在Gekko中使用分层条件约束

如何求相邻对序列中元素 Select 的最小代价

有没有办法在不先将文件写入内存的情况下做到这一点?

如何获取包含`try`外部堆栈的`__traceback__`属性的异常

高效生成累积式三角矩阵

将相应的值从第2列合并到第1列(Pandas )

使用xlsxWriter在EXCEL中为数据帧的各行上色

如何关联来自两个Pandas DataFrame列的列表项?

对于数组中的所有元素,Pandas SELECT行都具有值

如何在Python中实现高效地支持字典和堆操作的缓存?

#将多条一维曲线计算成其二维数组(图像)表示

Fake pathlib.使用pyfakefs的类变量中的路径'