簡體   English   中英

在 Sympy 中更改 FormalPowerSeries 的索引符號

[英]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產生以下結果由 k 索引的術語 z 的幾何級數 當我更願意擁有由 n 索引的術語 z 的幾何級數 . 雖然我可以從form變量構造Sum的實例,但我更願意保留FormalPowerSeries數據類型的語義。 我目前的解決方法適用於玩具問題,但我不確定它是否會破壞任何底層機制,或者是否有規范的方法來執行此重新索引。

使用xreplace是更改表達式中的符號的有效方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM