简体   繁体   English

Sympy - 比较表达式

[英]Sympy - Comparing expressions

Is there a way to check if two expressions are mathematically equal?有没有办法检查两个表达式在数学上是否相等? I expected tg(x)cos(x) == sin(x) to output True , but it outputs False .我预计tg(x)cos(x) == sin(x)到 output True ,但它输出False Is there a way to make such comparisons with sympy?有没有办法与 sympy 进行这样的比较? Another example is (a+b)**2 == a**2 + 2*a*b + b**2 which surprisingly also outputs False .另一个例子是(a+b)**2 == a**2 + 2*a*b + b**2令人惊讶地也输出False

I found some similar questions, but none covered this exact problem.我发现了一些类似的问题,但没有一个涵盖这个确切的问题。

Is there a way to check if two expressions are mathematically equal?有没有办法检查两个表达式在数学上是否相等? I expected tg(x)cos(x) == sin(x) to output True , but it outputs False .我期望tg(x)cos(x) == sin(x)输出True ,但输出False Is there a way to make such comparisons with sympy?有没有办法与sympy进行这种比较? Another example is (a+b)**2 == a**2 + 2*a*b + b**2 which surprisingly also outputs False .另一个例子是(a+b)**2 == a**2 + 2*a*b + b**2令人惊讶地还输出False

I found some similar questions, but none covered this exact problem.我发现了一些类似的问题,但没有一个涵盖这个确切的问题。

Is there a way to check if two expressions are mathematically equal?有没有办法检查两个表达式在数学上是否相等? I expected tg(x)cos(x) == sin(x) to output True , but it outputs False .我期望tg(x)cos(x) == sin(x)输出True ,但输出False Is there a way to make such comparisons with sympy?有没有办法与sympy进行这种比较? Another example is (a+b)**2 == a**2 + 2*a*b + b**2 which surprisingly also outputs False .另一个例子是(a+b)**2 == a**2 + 2*a*b + b**2令人惊讶地还输出False

I found some similar questions, but none covered this exact problem.我发现了一些类似的问题,但没有一个涵盖这个确切的问题。

Is there a way to check if two expressions are mathematically equal?有没有办法检查两个表达式在数学上是否相等? I expected tg(x)cos(x) == sin(x) to output True , but it outputs False .我期望tg(x)cos(x) == sin(x)输出True ,但输出False Is there a way to make such comparisons with sympy?有没有办法与sympy进行这种比较? Another example is (a+b)**2 == a**2 + 2*a*b + b**2 which surprisingly also outputs False .另一个例子是(a+b)**2 == a**2 + 2*a*b + b**2令人惊讶地还输出False

I found some similar questions, but none covered this exact problem.我发现了一些类似的问题,但没有一个涵盖这个确切的问题。

As previously stated, (expr1 - expr2).simplify() or expr1.equals(expr2) will sometimes fail to recognize equality for expressions that are complex to simplify.如前所述, (expr1 - expr2).simplify()expr1.equals(expr2)有时无法识别复杂简化的表达式的相等性。 To deal with this, a numerical evaluation of the expressions with random numbers may constitute a relatively safe "brute force" test.为了解决这个问题,使用随机数对表达式进行数值评估可能构成相对安全的“蛮力”测试。 I've adapted the excellent solution by @Okapi575 to:我已将@Okapi575 的出色解决方案改编为:

  1. Test the numerical equality N-times with different random numbers each time for a more confident diagnostic每次使用不同的随机数测试数值相等 N 次以获得更自信的诊断
  2. Warn the user when a pair of expressions only passes the numeric test but not the symbolic equality test.当一对表达式仅通过数字测试但未通过符号相等测试时警告用户。

For example:例如:

相等性检验示例

Hope it can prove useful:希望它可以证明是有用的:

import sympy as sp
import numpy as np

def check_equal(Expr1, Expr2, n=10):
    
    # Minimal tests
    if Expr1==None or Expr2==None:
        return(False)
    if Expr1.free_symbols!=Expr2.free_symbols:
        return(False)
    vars = Expr1.free_symbols
    
    # Numeric (brute force) equality testing n-times
    for i in range(n):
        your_values=np.random.random(len(vars))
        Expr1_num=Expr1
        Expr2_num=Expr2
        for symbol,number in zip(vars, your_values):
            Expr1_num=Expr1_num.subs(symbol, sp.Float(number))
            Expr2_num=Expr2_num.subs(symbol, sp.Float(number))
        Expr1_num=float(Expr2_num)
        Expr2_num=float(Expr2_num)
        if not np.allclose(Expr1_num, Expr2_num):
            print("Fails numerical test")
            return(False)
        
    # If all goes well so far, check symbolic equality
    if (Expr1.equals(Expr2)):
        return(True)

    else:
        print("Passes the numerical test but not the symbolic test")
        # Still returns true though
        return(True)

Is there a way to check if two expressions are mathematically equal?有没有办法检查两个表达式在数学上是否相等? I expected tg(x)cos(x) == sin(x) to output True , but it outputs False .我期望tg(x)cos(x) == sin(x)输出True ,但输出False Is there a way to make such comparisons with sympy?有没有办法与sympy进行这种比较? Another example is (a+b)**2 == a**2 + 2*a*b + b**2 which surprisingly also outputs False .另一个例子是(a+b)**2 == a**2 + 2*a*b + b**2令人惊讶地还输出False

I found some similar questions, but none covered this exact problem.我发现了一些类似的问题,但没有一个涵盖这个确切的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM