本文整理汇总了Python中sympy.core.Add.expand方法的典型用法代码示例。如果您正苦于以下问题:Python Add.expand方法的具体用法?Python Add.expand怎么用?Python Add.expand使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sympy.core.Add
的用法示例。
在下文中一共展示了Add.expand方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: interpolate
# 需要导入模块: from sympy.core import Add [as 别名]
# 或者: from sympy.core.Add import expand [as 别名]
def interpolate(data, x):
"""
Construct an interpolating polynomial for the data points.
Examples
========
>>> from sympy.polys.polyfuncs import interpolate
>>> from sympy.abc import x
A list is interpreted as though it were paired with a range starting
from 1:
>>> interpolate([1, 4, 9, 16], x)
x**2
This can be made explicit by giving a list of coordinates:
>>> interpolate([(1, 1), (2, 4), (3, 9)], x)
x**2
The (x, y) coordinates can also be given as keys and values of a
dictionary (and the points need not be equispaced):
>>> interpolate([(-1, 2), (1, 2), (2, 5)], x)
x**2 + 1
>>> interpolate({-1: 2, 1: 2, 2: 5}, x)
x**2 + 1
"""
n = len(data)
poly = None
if isinstance(data, dict):
X, Y = list(zip(*data.items()))
poly = interpolating_poly(n, x, X, Y)
else:
if isinstance(data[0], tuple):
X, Y = list(zip(*data))
poly = interpolating_poly(n, x, X, Y)
else:
Y = list(data)
numert = Mul(*[(x - i) for i in range(1, n + 1)])
denom = -factorial(n - 1) if n%2 == 0 else factorial(n - 1)
coeffs = []
for i in range(1, n + 1):
coeffs.append(numert/(x - i)/denom)
denom = denom/(i - n)*i
poly = Add(*[coeff*y for coeff, y in zip(coeffs, Y)])
return poly.expand()