我有蟒蛇pet_cycleind_symmNM(n, m)
一個函數時呼籲pet_cycleind_symmNM(5, 5)
返回一個「術語列表」轉換條款值,而不會丟失精度(分數)
[[Fraction(1, 14400), [[1, 25]]], [Fraction(1, 720), [[2, 5], [1, 15]]], [Fraction(1, 360), [[3, 5], [1, 10]]], [Fraction(1, 480), [[2, 10], [1, 5]]], [Fraction(1, 240), [[4, 5], [1, 5]]], [Fraction(1, 360), [[3, 5], [2, 5]]], [Fraction(13, 300), [[5, 5]]], [Fraction(1, 144), [[2, 8], [2, -2], [1, 9]]], [Fraction(1, 36), [[6, 1], [2, 2], [3, 3], [1, 6]]], [Fraction(1, 48), [[2, 11], [2, -2], [1, 3]]], [Fraction(1, 24), [[4, 5], [2, 1], [1, 3]]], [Fraction(1, 36), [[6, 1], [2, 5], [3, 3]]], [Fraction(1, 30), [[10, 1], [5, 3]]], [Fraction(1, 36), [[3, 7], [3, -2], [1, 4]]], [Fraction(1, 24), [[6, 2], [3, 1], [2, 4], [1, 2]]], [Fraction(1, 12), [[12, 1], [3, 1], [4, 2], [1, 2]]], [Fraction(1, 18), [[3, 5], [6, 1], [2, 2]]], [Fraction(1, 15), [[15, 1], [5, 2]]], [Fraction(1, 64), [[2, 12], [2, -2], [1, 1]]], [Fraction(1, 16), [[4, 5], [2, 2], [1, 1]]], [Fraction(1, 24), [[6, 2], [2, 5], [3, 1]]], [Fraction(1, 20), [[10, 2], [5, 1]]], [Fraction(1, 16), [[4, 6], [4, -2], [1, 1]]], [Fraction(1, 12), [[12, 1], [4, 2], [3, 1], [2, 1]]], [Fraction(1, 10), [[20, 1], [5, 1]]], [Fraction(1, 36), [[3, 3], [6, 2], [2, 2]]], [Fraction(1, 15), [[15, 1], [10, 1]]]]
每學期由fractions.Fraction
的價值觀,和一個列表一個或多個變量
例如,術語的
[Fraction(1, 720), [[2, 5], [1, 15]]]
裝置1/720 * a2**5 * a1**15
我要評估在列表中,所有條款,所有的「變量」(A1,A2,A3等),僅僅是整數的總和4.
當calcalated「象徵性」的結果應該是79846389608
但我發現79728205394.7
這是我使用評價術語列表
def substitute(term, v):
total = 1
for a in term[1]:
total *= v**a[1]
return (term[0] * total)
def mat_count(n, m, q):
terml = pet_cycleind_symmNM(n, m)
total = 0
for term in terml:
total += substitute(term, q)
return total
print mat_count(5, 5, 4)
我怎樣才能做到這一點的代碼,而不會丟失精度?
答案的核心是sympy,其中Fraction被理性所取代,您可以將a1和a2聲明爲變量,然後使用.sub精確計算涉及它們的表達式。 –