[英]Change the index symbol of a FormalPowerSeries in Sympy
如果我構造一個表達式的FormalPowerSeries
,則用作索引變量的默認符號是k
。 這不是一個固有的問題,但我想遵守使用n
的其他正式文本的約定。
from sympy.series.formal import FormalPowerSeries, fps
gfps: FormalPowerSeries = fps(1/(1-z))
display(gfps)
# Workaround utility to find the first dummy with a given name
def findDummy(expr: Expr, name: str) -> Dummy:
from sympy.utilities.iterables import default_sort_key
return next(filter(
lambda s: isinstance(s, Dummy) and s.name == name,
sorted(expr.free_symbols, key=default_sort_key)))
# Workaround, replace the dummy `k` with another dummy, `n`
form = (gfps.ak.formula * gfps.xk.formula)
k: Dummy = findDummy(gfps, 'k')
gfps = gfps.xreplace({k: Dummy('n', integer=True)})
display(gfps)
第一次調用display
產生以下結果 當我更願意擁有 . 雖然我可以從form
變量構造Sum
的實例,但我更願意保留FormalPowerSeries
數據類型的語義。 我目前的解決方法適用於玩具問題,但我不確定它是否會破壞任何底層機制,或者是否有規范的方法來執行此重新索引。
使用xreplace
是更改表達式中的符號的有效方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.