ChatGPT and DeepSeek generated Python code

STEM版,合并数学,物理,化学,科学,工程,机械。不包括生物、医学相关,和计算机相关内容。

版主: verdeliteTheMatrix

头像
TheMatrix楼主
论坛支柱
论坛支柱
2024年度优秀版主
TheMatrix 的博客
帖子互动: 264
帖子: 13265
注册时间: 2022年 7月 26日 00:35

#1 ChatGPT and DeepSeek generated Python code

帖子 TheMatrix楼主 »

to visualize an elliptic curve in a projective space.

final prompt:

图片

generated code:

代码: 全选

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

def elliptic_curve_surface(a, b, res=100):
    """
    Generates points on the elliptic curve surface Y^2Z = X^3 + aXZ^2 + bZ^3 in projective space.
    """
    X, Z = np.meshgrid(np.linspace(-2, 2, res), np.linspace(-2, 2, res))
    Y2 = X**3 + a * X * Z**2 + b * Z**3
    real_mask = Y2 >= 0  # Only keep real solutions
    Y = np.zeros_like(Y2)
    Y[real_mask] = np.sqrt(Y2[real_mask])
    return X, Y, Z

def intersect_with_sphere(X, Y, Z):
    """
    Finds the intersection points of the elliptic curve surface with the unit sphere.
    """
    norm = np.sqrt(X**2 + Y**2 + Z**2)
    mask = np.abs(norm - 1) < 0.05  # Approximate intersection within a small threshold
    return X[mask], Y[mask], Z[mask]

def plot_elliptic_curve_intersection(a, b):
    """
    Plots the intersection of the elliptic curve surface with the unit sphere.
    """
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    
    # Generate elliptic curve surface and intersect with sphere
    X, Y, Z = elliptic_curve_surface(a, b)
    Xs, Ys, Zs = intersect_with_sphere(X, Y, Z)
    
    # Plot unit sphere
    u, v = np.mgrid[0:2*np.pi:50j, 0:np.pi:25j]
    sphere_x = np.cos(u) * np.sin(v)
    sphere_y = np.sin(u) * np.sin(v)
    sphere_z = np.cos(v)
    ax.plot_wireframe(sphere_x, sphere_y, sphere_z, color='gray', alpha=0.3)
    
    # Plot intersection curve
    ax.scatter(Xs, Ys, Zs, color='b', label='Intersection Curve')
    
    ax.set_xlabel("X")
    ax.set_ylabel("Y")
    ax.set_zlabel("Z")
    ax.legend()
    plt.show()

# Example parameters for the elliptic curve
a, b = -2, 1
plot_elliptic_curve_intersection(a, b)
visual:

图片
上次由 TheMatrix 在 2025年 2月 9日 13:40 修改。
原因: 未提供修改原因

标签/Tags:
头像
verdelite(众傻之傻)
论坛元老
论坛元老
帖子互动: 926
帖子: 22831
注册时间: 2022年 7月 21日 23:33

#2 Re: ChatGPT generated Python code

帖子 verdelite(众傻之傻) »

这看着画得不对。



我让deepseek试了试,把它CPU干冒烟了。思考了一大堆,我就不写出来了,只写出code。试了几个online python, 都是说没有matplotlib. code在此。

问:
write python code. The code treats a 2-D elliptic curve as the vertical surface in 3-D space, then intersect the surface with the surface of a sphere. Plot the intersection curve. Thanks!

答:(解说就去掉了)

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

def find_x_upper(r):
target = r**2 + 7 # Since z² = r² - x³ -x² +7, x³ +x² = r² +7 at upper limit
x = 3.0 # Initial guess above the root
tolerance = 1e-6
for _ in range(100):
f = x**3 + x**2 - target
if abs(f) < tolerance:
break
df_dx = 3 * x**2 + 2 * x
x -= f / df_dx
return x

# Parameters for the elliptic curve y² = x³ + a x + b
a = 0
b = -7
r = 5 # Radius of the sphere

# Calculate valid x range
x_lower = np.cbrt(7) # Cube root of 7 for lower bound
x_upper = find_x_upper(r)

# Generate x values within the valid range
x = np.linspace(x_lower, x_upper, 1000)

# Compute y values (positive and negative)
y_squared = x**3 + a * x + b
valid_indices = y_squared >= 0 # Ensure y is real (theoretically all should be valid)
x = x[valid_indices]
y_squared = y_squared[valid_indices]

y = np.sqrt(y_squared)
y_neg = -y

# Compute z values (positive and negative)
z_squared = r**2 - x**3 - a * x - b - x**2
valid_z = z_squared >= 0 # Ensure z is real (should be valid due to x_upper)
x = x[valid_z]
y = y[valid_z]
y_neg = y_neg[valid_z]
z_squared = z_squared[valid_z]

z = np.sqrt(z_squared)
z_neg = -z

# Create 3D plot
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')

# Plot all four intersection curves
ax.plot(x, y, z, 'r', label='y>0, z>0')
ax.plot(x, y, z_neg, 'g', label='y>0, z<0')
ax.plot(x, y_neg, z, 'b', label='y<0, z>0')
ax.plot(x, y_neg, z_neg, 'm', label='y<0, z<0')

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.legend()

plt.title('Intersection of Elliptic Curve Surface and Sphere')
plt.show()
没有光子;也没有量子能级,量子跃迁,量子叠加,量子塌缩和量子纠缠。
头像
TheMatrix楼主
论坛支柱
论坛支柱
2024年度优秀版主
TheMatrix 的博客
帖子互动: 264
帖子: 13265
注册时间: 2022年 7月 26日 00:35

#3 Re: ChatGPT generated Python code

帖子 TheMatrix楼主 »

verdelite 写了: 2025年 2月 8日 14:28 这看着画得不对。



我让deepseek试了试,把它CPU干冒烟了。思考了一大堆,我就不写出来了,只写出code。试了几个online python, 都是说没有matplotlib. code在此。

问:
write python code. The code treats a 2-D elliptic curve as the vertical surface in 3-D space, then intersect the surface with the surface of a sphere. Plot the intersection curve. Thanks!

答:(解说就去掉了)

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

def find_x_upper(r):
target = r**2 + 7 # Since z² = r² - x³ -x² +7, x³ +x² = r² +7 at upper limit
x = 3.0 # Initial guess above the root
tolerance = 1e-6
for _ in range(100):
f = x**3 + x**2 - target
if abs(f) < tolerance:
break
df_dx = 3 * x**2 + 2 * x
x -= f / df_dx
return x

# Parameters for the elliptic curve y² = x³ + a x + b
a = 0
b = -7
r = 5 # Radius of the sphere

# Calculate valid x range
x_lower = np.cbrt(7) # Cube root of 7 for lower bound
x_upper = find_x_upper(r)

# Generate x values within the valid range
x = np.linspace(x_lower, x_upper, 1000)

# Compute y values (positive and negative)
y_squared = x**3 + a * x + b
valid_indices = y_squared >= 0 # Ensure y is real (theoretically all should be valid)
x = x[valid_indices]
y_squared = y_squared[valid_indices]

y = np.sqrt(y_squared)
y_neg = -y

# Compute z values (positive and negative)
z_squared = r**2 - x**3 - a * x - b - x**2
valid_z = z_squared >= 0 # Ensure z is real (should be valid due to x_upper)
x = x[valid_z]
y = y[valid_z]
y_neg = y_neg[valid_z]
z_squared = z_squared[valid_z]

z = np.sqrt(z_squared)
z_neg = -z

# Create 3D plot
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')

# Plot all four intersection curves
ax.plot(x, y, z, 'r', label='y>0, z>0')
ax.plot(x, y, z_neg, 'g', label='y>0, z<0')
ax.plot(x, y_neg, z, 'b', label='y<0, z>0')
ax.plot(x, y_neg, z_neg, 'm', label='y<0, z<0')

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.legend()

plt.title('Intersection of Elliptic Curve Surface and Sphere')
plt.show()


漂亮。这正是我想要的:

图片

format 一下程序:

代码: 全选

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

def find_x_upper(r):
    target = r**2 + 7 # Since z² = r² - x³ -x² +7, x³ +x² = r² +7 at upper limit
    x = 3.0 # Initial guess above the root
    tolerance = 1e-6
    for _ in range(100):
        f = x**3 + x**2 - target
        if abs(f) < tolerance:
            break
    df_dx = 3 * x**2 + 2 * x
    x -= f / df_dx
    return x

# Parameters for the elliptic curve y² = x³ + a x + b
a = 0
b = -7
r = 5 # Radius of the sphere

# Calculate valid x range
x_lower = np.cbrt(7) # Cube root of 7 for lower bound
x_upper = find_x_upper(r)

# Generate x values within the valid range
x = np.linspace(x_lower, x_upper, 1000)

# Compute y values (positive and negative)
y_squared = x**3 + a * x + b
valid_indices = y_squared >= 0 # Ensure y is real (theoretically all should be valid)
x = x[valid_indices]
y_squared = y_squared[valid_indices]

y = np.sqrt(y_squared)
y_neg = -y

# Compute z values (positive and negative)
z_squared = r**2 - x**3 - a * x - b - x**2
valid_z = z_squared >= 0 # Ensure z is real (should be valid due to x_upper)
x = x[valid_z]
y = y[valid_z]
y_neg = y_neg[valid_z]
z_squared = z_squared[valid_z]

z = np.sqrt(z_squared)
z_neg = -z

# Create 3D plot
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')

# Plot all four intersection curves
ax.plot(x, y, z, 'r', label='y>0, z>0')
ax.plot(x, y, z_neg, 'g', label='y>0, z<0')
ax.plot(x, y_neg, z, 'b', label='y<0, z>0')
ax.plot(x, y_neg, z_neg, 'm', label='y<0, z<0')

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.legend()

plt.title('Intersection of Elliptic Curve Surface and Sphere')
plt.show()

上次由 TheMatrix 在 2025年 2月 8日 15:41 修改。
原因: 未提供修改原因
头像
VladPutin(清风不识字何故乱翻书)
论坛支柱
论坛支柱
VladPutin 的博客
帖子互动: 1396
帖子: 12266
注册时间: 2022年 7月 24日 11:12

#4 Re: ChatGPT generated Python code

帖子 VladPutin(清风不识字何故乱翻书) »

你不会自己写代码?
x1 图片
图片
头像
huangchong(净坛使者)
论坛元老
论坛元老
2023-24年度优秀版主
帖子互动: 3683
帖子: 57276
注册时间: 2022年 7月 22日 01:22

#5 Re: ChatGPT generated Python code

帖子 huangchong(净坛使者) »

VladPutin 写了: 2025年 2月 8日 15:42 你不会自己写代码?
画图code很多都是加颜色 加风格 加注释这种无聊东西,花时间看文档不值得,自己写很容易一写一大坨,确实应该让ai来干
头像
YouHi
论坛元老
论坛元老
YouHi 的博客
帖子互动: 2623
帖子: 36060
注册时间: 2022年 7月 22日 22:36

#6 Re: ChatGPT generated Python code

帖子 YouHi »

huangchong 写了: 2025年 2月 8日 15:48 画图code很多都是加颜色 加风格 加注释这种无聊东西,花时间看文档不值得,自己写很容易一写一大坨,确实应该让ai来干
huangchong是高级干部啊
著名网友名单
🇺🇸 NC CHINESE AMERICANS FOR TRUMP 🛩️
你也是Trump U毕业的吗???
头像
huangchong(净坛使者)
论坛元老
论坛元老
2023-24年度优秀版主
帖子互动: 3683
帖子: 57276
注册时间: 2022年 7月 22日 01:22

#7 Re: ChatGPT generated Python code

帖子 huangchong(净坛使者) »

YouHi 写了: 2025年 2月 8日 15:50 huangchong是高级干部啊
以后ai就是我们的秘书 可惜都是行政秘书
头像
TheMatrix楼主
论坛支柱
论坛支柱
2024年度优秀版主
TheMatrix 的博客
帖子互动: 264
帖子: 13265
注册时间: 2022年 7月 26日 00:35

#8 Re: ChatGPT generated Python code

帖子 TheMatrix楼主 »

我前面问GPT画的是y2=x3-2x+1,对应a=-2, b=1。
DeepSeek这个程序要改一下a和b。而且它计算x的lower bound和upper bound好像不太对,我把它hardcode起来。

得到的图形是这样:

图片

程序改了一点点:

代码: 全选

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

def find_x_upper(r):
    target = r**2 + 7 # Since z² = r² - x³ -x² +7, x³ +x² = r² +7 at upper limit
    x = 3.0 # Initial guess above the root
    tolerance = 1e-6
    for _ in range(100):
        f = x**3 + x**2 - target
        if abs(f) < tolerance:
            break
    df_dx = 3 * x**2 + 2 * x
    x -= f / df_dx
    return x

# Parameters for the elliptic curve y² = x³ + a x + b
a = 0
b = -7

a=-2
b=1
r = 5 # Radius of the sphere

# Calculate valid x range
x_lower = np.cbrt(7) # Cube root of 7 for lower bound
x_upper = find_x_upper(r)

x_lower = 1
x_upper = 6


# Generate x values within the valid range
x = np.linspace(x_lower, x_upper, 1000)

# Compute y values (positive and negative)
y_squared = x**3 + a * x + b
valid_indices = y_squared >= 0 # Ensure y is real (theoretically all should be valid)
x = x[valid_indices]
y_squared = y_squared[valid_indices]

y = np.sqrt(y_squared)
y_neg = -y

# Compute z values (positive and negative)
z_squared = r**2 - x**3 - a * x - b - x**2
valid_z = z_squared >= 0 # Ensure z is real (should be valid due to x_upper)
x = x[valid_z]
y = y[valid_z]
y_neg = y_neg[valid_z]
z_squared = z_squared[valid_z]

z = np.sqrt(z_squared)
z_neg = -z

# Create 3D plot
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')

# Plot all four intersection curves
ax.plot(x, y, z, 'r', label='y>0, z>0')
ax.plot(x, y, z_neg, 'g', label='y>0, z<0')
ax.plot(x, y_neg, z, 'b', label='y<0, z>0')
ax.plot(x, y_neg, z_neg, 'm', label='y<0, z<0')

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.legend()

plt.title('Intersection of Elliptic Curve Surface and Sphere')
plt.show()

头像
TheMatrix楼主
论坛支柱
论坛支柱
2024年度优秀版主
TheMatrix 的博客
帖子互动: 264
帖子: 13265
注册时间: 2022年 7月 26日 00:35

#9 Re: ChatGPT generated Python code

帖子 TheMatrix楼主 »

VladPutin 写了: 2025年 2月 8日 15:42 你不会自己写代码?
这个代码并不容易写。你自己写写就知道了。
头像
TheMatrix楼主
论坛支柱
论坛支柱
2024年度优秀版主
TheMatrix 的博客
帖子互动: 264
帖子: 13265
注册时间: 2022年 7月 26日 00:35

#10 Re: ChatGPT generated Python code

帖子 TheMatrix楼主 »

ChatGPT的思考过程我可以理解。

deepseek的思考过程我感觉很玄妙。它是自己想出来的步骤,还是在网上找到的代码?
头像
TheMatrix楼主
论坛支柱
论坛支柱
2024年度优秀版主
TheMatrix 的博客
帖子互动: 264
帖子: 13265
注册时间: 2022年 7月 26日 00:35

#11 Re: ChatGPT generated Python code

帖子 TheMatrix楼主 »

TheMatrix 写了: 2025年 2月 8日 16:08 ChatGPT的思考过程我可以理解。

deepseek的思考过程我感觉很玄妙。它是自己想出来的步骤,还是在网上找到的代码?
deepseek这个程序好像不太对啊。

它就是把这两个方程联立:
y2=x3-2x+1
x2+y2+z2=r2

r=5
给定一组x,求出相应的y和z。

而我要的应该是这样:
y2z=x3-2xz2+z3
x2+y2+z2=r2

第一个方程要变成齐次方程。
头像
TheMatrix楼主
论坛支柱
论坛支柱
2024年度优秀版主
TheMatrix 的博客
帖子互动: 264
帖子: 13265
注册时间: 2022年 7月 26日 00:35

#12 Re: ChatGPT generated Python code

帖子 TheMatrix楼主 »

ChatGPT的思路很清晰。但是elliptic方程齐次化的代码也是错的。它的注释是对的,但是代码错了。
def elliptic_curve_surface(a, b, res=100):
"""
Generates points on the elliptic curve surface Y^2Z = X^3 + aXZ^2 + bZ^3 in projective space.
"""
X, Z = np.meshgrid(np.linspace(-2, 2, res), np.linspace(-2, 2, res))
Y2 = X**3 + a * X * Z**2 + b * Z**3
real_mask = Y2 >= 0 # Only keep real solutions
Y = np.zeros_like(Y2)
Y[real_mask] = np.sqrt(Y2[real_mask])
return X, Y, Z
上次由 TheMatrix 在 2025年 2月 8日 16:32 修改。
原因: 未提供修改原因
头像
TheMatrix楼主
论坛支柱
论坛支柱
2024年度优秀版主
TheMatrix 的博客
帖子互动: 264
帖子: 13265
注册时间: 2022年 7月 26日 00:35

#13 Re: ChatGPT generated Python code

帖子 TheMatrix楼主 »

TheMatrix 写了: 2025年 2月 8日 16:31 ChatGPT的思路很清晰。但是elliptic方程齐次化的代码也是错的。它的注释是对的,但是代码错了。
ChatGPT这个改一点点就可以:
def elliptic_curve_surface(a, b, res=100):
"""
Generates points on the elliptic curve surface Y^2Z = X^3 + aXZ^2 + bZ^3 in projective space.
"""
X, Z = np.meshgrid(np.linspace(-2, 2, res), np.linspace(-2, 2, res))
Y2 = (X**3 + a * X * Z**2 + b * Z**3)/Z
real_mask = Y2 >= 0 # Only keep real solutions
Y = np.zeros_like(Y2)
Y[real_mask] = np.sqrt(Y2[real_mask])
return X, Y, Z
结果是这样:

图片
头像
TheMatrix楼主
论坛支柱
论坛支柱
2024年度优秀版主
TheMatrix 的博客
帖子互动: 264
帖子: 13265
注册时间: 2022年 7月 26日 00:35

#14 Re: ChatGPT generated Python code

帖子 TheMatrix楼主 »

TheMatrix 写了: 2025年 2月 8日 16:38 ChatGPT这个改一点点就可以:

结果是这样:

图片
还是很ugly。至少它是对的。

deepseek那个不对。不知道怎么补救。
wass
论坛精英
论坛精英
2024年度优秀版主
wass 的博客
帖子互动: 695
帖子: 7097
注册时间: 2022年 7月 23日 22:13

#15 Re: ChatGPT generated Python code

帖子 wass »

TheMatrix 写了: 2025年 2月 8日 16:05 这个代码并不容易写。你自己写写就知道了。
不是难,而是费时间
头像
TheMatrix楼主
论坛支柱
论坛支柱
2024年度优秀版主
TheMatrix 的博客
帖子互动: 264
帖子: 13265
注册时间: 2022年 7月 26日 00:35

#16 Re: ChatGPT generated Python code

帖子 TheMatrix楼主 »

wass 写了: 2025年 2月 8日 20:31 不是难,而是费时间
没那么简单。

你要是觉得只是费时间的话,你可以让AI帮你写啊。

scatter plot容易写。要写出smooth curve的不容易。至少我还没想出怎么让AI帮我写出来。
pinfish
论坛支柱
论坛支柱
帖子互动: 462
帖子: 10469
注册时间: 2022年 7月 23日 23:56

#17 Re: ChatGPT generated Python code

帖子 pinfish »

缩进是空格还是tab?
TheMatrix 写了: 2025年 2月 8日 13:26 to visualize an elliptic curve in a projective space.

final prompt:

图片

generated code:

代码: 全选

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

def elliptic_curve_surface(a, b, res=100):
    """
    Generates points on the elliptic curve surface Y^2Z = X^3 + aXZ^2 + bZ^3 in projective space.
    """
    X, Z = np.meshgrid(np.linspace(-2, 2, res), np.linspace(-2, 2, res))
    Y2 = X**3 + a * X * Z**2 + b * Z**3
    real_mask = Y2 >= 0  # Only keep real solutions
    Y = np.zeros_like(Y2)
    Y[real_mask] = np.sqrt(Y2[real_mask])
    return X, Y, Z

def intersect_with_sphere(X, Y, Z):
    """
    Finds the intersection points of the elliptic curve surface with the unit sphere.
    """
    norm = np.sqrt(X**2 + Y**2 + Z**2)
    mask = np.abs(norm - 1) < 0.05  # Approximate intersection within a small threshold
    return X[mask], Y[mask], Z[mask]

def plot_elliptic_curve_intersection(a, b):
    """
    Plots the intersection of the elliptic curve surface with the unit sphere.
    """
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    
    # Generate elliptic curve surface and intersect with sphere
    X, Y, Z = elliptic_curve_surface(a, b)
    Xs, Ys, Zs = intersect_with_sphere(X, Y, Z)
    
    # Plot unit sphere
    u, v = np.mgrid[0:2*np.pi:50j, 0:np.pi:25j]
    sphere_x = np.cos(u) * np.sin(v)
    sphere_y = np.sin(u) * np.sin(v)
    sphere_z = np.cos(v)
    ax.plot_wireframe(sphere_x, sphere_y, sphere_z, color='gray', alpha=0.3)
    
    # Plot intersection curve
    ax.scatter(Xs, Ys, Zs, color='b', label='Intersection Curve')
    
    ax.set_xlabel("X")
    ax.set_ylabel("Y")
    ax.set_zlabel("Z")
    ax.legend()
    plt.show()

# Example parameters for the elliptic curve
a, b = -2, 1
plot_elliptic_curve_intersection(a, b)
visual:

图片
头像
OckhamT1(奥卡姆剃刀)
论坛精英
论坛精英
帖子互动: 435
帖子: 6199
注册时间: 2022年 11月 25日 12:25

#18 Re: ChatGPT generated Python code

帖子 OckhamT1(奥卡姆剃刀) »

YouHi 写了: 2025年 2月 8日 15:50 huangchong是高级干部啊
蝗虫计算机水平很可以的,可惜是个男的。。。
头像
TheMatrix楼主
论坛支柱
论坛支柱
2024年度优秀版主
TheMatrix 的博客
帖子互动: 264
帖子: 13265
注册时间: 2022年 7月 26日 00:35

#19 Re: ChatGPT generated Python code

帖子 TheMatrix楼主 »

又上deepseek试了一下。

直接给它明确的要求:
Please write a python code to visualize an elliptic curve in the projective space. I want you to first treat the elliptic curve as a surface by converting the elliptic curve to a homogeneous equation, then intersect the surface with the surface of a unit sphere, the resulting intersection curve will be the desired visualization.
deepseek给回我这个代码。和ChatGPT基本是一样的。也是scatter plot。

代码: 全选

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# Define the elliptic curve parameters
a = -1
b = 1

a,b = -2,1

# Define the homogeneous equation of the elliptic curve
def elliptic_curve(x, y, z):
    return y**2 * z - (x**3 + a * x * z**2 + b * z**3)

# Define the unit sphere equation
def unit_sphere(x, y, z):
    return x**2 + y**2 + z**2 - 1

# Create a grid of points in 3D space
x = np.linspace(-2, 2, 100)
y = np.linspace(-2, 2, 100)
z = np.linspace(-2, 2, 100)
X, Y, Z = np.meshgrid(x, y, z)

# Evaluate the elliptic curve and unit sphere equations on the grid
E = elliptic_curve(X, Y, Z)
S = unit_sphere(X, Y, Z)

# Find the intersection (where both equations are zero)
intersection = (np.abs(E) < 0.1) & (np.abs(S) < 0.1)

# Extract the points of intersection
x_intersect = X[intersection]
y_intersect = Y[intersection]
z_intersect = Z[intersection]

# Plot the intersection curve
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x_intersect, y_intersect, z_intersect, c='r', s=1)

# Set labels and title
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title('Intersection of Elliptic Curve and Unit Sphere')

# Show the plot
plt.show()
图片是这样,还是不错的:

图片

但是scatter plot还是不漂亮。我让它画一个smooth curve出来。

它给回我这个:

代码: 全选

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from scipy.optimize import root
from scipy.interpolate import splprep, splev

# Define the elliptic curve parameters
a = -1
b = 1

a,b = -2,1

# Define the homogeneous equation of the elliptic curve
def elliptic_curve(x, y, z):
    return y**2 * z - (x**3 + a * x * z**2 + b * z**3)

# Define the unit sphere equation
def unit_sphere(x, y, z):
    return x**2 + y**2 + z**2 - 1

# Function to find the intersection point for a given z
def find_intersection(z_val):
    def equations(vars):
        x, y = vars
        return [elliptic_curve(x, y, z_val), unit_sphere(x, y, z_val)]
    
    # Initial guess (random or based on z_val)
    initial_guess = [0.5, 0.5]
    sol = root(equations, initial_guess, method='lm')  # Levenberg-Marquardt method
    if sol.success:
        return sol.x[0], sol.x[1], z_val
    else:
        return None

# Generate a range of z values
z_values = np.linspace(-1, 1, 400)

# Find intersection points for each z
intersection_points = []
for z in z_values:
    point = find_intersection(z)
    if point is not None:
        intersection_points.append(point)

# Convert to numpy array
intersection_points = np.array(intersection_points)

# Sort points for smooth plotting
intersection_points = intersection_points[np.argsort(intersection_points[:, 2])]

# Spline interpolation for smooth curve
tck, u = splprep([intersection_points[:, 0], intersection_points[:, 1], intersection_points[:, 2]], s=0)
u_new = np.linspace(0, 1, 1000)
x_new, y_new, z_new = splev(u_new, tck)

# Plot the smooth intersection curve
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot(x_new, y_new, z_new, 'r-', lw=2, label='Intersection Curve')

# Set labels and title
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title('Smooth Intersection of Elliptic Curve and Unit Sphere')
ax.legend()

# Show the plot
plt.show()
图片是这样,还不如scatter plot:

图片
头像
TheMatrix楼主
论坛支柱
论坛支柱
2024年度优秀版主
TheMatrix 的博客
帖子互动: 264
帖子: 13265
注册时间: 2022年 7月 26日 00:35

#20 Re: ChatGPT generated Python code

帖子 TheMatrix楼主 »

pinfish 写了: 2025年 2月 8日 21:10 缩进是空格还是tab?
是空格。
回复

回到 “STEM”