在 Python 中处理非常大的数字时如何处理双标量溢出?

问题描述 投票:0回答:1

我正在处理涉及非常大的数字的计算的数据,并且遇到了数字溢出和绘图的问题。当我增加参数𝑁(在代码开头)的值时,问题就出现了。

例如,当 𝑁=5 时,我的代码运行完美,没有任何错误,并且生成了曲线。然而,当我逐渐增加 𝑁 时,部分绘制的曲线开始消失。当 𝑁 达到 20 时,曲线完全消失。问题是我应该使用 N=1000 或 2000 的值。

出现以下错误 运行时警告:double_scalars 中遇到溢出,

这是Python代码:

import numpy as np
import cmath
import math
from math import e
import matplotlib.pyplot as plt
import mpmath
from scipy.special import gamma, factorial
import scipy.special as sc

N = 5 # VARIABLE OF INTEREST (SHOULD BE INCREASED)



g1, g2= 1.31928e-06, 4.947323e-07

H, ms = 1/6, 6

x_axis_points = np.arange(0, 40, 1)
E1 = 10.0 ** ((x_axis_points-30) / 10.0)/1e-11
y_axis_points = []
for QS in E1:
 
    A, B = N * 0.979405**2, N * 0.07986762

    w= (B/A)**2*(A**2/B)*(A**2/B+1)
    r= (B/A)**4*(A**2/B)*(A**2/B+1)*(A**2/B+2)*(A**2/B+3)


    f= w**2/ (r-w**2)
    c= g2**2 * QS*(r-w**2)/w
  


    BB=H *QS * g1**2
    L1, L2= 1/BB, 1/c


################### THE PROBLEM APPEARS IN THE COMMING LINES
#(GIVES INFINITY WHEN INCREASING N) ##############

    cons1= L1**(N*ms)/ sc.gamma(N*ms)  
    cons2= L2**(f)/ sc.gamma(f  )    

    sup3_num= L2**(f)*sc.gamma(N*ms+1   +  f)
    sup3_den= (N*ms+1)*( L2 + L1)**( N*ms+1 +f)               
    sup3=mpmath.hyp2f1(1, N*ms+1 + f, N*ms+1+1, L1/(L1+L2))

    sup4_num= L1**(N*ms)*sc.gamma(f+1   +  N*ms)
    sup4_den= (f+1)*( L2 + L1)**( f+1 +N*ms)                   
    sup4=mpmath.hyp2f1(1, f+1 + N*ms, f+1+1, L2/(L1+L2))



    Z1= cons1* sup3_num/sup3_den * sup3/ sc.gamma(f)

    Z2= cons2* sup4_num/sup4_den * sup4/ sc.gamma(N*ms)   
  
    y = math.log2(1+ Z1+Z2 ) 

    y_axis_points.append((y))            
plt.plot(x_axis_points, y_axis_points,    linewidth='2.5', color='blue')

plt.xlabel('x')
plt.ylabel('y')
plt.grid()

plt.xlim([0, 40])
plt.ylim([0 , 3 ])
plt.show()
python numpy floating-point bigint largenumber
1个回答
0
投票

鉴于您需要处理大于最大可能浮点数的数字,我建议使用 mpmath 进行更多计算。

示例:

import numpy as np
import cmath
import math
from math import e
import matplotlib.pyplot as plt
import mpmath
from scipy.special import gamma, factorial
import scipy.special as sc

N = 2000 # VARIABLE OF INTEREST (SHOULD BE INCREASED)



g1, g2= 1.31928e-06, 4.947323e-07

H, ms = 1/6, 6

x_axis_points = np.arange(0, 40, 1)
E1 = 10.0 ** ((x_axis_points-30) / 10.0)/1e-11
y_axis_points = []
for QS in E1:
 
    A, B = N * 0.979405**2, N * 0.07986762

    w= (B/A)**2*(A**2/B)*(A**2/B+1)
    r= (B/A)**4*(A**2/B)*(A**2/B+1)*(A**2/B+2)*(A**2/B+3)


    f= w**2/ (r-w**2)
    c= g2**2 * QS*(r-w**2)/w
  


    BB=H *QS * g1**2
    L1, L2= 1/BB, 1/c


################### THE PROBLEM APPEARS IN THE COMMING LINES
#(GIVES INFINITY WHEN INCREASING N) ##############

    cons1= mpmath.power(L1, (N*ms))/ mpmath.gamma(N*ms)
    cons2= mpmath.power(L2, (f))/ mpmath.gamma(f)

    sup3_num= mpmath.power(L2, (f))*mpmath.gamma(N*ms+1   +  f)
    sup3_den= (N*ms+1)*mpmath.power(( L2 + L1), ( N*ms+1 +f))
    sup3=mpmath.hyp2f1(1, N*ms+1 + f, N*ms+1+1, L1/(L1+L2))

    sup4_num= mpmath.power(L1, (N*ms))*mpmath.gamma(f+1   +  N*ms)
    sup4_den= (f+1)*mpmath.power(( L2 + L1), ( f+1 +N*ms))
    sup4=mpmath.hyp2f1(1, f+1 + N*ms, f+1+1, L2/(L1+L2))



    Z1= cons1* sup3_num / sup3_den * sup3 / mpmath.gamma(f)

    Z2= cons2* sup4_num / sup4_den * sup4 / mpmath.gamma(N*ms)   
  
    y = mpmath.log1p(Z1 + Z2) / mpmath.log(2)

    y_axis_points.append(float(y))            
plt.plot(x_axis_points, y_axis_points,    linewidth='2.5', color='blue')

plt.xlabel('x')
plt.ylabel('y')
plt.grid()

plt.xlim([0, 40])
# plt.ylim([0 , 3 ])
plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.