【问题标题】:python sympy How do I simplify an expression where I know that a certain variable is an odd integerpython sympy 如何简化我知道某个变量是奇整数的表达式
【发布时间】:2020-07-05 17:21:29
【问题描述】:

例如,给定表达式(3*cos(pi*n/2)+2*sin(pi*n/2))/n**2 并且知道 n 是一个正奇整数(即 1,3,5,...),表达式可以简化为 2*(-1)**((n-1)/2)/n**2,因为 cos(pi*n/2) 术语全部归零,sin(pi*n/2) 项变为 -1 或 +1。有什么方法可以让 sympy 认识到这一事实并进行简化?

【问题讨论】:

    标签: python sympy symbolic-math simplification


    【解决方案1】:

    您可以将n 声明为odd

    In [72]: n = Symbol('n', odd=True)                                                                                                             
    
    In [73]: (3*cos(pi*n/2)+2*sin(pi*n/2))/n**2                                                                                                    
    Out[73]: 
          n   1
          ─ - ─
          2   2
    2⋅(-1)     
    ───────────
          2    
         n 
    

    【讨论】:

    • 谢谢 - 完美。您的回复促使我找到了可能的假设的完整列表。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多