• Sympy-nonlinsolve-只保留实数解问题


    问题描述

    通过sympy中nonlinsolve求解二元二次方程,得到的解为负数解,如下。而我只想要里面的(1, 0)实数结果。

    # 后面用的也一起导入了
    from sympy import S, symbols, nonlinsolve, Rational, I
    
    x, y = symbols('x y')
    system = [x**2+y**2-1, (x-3)**2/4+y**2-1]
    result = nonlinsolve(system, [x, y])
    print(result)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    输出为{(-3, -2*sqrt(2)*I), (-3, 2*sqrt(2)*I), (1, 0)}
    
    • 1

    搜了一些诸如:符号设置为实数变量等的方法都没用,感觉有必要记录一下。

    不明原理的解决方法

    搜到有人遇到了类似的问题,该问题下有人指出了nonlinsolve需要通过和实数集的交集来获取实数结果。
    在这里插入图片描述
    该方法提供了思路,但是在实践过后并不是和实数的交集,而是和复数的交集:

    x, y = symbols('x y')
    system = [x**2+y**2-1, (x-3)**2/4+y**2-1]
    result = nonlinsolve(system, [x, y])
    print(result)
    print(result & S.Complexes)
    print(result & S.Reals)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    结果依次为
    {(-3, -2*sqrt(2)*I), (-3, 2*sqrt(2)*I), (1, 0)}
    {(1, 0)}
    EmptySet
    
    • 1
    • 2
    • 3
    • 4
    • 为什么和复数的交集是唯一的实数集,而放弃了另外两个复数;
    • 为什么和实数的交集为空…

    想了半天,原因没有搞懂。但是既然代码能跑对,就暂时先这么用了。
    另外,也可以用

    result.intersect(S.Complexes)
    
    • 1

    来获得实数集

    当然,换几个例子也是可以跑对的。

    x, y = symbols('x y')
    system = [x**2+y**2-1, (x-1)**2/4+y**2-1]
    result = nonlinsolve(system, [x, y])
    print(result)
    print(result & S.Complexes)
    print(result & S.Reals)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    {(-1, 0), (1/3, -2*sqrt(2)/3), (1/3, 2*sqrt(2)/3)}
    {(-1, 0), (1/3, -2*sqrt(2)/3), (1/3, 2*sqrt(2)/3)}
    EmptySet
    
    • 1
    • 2
    • 3
    x, y = symbols('x y')
    system = [x**2+y**2-1, (x-4)**2/4+y**2-1]
    result = nonlinsolve(system, [x, y])
    print(result)
    print(result & S.Complexes)
    print(result & S.Reals)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    {(-4, -sqrt(15)*I), (-4, sqrt(15)*I), (4/3, -sqrt(7)*I/3), (4/3, sqrt(7)*I/3)}
    EmptySet
    EmptySet
    
    • 1
    • 2
    • 3

    system的两个方程分别是圆和椭圆,也比较好画,画出来图就可以看到结果与几何结果保持一致。

    判断是否有实数解

    我的需求就是判断有没有实数解就可以了

    x, y = symbols('x y')
    system = [x**2+y**2-1, (x-3)**2/4+y**2-1]
    result = nonlinsolve(system, [x, y])
    realResult = result & S.Complexes
    
    print(result)
    print(realResult)
    print(result & S.Reals)
    
    if realResult.is_empty is True:
        print('无实数解')
    else:
        print(realResult)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    {(-4, -sqrt(15)*I), (-4, sqrt(15)*I), (4/3, -sqrt(7)*I/3), (4/3, sqrt(7)*I/3)}
    EmptySet
    EmptySet
    无实数解
    
    • 1
    • 2
    • 3
    • 4

    简单的讨论与学习

    根据官方文档进行了简要学习

    • sympy的返回结果可以按照数据类型进行分类,以方便对结果筛选
    • nonlinsolve是同时返回实数解和复数解的

    在这里插入图片描述

  • 相关阅读:
    python 装饰器
    LeetCode 496. Next Greater Element I
    linux os cpufreq 调频
    详解 Scala 的隐式转换
    四六级成绩查询方法及相关问题解答(全网最贴心版)
    msvcp100.dll丢失怎样修复,msvcp100.dll丢失问题全面解析
    信钰证券:买基金有哪些平台?
    AJAX概念
    windows系统下压力测试工具(cpu使用率,内存使用率,磁盘使用率,磁盘空间)
    数据库timestamp遇到的小问题
  • 原文地址:https://blog.csdn.net/wwz1751879/article/details/126253345