You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SymPy求解含绝对值的复变量不等式及绘图问题求助

Better Ways to Plot Complex Number Point Sets with SymPy

Hey there! Let’s break down how to fix this issue you’re facing with SymPy and complex number point sets—since you’re trying to plot the set of complex numbers ( z = a + bi ) satisfying ( |z - i| < 10 ), here are some practical alternatives to get past those SymPy roadblocks:

1. Skip SymPy Solving Entirely (Use Geometric Logic + Matplotlib)

First off, your inequality ( \sqrt{a^2 + (b-1)^2} < 10 ) is just an open disk on the complex plane: center at ( (0,1) ) (since that’s ( 0 + 1i )), radius 10. You don’t need to mess with solveset at all—just plot it directly with matplotlib:

import matplotlib.pyplot as plt
import numpy as np

# Create a grid of real (a) and imaginary (b) values
a_vals = np.linspace(-10, 10, 400)
b_vals = np.linspace(-9, 11, 400)
a_grid, b_grid = np.meshgrid(a_vals, b_vals)

# Check which points satisfy the inequality
in_disk = np.sqrt(a_grid**2 + (b_grid - 1)**2) < 10

# Plot the region
plt.figure(figsize=(6,6))
plt.contourf(a_grid, b_grid, in_disk, cmap='coolwarm', levels=[0,1])
plt.scatter(0, 1, color='black', marker='x', label='Center (0, 1)')
plt.xlabel('Real Part (a)')
plt.ylabel('Imaginary Part (b)')
plt.title('Open Disk: |z - i| < 10')
plt.legend()
plt.axis('equal')
plt.show()

This is fast, intuitive, and gives you a clean plot right away.

2. Use SymPy’s Implicit Plotting (No Solveset Needed)

If you want to stick with SymPy for the symbolic side, use plot_implicit to render the inequality directly. This bypasses the ConditionSet problem entirely:

from sympy import symbols, sqrt, Lt, plot_implicit

# Define real variables for the complex parts
a, b = symbols('a b', real=True)
# Your inequality rewritten symbolically
disk_inequality = Lt(sqrt(a**2 + (b - 1)**2), 10)

# Plot it directly
plot_implicit(
    disk_inequality,
    (a, -10, 10),
    (b, -9, 11),
    title='|z - i| < 10',
    xlabel='Real Part (a)',
    ylabel='Imaginary Part (b)'
)

SymPy handles the real-domain constraint internally here, so you don’t have to wrestle with solveset errors.

3. Working with ConditionSet (If You Must)

If you really need to extract usable points from the ConditionSet that solveset returns, you can generate random points that satisfy the condition. It’s not the most efficient, but it works:

from sympy import symbols, sqrt, Lt, solveset, S, random

a, b = symbols('a b', real=True)
disk_inequality = Lt(sqrt(a**2 + (b - 1)**2), 10)
solution_set = solveset(disk_inequality, (a, b), domain=S.Reals)

# Function to generate a valid point in the set
def get_valid_point():
    while True:
        a_val = random.uniform(-10, 10)
        b_val = random.uniform(-9, 11)
        if sqrt(a_val**2 + (b_val - 1)**2) < 10:
            return (a_val, b_val)

# Generate 50 sample points
sample_points = [get_valid_point() for _ in range(50)]
print("Sample valid points:", sample_points[:5])

Just keep in mind—this is only useful if you need discrete points, not the full continuous region.

Why You Ran Into That Error

The core issue here is that solveset is designed for finding symbolic solutions, but your inequality describes a continuous region, not a finite set of points. SymPy returns a ConditionSet because there’s no way to write this solution as a simple interval or list of points. Direct plotting tools are made exactly for this kind of problem, so they’re always the better bet.

内容的提问来源于stack exchange,提问作者Moritz Lehner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 10:37:47