Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 不使用rsolve计算n次递推_Python_Sympy - Fatal编程技术网

Python 不使用rsolve计算n次递推

Python 不使用rsolve计算n次递推,python,sympy,Python,Sympy,我正在编写一个应用程序,它必须解决一些递归关系,但有一些关系不能用sympy的rsolve方法解析解决。它只返回None。有没有办法强迫sympy用数值方法来求解它们 我有这样的想法: from sympy import * ctx = { "f": Function("f"), "x": var("x",integer=True) } initial_conditions = { 0: 1, 1: 1, 2: 1, 3: 1 } f = sympi

我正在编写一个应用程序,它必须解决一些递归关系,但有一些关系不能用
sympy
rsolve
方法解析解决。它只返回
None
。有没有办法强迫sympy用数值方法来求解它们

我有这样的想法:

from sympy import *

ctx = {
  "f": Function("f"),
  "x": var("x",integer=True)
}

initial_conditions = {
    0: 1,
    1: 1,
    2: 1,
    3: 1
}

f = sympify("-2*f(x-1)+11*f(x-2)+12*f(x-3)-36*f(x-4) +41**(x-4)+3 -f(x)", ctx)

# calculate f(10) here without creating a closed from
# The code below will not work rsolve returns None
solve_for = sympify("f(x)", ctx)
solved = rsolve(f, solve_for, initial_conditions)

我希望有人能帮助我

>P>这可能是一个没有解析解(见下文)的问题,我将考虑线性代数方法:

返回:

(-3)**x*(C0 + C1*x) + 2**x*C1*(C0 + C1*x)/C0 + 41**x/2944656 + 3/16

None

这是我用数值计算递推关系的方法。 当指定重复关系作为输入以简化时,请确保 值
f(x)
不是字符串的一部分。也就是说,如果你的递归关系是

f(x) = -2*f(x-1)+11*f(x-2)+12*f(x-3)-36*f(x-4) +41**(x-4)+3
您的输入字符串应为:

"-2*f(x-1)+11*f(x-2)+12*f(x-3)-36*f(x-4) +41**(x-4)+3"
此外,该解决方案仅限于线性递推关系,但也可扩展到其他情况

代码所做的是遍历递归关系的语法树,通过数值计算或查找已知的f(x)值来计算每个节点


这毫无意义。我有足够的初始条件,可以手工迭代求解。您介意也包括您的预期输出吗?预期输出是一个函数,我可以要求sympy为
y(10)
y(11)
y(12)
。我不需要闭式公式,即使它很方便。你是什么意思?没有什么可以确认的,你可以用手从下到上计算它。先是f(4),然后是f(5),依此类推。重复次数最多只返回4次,因此如果您有最后4次,则始终可以计算下一次。因为你有4个初始条件,这是完全有效的,你可以把它计算成任何数>=4,所以你也可以得到
f(4)
给出
y(4)=-11
?你真的是说
41**
?是的,我也可以写41^是的,我最终实现了类似的东西。只是让你知道你已经复制了两次代码!谢谢你的帮助!不客气。我喜欢你的问题,并编辑了答案以删除重复。
"-2*f(x-1)+11*f(x-2)+12*f(x-3)-36*f(x-4) +41**(x-4)+3"
from sympy import *
import operator
ctx = {
  "f": Function("f"),
  "x": var("x",integer=True)
}

initial_conditions = {
    0: 1,
    1: 1,
    2: 1,
    3: 1
}

func1 = sympify("-2*f(x-1)+11*f(x-2)+12*f(x-3)-36*f(x-4) +41**(x-4)+3", ctx)



def contains_function(f):
    if issubclass(type(f),Function):
        return True
    r = map(contains_function,f.args)
    return (sum(r) != 0)


def get_numeric_value(arg):

    if arg.is_number:
        if arg.is_integer:
            return int(arg)

        else:
            return float(arg)    
    else:
        return None

def evaluate_at(f, n, initial_conditions):


    if f.is_Add:
        result = 0
        op = operator.add
    elif f.is_Mul:
        result = 1
        op = operator.mul
    elif f.is_Function:
        func_arg = f.args[0]
        func_arg_val = int(func_arg.subs(func_arg.free_symbols.pop(),n))
        if not func_arg_val in initial_conditions:
            return None
        else:
            return initial_conditions[func_arg_val]

    else:
        return None

    for arg in f.args:
        if arg.is_number:
            result= op(result, get_numeric_value(arg))
        elif contains_function(arg):
            r = evaluate_at(arg,n,initial_conditions)
            if r:
                result=op(result,r)
            else:
                return None
        else:
            result =op(result,get_numeric_value(arg.subs(arg.free_symbols.pop(),n)))


    return result


known_values = dict(initial_conditions)
for n in range(4,11):
    known_values[n]  = evaluate_at(func1,n,known_values)