在Python中解决一个困难的(多项式?)方程

时间:2012-02-12 02:49:19

标签: python solver equations sympy

我是编程新手(Python是我的第一语言),但我喜欢设计算法。我目前正在研究一个方程组(整数),我找不到任何解决我特定问题的参考。

让我解释一下。

我有一个等式(测试,如果你愿意):

raw_input == [(90*x + a) * y] + z

其中a是常数。

我的问题是,变量z的计数方式与Fibonacci序列非常相似,变量x是z的步长。所以我的意思是(对于Fibonacci序列)是在z序列的第一项,x = 0,并且在z序列的第二项,x = 1.我需要求解y。

确定z的确切过程如下

where c and d are constants:
#at x = 0
temp = (c+(90*x)) * (d+(90*x))
temp/90 = z(0) 

#at x = 1
new_temp = (c+(90*x)) * (d + (90*x))

new_temp/90 = z(1)  

#for all the rest of the values of z (and x), use:

j = z(@ x=1) - z(@ x=0)
k = j + 180
l = z(@ x=1) + k
print "z(@ x=1) - z(@ x=0) = j"
print "j + 180 = k"
print "k + z(1) = l"
repeat until z > raw_input

this creates the spread of z values by the relation:
j = z(@ x=n) - z(@ x=n-1)
k = j + 180
l = k + z(@ x = n)

我需要扫描(跳过)z<的值。 x来测试y的整数解的条件。

这看起来有可能吗?

2 个答案:

答案 0 :(得分:2)

似乎你最好的方法是将给定的方程重铸为递归关系,然后定义一个递归函数来确定你想要计算的值或找到关系的闭合形式解。有关递归关系的更多信息,请参阅:

最后,根据我的经验,这些问题最好用MatLab,Octave或Mathematica等数学数值分析软件来解决。至少,通过这些平台可以实现快速部署和测试。

答案 1 :(得分:1)

我所做的就是将你的伪代码翻译成Python。也许它可以提供一些帮助。如果您还没有,可以查看Python tutorial

# python 2.7

# raw_input returns string - convert to int
upper_bound = int(raw_input('Upper bound: '))

def z(x):
    'A function to calculate z from x.'
    # c and d are constants
    c = 5
    d = 2
    # integer division here
    return (c + 90*x)*(d + 90*x)/90

# the value of z_0
z0 = z_x = z(0)
# a list to hold the z values z_0, z_1, ...
# the list includes z_0 (when x = 0)
zs = [z0]

x = 1
while z_x < upper_bound:
    z_x = z(x)
    zs.append(z_x)

    j = zs[x] - zs[x - 1]
    k = j + 180
    l = zs[x] + k
    print j, k, l

    x += 1