奇怪,这个函数图像,怎么画不出来,报异常?

jason0616 2024-09-30 13:33:38
import numpy as np 
 import matplotlib.pyplot as plt 
   
 # 将16进制字符串转为整数 
 a_hex = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0000000000000000FFFFFFFC"
 b_hex = "28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93"
 # a_hex = "FF" 
 # b_hex = "28" 
 # 转换为整数 
 a = int(a_hex, 16) 
 b = int(b_hex, 16) 
 # a=10 
 # b=5 
 # 设定 x 的范围 
 #x_values = np.linspace(-2**32, 2**32, 1000)  # 选择合适的范围 
 #x_values =  np.linspace(-2000, 2000,  250) 
 x_values = np.linspace(-10, 200, 500) 
 # 计算 y^2 = x^3 + ax + b 
 y_squared = x_values**3 + a * x_values + b 
 print(y_squared) 
 # 计算 y,去掉负值和无效值 
 # y_positive = np.where(y_squared >= 0, np.sqrt(y_squared), np.nan) 
 # y_negative = np.where(y_squared >= 0, -np.sqrt(y_squared), np.nan) 
 y_positive = np.sqrt(np.maximum(0, y_squared)) 
 y_negative = -y_positive 
 # 绘制椭圆曲线 
 plt.figure(figsize=(10, 10)) 
 plt.plot(x_values, y_positive, label=r'$y^2 = x^3 + ax + b, color='blue') 
 plt.plot(x_values, y_negative, label='y = -√(x? + ax + b)',color='red') 
   
 # 设置图的范围 
 plt.xlim(-10, 200) 
 plt.ylim(-10**42, 10**42) 
 # plt.ylim(-30, 30)  # 限制 y 轴的范围 
 # plt.xlim(-20, 20)  # 限制 x 轴的范围 
 plt.axhline(0, color='black', linewidth=0.5, linestyle='--') 
 plt.axvline(0, color='black', linewidth=0.5, linestyle='--') 
 plt.grid() 
 plt.title(r'Elliptic Curve: $y^2 = x^3 + ' + hex(a)[2:] + 'x + ' + hex(b)[2:] + ') 
 plt.xlabel('x') 
 plt.ylabel('y') 
 plt.legend() 
 plt.show()

有时候报:

AttributeError: 'float' object has no attribute 'sqrt'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "D:\myProgram\pythonProgram\myfindprime\测试一下\国密sm2\官方-用官方的a和b.py", line 28, in <module>
    y_positive = np.where(y_squared > 0, np.sqrt(y_squared), np.nan)
TypeError: loop of ufunc does not support argument 0 of type float which has no callable sqrt method

有时候报:

报异常:AttributeError: 'int' object has no attribute 'sqrt'
  
The above exception was the direct cause of the following exception:
  
Traceback (most recent call last):
   File "D:\myProgram\pythonProgram\myfindprime\测试一下\国密sm2\官方-用官方的a和b.py", line 24, in <module>
     y_positive = np.sqrt(np.maximum(0, y_squared))
TypeError: loop of ufunc does not support argument 0 of type int which has no callable sqrt method
如果把a和b设小一点(比如a=10,b=5),就可以画出来。这个异常莫名其妙,chatgpt也解决不了。盼大牛看看,谢谢

...全文
105 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

6,234

社区成员

发帖
与我相关
我的任务
社区描述
人生苦短,我用python
社区管理员
  • Python 学习者
  • 嗨学编程
  • 松鼠爱吃饼干
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

欢迎来到Python学习者们的社区,

 

本社区分享你需要的文章、问题解答、技术互助、学习资源、面试系列等等

 

欢迎你的加入,祝你学有所成~

试试用AI创作助手写篇文章吧