Python Cython魔术功能不起作用

时间:2017-01-02 04:24:25

标签: python cython jupyter spyder

我有3个关于我的Python代码的问题(我对它很新) 我正在尝试从我正在使用的Python代码中构建一个Cython代码,以使其更快地运行(希望如此)。我尝试按照制作setup.py文件的方式进行操作,直到我运行我运行的文件(保存为run_practice.py),我才设法完成它:

 import pyximport; pyximport.install()
 import practice.pyx
终端中的

并在终端中收到一条错误,它无法找到“3d”投影类型,因为我的代码在3d图中绘制了值。

我搜索了很多来解决这个问题,但实际上并没有任何关于它的问题。我的问题的一部分是如何解决这个问题。

所以我遇到了cythonmagic方法。我读到cythonmagic现在是Cython包的一部分,并相应地调整了我的代码。我有当前版本的Anaconda以及当前版本的Jupyter以及Anaconda提供的其他所有功能。我将我正在运行的Python代码复制并粘贴到Jupyter笔记本中并运行它,但问题是: 我用

启动了代码
%load_ext Cython
%%cython

然后粘贴我的代码。最后我得到一个错误,上面写着"文件"",第6行     %%用Cython     ^ SyntaxError:语法无效

这没有任何意义,因为它在加载Cython扩展时没有任何问题,当我在第二行输入'%cython'时,它建议我键入'%% cython',它显得神奇函数可以从错误消息中读取'(错误:root:行魔术函数%cython未找到(但是单元格魔术%%cython存在,你的意思是代替吗?))' ,并得到此语法错误。另外我之前提到我有一个从我的代码生成的3d图,当我在Jupyter笔记本中运行它时,它不会出现在任何地方,但在Spyder环境中工作正常。 这两个问题是我问题的B部分。

这是我的总代码。嵌套的for循环显然是代码的拐杖。只是一点基础......'Fraction'变量告诉我们在for循环中迭代'theta'和'beta'的准确度。在分数= 1时,运行时间为0.1499929428100586秒,但在分数= .1时,运行时间约为13秒(较小的“分数”,准确度较高)。我需要代码有效地运行到至少10位小数(分数= .0000000001或其他东西),这将完全冻结我的计算机(即使在分数= 0.005我的计算机冻结!)。任何有关优化这一点的帮助都会有很大帮助!!非常感谢!

也不要担心校正因子术语是否是一个线空间数组...目前正在处理从for循环中迭代的第三个变量追加一个3d数组。

"""
Created on Tue Nov 22 23:13:40 2016
@author: Eric Holmes
"""
# Scattering formulas
# Eric Holmes
# Constants
import math
import numpy as np
import scipy as sc
import matplotlib.pyplot as plt
import pvlib as pv
from mpl_toolkits.mplot3d import Axes3D
import time
fig = plt.figure()
ax = fig.gca(projection='3d')
h = 6.62607004e-34 # Plank Constant m^2*kg/s
c = 299792458 # Speed of light m/s
m = 9.10938356e-31 # Mass kg
r = 2.8179403267e-15
pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273
try:
    InitialElectronVelocity = float(input("Enter Initial Electron Velocity: "))
except ValueError:
    InitialElectronVelocity = 2e8
try:
    InitialPhotonWavelength = float(input("Enter Initial Photon Wavelength: "))
except ValueError:
    InitialPhotonWavelength = 2.426310236092620e-12
try:
    MinALPHAAngleEvaluated = float(input("Enter Min ALPHA Angle Evaluated: ")) 
except ValueError:
    MinALPHAAngleEvaluated = 90
try:
    MaxALPHAAngleEvaluated = float(input("Enter Max ALPHA Angle Evaluated: ")) 
except ValueError:
    MaxALPHAAngleEvaluated = 90
try:
    Fraction = float(input("Enter Fraction: ")) 
except ValueError:
    Fraction = 1
try:
    THETABETAMINANGLEEVALUATED = float(input("Enter THETA/BETA MIN ANGLE EVALUATED: "))
except ValueError:
    THETABETAMINANGLEEVALUATED = 0
try:
    THETABETAMAXANGLEEVALUATED = float(input("Enter THETA/BETA MAX ANGLE EVALUATED: "))
except ValueError:
    THETABETAMAXANGLEEVALUATED = 90
start = time.time()
gamma = 1/(math.sqrt(1-((InitialElectronVelocity**2)/(c**2))))       
Elambda1 = ((h*c)/InitialPhotonWavelength)                        
Eelectron1 = (gamma*m*(c**2))                                        
SCALEDALPHAMinAngle = MinALPHAAngleEvaluated/Fraction                       
SCALEDALPHAMaxAngle = MaxALPHAAngleEvaluated/Fraction                       
JJ = ((SCALEDALPHAMaxAngle - SCALEDALPHAMinAngle)/Fraction)
QQ = (JJ + (1/Fraction))*Fraction                           
CORRECTIONFACTOR1 = ((SCALEDALPHAMinAngle) - (1))                         
if SCALEDALPHAMinAngle == SCALEDALPHAMaxAngle:
    CORRECTIONFACTOR2 = np.linspace(1,1,1)                                                
else:
    CORRECTIONFACTOR2 = np.linspace(SCALEDALPHAMinAngle,SCALEDALPHAMaxAngle,QQ)   
SCALEDTHETABETAMinAngle = THETABETAMINANGLEEVALUATED/Fraction                  
SCALEDTHETABETAMaxAngle = THETABETAMAXANGLEEVALUATED/Fraction                   
JJJ = ((SCALEDTHETABETAMaxAngle - SCALEDTHETABETAMinAngle)/Fraction) 
QQQ = (JJJ + (1/Fraction))*Fraction                        
CORRECTIONFACTOR3 = ((SCALEDTHETABETAMinAngle) - 1)                    
if SCALEDTHETABETAMinAngle == SCALEDTHETABETAMaxAngle:
    CORRECTIONFACTOR4 = np.linspace(1,1,1)
else:                                        
    CORRECTIONFACTOR4 = np.linspace(SCALEDTHETABETAMinAngle,SCALEDTHETABETAMaxAngle,QQQ)
length1 = np.shape(CORRECTIONFACTOR4)
length2 = np.shape(CORRECTIONFACTOR2)
int1 = length1[0] 
int2 = length2[0]
int3 = int1 + 1
int4 = int2 + 1
alpha = 1
matrix1 = []
for theta in range(1,int3):
    i = theta - 1
    matrix1.append([])
    for beta in range(1,int3):
        Elambda2 = ((h*c*(((InitialElectronVelocity*pv.tools.cosd((alpha + CORRECTIONFACTOR1)*Fraction))-c)))/((((h/(m*gamma)))*(pv.tools.cosd((theta + CORRECTIONFACTOR3)*Fraction)-1))-(InitialPhotonWavelength*(c-(InitialElectronVelocity*pv.tools.cosd((beta + CORRECTIONFACTOR3)*Fraction))))))
        DifferentialCrossSection = (((pi*(r**2))/2)*((m**2)/(Elambda1*(Eelectron1**2)))*((((m**4)/(4*(Elambda1**2)*(Eelectron1**2))) * (((Elambda2/(Eelectron1-Elambda2)))**2)) - (((m**2)/(Elambda1*Eelectron1))*((Elambda2/(Eelectron1-Elambda2)))) + ((Eelectron1 - Elambda2)/(Eelectron1)) + ((Eelectron1/(Eelectron1-Elambda2)))))          
        matrix1[i].append(DifferentialCrossSection)     
Y = np.linspace(SCALEDTHETABETAMinAngle,SCALEDTHETABETAMaxAngle,QQQ) 
Z = np.reshape(Y,(int1,1))                 
theta = np.tile(Z,(1,int1))
beta  = np.tile(Y,(int1,1)) 
ax.plot_wireframe((theta*Fraction),(beta*Fraction),matrix1)
matrix2 = np.reshape(matrix1,(1,(int1*int1)))
minimum = np.amin(matrix2)
maximum = np.amax(matrix2)
ax.auto_scale_xyz([THETABETAMINANGLEEVALUATED, THETABETAMAXANGLEEVALUATED], [THETABETAMINANGLEEVALUATED, THETABETAMAXANGLEEVALUATED], [minimum, maximum])   
end = time.time()
print("Code Run Time =",(end - start),"seconds") 

0 个答案:

没有答案