[英]Writing a mathematical formula in python
我正在用python編寫此公式。 請查看圖片我正在使用以下代碼。 它總是會出錯。
import math
import matplotlib
matplotlib.use('TkAgg')
from pylab import *
import cmath
import networkx as nx
import random as rd
import numpy as np
g=nx.karate_club_graph()
for i in g.nodes_iter():
g.node[i]['theta']=1
a=abs((cmath.exp( (g.node[i]['theta']*(1j)) for i in g.nodes_iter()))/g.number_of_nodes())
runfile('C:/Users/Bhawesh/Desktop/workingdirectory/bookpractice.py', wdir='C:/Users/Bhawesh/Desktop/workingdirectory')
Traceback (most recent call last):
File "<ipython-input-6-9fd718b8faeb>", line 1, in <module>
runfile('C:/Users/Bhawesh/Desktop/workingdirectory/bookpractice.py', wdir='C:/Users/Bhawesh/Desktop/workingdirectory')
File "C:\Users\Bhawesh\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "C:\Users\Bhawesh\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 87, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)
File "C:/Users/Bhawesh/Desktop/workingdirectory/bookpractice.py", line 20, in <module>
a=abs((cmath.exp( (g.node[i]['theta']*(1j)) for i in g.nodes_iter()))/g.number_of_nodes())
TypeError: a float is required
我認為問題之一是:
a=abs((cmath.exp( (g.node[i]['theta']*(1j)) for i in g.nodes_iter()))/g.number_of_nodes())
您有1j,這是一個文字,而python不知道如何執行它。
根據您的評論,使用以下命令:
sumValue = sum(cmath.exp(g.node[i]['theta'] * cmath.sqrt(-1)) for i in g.nodes_iter())
r = abs(sumValue/g.number_of_nodes())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.