用Python总结阶乘

编程入门 行业动态 更新时间:2024-10-26 22:18:18
本文介绍了用Python总结阶乘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想使用python中的符号代数来计算阶乘和.我可以生成的最简单的问题是以下版本:

I would like to compute sums with factorials using symbolic algebra in python. The simplest version of the problem I can generate is the following one:

from sympy.abc import j from math import factorial from sympy import summation summation(factorial(j), (j, 1, 4))

然后出现以下错误:

Traceback (most recent call last): File "<stdin>", line 1, in <module> File "sympy/core/expr.py", line 194, in __int__ r = self.round(2) File "sympy/core/expr.py", line 3042, in round raise TypeError('%s is not a number' % type(x)) TypeError: <class 'sympy.core.symbol.Symbol'> is not a number

从根本上说,我想计算的是

Fundamentally, what I would like to compute is

summation(x**(j-1)/factorial(j-1), (j, 1, 3))

有什么建议吗?

推荐答案

使用Sympy自己的factorial函数(而不是math模块的阶乘函数)可能会返回您想要的内容.

Using Sympy's own factorial function (instead of the math module's factorial function) could perhaps return what you want.

在完成初始设置但省略from math import factorial之后,您可以这样写:

Following your initial setup but omitting from math import factorial, you could then write:

>>> from sympy import factorial, symbols >>> x = symbols('x') >>> summation(x**(j-1)/factorial(j-1), (j, 1, 3)) x**2/2 + x + 1

这将阶乘级的总和简化为一个简单的二次方程.

This reduces the summation of the factorial series to a simple quadratic equation.

我注意到您正在计算exp(x)的幂级数展开的前几个项的总和:

I notice you are calculating the sum of the first few terms of the power series expansion of exp(x):

1 + x + x**2/2! + x**3/3! + ...

更多推荐

用Python总结阶乘

本文发布于:2023-11-29 19:39:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647430.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:阶乘   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!