【问题标题】:How to calculate the sum of the sequence without using built-in functions for exponentiation?如何在不使用内置函数求幂的情况下计算序列的总和?
【发布时间】:2021-02-09 13:53:27
【问题描述】:

我需要在 Python 中计算序列的总和 但我不能使用内置函数进行求幂。

这意味着我不能使用**pow()。我必须为此创建自己的函数。

所以我创建了求幂函数,但它只适用于数字。我需要将我的公式计算到第 n 个。

我的求幂函数:

def exponentiation(a,b):
    result = 1
    for index in range(b):
        result = result * a
    return result

对于数字,它有效。但是当我想这样做到第 n 个(我将 'n' 定义为符号)时,我得到:

'Symbol' object cannot be interpreted as an integer

所以我不知道如何解决这个问题。

如果我想计算序列的总和,我使用它并且它有效:

sy.summation((-1/2)**n, (n,1, oo))

但正如我之前所说,我需要将** 更改为我自己的求幂函数,但它仍然表明“符号”对象不能解释为整数。

sy.summation(exponentiation((-1/2),n), (n,1, oo))

你有什么建议吗?

【问题讨论】:

  • 用符号取幂的结果究竟是什么?
  • @eugenhu 是的,我使用 sympy
  • 这意味着我不能使用**pow() - 为什么?
  • 所以你可以使用 SymPy 但不能使用 **, pow() ?这似乎是一个 XY 问题。您似乎想计算函数 (-1/2)**n for n in 0 ... infinity 的石灰。 - 你不能用循环“计算”无限的东西,你的代码永远不会完成,你需要一些中断条件,或者需要找到一个封闭的解决方案。
  • 没有任何意义,因为在这种情况下**不是python内置,而是SymPy实现的函数(pow)。跨度>

标签: python python-3.x sum sympy exponentiation


【解决方案1】:

'nth' 表示任何给定的数字。所以你不需要(我想不出你会如何)对任何符号求幂。如果您返回一个列表而不是仅返回第 n 个值,我认为您可能会简化一些事情:

def exponentiation(a, n):
    result = 1
    exponents_list = []
    for i in range(n):
        result *= a
        exponents_list.append(result)
    return exponents_list

然后使用 for 循环处理列表以获取总和

如果您必须使用 sympy,请查看以下答案:Summation over a sympy Array

【讨论】:

  • 它不起作用。我需要改变它:求和((-1/2)**n,(n,1,oo))。 - 我必须用我自己的函数替换**。您编写的函数不起作用,因为它得到“Sum(n**(-0.5), (n, 1, oo))”,结果应该是 1/3。
  • print(sum(exponentiation(-.5, 64))) 大约返回 -1/3 (-0.33333333333333337)
  • 假设您被允许使用内置的sum() 函数。否则,请像求幂函数一样使用 for 循环。
【解决方案2】:

您不能将“n”提高到幂。我很确定如果你被禁止使用 **pow() 使用 SymPy 也不会飞。

要计算 的结果,您可以简单地假设一个“大”n 并检查您是否仍然可以检测到早期结果和下一个结果之间的任何差异 - 由于浮动,您不会很快看到任何变化数学限制 (Is floating point math broken?):

def exponentiation(a,b):
    result = 1
    for index in range(b):
        result = result * a
    return result
s = 0
a = -1/2
for n in range(1, 10000000):
    old_s = s
    s += exponentiation(a,n)

    # do not compare floats with small differences with ==
    # see link below for better ways to do that
    if s == old_s:
        print("\nThe sum no longer changes due to floating math limitations.")
        print(f"Result before: {old_s} and after {s} for n={n}")
        break
    else:
        print(f"nResult before: {old_s} and after {s} for n={n}")

输出:

Result before: 0 and after -0.5 for n=1
Result before: -0.5 and after -0.25 for n=2
Result before: -0.25 and after -0.375 for n=3
[...snipp...]
Result before: -0.33333333333333326 and after -0.33333333333333337 for n=53
Result before: -0.33333333333333337 and after -0.3333333333333333 for n=54
Result before: -0.3333333333333333 and after -0.33333333333333337 for n=55

The sum no longer changes due to floating math limitations.
Result before: -0.33333333333333337 and after -0.33333333333333337 for n=56

有关浮点比较的更多信息,请参阅What is the best way to compare floats for almost-equality in Python?

【讨论】:

    【解决方案3】:

    我想说,用 Python 代码替换 ** 的唯一有意义的解决方案是:

    def exponentiation(a,b):
        if isinstance(a, Symbol):
            return a.__pow__(b)
        if isinstance(b, Symbol):
            return b.__pow__(a)
        result = 1
        for index in range(b):
            result = result * a
        return result
    

    如果你想重新实现 SymPys pow 函数 - 这对于 Stackoverflow-answer 来说肯定太难了;)。

    但是你可以在这里找到 SymPys 源代码: https://github.com/sympy/sympy/blob/master/sympy/core/power.py

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-16
      • 2011-03-04
      • 2011-03-15
      • 1970-01-01
      • 2022-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多